In [2]:
using Dates, LinearAlgebra, Statistics, DelimitedFiles
include("jlFiles/printmat.jl")
include("jlFiles/printTable.jl")
include("jlFiles/OlsGMFn.jl") #function for OLS
Out[2]:
The single index model implies that the covariance of assets $i$ and $j$ is
$\sigma_{ij} = \beta_i \beta_j \text{Var}(R_{mt}) + \text{Cov}(\varepsilon_{it},\varepsilon_{jt}),$ but where $\text{Cov}(\varepsilon_{it},\varepsilon_{jt}) = 0 \ \text{ if } \ i \neq j$
The betas are typically estimated by a linear regression (OLS)
$R_{it} = \alpha_i + \beta_i R_{mt} + \varepsilon_{it}$
The next section gives a simple example.
In [3]:
β = [0.9 1.1] #βs of the two assets, row vector
VarRes = ([10 5]/100).^2 #var(ϵ) for the two assets, row vector
VarRm = 0.15^2 #var(market return)
printblue("β:")
printmat(β)
printblue("VarRm*100^2:")
printmat(VarRm*100^2)
printblue("Var(residuals)*100^2:")
printmat(VarRes*100^2)
In [4]:
"""
CovFromIndexModel(b,VarRes,Ω)
Calculate covariance matrix from a multi-factor model.
Cov(Ri,Rj) = bᵢ'*Ω*bⱼ, where Ω is the Cov(indices) and bᵢ is the vector of regression
coefficients when regressing Ri on a constant and the indices (and bⱼ is for asset j).
"""
function CovFromIndexModel(b,VarRes,Ω) #coefs for regression i is in b[:,i]
n = length(VarRes)
CovR = fill(NaN,(n,n))
for i = 1:n, j = 1:n #loop over both i and j
if i == j
CovR[i,i] = b[:,i]'Ω*b[:,i] + VarRes[i]
else
CovR[i,j] = b[:,i]'Ω*b[:,j]
end
end
return CovR
end
CovR = CovFromIndexModel(β,VarRes,VarRm) #notice: use β' to get 1xn matrix
printblue("Covariance matrix*100^2 calculated from betas:")
printmat(CovR*100^2)
The next section applies the single index model on 5 of the (25) FF portfolios.
In [5]:
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[5]:
In [6]:
x = [ones(T) Rme] #regressors
(β,VarRes) = (fill(NaN,1,n),fill(NaN,n))
for i = 1:n
#local b_i, ϵ_i #only needed in REPL/script
(b_i,ϵ_i,_) = OlsGMFn(Re[:,i],x) #OLS
β[1,i] = b_i[2] #2nd coef is for Rme
VarRes[i] = var(ϵ_i)
end
colNames = [string("asset ",i) for i=1:n]
printblue("β for $n assets, from OLS of Re on constant and Rme:")
printTable(β,colNames,["β on Rme"],)
In [7]:
VarRm = var(Rme)
CovR = CovFromIndexModel(β,VarRes,VarRm)
printblue("Covariance matrix calculated from betas:")
printmat(CovR)
printblue("Covariance matrix calculated from data:")
printmat(cov(Re))
printblue("Difference between the two:")
printmat(CovR-cov(Re))
A multi-index model is based on
$R_{it} =a_{i}+b_{i}^{\prime}I_{t}+\varepsilon_{it}$,
where $b_{i}$ is a $K\times 1$ vector of slope coefficients.
If $\Omega$ is the covariance matrix of the indices $I_t$, then the covariance of assets $i$ and $j$ is
$\sigma_{ij}=b_{i}^{\prime}\Omega b_{j} + \text{Cov}(\varepsilon_{it},\varepsilon_{jt}),$ but where $\text{Cov}(\varepsilon_{it},\varepsilon_{jt}) = 0 \ \text{ if } \ i \neq j$
In [8]:
x = [ones(T) Rme RSMB RHML] #regressors
K = size(x,2) - 1
(b,VarRes) = (fill(NaN,(K,n)),fill(NaN,n)) #b is Kxn
for i = 1:n
#local b_i,ϵ_i #only needed in REPL/script
(b_i,ϵ_i,_) = OlsGMFn(Re[:,i],x) #OLS
b[:,i] = b_i[2:end]
VarRes[i] = var(ϵ_i)
end
printblue("OLS slope coefficients:")
printTable(b,colNames,["β on Rme", "β on RSMB", "β on RHML"])
In [9]:
Ω = cov(x[:,2:end]) #covariance matrix of factors
CovR = CovFromIndexModel(b,VarRes,Ω)
printblue("Covariance matrix calculated from betas:")
printmat(CovR)
printblue("Covariance matrix calculated from data:")
printmat(cov(Re))
printblue("Difference between the two:")
printmat(CovR-cov(Re))
printred("Is the multi-index model better than the single-index model?")
In [ ]: