1) Set your working directory to RStudio folder

that you have created inside the ANLY 515 forlder"

setwd("F:/THESIS/Tanya/R-Folder/data/")

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)
## Warning: package 'readr' was built under R version 3.4.4
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 in rbind(names(probs), probs_f): number of columns of result is not
## a multiple of vector length (arg 1)
## Warning: 2948 parsing failures.
## row # A tibble: 5 x 5 col     row col   expected actual file       expected   <int> <chr> <chr>    <chr>  <chr>      actual 1     1 AUD   a number ND     'fcur.csv' file 2     1 NZD   a number ND     'fcur.csv' row 3     1 BRL   a number ND     'fcur.csv' col 4     1 CAD   a number ND     'fcur.csv' expected 5     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)
## Warning: package 'FRAPO' was built under R version 3.4.4
## Loading required package: cccp
## Warning: package 'cccp' was built under R version 3.4.4
## Loading required package: Rglpk
## Warning: package 'Rglpk' was built under R version 3.4.4
## Loading required package: slam
## Warning: package 'slam' was built under R version 3.4.4
## Using the GLPK callable library version 4.47
## Loading required package: timeSeries
## Warning: package 'timeSeries' was built under R version 3.4.4
## Loading required package: timeDate
## Warning: package 'timeDate' was built under R version 3.4.4
## Financial Risk Modelling and Portfolio Optimisation with R (version 0.4-1)
library(timeSeries)
library(QRM)
## Warning: package 'QRM' was built under R version 3.4.4
## Loading required package: gsl
## Warning: package 'gsl' was built under R version 3.4.4
## Loading required package: Matrix
## Loading required package: mvtnorm
## Warning: package 'mvtnorm' was built under R version 3.4.4
## Loading required package: numDeriv
## Warning: package 'numDeriv' was built under R version 3.4.4
## 
## Attaching package: 'QRM'
## The following object is masked from 'package:base':
## 
##     lbeta
library(fGarch)
## Warning: package 'fGarch' was built under R version 3.4.4
## Loading required package: fBasics
## Warning: package 'fBasics' was built under R version 3.4.4
library(copula)
## Warning: package 'copula' was built under R version 3.4.4
## 
## Attaching package: 'copula'
## The following objects are masked from 'package:gsl':
## 
##     psi, sinc

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

RM<- returnseries(fcpricets[1:3250,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
RA<-returnseries(fcpricets[1:3250, -1], method = "discrete", trim = TRUE)
head(RA)
## GMT
##                   AUD        NZD        BRL        CAD         CNY
## 2006-01-04 -1.1520429 -0.9879413  1.3032146  0.4342162  0.00000000
## 2006-01-05 -0.2272120  0.1163636  0.9206488 -0.8950856  0.02974789
## 2006-01-06 -0.6110521 -0.4921117 -0.2841530 -0.2660944  0.01859589
## 2006-01-09  0.1463350 -0.1878070  1.2168142 -0.3506971 -0.00247939
## 2006-01-10  0.3604806 -0.1730603  0.1595462  0.5417957  0.06822975
## 2006-01-11 -0.8997089 -0.5450373 -0.6866197  0.4231799 -0.08923924
##                   DKK          HKD       INR         JPY         MYR
## 2006-01-04  0.9252508  0.002579547 0.4247708  0.02579314  0.13775564
## 2006-01-05  0.1135571 -0.001289757 0.2914798  0.30182822  0.23633129
## 2006-01-06  0.4366599  0.003869420 0.6317690  1.31935343  0.27692717
## 2006-01-09 -0.7101951  0.030964945 0.2714932 -0.18315018  0.18674136
## 2006-01-10 -0.0274943  0.001290223 0.1359311  0.01744592  0.00000000
## 2006-01-11  0.6151042 -0.003870518 0.5696058  0.59670060 -0.04799616
##                    MXN         NOK        ZAR         SGD         GBP
## 2006-01-04  0.52705153  1.35832240  1.1423935  0.43161094 -1.04616784
## 2006-01-05 -0.03587952 -0.09301191  0.2358491 -0.09716993  0.12524194
## 2006-01-06  0.27456921  0.52266945  1.0021357  0.50048828 -0.74023846
## 2006-01-09  0.03788596 -0.76054881  0.1975309  0.13445789  0.29470105
## 2006-01-10 -0.42065154 -0.76529457 -0.6378803  0.11625773  0.00000000
## 2006-01-11 -0.05090498  0.09972349  0.9327280  0.38697789  0.01133594
##                   KRW        SEK          LKR         CHF        TWD
## 2006-01-04  0.4809137  1.0977488 -0.117485804  1.07022889  0.9290802
## 2006-01-05  0.3922752  0.2036528  0.029380080  0.24275646  1.0325407
## 2006-01-06  0.5054590  0.6042099  0.107843137  0.59078377 -0.5909798
## 2006-01-09  1.2072846 -0.6946154  0.009804883 -0.77380022  0.7836991
## 2006-01-10 -0.4481564 -0.4643663  0.137457045 -0.08590394 -0.2501563
## 2006-01-11  0.4501739  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         DKK 
## -1.86859732 -1.77445935  1.76759296  1.36121107  0.13193568  1.41039722 
##         HKD         INR         JPY         MYR         MXN         NOK 
##  0.01885260  0.63600756  0.33535931  0.52846645  1.48152862  1.78880025 
##         ZAR         SGD         GBP         KRW         SEK         LKR 
##  2.08111745  0.79334984 -1.21896822  1.12823191  1.73818606  0.01077589 
##         CHF         TWD         THB 
##  1.16903633  0.44343416  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<median(Beta)

15) Create a variable “WBeta” which represents inverse log-weighted

and scaled portfolio weights of each selected currency.

WBeta<-1/log(Beta)
## Warning 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<median(Lambda)

17) Create a variable “WTD” which represents inverse log-weighted

and scaled portfolio weights of each selected currency based on

low tale dependency selction criteria.

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 38 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         CNY
## 2006-01-04 -1.1520429 -0.9879413  1.3032146  0.4342162  0.00000000
## 2006-01-05 -0.2272120  0.1163636  0.9206488 -0.8950856  0.02974789
## 2006-01-06 -0.6110521 -0.4921117 -0.2841530 -0.2660944  0.01859589
## 2006-01-09  0.1463350 -0.1878070  1.2168142 -0.3506971 -0.00247939
## 2006-01-10  0.3604806 -0.1730603  0.1595462  0.5417957  0.06822975
## 2006-01-11 -0.8997089 -0.5450373 -0.6866197  0.4231799 -0.08923924
##                   DKK          HKD       INR         JPY         MYR
## 2006-01-04  0.9252508  0.002579547 0.4247708  0.02579314  0.13775564
## 2006-01-05  0.1135571 -0.001289757 0.2914798  0.30182822  0.23633129
## 2006-01-06  0.4366599  0.003869420 0.6317690  1.31935343  0.27692717
## 2006-01-09 -0.7101951  0.030964945 0.2714932 -0.18315018  0.18674136
## 2006-01-10 -0.0274943  0.001290223 0.1359311  0.01744592  0.00000000
## 2006-01-11  0.6151042 -0.003870518 0.5696058  0.59670060 -0.04799616
##                    MXN         NOK        ZAR         SGD         GBP
## 2006-01-04  0.52705153  1.35832240  1.1423935  0.43161094 -1.04616784
## 2006-01-05 -0.03587952 -0.09301191  0.2358491 -0.09716993  0.12524194
## 2006-01-06  0.27456921  0.52266945  1.0021357  0.50048828 -0.74023846
## 2006-01-09  0.03788596 -0.76054881  0.1975309  0.13445789  0.29470105
## 2006-01-10 -0.42065154 -0.76529457 -0.6378803  0.11625773  0.00000000
## 2006-01-11 -0.05090498  0.09972349  0.9327280  0.38697789  0.01133594
##                   KRW        SEK          LKR         CHF        TWD
## 2006-01-04  0.4809137  1.0977488 -0.117485804  1.07022889  0.9290802
## 2006-01-05  0.3922752  0.2036528  0.029380080  0.24275646  1.0325407
## 2006-01-06  0.5054590  0.6042099  0.107843137  0.59078377 -0.5909798
## 2006-01-09  1.2072846 -0.6946154  0.009804883 -0.77380022  0.7836991
## 2006-01-10 -0.4481564 -0.4643663  0.137457045 -0.08590394 -0.2501563
## 2006-01-11  0.4501739  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?