In Python, using stats of scipy package, variables beta
, alpha
, r
, p
, std_err
of CAPM can be calculated as follows:
beta, alpha, r_value, pvalue, std_err = stats.linregress(stock_rtn_arr, mkt_rtn_arr)
Please guide me in calculating the above variables in Julia.
I'm assuming you are looking to run a simple OLS model, which in Julia can be fit using the GLM
package:
julia> using GLM, DataFrame
julia> mkt_rtn_arr = randn(500); stock_rtn_arr = 0.5*mkt_rtn_arr .+ rand();
julia> df = DataFrame(mkt_rtn = mkt_rtn_arr, stock_rtn = stock_rtn_arr);
julia> linear_model = lm(@formula(stock_rtn ~ mkt_rtn), df)
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}
stock_rtn ~ 1 + mkt_rtn
Coefficients:
──────────────────────────────────────────────────────────────────────────────
Estimate Std. Error t value Pr(>|t|) Lower 95% Upper 95%
──────────────────────────────────────────────────────────────────────────────
(Intercept) 0.616791 7.80308e-18 7.90446e16 <1e-99 0.616791 0.616791
mkt_rtn 0.5 7.78767e-18 6.42041e16 <1e-99 0.5 0.5
──────────────────────────────────────────────────────────────────────────────
You can then extract the parameters of interest from the linear_model
:
julia> β = coef(linear_model)[2]
0.4999999999999999
julia> α = coef(linear_model)[1]
0.6167912017573035
julia> r_value = r2(linear_model)
1.0
julia> pvalues = coeftable(linear_model).cols[4]
2-element Array{Float64,1}:
0.0
0.0
julia> stderror(linear_model)
2-element Array{Float64,1}:
7.803081577574428e-18
7.787667394841443e-18
Note that I have used the @formula
API to run the regression, which requires putting your data into a DataFrame
and is in my opinion the preferred way of estimating the linear model in GLM, as it allows for much more flexibility in specifying the model. Alternatively you could have called lm(X, y)
directly on an array for your X
variable and the y
variable:
julia> lm([ones(length(mkt_rtn_arr)) mkt_rtn_arr], stock_rtn_arr)
LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}}:
Coefficients:
─────────────────────────────────────────────────────────────────────
Estimate Std. Error t value Pr(>|t|) Lower 95% Upper 95%
─────────────────────────────────────────────────────────────────────
x1 0.616791 7.80308e-18 7.90446e16 <1e-99 0.616791 0.616791
x2 0.5 7.78767e-18 6.42041e16 <1e-99 0.5 0.5
─────────────────────────────────────────────────────────────────────
Note that here I have appended a column of ones to the market return array to estimate the model with an intercept, which the @formula
macro will do automatically (similar to the way it's done in R).