load("dataset.RData") #MEAN RETURN AND RISK OF ASSETS #select the returns DJIA_ret <- DJIA[,-c(1,2)] #compute the mean of each stock DJIA_mu <- colMeans(DJIA_ret) #compute the standard deviation of each stock. "2" is used to select the column. "1" would compute the sd of the rows DJIA_sd <- apply(DJIA_ret, 2, sd) #we compute the covariance matrix of the returns of the individual stocks DJIA_cov <- cov(as.matrix(DJIA_ret)) #we plot mean vs. sd on a scatter plot plot(DJIA_sd, DJIA_mu, xlab = "Standard deviation", ylab = "Mean return", xlim=c(0.04,0.1), pch=19) #we now create an equally weighted portfolio with the 22 stocks #as it is equally weighted we just compute the mean return of each stock in each periods to get the returns of this portfolio EWP_ret <- rowMeans(DJIA_ret) #we compute the mean and sd DJIA_mean <- mean(EWP_ret) DJIA_sd <- sd(EWP_ret) #we now plot this portfolio on the previous plot points(DJIA_sd, DJIA_mean, col="red", pch=19) #COMPUTATION AND PLOTTING OF WEALTH DYNAMICS #custom function to compute the evolution of wealth Wealth <- function(Returns){ wealth <- c(1,Returns) wealth <- timeSeries::as.timeSeries(wealth) for(i in 2:length(wealth)){ if (wealth[i-1]>0){ wealth[i] = wealth[i-1]+wealth[i-1]*wealth[i] } else { wealth[i] = 0 } } wealth <- as.vector(wealth) return(wealth) } #we use this function to compute the wealth of the equally weighted portfolio #first we need the dates Date <- seq(as.Date("2000-01-01"),length=nrow(DJIA)+1,by="months")-1 #we compute the wealth EWP_wealth <- Wealth(EWP_ret) plot(EWP_wealth~Date,type="l", lwd=2, col="red", ylab="Wealth") #MEAN RETURN AND RISK OF A PORTFOLIO WITH GIVEN WEIGHTS #we now consider a portfolio with five assets: AXP, BA, CAT, CSCO, CVX library(dplyr) stocks_ret <- DJIA_ret %>% select(AXP,BA,CAT,CSCO,CVX) #while it is more convenient to use dplyr, we can also select the columns in other ways using base are stocks_ret <- DJIA_ret[,1:5] #we compute the mean return of the stocks of interest stocks_mu <- colMeans(stocks_ret) #we compute the covariance matrix of hese stocks stocks_cov <- cov(stocks_ret) #we use as set of weight: 0.2 for AXP, 0.1 for BA, 0.3 for CAT, 0.3 for CSCO and 0.1 for CVX p_w <- c(0.2,0.1,0.3,0.3,0.1) #using the means as expected returns, we not compute the expected return of the portfolio p_mu <- t(p_w) %*% stocks_mu #and we compute the variance of the portfolio p_var <- t(p_w) %*% stocks_cov %*% p_w #ESTIMATION WITH ROLLING AND EXPANDING WINDOW #up to now we used the entire dataset. now we see how to use rolling and expanding windows WIND <- 120 #window size (initial window size in the case of expanding window) #rolling window DJIA_mu_roll <- list() DJIA_cov_roll <- list() for (i in 1:(nrow(DJIA_ret)-WIND)) { DJIA_mu_roll[[i]] <- colMeans(DJIA_ret[i:(WIND+i-1),]) DJIA_cov_roll[[i]] <- cov(DJIA_ret[i:(WIND+i-1),]) } #expanding window DJIA_mu_exp <- list() DJIA_cov_exp <- list() for (i in 1:(nrow(DJIA_ret)-WIND)) { DJIA_mu_exp[[i]] <- colMeans(DJIA_ret[1:(WIND+i-1),]) DJIA_cov_exp[[i]] <- cov(DJIA_ret[1:(WIND+i-1),]) }