[Uwgarp-commits] r115 - pkg/GARPFRM/vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Mar 9 02:52:32 CET 2014


Author: rossbennett34
Date: 2014-03-09 02:52:31 +0100 (Sun, 09 Mar 2014)
New Revision: 115

Added:
   pkg/GARPFRM/vignettes/DelineatingEfficientPortfolios.Rnw
   pkg/GARPFRM/vignettes/DelineatingEfficientPortfolios.pdf
Log:
Adding Delineating Efficient Portfolios vignette

Added: pkg/GARPFRM/vignettes/DelineatingEfficientPortfolios.Rnw
===================================================================
--- pkg/GARPFRM/vignettes/DelineatingEfficientPortfolios.Rnw	                        (rev 0)
+++ pkg/GARPFRM/vignettes/DelineatingEfficientPortfolios.Rnw	2014-03-09 01:52:31 UTC (rev 115)
@@ -0,0 +1,426 @@
+\documentclass{article}
+
+\usepackage{amsmath}
+\usepackage{Rd}
+
+\begin{document}
+
+<<echo=FALSE>>=
+opts_chunk$set(warning=FALSE, fig.width=5, fig.height=5)
+@
+
+
+\title{Delineating Efficient Portfolios}
+\author{Ross Bennett}
+
+\maketitle
+
+\begin{abstract}
+The purpose of this vignette is to demonstrate the efficient frontier of portfolios as outlined in Chapter 3 of Foundations of Risk Management.
+\end{abstract}
+
+\tableofcontents
+
+\section{Portfolio of Two Risky Assets}
+First, we consider a portfolio of two risky assets. The expected return for the portfolio of two assets is given by:
+
+\begin{equation}
+\bar{R}_P = X_A \bar{R}_A + X_B \bar{R_B}
+\end{equation}
+
+where:
+\begin{description}
+  \item[$X_A$] is the fraction of the portfolio held in asset A
+  \item[$X_B$] is the fraction of the portfolio held in asset B
+  \item[$R_P$] is the expected return of the portfolio
+  \item[$R_A$] is the expected return of asset A
+  \item[$R_B$] is the expected return of asset B
+\end{description}
+
+We can impose the full investment constraint such that the fraction of the portfolio invested in asset A and the the fraction of the portfolio invested in asset B sum to 1, $X_A + X_B = 1$.
+
+With the full investment constraint, equation (1) can be written as
+
+\begin{equation}
+\bar{R}_P = X_A \bar{R}_A + (1 - X_A) \bar{R_B}
+\end{equation}
+
+Imposing the full investment constraint, the standard deviation of a portfolio with two assets is given by
+\begin{equation}
+\sigma_P = (X_A^2 \sigma_A^2 + (1 - X_A)^2 \sigma_B^2 + 2 X_A (1 - X_A) \sigma_{AB})^{1/2}
+\end{equation}
+
+where:
+\begin{description}
+  \item[$\sigma_P$] is the standard deviation of the portfolio
+  \item[$\sigma_A^2$] is the variance of the returns of asset A
+  \item[$\sigma_B^2$] is the variance of the returns of asset A
+  \item[$\sigma_{AB}$] is the covariance between the returns on asset A and asset B
+\end{description}
+
+Recall that $\sigma_{AB} = \rho_{AB} \sigma_A \sigma_B$. Substituting the equation for the covariance into equation (3) results in
+
+\begin{equation}
+\sigma_P = (X_A^2 \sigma_A^2 + (1 - X_A)^2 \sigma_B^2 + 2 X_A (1 - X_A) \rho_{AB} \sigma_A \sigma_B)^{1/2}
+\end{equation}
+
+This equation is useful in that it allows one to explore the effect of correlation on the portfolio returns and standard deviation.
+
+
+For the following examples, unless noted otherwise, consider the following two stocks with characteristics given in the following table.
+
+\begin{table}[h]
+%\caption{default}
+\begin{center}
+\begin{tabular}{|c|c|c|}
+\hline
+ & Expected Return & Standard Deviation\\
+\hline
+ Colonel Motors (C) & 14\% & 6\%\\
+ Separated Edison (S) & 8\% & 3\%\\
+ \hline
+\end{tabular}
+\end{center}
+%\label{default}
+\end{table}%
+
+\subsection{Case 1 - Perfect Positive Correlation ($\rho = +1$)}
+
+Here we calculate the portfolio expected return and standard deviation of the portfolio assuming the two assets are perfectly positively correlated.
+<<tidy=FALSE>>=
+suppressPackageStartupMessages(library(GARPFRM))
+
+# Colonel Motors expected return
+R_C <- 0.14
+
+# Colonel Motors standard deviation
+sigma_C <- 0.06
+
+# Separated Edison expected return
+R_S <- 0.08
+
+# Separated Edison standard deviation
+sigma_S <- 0.03
+
+# Correlation coefficient
+rho <- 1
+
+# Create a vector of portfolio weights
+X_C <- seq(from=0, to=1, by=0.2)
+
+# Calculate the portfolio expected return
+R_P <- portReturnTwoAsset(R_C, R_S, X_C)
+
+# Calculate the portfolio standard deviation
+sigma_P <- portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho)
+
+# Combine the portfolio returns and standard deviations in a data.frame object.
+# Note that this is the transpose t() of the data.frame object to match the
+# layout of the tables in the FRM book.
+df <- t(data.frame(R_P=R_P, sigma_P=sigma_P))
+colnames(df) <- X_C
+df
+@
+
+Now we can plot the portfolio return and standard deviation to understand the risk and return of the portfolio. Figure 1 shows that risk increases linearly with return and there is no gain by diversifying compared to purchasing the individual assets.
+
+<<tidy=FALSE, fig.cap="Relationship between expected return and standard deviation">>=
+# Create and plot the efficient frontier object
+ef <- efficientFrontierTwoAsset(R_C, R_S, sigma_C, sigma_S, rho)
+plot(ef)
+@
+
+\subsection{Case 2 - Perfect Negative Correlation ($\rho = -1$)}
+Here we calculate the portfolio expected return and standard deviation of the portfolio assuming the two assets are perfectly negatively correlated. Note that we just need to change the vaue of the correlation coefficient and recalculate the portfolio expected return and standard deviation.
+<<>>=
+# Correlation coefficient
+rho <- -1
+
+# Calculate the portfolio expected return
+R_P <- portReturnTwoAsset(R_C, R_S, X_C)
+
+# Calculate the portfolio standard deviation
+sigma_P <- portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho)
+
+# Combine the portfolio returns and standard deviations in a data.frame object.
+# Note that this is the transpose t() of the data.frame object to match the
+# layout of the tables in the FRM book.
+df <- t(data.frame(R_P=R_P, sigma_P=sigma_P))
+colnames(df) <- X_C
+df
+@
+
+Now we plot the portfolio return and standard deviation to understand the risk and return of the portfolio. Figure 2 shows an interesting result, ther is a combination of assets that results in a portfolio with zero risk. This demonstrates how diversification can reduce risk when assets do not have perfect positive correlation.
+<<tidy=FALSE, fig.cap="Relationship between expected return and standard deviation">>=
+# Create and plot the efficient frontier object
+ef <- efficientFrontierTwoAsset(R_C, R_S, sigma_C, sigma_S, rho)
+plot(ef)
+@
+
+\subsection{Case 3 - No Relationship between Returns on the Assets ($\rho = 0$)}
+Here we calculate the portfolio expected return and standard deviation of the portfolio assuming the two assets are uncorrelated. Note that we just need to change the vaue of the correlation coefficient and recalculate the portfolio expected return and standard deviation.
+<<>>=
+# Correlation coefficient
+rho <- 0
+
+# Calculate the portfolio expected return
+R_P <- portReturnTwoAsset(R_C, R_S, X_C)
+
+# Calculate the portfolio standard deviation
+sigma_P <- portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho)
+
+# Combine the portfolio returns and standard deviations in a data.frame object.
+# Note that this is the transpose t() of the data.frame object to match the
+# layout of the tables in the FRM book.
+df <- t(data.frame(R_P=R_P, sigma_P=sigma_P))
+colnames(df) <- X_C
+df
+@
+
+Now we plot the portfolio return and standard deviation to understand the risk and return of the portfolio.
+<<tidy=FALSE, fig.cap="Relationship between expected return and standard deviation">>=
+# Create and plot the efficient frontier object
+ef <- efficientFrontierTwoAsset(R_C, R_S, sigma_C, sigma_S, rho)
+plot(ef)
+
+@
+
+Figure 4 also shows that there is a point along the curve that is a portfolio with minimum risk. The minimum risk portfolio can be solved for using the equation for portfolio standard deviation. By taking the first derivative of the equation with respect to $X_C$, we solve for the value of $X_C$ that minimizes portfolio risk.
+
+\begin{equation}
+\sigma_P = (X_C^2 \sigma_C^2 + (1 - X_C)^2 \sigma_S^2 + 2 X_C (1 - X_C) \rho_{CS} \sigma_C \sigma_S)^{1/2}
+\end{equation}
+
+TODO: The derivative of this equation is
+
+Set the derivative equal to zero and solve for $X_C$. 
+\begin{equation}
+X_C = \frac{\sigma_S^2 - \sigma_C \sigma_S \rho_{CS}}{\sigma_C^2 + \sigma_S^2 - 2 \sigma_C \sigma_S \rho_{CS}}
+\end{equation}
+
+We can easily write an \code{R} function to solve for the weights of the minimum risk portfolio.
+<<>>=
+minRisk <- function(R_A, R_B, sigma_A, sigma_B, rho){
+  top_term <- sigma_B^2 - sigma_A * sigma_B * rho
+  bottom_term <- sigma_A^2 + sigma_B^2 - 2 * sigma_A * sigma_B * rho
+  # x is the fraction of asset A 
+  x <- top_term / bottom_term
+  # calculate the weights for the minimum risk portfolio
+  weights <- c(x, 1-x)
+  names(weights) <- c("Asset_A", "Asset_B")
+  # calculate the portfolio return and standard deviation of the minimum 
+  # risk portfolio
+  port_ret <- portReturnTwoAsset(R_A, R_B, x)
+  port_sd <- portSDTwoAsset(R_A, R_B, x, sigma_A, sigma_B, rho)
+  return(list(weights=weights, 
+              portfolio_return=port_ret, 
+              portfolio_sd=port_sd))
+}
+@
+
+Now we can solve for the fraction of the portfolio to invest in each risky asset that minimizes risk.
+<<>>=
+minRisk(R_C, R_S, sigma_C, sigma_S, rho)
+@
+
+
+\subsection{Case 4 - Intermediate Risk ($\rho = 0.5$)}
+Here we calculate the portfolio expected return and standard deviation of the portfolio assuming the two assets have a correlation coefficient of 0.5. Note that we just need to change the vaue of the correlation coefficient and recalculate the portfolio expected return and standard deviation.
+<<>>=
+# Correlation coefficient
+rho <- 0.5
+
+# Calculate the portfolio expected return
+R_P <- portReturnTwoAsset(R_C, R_S, X_C)
+
+# Calculate the portfolio standard deviation
+sigma_P <- portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho)
+
+# Combine the portfolio returns and standard deviations in a data.frame object.
+# Note that this is the transpose t() of the data.frame object to match the
+# layout of the tables in the FRM book.
+df <- t(data.frame(R_P=R_P, sigma_P=sigma_P))
+colnames(df) <- X_C
+df
+@
+
+Now we plot the portfolio return and standard deviation to understand the risk and return of the portfolio. The plot shows an interesting result, ther is a combination of assets that results in a portfolio with zero risk. This demonstrates how diversification can reduce risk when assets do not have perfect positive correlation.
+<<tidy=FALSE, fig.cap="Relationship between expected return and standard deviation">>=
+# Create and plot the efficient frontier object
+ef <- efficientFrontierTwoAsset(R_C, R_S, sigma_C, sigma_S, rho)
+plot(ef)
+@
+
+Now we can solve for the fraction of the portfolio to invest in each risky asset that minimizes risk.
+<<>>=
+minRisk(R_C, R_S, sigma_C, sigma_S, rho)
+@
+
+Figure 5 shows the relationship between portfolio expected return and standard deviation for the four cases of correlation coefficients. This plot offers insight into the shape of the efficient frontier along which all combinations of portfolio weights in the two risky assets must lie as the correlation coefficient is varied.
+<<echo=FALSE, tidy=FALSE, fig.cap="Relationship between expected return and standard deviation">>=
+X_C <- seq(from=0, to=1, by=0.01)
+R_P <- portReturnTwoAsset(R_C, R_S, X_C)
+# rho = 1
+plot(portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho=1), R_P, 
+     main="Portfolio Return and Standard Deviation", 
+     type="l", xlim=c(0, 0.08), ylim=c(0, 0.18),
+     xlab="Portfolio Standard Deviation",
+     ylab="Portfolio Expected Return", cex.lab=0.8)
+# rho = -1
+lines(portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho=-1), R_P, col="blue")
+# rho = 0
+lines(portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho=0), R_P, col="red")
+# rho = 0.5
+lines(portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho=0.5), R_P, col="green")
+legend("topleft", legend=c(legend=expression(paste(rho, " = 1")),
+                           legend=expression(paste(rho, " = -1")),
+                           legend=expression(paste(rho, " = 0")),
+                           legend=expression(paste(rho, " = 0.5"))), 
+       col=c("black", "blue", "red", "green"), 
+       lty=rep(1, 4), bty="n")
+@
+
+\section{The Efficient Frontier with Short Sales Allowed}
+Here we consider the case where short selling is allowed.
+<<>>=
+# correlation coefficient
+rho <- 0.5
+
+# Fraction of the portfolio invested in Colonel Motors
+X_C <- seq(from=-1, to=2, by=0.2)
+
+# Calculate the portfolio expected return
+R_P <- portReturnTwoAsset(R_C, R_S, X_C)
+
+# Calculate the portfolio standard deviation
+sigma_P <- portSDTwoAsset(R_C, R_S, X_C, sigma_C, sigma_S, rho)
+
+# Combine the portfolio returns and standard deviations in a data.frame object.
+# Note that this is the transpose t() of the data.frame object to match the
+# layout of the tables in the FRM book.
+df <- t(data.frame(R_P=R_P, sigma_P=sigma_P))
+colnames(df) <- X_C
+print(df, digits=4)
+@
+
+Now we calculate and plot the efficient frontier with short sales allowed. For comparison, we also calculate and plot the efficient frontier for the long only case where short selling is not allowed
+<<tidy=FALSE, fig.cap="Relationship between expected return and standard deviation">>=
+# Create and plot the efficient frontier object
+ef_short <- efficientFrontierTwoAsset(R_C, R_S, sigma_C, sigma_S, rho, 
+                                      allowShorting=TRUE)
+ef_long <- efficientFrontierTwoAsset(R_C, R_S, sigma_C, sigma_S, rho)
+plot(ef_short)
+lines(ef_long$efficient_frontier[,2], ef_long$efficient_frontier[,1], 
+      lty=2, lwd=2, col="blue")
+@
+
+\section{Examples}
+This first example considers an allocation between equity and bonds.
+<<>>=
+# Estimated inputs for equity
+R_SP <- 0.125
+sigma_SP <- 0.149
+
+# Estimated inputs for bonds
+R_B <- 0.06
+sigma_B <- 0.048
+
+# Estimated correlation between equity and bonds
+rho <- 0.45
+@
+
+Here we calculate the allocation to equities and bonds to achieve the portfolio which minimizes risk where risk is defined as the standard deviation of the portfolio. We see that we should short sell approximately 5.1\% of equity.
+<<>>=
+# Calculate the allocation and values for the minimum variance portfolio
+minRisk(R_SP, R_B, sigma_SP, sigma_B, rho)
+@
+
+Now we create and plot the efficient frontier so we can better understand the portfolio expected return and standard deviation as the allocation as we vary the allocation between equity and bonds with short selling allowed. We also consider the riskless borrowing and lending rate as 5\%.
+<<tidy=FALSE>>=
+ef <- efficientFrontierTwoAsset(R_SP, R_B, sigma_SP, sigma_B, rho,
+                                rf=0.05, allowShorting=TRUE)
+plot(ef)
+@
+
+
+For the second example, we consider an allocation between a domestic portfolio represented by the S\&P index and an international portfolio represented by an average international fund. We proceed with the same analysis as the first example. We first solve for the portfolio that minimizes risk and the create and plot the efficient frontier.
+<<>>=
+# Estimated inputs for domestic portfolio
+R_SP <- 0.125
+sigma_SP <- 0.149
+
+# Estimated inputs for international portfolio
+R_int <- 0.105
+sigma_int <- 0.14
+
+# Estimated correlation between domestic and international portfolio
+rho <- 0.33
+@
+
+Here we calculate the allocation to domestic stocks and international stocks to achieve the portfolio which minimizes risk where risk is defined as the standard deviation of the portfolio. We see that we should have an allocation of approximately 45\% of the S\&P index and 55\% of the international fund for the minimum risk portfolio.
+<<>>=
+# Calculate the allocation and values for the minimum variance portfolio
+minRisk(R_SP, R_int, sigma_SP, sigma_int, rho)
+@
+
+Now we create and plot the efficient frontier so we can better understand the portfolio expected return and standard deviation as the allocation as we vary the allocation between the domestic and international portfolios. We also consider the riskless borrowing and lending rate as 5\%.
+<<tidy=FALSE>>=
+ef <- efficientFrontierTwoAsset(R_SP, R_int, sigma_SP, sigma_int, rho, rf=0.05)
+plot(ef)
+@
+
+\section{Extended Examples for Portfolios with Multiple Assets}
+The previous sections all considered the case of a portfolios with two assets. Those assets, in general, could be portfolios or individual securities. The code used in this section depends on the PortfolioAnalytics (TODO citation) package. TODO add description of PortfolioAnalytics.
+
+
+Load weekly returns data for a set of large cap, mid cap, and small cap stocks.
+<<>>=
+data(crsp_weekly)
+R_large <- largecap_weekly[,1:5]
+R_mid <- midcap_weekly[,1:5]
+R_small <- smallcap_weekly[,1:5]
+@
+
+Here we create and plot an efficient frontier of a selection of large cap stocks.
+<<>>=
+# Create and plot an efficient frontier of large cap stocks
+ef_large <- efficientFrontier(R_large)
+plot(ef_large, main="Large Cap Efficient Frontier", cexAssets=0.6)
+@
+
+Here we add box constraints to the large cap portfolio such that each asset must have a weight greater than 15\% and less than 55\%.
+<<tidy=FALSE>>=
+# Create an efficient frontier object with box constraints
+# Add box constraints such that each asset must have a weight greater than 15%
+# and less than 55%
+ef_large_box <- efficientFrontier(R_large, minBox=0.15, maxBox=0.55)
+plot(ef_large_box, main="Large Cap Efficient Frontier\nwith Box Constraints",
+     cexAssets=0.6)
+@
+
+Here we combine the asset returns for large cap, mid cap, and small cap stocks. Next we specify group constraints for three groups such that each market cap is in its own group. We then specify weight limits on the groups. Finally, we create an efficient frontier and plot it.
+
+<<tidy=FALSE>>=
+# Combine the large cap, mid cap, and small cap stocks
+R <- cbind(R_large, R_mid, R_small)
+
+# Specify the list of groups such that each market cap is in its own group
+groups <- list(c(1:5), c(6:10), c(11:15))
+
+# Specify the sum weights for each group:
+# largecap: no more than 60% and no less than 30%
+# midcap: no more than 50% and no less than 20%
+# smallcap: no more than 40% and no less than 15%
+groupMin <- c(0.3, 0.2, 0.15)
+groupMax <- c(0.6, 0.5, 0.4)
+
+# Create the efficient frontier with the group constraints
+efGroup <- efficientFrontier(R, groupList=groups, 
+                             groupMin=groupMin, 
+                             groupMax=groupMax)
+plot(efGroup)
+@
+
+In this vignette, we have demonstrated the behavior of efficient frontiers of two assets as the correlation varies under the full investment constraint with and without short selling allowed. We have also presented the more general case for generating portfolios along an efficient frontier for more than 2 assets. We also demonstrated functionality to generate portfolios along the efficient frontier with box constraints as well as group constraints.
+
+\end{document}

Added: pkg/GARPFRM/vignettes/DelineatingEfficientPortfolios.pdf
===================================================================
(Binary files differ)


Property changes on: pkg/GARPFRM/vignettes/DelineatingEfficientPortfolios.pdf
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream



More information about the Uwgarp-commits mailing list