In [1]:
using Dates, LinearAlgebra, Distributions
include("jlFiles/printmat.jl")
include("jlFiles/printTable.jl")
Out[1]:
In [2]:
rf = 0.03 #riskfree rate
σ² = 100/10000 #variance of risky asset
μe1A = 1/100 #mean excess return of single risky asset, state A
μe1B = 0.5/100 #state B
vA = (μe1A + σ²/2)/σ²
vB = (μe1B + σ²/2)/σ²
rowNames1 = ["risky asset","riskfree"]
printblue("Portfolio weights in the two states (the case of a single risky asset):")
printTable([vA vB;1-sum(vA) 1-sum(vB)],["state A","state B"],rowNames1)
In [3]:
"""
Calculate optimal portfolio for log utility case, when the log returns are N(μe+rf,Σ),
Campbell&Viceira.
"""
function OptPortLogUtil(Σ,μe,rf)
v = inv(Σ)*(μe+diag(Σ)/2)
Erp = rf + v'μe + v'diag(Σ)/2 - v'Σ*v/2
Varrp = v'Σ*v
return v, Erp, Varrp
end
Out[3]:
In [4]:
Σ = [166 34 58; #3 risky assets
34 64 4;
58 4 100]/10000
μeA = [2.0, 1.0, 0.5]/100 #state A
μeB = [2.0, 0.0, 0.5]/100 #state B
vA, = OptPortLogUtil(Σ,μeA,rf) #myopic portfolio choice in each state
vB, = OptPortLogUtil(Σ,μeB,rf)
printblue("Portfolio weights in the two states (several risky assets):")
rowNames3 = ["asset 1","asset 2","asset 3","riskfree"]
printTable([vA vB;1-sum(vA) 1-sum(vB)],["state A","state B"],rowNames3)
In [5]:
γ = 3 #risk aversion
vA = (μe1A + σ²/2)/(σ²*γ) #with a single risky asset
vB = (μe1B + σ²/2)/(σ²*γ)
printblue("Portfolio weights in the two states (the case of a single risky asset):")
printTable([vA vB;1-sum(vA) 1-sum(vB)],["state A","state B"],rowNames1)
In [6]:
"""
Calculate optimal portfolio for CRRA utility case, when the log returns are N(μe+rf,Σ),
Campbell&Viceira.
"""
function OptPortCRRA(Σ,μe,rf,γ)
v = inv(Σ)*(μe+diag(Σ)/2)/γ
Erp = rf + v'μe + v'diag(Σ)/2 - v'Σ*v/2
Varrp = v'Σ*v
return v, Erp, Varrp
end
Out[6]:
In [7]:
γ = 3
vA, = OptPortCRRA(Σ,μeA,rf,γ)
vB, = OptPortCRRA(Σ,μeB,rf,γ)
printblue("Portfolio weights in the two states (several risky assets):")
printTable([vA vB;1-sum(vA) 1-sum(vB)],["state A","state B"],rowNames3)
We now consider the more difficult case when the CRRA investor considers several periods.
In the case of log utility ($\gamma=1$), this actually gives a myopic solution: in each period, $\ln(1+R_p$) is maximized, where $R_p$ is the one period portfolio return.
With CRRA this may no longer hold. In particular, if there are some predictable (non-iid) features of the asset returns, then today's investment may be influenced by how the return over the next period is correlated with the investment opportunities in the subsequent periods.
The optimal solution used in the next few cells (see lecture notes for details) is for the case when the vector ($n$ assets) of excess returns follow
$ r_{t+1}^{e}=a+z_{t}+u_{t+1}, $
where the vector $z_t$ follows the VAR(1)
$ z_{t+1}=\phi z_{t}+\eta_{t+1}, $ with $\eta_{t+1}$ being $N(\mathbf{0},\Sigma_{\eta})$.
The covariance matrix of $u_{t+1}$ and $\eta_{t+1}$, which plays a key role of the analysis, is denoted by $\Sigma_{u\eta}$.
In [8]:
"""
Optimal dynamic portfolio choice when there is a single risky asset.
"""
function CRRAPortOpt1(Σ_u,Σ_η,Σ_uη,a,ϕ,γ,z)
v_myop = inv(Σ_u)*(a + z + Σ_u/2)/γ
Σ = 2*Σ_u + Σ_η + 2*Σ_uη
v_noreb = inv(Σ)*(2*a + (I+ϕ)*z + Σ/2)/γ
Ev1 = inv(Σ_u)*(a + ϕ*z + Σ_u/2)/γ
v_rebal = inv(Σ_u)*(a + z + Σ_u/2 + (1-γ)*Σ_uη*Ev1)/γ
return v_myop, v_noreb, v_rebal
end
Out[8]:
In [9]:
(γ,Σ_u,Σ_η,a,ϕ) = (3,100/10000,75/10000,0.75/100,0.9) #parameters in example with 1 risky asset
Σ_uηL = -0.5*sqrt(Σ_u*Σ_η) #corr(u,η)=-0.5
(v_myopL,v_norebL,v_rebalL) = CRRAPortOpt1(Σ_u,Σ_η,Σ_uηL,a,ϕ,γ,0)
printblue("Portfolio weights: myopic and 2-period investor (who can rebalance), corr(u,η)=-0.5")
xx = [v_myopL v_rebalL;
1-sum(v_myopL) 1-sum(v_rebalL)]
printTable(xx,["myopic","2-period investor"],rowNames1,width=20)
In [10]:
Σ_uηH = 0.5*sqrt(Σ_u*Σ_η) #corr(u,η)=0.5
(v_myopH,v_norebH,v_rebalH) = CRRAPortOpt1(Σ_u,Σ_η,Σ_uηH,a,ϕ,γ,0)
printblue("Portfolio weights: myopic and 2-period investor (who can rebalance), corr(u,η)=0.5")
xx = [v_myopH v_rebalH;
1-sum(v_myopH) 1-sum(v_rebalH)]
printTable(xx,["myopic","2-period investor"],rowNames1,width=20)
In [11]:
"""
Optimal dynamic portfolio choice when there are several risky assets
"""
function CRRAPortOpt(Σ_u,Σ_η,Σ_uη,a,ϕ,γ,z)
v_myop = inv(Σ_u)*(a + z + diag(Σ_u)/2)/γ
Σ = 2*Σ_u + Σ_η + 2*Σ_uη
v_noreb = inv(Σ)*(2*a + (I+ϕ)*z + diag(Σ)/2)/γ
Ev1 = inv(Σ_u)*(a + ϕ*z + diag(Σ_u)/2)/γ
v_rebal = inv(Σ_u)*(a + z + diag(Σ_u)/2 + (1-γ)*Σ_uη*Ev1)/γ
return v_myop, v_noreb, v_rebal
end
Out[11]:
In [12]:
γ = 3
Σ_u = [166 34 58;
34 64 4;
58 4 100]/10000
Σ_η = [0 0 0;
0 100 0;
0 0 0]/10000 #only asset 2 has dynamics in expected returns
a = [2,0.5,0.5]/100
ϕ = [0 0 0; #doesn't matter for 2-period problem when z_t=0
0 0.9 0;
0 0 0]
Σ_uηL = [0 0 0; #Cov(u,η)<0 for asset 2
0 -40 0;
0 0 0]/10000
Σ_uηH = [0 0 0; #Cov(u,η)>0 for asset 2
0 40 0;
0 0 0]/10000
printblue("The model parameters:\n")
println("Σ_u*10000")
printmat(Σ_u*10000)
println("Σ_η*10000")
printmat(Σ_η*10000)
println("a*100")
printmat(a*100)
println("\nTwo cases: negative and positive Cov(u,η) for asset 2")
printmat(Σ_uηL*10000)
printmat(Σ_uηH*10000)
In [13]:
(v_myopL,v_norebL,v_rebalL) = CRRAPortOpt(Σ_u,Σ_η,Σ_uηL,a,ϕ,γ,zeros(3)) #different Cov(u,η)
(v_myopH,v_norebH,v_rebalH) = CRRAPortOpt(Σ_u,Σ_η,Σ_uηH,a,ϕ,γ,zeros(3))
printblue("Portfolio weights: myopic and 2-period investor (who can rebalance), corr(u,η) < 0")
xx = [v_myopL v_rebalL;
1-sum(v_myopL) 1-sum(v_rebalL)]
printTable(xx,["myopic","2-period investor"],rowNames3,width=20)
printblue("Portfolio weights: myopic and 2-period investor (who can rebalance), corr(u,η) > 0")
xx = [v_myopH v_rebalH;
1-sum(v_myopH) 1-sum(v_rebalH)]
printTable(xx,["myopic","2-period investor"],rowNames3,width=20)
In [ ]: