Disclaimer: The content of this RMarkdown note came from a course called Introduction to Portfolio Analysis in R in datacamp.

Chapter 1 The building blocks

1.1 Calculating portfolio weights when component values are given

# Define the vector values
values <- c(4000, 4000, 2000)

# Define the vector weights
weights <- values / sum(values)

# Print the resulting weights
weights  
## [1] 0.4 0.4 0.2

Interpretation

  • Remember that the weight of an asset is calculated by taking the value of the asset divided by the sum of values of all assets. You can also pre-define weights as a vector. For example weights <- c(0.2, 0.2, 0.6).

1.2 The weights of an equally weighted portfolio

In an equally weighted portfolio, you invest an equal amount of capital in each asset. Define the weight vector for an equally weighted portfolio.

# case of four assets
N = 4
weights <- rep(1/N,N)

1.3 The weights of a market capitalization weighted portfolio

In a market capitalization weighted portfolio, the weights are given by the individual assets’ market capitalization (or market value), divided by the sum of the market capitalizations of all assets. A typical example is the S&P 500 portfolio.

# Define marketcaps
 marketcaps <- c(5,8,9,20,25,100,100,500,700,2000)
  
# Compute the weights
weights <- marketcaps / sum(marketcaps)
  
# Inspect summary statistics
summary(weights)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.001442 0.003389 0.018030 0.100000 0.115400 0.576900
  
# Create a barplot of weights
  barplot(weights)

1.4 Calculation of portfolio returns

Calculate a portfolio return by taking the sum of simple returns multiplied by the portfolio weights.

Assume that you invested in three assets. Their initial values are 1000 USD, 5000 USD, 2000 USD respectively. Over time, the values change to 1100 USD, 4500 USD, and 3000 USD.

# Vector of initial value of the assets
in_values <- c(1000, 5000, 2000)
  
# Vector of final values of the assets
fin_values <- c(1100, 4500, 3000)

# Weights as the proportion of total value invested in each assets
weights <- in_values / sum(in_values)

# Vector of simple returns of the assets 
returns <- (fin_values - in_values) / in_values
  
# Compute portfolio return using the portfolio return formula
preturns <- sum(returns * weights)

Interpretation

  • Note that the weighted average portfolio return equals the percentage change in the total portfolio value. Remember that portfolio returns are calculated by taking the sum of simple returns, multiplied by the portfolio weights.

1.5 From simple to gross and multi-period returns

  • The simple return R is the percentage change in the value of an investment.
  • The gross return is equal to R + 1 and defined as the future value of $1 invested in the asset for one period.
  • The gross return over two periods = (R1 + 1) * (R2 + 1)
  • The simple return over two periods = (R1 + 1) * (R2 + 1) - 1

1.6 The asymmetric impact of gains and losses

A positive and negative return of the same relative magnitude do not compensate each other in terms of terminal wealth. For example, a 50% loss followed a 50% gain would not result in the initial amount. ### 1.7 Buy-and-hold versus (daily) rebalancing

  • A buy and hold strategy means that you buy and do not trade any further.
  • A rebalanced portfolio means that you trade at the end of a period so that weights of assets remain the same.

1.8 The time series of asset returns

Calculate the returns of assets by using Return.calculate() in the R package PerformanceAnalytics. Its manual can be found here.

# Load packages  
library(PerformanceAnalytics)
library(quantmod)
library(xts)

# Import data
prices <- read.csv("data/Portfolio Theory/prices.csv")

prices$Date <- as.Date(prices$Date, format("%m/%d/%Y"))
prices <- prices[order(prices$Date), ]
prices <- as.xts(prices[, 2:3], order.by = prices$Date)

# Import data (alternative)
#data.AAPL <- getSymbols("AAPL", from = "2005-12-31", to = "2016-08-31", auto.assign = FALSE)
#data.MSFT <- getSymbols("MSFT", from = "2005-12-31", to = "2016-08-31", auto.assign = FALSE)


# Print the first and last six rows of prices
head(prices)
##                 AAPL     MSFT
## 2006-01-03  9.776503 20.94342
## 2006-01-04  9.805277 21.04486
## 2006-01-05  9.728111 21.06047
## 2006-01-06  9.979226 20.99804
## 2006-01-09  9.946529 20.95903
## 2006-01-10 10.575626 21.06827
tail(prices)
##              AAPL  MSFT
## 2016-08-24 108.03 57.95
## 2016-08-25 107.57 58.17
## 2016-08-26 106.94 58.03
## 2016-08-29 106.82 58.10
## 2016-08-30 106.00 57.89
## 2016-08-31 106.10 57.46

# Create the variable returns using Return.calculate()  
returns <- Return.calculate(prices)
  
# Print the first six rows of returns. Note that the first observation is NA, because there is no prior price.
head(returns)
##                    AAPL          MSFT
## 2006-01-03           NA            NA
## 2006-01-04  0.002943179  0.0048435260
## 2006-01-05 -0.007869844  0.0007417488
## 2006-01-06  0.025813336 -0.0029643213
## 2006-01-09 -0.003276507 -0.0018577924
## 2006-01-10  0.063247893  0.0052120733
tail(returns)
##                     AAPL         MSFT
## 2016-08-24 -0.0075332937  0.001036448
## 2016-08-25 -0.0042580672  0.003796376
## 2016-08-26 -0.0058566329 -0.002406739
## 2016-08-29 -0.0011221432  0.001206273
## 2016-08-30 -0.0076764651 -0.003614458
## 2016-08-31  0.0009433774 -0.007427880

# Remove the first row of returns
returns <- returns[-1, ]

1.9 The time series of portfolio returns

Create 1) a portfolio in which you don’t rebalance, and 2) one where you rebalance monthly. Use the Return.portfolio() function.

# Create the weights
eq_weights <- c(0.5, 0.5)

# Create a portfolio using buy and hold
pf_bh <- Return.portfolio(returns, weights = eq_weights)

# Create a portfolio rebalancing monthly 
pf_rebal <- Return.portfolio(returns, weights = eq_weights, rebalance_on = "months")

# Plot the time-series
par(mfrow = c(2, 1), mar = c(2, 4, 2, 2))
plot.zoo(pf_bh)
plot.zoo(pf_rebal)

Interpreation

  • Can you see that rebalancing a portfolio can lead to less extreme returns in either direction?

1.10 The time series of weights

Create a list of beginning of period (BOP) and end of period (EOP) weights and values as well as portfolio returns and contributions, by setting the argument verbose = TRUE in Return.portfolio().

Access these from the resultant list-object created from Return.portfolio() by typing:

  • $returns
  • $contributions
  • $BOP.Weight
  • $EOP.Weight
  • $BOP.Value
  • $EOP.Value
# Create the weights
eq_weights <- c(0.5, 0.5)

# Create a portfolio using buy and hold
pf_bh <- Return.portfolio(returns, weights = eq_weights, verbose = TRUE )

# Create a portfolio that rebalances monthly
pf_rebal <- Return.portfolio(returns, weights = eq_weights, rebalance_on = "months", verbose = TRUE )

# Create eop_weight_bh
eop_weight_bh <- pf_bh$EOP.Weight
head(eop_weight_bh)
##                 AAPL      MSFT
## 2006-01-04 0.4995268 0.5004732
## 2006-01-05 0.4973662 0.5026338
## 2006-01-06 0.5044797 0.4955203
## 2006-01-09 0.5041241 0.4958759
## 2006-01-10 0.5181486 0.4818514
## 2006-01-11 0.5246922 0.4753078

# Create eop_weight_rebal
eop_weight_rebal <- pf_rebal$EOP.Weight
head(eop_weight_rebal)
##                 AAPL      MSFT
## 2006-01-04 0.4995268 0.5004732
## 2006-01-05 0.4973662 0.5026338
## 2006-01-06 0.5044797 0.4955203
## 2006-01-09 0.5041241 0.4958759
## 2006-01-10 0.5181486 0.4818514
## 2006-01-11 0.5246922 0.4753078

# Plot end of period weights
par(mfrow = c(2, 1), mar=c(2, 4, 2, 2))
plot.zoo(eop_weight_bh$AAPL)
plot.zoo(eop_weight_rebal$AAPL)

Interpreation

  • The weights of Apple in the rebalanced portfolio stay close to the initial weight of 50%.
  • The weight of Apple in the buy and hold strategy rose above 80% during much of the second half of the period.

Chapter 2 Analyzing performance

This chapter introduces the R functionality to analyze the investment performance based on a statisical analysis of the portfolio returns. It includes graphical analysis and the calculation of performance statistics expressing average return, risk and risk-adjusted return over rolling estimation samples.

2.1 Exploring the monthly S&P 500 returns

Dimensions of portfolio performance

  1. Reward: portfolio mean return
    • Arithmetic versus geometric mean
  2. Risk: portfolio return volatility
# Import data
sp500 <- read.csv("data/Portfolio Theory/sp500.csv")

sp500$Date <- as.Date(sp500$Date, format("%m/%d/%Y"))
sp500 <- sp500[order(sp500$Date), ]
sp500 <- as.xts(sp500[, 2], order.by = sp500$Date)

# Convert the daily frequency of sp500 to monthly frequency: sp500_monthly
sp500_monthly <- to.monthly(sp500)

# Print the first six rows of sp500_monthly
head(sp500_monthly)
##          sp500.Open sp500.High sp500.Low sp500.Close
## Dec 1985     211.28     211.28    211.28      211.28
## Jan 1986     211.78     211.78    211.78      211.78
## Feb 1986     226.92     226.92    226.92      226.92
## Mar 1986     238.90     238.90    238.90      238.90
## Apr 1986     235.52     235.52    235.52      235.52
## May 1986     247.35     247.35    247.35      247.35

# Create sp500_returns using Return.calculate using the closing prices
sp500_returns <- Return.calculate(sp500_monthly$sp500.Close)
head(sp500_returns)
##           sp500.Close
## Dec 1985           NA
## Jan 1986  0.002366528
## Feb 1986  0.071489281
## Mar 1986  0.052793936
## Apr 1986 -0.014148179
## May 1986  0.050229280

# Time series plot
plot.zoo(sp500_returns)


# Produce the year x month table
table.CalendarReturns(sp500_returns)
##       Jan   Feb  Mar  Apr  May  Jun  Jul   Aug   Sep   Oct  Nov  Dec
## 1985   NA    NA   NA   NA   NA   NA   NA    NA    NA    NA   NA   NA
## 1986  0.2   7.1  5.3 -1.4  5.0  1.4 -5.9   7.1  -8.5   5.5  2.1 -2.8
## 1987 13.2   3.7  2.6 -1.1  0.6  4.8  4.8   3.5  -2.4 -21.8 -8.5  7.3
## 1988  4.0   4.2 -3.3  0.9  0.3  4.3 -0.5  -3.9   4.0   2.6 -1.9  1.5
## 1989  7.1  -2.9  2.1  5.0  3.5 -0.8  8.8   1.6  -0.7  -2.5  1.7  2.1
## 1990 -6.9   0.9  2.4 -2.7  9.2 -0.9 -0.5  -9.4  -5.1  -0.7  6.0  2.5
## 1991  4.2   6.7  2.2  0.0  3.9 -4.8  4.5   2.0  -1.9   1.2 -4.4 11.2
## 1992 -2.0   1.0 -2.2  2.8  0.1 -1.7  3.9  -2.4   0.9   0.2  3.0  1.0
## 1993  0.7   1.0  1.9 -2.5  2.3  0.1 -0.5   3.4  -1.0   1.9 -1.3  1.0
## 1994  3.3  -3.0 -4.6  1.2  1.2 -2.7  3.1   3.8  -2.7   2.1 -4.0  1.2
## 1995  2.4   3.6  2.7  2.8  3.6  2.1  3.2   0.0   4.0  -0.5  4.1  1.7
## 1996  3.3   0.7  0.8  1.3  2.3  0.2 -4.6   1.9   5.4   2.6  7.3 -2.2
## 1997  6.1   0.6 -4.3  5.8  5.9  4.3  7.8  -5.7   5.3  -3.4  4.5  1.6
## 1998  1.0   7.0  5.0  0.9 -1.9  3.9 -1.2 -14.6   6.2   8.0  5.9  5.6
## 1999  4.1  -3.2  3.9  3.8 -2.5  5.4 -3.2  -0.6  -2.9   6.3  1.9  5.8
## 2000 -5.1  -2.0  9.7 -3.1 -2.2  2.4 -1.6   6.1  -5.3  -0.5 -8.0  0.4
## 2001  3.5  -9.2 -6.4  7.7  0.5 -2.5 -1.1  -6.4  -8.2   1.8  7.5  0.8
## 2002 -1.6  -2.1  3.7 -6.1 -0.9 -7.2 -7.9   0.5 -11.0   8.6  5.7 -6.0
## 2003 -2.7  -1.7  0.8  8.1  5.1  1.1  1.6   1.8  -1.2   5.5  0.7  5.1
## 2004  1.7   1.2 -1.6 -1.7  1.2  1.8 -3.4   0.2   0.9   1.4  3.9  3.2
## 2005 -2.5   1.9 -1.9 -2.0  3.0  0.0  3.6  -1.1   0.7  -1.8  3.5 -0.1
## 2006  2.5   0.0  1.1  1.2 -3.1  0.0  0.5   2.1   2.5   3.2  1.6  1.3
## 2007  1.4  -2.2  1.0  4.3  3.3 -1.8 -3.2   1.3   3.6   1.5 -4.4 -0.9
## 2008 -6.1  -3.5 -0.6  4.8  1.1 -8.6 -1.0   1.2  -9.1 -16.9 -7.5  0.8
## 2009 -8.6 -11.0  8.5  9.4  5.3  0.0  7.4   3.4   3.6  -2.0  5.7  1.8
## 2010 -3.7   2.9  5.9  1.5 -8.2 -5.4  6.9  -4.7   8.8   3.7 -0.2  6.5
## 2011  2.3   3.2 -0.1  2.8 -1.4 -1.8 -2.1  -5.7  -7.2  10.8 -0.5  0.9
## 2012  4.4   4.1  3.1 -0.7 -6.3  4.0  1.3   2.0   2.4  -2.0  0.3  0.7
## 2013  5.0   1.1  3.6  1.8  2.1 -1.5  4.9  -3.1   3.0   4.5  2.8  2.4
## 2014 -3.6   4.3  0.7  0.6  2.1  1.9 -1.5   3.8  -1.6   2.3  2.5 -0.4
## 2015 -3.1   5.5 -1.7  0.9  1.0 -2.1  2.0  -6.3  -2.6   8.3  0.1 -1.8
## 2016 -5.1  -0.4  6.6  0.3  1.5  0.1  3.6  -0.1    NA    NA   NA   NA
##      sp500.Close
## 1985          NA
## 1986        14.6
## 1987         2.0
## 1988        12.4
## 1989        27.3
## 1990        -6.6
## 1991        26.3
## 1992         4.5
## 1993         7.1
## 1994        -1.5
## 1995        34.1
## 1996        20.3
## 1997        31.0
## 1998        26.7
## 1999        19.5
## 2000       -10.1
## 2001       -13.0
## 2002       -23.4
## 2003        26.4
## 2004         9.0
## 2005         3.0
## 2006        13.6
## 2007         3.5
## 2008       -38.5
## 2009        23.5
## 2010        12.8
## 2011         0.0
## 2012        13.4
## 2013        29.6
## 2014        11.4
## 2015        -0.7
## 2016         6.2

2.2 The monthly mean and volatility

Calculate the mean (arithmetic and geometric) and volatility (standard deviation) of the returns. In calculating returns, geometric mean is used because the arithmetic mean of a 50% loss followed a 50% gain would be a 0% gain when, in fact, it should be 13.4% less than the initial investment.

  • arithmetic mean: (50% - 50%) / 2 = 0%
  • geometric mean: ((1 + 50%) * (1 - 50%))^(1/2) = -13.4%
# Define sp500_returns
sp500_returns <- Return.calculate(sp500)
sp500_returns <- sp500_returns[-1, ]
colnames(sp500_returns) <- "sp500"
head(sp500_returns)
##                   sp500
## 1986-01-02  0.002366528
## 1986-02-03  0.071489281
## 1986-03-03  0.052793936
## 1986-04-01 -0.014148179
## 1986-05-01  0.050229280
## 1986-06-02  0.014109561

# Compute the mean monthly returns
mean(sp500_returns)
## [1] 0.007320322

# Compute the geometric mean of monthly returns
mean.geometric(sp500_returns)
##                      sp500
## Geometric Mean 0.006350886

# Compute the standard deviation
sd(sp500_returns)
## [1] 0.04364617

2.3 Excess returns and the portfolio’s Sharpe ratio

To evaluate your portfolio’s performance, compare returns (reward) and volatility (risk) to benchmark, such as the US Treasury bill (risk-free asset).

The (annualized) Sharp Ratio

  • (portfolio mean return – U.S. Treasury’s return) / portfolio’s volatility
  • A measure of the risk-adjusted return for the portfolio
  • Compared to U.S. Treasury: 0 volatility and its mean return
# Import data
rf <- read.csv("data/Portfolio Theory/rf.csv")

rf$Date <- as.Date(rf$Date, format("%m/%d/%Y"))
rf <- rf[order(rf$Date), ]
rf <- as.xts(rf[, 2], order.by = rf$Date)
head(rf)
##              [,1]
## 1986-01-31 0.0053
## 1986-02-28 0.0060
## 1986-03-31 0.0052
## 1986-04-30 0.0049
## 1986-05-31 0.0052
## 1986-06-30 0.0052

# Align dates of sp500_returns
merged <- merge(rf, sp500_returns)
head(merged)
##                rf       sp500
## 1986-01-02     NA 0.002366528
## 1986-01-31 0.0053          NA
## 1986-02-03     NA 0.071489281
## 1986-02-28 0.0060          NA
## 1986-03-03     NA 0.052793936
## 1986-03-31 0.0052          NA
merged <- na.locf(merged)
head(merged)
##                rf       sp500
## 1986-01-02     NA 0.002366528
## 1986-01-31 0.0053 0.002366528
## 1986-02-03 0.0053 0.071489281
## 1986-02-28 0.0060 0.071489281
## 1986-03-03 0.0060 0.052793936
## 1986-03-31 0.0052 0.052793936
merged <- merged[index(rf)]
head(merged)
##                rf        sp500
## 1986-01-31 0.0053  0.002366528
## 1986-02-28 0.0060  0.071489281
## 1986-03-31 0.0052  0.052793936
## 1986-04-30 0.0049 -0.014148179
## 1986-05-31 0.0052  0.050229280
## 1986-06-30 0.0052  0.014109561
sp500_returns <- merged[, 2]
head(sp500_returns)
##                   sp500
## 1986-01-31  0.002366528
## 1986-02-28  0.071489281
## 1986-03-31  0.052793936
## 1986-04-30 -0.014148179
## 1986-05-31  0.050229280
## 1986-06-30  0.014109561

# Compute the annualized risk free rate
annualized_rf <- (1 + rf)^12 - 1

# Plot the annualized risk free rate
plot.zoo(annualized_rf)


# Compute the series of excess portfolio returns
# Adjust date for sp500_returns. This code below doesn't work b/c two objects date don't match
sp500_excess <- sp500_returns - rf

head(sp500_returns)
##                   sp500
## 1986-01-31  0.002366528
## 1986-02-28  0.071489281
## 1986-03-31  0.052793936
## 1986-04-30 -0.014148179
## 1986-05-31  0.050229280
## 1986-06-30  0.014109561
head(rf)
##              [,1]
## 1986-01-31 0.0053
## 1986-02-28 0.0060
## 1986-03-31 0.0052
## 1986-04-30 0.0049
## 1986-05-31 0.0052
## 1986-06-30 0.0052

# Compare the mean
mean(sp500_returns)
## [1] 0.007320322
mean(sp500_excess)
## [1] 0.004582279

# Compute the Sharpe ratio
sp500_sharpe <- mean(sp500_excess) / sd(sp500_returns)
sp500_sharpe
## [1] 0.104987

2.4 Annualized mean and volatility

The average and standard deviation of returns over a monthly investment horizon can be annualized by using Return.annualized() and StdDev.annualized() in the PerformanceAnalytics package.

# Compute the annualized mean
Return.annualized(sp500_returns)
##                        sp500
## Annualized Return 0.07892983

# Compute the annualized standard deviation
StdDev.annualized(sp500_returns)
##                                   sp500
## Annualized Standard Deviation 0.1511948

# Compute the annualized Sharpe ratio: ann_sharpe
ann_sharpe <- Return.annualized(sp500_returns) / StdDev.annualized(sp500_returns)

# Compute all of the above at once using table.AnnualizedReturns()
table.AnnualizedReturns(sp500_returns)
##                            sp500
## Annualized Return         0.0789
## Annualized Std Dev        0.1512
## Annualized Sharpe (Rf=0%) 0.5220

2.5 Effect of window length choice

Due to the dynamic nature of stock markets and constantly changing environment, its performance analysis should give more weights on recent observations than on distance observations. The standard approach of doing this is the use of rolling estimation samples.

How to determine the length of rolling samples

  • Short samples: recent but volatile (good for detecting recent changes)
  • Long samples: not as recent but less noise (good for detecting underlying long-term trend)

2.6 Rolling annualized mean and volatility

Visualize the rolling estimates of performance by using chartRollingPerformance().

# Calculate the mean, volatility, and sharpe ratio of sp500_returns
returns_ann = Return.annualized(sp500_returns)
sd_ann = StdDev.annualized(sp500_returns)
sharpe_ann = SharpeRatio.annualized(sp500_returns, Rf = rf)



# Plotting the 12-month rolling annualized mean
chart.RollingPerformance(R = sp500_returns, width = 12, FUN = "Return.annualized")
abline(h = returns_ann)


# Plotting the 12-month rolling annualized standard deviation
chart.RollingPerformance(R = sp500_returns, width = 12, FUN = "StdDev.annualized")
abline(h = sd_ann)



# Plotting the 12-month rolling annualized Sharpe ratio
chart.RollingPerformance(R = sp500_returns, width = 12, FUN = "SharpeRatio.annualized", Rf = rf)
abline(h = sharpe_ann)


# Plotting all three charts together. 
charts.RollingPerformance(R = sp500_returns, width = 12, Rf = rf) # Note the charts instead of chart. 

2.7 Subperiod performance analysis and the function window

Measure the performance of a specific subwindow by using the function window().

# Import data
sp500_returns <- read.csv("data/Portfolio Theory/sp500Daily.csv")

sp500_returns$Date <- as.Date(sp500_returns$Date, format("%m/%d/%Y"))
sp500_returns <- sp500_returns[order(sp500_returns$Date), ]
sp500_returns <- as.xts(sp500_returns[, 2], order.by = sp500_returns$Date)
colnames(sp500_returns) <- "sp500"
head(sp500_returns)
##                   sp500
## 1986-01-02 -0.007998878
## 1986-01-03  0.006154917
## 1986-01-06 -0.001090720
## 1986-01-07  0.014953760
## 1986-01-08 -0.027268480
## 1986-01-09 -0.008943598

# Fill in window for 2008
sp500_2008 <- window(sp500_returns, start = "2008-01-01", end = "2008-12-31")
head(sp500_2008)
##                   sp500
## 2008-01-02 -0.014437840
## 2008-01-03  0.000000000
## 2008-01-04 -0.024551550
## 2008-01-07  0.003223259
## 2008-01-08 -0.018352270
## 2008-01-09  0.013624080

# Create window for 2014
sp500_2014 <- window(sp500_returns, start = "2014-01-01", end = "2014-12-31")
head(sp500_2014)
##                   sp500
## 2014-01-02 -0.008861913
## 2014-01-03 -0.000332965
## 2014-01-06 -0.002511767
## 2014-01-07  0.006081764
## 2014-01-08 -0.000212209
## 2014-01-09  0.000348309

# Plotting settings
par(mfrow = c(1, 2) , mar=c(3, 2, 2, 2))
names(sp500_2008) <- "sp500_2008"
names(sp500_2014) <- "sp500_2014"

# Plot histogram of 2008
chart.Histogram(sp500_2008, methods = c("add.density", "add.normal"))

# Plot histogram of 2014
chart.Histogram(sp500_2014, methods = c("add.density", "add.normal"))

2.8 Balancing risk and reward

A risk-averse investor would prefer an investment that has the highest average return with the lowest volatility.

Non-normality of the return distribution

  1. When we use the standard deviation as a measure of risk, we assume that portfolio returns have a normal distribution. It means that large gains (positive extremes) are as equally likely as large losses (negative extremes) of the same magnitude.
  2. In reality, returns are skewed to the left with fatter tails: negative returns are more likely.
  3. Hence, need for additional risk measures.
    • Semi-deviation, Value at risk, and 5% expected short fall
  4. Additional measures
    • Skewness, kurtosis
  5. measure of the worst case risk
    • the portfolio’s drawdowns, or peak-to-trough decline in cumulative returns.
    • The metrics discussed above do not do a great job at describing the worst case risk of buying at a peak, and selling at a trough.

2.9 Detecting non-normality using skewness and kurtosis

Two metrics key to understanding the distribution of non-normal returns:

  • Skewness (is it symmetric?)
  • Excess kurtosis (does it have fat-tails?)

Skewness

  • Zero: symmetric
  • Negative: large negative returns occur more often
  • Positive: large positive returns occur more often

Kurtosis

  • Zero: normal distribution
  • Greater than zero: large returns of both positive and negative occur more often
# Define sp500_daily and sp500_monthly
sp500_daily <- sp500_returns
head(sp500_daily)
##                   sp500
## 1986-01-02 -0.007998878
## 1986-01-03  0.006154917
## 1986-01-06 -0.001090720
## 1986-01-07  0.014953760
## 1986-01-08 -0.027268480
## 1986-01-09 -0.008943598

sp500_monthly <- read.csv("data/Portfolio Theory/sp500.csv")

sp500_monthly$Date <- as.Date(sp500_monthly$Date, format("%m/%d/%Y"))
sp500_monthly <- sp500_monthly[order(sp500_monthly$Date), ]
sp500_monthly <- as.xts(sp500_monthly[, 2], order.by = sp500_monthly$Date)
sp500_monthly <- Return.calculate(sp500_monthly)
sp500_monthly <- sp500_monthly[-1, ]
colnames(sp500_monthly) <- "sp500"
head(sp500_monthly)
##                   sp500
## 1986-01-02  0.002366528
## 1986-02-03  0.071489281
## 1986-03-03  0.052793936
## 1986-04-01 -0.014148179
## 1986-05-01  0.050229280
## 1986-06-02  0.014109561

#  Compute the skewness 
skewness(sp500_daily)  
## [1] -0.825167
skewness(sp500_monthly)  
## [1] -0.7916029

# Compute the excess kurtois 
kurtosis(sp500_daily)
## [1] 20.88491
kurtosis(sp500_monthly)
## [1] 2.435507

Interpreation

  • The combination of a negative value of skewness and a positive value of kurtosis indicates that large negative returns are more likely than large positive returns.

2.10 Downside risk measures

When the return distribution is asymmetric (skewed), investors use additional risk measures that focus on describing the potential losses.

  • Semi-deviation: the variability of returns below the mean return
  • Value-at-Risk (or VaR): the 5% quantile of the return distribution, meaning that a more negative return can only happen with a probability of 5%. For example, you might ask: “what is the largest loss I could potentially take within the next quarter, with 95% confidence?”
  • Expected Shortfall: the average of the 5% (p = 0.05) or 2.5% (p = 0.025) most negative returns
# Calculate the SemiDeviation
SemiDeviation(sp500_monthly)
##                     sp500
## Semi-Deviation 0.03334653

# Calculate the value at risk
VaR(sp500_monthly, p = 0.025)
##          sp500
## VaR -0.0977536
VaR(sp500_monthly, p = 0.05)
##           sp500
## VaR -0.07152617

# Calculate the expected shortfall
ES(sp500_monthly, p = 0.025)
##         sp500
## ES -0.1653925
ES(sp500_monthly, p = 0.05)
##         sp500
## ES -0.1180061

Interpreation

  • Semi-Deviation of 0.033 means that the standard deviation of returns below the mean return is 0.033.
  • VaR of - 0.071 means that – 7.1% is the largest loss one could expect with 95% confidence.
  • ES of – 0.118 means that – 11.8% is the average of the 5% (p = 0.05) most negative returns.

2.11 Drawdowns due to buying high, selling low

The volatility, semi-deviation, value-at-risk, and expected shortfall are all measures that describe risk over 1 period. These metrics do not do a great job at describing the worst case risk of buying at a peak, and selling at a trough. This sort of risk can be quantified by analyzing the portfolio’s drawdowns, or peak-to-trough decline in cumulative returns.

The function table.Drawdowns() in PerformanceAnalytics reports the five largest drawdown episodes over a sample period. The package also has another function chart.Drawdown() to visualize the evolution of the drawdowns from each peak over time.

# Table of drawdowns
table.Drawdowns(sp500_monthly)
##         From     Trough         To   Depth Length To Trough Recovery
## 1 2007-11-01 2009-02-02 2013-03-01 -0.5256     65        16       49
## 2 2000-09-01 2002-09-03 2007-05-01 -0.4628     81        25       56
## 3 1987-09-01 1987-11-02 1989-07-03 -0.3017     23         3       20
## 4 1990-06-01 1990-10-01 1991-02-01 -0.1584      9         5        4
## 5 1998-07-01 1998-08-03 1998-11-02 -0.1557      5         2        3

# Plot of drawdowns
chart.Drawdown(sp500_monthly)

Interpreation

  • The worst cumulative loss from peak value happend over the 16-months period from November 2007 to February 2009.
  • The asset lost more than half of its value during the period.
  • It took 49 months to recover to the previous peak value.

Chapter 3 Performance drivers

Find out how individual (expected) returns, volatilities and correlations interact to determine the total portfolio performance.

Portfolio drivers:

  1. The assets’ individual performance
  2. The choice of portfolio weights: individuals can obtain the highest risk-adjusted return, as measured by the Sharpe ratio, by optimizing the choice of weight.
  3. The correlation b/t the asset returns: the lower the correlation, the more successful the portfolio tends to be in regards to partially offsetting large losses in one asset with only a minor loss, or even a gain in another asset.
    • When the correlation is 1, there is no diversification potential.
    • When the correlation is negative, if one asset return is above average, and the other is almost always below average.
    • When the correlation is 0, the asset returns are linearly independent of each other. Note that interdependency can still exist on a non-linear level even when the correlation is 0.

3.1 Driver 1: The assets’ individual performance

3.2 Driver 2: The choice of portfolio weights

Identify the weight that yields the highest risk-adjusted return, as measured by the Sharpe ratio in the case of a portfolio invested US equities and US bonds. We will be using the brute force approach of trying a large number of possible weights.

# Import data
returns_equities <- read.csv("data/Portfolio Theory/returns_equities.csv")
returns_equities$Date <- as.Date(returns_equities$Date, format("%m/%d/%Y"))
returns_equities <- returns_equities[order(returns_equities$Date), ]
returns_equities <- as.xts(returns_equities[, 2], order.by = returns_equities$Date)
colnames(returns_equities) <- "equities"
head(returns_equities)
##              equities
## 2006-08-30 0.02182242
## 2006-09-29 0.02700156
## 2006-10-30 0.03151669
## 2006-11-29 0.01988532
## 2006-12-30 0.01337125
## 2007-01-30 0.01504026

returns_bonds <- read.csv("data/Portfolio Theory/returns_bonds.csv")
returns_bonds$Date <- as.Date(returns_bonds$Date, format("%m/%d/%Y"))
returns_bonds <- returns_bonds[order(returns_bonds$Date), ]
returns_bonds <- as.xts(returns_bonds[, 2], order.by = returns_bonds$Date)
colnames(returns_bonds) <- "bonds"
head(returns_bonds)
##                bonds
## 2006-08-30  0.015900
## 2006-09-29  0.010100
## 2006-10-30  0.006860
## 2006-11-29  0.010600
## 2006-12-30 -0.006000
## 2007-01-30 -0.000702

# Create a grid
grid <- seq(from = 0, to = 1, by = 0.01)

# Initialize an empty vector for sharpe ratios
vsharpe <- rep(NA, times = 100 )

# Create a for loop to calculate Sharpe ratios
for(i in 1:length(grid)) {
    weight <- grid[i]
    preturns <- weight * returns_equities + (1 - weight) * returns_bonds
    vsharpe[i] <- SharpeRatio.annualized(preturns)
}

# Plot weights and Sharpe ratio
plot(grid, vsharpe, xlab = "Weights", ylab= "Ann. Sharpe ratio")
abline(v = grid[vsharpe == max(vsharpe)], lty = 3)

Interpretation

  • The portfolio that is 11% invested in equities, 89% in bonds has the largest possible Sharpe ratio.

3.3 Driver 3: The correlation between the asset returns

The more correlated assets are in the portfolio, the greater the variability of the portfolio returns.

3.4 Interpreting correlation

Compute the correlation between equity returns and bond returns. You need to distinguish between a static analysis that calculates correlations over a complete sample, and a dynamic analysis that calculates correlations over a rolling sample. It means the correlation evolves over time (see the chart on the bottom right).

# Create a scatter plot
chart.Scatter(returns_bonds, returns_equities)

# Find the correlation
cor(returns_equities, returns_bonds)
##               bonds
## equities 0.06240395

# Merge returns_equities and returns_bonds 
returns <- merge(returns_equities, returns_bonds)

# Find and visualize the correlation using chart.Correlation
chart.Correlation(returns)


# Visualize the rolling estimates using chart.RollingCorrelation
chart.RollingCorrelation(returns_bonds, returns_equities, width = 24)

3.5 Making a risk-reward scatter diagram

Visualize relative attractiveness of the investments by making a scatterplot of the average returns against the portfolio volatilities.

# Import data
returns <- read.csv("data/Portfolio Theory/returns.csv")
returns$Date <- as.Date(returns$Date, format("%m/%d/%Y"))
returns <- returns[order(returns$Date), ]
returns <- as.xts(returns[, 2:5], order.by = returns$Date)
head(returns)
##              equities        bonds  realestate commodities
## 2006-08-30 0.02182242  0.015909610  0.03484325 -0.07133003
## 2006-09-29 0.02700156  0.010115275  0.01965907 -0.10736263
## 2006-10-30 0.03151669  0.006857468  0.06027768 -0.02543380
## 2006-11-29 0.01988532  0.010619145  0.04781250  0.04999998
## 2006-12-30 0.01337125 -0.006004894 -0.01842563 -0.06922181
## 2007-01-30 0.01504026 -0.000702100  0.08779223 -0.02545548

# Create a vector of returns 
means <- apply(returns, 2, "mean")
  
# Create a vector of standard deviation
sds <- apply(returns, 2, "sd")

# Create a scatter plot
plot(sds, means)
text(sds, means, labels = colnames(returns), cex = 0.7)
abline(h = 0, lty = 3)

Interpretation

  • With an exception of commodities, greater volatilities of returns are associated with greater average returns.
  • Commodities appears to be a bad investment. A high risk is met with a negative average return.

3.6 The covariance matrix

Compute and analyze the covariance, and correlation matrix on the monthly returns of the four asset classes.

# Create a matrix with variances on the diagonal
diag_cov <- diag(sds^2)
diag_cov
##             [,1]         [,2]        [,3]        [,4]
## [1,] 0.001920218 0.0000000000 0.000000000 0.000000000
## [2,] 0.000000000 0.0001222713 0.000000000 0.000000000
## [3,] 0.000000000 0.0000000000 0.005447762 0.000000000
## [4,] 0.000000000 0.0000000000 0.000000000 0.004797972

# Create a covariance matrix of returns
cov_matrix <- cov(returns)
cov_matrix
##                 equities         bonds   realestate   commodities
## equities    1.920218e-03  3.009761e-05 0.0024063778  1.510994e-03
## bonds       3.009761e-05  1.222713e-04 0.0002632232 -6.860821e-05
## realestate  2.406378e-03  2.632232e-04 0.0054477625  1.329142e-03
## commodities 1.510994e-03 -6.860821e-05 0.0013291423  4.797972e-03

# Create a correlation matrix of returns
cor_matrix <- cor(returns)
cor_matrix
##               equities       bonds realestate commodities
## equities    1.00000000  0.06211473  0.7440112  0.49780426
## bonds       0.06211473  1.00000000  0.3225172 -0.08957463
## realestate  0.74401117  0.32251718  1.0000000  0.25997618
## commodities 0.49780426 -0.08957463  0.2599762  1.00000000

# Verify covariances equal the product of standard deviations and correlation
all.equal(cov_matrix[1,2], cor_matrix[1,2] * sds[1] * sds[2]) #This doesn't render TRUE due to a rounding error.
## [1] "names for current but not for target"
cor_matrix[1,2] * sds[1] * sds[2]
##     equities 
## 3.009761e-05

3.7 Matrix-based calculation of portfolio mean and variance

# Define variables
weights <- c(0.4, 0.4, 0.1, 0.1)
vmeans <- c(0.007069242,  0.004009507,  0.008949548, -0.008296070)
names(vmeans) <- c("equities", "bonds", "realestate", "commodities")
sigma <- matrix(c(0.00192021806,  0.00003009761, 0.0024063778,  0.00151099397,
                  0.00003009761,  0.00012227126, 0.0002632232, -0.00006860821,
                  0.00240637781,  0.00026322319, 0.0054477625,  0.00132914233,
                  0.00151099397, -0.00006860821, 0.0013291423,  0.00479797179),
                ncol = 4, 
                nrow = 4)
colnames(sigma) <- c("equities", "bonds", "realestate", "commodities")
row.names(sigma) <- c("equities", "bonds", "realestate", "commodities")

# Create a weight matrix w
w <- as.matrix(weights)
w
##      [,1]
## [1,]  0.4
## [2,]  0.4
## [3,]  0.1
## [4,]  0.1
# Create a matrix of returns
mu <- as.matrix(vmeans)
mu
##                     [,1]
## equities     0.007069242
## bonds        0.004009507
## realestate   0.008949548
## commodities -0.008296070
# Calculate portfolio mean monthly returns
t(w)%*%mu
##             [,1]
## [1,] 0.004496847

# Calculate portfolio volatility
sqrt(t(w) %*% sigma %*% w)
##            [,1]
## [1,] 0.02818561

3.8 Who did it?

Construct a risk budget, which shows how large each asset’s percent risk contribution is in the total portfolio volatility. The purpose of the risk budget is to avoid the portfolio risk being concentrated on a few assets.

# Create portfolio weights
weights <- c(0.4, 0.4, 0.1, 0.1)

# Create volatility budget
vol_budget <- StdDev(returns, portfolio_method = "component", weights = weights)
vol_budget
## $StdDev
##            [,1]
## [1,] 0.02818561
## 
## $contribution
##    equities       bonds  realestate commodities 
## 0.016630662 0.001141136 0.006192986 0.004220826 
## 
## $pct_contrib_StdDev
##    equities       bonds  realestate commodities 
##  0.59004087  0.04048648  0.21972154  0.14975111

# Make a table of weights and risk contribution
weights_percrisk <- cbind(weights, vol_budget$pct_contrib_StdDev)
colnames(weights_percrisk) <- c("weights", "perc vol contrib")

# Print the table
weights_percrisk
##             weights perc vol contrib
## equities        0.4       0.59004087
## bonds           0.4       0.04048648
## realestate      0.1       0.21972154
## commodities     0.1       0.14975111

Interpretation

  • The percentage volatility risk caused by the bonds is much less than their portfolio weight.

Chapter 4 Optimizing the portfolio

We have up to now considered the portfolio weights as given. In this chapter you learn how to determine the portfolio weights that are optimal in terms of achieving a target return with minimum variance, while satisfying constraints on the portfolio weights.

# Import data
prices <- readRDS("~/resources/rstudio/finModeling_datacamp/data/Portfolio Theory/prices.rds")
str(prices)
## An 'xts' object on 1990-12-31/2015-12-31 containing:
##   Data: num [1:301, 1:30] 4.53 5.12 5.1 5.21 5.4 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:30] "AA" "AAPL" "AXP" "BA" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
##  NULL
head(prices)
##                  AA     AAPL      AXP       BA      BAC      CAT      CVX
## 1990-12-31 4.534478 1.345204 3.445906 13.77601 2.740819 3.259666 7.440307
## 1991-01-31 5.115716 1.736251 3.800936 14.99043 3.369860 3.473104 7.312247
## 1991-02-28 5.095843 1.794772 4.009782 14.71930 3.504654 3.813431 7.795698
## 1991-03-31 5.205114 2.131782 4.824266 14.33797 4.199830 3.342211 7.976993
## 1991-04-30 5.399917 1.724235 4.210390 13.95664 4.472743 3.346168 8.102942
## 1991-05-31 5.689909 1.477213 4.315652 15.02803 5.109541 3.635996 7.736412
##                   DD      DIS       GE       HD      HPQ     INTC      IBM
## 1990-12-31  7.680170 6.428398 2.355960 2.042869 1.004518 0.841387 18.27009
## 1991-01-31  7.575678 6.866336 2.627997 2.287483 1.225118 0.999835 20.49322
## 1991-02-28  7.953312 7.825717 2.812929 2.578379 1.469351 1.043544 21.01702
## 1991-03-31  7.847620 7.548209 2.880146 2.857695 1.579714 1.021690 18.58884
## 1991-04-30  8.798847 7.313112 2.926684 3.042916 1.615256 1.076322 16.81362
## 1991-05-31 10.086981 7.368695 3.217015 3.486123 1.713990 1.218378 17.52841
##                 JNJ      JPM       KO      MCD      MMM      MRK     MSFT
## 1990-12-31 5.103177 1.651136 3.398343 4.583788 10.70052 6.468881 0.723928
## 1991-01-31 5.441018 2.015923 3.562779 4.485423 10.59133 6.585841 0.943993
## 1991-02-28 5.866060 2.534304 3.827706 4.977246 11.13890 7.350589 0.998104
## 1991-03-31 6.848207 2.706823 3.983028 5.483410 11.13890 7.635325 1.020955
## 1991-04-30 6.749990 3.193658 3.872898 5.286165 11.21757 7.798163 0.952409
## 1991-05-31 6.501354 3.329975 4.203288 5.537626 12.09569 8.603312 1.055830
##                 NKE      PFE       PG      TRV      UTX       VZ      WMT
## 1990-12-31 1.024851 1.674406 5.991295 7.683583 3.471237 7.835626 5.455634
## 1991-01-31 1.215822 1.863621 5.515581 7.806031 3.444045 7.167901 5.951600
## 1991-02-28 1.222185 2.191377 5.654775 8.112150 3.641321 7.241796 6.379935
## 1991-03-31 1.174849 2.233116 5.924462 8.607386 3.549830 7.592801 6.996564
## 1991-04-30 1.209873 2.274859 5.871020 8.715557 3.366849 7.480882 7.312538
## 1991-05-31 1.012474 2.471658 5.958511 8.004714 3.466449 7.050730 7.741360
##                 XOM        T
## 1990-12-31 6.233811 4.643250
## 1991-01-31 6.218753 4.387233
## 1991-02-28 6.728324 4.513183
## 1991-03-31 7.140262 4.712602
## 1991-04-30 7.262318 4.539377
## 1991-05-31 7.191589 4.401176

returns <- Return.calculate(prices)
returns <- returns[-1, ]
head(returns)
##                      AA        AAPL         AXP          BA         BAC
## 1991-01-31  0.128181899  0.29069717  0.10302951  0.08815424  0.22950841
## 1991-02-28 -0.003884696  0.03370538  0.05494594 -0.01808654  0.03999988
## 1991-03-31  0.021443165  0.18777315  0.20312426 -0.02590673  0.19835795
## 1991-04-30  0.037425309 -0.19117668 -0.12724754 -0.02659574  0.06498192
## 1991-05-31  0.053703048 -0.14326469  0.02500053  0.07676566  0.14237304
## 1991-06-30 -0.050966017 -0.11702172 -0.11308280 -0.06632650 -0.14381507
##                     CAT         CVX          DD          DIS          GE
## 1991-01-31  0.065478488 -0.01721166 -0.01360543  0.068125527  0.11546758
## 1991-02-28  0.097989291  0.06611524  0.04984821  0.139722408  0.07036994
## 1991-03-31 -0.123568513  0.02325578 -0.01328905 -0.035461032  0.02389573
## 1991-04-30  0.001183947  0.01578903  0.12121216 -0.031146064  0.01615821
## 1991-05-31  0.086614898 -0.04523419  0.14639805  0.007600458  0.09920135
## 1991-06-30 -0.045892790 -0.04737739 -0.02910058 -0.038793029 -0.04207068
##                    HD         HPQ        INTC         IBM         JNJ
## 1991-01-31 0.11974042  0.21960781  0.18831762  0.12168142  0.06620209
## 1991-02-28 0.12716860  0.19935467  0.04371621  0.02555987  0.07811810
## 1991-03-31 0.10833008  0.07511003 -0.02094210 -0.11553398  0.16742873
## 1991-04-30 0.06481482  0.02249901  0.05347219 -0.09549945 -0.01434200
## 1991-05-31 0.14565207  0.06112591  0.13198281  0.04251293 -0.03683502
## 1991-06-30 0.02751452 -0.06230900 -0.16592306 -0.08480569 -0.07862039
##                   JPM          KO         MCD          MMM         MRK
## 1991-01-31 0.22093092  0.04838711 -0.02145933 -0.010204084  0.01808041
## 1991-02-28 0.25714325  0.07435965  0.10964919  0.051699829  0.11612002
## 1991-03-31 0.06807352  0.04057835  0.10169560  0.000000000  0.03873649
## 1991-04-30 0.17985476 -0.02764982 -0.03597123  0.007062097  0.02132692
## 1991-05-31 0.04268366  0.08530821  0.04756965  0.078281160  0.10324855
## 1991-06-30 0.01041599 -0.04385305 -0.06071428 -0.017060376 -0.01850996
##                   MSFT          NKE         PFE           PG         TRV
## 1991-01-31  0.30398741  0.186340258  0.11300425 -0.079400864  0.01593632
## 1991-02-28  0.05732140  0.005233496  0.17587052  0.025236507  0.03921570
## 1991-03-31  0.02289441 -0.038730634  0.01904693  0.047691906  0.06104867
## 1991-04-30 -0.06713910  0.029811491  0.01869271 -0.009020566  0.01256723
## 1991-05-31  0.10858885 -0.163156794  0.08651042  0.014902181 -0.08156025
## 1991-06-30 -0.06890977 -0.090778627 -0.05732508 -0.091041705 -0.02082535
##                     UTX           VZ          WMT          XOM           T
## 1991-01-31 -0.007833519 -0.085216548  0.090908958 -0.002415537 -0.05513746
## 1991-02-28  0.057280320  0.010309155  0.071969722  0.081941026  0.02870830
## 1991-03-31 -0.025125772  0.048469330  0.096651298  0.061224459  0.04418589
## 1991-04-30 -0.051546412 -0.014740147  0.045161311  0.017094051 -0.03675783
## 1991-05-31  0.029582556 -0.057500172  0.058642020 -0.009739177 -0.03044493
## 1991-06-30 -0.050667412  0.002652633 -0.001920334 -0.002145979  0.03381642

4.1 Exploring monthly returns of the 30 DJIA stocks

The 1991-2015 monthly returns on the 30 DJIA stocks are available as the variable returns.

# Verify the class of returns 
class(returns)
## [1] "xts" "zoo"

# Investigate the dimensions of returns
dim(returns)
## [1] 300  30

# Create a vector of row means
ew_preturns <- rowMeans(returns)

# Cast the numeric vector back to an xts object
ew_preturns <- xts(ew_preturns, order.by = time(returns))

# Plot ew_preturns
plot.zoo(ew_preturns)

4.2 Finding the mean-variance efficient portfolio

A mean-variance efficient portfolio is a portfolio with weights that has the lowest portfolio variance given a target return. It can be obtained by using the portfolio.optim() function in the tseries package.

In the portfolio.optim() function, the target return equals the return on the equally-weighted portfolio by default. There are four outputs of the portfolio.optim() function`:

  1. $pw: weights of each asset within the portfolio,
  2. $px: portfolio return per each period,
  3. $pm: expected portfolio return (= target return) for the whole period,
  4. $ps: the standard deviation of the portfolio returns.
# Load tseries
library(tseries)

# Create an optimized portfolio of returns
opt <- portfolio.optim(returns) #Understand the resulted components such pw, px, pm...
head(opt)
## $pw
##  [1] -1.807044e-18  4.709721e-02  6.139318e-17  4.557858e-19 -2.323180e-17
##  [6] -2.484138e-17  5.889207e-18 -3.524158e-19 -3.030093e-19 -2.257005e-17
## [11]  9.369407e-02 -3.037605e-18  2.382872e-02 -4.767929e-19  8.182345e-02
## [16]  1.676486e-18  1.577342e-02  4.480823e-02  5.802650e-02  1.784812e-04
## [21]  3.680218e-02  5.823022e-02  1.774000e-02  1.676271e-01  3.426857e-02
## [26] -8.662761e-17 -8.673617e-19  4.077031e-02  2.424344e-01  3.689716e-02
## 
## $px
##   [1]  4.639251e-02  6.500998e-02  6.682324e-02 -1.320396e-03  9.708116e-03
##   [6] -4.189021e-02  5.425177e-02  4.526767e-02  7.187954e-03  2.565351e-02
##  [11] -2.405932e-03  1.259594e-01  7.200689e-03 -3.826041e-03 -3.251357e-02
##  [16]  2.522418e-02  2.414128e-02 -2.648454e-02  5.958498e-02 -2.260212e-03
##  [21]  2.564437e-02  2.333253e-02  2.936227e-02  1.915152e-02 -2.487091e-02
##  [26]  1.304910e-02  1.599273e-02 -3.290022e-02  3.461621e-02 -3.474712e-02
##  [31] -3.833523e-02  2.330029e-02 -3.376723e-02  6.059055e-02  1.398268e-02
##  [36] -9.857280e-03  3.146318e-02 -1.964263e-03 -4.051622e-02  2.459762e-02
##  [41]  2.069262e-02 -4.111349e-02  4.521898e-02  5.458274e-02 -3.074730e-02
##  [46]  7.358151e-02 -1.685293e-02  1.585259e-02  3.633164e-02  1.868475e-02
##  [51]  2.692895e-02  3.973556e-02  3.577725e-02  1.699745e-02  1.773766e-02
##  [56] -2.322110e-02  5.391121e-02  3.005455e-02  5.785714e-02  1.557826e-02
##  [61]  4.500256e-03 -9.948877e-03  3.100479e-02  2.221223e-02  4.505838e-02
##  [66]  1.048357e-02 -2.742366e-02  2.566517e-02  5.056606e-02  2.140968e-02
##  [71]  6.033336e-02 -9.082047e-03  6.390057e-02  1.986063e-02 -7.623018e-03
##  [76]  7.152301e-02  4.744454e-02  3.811985e-02  6.181912e-02 -6.156606e-02
##  [81]  4.336263e-02 -2.911187e-02  5.742710e-02 -1.933604e-02  3.008574e-02
##  [86]  9.391169e-02  4.696600e-02  2.424448e-02  1.500093e-03  4.853482e-02
##  [91] -2.117331e-02 -9.192614e-02  5.935856e-02  9.372368e-02  3.530264e-02
##  [96]  5.336596e-02  1.583246e-02 -1.640191e-02  5.396233e-02  6.605411e-02
## [101] -2.867743e-02  2.134232e-02  2.118163e-04  3.588549e-02 -7.343014e-03
## [106]  6.603404e-02  3.213886e-02  4.702109e-02 -5.416481e-02 -1.030544e-01
## [111]  4.091270e-02  4.083367e-03  1.616834e-02 -1.119224e-02  1.317413e-02
## [116]  1.285418e-02  1.617540e-02 -1.479781e-03  4.560629e-04  3.844862e-02
## [121]  3.300465e-03 -5.529615e-02 -3.466023e-02  6.702936e-02  1.560824e-02
## [126] -6.564488e-03  2.688917e-02 -2.745520e-02 -4.057473e-02  2.738891e-02
## [131]  5.609264e-02  3.251684e-02  1.020602e-02  2.075635e-02  3.634337e-02
## [136] -3.729862e-02 -1.759139e-02 -4.203022e-02 -7.568867e-02 -1.150227e-02
## [141] -8.449386e-02  8.455594e-02  7.411059e-03 -3.990422e-02 -3.315274e-02
## [146]  1.633379e-03  4.631186e-02  4.318447e-02  5.237658e-02  4.349631e-03
## [151]  3.576122e-03  2.624961e-02  4.209175e-03  5.772672e-02 -8.376309e-03
## [156]  5.345344e-02  7.325573e-03  2.572558e-02  6.368277e-03  3.161261e-03
## [161]  1.432145e-02  2.208721e-02 -1.205716e-02  2.480003e-02  1.464793e-02
## [166]  2.531046e-02  4.701826e-02  2.129025e-02 -2.047920e-03  6.679571e-02
## [171] -2.919532e-02 -3.023827e-02  2.876019e-02 -1.362141e-02  4.723250e-02
## [176] -4.852840e-03  3.038803e-02 -2.612011e-02  3.979042e-02 -1.364080e-02
## [181]  3.060377e-02 -6.387919e-03 -2.777902e-03  1.270920e-02 -3.253705e-02
## [186]  2.341058e-03  3.456255e-02  2.988984e-02  3.459864e-02  4.587786e-02
## [191]  3.217161e-02  8.023613e-03 -1.570929e-03 -2.082570e-02  1.318344e-02
## [196]  4.742584e-02  3.707520e-02 -3.865560e-03 -7.050825e-03  2.553797e-02
## [201]  4.142059e-02  2.669382e-02 -8.167040e-03  1.405466e-02 -6.599863e-02
## [206] -2.474197e-02  3.991867e-02  4.088073e-02 -1.024549e-02 -6.837466e-02
## [211] -1.039803e-03  4.129172e-02 -3.275231e-02 -7.540644e-02  5.891457e-03
## [216] -1.397410e-02 -7.604549e-02 -8.310085e-02  6.023440e-02  4.434220e-02
## [221]  3.173039e-02  9.318819e-03  6.406131e-02  1.193325e-02  2.760266e-02
## [226]  6.259990e-03  6.574976e-02 -9.173909e-03 -2.901832e-02  3.011457e-02
## [231]  3.848367e-02  2.038189e-02 -6.382406e-02 -4.624385e-02  4.482462e-02
## [236] -1.923137e-02  6.868041e-02  4.231801e-02  6.124515e-03  4.820159e-02
## [241]  2.508268e-02  3.267314e-02 -1.744197e-02  5.038845e-02 -3.957519e-03
## [246] -1.375162e-02 -1.325140e-02 -2.023907e-02 -2.454447e-02  6.977605e-02
## [251]  2.215692e-02  3.875754e-02  1.488103e-02  4.431192e-02  2.024946e-02
## [256] -1.914395e-03 -3.912155e-02  3.427109e-02  2.615429e-02  2.517111e-02
## [261]  2.599044e-02 -1.201852e-02  1.088323e-03 -1.295391e-02  5.510016e-02
## [266]  1.454729e-02  2.879804e-02  2.311171e-02  9.469614e-03 -3.043526e-03
## [271]  3.757782e-02 -3.625911e-02  1.041879e-02  5.250611e-02  4.247613e-02
## [276]  1.757139e-02 -6.441260e-02  4.089929e-02  1.918599e-02  3.293365e-02
## [281]  4.774561e-03  5.444187e-03 -1.163294e-02  4.738929e-02 -2.276475e-03
## [286]  3.467860e-02  2.177481e-02 -5.587068e-05 -3.822578e-02  4.195808e-02
## [291] -2.639213e-02 -1.247297e-03  3.645757e-03 -1.640300e-02  3.868437e-03
## [296] -5.043854e-02  7.325674e-03  8.653364e-02  9.520121e-03 -6.265845e-03
## 
## $pm
## [1] 0.01249652
## 
## $ps
## [1] 0.0355865

# Create pf_weights
pf_weights <- opt$pw

# Assign asset names
names(pf_weights) <- colnames(returns)
head(pf_weights)
##            AA          AAPL           AXP            BA           BAC 
## -1.807044e-18  4.709721e-02  6.139318e-17  4.557858e-19 -2.323180e-17 
##           CAT 
## -2.484138e-17

# Select optimum weights opt_weights
opt_weights <- pf_weights[pf_weights >= 0.01] #How is 0.01 determined?

# Barplot of opt_weights
barplot(opt_weights)


# Print expected portfolio return and volatility
opt$pm 
## [1] 0.01249652
opt$ps
## [1] 0.0355865

Interpreation

  • The portfolio has an average return of 1.2% with a standard deviation of 3.5%.

4.3 Effect of the return target

Show the effect of increasing your target return on the volatility of your mean-variance efficient portfolio. The higher the target return, the greater the portfolio volatility is.

# Create portfolio with target return of average returns 
pf_mean <- portfolio.optim(returns, pm = mean(returns))

# Create portfolio with target return 10% greater than average returns
pf_10plus <- portfolio.optim(returns, pm = 1.1 * mean(returns))

# Print the standard deviations of both portfolios
pf_mean$ps
## [1] 0.0355865
pf_10plus$ps
## [1] 0.03842654

# Calculate the proportion increase in standard deviation
(pf_10plus$ps - pf_mean$ps) / (pf_mean$ps)
## [1] 0.07980684

Interpretation

  • By increasing your target return, your portfolio’s volatility increased by 8%!

4.4 Imposing weight constraints

Create three portfolios with different maximum weight constraints.

An advantage of a maximum weight constraint is:

  • the subsequent portfolio will be less concentrated in certain assets.

A disadvantage of a maximum weight constraint is:

  • the same target return may no longer be possible or will be obtained at the expense of a higher volatility.

The function portfolio.optim() allows you to set weight constraints within the reshigh argument. reshigh requires a vector of maximum weights for each asset.

# Create vectors of maximum weights
max_weights1 <- rep(1, ncol(returns))
max_weights2 <- rep(0.1, ncol(returns))
max_weights3 <- rep(0.05, ncol(returns))

# Create an optimum portfolio with max weights of 100%
opt1 <- portfolio.optim(returns, reshigh = max_weights1)

# Create an optimum portfolio with max weights of 10%
opt2 <- portfolio.optim(returns, reshigh = max_weights2)

# Create an optimum portfolio with max weights of 5%
opt3 <- portfolio.optim(returns, reshigh = max_weights3)

# Calculate how many assets have a weight that is greater than 1% for each portfolio
sum(opt1$pw > .01)
## [1] 15
sum(opt2$pw > .01)
## [1] 17
sum(opt3$pw > .01)
## [1] 22

# Print portfolio volatilites 
opt1$ps
## [1] 0.0355865
opt2$ps
## [1] 0.03616089
opt3$ps
## [1] 0.03798736

Interpretation

  • By imposing weight constraints you can ensure that your portfolio isn’t completely dominated by just a few assets but at the expense of higher volatility.

4.5 Computing the efficient frontier using a grid of target returns

The efficient frontier first finds a grid of target returns and then a portfolio that has the lowest variance per each target return.

But what is a reasonable grid of target returns?

  • The maximum target return = the maximum of average returns of all stocks (the best performing stock for the period!)
  • The minimum target return should be set = the minimum of average returns of all stocks (the worst performer!)

Ideally, the minimum target return should be the return of the minimum variance portfolio, but we don’t know the minimum variance portfolio yet.

Compute the efficient frontier from a grid of potential target returns. Calculate your grid of potential portfolio means, deviations, and weights, using a for loop.

# Calculate each stocks mean returns
stockmu <- colMeans(returns)
stockmu
##          AA        AAPL         AXP          BA         BAC         CAT 
## 0.007648305 0.023517567 0.013854122 0.010923938 0.012116789 0.014203660 
##         CVX          DD         DIS          GE          HD         HPQ 
## 0.009895578 0.009852449 0.012057451 0.011236210 0.016825887 0.013484390 
##        INTC         IBM         JNJ         JPM          KO         MCD 
## 0.018211538 0.009803969 0.011589552 0.016718347 0.010224562 0.012711573 
##         MMM         MRK        MSFT         NKE         PFE          PG 
## 0.010468285 0.009702221 0.018772935 0.017633009 0.012064263 0.010278440 
##         TRV         UTX          VZ         WMT         XOM           T 
## 0.011470929 0.013342172 0.007916786 0.010143516 0.009518167 0.008708987
# Create a grid of target values
grid <- seq(from = 0.01, to = max(stockmu), length.out = 50)
grid
##  [1] 0.01000000 0.01027587 0.01055174 0.01082761 0.01110347 0.01137934
##  [7] 0.01165521 0.01193108 0.01220695 0.01248282 0.01275869 0.01303456
## [13] 0.01331042 0.01358629 0.01386216 0.01413803 0.01441390 0.01468977
## [19] 0.01496564 0.01524151 0.01551737 0.01579324 0.01606911 0.01634498
## [25] 0.01662085 0.01689672 0.01717259 0.01744846 0.01772432 0.01800019
## [31] 0.01827606 0.01855193 0.01882780 0.01910367 0.01937954 0.01965540
## [37] 0.01993127 0.02020714 0.02048301 0.02075888 0.02103475 0.02131062
## [43] 0.02158649 0.02186235 0.02213822 0.02241409 0.02268996 0.02296583
## [49] 0.02324170 0.02351757
# Create empty vectors to store means and deviations
vpm <- vpsd <- rep(NA, times = 50) # 50 b/c 50 target returns were created in grid 

# Create an empty matrix to store weights
mweights <- matrix(NA, 50, 30) # 50 & 30 b/c a weight is to be found for each of 30 stocks (columns) given each of 50 target return (rows)

# Create your for loop
for(i in 1:length(grid)) {
  opt <- portfolio.optim(x = returns, pm = grid[i])
  vpm[i] <- opt$pm  # Insert print(vpm) to see how the for loop creates vpm
  vpsd[i] <- opt$ps
  mweights[i, ] <- opt$pw
}

vpm
##  [1] 0.01000000 0.01027587 0.01055174 0.01082761 0.01110347 0.01137934
##  [7] 0.01165521 0.01193108 0.01220695 0.01248282 0.01275869 0.01303456
## [13] 0.01331042 0.01358629 0.01386216 0.01413803 0.01441390 0.01468977
## [19] 0.01496564 0.01524151 0.01551737 0.01579324 0.01606911 0.01634498
## [25] 0.01662085 0.01689672 0.01717259 0.01744846 0.01772432 0.01800019
## [31] 0.01827606 0.01855193 0.01882780 0.01910367 0.01937954 0.01965540
## [37] 0.01993127 0.02020714 0.02048301 0.02075888 0.02103475 0.02131062
## [43] 0.02158649 0.02186235 0.02213822 0.02241409 0.02268996 0.02296583
## [49] 0.02324170 0.02351757
vpsd
##  [1] 0.03378219 0.03371503 0.03372651 0.03380522 0.03394887 0.03415266
##  [7] 0.03441543 0.03473617 0.03511685 0.03556262 0.03607463 0.03665055
## [13] 0.03728737 0.03798818 0.03875881 0.03959858 0.04050740 0.04148170
## [19] 0.04251914 0.04361773 0.04477296 0.04598058 0.04723726 0.04854015
## [25] 0.04988562 0.05127199 0.05271764 0.05422470 0.05578819 0.05740350
## [31] 0.05906639 0.06077294 0.06252374 0.06442350 0.06666514 0.06923637
## [37] 0.07211348 0.07527702 0.07871152 0.08238318 0.08626744 0.09042921
## [43] 0.09486345 0.09953375 0.10440843 0.10946020 0.11466564 0.12000478
## [49] 0.12546052 0.13102620
mweights
##                [,1]        [,2]          [,3]          [,4]          [,5]
##  [1,]  1.400412e-17 0.001716116  2.521941e-17 -1.708075e-18 -8.382685e-18
##  [2,]  1.133771e-17 0.010219324  3.028915e-17 -4.807806e-19 -8.471211e-18
##  [3,]  9.135390e-18 0.015839592  3.436504e-17  1.057426e-18 -9.082449e-18
##  [4,]  7.059837e-18 0.021191352  3.839224e-17  2.672249e-18 -1.000516e-17
##  [5,]  5.153410e-18 0.025597639  4.194884e-17  5.737667e-19 -1.185751e-17
##  [6,]  3.240136e-18 0.029837753  4.542030e-17 -1.317006e-18 -1.378380e-17
##  [7,]  1.326861e-18 0.034077868  4.889176e-17 -1.873280e-18 -1.571008e-17
##  [8,] -3.752051e-19 0.038250044  5.249410e-17  4.085313e-19 -1.802442e-17
##  [9,] -1.203106e-18 0.042391146  5.671326e-17  3.598179e-19 -2.060544e-17
## [10,] -1.776054e-18 0.046862670  6.117148e-17  4.460074e-19 -2.311012e-17
## [11,] -2.480433e-18 0.051622156  6.560868e-17  2.257983e-18 -2.553356e-17
## [12,]  2.547970e-17 0.056392907 -1.837589e-17 -1.411302e-18 -2.457629e-17
## [13,]  2.908668e-17 0.061134574 -1.889657e-17 -1.421141e-18 -4.018101e-17
## [14,]  3.426268e-17 0.066731455 -2.043502e-17 -1.540022e-18 -2.703125e-17
## [15,]  3.955817e-17 0.072422017 -2.205841e-17 -1.642272e-18 -2.768538e-17
## [16,]  4.540454e-17 0.079034586 -2.452444e-17 -2.686016e-18 -1.389666e-17
## [17,]  5.142309e-17 0.085857686 -2.700206e-17 -3.554445e-18 -2.783509e-17
## [18,]  5.750241e-17 0.092750515 -2.946892e-17 -4.333334e-18 -2.789444e-17
## [19,]  6.462345e-17 0.099873304 -3.307732e-17 -5.347273e-18 -2.817035e-17
## [20,]  7.180306e-17 0.107009021 -3.674989e-17 -6.374426e-18 -2.845844e-17
## [21,]  7.898266e-17 0.114144737 -4.042245e-17 -7.401579e-18 -2.874652e-17
## [22,]  8.616227e-17 0.121280454 -4.409502e-17 -8.428732e-18 -2.903461e-17
## [23,]  9.380294e-17 0.128687178 -4.751781e-17 -9.791709e-18 -2.935709e-17
## [24,]  1.014863e-16 0.136118995 -5.091747e-17 -1.118578e-17 -4.356053e-17
## [25,]  1.091697e-16 0.143550812 -5.431713e-17 -1.257985e-17 -3.000840e-17
## [26,]  1.179485e-16 0.151394564 -5.824387e-17 -3.263764e-18 -3.038733e-17
## [27,]  1.286407e-16 0.160530655 -6.299286e-17 -1.549791e-18 -3.082334e-17
## [28,]  1.393329e-16 0.169666747 -6.774185e-17  1.641823e-19 -1.738155e-17
## [29,]  1.500250e-16 0.178802839 -7.249085e-17  1.878155e-18 -3.169535e-17
## [30,]  1.607172e-16 0.187938930 -7.723984e-17  3.592128e-18 -3.213136e-17
## [31,]  1.714094e-16 0.197075022 -8.198884e-17 -7.784856e-18 -3.256736e-17
## [32,]  1.821017e-16 0.206213231 -8.674057e-17 -1.905528e-17 -3.299946e-17
## [33,]  1.926002e-16 0.215779832 -9.204382e-17 -1.513739e-17 -3.313949e-17
## [34,]  2.134065e-16 0.250064361 -1.002645e-16 -1.606955e-17 -3.484492e-17
## [35,]  2.369712e-16 0.290747966 -1.092813e-16  7.683637e-18 -3.709604e-17
## [36,]  2.606666e-16 0.330124405 -1.193852e-16  2.048943e-17 -4.332540e-17
## [37,]  2.843621e-16 0.369500843 -1.294890e-16  5.539643e-18 -4.955476e-17
## [38,]  3.426167e-16 0.411142054 -1.421234e-16  6.979366e-18 -5.430432e-17
## [39,]  3.180698e-16 0.452938106 -1.549308e-16  8.590814e-18 -5.895270e-17
## [40,]  4.045452e-16 0.494734158 -1.677383e-16 -5.256249e-17 -6.360109e-17
## [41,]  4.084703e-16 0.539163199 -1.821410e-16 -6.142364e-17 -6.797957e-17
## [42,]  4.696674e-16 0.590062100 -2.004638e-16 -7.304438e-17 -7.169484e-17
## [43,]  5.031088e-16 0.640961002 -2.187865e-16  1.517703e-17 -7.541011e-17
## [44,]  5.087947e-16 0.691859903 -2.371093e-16  1.568705e-17 -7.912537e-17
## [45,]  5.422362e-16 0.742758805 -2.554321e-16  1.833783e-17 -8.284064e-17
## [46,]  5.756776e-16 0.793657706 -2.737548e-16  1.795173e-17 -8.655591e-17
## [47,]  6.091191e-16 0.844556608 -3.552859e-16  1.643052e-16 -9.027118e-17
## [48,]  6.425606e-16 0.895455509 -3.848442e-16  1.786611e-16 -9.398644e-17
## [49,]  6.760020e-16 0.946354411  1.485740e-17  6.526182e-16 -9.770171e-17
## [50,]  7.111711e-16 1.000000000 -5.291809e-17  6.949005e-16 -1.019374e-16
##                [,6]          [,7]          [,8]          [,9]
##  [1,]  1.954673e-17  4.443567e-02 -1.650549e-17  9.026687e-03
##  [2,]  1.278128e-17  3.663081e-02 -1.352899e-17  6.813319e-03
##  [3,]  7.940480e-18  3.029739e-02 -1.179482e-17  2.883543e-03
##  [4,]  3.380887e-18  2.396461e-02 -1.041903e-17  0.000000e+00
##  [5,] -9.146994e-19  1.701083e-02 -8.551910e-18  0.000000e+00
##  [6,] -5.168910e-18  9.977785e-03 -6.611157e-18  0.000000e+00
##  [7,] -9.423121e-18  2.944744e-03 -4.670404e-18  0.000000e+00
##  [8,] -1.391688e-17  0.000000e+00 -3.298890e-18  3.051369e-19
##  [9,] -1.900682e-17  2.142490e-18 -2.051988e-18  1.587621e-19
## [10,] -2.455536e-17  5.695048e-18 -4.415442e-19 -2.813188e-19
## [11,] -3.033009e-17  1.322959e-17  1.294097e-18 -1.116707e-17
## [12,] -4.527464e-18  5.000462e-17 -4.042570e-17  4.752932e-18
## [13,] -2.317404e-18  5.033130e-17 -2.496679e-17  1.816418e-18
## [14,] -5.585977e-19  5.794674e-17 -3.848784e-17 -1.127616e-18
## [15,]  1.142891e-18  5.181316e-17 -3.821614e-17 -1.104406e-17
## [16,]  2.763011e-18  4.493530e-17 -3.900786e-17 -7.637030e-18
## [17,]  4.394706e-18  5.185732e-17 -3.994783e-17 -1.146305e-17
## [18,]  6.032666e-18  5.182206e-17 -4.092927e-17 -1.540001e-17
## [19,]  6.550462e-18  5.479065e-17 -4.144532e-17 -2.375210e-17
## [20,]  7.005288e-18  5.792809e-17 -4.193521e-17 -1.808452e-17
## [21,]  7.460115e-18  6.106554e-17 -4.242510e-17 -1.935583e-17
## [22,]  7.914941e-18  6.420298e-17 -4.291500e-17 -2.062715e-17
## [23,]  1.593382e-17  6.610235e-17 -4.308125e-17 -2.157234e-17
## [24,]  1.013279e-17  6.788708e-17 -4.321753e-17 -2.248733e-17
## [25,]  0.000000e+00  6.967181e-17 -4.335382e-17 -2.340232e-17
## [26,]  8.184383e-19  7.110506e-17 -4.377399e-17 -2.233857e-17
## [27,] -6.881452e-19  7.431809e-17 -4.484165e-17 -1.833260e-17
## [28,] -1.347558e-18  7.753111e-17 -4.590932e-17 -1.432664e-17
## [29,]  0.000000e+00  8.074414e-17 -4.697698e-17 -1.725957e-17
## [30,]  0.000000e+00  8.395716e-17 -4.804464e-17 -6.314707e-18
## [31,]  0.000000e+00  8.717019e-17 -4.911231e-17 -2.308742e-18
## [32,]  3.468527e-18  9.037408e-17 -5.017662e-17  1.008227e-16
## [33,] -1.267066e-19  9.256976e-17 -5.093358e-17  8.301679e-17
## [34,]  2.981828e-18  7.990969e-17 -3.110167e-17  1.072935e-16
## [35,] -2.255915e-18  6.364059e-17 -5.907140e-18 -1.093758e-16
## [36,] -4.292387e-19  5.480931e-17  1.756886e-17 -1.283707e-16
## [37,] -8.384149e-19  4.597804e-17  4.104486e-17 -1.473656e-16
## [38,] -1.246326e-18  2.780358e-17  6.152463e-17 -1.978789e-16
## [39,] -1.654151e-18  8.990332e-18  8.179954e-17 -1.931383e-16
## [40,]  5.621713e-18 -9.822915e-18  1.020745e-16 -9.863063e-17
## [41,]  7.899972e-18 -2.860911e-17  1.214578e-16 -1.490138e-16
## [42,]  3.146545e-18 -4.732883e-17  1.386506e-16 -1.786407e-16
## [43,] -1.081097e-18 -6.604854e-17  1.558433e-16 -1.805120e-16
## [44,] -9.630536e-19 -8.476826e-17  1.730360e-16 -2.101388e-16
## [45,] -3.870492e-17 -2.791996e-16  1.902287e-16  3.600784e-16
## [46,]  1.978956e-17 -3.139521e-16  2.074214e-16  4.231826e-16
## [47,]  5.621808e-18 -3.592916e-17  2.246142e-16 -3.292084e-16
## [48,]  4.715019e-18 -5.177671e-17  2.418069e-16 -3.754701e-16
## [49,]  2.889142e-18 -9.061339e-16  2.589996e-16  1.506858e-16
## [50,] -2.559344e-17 -9.465284e-16  2.811302e-16  1.926695e-16
##               [,10]         [,11]         [,12]         [,13]
##  [1,] -1.984960e-17  3.380838e-03 -4.579099e-18  0.000000e+00
##  [2,] -1.969851e-17  1.825287e-02 -4.163402e-18  9.096932e-04
##  [3,] -1.958247e-17  2.964433e-02 -4.036841e-18  6.161078e-03
##  [4,] -1.971226e-17  4.036087e-02 -4.397977e-18  1.103390e-02
##  [5,] -1.977527e-17  4.892995e-02 -6.835917e-18  1.364040e-02
##  [6,] -1.973727e-17  5.713446e-02 -9.553914e-18  1.591457e-02
##  [7,] -1.969928e-17  6.533898e-02 -1.227191e-17  1.818874e-02
##  [8,] -1.994341e-17  7.367836e-02 -1.486385e-18  2.064798e-02
##  [9,] -2.104190e-17  8.291170e-02 -2.284500e-18  2.244442e-02
## [10,] -2.249532e-17  9.315958e-02 -3.006369e-18  2.376762e-02
## [11,] -2.407408e-17  1.040428e-01 -9.579808e-19  2.494275e-02
## [12,]  3.304783e-17  1.149629e-01 -1.191678e-17  2.610127e-02
## [13,]  3.708890e-17  1.256801e-01 -2.426111e-18  2.710840e-02
## [14,]  4.231765e-17  1.327230e-01 -5.807584e-18  2.790097e-02
## [15,]  4.763659e-17  1.394328e-01 -9.056882e-18  2.870365e-02
## [16,]  5.410721e-17  1.461154e-01  1.542614e-18  2.957088e-02
## [17,]  6.088501e-17  1.528587e-01  5.231548e-18  3.051418e-02
## [18,]  6.776812e-17  1.596275e-01  8.932685e-18  3.148763e-02
## [19,]  7.346343e-17  1.653453e-01  1.467782e-17  3.170164e-02
## [20,]  7.909197e-17  1.710039e-01  2.053785e-17  3.187295e-02
## [21,]  8.472051e-17  1.766626e-01  1.945899e-17  3.204427e-02
## [22,]  9.034905e-17  1.823213e-01  3.225792e-17  3.221558e-02
## [23,]  9.639657e-17  1.875612e-01  3.831905e-17  3.231207e-02
## [24,]  1.024829e-16  1.927623e-01  4.439881e-17  3.240162e-02
## [25,]  1.085692e-16  1.979634e-01  5.047856e-17  3.249118e-02
## [26,]  1.131459e-16  2.033300e-01  5.738924e-17  3.232857e-02
## [27,]  1.144480e-16  2.085666e-01  6.564879e-17  3.173780e-02
## [28,]  1.157500e-16  2.138033e-01  7.390835e-17  3.114704e-02
## [29,]  1.170521e-16  2.190400e-01  8.216791e-17  3.055627e-02
## [30,]  1.183542e-16  2.242767e-01  9.042746e-17  2.996550e-02
## [31,]  1.196562e-16  2.295134e-01  9.868702e-17  2.937474e-02
## [32,]  1.209606e-16  2.347483e-01  1.069481e-16  2.878080e-02
## [33,]  1.213430e-16  2.388524e-01  1.153119e-16  2.728531e-02
## [34,]  1.094234e-16  2.196515e-01  1.318180e-16  1.467904e-02
## [35,]  6.671473e-17  1.944353e-01  3.856663e-17  0.000000e+00
## [36,]  7.789152e-17  1.675997e-01  5.571367e-17  0.000000e+00
## [37,]  8.906831e-17  1.407642e-01  7.286072e-17  0.000000e+00
## [38,]  4.610110e-17  1.033580e-01  9.316962e-17  2.747379e-18
## [39,]  3.098294e-17  6.522909e-02  1.136947e-16  5.682595e-18
## [40,] -1.189080e-17  2.710017e-02 -8.954257e-17 -2.748988e-17
## [41,]  1.595819e-18  0.000000e+00 -1.060211e-16 -3.417084e-17
## [42,] -1.058642e-17  0.000000e+00 -1.317666e-16 -1.633805e-17
## [43,] -2.276867e-17  0.000000e+00  5.898503e-17 -4.494369e-17
## [44,] -7.195337e-18  0.000000e+00  7.127213e-17 -5.087481e-17
## [45,] -4.713316e-17  0.000000e+00  2.518003e-16 -2.670128e-17
## [46,] -8.707098e-17  0.000000e+00  2.507873e-16 -5.816129e-17
## [47,] -7.149765e-17  0.000000e+00 -1.653925e-18 -3.018333e-19
## [48,] -8.367989e-17  0.000000e+00 -9.021138e-18  1.505133e-18
## [49,] -1.236177e-16  0.000000e+00 -2.860310e-16 -8.344025e-17
## [50,] -1.089489e-16 -5.103331e-18 -3.059966e-16 -4.497721e-17
##               [,14]         [,15]         [,16]         [,17]
##  [1,]  5.502653e-02  4.529584e-02  1.230116e-18  2.847616e-02
##  [2,]  4.803411e-02  5.142587e-02  1.007345e-18  2.732334e-02
##  [3,]  4.096520e-02  5.601450e-02  8.368182e-19  2.641570e-02
##  [4,]  3.409209e-02  6.041010e-02  6.719562e-19  2.542723e-02
##  [5,]  2.628621e-02  6.430174e-02  6.455785e-19  2.353521e-02
##  [6,]  1.837360e-02  6.803790e-02  6.494372e-19  2.154295e-02
##  [7,]  1.046098e-02  7.177406e-02  6.532959e-19  1.955069e-02
##  [8,]  2.472801e-03  7.541750e-02  4.969883e-19  1.765447e-02
##  [9,]  0.000000e+00  7.860685e-02  8.984524e-19  1.650699e-02
## [10,] -4.381201e-19  8.166959e-02  1.638220e-18  1.580292e-02
## [11,]  8.822636e-20  8.457690e-02 -1.109247e-19  1.512263e-02
## [12,] -2.279759e-17  8.742665e-02  2.168404e-19  1.441627e-02
## [13,] -2.024316e-17  9.056150e-02  7.980984e-04  1.359438e-02
## [14,] -1.622578e-17  9.239682e-02  3.520107e-03  1.011472e-02
## [15,] -1.206200e-17  9.403875e-02  6.269928e-03  6.387544e-03
## [16,] -7.941401e-18  9.512075e-02  9.731758e-03  1.454670e-03
## [17,] -3.771223e-18  9.519278e-02  1.306077e-02 -4.336809e-19
## [18,]  4.201678e-19  9.485917e-02  1.632197e-02  0.000000e+00
## [19,]  5.837641e-18  9.276985e-02  1.947529e-02  7.335582e-19
## [20,]  1.132404e-17  9.058183e-02  2.262254e-02  1.508353e-18
## [21,]  1.681043e-17  8.839381e-02  2.576980e-02  0.000000e+00
## [22,]  2.229683e-17  8.620579e-02  2.891705e-02  0.000000e+00
## [23,]  2.759368e-17  8.343281e-02  3.166257e-02  3.526319e-19
## [24,]  3.287297e-17  8.060568e-02  3.437090e-02  7.379136e-19
## [25,]  3.815227e-17  7.777854e-02  3.707922e-02 -1.490384e-18
## [26,]  4.301623e-17  7.298487e-02  4.008784e-02  1.372244e-17
## [27,]  4.662718e-17  6.080797e-02  4.317808e-02  6.496925e-18
## [28,]  5.023813e-17  4.863108e-02  4.626832e-02 -7.285902e-19
## [29,]  5.384908e-17  3.645419e-02  4.935856e-02 -7.954106e-18
## [30,]  5.746003e-17  2.427730e-02  5.244879e-02 -1.517962e-17
## [31,]  6.107098e-17  1.210041e-02  5.553903e-02  4.602775e-17
## [32,]  6.467949e-17  0.000000e+00  5.863564e-02  3.593805e-17
## [33,]  6.722762e-17 -7.532533e-19  6.221674e-02  6.330295e-17
## [34,]  7.380182e-17 -1.703120e-19  5.220973e-02  8.538252e-17
## [35,]  8.156642e-17  2.235896e-17  3.849536e-02  1.405196e-17
## [36,]  8.911003e-17 -1.286572e-17  1.984446e-02  1.670761e-17
## [37,]  9.665364e-17 -5.670068e-18  1.193548e-03  1.936326e-17
## [38,]  1.045963e-16 -1.302751e-17  0.000000e+00  1.482238e-17
## [39,]  1.125662e-16 -1.349223e-17  0.000000e+00  9.789485e-18
## [40,] -1.274690e-16 -2.262403e-18  0.000000e+00  2.191496e-16
## [41,] -1.510885e-16 -5.180459e-18  2.424194e-18  2.348136e-16
## [42,] -1.817126e-16 -2.876559e-16  3.376431e-18  2.449243e-16
## [43,] -2.123367e-16 -3.012067e-16  5.995344e-18  2.958133e-17
## [44,] -2.429608e-16 -3.402616e-16  8.614257e-18  1.882238e-17
## [45,] -3.472555e-16 -1.679287e-16 -2.471503e-17  4.058258e-16
## [46,] -3.815816e-16 -1.608986e-16 -3.047711e-17  4.477329e-16
## [47,] -7.708408e-17 -2.473619e-16 -6.259430e-17 -5.844500e-16
## [48,] -8.240194e-17 -2.679546e-16 -7.254689e-17 -5.981135e-16
## [49,]  3.567245e-16  1.342249e-18  0.000000e+00 -1.401822e-16
## [50,]  3.881073e-16  4.944721e-17 -1.350489e-17 -1.396921e-16
##               [,18]         [,19]         [,20]         [,21]
##  [1,]  3.488941e-03  8.646236e-02  9.327631e-03  2.316732e-18
##  [2,]  1.128474e-02  8.414317e-02  8.036402e-03  2.244031e-19
##  [3,]  1.665962e-02  8.088425e-02  6.785818e-03 -1.941689e-18
##  [4,]  2.135084e-02  7.719049e-02  5.686809e-03 -4.126307e-18
##  [5,]  2.559191e-02  7.481593e-02  4.527521e-03  5.607229e-03
##  [6,]  2.984157e-02  7.278496e-02  3.387986e-03  1.220944e-02
##  [7,]  3.409122e-02  7.075399e-02  2.248452e-03  1.881165e-02
##  [8,]  3.803795e-02  6.828563e-02  1.378564e-03  2.533294e-02
##  [9,]  4.144171e-02  6.396849e-02  9.301374e-04  3.126463e-02
## [10,]  4.465079e-02  5.833116e-02  2.271204e-04  3.655507e-02
## [11,]  4.779343e-02  5.224555e-02  0.000000e+00  4.153569e-02
## [12,]  5.092785e-02  4.617460e-02  3.433308e-19  4.651790e-02
## [13,]  5.391527e-02  4.006520e-02  0.000000e+00  5.137226e-02
## [14,]  5.618336e-02  3.333522e-02 -1.802477e-18  5.664150e-02
## [15,]  5.840899e-02  2.654988e-02  1.763448e-18  6.198029e-02
## [16,]  5.885615e-02  1.811734e-02  6.480818e-18  6.704988e-02
## [17,]  5.889548e-02  9.348886e-03  3.295176e-19  7.204126e-02
## [18,]  5.879959e-02  4.724274e-04  7.947442e-19  7.700539e-02
## [19,]  5.953313e-02  0.000000e+00 -3.709715e-18  8.303310e-02
## [20,]  6.031330e-02  0.000000e+00 -1.359616e-18  8.912061e-02
## [21,]  6.109346e-02 -2.944440e-18 -2.478963e-18  9.520812e-02
## [22,]  6.187363e-02 -3.601115e-18 -3.598310e-18  1.012956e-01
## [23,]  6.202462e-02 -2.791145e-18  7.631687e-19  1.078099e-01
## [24,]  6.211735e-02 -3.170036e-18  2.876035e-19  1.143636e-01
## [25,]  6.221008e-02 -3.548927e-18  3.191295e-18  1.209174e-01
## [26,]  5.951865e-02 -1.333515e-17  9.650180e-19  1.279206e-01
## [27,]  5.050393e-02 -1.822881e-17 -9.958492e-18  1.354422e-01
## [28,]  4.148920e-02 -2.312246e-17 -2.088200e-17  1.429638e-01
## [29,]  3.247448e-02 -2.801612e-17 -3.180551e-17  1.504853e-01
## [30,]  2.345976e-02 -3.290978e-17 -4.272902e-17  1.580069e-01
## [31,]  1.444503e-02 -3.207635e-17  8.264148e-18  1.655285e-01
## [32,]  5.411565e-03 -3.141016e-17 -1.754204e-17  1.730428e-01
## [33,]  8.673617e-19 -3.147469e-17  7.320772e-18  1.793097e-01
## [34,] -2.217513e-18 -4.993215e-17  3.191771e-17  1.892120e-01
## [35,]  1.269362e-20 -1.292155e-16  3.357274e-17  1.997911e-01
## [36,]  1.647459e-17 -1.650925e-16  4.987758e-17  2.045606e-01
## [37,]  2.166727e-17 -2.009695e-16  6.618242e-17  2.093302e-01
## [38,]  2.795446e-17 -2.359284e-16  8.171108e-17  2.089315e-01
## [39,]  3.431648e-17 -2.708246e-16  9.718668e-17  2.081795e-01
## [40,]  4.218136e-18 -2.438992e-16  6.519942e-17  2.074275e-01
## [41,]  4.345526e-18 -2.702330e-16  8.532906e-17  2.008923e-01
## [42,]  5.730827e-17 -2.922015e-16  1.120232e-16  1.801464e-01
## [43,] -6.611510e-17 -3.141700e-16  1.692852e-16  1.594004e-01
## [44,] -7.504448e-17 -3.361385e-16  1.919376e-16  1.386545e-01
## [45,] -2.326437e-16 -7.228655e-17 -2.222740e-16  1.179086e-01
## [46,] -2.553676e-16 -6.985214e-17 -2.506660e-16  9.716269e-02
## [47,]  2.683414e-17  2.817657e-16  5.230786e-16  7.641677e-02
## [48,]  3.107509e-17  3.009745e-16  5.978208e-16  5.567086e-02
## [49,]  1.188481e-16  5.757954e-16  3.591486e-17  3.492494e-02
## [50,]  1.155558e-33  6.117761e-16  4.364512e-17  3.989864e-17
##               [,22]         [,23]         [,24]         [,25]
##  [1,] -2.211971e-18  7.426928e-03  1.833617e-01  2.905746e-02
##  [2,]  0.000000e+00  1.131175e-02  1.807072e-01  3.162915e-02
##  [3,]  6.136089e-03  1.401272e-02  1.786071e-01  3.252133e-02
##  [4,]  1.410026e-02  1.622246e-02  1.765092e-01  3.278082e-02
##  [5,]  2.112806e-02  1.690917e-02  1.744524e-01  3.336286e-02
##  [6,]  2.799211e-02  1.739960e-02  1.724196e-01  3.405855e-02
##  [7,]  3.485617e-02  1.789002e-02  1.703868e-01  3.475425e-02
##  [8,]  4.176304e-02  1.813113e-02  1.687727e-01  3.500695e-02
##  [9,]  4.949278e-02  1.792594e-02  1.677584e-01  3.485270e-02
## [10,]  5.780819e-02  1.774360e-02  1.676065e-01  3.430909e-02
## [11,]  6.634338e-02  1.736110e-02  1.679697e-01  3.340236e-02
## [12,]  7.489000e-02  1.688501e-02  1.683172e-01  3.246821e-02
## [13,]  8.337454e-02  1.622421e-02  1.685207e-01  3.142435e-02
## [14,]  9.183629e-02  1.584358e-02  1.668610e-01  2.936169e-02
## [15,]  1.003089e-01  1.553075e-02  1.650410e-01  2.721801e-02
## [16,]  1.092591e-01  1.419351e-02  1.633059e-01  2.450069e-02
## [17,]  1.183002e-01  1.285483e-02  1.606001e-01  2.137151e-02
## [18,]  1.273700e-01  1.153441e-02  1.574930e-01  1.808327e-02
## [19,]  1.361746e-01  1.043217e-02  1.527144e-01  1.370262e-02
## [20,]  1.449643e-01  9.342185e-03  1.478418e-01  9.260557e-03
## [21,]  1.537540e-01  8.252205e-03  1.429693e-01  4.818494e-03
## [22,]  1.625437e-01  7.162225e-03  1.380967e-01  3.764317e-04
## [23,]  1.706985e-01  5.721038e-03  1.334720e-01  0.000000e+00
## [24,]  1.787945e-01  4.247334e-03  1.288702e-01  0.000000e+00
## [25,]  1.868906e-01  2.773630e-03  1.242684e-01 -2.680372e-18
## [26,]  1.959186e-01  0.000000e+00  1.165163e-01 -4.076276e-19
## [27,]  2.067175e-01  8.673617e-19  1.025153e-01 -1.595270e-18
## [28,]  2.175163e-01  0.000000e+00  8.851420e-02  2.786356e-18
## [29,]  2.283152e-01  1.256699e-34  7.451313e-02 -6.783422e-18
## [30,]  2.391141e-01  1.460308e-34  6.051206e-02 -8.741971e-18
## [31,]  2.499129e-01 -4.206631e-19  4.651099e-02 -4.699959e-18
## [32,]  2.607083e-01  5.797148e-18  3.245933e-02  1.599794e-18
## [33,]  2.703729e-01  1.854419e-18  6.183106e-03 -9.863733e-18
## [34,]  2.741833e-01 -3.028700e-18  3.469447e-18 -1.320251e-17
## [35,]  2.765304e-01  4.018127e-17 -3.451430e-17  3.236243e-17
## [36,]  2.778708e-01  5.309877e-17 -6.394975e-18  3.142140e-17
## [37,]  2.792112e-01  7.989406e-17 -3.281305e-18  3.048037e-17
## [38,]  2.765684e-01  9.677761e-17  1.548708e-18  3.187914e-17
## [39,]  2.736533e-01  1.134579e-16  6.496066e-18  3.343788e-17
## [40,]  2.707382e-01  1.641312e-17 -8.046214e-17 -3.192186e-17
## [41,]  2.599445e-01  1.915338e-17 -9.472311e-17 -3.353086e-17
## [42,]  2.297915e-01 -6.603481e-17 -3.379137e-17 -2.952558e-17
## [43,]  1.996386e-01 -3.112203e-17  1.342915e-16  2.674286e-16
## [44,]  1.694856e-01 -3.853582e-17  1.535625e-16  3.003367e-16
## [45,]  1.393326e-01 -2.893724e-16  1.906224e-16  1.104266e-16
## [46,]  1.091796e-01 -3.188973e-16  2.089103e-16  1.297409e-16
## [47,]  7.902662e-02 -1.295811e-16  2.613498e-16  1.568623e-16
## [48,]  4.887363e-02 -1.456249e-16  2.791892e-16  1.640345e-16
## [49,]  1.872065e-02  2.963285e-17 -4.086798e-17 -1.474183e-16
## [50,]  0.000000e+00 -6.043526e-16 -2.727196e-16 -1.613949e-16
##               [,26]         [,27]         [,28]         [,29]
##  [1,] -1.837952e-17  6.200395e-02  1.371702e-01  2.537968e-01
##  [2,] -2.446238e-17  5.598830e-02  1.256919e-01  2.524320e-01
##  [3,] -3.077685e-17  5.005605e-02  1.147728e-01  2.527839e-01
##  [4,] -3.736444e-17  4.411353e-02  1.038745e-01  2.536340e-01
##  [5,] -4.502254e-17  3.644602e-02  9.411067e-02  2.551250e-01
##  [6,] -5.285170e-17  2.855594e-02  8.450878e-02  2.567103e-01
##  [7,] -6.068086e-17  2.066586e-02  7.490689e-02  2.582956e-01
##  [8,] -6.885493e-17  1.287092e-02  6.515385e-02  2.566026e-01
##  [9,] -7.745252e-17  4.062345e-03  5.404241e-02  2.506654e-01
## [10,] -8.619114e-17  0.000000e+00  4.143300e-02  2.428618e-01
## [11,] -9.499525e-17  3.218871e-19  2.803556e-02  2.342552e-01
## [12,]  6.950241e-18 -4.943512e-17  1.462155e-02  2.256480e-01
## [13,]  4.509015e-18 -4.690998e-17  1.236939e-03  2.171408e-01
## [14,]  1.548733e-18 -4.249630e-17  1.072194e-18  2.073128e-01
## [15,] -1.407827e-18 -3.795088e-17  1.927927e-18  1.973284e-01
## [16,] -2.405717e-18 -3.450397e-17 -3.716005e-18  1.836894e-01
## [17,] -3.272599e-18 -3.205025e-17 -5.806902e-18  1.691036e-01
## [18,] -4.121594e-18 -2.998535e-17 -1.134091e-17  1.541952e-01
## [19,] -7.541320e-18 -2.473808e-17 -1.323210e-17  1.352447e-01
## [20,] -1.110556e-17 -1.931191e-17 -1.164410e-17  1.160669e-01
## [21,] -1.466980e-17 -1.388574e-17 -1.352555e-17  9.688919e-02
## [22,] -1.823403e-17 -8.459578e-18 -1.540700e-17  7.771146e-02
## [23,] -2.221570e-17 -2.426057e-18  1.047842e-17  5.661815e-02
## [24,] -2.623601e-17  3.663699e-18  1.133343e-17  3.534748e-02
## [25,] -3.025633e-17  9.753455e-18  1.218844e-17  1.407681e-02
## [26,]  2.549813e-19  1.514500e-17 -7.747396e-18 -9.478989e-19
## [27,] -2.791698e-18  1.851837e-17 -1.237519e-17 -4.276999e-18
## [28,] -5.838377e-18  8.013954e-18 -1.700299e-17  2.883796e-18
## [29,]  8.420859e-18  2.526511e-17 -2.163078e-17 -1.607927e-17
## [30,] -4.970012e-18  2.863848e-17 -2.625858e-17 -1.069994e-17
## [31,]  2.916461e-18  3.201185e-17 -7.347135e-18 -1.674167e-17
## [32,] -1.000052e-17  4.926007e-17  3.920668e-17 -3.110541e-17
## [33,]  6.676441e-18  4.064154e-17  4.213191e-17 -4.880331e-17
## [34,]  2.081456e-17  7.461604e-17  4.465397e-17 -7.981090e-17
## [35,]  4.099450e-17  9.772289e-17  6.582262e-18 -3.520756e-17
## [36,]  6.234505e-17  9.965684e-17 -4.982930e-18 -3.738688e-17
## [37,]  6.981781e-17  1.154686e-16  3.896303e-17 -2.568840e-17
## [38,]  8.886031e-17  1.398217e-16  2.262408e-17 -2.131225e-17
## [39,]  1.355006e-16  1.647588e-16  5.958751e-18 -1.743672e-17
## [40,]  1.136552e-16  1.896959e-16 -2.904059e-16 -1.240354e-16
## [41,]  1.322695e-16  2.098082e-16 -3.099951e-16 -1.359354e-16
## [42,]  1.524741e-16  2.180646e-16 -2.705815e-16 -1.242320e-16
## [43,]  2.026960e-16  2.401988e-16 -2.866792e-16  3.488736e-16
## [44,]  2.238783e-16  2.345775e-16 -3.582879e-16  3.849905e-16
## [45,]  2.407665e-16  2.428339e-16 -6.654628e-17 -2.965659e-17
## [46,]  2.679767e-16  2.649681e-16 -7.201559e-17 -3.904200e-17
## [47,] -5.613827e-16  2.593468e-16 -7.500727e-17  7.047702e-17
## [48,] -6.123443e-16  2.676032e-16 -8.188662e-17  7.282633e-17
## [49,] -8.176810e-16  1.714934e-16 -3.214498e-18  7.811965e-17
## [50,] -8.796692e-16  1.887804e-16  1.313484e-31  7.324101e-17
##               [,30]
##  [1,]  4.054617e-02
##  [2,]  3.916596e-02
##  [3,]  3.855900e-02
##  [4,]  3.805690e-02
##  [5,]  3.862126e-02
##  [6,]  3.931218e-02
##  [7,]  4.000310e-02
##  [8,]  4.054261e-02
##  [9,]  4.073396e-02
## [10,]  3.721139e-02
## [11,]  3.075077e-02
## [12,]  2.424971e-02
## [13,]  1.784871e-02
## [14,]  9.237601e-03
## [15,]  3.791187e-04
## [16,]  0.000000e+00
## [17,] -2.272123e-18
## [18,] -3.444805e-18
## [19,] -4.414749e-18
## [20,] -5.373296e-18
## [21,] -6.331844e-18
## [22,] -7.290391e-18
## [23,]  9.294036e-18
## [24,]  1.104651e-17
## [25,]  1.292451e-17
## [26,]  1.575122e-17
## [27,]  1.694304e-17
## [28,]  1.813486e-17
## [29,]  1.932668e-17
## [30,]  2.051850e-17
## [31,]  1.045782e-17
## [32,]  2.509476e-17
## [33,]  2.907556e-17
## [34,]  3.462011e-17
## [35,]  1.193876e-16
## [36,]  1.219554e-16
## [37,]  1.245232e-16
## [38,]  1.254001e-16
## [39,]  1.261614e-16
## [40,]  9.063789e-17
## [41,]  9.369803e-17
## [42,]  9.103717e-17
## [43,]  8.837631e-17
## [44,]  1.134710e-16
## [45,]  5.915304e-16
## [46,]  6.400501e-16
## [47,]  7.942192e-17
## [48,]  7.853018e-17
## [49,] -2.507465e-16
## [50,] -2.711987e-16

4.6 Interpreting the efficient frontier

The portfolio with the greatest excess return per unit of portfolio volatility is the tangency portfolio.

4.7 Properties of the efficient frontier

What we did in the previous exercise: to find a portfolio that minimizes its variance satisfying the constraint that the expected return = a target return.

This may not be efficient or realistic b/c:

  • when the target return is below the minimum variance return, it’s not efficient b/c it can earn higher return with a lower risk
  • The optimized portfolios are not often realistic because they tend to invest large proprotions in only a few assets.

A practical solution to avoid these situations is to impose weight contraints.

  • Imposing a more strict weight constraint leads to a shift to the right of the efficient frontier.
  • The portfolios on the dashed lines are not mean-variance efficient.

4.8 The minimum variance and maximum Sharpe ratio portfolio

Identify the portfolios with the least volatility and the greatest Sharpe ratio, using the outputs of the efficient frontier calculation in the previous section.

  • vpm: vector of the portfolio means
  • vpsd: vector of standard deviations or volatility
  • mweights: a matrix of weights
# Define vpm, vpsd, and mweights
vpm <- c(0.007972167, 0.008296030, 0.008619892, 0.008943755, 0.009267617, 0.009591480,
         0.009915342, 0.010239205, 0.010563067, 0.010886930, 0.011210792, 0.011534655,
         0.011858517, 0.012182380, 0.012506242, 0.012830104, 0.013153967, 0.013477829,
         0.013801692, 0.014125554, 0.014449417, 0.014773279, 0.015097142, 0.015421004,
         0.015744867, 0.016068729, 0.016392592, 0.016716454, 0.017040317, 0.017364179,
         0.017688042, 0.018011904, 0.018335767, 0.018659629, 0.018983492, 0.019307354,
         0.019631217, 0.019955079, 0.020278942, 0.020602804, 0.020926667, 0.021250529,
         0.021574392, 0.021898254, 0.022222117, 0.022545979, 0.022869842, 0.023193704,
         0.023517567)
vpsd <- c(0.05753845, 0.05053003, 0.04459450, 0.03987652, 0.03653676, 0.03446960,
          0.03382149, 0.03371899, 0.03372842, 0.03383093, 0.03402107, 0.03429342,
          0.03464616, 0.03508031, 0.03560354, 0.03621771, 0.03691892, 0.03770411,
          0.03858412, 0.03955908, 0.04062926, 0.04178903, 0.04303549, 0.04436324,
          0.04576521, 0.04723549, 0.04876942, 0.05036122, 0.05201652, 0.05375809,
          0.05557950, 0.05747316, 0.05943216, 0.06145073, 0.06355415, 0.06604677,
          0.06899817, 0.07237495, 0.07614625, 0.08027864, 0.08471929, 0.08949816,
          0.09466386, 0.10015716, 0.10592712, 0.11193095, 0.11813300, 0.12450366,
          0.13102620)
mweights <- read.csv("data/Portfolio Theory/mweights.csv")
mweights
##               AA          AAPL           AXP            BA           BAC
## 1   2.500413e-01  2.826704e-17  1.298240e-16 -6.362972e-18 -8.562982e-17
## 2   1.958210e-01  2.146786e-17  1.014490e-16  2.646376e-17 -6.811375e-17
## 3   1.453890e-01  1.672552e-17  7.337035e-17  1.767478e-17 -5.039109e-17
## 4   9.680079e-02 -1.886119e-18  4.864670e-17  1.301775e-18 -2.812694e-17
## 5   4.549978e-02 -7.041962e-18  2.730571e-17 -1.111477e-19 -7.186243e-18
## 6   2.051779e-17 -8.406904e-19  1.881370e-17 -2.609600e-18 -1.509007e-18
## 7   1.514220e-17  8.222917e-21  2.389718e-17 -2.046173e-18 -7.952368e-18
## 8   1.165272e-17  9.354387e-03  2.972429e-17 -6.739938e-19 -8.444373e-18
## 9   9.047003e-18  1.605952e-02  3.453029e-17  1.121634e-18 -9.112574e-18
## 10  6.655250e-18  2.226933e-02  3.922388e-17  2.444314e-19 -1.034546e-17
## 11  4.409117e-18  2.724711e-02  4.329929e-17  7.369813e-19 -1.260687e-17
## 12  2.162984e-18  3.222489e-02  4.737469e-17 -1.630182e-18 -1.486827e-17
## 13  3.248697e-20  3.716548e-02  5.152175e-17  4.168953e-19 -1.734214e-17
## 14 -1.157919e-18  4.202161e-02  5.631682e-17  3.648254e-19 -2.037607e-17
## 15 -1.829032e-18  4.726362e-02  6.155048e-17  4.627241e-19 -2.331813e-17
## 16 -2.669072e-18  5.285722e-02  6.675532e-17 -3.511870e-18  4.714709e-18
## 17  2.695706e-17  5.845795e-02 -1.162312e-17 -1.358780e-18 -2.538167e-17
## 18  3.218065e-17  6.449409e-02 -1.979675e-17 -1.499820e-18 -2.677407e-17
## 19  3.839741e-17  7.117466e-02 -2.170256e-17 -1.619859e-18 -1.366421e-17
## 20  4.513903e-17  7.873367e-02 -2.441121e-17 -2.636909e-18 -2.777156e-17
## 21  5.220579e-17  8.674513e-02 -2.731966e-17 -3.654725e-18 -2.784274e-17
## 22  5.961727e-17  9.489772e-02 -3.051652e-17 -4.631062e-18 -2.796947e-17
## 23  6.804593e-17  1.032749e-01 -3.482801e-17 -5.836912e-18 -2.830768e-17
## 24  7.647460e-17  1.116520e-01 -3.913951e-17 -7.042762e-18 -2.864589e-17
## 25  8.490326e-17  1.200291e-01 -4.345101e-17 -8.248612e-18 -2.898409e-17
## 26  9.379230e-17  1.286769e-01 -4.751310e-17 -9.789777e-18 -2.935663e-17
## 27  1.028124e-16  1.374016e-01 -5.150421e-17 -1.142638e-17 -2.973895e-17
## 28  1.118324e-16  1.461264e-01 -5.549532e-17 -1.306298e-17 -1.624348e-17
## 29  1.235142e-16  1.561502e-01 -6.071588e-17 -2.371582e-18 -4.449207e-17
## 30  1.360665e-16  1.668757e-01 -6.629107e-17 -3.594232e-19 -3.112615e-17
## 31  1.486188e-16  1.776013e-01 -7.186627e-17  1.652735e-18 -3.163801e-17
## 32  1.611711e-16  1.883268e-01 -7.744146e-17  3.664894e-18 -3.214987e-17
## 33  1.737234e-16  1.990523e-01 -8.301665e-17 -1.815052e-17 -3.266173e-17
## 34  1.862849e-16  2.099116e-01 -8.876474e-17 -1.977029e-17 -3.292696e-17
## 35  2.031440e-16  2.323122e-01 -9.636096e-17 -1.588335e-17 -3.395362e-17
## 36  2.308004e-16  2.801528e-01 -1.068807e-16  8.764221e-18 -3.635558e-17
## 37  2.585890e-16  3.266719e-01 -1.184992e-16  6.705632e-18 -4.277921e-17
## 38  2.865690e-16  3.729527e-01 -1.304212e-16  5.506977e-18 -5.005706e-17
## 39  2.951646e-16  4.220202e-01 -1.454568e-16  7.398773e-18 -5.551414e-17
## 40  3.592713e-16  4.710876e-01 -1.604923e-16  9.290570e-18 -6.097122e-17
## 41  4.233780e-16  5.201551e-01 -1.755279e-16 -5.726891e-17 -6.642830e-17
## 42  4.346278e-16  5.789757e-01 -1.964728e-16 -7.051324e-17 -7.088561e-17
## 43  4.738872e-16  6.387296e-01 -2.179833e-16  1.515467e-17 -7.524723e-17
## 44  4.853910e-16  6.984836e-01 -2.394937e-16  1.867369e-17 -7.960885e-17
## 45  5.524060e-16  7.582375e-01 -2.610041e-16  1.822042e-17 -8.397048e-17
## 46  5.916653e-16  8.179915e-01  8.973086e-17  1.568126e-16 -8.833210e-17
## 47  6.309247e-16  8.777454e-01 -3.745595e-16  1.736660e-16 -9.269373e-17
## 48  6.701841e-16  9.374994e-01  1.470915e-17  6.451694e-16 -9.705535e-17
## 49  7.111711e-16  1.000000e+00 -5.291809e-17  6.949005e-16 -1.019374e-16
##              CAT           CVX            DD           DIS            GE
## 1   5.067696e-17 -3.404934e-17  5.406521e-17 -7.103697e-17 -1.132859e-16
## 2   4.706518e-17 -3.400983e-17  3.900540e-17 -5.278110e-17 -9.211364e-17
## 3   4.445128e-17 -1.673593e-17  2.984499e-17 -1.185420e-17 -6.914559e-17
## 4   3.641690e-17 -2.612255e-18  2.047724e-17  9.981674e-18 -5.084131e-17
## 5   2.657383e-17  4.426338e-03  1.226675e-17 -4.889695e-18 -3.626256e-17
## 6   2.373537e-17  4.034708e-02 -6.948974e-18  6.074775e-18 -2.409659e-17
## 7   2.101665e-17  4.726010e-02 -1.686279e-17  7.737701e-03 -2.016958e-17
## 8   1.354469e-17  3.750144e-02 -1.393143e-17  7.327910e-03 -1.974086e-17
## 9   7.752755e-18  3.003995e-02 -1.173947e-17  2.721441e-03 -1.958019e-17
## 10  2.424676e-18  2.253147e-02 -1.007532e-17  0.000000e+00 -1.980509e-17
## 11 -2.569653e-18  1.427486e-02 -7.796927e-18  0.000000e+00 -1.976049e-17
## 12 -7.563983e-18  6.018261e-03 -5.518535e-18 -1.734723e-18 -1.971588e-17
## 13 -1.268946e-17  0.000000e+00 -3.551801e-18  1.670623e-19 -1.982574e-17
## 14 -1.853673e-17  1.866952e-18 -2.174328e-18  1.983431e-19 -2.091832e-17
## 15 -2.504433e-17  6.026974e-18 -2.891743e-19 -3.183999e-19 -2.262307e-17
## 16 -3.182631e-17 -1.159835e-17  1.738821e-18 -1.213474e-17 -2.448859e-17
## 17 -3.593584e-18  5.033599e-17 -3.965905e-17  1.035384e-17  3.473296e-17
## 18 -1.227575e-18  5.069122e-17 -3.859466e-17  4.307113e-20  4.022639e-17
## 19  7.699280e-19  4.469774e-17 -3.827570e-17 -3.452491e-18  4.647068e-17
## 20  2.689905e-18  5.187293e-17 -3.896991e-17 -5.372869e-19  5.381224e-17
## 21  4.605590e-18  5.185278e-17 -4.007419e-17 -1.196993e-17  6.177120e-17
## 22  6.233321e-18  5.260298e-17 -4.110373e-17 -1.592674e-17  6.953877e-17
## 23  6.767275e-18  5.628625e-17 -4.167885e-17 -1.741923e-17  7.614653e-17
## 24  7.301230e-18  5.996953e-17 -4.225397e-17 -1.891172e-17  8.275428e-17
## 25  7.835184e-18  6.365281e-17 -4.282909e-17 -2.040421e-17  8.936204e-17
## 26  2.054456e-18  6.609987e-17 -4.308106e-17 -2.850997e-17  9.638814e-17
## 27  1.032918e-17  6.819510e-17 -4.324105e-17 -2.264525e-17  1.035333e-16
## 28  0.000000e+00  7.029033e-17 -4.340105e-17 -2.371943e-17  1.106785e-16
## 29 -3.719794e-19  7.277755e-17 -4.432974e-17 -2.719222e-17  1.138237e-16
## 30 -1.146113e-18  7.654956e-17 -4.558315e-17 -1.555043e-17  1.153523e-16
## 31  0.000000e+00  8.032156e-17 -4.683656e-17 -3.908639e-18  1.168808e-16
## 32  0.000000e+00  8.409357e-17 -4.808997e-17 -6.144636e-18  1.184094e-16
## 33  0.000000e+00  8.786558e-17 -4.934338e-17  6.669689e-17  1.199380e-16
## 34  2.336759e-19  9.106107e-17 -5.038489e-17  7.688538e-17  1.216145e-16
## 35  1.277346e-18  8.716399e-17 -4.211566e-17  9.509596e-17  1.159009e-16
## 36 -1.982995e-18  6.761429e-17 -1.243393e-17 -1.049386e-16  9.844472e-17
## 37 -3.933624e-19  5.558364e-17  1.551050e-17 -1.267053e-16  1.071007e-16
## 38 -8.736942e-19  4.499336e-17  4.299932e-17 -1.490944e-16  5.991466e-17
## 39 -1.352470e-18  2.290711e-17  6.680152e-17 -1.761133e-16  4.216634e-17
## 40 -1.831245e-18  8.208581e-19  9.060373e-17 -2.031323e-16  2.441802e-17
## 41 -2.150056e-17 -2.126539e-17  1.144059e-16 -1.384162e-16  6.669696e-18
## 42  1.205953e-17 -4.325143e-17  1.349058e-16 -1.721876e-16 -3.568855e-17
## 43 -1.086272e-18 -6.522788e-17  1.550896e-16 -2.069687e-16 -4.999018e-17
## 44  1.685873e-17 -2.489696e-16  1.752734e-16  3.293299e-16 -3.653623e-17
## 45  7.322463e-17 -2.897681e-16  1.954571e-16  3.430726e-16 -5.083786e-17
## 46 -1.004986e-17 -2.765802e-17  2.156409e-16 -3.473053e-16 -6.513949e-17
## 47  5.030533e-18 -4.626261e-17  2.358247e-16 -3.690310e-16 -7.944112e-17
## 48  5.769731e-17 -8.979484e-16  2.560085e-16  1.756662e-16 -1.214983e-16
## 49 -2.559344e-17 -9.465284e-16  2.811302e-16  1.926695e-16 -1.089489e-16
##               HD           HPQ          INTC           IBM           JNJ
## 1  -2.283165e-17 -4.810977e-17  3.174840e-17 -1.374090e-17  2.219800e-17
## 2  -1.303594e-17  4.705699e-18  2.069763e-17  1.703180e-18 -6.811368e-17
## 3   2.248861e-18 -1.029998e-17  1.613679e-17 -1.887659e-19 -8.390824e-18
## 4  -4.316595e-17  1.607002e-17  4.392765e-17  0.000000e+00 -4.337350e-18
## 5  -2.019011e-17  3.814920e-18 -2.279980e-17  3.645839e-02 -2.428021e-17
## 6  -7.178601e-18 -2.090251e-19 -1.182593e-17  5.650550e-02  2.178784e-18
## 7   7.182906e-19 -3.702768e-18 -3.482348e-19  5.716259e-02  4.090884e-02
## 8   1.645389e-02 -4.315389e-18  1.079063e-04  4.911507e-02  5.066905e-02
## 9   3.008586e-02 -4.044121e-18  6.367159e-03  4.068794e-02  5.618938e-02
## 10  4.248975e-02 -4.702404e-18  1.185527e-02  3.249727e-02  6.136902e-02
## 11  5.212163e-02 -7.893260e-18  1.452509e-02  2.320808e-02  6.575517e-02
## 12  6.175352e-02 -1.108412e-17  1.719490e-02  1.391888e-02  7.014131e-02
## 13  7.145924e-02 -1.302153e-18  1.996604e-02  4.588315e-03  7.447670e-02
## 14  8.205783e-02 -2.209552e-18  2.231590e-02  0.000000e+00  7.833811e-02
## 15  9.407332e-02 -3.059768e-18  2.387207e-02 -5.042334e-19  8.193263e-02
## 16  1.068698e-01  4.703074e-19  2.524267e-02 -2.629200e-18  8.531465e-02
## 17  1.196897e-01 -1.075245e-17  2.660274e-02 -2.169869e-17  8.866017e-02
## 18  1.300848e-01 -7.258228e-18  2.758538e-02 -1.786286e-17  9.175126e-02
## 19  1.379620e-01 -2.926741e-18  2.852771e-02 -1.297469e-17  9.367884e-02
## 20  1.458133e-01  1.377122e-18  2.953153e-02 -8.127668e-18  9.507295e-02
## 21  1.537302e-01 -1.230831e-18  3.063951e-02 -3.231589e-18  9.514983e-02
## 22  1.613996e-01  1.059174e-17  3.158218e-02  2.012095e-18  9.429551e-02
## 23  1.680427e-01  1.053237e-17  3.178330e-02  8.452977e-18  9.172683e-02
## 24  1.746859e-01  1.741190e-17  3.198442e-02  1.489386e-17  8.915815e-02
## 25  1.813290e-01  3.123032e-17  3.218554e-02  2.133474e-17  8.658948e-02
## 26  1.875540e-01  3.831063e-17  3.231194e-02  2.758636e-17  8.343673e-02
## 27  1.936599e-01  4.544810e-17  3.241708e-02  3.378412e-17  8.011775e-02
## 28  1.997659e-01  5.258556e-17  3.252222e-02  3.998187e-17  7.679877e-02
## 29  2.060558e-01  6.168862e-17  3.202106e-02  4.489586e-17  6.664637e-02
## 30  2.122036e-01  7.832401e-17  3.132751e-02  4.913502e-17  5.235103e-02
## 31  2.183513e-01  8.108162e-17  3.063397e-02  5.337417e-17  3.805568e-02
## 32  2.244990e-01  9.077812e-17  2.994042e-02  5.761333e-17  2.376034e-02
## 33  2.306467e-01  1.004746e-16  2.924688e-02  6.185249e-17  9.464992e-03
## 34  2.366837e-01  1.102672e-16  2.835296e-02  6.593730e-17  0.000000e+00
## 35  2.306000e-01  1.236838e-16  2.140331e-02  7.041439e-17 -6.477559e-19
## 36  2.010948e-01  3.337774e-17  3.281997e-03  7.954321e-17  1.860253e-17
## 37  1.699527e-01  5.421023e-17  0.000000e+00  8.844862e-17 -1.288824e-17
## 38  1.381966e-01  7.441573e-17  6.545491e-20  9.731412e-17 -1.260289e-17
## 39  9.343430e-02  9.851163e-17  3.511320e-18  1.066706e-16 -1.314846e-17
## 40  4.867197e-02  1.226075e-16  6.957185e-18  1.160270e-16 -1.369403e-17
## 41  3.909642e-03 -1.250268e-16 -3.318460e-17 -1.401009e-16 -4.891969e-18
## 42  0.000000e+00 -1.261589e-16 -3.424162e-17 -1.750423e-16 -4.761785e-18
## 43  0.000000e+00  5.844636e-17 -4.468367e-17 -2.109941e-16 -2.994946e-16
## 44  0.000000e+00  2.007822e-16  1.759194e-17 -3.173964e-16  8.825932e-18
## 45  0.000000e+00  2.321773e-16 -2.782783e-17 -3.576943e-16 -1.742315e-16
## 46  0.000000e+00  2.191169e-18  6.309564e-17 -7.430858e-17 -1.730874e-17
## 47  0.000000e+00 -6.457742e-18  8.764056e-19 -8.055161e-17 -2.885450e-16
## 48  0.000000e+00 -2.826020e-16 -8.212267e-17  3.516089e-16  1.594502e-18
## 49 -5.103331e-18 -3.059966e-16 -4.497721e-17  3.881073e-16  4.944721e-17
##              JPM            KO           MCD           MMM           MRK
## 1   1.388062e-17 -7.867687e-17 -7.651686e-17  6.771145e-18  0.000000e+00
## 2   8.629911e-18  1.301815e-17 -8.116949e-17  1.085035e-17  0.000000e+00
## 3   3.516594e-18  2.764076e-17 -8.100975e-17 -2.958045e-17  0.000000e+00
## 4  -2.662139e-19  3.716041e-18  7.922489e-17  2.692981e-18  9.016730e-04
## 5   2.082739e-18  7.773420e-03  3.071365e-17 -4.336809e-19  1.400944e-02
## 6   5.643014e-18  2.576002e-02  2.053103e-18  6.277400e-02  2.116400e-02
## 7   1.438959e-18  2.947633e-02  0.000000e+00  8.675831e-02  1.159836e-02
## 8   1.037766e-18  2.746359e-02  1.034804e-02  8.448407e-02  8.298201e-03
## 9   8.305309e-19  2.638024e-02  1.685984e-02  8.074190e-02  6.743282e-03
## 10  6.425496e-19  2.509905e-02  2.225611e-02  7.641016e-02  5.422007e-03
## 11  6.470796e-19  2.276019e-02  2.724509e-02  7.402585e-02  4.084224e-03
## 12  6.516096e-19  2.042133e-02  3.223407e-02  7.164155e-02  2.746441e-03
## 13  5.684486e-19  1.813505e-02  3.705720e-02  6.901777e-02  1.556290e-03
## 14  8.360793e-19  1.658232e-02  4.115140e-02  6.441364e-02  9.610909e-04
## 15  1.703637e-18  1.575249e-02  4.491994e-02  5.781032e-02  1.439690e-04
## 16 -6.426945e-20  1.493977e-02  4.860488e-02  5.067389e-02  0.000000e+00
## 17  0.000000e+00  1.411051e-02  5.228460e-02  4.354675e-02  4.528918e-20
## 18  2.438955e-03  1.158014e-02  5.530831e-02  3.600302e-02 -4.071859e-18
## 19  5.667172e-03  7.204534e-03  5.792113e-02  2.803722e-02  1.849165e-18
## 20  9.573757e-03  1.680197e-03  5.883952e-02  1.850203e-02  6.265146e-18
## 21  1.348064e-02  0.000000e+00  5.888313e-02  8.206057e-03  3.894147e-19
## 22  1.728077e-02  1.933105e-19  5.898914e-02  0.000000e+00  5.402286e-19
## 23  2.097557e-02  1.102899e-18  5.990503e-02 -1.734724e-18 -7.738553e-19
## 24  2.467036e-02  2.012487e-18  6.082093e-02  0.000000e+00 -2.087939e-18
## 25  2.836516e-02 -3.469447e-18  6.173682e-02 -3.485962e-18 -3.402023e-18
## 26  3.165882e-02  3.520982e-19  6.202449e-02 -2.790620e-18  7.638276e-19
## 27  3.483832e-02  8.044086e-19  6.213335e-02  3.703466e-18  2.055267e-19
## 28  3.801782e-02  4.905722e-18  6.224221e-02 -1.163505e-17  2.044717e-18
## 29  4.169642e-02  9.961311e-18  5.482618e-02 -1.588247e-17 -4.721045e-18
## 30  4.532427e-02  1.478749e-18  4.424313e-02 -2.162749e-17 -1.754495e-17
## 31  4.895213e-02 -7.003814e-18  3.366009e-02 -2.737251e-17 -3.036886e-17
## 32  5.257999e-02 -1.548638e-17  2.307704e-02 -3.311753e-17 -4.319277e-17
## 33  5.620784e-02  3.286213e-17  1.249400e-02 -3.061097e-17 -2.975738e-17
## 34  6.023836e-02 -4.967573e-17  7.273827e-04 -4.572409e-17 -7.477499e-18
## 35  5.807319e-02  7.375990e-17 -9.980028e-19 -3.969007e-17  4.983114e-18
## 36  4.227169e-02  1.400211e-17  0.000000e+00 -1.190382e-16  1.479384e-17
## 37  2.147975e-02  1.647477e-17  1.601930e-17 -1.619468e-16  4.844798e-17
## 38  0.000000e+00  1.942097e-17  2.214144e-17 -2.040436e-16  6.757094e-17
## 39  0.000000e+00  1.351249e-17  2.961028e-17 -2.450107e-16  8.573887e-17
## 40  0.000000e+00  7.603996e-18  3.707912e-17 -2.859779e-16  1.039068e-16
## 41  0.000000e+00  2.300512e-16  4.070032e-18 -2.609963e-16  7.581769e-17
## 42  8.979704e-18  2.427221e-16  5.158039e-18 -2.874165e-16  1.062089e-16
## 43  5.880532e-18  3.005300e-17 -6.572363e-17 -3.132069e-16  1.960477e-16
## 44 -2.865785e-17  3.693723e-16 -1.284346e-17 -7.440417e-17  1.825321e-16
## 45 -2.646732e-17  4.185701e-16 -2.395542e-16 -7.154623e-17 -2.309082e-16
## 46 -2.416799e-17 -5.628326e-16 -7.956777e-18  2.717402e-16  5.263110e-16
## 47 -6.908392e-17 -6.114574e-16  2.959947e-17  2.942909e-16  5.814719e-16
## 48  0.000000e+00 -1.109785e-16  1.173563e-16  5.695255e-16  3.477732e-17
## 49 -1.350489e-17 -1.396921e-16  0.000000e+00  6.117761e-16  4.364512e-17
##             MSFT           NKE           PFE            PG           TRV
## 1  -3.308735e-17  1.967923e-17 -2.886706e-16  0.000000e+00 -2.190680e-17
## 2  -1.839682e-17  1.908626e-17  2.209616e-17  5.980581e-02 -1.501376e-18
## 3  -6.688891e-18  1.729856e-17 -2.396707e-17  1.341766e-01 -2.353606e-17
## 4   1.398479e-17  2.174111e-17  2.235910e-18  1.740898e-01  6.094680e-18
## 5   2.536649e-17  1.427626e-17 -2.002834e-17  1.981513e-01  5.229552e-18
## 6   5.935482e-18 -2.670393e-18  5.333580e-18  1.942548e-01  0.000000e+00
## 7   3.783007e-18 -2.895636e-18  4.404974e-03  1.853456e-01  2.721564e-02
## 8   5.540859e-19  0.000000e+00  1.080263e-02  1.810028e-01  3.128128e-02
## 9  -2.026789e-18  6.463374e-03  1.410979e-02  1.785223e-01  3.253681e-02
## 10  4.247727e-04  1.574007e-02  1.652421e-02  1.760481e-01  3.281676e-02
## 11  8.175591e-03  2.379828e-02  1.709996e-02  1.736616e-01  3.363349e-02
## 12  1.592641e-02  3.185650e-02  1.767570e-02  1.712751e-01  3.445022e-02
## 13  2.363293e-02  3.993816e-02  1.811494e-02  1.691179e-01  3.502441e-02
## 14  3.075733e-02  4.877301e-02  1.795476e-02  1.678369e-01  3.486992e-02
## 15  3.697752e-02  5.852968e-02  1.773744e-02  1.676417e-01  3.423981e-02
## 16  4.282550e-02  6.855594e-02  1.723785e-02  1.680597e-01  3.316052e-02
## 17  4.867448e-02  7.858945e-02  1.667893e-02  1.684677e-01  3.206386e-02
## 18  5.454244e-02  8.850511e-02  1.596658e-02  1.675765e-01  3.020453e-02
## 19  6.081004e-02  9.845169e-02  1.559932e-02  1.654400e-01  2.768790e-02
## 20  6.682115e-02  1.088534e-01  1.425606e-02  1.633842e-01  2.462474e-02
## 21  7.268038e-02  1.194679e-01  1.268482e-02  1.602001e-01  2.094815e-02
## 22  7.878842e-02  1.300457e-01  1.119219e-02  1.561119e-01  1.679997e-02
## 23  8.593499e-02  1.403646e-01  9.912578e-03  1.503917e-01  1.158511e-02
## 24  9.308156e-02  1.506835e-01  8.632970e-03  1.446714e-01  6.370249e-03
## 25  1.002281e-01  1.610024e-01  7.353362e-03  1.389512e-01  1.155386e-03
## 26  1.078008e-01  1.706873e-01  5.723080e-03  1.334784e-01  0.000000e+00
## 27  1.154947e-01  1.801918e-01  3.992991e-03  1.280760e-01  0.000000e+00
## 28  1.231887e-01  1.896963e-01  2.262901e-03  1.226736e-01 -2.472750e-18
## 29  1.318359e-01  2.015398e-01  0.000000e+00  1.092283e-01 -1.025837e-18
## 30  1.406660e-01  2.142174e-01  0.000000e+00  9.279142e-02  2.548881e-18
## 31  1.494961e-01  2.268949e-01  0.000000e+00  7.635453e-02 -6.525836e-18
## 32  1.583262e-01  2.395725e-01  0.000000e+00  5.991765e-02 -8.825120e-18
## 33  1.671564e-01  2.522501e-01  5.300192e-18  4.348077e-02  1.318579e-18
## 34  1.755296e-01  2.647073e-01  7.912357e-18  2.384915e-02 -4.171643e-18
## 35  1.844730e-01  2.731383e-01  3.053827e-18  1.734723e-18 -1.113641e-17
## 36  1.972442e-01  2.759546e-01  3.544613e-17 -2.964420e-17  3.191239e-17
## 37  2.041424e-01  2.777532e-01  5.829667e-17 -6.667980e-18  1.762612e-17
## 38  2.096187e-01  2.792320e-01  8.153672e-17 -2.971723e-18  3.045491e-17
## 39  2.087358e-01  2.758097e-01  1.011189e-16  1.671413e-17  3.228483e-17
## 40  2.078530e-01  2.723874e-01  1.207012e-16  8.644409e-18  3.411475e-17
## 41  2.069701e-01  2.689651e-01  1.832817e-17 -8.953167e-17 -3.429012e-17
## 42  1.846651e-01  2.363593e-01  2.051187e-17 -1.046271e-16 -3.039798e-17
## 43  1.603099e-01  2.009605e-01 -3.079701e-17  1.334466e-16  2.659859e-16
## 44  1.359548e-01  1.655617e-01 -5.721738e-17  2.164734e-16  2.671727e-17
## 45  1.115996e-01  1.301629e-01 -2.428400e-16  1.961839e-16  1.163002e-16
## 46  8.724448e-02  9.476406e-02  3.486614e-18  2.449501e-16  1.531190e-16
## 47  6.288933e-02  5.936526e-02 -1.400425e-16  2.729820e-16  1.615390e-16
## 48  3.853418e-02  2.396646e-02  2.904625e-17 -6.711027e-17 -1.454698e-16
## 49  3.989864e-17  0.000000e+00 -6.043526e-16 -2.727196e-16 -1.613949e-16
##              UTX            VZ           WMT           XOM             T
## 1  -7.049095e-17  6.734542e-01  2.509023e-18  7.650450e-02  3.066067e-18
## 2  -3.965611e-17  5.629189e-01 -4.258935e-18  1.814543e-01  2.286827e-17
## 3  -1.365759e-17  4.590255e-01  1.062859e-02  2.507802e-01 -8.029829e-18
## 4   7.785156e-18  3.559124e-01  7.039491e-02  3.019005e-01 -2.612188e-19
## 5   1.985463e-17  2.491180e-01  1.127597e-01  3.220874e-01  9.716212e-03
## 6  -2.070807e-18  1.548682e-01  1.335944e-01  2.822761e-01  2.845598e-02
## 7  -1.619308e-17  6.667664e-02  1.400805e-01  2.547516e-01  4.062279e-02
## 8  -2.372719e-17  5.684937e-02  1.271161e-01  2.525890e-01  3.923531e-02
## 9  -3.104579e-17  4.981912e-02  1.143219e-01  2.528171e-01  3.853303e-02
## 10 -3.887698e-17  4.263940e-02  1.016478e-01  2.538806e-01  3.807892e-02
## 11 -4.806821e-17  3.337666e-02  9.037539e-02  2.557417e-01  3.889004e-02
## 12 -5.725943e-17  2.411391e-02  7.910303e-02  2.576028e-01  3.970116e-02
## 13 -6.663949e-17  1.490326e-02  6.774791e-02  2.576690e-01  4.042938e-02
## 14 -7.667986e-17  4.889669e-03  5.508146e-02  2.512686e-01  4.072637e-02
## 15 -8.693730e-17  0.000000e+00  4.030010e-02  2.421311e-01  3.667420e-02
## 16 -9.727575e-17  9.502546e-19  2.456291e-02  2.320269e-01  2.906776e-02
## 17  6.009683e-18 -4.846783e-17  8.815220e-03  2.219223e-01  2.143569e-02
## 18 -7.582784e-19 -4.428342e-17 -1.369616e-18  2.112384e-01  1.272050e-02
## 19 -7.597545e-19 -3.894723e-17  2.500848e-18  1.995169e-01  2.320883e-03
## 20 -2.364548e-18 -3.465763e-17 -7.087999e-18  1.843136e-01  0.000000e+00
## 21 -3.381906e-18 -3.178440e-17 -6.072710e-18  1.671842e-01 -2.423104e-18
## 22 -5.056053e-18 -2.852162e-17 -8.450762e-18  1.486169e-01 -3.746374e-18
## 23 -9.240374e-18 -2.215145e-17 -1.065953e-17  1.261027e-01 -4.871683e-18
## 24 -1.342469e-17 -1.578128e-17 -1.286830e-17  1.035886e-01 -5.996993e-18
## 25 -1.760901e-17 -9.411103e-18 -1.854652e-17  8.107444e-02 -7.122302e-18
## 26 -2.221013e-17 -2.434493e-18  1.047724e-17  5.664762e-02  9.291608e-18
## 27 -2.692987e-17  4.714718e-18  1.148100e-17  3.167642e-02  1.134897e-17
## 28  4.062673e-18  1.186393e-17  1.862392e-17  6.705223e-03  1.984175e-17
## 29 -1.330920e-18  1.690096e-17 -1.015632e-17 -3.412579e-18  1.637161e-17
## 30 -4.907640e-18  2.086120e-17  1.216635e-17  2.692665e-18  1.777077e-17
## 31 -5.520967e-18  2.482145e-17 -2.102214e-17 -8.935259e-18  3.304772e-17
## 32 -4.949340e-18  2.878170e-17 -2.645505e-17 -1.076615e-17  2.056909e-17
## 33 -9.003699e-18  3.274194e-17  3.595732e-17 -2.877185e-17  2.261945e-17
## 34  8.996838e-18  3.651662e-17  4.030141e-17  8.884083e-18  2.634263e-17
## 35  1.329440e-17  5.050846e-17  4.361276e-17 -6.309961e-17  3.209967e-17
## 36  3.612317e-17  7.807685e-17  1.207889e-16 -5.267598e-17  1.191574e-16
## 37  6.047305e-17  9.827048e-17  5.154225e-17 -3.841259e-17  1.217302e-16
## 38  8.548303e-17  1.170365e-16  3.785129e-17 -2.485335e-17  1.247045e-16
## 39  1.076532e-16  1.463120e-16  1.828664e-17 -2.030357e-17  1.255982e-16
## 40  1.298233e-16  1.755875e-16  5.423314e-17 -1.575380e-17  1.264920e-16
## 41  1.245830e-16  2.048630e-16 -3.031845e-16 -1.302454e-16  1.216707e-16
## 42  1.480732e-16  2.162662e-16 -3.225864e-16 -1.207357e-16  9.161674e-17
## 43  2.017674e-16  2.259591e-16 -3.414846e-16  3.472903e-16  8.849296e-17
## 44  2.676186e-16  2.356519e-16 -6.178871e-17 -2.149254e-17  5.493247e-16
## 45  2.490413e-16  2.453447e-16 -6.820954e-17 -6.026634e-17  6.062855e-16
## 46  2.926800e-17  2.411598e-16 -7.141681e-17  6.925087e-17  7.988733e-17
## 47 -5.946124e-16  2.647304e-16 -7.949297e-17  7.200889e-17  7.884046e-17
## 48 -8.064303e-16  1.677073e-16 -4.175122e-18  7.839001e-17 -2.473557e-16
## 49 -8.796692e-16  1.887804e-16  0.000000e+00  7.324101e-17 -2.711987e-16

# Create weights_minvar as the portfolio with the least risk
weights_minvar <- mweights[vpsd == min(vpsd), ]
weights_minvar
##             AA        AAPL          AXP            BA           BAC
## 8 1.165272e-17 0.009354387 2.972429e-17 -6.739938e-19 -8.444373e-18
##            CAT        CVX            DD        DIS            GE
## 8 1.354469e-17 0.03750144 -1.393143e-17 0.00732791 -1.974086e-17
##           HD           HPQ         INTC        IBM        JNJ          JPM
## 8 0.01645389 -4.315389e-18 0.0001079063 0.04911507 0.05066905 1.037766e-18
##           KO        MCD        MMM         MRK         MSFT NKE        PFE
## 8 0.02746359 0.01034804 0.08448407 0.008298201 5.540859e-19   0 0.01080263
##          PG        TRV           UTX         VZ       WMT      XOM
## 8 0.1810028 0.03128128 -2.372719e-17 0.05684937 0.1271161 0.252589
##            T
## 8 0.03923531

# Calculate the Sharpe ratio
vsr <- (vpm - 0.0075) / vpsd
vsr
##  [1] 0.008206113 0.015753602 0.025112783 0.036205642 0.048379139
##  [6] 0.060676074 0.071414417 0.081236271 0.090815609 0.100113417
## [11] 0.109073348 0.117650995 0.125800868 0.133476015 0.140610793
## [16] 0.147168443 0.153145515 0.158545819 0.163323461 0.167485038
## [21] 0.171044636 0.174047567 0.176532020 0.178548817 0.180155778
## [26] 0.181404469 0.182339507 0.183006964 0.183409367 0.183491992
## [31] 0.183305751 0.182901097 0.182321608 0.181602871 0.180688311
## [36] 0.178772618 0.175819402 0.172091020 0.167821029 0.163216567
## [41] 0.158484178 0.153640354 0.148677563 0.143756612 0.138983454
## [46] 0.134421972 0.130106253 0.126050142 0.122247054

# Create weights_max_sr as the portfolio with the maximum Sharpe ratio
weights_max_sr <- mweights[vsr == max(vsr), ]
weights_max_sr
##              AA      AAPL           AXP            BA           BAC
## 30 1.360665e-16 0.1668757 -6.629107e-17 -3.594232e-19 -3.112615e-17
##              CAT          CVX            DD           DIS           GE
## 30 -1.146113e-18 7.654956e-17 -4.558315e-17 -1.555043e-17 1.153523e-16
##           HD          HPQ       INTC          IBM        JNJ        JPM
## 30 0.2122036 7.832401e-17 0.03132751 4.913502e-17 0.05235103 0.04532427
##              KO        MCD           MMM           MRK     MSFT       NKE
## 30 1.478749e-18 0.04424313 -2.162749e-17 -1.754495e-17 0.140666 0.2142174
##    PFE         PG          TRV          UTX          VZ          WMT
## 30   0 0.09279142 2.548881e-18 -4.90764e-18 2.08612e-17 1.216635e-17
##             XOM            T
## 30 2.692665e-18 1.777077e-17

# Create barplot of weights_minvar and weights_max_sr
par(mfrow = c(2, 1), mar = c(3, 2, 2, 1))
barplot(weights_minvar[weights_minvar > 0.01], xlab = names(weights_minvar[weights_minvar > 0.01]))
barplot(weights_max_sr[weights_max_sr > 0.01])

4.9 Split-sample evaluation

Illustrate how portfolio weights can differ when changing the estimation window.

estimation errors Split a sample into two parts - one for estimation and another for evaluation.

# Create returns_estim 
returns_estim <- window(returns, start = "1991-01-01", end = "2003-12-31")

# Create returns_eval
returns_eval <- window(returns, start = "2004-01-01", end = "2015-12-31")

# Create vector of max weights
max_weights <- rep(0.1, ncol(returns))

# Create portfolio with estimation sample 
pf_estim <- portfolio.optim(returns_estim, reshigh = max_weights)
pf_estim
## $pw
##  [1]  1.628280e-17  3.116144e-03 -9.344865e-18 -1.732265e-19  2.366191e-02
##  [6]  3.258116e-02  1.000000e-01  4.748075e-18  1.053486e-18  2.899345e-02
## [11]  6.119520e-02  2.166993e-03  6.210103e-02 -2.244767e-18  4.312822e-02
## [16] -3.924516e-18  7.663393e-02  3.635972e-18  1.000000e-01  1.300691e-18
## [21]  2.898259e-02  0.000000e+00  9.338919e-02  1.000000e-01  4.143141e-02
## [26] -8.699639e-17 -8.804702e-19  5.056206e-02  1.000000e-01  5.205670e-02
## 
## $px
##   [1]  0.0487060879  0.0736240214  0.0431434953  0.0098301788  0.0438597618
##   [6] -0.0443020794  0.0398076843  0.0404540754 -0.0105952193  0.0304415980
##  [11] -0.0225822315  0.1351560614 -0.0147051046  0.0121501627 -0.0233317261
##  [16]  0.0311154339  0.0283690810 -0.0198186785  0.0541699635 -0.0050271036
##  [21]  0.0242681013  0.0101762756  0.0250654309  0.0215729196 -0.0045306098
##  [26]  0.0216718875  0.0267484214 -0.0154160756  0.0392081873 -0.0092773746
##  [31] -0.0255512592  0.0491139140 -0.0151896632  0.0297992064  0.0047063292
##  [36]  0.0060836365  0.0165608693 -0.0148954209 -0.0400403601  0.0279434064
##  [41]  0.0179861577 -0.0284965351  0.0425511791  0.0463390725 -0.0181927855
##  [46]  0.0533498159 -0.0152467609  0.0100693350  0.0312300401  0.0377137111
##  [51]  0.0337029280  0.0335936954  0.0389164803  0.0126451088  0.0311220143
##  [56] -0.0162185590  0.0428326192  0.0208169015  0.0552500523  0.0142492216
##  [61]  0.0106440416  0.0140146417  0.0201283381  0.0214012831  0.0484186368
##  [66]  0.0165348590 -0.0251828536  0.0298859009  0.0640675243  0.0348364575
##  [71]  0.0699066374 -0.0179151278  0.0777083808  0.0227591508 -0.0226525768
##  [76]  0.0816440237  0.0558271598  0.0631814094  0.0515993285 -0.0654762071
##  [81]  0.0539502069 -0.0073344956  0.0518461634 -0.0094513237  0.0262214396
##  [86]  0.0771979229  0.0481368210  0.0252245005 -0.0111452395  0.0378754734
##  [91] -0.0198720468 -0.1077139703  0.0600292035  0.0777270489  0.0585444344
##  [96]  0.0336705099  0.0139894639 -0.0168584250  0.0425285444  0.0628474731
## [101] -0.0379588095  0.0299841718 -0.0108184173  0.0404659879 -0.0342578162
## [106]  0.0807659849  0.0063982189  0.0240117866 -0.0376209774 -0.0873048041
## [111]  0.0750792846 -0.0004568123  0.0405235622 -0.0090459160  0.0066310318
## [116]  0.0199392316  0.0048751514  0.0168918803 -0.0012974927  0.0278599523
## [121]  0.0008950385 -0.0377194256 -0.0491042060  0.0739649461  0.0165100659
## [126] -0.0238163521  0.0241918791 -0.0381052945 -0.0401013914  0.0335016800
## [131]  0.0573039468  0.0213442469 -0.0087115457  0.0231111869  0.0266253835
## [136] -0.0301446834 -0.0166359956 -0.0427083849 -0.0689251412 -0.0006432457
## [141] -0.1006761956  0.0895542475  0.0315842675 -0.0511359407 -0.0318794535
## [146]  0.0024754456  0.0317846143  0.0394687267  0.0519944967  0.0171828753
## [151]  0.0168436683  0.0109663479 -0.0038367050  0.0663849868  0.0015730968
## [156]  0.0654423503
## 
## $pm
## [1] 0.01560684
## 
## $ps
## [1] 0.03826049
# Create portfolio with evaluation sample
pf_eval <- portfolio.optim(returns_eval, reshigh = max_weights)
pf_eval
## $pw
##  [1] -2.663661e-17  1.136072e-02  1.644377e-17  6.081566e-18 -2.966699e-17
##  [6] -3.283757e-17 -2.319759e-18  1.490390e-16 -3.786811e-17  3.630303e-17
## [11]  7.888504e-02  1.124801e-17  4.455822e-18  9.114306e-02  1.000000e-01
## [16] -8.472577e-18  4.523224e-02  1.000000e-01  0.000000e+00  2.863112e-02
## [21]  7.652233e-18  8.522570e-02 -1.123040e-18  1.000000e-01 -1.609293e-17
## [26]  2.033972e-18  5.952213e-02  1.000000e-01  1.000000e-01  1.000000e-01
## 
## $px
##   [1]  2.041899e-02  2.941353e-02 -7.208661e-04 -6.853856e-03 -3.786658e-03
##   [6]  6.823606e-03  1.725418e-03  2.284573e-02  4.422581e-03  1.843964e-02
##  [11]  2.848488e-02  3.465939e-02 -2.626379e-02  3.172696e-02 -2.316050e-02
##  [16] -3.400005e-02  1.803094e-02 -1.554836e-02  4.801377e-02 -2.119194e-02
##  [21]  1.171460e-02 -4.450706e-03  3.719125e-02 -2.070659e-02  2.147688e-02
##  [26]  1.347529e-02  4.480988e-03 -9.858718e-03 -1.395371e-02  9.168388e-03
##  [31]  2.722227e-02  2.926601e-02  4.296004e-02  4.987660e-02  8.952102e-03
##  [36]  2.794376e-02  1.505679e-02 -1.881241e-02  9.876588e-03  4.447958e-02
##  [41]  3.730588e-02 -5.388794e-03 -1.270349e-02  1.901583e-02  4.048205e-02
##  [46]  3.097767e-02 -1.284574e-02  9.172530e-03 -4.430373e-02 -2.041993e-02
##  [51]  4.512605e-02  3.928017e-02  2.584157e-03 -7.480683e-02  1.037782e-02
##  [56]  3.453519e-02 -2.469539e-02 -8.815955e-02 -1.113074e-03  8.643382e-03
##  [61] -7.055957e-02 -6.064960e-02  6.067957e-02  2.606593e-02  3.297259e-02
##  [66] -5.130036e-03  6.012982e-02  3.788286e-03  2.771833e-02 -5.586239e-03
##  [71]  7.230940e-02  6.401024e-03 -3.312884e-02  2.312380e-02  3.968292e-02
##  [76]  8.918939e-03 -5.191000e-02 -3.120179e-02  5.154829e-02 -2.539851e-03
##  [81]  7.226271e-02  3.306167e-02  4.396175e-05  4.154252e-02  8.005507e-03
##  [86]  2.101841e-02 -5.191521e-03  4.817036e-02  8.007822e-03 -1.898824e-03
##  [91] -8.985077e-03 -5.145997e-03 -1.210711e-02  5.692481e-02  2.365371e-02
##  [96]  3.387051e-02  7.485619e-03  2.749749e-02  2.238922e-02  6.450904e-03
## [101] -1.700812e-02  2.585119e-02  3.529055e-02  4.482220e-03  3.230984e-02
## [106] -1.769298e-02  1.970336e-03 -1.256543e-02  5.446239e-02  1.853805e-02
## [111]  4.313120e-02  2.974021e-02 -1.470173e-02  1.455067e-03  2.699690e-02
## [116] -4.333595e-02  1.528898e-02  4.128777e-02  2.791152e-02  5.820023e-03
## [121] -5.044905e-02  3.010640e-02  2.405325e-02  2.737462e-02 -1.233885e-05
## [126] -1.045931e-03 -9.422937e-03  3.421869e-02  1.179095e-02  4.412998e-03
## [131]  3.256918e-02 -1.823271e-02 -2.674275e-02  4.368554e-02 -2.055555e-02
## [136]  5.095444e-03  4.725147e-04 -1.303261e-02  1.569225e-02 -5.174120e-02
## [141]  4.369566e-03  5.320624e-02  9.916602e-03  7.736116e-03
## 
## $pm
## [1] 0.009127011
## 
## $ps
## [1] 0.02904969

Interprtation

  • The optimal portfolio weights of the estimation period are a poor match for that of the evaluation period.
  • Do some research on this!

4.10 Out of sample performance evaluation

Illustrate how your returns can change based on the weighting created by an optimized portfolio. Compare, for the portfolio weights in pf_estim,

  • the return you expected using the evaluation sample (returns_estim) with
  • the actual return on the out-of-sample period (returns_eval).
# Create returns_pf_estim
returns_pf_estim <- Return.portfolio(returns_estim, pf_estim$pw, rebalance_on = "months")


# Create returns_pf_eval
returns_pf_eval <- Return.portfolio(returns_eval, pf_estim$pw, rebalance_on = "months")

# Print a table for your estimation portfolio
table.AnnualizedReturns(returns_pf_estim)
##                           portfolio.returns
## Annualized Return                    0.1940
## Annualized Std Dev                   0.1325
## Annualized Sharpe (Rf=0%)            1.4634

# Print a table for your evaluation portfolio
table.AnnualizedReturns(returns_pf_eval)
##                           portfolio.returns
## Annualized Return                    0.0864
## Annualized Std Dev                   0.1242
## Annualized Sharpe (Rf=0%)            0.6954

Interpretation

  • The actual return is less than half of the expected return.
  • The actual Sharpe ratio is less than half of the expected Sharpe ratio.
  • The portfolio didn’t perform as well as expected.

It ain’t over

Explore other useful R packages for portfolio analysis like PortfolioAnalytics. Instead of optimizing the portfolio by minimizing the variance under a return target constraint, this package allows you optimize virtually all objective functions under all possible constraints.

Quiz (Chapter 1 and 2)

Calculate the Sharpe Ratio of the NASDAQ Composite Index for the period of “1985-12-31” and “2016-08-01”. Refer to the codes you learned in Section 2.1 through 2.3. Use the getSymbols() function to import the NASDAQ Composite Index. Discuss the difference in the Sharpe Ratio between NASDAQ and S&P500.

# 
#sp500 <- getSymbols("^GSPC", from = "1985-12-31", to = "2016-08-01", auto.assign = FALSE)
nasdaq <- getSymbols("^IXIC", from = "1985-12-31", to = "2016-08-01", auto.assign = FALSE)

# Convert the daily frequency to monthly frequency: (2.1 Exploring the monthly S&P 500 returns)
nasdaq_monthly <- to.monthly(nasdaq)
head(nasdaq_monthly)
##          nasdaq.Open nasdaq.High nasdaq.Low nasdaq.Close nasdaq.Volume
## Dec 1985       323.6       324.9      323.2        324.9     112700000
## Jan 1986       325.0       335.8      322.1        335.8    2410850000
## Feb 1986       336.1       359.8      335.8        359.5    2345500000
## Mar 1986       360.0       374.7      359.1        374.7    2595900000
## Apr 1986       375.8       393.0      368.0        383.2    2814600000
## May 1986       383.2       400.4      381.5        400.2    2412320000
##          nasdaq.Adjusted
## Dec 1985           324.9
## Jan 1986           335.8
## Feb 1986           359.5
## Mar 1986           374.7
## Apr 1986           383.2
## May 1986           400.2

# Define nasdaq_returns (2.2 The monthly mean and volatility)
nasdaq_returns <- Return.calculate(nasdaq_monthly$nasdaq.Close)
head(nasdaq_returns)
##          nasdaq.Close
## Dec 1985           NA
## Jan 1986   0.03354877
## Feb 1986   0.07057776
## Mar 1986   0.04228098
## Apr 1986   0.02268481
## May 1986   0.04436326
nasdaq_returns <- nasdaq_returns[-1, ]
colnames(nasdaq_returns) <- "nasdaq"
head(nasdaq_returns)
##              nasdaq
## Jan 1986 0.03354877
## Feb 1986 0.07057776
## Mar 1986 0.04228098
## Apr 1986 0.02268481
## May 1986 0.04436326
## Jun 1986 0.01324335

# Import rf (2.3 Excess returns and the portfolio’s Sharpe ratio)
rf <- read.csv("/resources/rstudio/finModeling_datacamp/data/Portfolio Theory/rf.csv")

rf$Date <- as.Date(rf$Date, format("%m/%d/%Y"))
rf <- rf[order(rf$Date), ]
rf <- as.xts(rf[, 2], order.by = rf$Date)
head(rf)
##              [,1]
## 1986-01-31 0.0053
## 1986-02-28 0.0060
## 1986-03-31 0.0052
## 1986-04-30 0.0049
## 1986-05-31 0.0052
## 1986-06-30 0.0052

# Align dates of nasdaq_returns
merged <- merge(rf, nasdaq_returns)
head(merged)
##                rf     nasdaq
## 1986-01-01     NA 0.03354877
## 1986-01-31 0.0053         NA
## 1986-02-01     NA 0.07057776
## 1986-02-28 0.0060         NA
## 1986-03-01     NA 0.04228098
## 1986-03-31 0.0052         NA
merged <- na.locf(merged)
head(merged)
##                rf     nasdaq
## 1986-01-01     NA 0.03354877
## 1986-01-31 0.0053 0.03354877
## 1986-02-01 0.0053 0.07057776
## 1986-02-28 0.0060 0.07057776
## 1986-03-01 0.0060 0.04228098
## 1986-03-31 0.0052 0.04228098
merged <- merged[index(rf)]
head(merged)
##                rf     nasdaq
## 1986-01-31 0.0053 0.03354877
## 1986-02-28 0.0060 0.07057776
## 1986-03-31 0.0052 0.04228098
## 1986-04-30 0.0049 0.02268481
## 1986-05-31 0.0052 0.04436326
## 1986-06-30 0.0052 0.01324335
nasdaq_returns <- merged[, 2]
head(nasdaq_returns)
##                nasdaq
## 1986-01-31 0.03354877
## 1986-02-28 0.07057776
## 1986-03-31 0.04228098
## 1986-04-30 0.02268481
## 1986-05-31 0.04436326
## 1986-06-30 0.01324335

# Compute the annualized risk free rate
annualized_rf <- (1 + rf)^12 - 1

# Plot the annualized risk free rate
plot.zoo(annualized_rf)


# Compute the series of excess portfolio returns
# Adjust date for nasdaq_returns. This code below doesn't work b/c two objects date don't match
nasdaq_excess <- nasdaq_returns - rf

head(nasdaq_returns)
##                nasdaq
## 1986-01-31 0.03354877
## 1986-02-28 0.07057776
## 1986-03-31 0.04228098
## 1986-04-30 0.02268481
## 1986-05-31 0.04436326
## 1986-06-30 0.01324335
head(rf)
##              [,1]
## 1986-01-31 0.0053
## 1986-02-28 0.0060
## 1986-03-31 0.0052
## 1986-04-30 0.0049
## 1986-05-31 0.0052
## 1986-06-30 0.0052

# Compare the mean
mean(nasdaq_returns)
## [1] 0.009794148
mean(nasdaq_excess)
## [1] 0.007056104

# Compute the Sharpe ratio
nasdaq_sharpe <- mean(nasdaq_excess) / sd(nasdaq_returns)
nasdaq_sharpe
## [1] 0.1107234