编程代考 SP500″)

###### START RUNNING FROM HERE ################
library(quantmod)
getSymbols(‘MSFT’, from = ‘2006-12-29’, to = ‘2021-12-31’)
Ad.MSFT <- Ad(MSFT) # helper function for ad cl price Copyright By PowCoder代写 加微信 powcoder

# To calculate daily/monthly returns:
# The leading = FALSE option ensures that the first return is NA
# na.omit() removes the NA value
msftDailyRetS <- na.omit(dailyReturn(Ad.MSFT, type = 'arithmetic', leading = FALSE)) msftMonthlyRetS <- na.omit(monthlyReturn(Ad.MSFT, type = 'arithmetic', leading = FALSE)) msftMonthlyRetC <- na.omit(monthlyReturn(Ad.MSFT, type = 'log', leading = FALSE)) getSymbols('^GSPC', from = '2006-12-29', to = '2021-12-31') # Note the ^ symbol for indexes Ad.GSPC <- Ad(GSPC) # helper function for ad cl price sp500DailyRetS <- na.omit(dailyReturn(Ad.GSPC, type = 'arithmetic', leading = FALSE)) sp500MonthlyRetS <- na.omit(monthlyReturn(Ad.GSPC, type = 'arithmetic', leading = FALSE)) sp500MonthlyRetC <- na.omit(monthlyReturn(Ad.GSPC, type = 'log', leading = FALSE)) # generate random values with same length, mean, and sd as MSFT monthly simple returns set.seed(200) gwnMonthly <- rnorm(nrow(msftMonthlyRetS), mean = mean(msftMonthlyRetS), sd = sd(msftMonthlyRetS)) # Convert into time series using xts() function with same dates as MSFT data gwnMonthly <- xts(gwnMonthly, order.by = index(msftMonthlyRetS)) # Same for daily gwnDaily <- rnorm(length(msftDailyRetS), mean=mean(msftDailyRetS), sd=sd(msftDailyRetS)) gwnDaily <- xts(gwnDaily, index(msftDailyRetS)) # merge the returns msftsp500DailyRetS <- merge(msftDailyRetS, sp500DailyRetS) names(msftsp500DailyRetS) <- c("MSFT", "SP500") msftsp500MonthlyRetS <- merge(msftMonthlyRetS, sp500MonthlyRetS) names(msftsp500MonthlyRetS) <- c("MSFT", "SP500") msftsp500MonthlyRetC <- merge(msftMonthlyRetC, sp500MonthlyRetC) names(msftsp500MonthlyRetC) <- c("MSFT", "SP500") ####### END HERE ############################## # Plot adjusted closing price plot(Ad.MSFT) plot(Ad.GSPC) # Not weakly stationary # strong upward trend # drops corresponding to recessions # Plot the Monthly returns plot(msftsp500MonthlyRetS, multi.panel = TRUE, yaxis.same = FALSE, main = "Monthly Returns: Simple") # options only for xts objects # No trend behavior => mean stays roughly the same
# But we have evidence of changing volatility => non-stationary behavior
plot(msftsp500MonthlyRetS, multi.panel = FALSE, col = c(‘red’, ‘blue’), lty = c(2,1), main = “Monthly Returns: Simple”, legend.loc = “topright”) # options only for xts objects
# Daily Returns
plot(msftsp500DailyRetS, multi.panel = TRUE, yaxis.same = FALSE, main = “Monthly Returns: Simple”) # options only for xts objects
# Average daily returns look very close to zero

# Plot the monthly continuously compounded return:
plot(msftsp500MonthlyRetC, multi.panel = TRUE, yaxis.same = FALSE, main = “Monthly Returns: CC”)
# Calculate the difference b/w simple & cc returns:
retDiff <- msftMonthlyRetS - msftMonthlyRetC plotData <- merge(msftMonthlyRetS, msftMonthlyRetC, retDiff) names(plotData) <- c("Simple", "CC", "Difference") plot(plotData, multi.panel = TRUE, yaxis.same = FALSE, col = c("black", "blue", "red"), main = "Simple vs CC Monthly Returns") # Histograms par(mfrow = c(2,2)) hist(msftMonthlyRetS, main = '', col = "orchid") hist(msftDailyRetS, main = '', col = 'orchid') hist(sp500MonthlyRetS, main = '', col = 'orchid') hist(sp500DailyRetS, main = '', col = 'orchid') # potential left skewness # Saving histogram values monthlyHist <- hist(c(msftMonthlyRetS, sp500MonthlyRetS), plot = FALSE) dailyHist <- hist(c(msftDailyRetS, sp500DailyRetS), plot = FALSE) par(mfrow = c(2,2)) hist(msftMonthlyRetS, main = '', col = "orchid", breaks = monthlyHist$breaks) hist(msftDailyRetS, main = '', col = 'orchid', breaks = dailyHist$breaks) hist(sp500MonthlyRetS, main = '', col = 'orchid', breaks = monthlyHist$breaks) hist(sp500DailyRetS, main = '', col = 'orchid', breaks = dailyHist$breaks) # Plot and compare msft returns against simulated data: par(mfrow = c(2,2)) plot(msftMonthlyRetS) plot(gwnMonthly) hist(msftMonthlyRetS, breaks = monthlyHist$breaks, col = 'orchid') hist(gwnMonthly, breaks = monthlyHist$breaks, col = 'orchid') # Density Function hist(msftMonthlyRetS, breaks = 15, main = '', col = 'orchid', probability = TRUE) lines(density(msftMonthlyRetS), col = 'red', lwd = 2) 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com