In [1]:
using Dates
include("jlFiles/printmat.jl")
Out[1]:
In [2]:
P = [100;108;109] #price series, after dividends
D = [0;2;0] #dividends
R = zeros(length(P)) #where to store the results
for t = 2:length(P) #P[2] is the 2nd element of P
R[t] = (P[t] + D[t])/P[t-1] - 1
end
R = R[2:end]
#compact notation
R_alt = (P[2:end] + D[2:end])./P[1:end-1] .- 1 #notice the ./ and .-
println(" period return return (alt), %")
printmat([1:2 R*100 R_alt*100])
Net returns can be cumulated into a portfolio value as
$ V_t = V_{t-1}(1+R_t) $
where we need a starting value (initial investment) for the portfolio (a common choice is to normalise to $V_0=1$).
With log returns, $r_t=\log(1+R_t)$, we instead do
$ \ln V_t = \ln V_{t-1} + r_t $
We can cumulate the returns by a loop of by more compact notation (see below).
In [3]:
R = [20;-35;25]/100 #returns for t=1,2,3
V = cumprod(1 .+ R) #V(t) = V(t-1)*(1+R(t)), starting at 1 in t=0
lnV = cumsum(log.(1 .+ R)) #lnV(t) = lnV(t-1) + r(t)
println(" period return V logV")
printmat([1:3 R V lnV])
println("Check that logV really equals log.(V). Also, try a loop instead")
With portfolio weights in the vector $w$, the portfolio return, the expected portfolio return and the portfolio variance can be computed as
$R_p = w'R$,
$\text{E}R_p = w'\mu$ and
$\text{Var}(R_p) = w'\Sigma w$
In [4]:
w = [0.8;0.2]
R = [10;5]/100 #returns of asset 1 and 2
Rp = w'R
println("portfolio weights (for the two assets): ")
printmat(w)
println("Returns of assets: ")
printmat(R)
printlnPs("Portfolio return: ",Rp)
In [5]:
μ = [9;6]/100 #\mu and tab to get this
Σ = [256 96;
96 144]/100^2
println("expected returns*100: ")
printmat(μ*100)
println("covariance matrix*100^2:")
printmat(Σ*100^2)
In [6]:
ERp = w'μ
VarRp = w'Σ*w
printlnPs("Expected portfolio return: ",ERp)
printlnPs("Portfolio variance and std:",VarRp,sqrt(VarRp))
In [7]:
Σb = [256 -96;
-96 144]/100^2
printlnPs("Portfolio std if the assets were negatively correlated: ",sqrt(w'Σb*w))
In [ ]: