Research project - R programming / analytics

profileSkaur
Assignment2.Rmd.pdf

--- title: 'Assignment #2' author: "Rongtao Wang" date: "1/14/2021" output: word_document ---

# 1. Check your working directory ```{r} getwd() ```

# 2. Set your working directory to ANLY 515/RStudio ```{r} setwd("/Users/wangjo/desktop") ```

# 3. Download the followin 3 data set, and lable them # BTC, LTC, ETH. # These data sets reperesnet daily prices # of three chryptocurrencies: Bitcoin, Litcoin, and Ethereum. # Set first column in the date format and the remaining columns # in numerical format. ```{r} library(readxl) BTC <- read_excel("btc.xlsx", col_types = c("date","numeric", "numeric", "numeric", "numeric","numeric", "numeric", "numeric", "numeric","numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric")) LTC <- read_excel("ltc.xlsx", col_types = c("date","numeric", "numeric", "numeric", "numeric","numeric", "numeric", "numeric", "numeric","numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric")) ETH <- read_excel("eth.xlsx", col_types = c("date","numeric","numeric","numeric","numeric","numeric","numeric","numeric"," numeric","numeric","numeric","numeric","numeric","numeric","numeric","numeric")) ```

# 4. Create three new datasets that are subsets of the original # datasets, to include dates only after "2015-08-31". # Name these datasets btcnew, ltcnew, ethnew. ```{r} btcnew<-BTC[BTC$date>"2015-08-31",] ltcnew<-LTC[LTC$date>"2015-08-31",] ethnew<-ETH[ETH$date>"2015-08-31",] ```

# 5. To each one of the three datasets created in the previous step # add a new variable that represents daily returns of each coin. # Use returnseries() function (part of FRAPO package). # Call these variables BTCRet, LTCRet, and ETHRet. ```{r} library(FRAPO) btcnew$BTCret <- returnseries(btcnew$`price(USD)`) ltcnew$LTCret <- returnseries(ltcnew$`price(USD)`) ethnew$ETHret <- returnseries(ethnew$`price(USD)`) ```

# 6. Create three new datasets labled "xxxsubset" that keep # the following two columns ("date", and "XXXRet") from the # "xxxnew" datasets. Make sure to omit missing values by using na.omit(). ```{r} btcsubset<-na.omit(btcnew[,c("date", "BTCret")]) ltcsubset<-na.omit(ltcnew[,c("date", "LTCret")]) ethsubset<-na.omit(ethnew[,c("date", "ETHret")])

This study source was downloaded by 100000773535061 from CourseHero.com on 06-09-2021 02:13:07 GMT -05:00

https://www.coursehero.com/file/89689854/Assignment2Rmd/

Th is

stu dy

re so

ur ce

w as

sh ar

ed v

ia C

ou rs

eH er

o. co

m

```

# 7. Create a stand alone variable "date" that takes on values # of "date" varible from the "btcsubset". ```{r} btcdate<-btcsubset$date ltcdate<-ltcsubset$date ethdate<-ethsubset$date ```

# 8. Create three stand alone variables titled "btcret", "ltcret", "ethret". # that represent daily returns of each of the coins. ```{r} btcret<-btcsubset$BTCret str(btcret) ltcret<-ltcsubset$LTCret str(ltcret) ethret<-ethsubset$ETHret str(ethret) ```

# 9. Use "date" variable to create 'times' attribute for # "btcret", "ltcret", "ethret". ```{r} attr(btcret,'times')<-btcdate str(btcret) attr(ltcret,'times')<-ltcdate str(ltcret) attr(ethret,'times')<-ethdate str(ethret) ```

# 10.Create three variables called "expbtcret", "expltcret", "expethret", # that would represent mean returns of each of the coins in attempt # to capture expected returns. Which coin offers the highest average rate of return? ```{r} expbtcret<-mean(btcret,na.rm=TRUE) expltcret<-mean(ltcret,na.rm=TRUE) expethret<-mean(ethret,na.rm=TRUE) c(btcret,ltcret,ethret) ```

# 11.Create three variables called "varbtc", "varltc", "vareth", # that would represent variance of returns of each of the coins in attempt # to capture volatility. Which coin is the most volatile? ```{r} varbtcret<-var(btcret) varltcret<-var(ltcret) varethret<-var(ethret) c(varbtcret, varltcret, varethret) ```

# 12.Create a formula that takes return series of the three coins and # associated weights as inputs and returns a vector with values of risk # and expected return for a portfolio of three coins. Lable the formula # "portriskret" ```{r} portriskret<-function(x,y,z,wx,wy) {varx<-var(x, na.rm = TRUE) vary<-var(y, na.rm = TRUE)

This study source was downloaded by 100000773535061 from CourseHero.com on 06-09-2021 02:13:07 GMT -05:00

https://www.coursehero.com/file/89689854/Assignment2Rmd/

Th is

stu dy

re so

ur ce

w as

sh ar

ed v

ia C

ou rs

eH er

o. co

m

varz<-var(z, na.rm = TRUE) meanx<-mean(x, na.rm = TRUE) meany<-mean(y, na.rm = TRUE) meanz<-mean(z, na.rm = TRUE) corxy<-cor(x,y, use="pairwise.complete.obs") +corxz<-cor(x,z, use="pairwise.complete.obs") +coryz<-cor(z,y, use="pairwise.complete.obs") +risk<-wx^2*varx+wy^2*vary+(1-wx- wy)^2*varz+ 2*sqrt(varx)*sqrt(vary)*wx*wy*corxy+ 2*sqrt(varx)*sqrt(varz)*wx*(1-wx-wy)*corxz+ 2*sqrt(vary)*sqrt(varz)*wy*(1-wx- wy)*coryz +portriskret<-wx*meanx+wy*meany+(1-wx-wy)*meanz c(portriskret,risk)} ```

# 13.Find risk and expected return for a portfolio that holds 60% Bitcoin, # 35% Litecoin, and 5% Etherium. Store the resulting vector as "one". # Would you rather invest in this portfolio or in any individual coin?

one<-portriskret(btcret,ltcret,ethret,0.60,0.35)

# 14.Create a dataset called "coinsrets" by binding "btcret", "ltcret", "ethret" ```{r} coinsrets<-cbind(btcret,ltcret,ethret) head(coinsrets) ```

# 15.Create a covariance matrix for "coinsrets" by using cov() function and use # use="pairwise.complete.obs" specification. Lable the matrix "coinscov" ```{r} coinscov<-cov(coinsrets, use="pairwise.complete.obs") head(coinscov) ```

# 16.Use the "coinsrets" matrix to find weights of the # "Global Minimum Variance Portfolio" # Save the suggested weights as DECIMALS in three seperate varibales. # Lable these weights "wbtc", "wltc", and "weth" # Hint 1: after you estimate the portfolio, use Weights() function to extract the vector of three weights. # Hint 2: Make sure to save each separate weight as a numeric vector. ```{r} GMV<-PGMV(coinscov) DECIMALS<-Weights(GMV)/100 str(DECIMALS) attr(DECIMALS,"names")<-NULL str(DECIMALS) wBTC<-c(DECIMALS[1]) wLTC<-c(DECIMALS[2]) wETH<-c(DECIMALS[3]) ```

# 17.Use "portriskret" function to find risk and expected return of the # portfolio with "GMVP" weights.Save the variable as "gmvpretrisk" # How does its risk and expected return compare to that of "one"?

gmvpretrisk<-portriskret(btcret,ltcret,ethret,wBTC,wLTC) gmvpretrisk

# 18.Create a variable called "myport" that represents daily returns of the GMVP portfolio. ```{r} myport<-(wBTC*btcret+wLTC*ltcret+wETH*ethret)

This study source was downloaded by 100000773535061 from CourseHero.com on 06-09-2021 02:13:07 GMT -05:00

https://www.coursehero.com/file/89689854/Assignment2Rmd/

Th is

stu dy

re so

ur ce

w as

sh ar

ed v

ia C

ou rs

eH er

o. co

m

```

# 19.Upload "timeSeries", "ghyp", and "fBasics" libraries ```{r} library(ghyp) library(timeSeries) library(fBasics) ```

# 20.Convert "myport" to TimeSeries format and name it myportts ```{r} myportts<-timeSeries(myport) ```

# 21.Use stepAIC.ghyp() function to see which distribution has the closest fit # to the actual distribution of "myport" returns. Which model would you choose? ```{r} AIC <- stepAIC.ghyp(myportts,control = list(maxit = 1000)) ```

# 22.Fit "myport" data to the chosen model and save the estimated coeficients as xxxfit. # where xxx represents the choice of the model ("ghyp", "hyp", "NIG", "VG", "t", "gauss") ```{r} AIC$fit.table ghypfit<- fit.ghypuv(myportts, symmetric = FALSE, control = list(maxit = 1000), na.rm = TRUE) summary(ghypfit) ```

# 23 What is the probability that the loss of the portfolio # would exceed 5% over the next trading day? ```{r} p=0.05 portvar<-abs(qghyp(p, ghypfit)) portvar ``` # 24.Create a vector called p that takes on values of 0.01, 0.05, 0.1 ```{r} p<-c(0.01,0.05,0.1) ```

# 25.Estimate VaR measurements of risk for your portfolio by using # qghyp(p, xxxfit)).Save these estimates as "portvar". # What is your largest expected daily loss in 95% of all cases. ```{r} portvar<-abs(qghyp(p, ghypfit)) portvar ```

# 26.Estimate ES measurement of risk for your portfolio by using # ESghyp(p, xxxfit). Save these estimates as "portes". # How do these estimates of risk compare to VaR estimates. ```{r} portes<-abs(ESghyp(p, ghypfit)) portes ``` # 27. Generate 100,000 random returns that follow the best fit distribution which you # have identified in question#21. Save these observations as xxxfit.rand,

This study source was downloaded by 100000773535061 from CourseHero.com on 06-09-2021 02:13:07 GMT -05:00

https://www.coursehero.com/file/89689854/Assignment2Rmd/

Th is

stu dy

re so

ur ce

w as

sh ar

ed v

ia C

ou rs

eH er

o. co

m

where # xxx represents the choice of the model ("ghyp", "hyp", "NIG", "VG", "t", "gauss"). ```{r} AIC <- stepAIC.ghyp(myportts, control = list(maxit = 100000)) ```

# 28. Find values of the 1,000th, 5,000th, and 10,000th lowest simulated returns. # These returns represent Value at Risk with 99%, 95%, and 90% # Save these values as portvar2.0. # How do these estimates of value of risk compare to those obtained in #25?

# 29 Find the average value of among the 1,000, 5,000, and 10,000 lowest simulated returns. # These values would represent expected shortfall with 99%, 95%, and 90% confidence. # Lable these values as portes2.0. # How do these espirates of expected shortfall compare to those obtained in #26?

# 30. Plot the histogram with 100 breaks of the xxxfit.rand returns series. # Moreover, add six vertical lines to represent value at risk at 99%, 95%, and 90% # confidence levels, which you calculated in #28, # and the expected shortfall at 99%, 95%, and 90% confidence levels, # which you calculated in #29. # Please assign red color to represent 99%, green color to represent 95%, # and blue color to represent 90% confidence levels. # Moreover, use the dashed line style to represent Value at Risk measurenments, # and solid line to represent Expected Shortfall measurenments. # Make sure to add a legend.

hist(btcret, breaks = 100) tail(BTCPrice) V0<-BTCPrice[1239] stdev<-sd(btcret, na.rm = TRUE) alpha<-qnorm(0.05) VAR<-V0*stdev*alpha VAR VAR252<-V0*stdev*alpha*sqrt(252) VAR252

This study source was downloaded by 100000773535061 from CourseHero.com on 06-09-2021 02:13:07 GMT -05:00

https://www.coursehero.com/file/89689854/Assignment2Rmd/

Th is

stu dy

re so

ur ce

w as

sh ar

ed v

ia C

ou rs

eH er

o. co

m

Powered by TCPDF (www.tcpdf.org)