代写 R code

code
37 1044
43 1880
50 3988
42 1398
RMSC4002 2019/20 1st term Assignment 1
Q1.(a)
The file “hkse50.csv” contains 50 names and codes of stocks listed on the main board of the Hong Kong stock exchange. Use the last 5 digits of your student ID as a random seed to select 8 stocks randomly from the list. For example, if your student ID is 12345:
stock <- read.csv(“hkse50.csv”) set.seed(12345) r <- sample(1:50, size=5) stock[r,] In Chapter 2 of my notes, I have generated one single path of future 90 days of stock prices for HSBC, CLP, and CK. Now you are going to perform simulation on these 8 stocks. Imagine we have a portfolio of 5,000 shares of each of these 8 stocks. Now we want to generate 1000 random paths of future 10 days of these 8 stocks and hence compute the value of this portfolio based on these simulated prices. Modify my R codes according to the following: 1. In the internet, search and download the adjusted daily closing prices for the 8 selected stocks from 1/1/2017 to 31/12/2018. (In the tseries library, there is a function get.hist.quote() which can download prices easily, see help(get.hist.quote) for more details. Alternatively, you can download manually from Yahoo Finance or Bloomberg. 2. Compute the value of your portfolio based on the closing price of the last day, 31/12/2018, say, 𝑣0. 3. Using the last 5 digits of your student ID as the initial seed, set up a loop to generate 1000 random paths of the prices of these 8 stocks for future 10 days. Use the last 60 days in your dataset to estimate the mean vector and covariance matrix in your simulation. 4. Save the last simulated stock prices and compute the portfolio value based on these simulated stock prices. Compute the profit loss by: 𝑠𝑖𝑚𝑢𝑙𝑎𝑡𝑒𝑑 𝑠𝑡𝑜𝑐𝑘 𝑝𝑟𝑖𝑐𝑒𝑠 − 𝑣0 . 5. Find the min, max, mean, median, standard deviation, and lowest 1 and 5 percentiles from this profit/loss distribution. Q1.(b) Repeat the previous question (Q1.(a)) except using 8 stocks chosen from the Dow Jones Industrial Average (Dow). The Dow is a stock market index comprised of 30 large companies listed on American exchanges. You can find a list of said companies here: https://money.cnn.com/data/dow30/ After selecting 8 stocks from the above list (at your discretion), download their daily adjusted prices from 1/1/2017 to 31/12/2018 as in the previous question, and follow the steps 1-5 in Question 1.(a). name Hengan Int’l Belle Int’l Bank of China ICBC 21 0291 China Resources #read in data #set random seed #select 5 random integers #list 5 selected stocks RMSC4002 2019/20 1st term Assignment 1 Q2.(a) Continuing with the same Hong Kong stock prices as in Q1.(a), we want to fit an EWMA model using Excel. Choose the first stock in Q1.(a) and fit an EWMA model which minimizes the sum of absolute error: 𝑛−19 ∑|𝜎𝑖 −𝑠𝑖| 𝑖=1 instead of maximizing the likelihood function, where 𝜎2 = 𝜆𝜎2 𝑖 𝑖−1 + (1 − 𝜆)𝑢2 is the variance 1. Using EXCEL, compute the relative change of stock prices, setup the columns for 𝜎𝑖 and 𝑠𝑖 and the parameter 𝜆 in EWMA model that minimizes the sum of absolute error: 𝑛−19 ∑|𝜎𝑖 −𝑠𝑖|. 𝑖=1 2. Plot the fitted volatilities 𝜎𝑖 using the EWMA model in 1. 3. If we use 𝜆 = 0.96, compute and compare the sum of absolute error with the EWMA model in 1. Q2.(b) Repeat the previous question but by fitting the GARCH(1,1) model instead of EWMA (note: You do not need to compare the fit as in part 3, just fit the GARCH(1,1) model and plot the fitted volatilities). You may use R to fit the GARCH model using garch() function in the tseries package. Example code can be found in Chapter 3 of the lecture notes. You need to submit eight files via Blackboard: asg1HK.csv, asg1DOW.csv, asg1-1.R, asg1-1.pdf, asg1- 2(a).xls, asg1-2(b).xls, asg1-2.R, and asg1-2. pdf. The files asg1HK.csv and asg1DOW.csv should contain the stock prices of the 8 selected stocks for the HK and DOW respectively. The asg1-1.R and asg1-1. pdf should contain the R codes and output in Question 1; asg1-2a.xls and asg1-2b.xls should contain the EXCEL file and data for Question 2(a) and Question 2(b) respectively. The asg1-2.R and asg1-2. pdf should contain the R code and output for fitting the GARCH(1,1) model in Question 2(b). Note: To save your R output as a PDF file, go to file -> Print… -> Microsoft Print to PDF in R.
The R codes should be fully commented to explain clearly what you are doing, and ready to execute
without bugs/errors.
Submit your files on or before Thursday October 10th, 2019.
Put a hardcopy of asg1-1.out and asg1-2.out (stapled together!) in the dropbox outside the stat. lab on or before Thursday October 10th, 2019.
rate estimated from EWMA model and 𝑠2 is the variance of 𝑢 , … , 𝑢 . 𝑖 𝑖𝑖+19
𝑖−1