Get startedGet started for free

Split-sample evaluation

In chapter 2, you used the function window() to subset your returns for graphical purposes. In this exercise, you will use the window() to create two samples: an estimation sample and an evaluation sample. This exercise will illustrate how portfolio weights can differ when changing the estimation window.

To remind you, the function window() has argument of x, start, and end. Where start and end are in the format "YYYY-MM-DD".

The object returns is loaded in your workspace.

This exercise is part of the course

Introduction to Portfolio Analysis in R

View Course

Exercise instructions

  • Create the sample returns_estim by subsetting returns, where the sample begins on January 1st, 1991, and ends on December 31st, 2003.
  • Create the sample returns_eval by subsetting returns, where the sample begins on the first day of 2004 and ends on the last day of 2015.
  • Create a vector of maximum weights equal to 10%, with a length as the number of columns there are in returns called max_weights.
  • Create a portfolio with the estimation sample called pf_estim, where the maximum weight (reshigh) is set to max_weights.
  • Create a portfolio with the evaluation sample called pf_eval, where the maximum weight (reshigh) is set to max_weights.
  • Create a scatter plot of the evaluation portfolio weights versus the estimation portfolio weights (note that you can use $pw). If portfolio weights are identical, they should be on the 45-degree line.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create returns_estim 
returns_estim <- window(___, start = "YYYY-MM-DD", end = "YYYY-MM-DD")

# Create returns_eval


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

# Create portfolio with estimation sample 
pf_estim <- portfolio.optim(___, reshigh = ___)

# Create portfolio with evaluation sample


# Create a scatter plot with evaluation portfolio weights on the vertical axis
plot(___, ___)
abline(a = 0, b = 1, lty = 3)
Edit and Run Code