In [1]:
using Dates, DelimitedFiles, Statistics
include("jlFiles/printmat.jl")
include("jlFiles/printTable.jl")
include("jlFiles/OlsGMFn.jl")
Out[1]:
In [2]:
using Plots
#pyplot(size=(600,400))
gr(size=(480,320))
default(fmt = :svg)
The following section illustrates the theoretical predictions of CAPM by taking the following steps:
In [3]:
μ = [0.115; 0.095; 0.06] #expected returns
Σ = [166 34 58; #covariance matrix
34 64 4;
58 4 100]/100^2
Rf = 0.03
printblue("expected returns:")
printmat(μ)
printblue("covariance matrix:")
printmat(Σ)
In [4]:
include("jlFiles/MvCalculations.jl")
Out[4]:
In [5]:
(wT,μT,σT) = MVTangencyP(μ,Σ,Rf)
printblue("Tangency portfolio weights:")
printmat(wT)
The tangency portfolio is a portfolio of the investable assets ($R_T=w_T'R$). It is therefore straightforward to calculate the covariance (and betas) of $R_T$ with each of the assets.
Details:
$\textrm{Cov}(R_i,R_T) = w_i'\Sigma w_T$, and $\beta_i = \textrm{Cov}(R_i,R_T)/\textrm{Var}(R_T)$.
In [6]:
n = length(μ) #no. assets
β = fill(NaN,n)
for i = 1:n
#local wi, CoviT #only needed in REPL/script
wi = zeros(n)
wi[i] = 1 #weight 1 on asset i
CoviT = wi'Σ*wT
β[i] = CoviT/σT^2 #usual OLS coefficient: Cov(x,y)/Var(x)
end
printblue("β of the $n assets:")
printmat(β)
Recall: CAPMS says
$\textrm{E}R_{i} = R_f + \beta_{i}(\mu_{T}-R_f)$
This can be compared with the (actual) average returns.
In [7]:
ERi_CAPM = Rf .+ β*(μT-Rf) #vector since β is
printblue("μ and ER as suggested by CAPM: ")
printmat([μ ERi_CAPM])
In [8]:
scatter( β,ERi_CAPM*100,
xlim = (0,2),
ylim = (0,15),
legend = false,
title = "beta vs ERi",
xlabel = "beta",
ylabel = "ERi, %" )
Out[8]:
In [9]:
x = readdlm("Data/FFmFactorsPs.csv",',',skipstart=1)
Rme = x[:,2] #market excess return
RSMB = x[:,3] #small minus big firms
RHML = x[:,4] #high minus low book-to-market ratio
Rf = x[:,5] #interest rate
x = readdlm("Data/FF25Ps.csv",',') #no header line: x is matrix
R = x[:,2:end] #returns for 25 FF portfolios
Re = R .- Rf #excess returns for the 25 FF portfolios
Re = Re[:,[1;7;13;19;25]] #use just 5 assets to make the printing easier
(T,n) = size(Re) #no. obs and no. test assets
Out[9]:
We now use the market return as a proxy for the tangency portfolio - and test if the model holds.
Recall: estimate $(\alpha_{i},b_{i})$ in the CAPM regression
$R_{it}^{e} =\alpha_{i}+b_{i}R_{mt}^{e}+\varepsilon_{it}$
Test if $\alpha_{i}=0$.
In [10]:
x = [ones(T) Rme] #regressors
α = fill(NaN,n)
tstat = fill(NaN,n)
for i = 1:n #loop over the different test assets
#local b_i, residual, Covb #only needed in REPL/script
(b_i,_,_,Covb,) = OlsGMFn(Re[:,i],x)
α[i] = b_i[1] #estimated α
tstat[i] = (b_i[1]-0)/sqrt(Covb[1,1]) #tstat of H₀: true α=0
end
printblue("OLS intercepts and t-stats:")
colNames = [string("asset ",i) for i=1:n]
rowNames = ["α","t-stat"]
printTable([α';tstat'],colNames,rowNames)
In [ ]: