---
title: "Introduction to Portfolio Theory"
author: "Eric Zivot"
date: "Wednesday, February 24, 2016"
output: slidy_presentation
---

## Set options and load packages

```{r}
options(digits=3, width=70)
library(IntroCompFinR)
library(PerformanceAnalytics)
```

## 2 asset example

Hypothetical asset return data. Asset A (Amazon) is a high return and high risk asset. Asset B (Boeing) is a low return and low risk asset. Data is calibrated to reflect a yearly investment horizon.

```{r}
mu.A = 0.175
sig.A = 0.258
sig2.A = sig.A^2
mu.B = 0.055
sig.B = 0.115
sig2.B = sig.B^2
rho.AB = -0.164
sig.AB = rho.AB*sig.A*sig.B

```

## Example portfolio 1: long only

```{r}
x.A = 0.5
x.B = 0.5
mu.p1 = x.A*mu.A + x.B*mu.B
sig2.p1 = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*sig.AB
sig.p1 = sqrt(sig2.p1)
mu.p1
sig2.p1
sig.p1
```

Portfolio mean return is half-way between asset mean returns, but portfolio volatility is less than half-way between asset volatilities. 

## Example portfolio 2: long-short

Here, a short sale is selling an asset you don't own (a type of leverage) and taking the proceeds and buying more of the other asset. The short-sold asset has a negative weight, and the other asset has a weight greater than one. 


```{r}
x.A = 1.5
x.B = -0.5
mu.p2 = x.A*mu.A + x.B*mu.B
sig2.p2 = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*sig.AB
sig.p2 = sqrt(sig2.p2)
mu.p2
sig2.p2
sig.p2

```

Notice that short selling (leverage) greatly increases the risk of the portfolio.

## Risk return characteristics of portfolios

```{r, echo=FALSE}
sigma.vals = c(sig.A, sig.B, sig.p1, sig.p2)
mu.vals = c(mu.A, mu.B, mu.p1, mu.p2)
names(sigma.vals) = names(mu.vals) = c("A", "B", "P1", "P2")
plot(sigma.vals, mu.vals, 
     xlab=expression(sigma[p]), ylab=expression(mu[p]), cex.lab=2,
     xlim = c(0, 0.40), ylim = c(0, 0.30),
     pch=16, col="blue", cex=2)
text(sigma.vals, mu.vals, labels=names(mu.vals), pos=2)
slope = (mu.A - mu.B)/(sig.A - sig.B)
intercept = mu.A - slope*sig.A
abline(a=intercept, b=slope, lwd=2, lty="dotted")

```


## Asset and portfolio VaR

```{r}
# equally weighted portfolio
x.A = 0.5
x.B = 0.5
mu.p1 = x.A*mu.A + x.B*mu.B
sig2.p1 = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*sig.AB
sig.p1 = sqrt(sig2.p1)
```

```{r}
# Asset VaR
w0 = 100000
VaR.A = (mu.A + sig.A*qnorm(0.05))*w0
VaR.A
VaR.B = (mu.B + sig.B*qnorm(0.05))*w0
VaR.B
```

```{r}
# portfolio VaR
VaR.p1 = (mu.p1 + sig.p1*qnorm(0.05))*w0
VaR.p1
# note: portfolio VaR is not a weighted average of individual asset VaR
x.A*VaR.A + x.B*VaR.B
```

## Create portfolio frontier

```{r, echo=FALSE}
# minimum variance portfolio for plot
xA.min = (sig2.B - sig.AB)/(sig2.A + sig2.B - 2*sig.AB)
xB.min = 1 - xA.min
mu.p.min = xA.min*mu.A + xB.min*mu.B
sig2.p.min = xA.min^2 * sig2.A + xB.min^2 * sig2.B + 2*xA.min*xB.min*sig.AB
sig.p.min = sqrt(sig2.p.min)
```

```{r}
# create portfolios for frontier plot
x.A = seq(from=-0.4, to=1.4, by=0.1)
x.B = 1 - x.A
mu.p = x.A*mu.A + x.B*mu.B
sig2.p = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*sig.AB
sig.p = sqrt(sig2.p)
```

## Plot portfolio frontier

```{r}
# plot portfolio frontier
cex.val = 2
plot(sig.p, mu.p, type="b", pch=16, cex = cex.val,
     ylim=c(0, max(mu.p)), xlim=c(0, max(sig.p)),
     xlab=expression(sigma[p]), ylab=expression(mu[p]), cex.lab=cex.val,
     col=c(rep("red", 6), "blue", rep("green", 12)))
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)
text(x=sig.p.min, y=mu.p.min, labels="Global min", pos=2, cex = cex.val)
```

* Portfolio frontier is one side of a hyperbole
* Portfolio frontier is often called the "Markowitz bullet"
* Long only portfolios are between asset A and asset B
* Long-short portfolios are to the right of asset A and to the right of asset B
* Shape of frontier depends crucially $\rho_{AB}$

## Portfolio frontier as a function of $\rho_{AB}$

```{r, echo=FALSE}
rho.AB.vals = c(-0.9,-0.5, -0.25, 0, 0.25, 0.5, 0.9)
x.A = seq(from=-0.4, to=1.4, by=0.1)
x.B = 1 - x.A
mu.p = x.A*mu.A + x.B*mu.B
sig2.p = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*rho.AB.vals[1]*sig.A*sig.B
sig.p = sqrt(sig2.p)

# create portfolio plot
cex.val = 1.5
plot(sig.p, mu.p, type="b", pch=16, cex = cex.val,
     ylim=c(0, max(mu.p)), xlim=c(0, max(sig.p)),
     xlab=expression(sigma[p]), ylab=expression(mu[p]),
     cex.lab = cex.val, col=1)
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)

for (i in 2:length(rho.AB.vals)) {
  sig2.p = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*rho.AB.vals[i]*sig.A*sig.B
  sig.p = sqrt(sig2.p)
  points(sig.p, mu.p, type="b", pch=16, col=i, cex=cex.val)
}
legend(x="bottomright", legend=paste("rho", as.character(rho.AB.vals), sep="="), 
       col=1:length(rho.AB.vals), lty=1, pch=16, cex = cex.val)
```

* Portfolio frontier is almost linear as $\rho_{AB}$ approaches 1
* Portfolio frontier stretches to the y-axis as $\rho_{AB}$ approaches -1
   + When $\rho_{AB}=-1$ there exists a portfolio with zero volatility (no risk)

## Portfolio Frontier when $\rho_{AB}=+/-1$

```{r, echo=FALSE}
rho.AB = 1
sig.AB = rho.AB*sig.A*sig.B
mu.p.1 = x.A*mu.A + x.B*mu.B
sig2.p.1 = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*sig.AB
sig.p.1 = sqrt(sig2.p.1)
rho.AB = -1
sig.AB = rho.AB*sig.A*sig.B
mu.p.m1 = x.A*mu.A + x.B*mu.B
sig2.p.m1 = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*sig.AB
sig.p.m1 = sqrt(sig2.p.m1)

plot(sig.p.1, mu.p.1, type="b", pch=16, cex = cex.val,
     ylim=c(0, max(mu.p)), xlim=c(0, max(sig.p)),
     xlab=expression(sigma[p]), ylab=expression(mu[p]))
points(sig.p.m1, mu.p.m1, type="b", col="blue", pch=22, cex = cex.val)
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)
legend(x="topleft", legend=c(expression(rho==1), expression(rho==-1)),
       col=c("black", "blue"), pch=c(16,22), cex = cex.val)
```

## Efficient Portfolios

```{r, echo=FALSE}
rho.AB = -0.164
sig.AB = rho.AB*sig.A*sig.B
x.A = seq(from=-0.4, to=1.4, by=0.1)
x.B = 1 - x.A
mu.p = x.A*mu.A + x.B*mu.B
sig2.p = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*sig.AB
sig.p = sqrt(sig2.p)
plot(sig.p, mu.p, type="b", pch=16, ylim=c(0, max(mu.p)), cex = cex.val,
     xlim=c(0, max(sig.p)),
     xlab=expression(sigma[p]), ylab=expression(mu[p]),
     col=c(rep("red", 6), rep("green", 13)))
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)
segments(x0=sig.p[3], y0=0, x1=sig.p[3], y1=mu.p[11], 
         lwd=3, lty="dotted")
text(x=sig.p[3], y=mu.p[3], labels="Inefficient portfolio", 
     pos=4, cex = cex.val)
text(x=sig.p[3], y=mu.p[11], labels="Efficient portfolio", 
     cex = cex.val, pos=4)
```

* Efficient portfolios: portfolios with highest expected return for a given volatility (green dots)
* Minimum variance portfolio: efficient portfolio with the smallest variance
* Inefficient portfolios (red dots) are dominated by efficient portfolios (green dots)

## Computing the minimum variance portfolio

```{r}
# compute mininimum variance portfolio weights
xA.min = (sig2.B - sig.AB)/(sig2.A + sig2.B - 2*sig.AB)
xB.min = 1 - xA.min
xA.min
xB.min
```

```{r}
# compute expected return and volatility
mu.p.min = xA.min*mu.A + xB.min*mu.B
sig2.p.min = xA.min^2 * sig2.A + xB.min^2 * sig2.B + 2*xA.min*xB.min*sig.AB
sig.p.min = sqrt(sig2.p.min)
mu.p.min
sig.p.min
```

## Minimum variance portfolko

```{r, echo=FALSE}
x.A = seq(from=-0.4, to=1.4, by=0.1)
x.B = 1 - x.A
mu.p = x.A*mu.A + x.B*mu.B
sig2.p = x.A^2 * sig2.A + x.B^2 * sig2.B + 2*x.A*x.B*sig.AB
sig.p = sqrt(sig2.p)

# minimum variance portfolio
xA.min = (sig2.B - sig.AB)/(sig2.A + sig2.B - 2*sig.AB)
xB.min = 1 - xA.min
mu.p.min = xA.min*mu.A + xB.min*mu.B
sig2.p.min = xA.min^2 * sig2.A + xB.min^2 * sig2.B + 2*xA.min*xB.min*sig.AB
sig.p.min = sqrt(sig2.p.min)

# create portfolio plot
cex.val = 2
plot(sig.p, mu.p, type="b", pch=16, cex = cex.val,
     ylim=c(0, max(mu.p)), xlim=c(0, max(sig.p)),
     xlab=expression(sigma[p]), ylab=expression(mu[p]), cex.lab=cex.val,
     col=c(rep("red", 6), "blue", rep("green", 12)))
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)
text(x=sig.p.min, y=mu.p.min, labels="Global min", pos=2, cex = cex.val)

```

## Portfoliols of T-Bills and 1 risky asset

```{r}
# Risk-free rate
r.f = 0.03
# T-bills + asset A
x.A = seq(from=0, to=1.4, by=0.1)
mu.p.A = r.f + x.A*(mu.A - r.f)
sig.p.A = x.A*sig.A
sharpe.A = (mu.A - r.f)/sig.A
sharpe.A
```

```{r}
# T-bills + asset B
x.B = seq(from=0, to=1.4, by=0.1)
mu.p.B = r.f + x.B*(mu.B - r.f)
sig.p.B = x.B*sig.B
sharpe.B = (mu.B - r.f)/sig.B
sharpe.B
```

## Portfoliols of T-Bills and 1 risky asset

```{r, echo=FALSE}
plot(sig.p.A, mu.p.A, type="b", col="green", ylim=c(0, max(mu.p.A)),
     xlim=c(0, max(sig.p.A, sig.p.B)), pch=16, cex = cex.val,
     xlab=expression(sigma[p]), ylab=expression(mu[p]), cex.lab = cex.val)
points(sig.p.B, mu.p.B, type="b", col="red", pch=16, cex = cex.val)
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=1, cex = cex.val)
text(x=0, y=r.f, labels=expression(r[f]), pos=2, cex = cex.val)
```

* Portfolios of T-Bills and asset A are efficient relative to portfolios of T-Bills and asset B

## Portfolios of T-Bills and 2 risky assets

```{r, echo=FALSE}
rho.AB = -0.164
sig.AB = rho.AB*sig.A*sig.B
top = (mu.A - r.f)*sig2.B - (mu.B - r.f)*sig.AB
bot = (mu.A - r.f)*sig2.B + (mu.B - r.f)*sig2.A - (mu.A - r.f + mu.B - r.f)*sig.AB
x.A.tan = top/bot
x.B.tan = 1 - x.A.tan
mu.p.tan = x.A.tan*mu.A + x.B.tan*mu.B
sig2.p.tan = x.A.tan^2 * sig2.A + x.B.tan^2 * sig2.B + 2*x.A.tan*x.B.tan*sig.AB
sig.p.tan = sqrt(sig2.p.tan)

# T-bills plus tangency
x.tan = seq(from=0, to=2.4, by=0.1)
mu.p.tan.tbill = r.f + x.tan*(mu.p.tan - r.f)
sig.p.tan.tbill = x.tan*sig.p.tan

# plot portfolios w/o tangency portfolio
plot(sig.p, mu.p, type="b", pch=16, cex = cex.val,
     ylim=c(0, max(mu.p)), xlim=c(0, max(sig.p)),
     xlab=expression(sigma[p]), ylab=expression(mu[p]), cex.lab = cex.val)
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)
text(x=sig.p.min, y=mu.p.min, labels="Global min", pos=4, cex = cex.val)
text(x=0, y=r.f, labels=expression(r[f]), pos=2, cex = cex.val)
# text(x=sig.p.tan, y=mu.p.tan, labels="Tangency", pos=2, cex = cex.val)
points(sig.p.A, mu.p.A, type="b", col="red", pch=16, cex = cex.val)
points(sig.p.B, mu.p.B, type="b", col="blue", pch=16, cex = cex.val)

```

Q: Can you find the portfolio of assets A and B that has the highest Sharpe slope?

## Portfolios of T-Bills and 2 risky assets

```{r, echo=FALSE}
plot(sig.p, mu.p, type="b", pch=16, cex = cex.val,
     ylim=c(0, max(mu.p)), xlim=c(0, max(sig.p)),
     xlab=expression(sigma[p]), ylab=expression(mu[p]), cex.lab = cex.val)
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)
text(x=sig.p.min, y=mu.p.min, labels="Global min", pos=4, cex = cex.val)
text(x=0, y=r.f, labels=expression(r[f]), pos=2, cex = cex.val)
text(x=sig.p.tan, y=mu.p.tan, labels="Tangency", pos=2, cex = cex.val)
points(sig.p.A, mu.p.A, type="b", col="red", pch=16, cex = cex.val)
points(sig.p.B, mu.p.B, type="b", col="blue", pch=16, cex = cex.val)
points(sig.p.tan.tbill, mu.p.tan.tbill, type="b", col="green", pch=16, cex = cex.val)

```

* The portfolio of assets A and B with the highest Sharpe slope is the tangency portfolio

## Efficient portfolios and risk preferences

```{r, echo=FALSE}
mu.safe = r.f + 0.10*(mu.p.tan - r.f)
sig.safe = 0.10*sig.p.tan
# risky portfolio: 110% in tangency and -10% in T-bills
mu.risky = r.f + 2*(mu.p.tan - r.f)
sig.risky = 2*sig.p.tan
plot(sig.p, mu.p, type="b", pch=16, cex = cex.val,
     ylim=c(0, max(mu.p)), xlim=c(0, max(sig.p)), cex.lab = cex.val,
     xlab=expression(sigma[p]), ylab=expression(mu[p]))
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)
text(x=sig.p.min, y=mu.p.min, labels="Global min", pos=4, cex = cex.val)
text(x=0, y=r.f, labels=expression(r[f]), pos=2, cex = cex.val)
text(x=sig.p.tan, y=mu.p.tan, labels="Tangency", pos=2, cex = cex.val)
points(x=sig.p.tan, y=mu.p.tan, pch=16, cex=2, col="green")
points(sig.p.tan.tbill, mu.p.tan.tbill, type="b", col="green", 
       pch=16, cex = cex.val)
points(x=sig.safe, y=mu.safe, pch=16, cex=2)
text(x=sig.safe, y=mu.safe, labels="Safe", pos=3, cex = cex.val)
points(x=sig.risky, y=mu.risky, pch=16, cex=2)
text(x=sig.risky, y=mu.risky, labels="Risky", pos=3, cex = cex.val)
```

* The point labeled "Safe" has 90% T-Bills and 10% tangency portfolio
* The point labeled "Risky" has -100% T-Bills (leverage) amd 200% tangency

## Interpreting efficient portfolios

```{r, echo=FALSE}
x.t1 = sig.B/sig.p.tan
mu.p1.e = r.f + x.t1*(mu.p.tan - r.f)
sig.p1.e = x.t1*sig.p.tan

# find combination of T-bills and tangency that has same ER as asset B
x.t2 = (mu.B - r.f)/(mu.p.tan - r.f)
mu.p2.e = r.f + x.t2*(mu.p.tan - r.f)
sig.p2.e = x.t2*sig.p.tan

plot(sig.p, mu.p, type="b", pch=16, cex = cex.val,
     ylim=c(0, max(mu.p)), xlim=c(0, max(sig.p)),
     xlab=expression(sigma[p]), ylab=expression(mu[p]))
text(x=sig.A, y=mu.A, labels="Asset A", pos=4, cex = cex.val)
text(x=sig.B, y=mu.B, labels="Asset B", pos=4, cex = cex.val)
text(x=sig.p.min, y=mu.p.min, labels="Global min", pos=4, cex = cex.val)
text(x=0, y=r.f, labels=expression(r[f]), pos=2, cex = cex.val)
text(x=sig.p.tan, y=mu.p.tan, labels="Tangency", pos=4, cex = cex.val)
points(x=sig.p.tan, y=mu.p.tan, pch=16, col="green", cex = cex.val)
points(sig.p.tan.tbill, mu.p.tan.tbill, type="b", col="green", pch=16, cex = cex.val)
segments(x0=sig.B, y0=0, x1=sig.B, y1=mu.p1.e, lwd=3, lty="dotted")
segments(x0=sig.B, y0=mu.p1.e, x1=0, y1=mu.p1.e, lwd=3, lty="dotted")
segments(x0=sig.p2.e, y0=0, x1=sig.p2.e, y1=mu.p2.e, lwd=3, lty="dotted")
segments(x0=0, y0=mu.p2.e, x1=sig.B, y1=mu.p2.e, lwd=3, lty="dotted")
text(x=sig.B, y=mu.p1.e, labels="e1", pos=3, cex = cex.val)
text(x=sig.p2.e, y=mu.B, labels="e2", pos=3, cex = cex.val)

```

* e1: Efficient portfolio with the same volatility as asset B: $x_{t}=$ `r x.t1`, $x_{f}=$ `r 1-x.t1`
* e2: Efficient portfolio with the same mean as asset B: $x_{t}=$ `r x.t2`, $x_{f}=$ `r 1-x.t2`
