Docsity

Assignment 2 soluzioni, Schemi e mappe concettuali di Econometria

Toggle sidebar
bg1
1) Downloading dataset
Exxon stock price
DB_Exxon <- read.csv(
url("https://query1.finance.yahoo.com/v7/finance/download/XOM?period1=1640908800&period2=16
64150400&interval=1d&events=history&includeAdjustedClose=true"), header = TRUE)
Netflix stock price
DB_Netflix <- read.csv(
url("https://query1.finance.yahoo.com/v7/finance/download/NFLX?period1=1640908800&period2=16
64150400&interval=1d&events=history&includeAdjustedClose=true"), header = TRUE)
SPX index price
DB_SPX <- read.csv(file.choose(), header = TRUE, sep=";", dec=",")
View your dataset
View(DB_Exxon)
View(DB_Netflix)
View(DB_SPX)
2) Computing returns
Creating price variables
Exxon <- DB_Exxon$Close
Netflix <- DB_Netflix$Close
SPX <- DB_SPX$SPX.Index
Computing log difference returns
ReturnsExxon <- diff(log(Exxon))
ReturnsNetflix <- diff(log(Netflix))
ReturnsSPX<- diff(log(SPX))
pf3
pf4
pf5

Anteprima parziale del testo

Scarica Assignment 2 soluzioni e più Schemi e mappe concettuali in PDF di Econometria solo su Docsity!

  1. Downloading dataset Exxon stock price DB_Exxon <- read.csv( url("https://query1.finance.yahoo.com/v7/finance/download/XOM?period1=1640908800&period2= 64150400&interval=1d&events=history&includeAdjustedClose=true"), header = TRUE) Netflix stock price DB_Netflix <- read.csv( url("https://query1.finance.yahoo.com/v7/finance/download/NFLX?period1=1640908800&period2= 64150400&interval=1d&events=history&includeAdjustedClose=true"), header = TRUE) SPX index price DB_SPX <- read.csv(file.choose(), header = TRUE, sep=";", dec=",") View your dataset View(DB_Exxon) View(DB_Netflix) View(DB_SPX)
  2. Computing returns Creating price variables Exxon <- DB_Exxon$Close Netflix <- DB_Netflix$Close SPX <- DB_SPX$SPX.Index Computing log difference returns ReturnsExxon <- diff(log(Exxon)) ReturnsNetflix <- diff(log(Netflix)) ReturnsSPX<- diff(log(SPX))

Computing YTD returns ReturnsYTD_Exxon <- (Exxon[184]-Exxon[1])/Exxon[1] ReturnsYTD_Exxon [1] 0. ReturnsYTD_Netflix <- (Netflix[184]-Netflix[1])/Netflix[1] ReturnsYTD_Netflix [1] - 0. ReturnsYTD_SPX <- (SPX[184]-SPX[1])/SPX[1] ReturnsYTD_SPX [1] - 0. Computing average daily returns mean(ReturnsExxon) [1] 0. mean(ReturnsNetflix) [1] - 0. mean(ReturnsSPX) [1] - 0. Computing annualized standard deviation varExxon <- var(ReturnsExxon) stdevExxon <- (varExxon^0.5) stdevExxon_y <- stdevExxon(252^0.5) stdevExxon_y [1] 0. varNetflix <- var(ReturnsNetflix) stdevNetflix <- (varNetflix^0.5) stdevNetflix_y <- stdevNetflix(252^0.5) stdevNetflix_y [1] 0. varSPX <- var(ReturnsSPX) stdevSPX <- (varSPX^0.5) stdevSPX_y <- stdevSPX*(252^0.5) stdevSPX_y [1] 0.

  1. Plotting class(DB_Exxon$Date) [1] "character" t <- as.Date(DB_Exxon$Date, "%Y-%m-%d") class(t) [1] "Date" plot(t,Portfolio_Best,type="l",col="green", ylab = "Exxon, Netflix, SPX returns", ylim = c(20,200)) lines(t, Portfolio_Worst,col="blue") lines(t, Portfolio_Index,col="red") title("Exxon, Netflix, SPX returns YTD") legend("topleft",legend = c("Exxon","SPX", "Netflix"),col= c("green","red","blue"),lwd = c(1,1,1)) cor(ReturnsExxon, ReturnsSPX) [1] 0. cor(ReturnsNetflix, ReturnsSPX) [1] 0.

Effects of diversification Portfolio_MIX <- 1:184; 0 for (i in 1:184) { Portfolio_MIX[i]<- 0.5(Exxon[i]/head(Exxon,1)100)+0.5(Netflix[i]/head(Netflix,1)100) } Portfolio_MIX [1] 100.00000 101.49946 102.93616 101.64501 101.74687 101.18942 100.75398 103. [9] 102.71742 100.80511 102.35699 102.10990 102.55438 102.05352 91.96289 91. [17] 91.63865 90.45990 93.47707 93.41359 97.51980 103.98825 101.52183 98. … [161] 95.61998 98.71516 99.60905 100.38847 98.50353 100.44913 97.01053 96. [169] 95.79607 96.87535 95.71166 95.92706 96.43010 98.23809 99.39077 96. [177] 98.40981 97.02371 96.09419 96.37650 95.69076 93.97692 93.68135 88. plot(t,Portfolio_Best,type="l",col="green", ylab = "Exxon, Netflix, SPX returns", ylim = c(20,200)) lines(t, Portfolio_Worst,col="blue") lines(t, Portfolio_Index,col="red") lines(t,Portfolio_MIX, col="yellow") title("Exxon, Netflix, SPX returns YTD") legend("topleft",legend = c("Exxon","SPX", "Netflix"),col= c("green","red","blue"),lwd = c(1,1,1))