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 »
Please follow the file uploaded, and provide complete codes for each question. Thanks


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 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 corresponding remarks, Google for examples of how specific commands work, and/or refer to the help of R itself. I have again tried to break 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 errors (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 ([email protected]) 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 be 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 current working directory, use the function getwd(). To change your current 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/reference/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 library). Add the command library(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 corresponding assets, which you can extract from the matrices assetPrices or assetReturns using colnames(). Using the solve.QP() function from the quadprog library, 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(), rbind(), and diag() might come in handy.) Store the optimal portfolio weights in the first row of portfolioWeights, and the corresponding 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 vector/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 vector/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., 0.000000000001). 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 corresponding 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, 10000. Furthermore, 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 corresponding 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 corresponding 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 referencing 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 corresponding section in the previous homework). ∞).="" for="" each="" of="" these="" portfolios,="" store="" the="" weights,="" the="" portfolio="" sigma="" and="" mu="" in="" the="" corresponding="" 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="" referencing="" 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="" corresponding="" section="" in="" the="" previous="">
Answered Same DayMar 29, 2021

Answer To: Microsoft Word - HW 2 - Etudes in R - Episode II - Return of the Efficient Frontier.docx RMI 660...

Kshitij answered on Mar 30 2021
156 Votes
library(quadprog)
library(PerformanceAnalytics)
#Step 1: Import Data
assetPrices<-read.csv("stock
prices-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