Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Microsoft Word - HW 2 - Etudes in R - Episode II - Return of the Efficient Frontier.docx RMI 660 Finance for Actuarial Science Homework 2: Études in R – Episode II: Return of the Efficient Frontier...

1 answer below »
Microsoft Word - HW 2 - Etudes in R - Episode II - Return of the Efficient Frontier.docx
RMI 660
Finance for Actuarial Science
Homework 2: Études in R – Episode II: Return of the Efficient Frontie
Building on our R exercise on the 2-asset portfolio problem and our previous Excel exercise, in this homework you will develop an R program to derive the
Efficient Frontier and utility-maximizing portfolios for a 4-asset investment universe. Along the way, this introduces you to the remaining concepts necessary for
the course project. For assistance, refer to the list of introductory YouTube videos that I uploaded onto Blackboard and my co
esponding remarks, Google for
examples of how specific commands work, and/or refer to the help of R itself. I have again tried to
eak down the overall problem into sufficiently small bites to
make them digestible. Again, test every step along the way, and make sure your program does what you it to do (e.g. by cross-checking selected results in Excel).
Make sure that your code runs without generating e
ors (easily identifiable by red output in the console). Also make sure that you don’t hard-code values unless
it is absolutely certain that the values cannot change.
This homework has 2 deadlines:
1) Submit the R file with your (preliminary) solution for review and comments by email ( XXXXXXXXXX) before Sunday, 03/29, 11:59pm
2) Upon incorporating all comments, submit your revised and finalized program by email before Wednesday, 04/01, 11:59pm
Step 0: Create a New Script
Create a new (empty) R script and save it under the name _HW2.R (e.g., mine would
e rogalla_HW2.R).
Step 1: Import Data
Using the read.csv() function (or any other appropriate non-manual(!) method), have your program read
the monthly stock price data provided in the accompanying file “stockprices.csv” into the variable
assetPrices, e.g. via assetPrices = read.csv(…). (NB: The csv file must either be in your working directory,
or you have to specify the path to the file. To see your cu
ent working directory, use the function
getwd(). To change your cu
ent working directory, use the function setwd().)
Using, for example, the dim() command, have your program determine the number of assets and the
number of stock price observations in the data and store those into the variables numberAssets and
numberPriceObservations.
Step 2: Asset Returns and Parameters
Calculate the (discrete, not continuously compounded) monthly asset returns and store them in the
variable assetReturns. Do this in 1 line of code by leveraging R’s ability to comfortably do arithmetic
operations on vectors/matrices. (Hint: If you haven’t done this already for the previous homework, figure
out how you can index
eference/access selected parts of a given matrix. Your assetReturns matrix
needs to be of size (numberPriceObservations-1 x numberAssets).)
Determine the mean returns and the return covariance matrix using the colMeans() and cov() functions
and store these in the variables mu_vec and cov_mat.
Lucas Zheng
Lucas Zheng
Lucas Zheng
Step 3: Prepare for Optimization
Go to the “Packages” tab in the (probably) bottom right pane of R Studio, click on “Install” and install the
“quadprog” package (install with dependencies from Repository (CRAN) to the default li
ary). Add the
command li
ary(quadprog) at the very top of your code.
In addition to that, define two variables at the very top of your code: one named numberPortfolios,
specifying the number of portfolios you want to base your efficient frontier on, the other named
indexAssetExcluded, holding a non-negative value (possibly zero) that represents the index number of
the asset that will be excluded when determining a restricted efficient frontier.
Review the provided “Optimization in R” presentation from our friends at the University of Freiburg with
particular focus on sections 1 (Introduction), 2 (Linear Programming) and 3 (Quadratic Programming).
Step 4: Determine the MVP
Create two matrices holding all zeros: one of size (numberPortfolios x numberAssets) named
portfolioWeights, the other one of size (numberPortfolios x 2) named efficientFrontier. Using the
colnames() command, assign the first (second) column of efficientFrontier the name Sigma (Mu), and
assign the columns of portfolioWeights the names of the co
esponding assets, which you can extract
from the matrices assetPrices or assetReturns using colnames().
Using the solve.QP() function from the quadprog li
ary, determine the portfolio weights for the
Minimum Variance Portfolio under short-selling constraints. To do so, you first have to figure out exactly
how to specify the required input parameters (i.e., Dmat, dvec, Amat, bvec, and meq) in order to include
all required restrictions. (Hint: The commands matrix(), cbind(),
ind(), and diag() might come in handy.)
Store the optimal portfolio weights in the first row of portfolioWeights, and the co
esponding values for
sigma and mu in the first row of efficientFrontier.
N.B.: In order to work with the optimization outputs, you have to assign the optimization output to an
output variable (e.g. like this: “out = solveQP(…)”). This output variable will be of datatype list, which
(unfortunately) doesn’t allow arithmetic operations on its values. Therefore, it is helpful to extract the
values from the list and store them in a vecto
matrix. This is done with the unlist() command. For
instance, the command out_vec = unlist(out[1], use.names = FALSE) extracts the first element from the
list out and stores it in a vecto
matrix named out_vec. See the help for solve.QP for a description of the
outputs generated.
Step 5: Determine the Efficient Frontier
Create a sequence musEfficientFrontier of length numberPortfolios, starting at the mu of the MVP and
ending at the mu of the maximum return asset less 1e-12 (i.e., XXXXXXXXXX).
Using a for loop, calculate for all portfolio mus in musEfficientFrontier the portfolio that minimizes the
portfolio variance conditional on having that particular portfolio mu. (Hint: This requires adequately
adjusting the input parameter vectors/matrices for solve.QP.) For each of these efficient portfolios, store
the weights, the portfolio sigma and mu in the co
esponding row in the portfolioWeights and
efficientFrontier matrices.
Step 6: Determine Utility-Maximizing Portfolios
Create a vector a_vec holding the values 0.01, 1, 10, 100, XXXXXXXXXXFurthermore, create two matrices
holding all zeros: one of size (“length of a_vec” x numberAssets) named portfolioWeightsUtilityMax, the
other one of size (“length of a_vec” x 2) named efficientFrontierUtilityMax. Using the colnames()
command, assign the first (second) column of efficientFrontierUtilityMax the name Sigma (Mu), and
assign the columns of portfolioWeightsUtilityMax the names of the co
esponding assets. (Suggestion:
For consistency and better readability/usability, move the definition of vector a_vec (but only that) to
the top of your program, where you also specify numberPortfolios and indexAssetExcluded).
Using a for loop, calculate for all risk aversion parameters in a_vec the portfolio that maximizes the
utility of an investor with preference function ?(?) =     ?(?) − ? ⋅ ?,(?). (Hint: This requires adequately
adjusting the input parameter vectors/matrices for solve.QP. Particularly tricky: You have to think about
how to adjust these vectors/matrices to adequately incorporate the ?-parameter. To this end, the
following hint: min?(?) = min ? ⋅ ?(?) , ???    0 < ? < ∞). For each of these portfolios, store the
weights, the portfolio sigma and mu in the co
esponding row in the portfolioWeightsUtilityMax and
efficientFrontierUtilityMax matrices.
Step 7 (Bonus Problem): Determine the Restricted Efficient Frontier
Determine the Efficient Frontier under the condition that (potentially) one of the assets is excluded from
the investment universe. The asset to be excluded shall be determined by the variable
indexAssetExcluded, such that no asset is excluded if indexAssetExcluded is set to 0, or that – for example
– MetLife is excluded if indexAssetExcluded is set to 1 (in our dataset, MetLife is the first asset). It must,
however, be ensured that no asset is excluded if the index is set to a value greater than the number of
assets in the dataset.
(Hints: This requires you to incorporate a flow control element that allows conditional code execution,
an if/else condition. A relatively straightforward approach to exclude an asset is to include into your
optimization a restriction that sets the weight for this asset to zero. So make sure to adequately adjust
your optimization parameters. Also, remember that you will have to determine a new MVP and a new
maximum return portfolio when the investment universe is restricted. Here, the following hint: When
eferencing a vector with a negative index, the operation will be executed with all vector components
except the one with that particular index (Compare, e.g., a_vec with a_vec[-2]).
Step 8: Cleanup for Shipment
In order to make it easier for me (and your future you) to read your program, spend some effort on
formatting and commenting the code (see the co
esponding section in the previous homework).
Answered Same Day Mar 29, 2021

Solution

Kshitij answered on Mar 30 2021
145 Votes
li
ary(quadprog)
li
ary(PerformanceAnalytics)
#Step 1: Import Data
assetPrices<-read.csv("stockprices-4l4urmnj.csv", row.names=1)
#Step 2: Asset Returns and Parameters
numberAssets <- dim(assetPrices)[1]
numberPriceObservations <- dim(assetPrices)[2]
#Step 3: Prepare for...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here