程序代写代做 #Practical Assignment #4

#Practical Assignment #4
# This assignment is worth 150 points.

# Useful hints: Utilize book examples and make sure to download and library
# appropriate packages (FRAPO, timeSeries, QRM, fGarch,copula)
# Under each of the items provide the relevant R code and answer the questions.

# 1) Set your working directory to RStudio folder
# that you have created inside the ANLY 515 forlder”
> getwd()
[1] “/Users/”
> setwd(“/Users/Desktop/Harrisburg University/ANLY 515-90-O-2019: Late Fall”)

# 2) Download the followin data set, and lable it fcur
# This data sets reperesnets daily exchange rates between US dollar
# and 21 foreign currencies. Moreove, it has an Nominal Broad Dollar Index
# that captures the performance of USD against a equal weight basket
# of all the currencies. The exchange rates are expressed as price 1 USD
# in the foreign currency. Make sure to set first column in the date format
# and the remaining columns are in the numerical format.
# Hint: May have to set the format of each column manually.

> library(readr)
> fcur <- read_csv("fcur.csv", col_types = cols(AUD = col_number(), + BRL = col_number(), CAD = col_number(), + CHF = col_number(), CNY = col_number(), + DKK = col_number(), GBP = col_number(), + HKD = col_number(), INR = col_number(), + JPY = col_number(), KRW = col_number(), + LKR = col_number(), MXN = col_number(), + MYR = col_number(), NOK = col_number(), + NZD = col_number(), `Nominal Broad Dollar Index` = col_number(), + SEK = col_number(), SGD = col_number(), + THB = col_number(), TWD = col_number(), + `Time Period` = col_date(format = "%m/%d/%Y"), + ZAR = col_number())) Warning: 2948 parsing failures. row col expected actual file 1 AUD a number ND 'fcur.csv' 1 NZD a number ND 'fcur.csv' 1 BRL a number ND 'fcur.csv' 1 CAD a number ND 'fcur.csv' 1 CNY a number ND 'fcur.csv' ... ... ........ ...... .......... See problems(...) for more details. > view(fcur)

# 3) Upload all of the following packages:
# FRAPO, timeSeries, QRM, fGarch, copula

library(FRAPO)
library(timeSeries)
library(QRM)
library(fGarch)
library(copula)

# 4) Create a data set “fcurcc” that is a sub set of
# “fcur” and consists of all complete cases.

fcurcc<-fcur[complete.cases(fcur), ] # 5) By using the 'Time Period' column of the "fcurcc" create # a "date" variable and save it as.character. > date<- as.character(fcurcc$`Time Period`) # 6) Create a new data set "fcurccd", that excludes # the first column of the "fcurcc" data set ('Time Period'). # Should have 22 columns > fcurccd<-fcurcc[,-1] # 7) By using sapply function modify all exchnge rates including the Broad Index # to represent the prices of 1 foreign currency in terms of USD. # Save these exchange rates as "fcprice" # Hint: The price of 1 foreign currency in terms of USD, is a reciprocal # of the price 1 USD in terms of foreign currency. > fcprice<-sapply(fcurccd, function(x) 1/x) > head(fcprice)
Nominal Broad Dollar Index AUD NZD BRL
[1,] 0.01006549 1.355197 1.467351 0.4288165
[2,] 0.01011829 1.339585 1.452855 0.4344049
[3,] 0.01011136 1.336541 1.454545 0.4384042
[4,] 0.01013930 1.328374 1.447387 0.4371585
[5,] 0.01012368 1.330318 1.444669 0.4424779
[6,] 0.01012721 1.335113 1.442169 0.4431838
CAD CNY DKK HKD INR
[1,] 0.8646779 0.1239127 0.1605549 0.1289740 0.02226180
[2,] 0.8684325 0.1239127 0.1620404 0.1289773 0.02235636
[3,] 0.8606593 0.1239495 0.1622244 0.1289757 0.02242152
[4,] 0.8583691 0.1239726 0.1629328 0.1289807 0.02256318
[5,] 0.8553588 0.1239695 0.1617756 0.1290206 0.02262443
[6,] 0.8599931 0.1240541 0.1617312 0.1290223 0.02265519
JPY MYR MXN NOK ZAR
[1,] 0.008595496 0.2645503 0.09395847 0.1505752 0.1604390
[2,] 0.008597713 0.2649147 0.09445368 0.1526205 0.1622718
[3,] 0.008623663 0.2655408 0.09441979 0.1524785 0.1626545
[4,] 0.008737440 0.2662761 0.09467904 0.1532755 0.1642845
[5,] 0.008721437 0.2667734 0.09471491 0.1521098 0.1646091
[6,] 0.008722959 0.2667734 0.09431649 0.1509457 0.1635590
SGD GBP KRW SEK LKR
[1,] 0.6052902 0.5745806 0.0009971084 0.1280459 0.009802000
[2,] 0.6079027 0.5685695 0.0010019036 0.1294515 0.009790484
[3,] 0.6073120 0.5692816 0.0010058338 0.1297151 0.009793360
[4,] 0.6103516 0.5650675 0.0010109179 0.1304989 0.009803922
[5,] 0.6111722 0.5667328 0.0010231226 0.1295924 0.009804883
[6,] 0.6118828 0.5667328 0.0010185374 0.1289906 0.009818360
CHF TWD THB
[1,] 0.7729170 0.03068426 0.02453386
[2,] 0.7811890 0.03096934 0.02473411
[3,] 0.7830854 0.03128911 0.02483855
[4,] 0.7877117 0.03110420 0.02510670
[5,] 0.7816164 0.03134796 0.02516990
[6,] 0.7809449 0.03126954 0.02513194

# 8) By using “fcprice” and “date” variables create time series object,
# and label it “fcpricets”.

> fcpricets<-timeSeries(fcprice,charvec = date) > head(fcpricets)
GMT
Nominal Broad Dollar Index AUD NZD
2006-01-03 0.01006549 1.355197 1.467351
2006-01-04 0.01011829 1.339585 1.452855
2006-01-05 0.01011136 1.336541 1.454545
2006-01-06 0.01013930 1.328374 1.447387
2006-01-09 0.01012368 1.330318 1.444669
2006-01-10 0.01012721 1.335113 1.442169
BRL CAD CNY DKK HKD
2006-01-03 0.4288165 0.8646779 0.1239127 0.1605549 0.1289740
2006-01-04 0.4344049 0.8684325 0.1239127 0.1620404 0.1289773
2006-01-05 0.4384042 0.8606593 0.1239495 0.1622244 0.1289757
2006-01-06 0.4371585 0.8583691 0.1239726 0.1629328 0.1289807
2006-01-09 0.4424779 0.8553588 0.1239695 0.1617756 0.1290206
2006-01-10 0.4431838 0.8599931 0.1240541 0.1617312 0.1290223
INR JPY MYR MXN
2006-01-03 0.02226180 0.008595496 0.2645503 0.09395847
2006-01-04 0.02235636 0.008597713 0.2649147 0.09445368
2006-01-05 0.02242152 0.008623663 0.2655408 0.09441979
2006-01-06 0.02256318 0.008737440 0.2662761 0.09467904
2006-01-09 0.02262443 0.008721437 0.2667734 0.09471491
2006-01-10 0.02265519 0.008722959 0.2667734 0.09431649
NOK ZAR SGD GBP
2006-01-03 0.1505752 0.1604390 0.6052902 0.5745806
2006-01-04 0.1526205 0.1622718 0.6079027 0.5685695
2006-01-05 0.1524785 0.1626545 0.6073120 0.5692816
2006-01-06 0.1532755 0.1642845 0.6103516 0.5650675
2006-01-09 0.1521098 0.1646091 0.6111722 0.5667328
2006-01-10 0.1509457 0.1635590 0.6118828 0.5667328
KRW SEK LKR CHF
2006-01-03 0.0009971084 0.1280459 0.009802000 0.7729170
2006-01-04 0.0010019036 0.1294515 0.009790484 0.7811890
2006-01-05 0.0010058338 0.1297151 0.009793360 0.7830854
2006-01-06 0.0010109179 0.1304989 0.009803922 0.7877117
2006-01-09 0.0010231226 0.1295924 0.009804883 0.7816164
2006-01-10 0.0010185374 0.1289906 0.009818360 0.7809449
TWD THB
2006-01-03 0.03068426 0.02453386
2006-01-04 0.03096934 0.02473411
2006-01-05 0.03128911 0.02483855
2006-01-06 0.03110420 0.02510670
2006-01-09 0.03134796 0.02516990
2006-01-10 0.03126954 0.02513194

# 9) By using first 3250 observations of the “fcpricets” create two objects
# called: “RM” and “RA”. The “RM” should represent, daily returns
# of “Nominal Broad Dollar Index” while the “RA” should represent
# daily returns on each foreign currency.If the percentage change
# is positive it means that the foreign currency is getting stronger,
# and more dollars are need to buy 1 unit of foreign currency.
# Hint: Use returnseries function and specify trim=TRUE
# to get rid of the missing values.
> head(RM)
GMT
TS.1
2006-01-04 0.5246335
2006-01-05 -0.0685550
2006-01-06 0.2763974
2006-01-09 -0.1540824
2006-01-10 0.0348376
2006-01-11 0.2726569
> RA<-returnseries(fcpricets[1:3250, -1], method = "discrete", trim = TRUE) > head(RA)
GMT
AUD NZD BRL CAD
2006-01-04 -1.1520429 -0.9879413 1.3032146 0.4342162
2006-01-05 -0.2272120 0.1163636 0.9206488 -0.8950856
2006-01-06 -0.6110521 -0.4921117 -0.2841530 -0.2660944
2006-01-09 0.1463350 -0.1878070 1.2168142 -0.3506971
2006-01-10 0.3604806 -0.1730603 0.1595462 0.5417957
2006-01-11 -0.8997089 -0.5450373 -0.6866197 0.4231799
CNY DKK HKD INR
2006-01-04 0.00000000 0.9252508 0.002579547 0.4247708
2006-01-05 0.02974789 0.1135571 -0.001289757 0.2914798
2006-01-06 0.01859589 0.4366599 0.003869420 0.6317690
2006-01-09 -0.00247939 -0.7101951 0.030964945 0.2714932
2006-01-10 0.06822975 -0.0274943 0.001290223 0.1359311
2006-01-11 -0.08923924 0.6151042 -0.003870518 0.5696058
JPY MYR MXN NOK
2006-01-04 0.02579314 0.13775564 0.52705153 1.35832240
2006-01-05 0.30182822 0.23633129 -0.03587952 -0.09301191
2006-01-06 1.31935343 0.27692717 0.27456921 0.52266945
2006-01-09 -0.18315018 0.18674136 0.03788596 -0.76054881
2006-01-10 0.01744592 0.00000000 -0.42065154 -0.76529457
2006-01-11 0.59670060 -0.04799616 -0.05090498 0.09972349
ZAR SGD GBP KRW
2006-01-04 1.1423935 0.43161094 -1.04616784 0.4809137
2006-01-05 0.2358491 -0.09716993 0.12524194 0.3922752
2006-01-06 1.0021357 0.50048828 -0.74023846 0.5054590
2006-01-09 0.1975309 0.13445789 0.29470105 1.2072846
2006-01-10 -0.6378803 0.11625773 0.00000000 -0.4481564
2006-01-11 0.9327280 0.38697789 0.01133594 0.4501739
SEK LKR CHF TWD
2006-01-04 1.0977488 -0.117485804 1.07022889 0.9290802
2006-01-05 0.2036528 0.029380080 0.24275646 1.0325407
2006-01-06 0.6042099 0.107843137 0.59078377 -0.5909798
2006-01-09 -0.6946154 0.009804883 -0.77380022 0.7836991
2006-01-10 -0.4643663 0.137457045 -0.08590394 -0.2501563
2006-01-11 0.7393835 0.000000000 0.55756243 -0.2806361
THB
2006-01-04 0.8162256
2006-01-05 0.4222553
2006-01-06 1.0795883
2006-01-09 0.2516990
2006-01-10 -0.1507917
2006-01-11 0.3277862

# 10) By using apply function compute the value of Beta for each currency.
# The beta (?? or beta coefficient) of an investment indicates whether
# the investment is more or less volatile than the market as a whole.
# Beta can be found by dividing cov(Foreign, Index)/Var(Index)
# Which currency has the lowest Beta?

> Beta <- apply(RA, 2, function(x) cov(x,RM)/var(RM)) > Beta
AUD NZD BRL CAD CNY
-1.86859732 -1.77445935 1.76759296 1.36121107 0.13193568
DKK HKD INR JPY MYR
1.41039722 0.01885260 0.63600756 0.33535931 0.52846645
MXN NOK ZAR SGD GBP
1.48152862 1.78880025 2.08111745 0.79334984 -1.21896822
KRW SEK LKR CHF TWD
1.12823191 1.73818606 0.01077589 1.16903633 0.44343416
THB
0.43218647

# 11) By using apply function compute the value of Tau for each currency.
# Tau is a Kendall rank correlation coefficient, between
# two measured quantities(one of a foreign currency and one of Broad Index).

> Tau<- apply(RA, 2, function(x) cor(x,RM, method = "kendall")) # 12) By using Kendal rank correlation coeffients "Tau", estimate the # value of Clayton (Archimedean family) copula parameter "Theta" > Theta<- copClayton@iTau(Tau) # 13) Use Theta to extact lower tail dependence coefficients "Lambda". # Lambda represents the interdependence between each foreign currency # and Broad Index at the lower tail of the distributions Lambda<- copClayton@lambdaL(Theta) head(Lambda) > AUD NZD BRL CAD CNY DKK
> 2.6289954 2.8857969 0.6000178 0.7480467 0.2604640 0.8006601

# 14) Select foreign currencies which Betas are below the median value of Beta,
# and save the results as “IdxBeta”.
# Which currencies would you select?

> IdxBeta<-Beta WBeta<-1/log(Beta) Warning message: In log(Beta) : NaNs produced # 16) Select foreign currencies with Lambdas below # the median value of Lambda, and save the results as "IdxTD" # Which currencies would you select? > IdxTD<-Lambda WTD<- log(IdxTD) # 18) Create a variable "Intersection" that represnts in percentage terms # how similar are the portfolios' currency selections based # on Low Tau vs Low Lambda criteria. # Out-of-Sample Performance # 19) By using last 44 observations of the "fcpricets" create two objects # called: "RMo" and "RAo". The "RMo" should represent, daily returns # of "Nominal Broad Dollar Index", while the "RAo" should represent # the daily returns on each foreign currency. # Hint: use returnseries function, express returns as decimals # and DO NOT trim. > RMo<- returnseries(fcpricets[-38, 1], method = "discrete", trim = TRUE) > head(RM)
GMT
TS.1
2006-01-04 0.5246335
2006-01-05 -0.0685550
2006-01-06 0.2763974
2006-01-09 -0.1540824
2006-01-10 0.0348376
2006-01-11 0.2726569
> RAo<-returnseries(fcpricets[-38,-1], method = "discrete", trim = TRUE) > head(RA)
GMT
AUD NZD BRL CAD
2006-01-04 -1.1520429 -0.9879413 1.3032146 0.4342162
2006-01-05 -0.2272120 0.1163636 0.9206488 -0.8950856
2006-01-06 -0.6110521 -0.4921117 -0.2841530 -0.2660944
2006-01-09 0.1463350 -0.1878070 1.2168142 -0.3506971
2006-01-10 0.3604806 -0.1730603 0.1595462 0.5417957
2006-01-11 -0.8997089 -0.5450373 -0.6866197 0.4231799
CNY DKK HKD INR
2006-01-04 0.00000000 0.9252508 0.002579547 0.4247708
2006-01-05 0.02974789 0.1135571 -0.001289757 0.2914798
2006-01-06 0.01859589 0.4366599 0.003869420 0.6317690
2006-01-09 -0.00247939 -0.7101951 0.030964945 0.2714932
2006-01-10 0.06822975 -0.0274943 0.001290223 0.1359311
2006-01-11 -0.08923924 0.6151042 -0.003870518 0.5696058
JPY MYR MXN NOK
2006-01-04 0.02579314 0.13775564 0.52705153 1.35832240
2006-01-05 0.30182822 0.23633129 -0.03587952 -0.09301191
2006-01-06 1.31935343 0.27692717 0.27456921 0.52266945
2006-01-09 -0.18315018 0.18674136 0.03788596 -0.76054881
2006-01-10 0.01744592 0.00000000 -0.42065154 -0.76529457
2006-01-11 0.59670060 -0.04799616 -0.05090498 0.09972349
ZAR SGD GBP KRW
2006-01-04 1.1423935 0.43161094 -1.04616784 0.4809137
2006-01-05 0.2358491 -0.09716993 0.12524194 0.3922752
2006-01-06 1.0021357 0.50048828 -0.74023846 0.5054590
2006-01-09 0.1975309 0.13445789 0.29470105 1.2072846
2006-01-10 -0.6378803 0.11625773 0.00000000 -0.4481564
2006-01-11 0.9327280 0.38697789 0.01133594 0.4501739
SEK LKR CHF TWD
2006-01-04 1.0977488 -0.117485804 1.07022889 0.9290802
2006-01-05 0.2036528 0.029380080 0.24275646 1.0325407
2006-01-06 0.6042099 0.107843137 0.59078377 -0.5909798
2006-01-09 -0.6946154 0.009804883 -0.77380022 0.7836991
2006-01-10 -0.4643663 0.137457045 -0.08590394 -0.2501563
2006-01-11 0.7393835 0.000000000 0.55756243 -0.2806361
THB
2006-01-04 0.8162256
2006-01-05 0.4222553
2006-01-06 1.0795883
2006-01-09 0.2516990
2006-01-10 -0.1507917
2006-01-11 0.3277862

# 20) Set the value of the first observation of the “RMo” object to 100.

> RMo[1]<-"100" # 21) Generate a new variable called "RMEquity" that calculates cumulative # product of the RMo object. By doing so you will find cumulative perfomance # of the Broad Index. > RMEquity<- cumprod(RMo) # 22) Create a new variable called "LBEquite", that is subset of the "RAo" object # and includes only columns that represent currencies that were selected # to be a part of the portfolio under the low beta selection criteria. > LBEquite<- RAo[c(1,2,5,7,8,9,10,15,18,20,21)] # 23) Assign the values of the first row of the "LBEquite" object to be equal to # "WBeta" verctor. > #LBEquite[1]<- WBeta # 24) Apply rowSums(apply()) function to calculate weighted cumulative # product of the LBEquity object. By doing so you will find cumulative # perfomance of the low beta portfolio over the out-of-sample period. # Save the results as "LBEquity". > #LBEquity<- rowSums(apply(cumprod(LBEquite))) # 25) Create a new variable called "TDEquite", that is subset of the "RAo" # object and includes only columns that represent currencies that were # selected to be a part of the portfolio under the low tail dependence # selection criteria. > TDEquite<- RAo[c(3,5,7,8,9,15,17,20,21)] # 26) Assign the values of the first row of the "TDEquity" object to be equal to # "WTD" verctor. > #TDEquite[1]<- WTD # 27) Apply rowSums(apply()) function to calculate weighted cumulative product # of the TDEquity object. By doing so you will find cumulative perfomance # of the low tail dependency portfolio over the out-of-sample period. # Save the results as "TDEquity". # 28) Collect results of the out-of-sample performance by binding together # RMEquity, LBEquity, TDEquity, and compute summary statistics for each # element. Which investment strategy yeilds the best average equity? # 29) Create a time series plots of equity curves for the "Out-of-Sample Periods". # 30) Create a Bar plot of relative performance of the Broad Currency Index, # "Low Beta Portfolio", and "Low Tail Dependency Portfolio" # Which portfolio would you choose?