关于机器学习:R语言分析

34次阅读

共计 2032 个字符,预计需要花费 6 分钟才能阅读完成。

title: “Assignment 2”
author: “YOUR NAME”
date: “October 10, 2021 (due Oct 17, 2021)”

output: pdf_document

knitr::opts_chunk$set(echo = TRUE)

Note: This is an individual assignment. No discussion with a fellow student is allowed. Honor code is in place.

Use the following code to download monthly excess return data on microsoft and appl stock.

library(cbw);
prmdf = getfinmdat(symbols = c("MSFT","AAPL","^gspc"),
                   symnames = c("msft","aapl","sp500"),
                  from = "2001-01-01",
                  to = "2021-03-31");
rskfree = getmrskfree(from = "2001-01-01",
                  to = "2021-03-31")
rskfree1 = rskfree[(-1),]

Now write code to accomplish the following tasks. Write your code in the blank chunk provided below.

Question 1

1

The data in prmdf contains the excess returns on msft, aapl and prmsp500. Use the rskfree variable to create the monthly returns on msft, appl and prmsp500. Put these monthly returns in a data.frame called retdf. This data.frame should have 242 rows and 3 columns, with variable names given by retmsft, retaapl and retsp500 and rownames same as the rownames of prmdf.

# write your code here

2

Find the mean monthly returns on msft, appl and the sp500 index. Put these in a numeric vector called mret.

# write your code here

3

Now suppose that you were to invest $10,000 in each of these assets. Assuming that you get the same monthly return per month as the mean monthly return calculated in question #2, how much would you have in each asset after 60 months?

# write your code here

4

Calculate the sample covariance matrix of the returns in retdf. Call this matrix Sigmahat. From this matrix, find the estimated sd of returns – call this numeric vector sdret. What is the estimated covariance between retappl and retsp500?

# write your code here

5

Calculate the matrix L such that L times the transpose of L is equal to Sigmahat.

# write your code here

Question 2

1

Now suppose that retsp500 in the population follows the N(mu,sigmasq) distribution. Estimate this model by Bayesian methods (use the MCMCregressg function) and summarize the posterior distribution of two parameters in terms of the posterior mean, posterior sd and posterior .025 and .975 quantiles (use the apply function to get these summaries).

# write your code here

2

Use the posterior draws of the parameters and the method of composition to show how you would generate a prediction sample of retsp500 for the first month beyond the sample data.

# write your code here

正文完
 0