#Load package: library(stockPortfolio) # select stocks #IBM: International Business Machines Copr. #WFC: Wells Fargo & Compnay #JPM: JPMorgan Chase & Company #LUV: Southwest Airlines Co. #Select stocks: ticker <- c('IBM', 'WFC', 'JPM', 'LUV') #Get stock data gr <- getReturns(ticker, start='2012-03-31', end='2015-08-31') #Find the weights of the minimum risk portfolio: w=sigma^-1 * ones / ones' sigma^-1 * ones: #Vector of ones: ones <- c(1,1,1,1) #Variance-covariance marix: v <- cov(gr$R) #Compute the weights: w <- solve(v) %*% ones / as.numeric(t(ones) %*% solve(v) %*% ones) #Mean vector of the 4 stocks: m <- colMeans(gr$R) #Standard deviation vector of the 4 stocks: q <- sqrt(diag(v)) #Compute expected return of the minimum risk portfolio: ex_min <- t(m) %*% w #Compute expected return of the minimum risk portfolio: sd_min <- sqrt(t(w) %*% v %*% w) #Plot the fours stocks: plot(q,m, ylim=c(-0.01, .045), xlim=c(0.025,0.08), xlab="Standard deviation", ylab="Expected return") #Add the minimum risk portfolio: points(sd_min, ex_min, pch=19)