In [1]:
using Dates
include("jlFiles/printmat.jl")
include("jlFiles/printTable.jl")
Out[1]:
In [2]:
using Plots
#pyplot(size=(600,400)) #pyplot() or gr()
gr(size=(480,320))
default(fmt = :svg)
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)
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]:
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]:
In [6]:
vopt = (μ-Rf)/(k*σ^2)
println("Optimal weights on risky and riskfree assets when k = $k: ")
printTable([vopt,1-vopt],[""],["risky","riskfree"])
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)
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")
Out[8]:
In [ ]: