.chapter19<-function(i=0){ " i Chapter 19: Portfolio Theory - ------------------------------------- 1 What is the keyword for the portfolio theory? 2 Total risk, market risk and firm-specific risk 3 Implied assumption: Do not put all your eggs in one basket. 4 Variance vs. covariance 5 portfolio returns 6 portfolio risk of a 2-stock portfolio 7 portfolio risk for an n-stock portfolio 8 Correlation (coefficient) 9 Why correlation is a better measure than covariance 10 From daily returns to weekly, monthly, or annual ones 11 Annualized vol 12 Function to estimate vol from date1 to date2 13 R data sets from PortRisk 14 Portfolio optimization 15 A minimum-variance portfolio 16 Futures contract 17 Portfolio insurance (concept) 18 Portfolio insurance (implementation) 19 YouTube 20 Links Example #1:>.c19 # see the above list Example #2:>.c19(1) # see the 1st explanation ";.zchapter19(i)} .c19<-.chapter19 .n19chapter<-20 .zchapter19<-function(i){ if(i==0){ print(.c19) }else{ .printEachQ(19,i,.n19chapter) } } .C19EXPLAIN1<-"What is the keyword for the portfolio theory? ///////////////////////////// The keyword for the portfolio theory is diversification. ///////////////////////////// " .C19EXPLAIN2<-"total risk, market risk and firm specific risk ////////////////////////////////////// Total risk has two components: --------------------------- total risk = market risk + firm-specific risk 1) we use standard deviation of returns to measure the total risk 2) we use beta to measure the market risk ////////////////////////////////////// " .C19EXPLAIN3<-"What is the implied assumption behind Do not put all your eggs into one basket? ////////////////////////////////////// The implied assumption that all your baskets would not drop simultaneously. ////////////////////////////////////// " .C19EXPLAIN4<-"variance vs. co-variance ////////////////////////////////////// Variance of returns ------------------- Assume that we have n returns: R1, R2, ... Rn R1+ R2 + ... + Rn mean= ------------------------ n (R1-mean)^2 + (R2-mean)^2 + ... + (Rn-mean)^2 var = ------------------------------------------- n-1 std = sqrt(var) covariance is defined as ------------------------ We have n pairs of returns for two stocks A and B A: R(A,1), R(A,2), ..... R(A, n) A: R(B,1), R(B,2), ..... R(B, n) meanA is the mean of n returns from A meanB is the mean of n returns from B sum[ (R(A,i) - meanA)* (R(B,i)-meanB)] cov(A, B) = --------------------------- n-1 When A and B are the same, then covariance will become variance, see below. sum[ (R(A,i) - meanA)^2 ] var(A) = --------------------------- n-1 Note: variance of one stock could be viewed as a special case for covariance. ////////////////////////////////////// " .C19EXPLAIN5<-"portfolo returns ////////////////////////////////////// Portfolio returns is the weighted individual stock returns R(port) = w1 * R1 + w2 * R2 + ... + wn * Rn where w1 is the weight of the first stock R1 is the return of the first stock n is the number of stocks When n is quite small, we could just type However, when n is bigger such as 50, it is a good idea to apply the Excel mmult() function ////////////////////////////////////// " .C19EXPLAIN6<-"portfolio risk of a 2-stcok portfolio ////////////////////////////////////// Variance of a 2-stock portfolio is given below. var = w1^2*var1 + w2^2*var2 + 2*w1*w2 *cov(1,2) Var is the variance of this 2-stock portfolio w1 (w2) is the weight of the first (second) stock var1 (var2) is the variance of the first (second) stock cov(1,2) is the covariance between stocks 1 and 2. std=squrt(var) The risk of the portfolio is the square root of its variance. Since w1+w2 =1, cov(1,2) = rho(1,2) *std1 * std2 we have var= w1^2*std1^2 + (1-w1)^2*std2^2 + 2*w1*(1-w1)*rho(1,2)*std1*std2 R code ------ vol2stocks<-function(sigma1,sigma2,rho,weight_1){ x1<-weight_1 x2<-1-x1 var<-x1^2*sigma1^2 +x2^2*sigma2^2+2*x1*x2*rho*sigma1*sigma2 return(sqrt(var)) } ////////////////////////////////////// " .C19EXPLAIN7<-"portfolio risk of an n-stcok portfolio ////////////////////////////////////// Var = Sum sum [ wi*wj*cov(i,j) ] (1) i=1 j=1 ->n -> n where Sum is the summation for the first summation, i starts from 1, end at n For the second summation, j starts from 1, end as n wi is the weight of the ith stock cov(i,j) is the covariance between stocks i and j Variance of a 2-stock portfolio is a special case of the above formula for an n-stock portfolio # R code -------- portfolio_vol<-function(ret_matrix,weight){ m<-ncol(ret_matrix) m2<-length(weight) if(m!=m2) stop(' dimensions are not matched!!!') COV<-cov(ret_matrix) sum<-0 for(i in 1:m) for(j in 1:m) sum<-sum + weight[i]*weight[j]*COV[i,j] return(sqrt(sum)) } ////////////////////////////////////// " .C19EXPLAIN8<-"Correlation (correlation coefficient) ////////////////////////////////////// Correlation is defined below. cov(A,B) corr(A,B) = --------------- std_A * std_B where corr(A,B) is the correlation between stocks A and B note we could use rho(A,B) for corr(A,B) cov(A,B) is the covariance between stocks A and B std_A is the standard deviation of stock A std_B is the standard deviation of stock B ////////////////////////////////////// " .C19EXPLAIN9<-"Why correlation is a bettern measure than covariance? ////////////////////////////////////// Let's use an example. cov(A,B) =0.45 cov(A,C) =1.2 We could tell that both B and C are positively correlated with A. However, we could not tell which one is more closely correlated with A. On the other hand, if we know that corr(A,B) =0.15 corr(A,C) =0.22 we could say that C is more closely correlated with A than B. ////////////////////////////////////// " .C19EXPLAIN10<-"from daily returns to weekly, monthly or annual ones ////////////////////////////////////// Let us convert WMT daily returns to monthly ones as an example. Step 1: collect daily data >x=getDailyPrice(\"wmt\") >saveYan(x,\"c:/temp/wmt.csv\") [1] \"Your saved file is ==>c:/temp/wmt.csv\" Step 2: Retrieving the data set into Excel Step 3: estimate daily log return by applying =ln(Pt/pt-1) where pt is the price at day t pt-1 is the price at day t-1 Step 3: Generate a YYYYMM column by applying the following formula =year(date)*100 + month(date) where date is the date cell Step 4: Click \"Insert\" on the menu bar, then \"PivotTable\" chose YYYYMM as Row Labels LOG ret as Values Step 5: for Values, choose sum [summation of all daily log return is the log monthly return] Step 6: we could convert log return into percentage return R(%) = exp(logRet)-1 ////////////////////////////////////// " .C19EXPLAIN11<-"Annualized volatility ////////////////////////////////////// From monthly data ---------------- var(annual) = 12 *var(monthly) std(annual) = sqrt(12)*std(monthly) From daily data ---------------- var(annual) = 252 *var(daily) std(annual) = sqrt(252)*std(daily) n-day vol ---------------- var(n-day) = n *var(daily) std(n-day) = sqrt(n)*std(daily) ////////////////////////////////////// " .C19EXPLAIN12<-"Function to estimte vol from date1 to date2 ////////////////////////////////////// x<-read.csv('http://datayyy.com/data_csv/ibmDaily.csv') vol_stock<-function(x,date1,date2){ n<-nrow(x) ret<-x[1:(n-1),6]/x[2:n,6] -1 d<-data.frame(as.Date(x[1:(n-1),1]),ret) colnames(d)<-c('date','ret') vol_daily<- sd(subset(d[,2],d[,1]>=date1 & d[,1]<=date2)) return(vol_daily*sqrt(252)) } date1<-as.Date('1990-02-03') date2<-as.Date('1999-12-31') vol_stock(x,date1,date2) [1] 0.3060258 > ////////////////////////////////////// " .C19EXPLAIN13<-"R data sets from PortRisk ////////////////////////////////////// library(PortRisk) data(SnP500List) dim(SnP500List) [1] 500 1 data(SnP500Returns) dim(SnP500Returns) [1] 252 500 ////////////////////////////////////// " .C19EXPLAIN14<-"Portfolio optimization ////////////////////////////////////// require(fPortfolio) require(tseries) d<-SMALLCAP.RET[,c(\"BKE\",\"GG\",\"GYMB\",\"KRON\")] k<-portfolio.optim(d) print(k[1]) print(sum(unlist(k[1]))) # another example library(fPortfolio) begdate<-as.Date('1978-01-01') enddate<-as.Date('2009-12-01') stock1<-.monthlyRetTickerAsRet('IBM') stock2<-.monthlyRetTickerAsRet('WMT') sp500 <-.monthlyRetTickerAsRet('^GSPC') d<-merge(stock1,stock2) d2<-subset(d,d[,1]>=begdate & d[,1]<=enddate) d3<-merge(d2,sp500) data<-d3[,2:4] data<-as.matrix(d3[,2:4]) k<-portfolio.optim(data) print(k[1]) ////////////////////////////////////// " .C19EXPLAIN15<-"A minimum-variance portfolio ////////////////////////////////////// library(fPortfolio) d<-SMALLCAP.RET d2<-d[, c(\"BKE\",\"GG\",\"GYMB\",\"KRON\")] f<-portfolioFrontier(d2) frontierPlot(f,pch=19,xlim=c(0,0.25), ylim=c(0,0.035)) grid() abline(h = 0, col = \"grey\") abline(v = 0, col = \"grey\") minvariancePoints(f,pch=19,col=\"red\") ////////////////////////////////////// " .C19EXPLAIN44<-"Efficient frontier ////////////////////////////////////// library(fPortfolio) begdate<-as.Date('1978-01-01') enddate<-as.Date('2009-12-01') stock1<-.monthlyRetTickerAsRet('IBM') stock2<-.monthlyRetTickerAsRet('WMT') sp500 <-.monthlyRetTickerAsRet('^GSPC') d<-merge(stock1,stock2) d2<-subset(d,d[,1]>=begdate & d[,1]<=enddate) d3<-merge(d2,sp500) data<-d3[,2:4] data<-as.matrix(d3[,2:4]) factors<-d3[,3] attr(data,'factors') <- factors tailoredFrontierPlot(portfolioFrontier(data))#Long-only Markow M ////////////////////////////////////// " .C19EXPLAIN16<-"What is a futures contract? ////////////////////////////////////// What is a futures contract? -------------------------- A futures contract is a legal agreement to buy or sell a particular commodity or asset at a predetermined price at a specified time in the future. Futures contracts are standardized for quality and quantity to facilitate trading on a futures exchange. The buyer of a futures contract is taking on the obligation to buy the underlying asset when the futures contract expires. The seller of the futures contract is taking on the obligation to provide the underlying asset at the expiration date. https://www.investopedia.com/terms/f/futurescontract.asp What is a S&P500 futures contract? ------------------------------ S&P 500 futures contracts give buyers the right to a basket of the stocks in the S&P 500 on expiration date. Priced at 250 times the index, they're used mostly by institutional investors. http://www.cmegroup.com/trading/equity-index/us-index/sandp-500_contract_specifications.html ////////////////////////////////////// " .C19EXPLAIN17<-"Portfolio Insurance (concept) ////////////////////////////////////// From CAPM (Capital Asset Pricing Model), R(portfolio)= alpha + beta (Rmkt-Rf) We know that most portfolio has a positive beta. This means that if we expect the market would fall, our portfolio value would fall as well. How to protect our portfolio? 1) sell our portfolio 2) reduce our beta 3) others We could short the S&P500 futures Value of our portfolio n = (beta* - beta) * ------------------------- value of one S&P500 futures contract where n is the number of the S&P500 futures contract n >0 we long n <0 we short the ////////////////////////////////////// " .C19EXPLAIN18<-"Portfolio Insurance (implementation) ////////////////////////////////////// Portfolio insurance is a method of hedging a portfolio of stocks against the market risk by short selling stock index futures. This hedging technique is frequently used by institutional investors when the market direction is uncertain or volatile. Assume that we manage a portfolio with a current valued of $5 million today. Its beta is 1.25. If we expect the whole market would be quite volatility in next 3 months. In other words, the market might go down significantly. What might be our choices at the moment? Alternative #1: Sell stocks right now and buy them back in a few months Alternative #2: Sell S&P500 index futures Alternative #1 is costly because of the transaction cost. Let see what happen when the market is down 1 point. Below is today s S&P500 level. If the market goes down one point, the long position (S&P500 futures contract) would lose $250, while the short position would gain $250. The size of one futures contract on S&P500 is: index level *250 If we what to hedge our $5mportfolio, we should short n futures contracts 5,000,000 n =(beta* - beta) * ------------------- (index level *250) Where, beta* is our target beta beta is the portfolio beta index level is the S&P500 index level Assume that we plan to reduce our beta from 1.25 to 0.5 Applying the above formula,https://finance.yahoo.com/ we should short 6 futures contracts. > (0.5-1.25)*5000000/(2736.27*250) [1] -5.481915 Assume, in three months, it is 2636.27, i.e., 100 points down. What is the loss of your portfolio? What is the gain is you short one future contract of S&P500 future? ////////////////////////////////////// " .C19EXPLAIN19<-"YouTube ////////////////////////////////////// Markowitz, InPursuit of the Perfect Portfolio: Harry M. Markowitz (34m50s) https://www.youtube.com/watch?v=wdeoIPCFtDU Modern Portfolio Theory Explained, Part 1 (10m28s) https://www.youtube.com/watch?v=ML48st3rD04 Portfolio Theory: Tutorial 1 (9m39s) https://www.youtube.com/watch?v=lPKtI90f_sE Risk and Return II & Portfolio Theory I (1h18m36s) https://www.youtube.com/watch?v=tL7Lcl90Sc0&t=27s ////////////////////////////////////// " .C19EXPLAIN20<-"Links ////////////////////////////////////// An Excel data set http://canisius.edu/~yany/excel/stock11monthly.xlsx Definition http://financial-dictionary.thefreedictionary.com/portfolio+theory http://www.yourdictionary.com/portfolio-theory Modern Portfolio Theory (MPT) http://www.investopedia.com/terms/m/modernportfoliotheory.asp http://economistatlarge.com/portfolio-theory/introduction-to-portfolio-theory https://canisius.edu/~yany/markowitz.shtml Harry Markowitz https://en.wikipedia.org/wiki/Harry_Markowitz Chapter 1: http://economistatlarge.com/portfolio-theory/introduction-to-portfolio-theory Marling, Hannes and Sara Emanuelsson,2012,The Markowitz Portfolio Theory http://www.math.chalmers.se/~rootzen/finrisk/gr1_HannesMarling_SaraEmanuelsson_MPT.pdf ////////////////////////////////////// "