Subperiod performance analysis and the function window
In the previous exercise, you computed the performance measure on each possible sample of a fixed size by rolling through time. Often investors are interested in the performance of a specific subwindow. You can create subsets of a time series in R using the function window(). The first argument is the return series that needs subsetting. The second argument is the starting date of the subset in the form "YYYY-MM-DD"
, and the third argument is the ending date in the same format.
In this exercise, you will be working with the daily S&P 500 returns, which is available as the object sp500_returns
.
This exercise is part of the course
Introduction to Portfolio Analysis in R
Exercise instructions
- Fill in the missing arguments to the object
sp500_2008
so that you have subsetted the entire year of 2008. - Define the object
sp500_2014
as the S&P 500 portfolio returns for 2014. - Some plotting settings are added in the R console. Leave these as they are!
- Plot histogram of returns in 2008 using the function chart.Histogram(). Set the argument
methods = c("add.density", "add.normal")
to visualize the non-parametric estimate of the density and the density under an assumed normal distribution. - Plot the same histogram, but for 2014.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Fill in window for 2008
sp500_2008 <- window(sp500_returns, start = "___", end = "___")
# Create window for 2014
sp500_2014 <-
# 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
# Plot histogram of 2014