#S&P500 + 30 stocks. ^GSPC,AMZN,BA,COST,CVX, DIS,ELY,FL,GILD,GS, HD,JCP,LMT,M,MMM, PG,S,SBUX,SHOO,SMRT, TM,YUM,K,C,XOM, IBM,F,CAT,MCD,COKE,AAPL #Read your csv file: a <- read.csv("stockData-4.csv", sep=",", header=TRUE) #Convert adjusted close prices into returns: r <- (a[-1,3:ncol(a)]-a[-nrow(a),3:ncol(a)])/a[-nrow(a),3:ncol(a)] #Compute mean vector: means <- colMeans(r[-1]) #Without ^GSPC #Compute variance covariance matrix: covmat <- cov(r[-1]) #Without ^GSPC #Compute correlation matrix: cormat <- cor(r[-1]) #Without ^GSPC #Compute the vector of variances: variances <- diag(covmat) #Compute the vector of standard deviations: stdev <- diag(covmat)^.5 #Let's find the composition of the minimum risk portfolio for two stocks: # Let's work with two stocks: C and AAPL. # Find the composition of the minimum risk portfolio: x_c <- (variances[30] - covmat[23,30]) / (variances[23] + variances[30] - 2*covmat[23,30]) x_aapl <- 1-x_c # Find the mean and sd of the minimum risk portfolio: mean_min <- x_c* means[23] + x_aapl*means[30] var_min <- x_c^2*variances[23] + x_aapl^2*variances[30] + 2*x_c*x_aapl*covmat[23,30] sd_min <- var_min^0.5 # Construct the portfolio possibilities curve and identify the efficient frontier: wa <- seq(0,1,.01) wb <- 1-wa mean_p <- wa*means[23] + wb*means[30] var_p <- wa^2*variances[23] + wb^2*variances[30] + 2*wa*wb*covmat[23,30] sd_p <- var_p^0.5 plot(sd_p,mean_p, type="l", xlab="Portfolio standard deviation (risk)", ylab="Portfolio expected return") points(sd_min, mean_min, pch=19, col="green") # Identify the efficient frontier: xx <- cbind(sd_p,mean_p) xxx <- xx[which(xx[,2]>mean_min),] points(xxx, type="l", col="blue", lwd=3) #================================================= #Let's use three stocks: weights <- read.table("http://www.stat.ucla.edu/~nchristo/Fiatlux/abc.txt", header=TRUE) #Mean: rp3 <- weights$a*means[23] + weights$b*means[28]+ weights$c*means[30] var3 <- weights$a^2*variances[23] + weights$b^2*variances[28] + weights$c^2*variances[30] + 2*weights$a*weights$b*covmat[23,28] + 2*weights$a*weights$c*covmat[23,30] + 2*weights$b*weights$c*covmat[28,30] sd3 <- sqrt(var3) plot(sd3,rp3, xlim=c(0,0.3),xlab="Portfolio standard deviation (risk)", ylab="Portfolio expected return") #Now we want to add the minimum risk portfolio on the previous plot: rr <- r[,-1] rrr <- rr[, c(23,28,30)] covmat3 <- cov(rrr) means3 <- colMeans(rrr) #How do we find the composition of the minimum risk portfolio using 3 stocks. ones <- as.matrix(rep(1,3)) x <- solve(covmat3) %*% ones / as.numeric(t(ones) %*% solve(covmat3) %*% ones) #We compute the expected return and standard deviation of the minimum risk portfolio: rp3min <- x[1]*means[23] + x[2]*means[28]+ x[3]*means[30] var3min <- x[1]^2*variances[23] + x[2]^2*variances[28] + x[3]^2*variances[30] + 2*x[1]*x[2]*covmat[23,28] + 2*x[1]*x[3]*covmat[23,30] + 2*x[2]*x[3]*covmat[28,30] sd3min <- sqrt(var3min) #Add the point: points(sd3min, rp3min, cex=1, pch=19, col="green") #How do we find the composition of the minimum risk portfolio using n stocks. #Need: Inverse of a matrix. A^-1 is the inverse of a matrix if A*A^-1 = I (identity matrix). #Inverse of covmat: invcovmat <- solve(covmat) #Define: ones <- as.matrix(rep(1,30)) x <- solve(covmat) %*% ones / as.numeric(t(ones) %*% solve(covmat) %*% ones) #Variance: var30 <- t(x) %*% covmat %*% x sd30 <- var30^.5 #Mean: mean30 <- t(x) %*% means points(sd30,mean30)