Package 'StockDistFit'

Title: A Package for Fitting Stock Price Distributions
Description: The `StockDistFit` package provides functions for fitting probability distributions to stock price data. The package uses maximum likelihood estimation to find the best-fitting distribution for a given stock. It also offers a function to fit several distributions to one or more assets and compare the distribution with the Akaike Information Criterion (AIC) and then pick the best distribution.
Authors: Brian Njuguna [aut, cre] , Stanely Sayianka [ctb]
Maintainer: Brian Njuguna <[email protected]>
License: GPL (>= 3)
Version: 1.0.0
Built: 2024-11-17 04:40:23 UTC
Source: https://github.com/wagathu/stockdistfit

Help Index


Apple Inc. stock prices dataset

Description

This dataset contains the daily stock prices of Apple Inc. (AAPL) from January 2, 2013 to April 30, 2023. The data includes the open, high, low, and close prices, as well as the volume and adjusted close price. ~~

Usage

data("AAPL")

Format

A data frame with 2599 observations on the following 7 variables.

Date

a character vector

Open

a numeric vector

High

a numeric vector

Low

a numeric vector

Close

a numeric vector

Volume

a numeric vector

Adjusted

a numeric vector

References

Data source: Yahoo Finance

Examples

data(AAPL)
str(AAPL) ; plot(AAPL)

Amazon.com Inc. Stock Prices Dataset

Description

This dataset contains the daily stock prices of Amazon.com Inc. (AMZN) from January 2, 2013 to April 30, 2023. The data includes the open, high, low, and close prices, as well as the volume and adjusted close price. ~~

Usage

data("AMZN")

Format

A data frame with 2599 observations on the following 7 variables.

Date

a character vector

Open

a numeric vector

High

a numeric vector

Low

a numeric vector

Close

a numeric vector

Volume

a numeric vector

Adjusted

a numeric vector

References

Data source: Yahoo Finance

Examples

data(AMZN)
str(AMZN) ; plot(AMZN)

Compute Annual Returns of a Vector.

Description

This function takes a vector of asset returns and computes annual returns.

Usage

annual_return(vec)

Arguments

vec

a numeric vector of asset returns as an xts object with dates as rownames.

Value

A numeric vector of annual returns.

See Also

weekly_return, monthly_return

Examples

## Not run: 
# Compute annual returns of an asset vector
require(xts)
asset_returns_xts <- xts(c(0.05, -0.03, 0.02, -0.01, 0.04, -0.02, 0.01),
order.by = as.Date(c("2023-05-01", "2023-05-02", "2023-05-03",
"2023-05-04", "2023-05-05", "2023-05-06",
"2023-05-07")))
annual_return(asset_returns_xts)

## End(Not run)

Load Asset Data.

Description

This function reads in asset data stored in .csv format and returns a time-series object of the asset data.

Usage

asset_loader(data_path, assets, price_col)

Arguments

data_path

The path to the directory containing the .csv files.

assets

A vector of asset names to be loaded.

price_col

The name of the price column to be selected (e.g. Open, Close, Low, High).

Value

An xts object with asset data.

Note

The Date column in the files should be of the format "%m/%d/%y", that is 01/14/13 with 01 implying the month, 14 the date and 13 the year

The data to be loaded must be in .csv type and also must have the Date, Open, Low, High and Close Prices of the assest or assests to be loaded.

Examples

## Not run: 
asset_data <- asset_loader("path/to/data/folder", c("AAPL", "MSFT"), "Close")

## End(Not run)

Find the best distribution based on AIC values

Description

This function takes in a data frame of AIC values for different distributions and a vector of distribution names, and returns a data frame with the best distribution for each row based on the minimum AIC value. #' You can also write the distribution as "norm" or "cauchy" provided they follow the order in the data frame.

Usage

best_dist(aic_df, dist_names)

Arguments

aic_df

A data frame containing AIC values for different distributions

dist_names

A vector of distribution names corresponding to the AIC values

Value

A data frame with the best distribution for each row based on the minimum AIC value

Note

This function takes the data frame obtained from fit_multiple_dist function

Examples

## Not run: 
data = asset_loader("path/to/data", c("asset1", "asset2"), "Close")
df = fit_multiple_dist(c("norm_fit", "cauchy_fit"), data)
best_dist(df, c("norm_fit", "cauchy_fit"))

## End(Not run)

Fit Cauchy Distribution to a vector of returns/stock prices.

Description

This function fits the Cauchy distribution to a given data vector using the fitdist function from the fitdistrplus package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

cauchy_fit(vec)

Arguments

vec

a numeric vector containing the data to be fitted.

Value

a list containing the following elements:

par

a numeric vector of length 2 containing the estimated values for the parameters of the fitted distribution: lambda (location) and alpha (scale).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, t_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
cauchy_fit(returns)

## End(Not run)

Compute Cumulative Returns of a Vector.

Description

This function takes a vector of asset returns and computes the cumulative wealth generated over time, assuming that the initial wealth was initial_eq.

Usage

data.cumret(df_ret, initial_eq)

Arguments

df_ret

an xts object of asset returns, with dates as rownames.

initial_eq

a numeric value representing the initial wealth.

Value

An xts object of wealth generated over time.

See Also

weekly_return, monthly_return, annual_return

Examples

## Not run: 
# Compute cumulative returns of an asset vector
library(quantmod)
asset_returns_xts <- xts(c(0.05, -0.03, 0.02, -0.01, 0.04, -0.02, 0.01),
                         order.by = as.Date(c("2023-05-01", "2023-05-02", "2023-05-03",
                                               "2023-05-04", "2023-05-05", "2023-05-06",
                                               "2023-05-07")))
data.cumret(asset_returns_xts, initial_eq = 100)

## End(Not run)

Fits Multiple Probability Distributions to several assets/stock prices.

Description

This function fits multiple probability distributions to a dataframe and calculates the Akaike Information Criterion (AIC) and Bayesian Information Criterion (BIC) for each distribution and then returns a data frame of the AIC values for each asset where the column names are the names of the fitted distributions.

Usage

fit_multiple_dist(dist_names, dataframe)

Arguments

dist_names

a character vector of distribution names to be fitted.

dataframe

a dataframe containing the data to be fitted.

Details

Note that the available distributions are norm_fit - Normal distribution

t_fit - Student's t-distribution

cauchy_fit - Cauchy distribution

ghd_fit - Generalized hyperbolic distribution

hd_fit - Hyperbolic distribution

sym.ghd_fit - Symmetric generalized hyperbolic distribution

sym.hd_fit - Symmetric hyperbolic distribution

vg_fit - Variance-gamma distribution

sym.vg_fit - Symmetric variance-gamma distribution

nig_fit - Normal-inverse Gaussian distribution

ged_fit - Generalized error distribution

skew.t_fit - Skew Student's t-distribution

skew.normal_fit - Skew normal distribution

skew.ged_fit - Skew generalized error distribution

Also note that the distribution to be fitted from the above list must include the '_fit'. The function can also fit one distribution to one asset.

Value

A list of distributions and their corresponding AIC and BIC values.

See Also

asset_loader

Examples

## Not run: 
data = asset_loader("path/to/data/folder", c("asset1", "asset2"), "Close")
fit_multiple_dist(c("norm_fit", "cauchy_fit"), data)

## End(Not run)

Fit Generalized Error Distribution to a vector of returns/stock prices.

Description

This function fits the Generalized Error Distribution (GED) to a given data vector using the ged_fit function from the fGarch package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

ged_fit(vec)

Arguments

vec

A numeric vector of data.

Value

A list with the following elements:

params

A numeric vector of length 3 containing the fitted GED parameters: shape, scale, and location.

aic

The Akaike Information Criterion (AIC) for the fitted model.

bic

The Bayesian Information Criterion (BIC) for the fitted model.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, nig_fit, sym.vg_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
ged_fit(returns)

Fit Generalized Hyperbolic Distribution to a vector of returns/stock prices.

Description

This function fits the Generalized Hyperbolic (GH) distribution to a given data vector using the fit.ghypuv function from the ghyp package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

ghd_fit(vec)

Arguments

vec

a numeric vector containing the data to be fitted.

Value

a list containing the following elements:

par

a numeric vector of length 5 containing the estimated values for the parameters of the fitted distribution: lambda (location), alpha (scale), mu (degrees of freedom), sigma (standard deviation), and gamma (skewness).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, t_fit, cauchy_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
ghd_fit(returns)

## End(Not run)

Alphabet Inc Inc. Stock Prices Dataset

Description

This dataset contains the daily stock prices of Alphabet Inc. (GOOG) from January 2, 2013 to April 30, 2023. The data includes the open, high, low, and close prices, as well as the volume and adjusted close price. ~~

Usage

data("GOOG")

Format

A data frame with 2599 observations on the following 7 variables.

Open

a numeric vector

High

a numeric vector

Low

a numeric vector

Close

a numeric vector

Volume

a numeric vector

Adjusted

a numeric vector

Date

a character vector

References

Data source: Yahoo Finance

Examples

data(GOOG)
str(GOOG) ; plot(GOOG)

Fit Hyperbolic distribution to return/stock prices.

Description

This function fits the Hyperbolic distribution to a given data vector using the fit.hypuv function from the ghyp package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

hd_fit(vec)

Arguments

vec

a numeric vector containing the data to be fitted.

Value

a list containing the following elements:

par

a numeric vector of length 4 containing the estimated values for the parameters of the fitted distribution: alpha (scale), mu (location), sigma (standard deviation), and gamma (skewness).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, sym.ghd_fit, ghd_fit, cauchy_fit, t_fit, sym.hd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
hd_fit(returns)

## End(Not run)

Compute Monthly Returns of a Vector.

Description

This function takes a numeric vector of asset returns and computes monthly returns.

Usage

monthly_return(vec)

Arguments

vec

a numeric vector of asset returns.

Value

A numeric vector of monthly returns.

Note

The input data must be an xts object with dates as rownames.

See Also

weekly_return, annual_return

Examples

## Not run: 
# Compute monthly returns of an asset vector
require(xts)
asset_returns_xts <- xts(c(0.05, -0.03, 0.02, -0.01, 0.04, -0.02, 0.01),
                         order.by = as.Date(c("2023-05-01", "2023-05-02", "2023-05-03",
                                             "2023-05-04", "2023-05-05", "2023-05-06",
                                             "2023-05-07")))
monthly_return(asset_returns_xts)

## End(Not run)

Fit Normal Inverse Gaussian (NIG) Distribution to a vector of returns/stock prices.

Description

This function fits the Normal Inverse Gaussian (NIG) Distribution to a given data vector using the nig_fit function from the fBasics package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

nig_fit(vec)

Arguments

vec

A numeric vector of data.

Value

A list with the following elements:

params

The estimated parameters of the NIG distribution: location, scale, skewness, and shape.

aic

The Akaike Information Criterion (AIC) for the NIG distribution fit.

bic

The Bayesian Information Criterion (BIC) for the NIG distribution fit.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, sym.vg_fit ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
nig_fit(returns)

Fit Normal Distribution to a Vector/stock prices.

Description

This function takes a numeric vector and fits a normal distribution to it using the fitdist function from the fitdistrplus package. It returns a list with the mean and standard deviation parameters of the fitted normal distribution, as well as the AIC and BIC values of the fitted distribution.

Usage

norm_fit(vec)

Arguments

vec

a numeric vector to be fitted with a normal distribution.

Value

A list with the following components:

par

a numeric vector with the estimated mean and standard deviation parameters of the fitted normal distribution.

aic

a numeric value representing the Akaike information criterion (AIC) of the fitted distribution.

bic

a numeric value representing the Bayesian information criterion (BIC) of the fitted distribution.

See Also

t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
# Fit a normal distribution to a vector of returns
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
norm_fit(returns)

## End(Not run)

Fit Skewed Generalized Error Distribution to a vector of returns/stock prices.

Description

This function fits the Skewed Generalized Error Distribution to a given data vector using the skew.ged_fit function from the fGarch package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

skew.ged_fit(vec)

Arguments

vec

A numeric vector of data.

Value

A list with the following elements:

params

A numeric vector of length 4 containing the fitted SGED parameters: shape, scale, location, and skewness.

aic

The Akaike Information Criterion (AIC) for the fitted model.

bic

The Bayesian Information Criterion (BIC) for the fitted model.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
skew.ged_fit(returns)

## End(Not run)

Fit Skew Normal Distribution to a vector of returns/stock prices.

Description

This function fits the Skew Normal distribution to a given data vector using the snormFit function from the fGarch package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

skew.normal_fit(vec)

Arguments

vec

a numeric vector containing the data to be fitted.

Value

a list containing the following elements:

params

a numeric vector of length 3 containing the estimated values for the parameters of the fitted distribution: location (mu), scale (sigma), and skewness (alpha).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
skew.normal_fit(returns)

## End(Not run)

Fit Skewed Student-t Distribution to a vector of returns/stock prices.

Description

This function fits the Skewed Student-t Distribution to a given data vector using the skew.t_fit function from the fGarch package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

skew.t_fit(vec)

Arguments

vec

A numeric vector of data.

Value

A list with the following elements:

params

A numeric vector of length 4 containing the fitted Skewed Student-t parameters: degrees of freedom, skewness, scale, and location.

aic

The Akaike Information Criterion (AIC) for the fitted model.

bic

The Bayesian Information Criterion (BIC) for the fitted model.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, nig_fit, sym.vg_fit, ged_fit, skew.normal_fit, skew.ged_fit

Examples

stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
skew.t_fit(returns)

Fit Symmetric Generalized Hyperbolic Distribution to returns/stock prices.

Description

This function fits the Symmetric Generalized Hyperbolic (sGH) distribution to a given data vector using the fit.ghypuv function from the ghyp package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

sym.ghd_fit(vec)

Arguments

vec

a numeric vector containing the data to be fitted.

Value

a list containing the following elements:

par

a numeric vector of length 5 containing the estimated values for the parameters of the fitted distribution: lambda (location), alpha (scale), mu (degrees of freedom), sigma (standard deviation), and gamma (skewness).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.hd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
sym.ghd_fit(returns)

## End(Not run)

Fit a Symmetric Hyperbolic Distribution to a vector of return/stock prices.

Description

This function fits a Symmetric Hyperbolic distribution to a data vector using the fit.hypuv function from the ghyp package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

sym.hd_fit(vec)

Arguments

vec

a numeric vector containing the symmetric data to be fitted.

Value

a list containing the following elements:

par

a numeric vector of length 4 containing the estimated values for the parameters of the fitted distribution: alpha (scale), mu (degrees of freedom), sigma (standard deviation), and gamma (skewness).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
sym.hd_fit(returns)

## End(Not run)

Fit Symmetric Variance Gamma Distribution to a vector of returns/stock prices.

Description

This function fits the Symmetric Variance Gamma (sVG) distribution to a given data vector using the fit.VGuv function from the ghyp package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

sym.vg_fit(vec)

Arguments

vec

a numeric vector containing the data to be fitted.

Value

a list containing the following elements:

par

a numeric vector of length 4 containing the estimated values for the parameters of the fitted distribution: lambda (scale), mu (location), sigma (volatility), and gamma (skewness).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
sym.vg_fit(returns)

## End(Not run)

Fit Student's t Distribution to a vector of returns/stock prices.

Description

This function fits the Student's t distribution to a given data vector using the fit.tuv function from the ghyp package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

t_fit(vec)

Arguments

vec

a numeric vector containing the data to be fitted.

Value

a list containing the following elements:

par

a numeric vector of length 5 containing the estimated values for the parameters of the fitted distribution: lambda (location), alpha (scale), mu (degrees of freedom), sigma (standard deviation), and gamma (skewness).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, vg_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
t_fit(returns)

## End(Not run)

Tesla Inc. Stock Prices Dataset

Description

This dataset contains the daily stock prices of Tesla, Inc. (TSLA) from January 2, 2013 to May 6, 2023. The data includes the open, high, low, and close prices, as well as the volume and adjusted close price. ~~

Usage

data("TSLA")

Format

A data frame with 2599 observations on the following 7 variables.

Open

a numeric vector

High

a numeric vector

Low

a numeric vector

Close

a numeric vector

Volume

a numeric vector

Adjusted

a numeric vector

Date

a character vector

References

Data source: Yahoo Finance

Examples

data(TSLA)
str(TSLA) ; plot(TSLA)

Fit Variance Gamma Distribution to a vector of return/stock prices.

Description

This function fits the Variance Gamma (VG) distribution to a given data vector using the fit.VGuv function from the ghyp package. It returns the estimated parameters along with the AIC and BIC values for the fitted distribution.

Usage

vg_fit(vec)

Arguments

vec

a numeric vector containing the data to be fitted.

Value

a list containing the following elements:

par

a numeric vector of length 4 containing the estimated values for the parameters of the fitted distribution: lambda (location), mu (scale), sigma (shape), and gamma (skewness).

aic

the Akaike information criterion (AIC) value for the fitted distribution.

bic

the Bayesian information criterion (BIC) value for the fitted distribution.

See Also

norm_fit, t_fit, cauchy_fit, ghd_fit, hd_fit, sym.ghd_fit, sym.hd_fit, sym.vg_fit, nig_fit, ged_fit, skew.t_fit, skew.normal_fit, skew.ged_fit

Examples

## Not run: 
stock_prices <- c(10, 11, 12, 13, 14)
returns <- diff(log(stock_prices))
vg_fit(returns)

## End(Not run)

Compute Weekly Returns of a Vector.

Description

This function takes a numeric vector of asset returns and computes weekly returns.

Usage

weekly_return(vec)

Arguments

vec

a numeric vector of asset returns.

Value

A numeric vector of weekly returns.

Note

The input data must be an xts object with dates as rownames.

See Also

monthly_return, annual_return

Examples

## Not run: 
# Compute weekly returns of an asset vector
require(xts)
asset_returns_xts <- xts(c(0.05, -0.03, 0.02, -0.01, 0.04, -0.02, 0.01),
                         order.by = as.Date(c("2023-05-01", "2023-05-02", "2023-05-03",
                                             "2023-05-04", "2023-05-05", "2023-05-06",
                                             "2023-05-07")))
weekly_return(asset_returns_xts)

## End(Not run)