Portfolio Choice 1

This notebook analyses the effect of leverage and diversification.

Load Packages and Extra Functions


In [1]:
using Dates 

include("jlFiles/printmat.jl")
include("jlFiles/printTable.jl")


Out[1]:
printTable2

In [2]:
using Plots

#pyplot(size=(600,400))      #pyplot() or gr()
gr(size=(480,320))
default(fmt = :svg)

Portfolio Choice: A Risky and a Riskfree Asset

Suppose you can invest in a risky asset ($R$, with expected return $\mu$ and standard deviation $\sigma$) and also in a riskfree asset (at the rate $R_f$).

With the portfolio weight $v$ on the risky asset, the portfolio return is

$R_p = v R + (1-v)R_f$.

The average and standard deviation of the portfolio are therefore

$\text{E}R_p = v \mu + (1-v)R_f$

and

$\text{Std}(R_p) = |v|\sigma$


In [3]:
μ  = 9.5/100  #expected return on the risky asset
σ  = 8/100    #std of market 
Rf = 3/100    #risk free return (interest rate)
v  = 0.5  

ERp   = v*μ + (1 - v)*Rf
StdRp = abs(v)*σ

printlnPs("ERp and Std(Rp) when v=$v:",ERp,StdRp)


ERp and Std(Rp) when v=0.5:     0.062     0.040

In [4]:
v     = [0;0.5;1;2]          #trying different weights on risky asset 
ERp   = v*μ + (1 .- v)*Rf    #vector since v is
StdRp = abs.(v)*σ 

txt = text("portfolios of a single risky asset\n and a riskfree asset",8,:left)

scatter( StdRp*100,ERp*100,
         legend = false,
         ylim = (0,20),
         xlim = (-1,20),
         title = "Mean vs std",
         xlabel = "Std(Rp), %",
         ylabel = "ERp, %",
         annotation = (0.5,18,txt) )


Out[4]:
0 5 10 15 20 0 5 10 15 20 Mean vs std Std(Rp), % ERp, % portfolios of a single risky asset and a riskfree asset

Consider the "utility" $\text{E}R_p - k/2\times \text{Var}(R_p)$ as a function of the weight $v$ on the risky asset (and $1-v$ on the riskfree asset)

The optimal portfolio weight is

$ v = \frac{\mu - R_f}{k\sigma^2} $


In [5]:
k     = 25      #risk aversion         

v     = range(-0.5,stop=2,length=101)   #trying different portfolio weights
ERp   = v*μ + (1 .- v)*Rf
VarRp = v.^2*σ^2 
Util  = ERp - k/2*VarRp

plot( v,Util,
      linecolor = :red,
      linewidth = 2,
      legend = false,
      title = "Utility",
      xlabel = "v (weight on risky asset)" )


Out[5]:
-0.5 0.0 0.5 1.0 1.5 2.0 -0.15 -0.10 -0.05 0.00 Utility v (weight on risky asset)

In [6]:
vopt = (μ-Rf)/(k*σ^2)

println("Optimal weights on risky and riskfree assets when k = $k: ")
printTable([vopt,1-vopt],[""],["risky","riskfree"])


Optimal weights on risky and riskfree assets when k = 25: 
                  
risky        0.406
riskfree     0.594

Diversification

The variance of an equally weighted portfolio of two assets is

$\sigma^2_p = \sigma_{11}/4 + \sigma_{22}/4 + \rho\sigma_1\sigma_2/2,$

where $\rho$ is the correlation.

More generally, the variance of an equally weighted portfolio of n assets is

$\sigma^2_p = (\bar{\sigma}_{ii} - \bar{\sigma}_{ij})/n + \bar{\sigma}_{ij}$,

where $\bar{\sigma}_{ii}$ is the average variance (across the assets) and $\bar{\sigma}_{ij}$ is the average covariance.


In [7]:
σ₁₁ = 256/100^2
σ₂₂ = σ₁₁             #assume the same variance of the two assets
ρ   = 0.5

VarRp = σ₁₁/4 + σ₂₂/4 + ρ*sqrt(σ₁₁)*sqrt(σ₂₂)/2

printlnPs("Individual variance: ",σ₁₁, ",    portfolio variance: ",VarRp)


Individual variance:      0.026,    portfolio variance:      0.019

In [8]:
σᵢᵢ_avg = σ₁₁                        #average variance
σᵢⱼ_avg = ρ*sqrt(σᵢᵢ_avg*σᵢᵢ_avg)   #average covariance
n    = 1:49                         #try all these values of n

VarRp = (σᵢᵢ_avg-σᵢⱼ_avg)./n .+ σᵢⱼ_avg     #variance of equally weighted portfolio

println("Portfolio variance and std for n=1-3")
printTable([VarRp[1:3] sqrt.(VarRp[1:3])],["var","std"],string.(1:3))

plot( n,VarRp,
      linecolor = :red,
      linewidth = 2,
      ylim = (0,0.03),
      label = "variance",
      title = "Variance of EW portfolio",
      xlabel = "n (number of assets)",
      ylabel = "Variance" )

hline!([σᵢⱼ_avg;σᵢⱼ_avg],line=(:dash,1),linecolor=:blue,label="average covariance")


Portfolio variance and std for n=1-3
        var       std
1     0.026     0.160
2     0.019     0.139
3     0.017     0.131

Out[8]:
0 10 20 30 40 50 0.00 0.01 0.02 0.03 Variance of EW portfolio n (number of assets) Variance variance average covariance

(Extra) Appendix: A Primer in Optimization

See my Julia tutorial for code.


In [ ]: