Imports

Importing quantmod library for analyzing FRED data.

library(quantmod)
Loading required package: xts
Loading required package: zoo

Attaching package: 'zoo'
The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
Loading required package: TTR
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
library(ggplot2)
library(purrr)

Retrieving Symbol from FRED

We are going to work with WILSHIRE5000 a portfolio of stock / market indice which contains every stock that trades in equity market. WILSHIRE5000 is :

  • VALUATED:

    #stocks for any indice i Market Cap of Stock i \text{#stocks for any indice i } \propto \text{ Market Cap of Stock i}

  • For Risk Management we are interested in short term movement of our portfolio , especially during the downturn: log(indice), indicating changes in Total Return/daily, goes down from previous value.

wilsh <- getSymbols("WILL5000IND", src='FRED', auto.assign = FALSE); wilsh
           WILL5000IND
1970-12-31        1.00
1971-01-01          NA
1971-01-04          NA
1971-01-05          NA
1971-01-06          NA
1971-01-07          NA
1971-01-08          NA
1971-01-11          NA
1971-01-12          NA
1971-01-13          NA
       ...            
2023-04-28      206.20
2023-05-01      206.15
2023-05-02      203.55
2023-05-03      202.36
2023-05-04      200.85
2023-05-05      204.68
2023-05-08      204.81
2023-05-09      203.97
2023-05-10      204.88
2023-05-11      204.45

Removing na’s

wilsh <- na.omit(wilsh); wilsh
           WILL5000IND
1970-12-31        1.00
1971-01-29        1.05
1971-02-26        1.07
1971-03-31        1.12
1971-04-30        1.16
1971-05-28        1.12
1971-06-30        1.13
1971-07-30        1.09
1971-08-31        1.13
1971-09-30        1.12
       ...            
2023-04-28      206.20
2023-05-01      206.15
2023-05-02      203.55
2023-05-03      202.36
2023-05-04      200.85
2023-05-05      204.68
2023-05-08      204.81
2023-05-09      203.97
2023-05-10      204.88
2023-05-11      204.45

Clearly first few years have one point per month. Based on we will choose a duration to retrieve data where we have daily data available on weekdays starting from 1980 upto December 2017 end. Reason for this range is to ensure reproducible results for our calculations.

wilsh <- wilsh["1979-12-31/2017-12-31"]; wilsh
           WILL5000IND
1979-12-31        1.90
1980-01-02        1.86
1980-01-04        1.88
1980-01-07        1.89
1980-01-08        1.93
1980-01-09        1.93
1980-01-10        1.95
1980-01-11        1.95
1980-01-14        1.96
1980-01-15        1.97
       ...            
2017-12-15      123.53
2017-12-18      124.31
2017-12-19      123.85
2017-12-20      123.80
2017-12-21      124.10
2017-12-22      124.03
2017-12-26      123.94
2017-12-27      124.04
2017-12-28      124.33
2017-12-29      123.67

We will rename series to TR for Total Return.

names(wilsh) <- "TR"; wilsh
               TR
1979-12-31   1.90
1980-01-02   1.86
1980-01-04   1.88
1980-01-07   1.89
1980-01-08   1.93
1980-01-09   1.93
1980-01-10   1.95
1980-01-11   1.95
1980-01-14   1.96
1980-01-15   1.97
       ...       
2017-12-15 123.53
2017-12-18 124.31
2017-12-19 123.85
2017-12-20 123.80
2017-12-21 124.10
2017-12-22 124.03
2017-12-26 123.94
2017-12-27 124.04
2017-12-28 124.33
2017-12-29 123.67
head(wilsh, 3)
             TR
1979-12-31 1.90
1980-01-02 1.86
1980-01-04 1.88
tail(wilsh, 3)
               TR
2017-12-27 124.04
2017-12-28 124.33
2017-12-29 123.67
colnames(wilsh)
[1] "TR"
index(wilsh)[1:3]
[1] "1979-12-31" "1980-01-02" "1980-01-04"
df <- data.frame(date=index(wilsh), TR=as.numeric(wilsh$TR)); df

Plotting in logarithmic scale because we are largely interested in drops of changes in TR in short term for risk management.

df |> ggplot(mapping = aes(x=date, y=TR)) + 
  geom_line() +
  scale_y_log10() + 
  labs(title="WILSHIRE5000  Total Market Index(in Log Scale)", y="TR(Log Scale)")

Exercise 2

Following instruction loads gold data required for exercise #2

load("D:/rahuketu/PPV/S_Self_Study/FinancialRiskManagementR/W1_Exercise2_FRED_gold.gz")

Question 1 What is the date of the first observation of this series?


Question 2

What is the value of the first observation of this series?

head(gold, 1)
              TR
1980-01-02 559.5

Question 3

What is the date of the last observation of this series?

Enter the answer using  the format YYYY-MM-DD


Question 4

What is the value of the last observation of this series?

Enter the answer using two decimal places, i.e., nnn.nn (where n is an integer).

tail(gold, 1)
             TR
2017-12-28 1291

Calculating Daily Returns

Holding period is defined as single business day in below calculations.

  • Discrete Return : rett=wilshtwilsht11ret_t = \frac{wilsh_t}{wilsh_{t-1}} -1

  • Continuous Return :

    logrett=log(wilsht)log(wilsht1)logret_{t} = log(wilsh_{t}) - log(wilsh_{t-1})

    rett=elogrett1ret_{t} = e^{logret_{t}} -1

  • You can’t loose more than a 100% ret is in range (-1, infinity)

logret <- diff(log(wilsh))[-1]; logret
                      TR
1980-01-02 -0.0212773984
1980-01-04  0.0106952891
1980-01-07  0.0053050522
1980-01-08  0.0209431738
1980-01-09  0.0000000000
1980-01-10  0.0103093697
1980-01-11  0.0000000000
1980-01-14  0.0051151007
1980-01-15  0.0050890695
1980-01-16  0.0000000000
       ...              
2017-12-15  0.0091079755
2017-12-18  0.0062944043
2017-12-19 -0.0037072899
2017-12-20 -0.0004037957
2017-12-21  0.0024203320
2017-12-22 -0.0005642204
2017-12-26 -0.0007258943
2017-12-27  0.0008065167
2017-12-28  0.0023352267
2017-12-29 -0.0053225932
ret <-  exp(logret) -1; ret
                      TR
1980-01-02 -0.0210526316
1980-01-04  0.0107526882
1980-01-07  0.0053191489
1980-01-08  0.0211640212
1980-01-09  0.0000000000
1980-01-10  0.0103626943
1980-01-11  0.0000000000
1980-01-14  0.0051282051
1980-01-15  0.0051020408
1980-01-16  0.0000000000
       ...              
2017-12-15  0.0091495793
2017-12-18  0.0063142556
2017-12-19 -0.0037004264
2017-12-20 -0.0004037142
2017-12-21  0.0024232633
2017-12-22 -0.0005640612
2017-12-26 -0.0007256309
2017-12-27  0.0008068420
2017-12-28  0.0023379555
2017-12-29 -0.0053084533
Note

[-n] bit in code in R means exclude the row at nth element starting with first index as 1.

round(head(logret,3), 6)
                  TR
1980-01-02 -0.021277
1980-01-04  0.010695
1980-01-07  0.005305
round(head(ret,3), 6)
                  TR
1980-01-02 -0.021053
1980-01-04  0.010753
1980-01-07  0.005319
Note

Values of ret(discretely compounded) and logret(continuously compounded) are very close.

get_df <- function(tbl){
  df <- data.frame(date=index(tbl), TR=as.numeric(tbl$TR))
  return(df)
}
ret |> 
  get_df() |> 
  ggplot(mapping = aes(x = date, y=TR)) + 
  geom_line() 

logret |> 
  get_df() |> 
  ggplot(mapping = aes(x = date, y=TR)) + 
  geom_line() 

Exercise 3

logret_gold <- diff(log(gold))[-1]; logret_gold
                      TR
1980-01-03  0.1250054267
1980-01-04 -0.0753220065
1980-01-07  0.0745330519
1980-01-08 -0.0378010426
1980-01-09 -0.0046007311
1980-01-10 -0.0071898165
1980-01-11  0.0328781092
1980-01-14  0.0576933162
1980-01-15  0.0357180826
1980-01-16  0.1053605157
       ...              
2017-12-12 -0.0050240254
2017-12-13  0.0014092732
2017-12-14  0.0066970354
2017-12-15  0.0028735652
2017-12-18  0.0047710014
2017-12-19 -0.0001983379
2017-12-20  0.0033268675
2017-12-21  0.0000000000
2017-12-27  0.0116748904
2017-12-28  0.0090258938
ret_gold <- exp(logret_gold) -1; ret_gold
                      TR
1980-01-03  0.1331546023
1980-01-04 -0.0725552050
1980-01-07  0.0773809524
1980-01-08 -0.0370955012
1980-01-09 -0.0045901639
1980-01-10 -0.0071640316
1980-01-11  0.0334245666
1980-01-14  0.0593900482
1980-01-15  0.0363636364
1980-01-16  0.1111111111
       ...              
2017-12-12 -0.0050114261
2017-12-13  0.0014102667
2017-12-14  0.0067195107
2017-12-15  0.0028776978
2017-12-18  0.0047824008
2017-12-19 -0.0001983183
2017-12-20  0.0033324077
2017-12-21  0.0000000000
2017-12-27  0.0117433079
2017-12-28  0.0090667500

Question 1

What is the date of the first observation of the daily log returns in gold that is not “NA”?

Enter the answer using  the format YYYY-MM-DD.


Question 2

What is the value of the first observation of the daily log returns in gold that is not “NA”?

Enter the answer using six decimal places, i.e., n.nnnnnn (where n is an integer). If this is a negative number, please add a minus (“-”) sign in front. If this is a positive number, there is no need to add a plus sign in front.

round(head(logret_gold, 1), 6)
                 TR
1980-01-03 0.125005

Question 3

What is the date of the last observation of the daily discrete returns in gold?

Enter the answer using  the format YYYY-MM-DD.

Question 4

What is the value of the last observation of the daily discrete returns in gold?

Enter the answer using six decimal places, i.e., n.nnnnnn (where n is an integer). If this is a negative number, please add a minus (“-”) sign in front. If this is a positive number, there is no need to add a plus sign in front.

round(tail(ret_gold, 1), 6)
                 TR
2017-12-28 0.009067

Calculating Longer returns

  • logret(nth day, i-days return)=i=0n1logretni+1logret(\text{nth day, i-days return}) = \sum_{i=0}^{n-1}logret_{n-i+1}

  • ret(nth day, i-days return)=elogret(nth day, i-days return)1ret(\text{nth day, i-days return}) = e^{logret(\text{nth day, i-days return})} -1

logret_1 <- function(tbl){
  return(diff(log(tbl))[-1])
}

aggr_fn <-  function(tbl, fn){
  result <- fn(tbl, sum)
  return(result)
}

ret <- function(logret){
  return(exp(logret)-1)
}

aggr_fn(tbl=wilsh, fn=apply.weekly)
               TR
1980-01-04   5.64
1980-01-11   9.65
1980-01-18   9.84
1980-01-25  10.01
1980-02-01  10.19
1980-02-08  10.31
1980-02-15  10.44
1980-02-22   8.24
1980-02-29  10.15
1980-03-07   9.88
       ...       
2017-10-27 591.63
2017-11-03 594.27
2017-11-10 595.96
2017-11-17 594.13
2017-11-24 479.42
2017-12-01 607.34
2017-12-08 609.12
2017-12-15 614.66
2017-12-22 620.09
2017-12-29 495.98
wilsh |> logret_1() |> head(n=3)
                     TR
1980-01-02 -0.021277398
1980-01-04  0.010695289
1980-01-07  0.005305052
logret_w <- partial(aggr_fn, fn = apply.weekly)
wilsh |> logret_1() |> logret_w()|> head()
                    TR
1980-01-04 -0.01058211
1980-01-11  0.03655760
1980-01-18  0.01020417
1980-01-25  0.02010118
1980-02-01  0.01970507
1980-02-08  0.02409755
logret_q <- partial(aggr_fn, fn = apply.quarterly)
wilsh |> logret_1() |> logret_q()|> head()
                    TR
1980-03-31 -0.05963827
1980-06-30  0.14049036
1980-09-30  0.11886120
1980-12-31  0.09059690
1981-03-31  0.01562532
1981-06-30  0.00000000
logret_m <- partial(aggr_fn, fn = apply.monthly)
wilsh |> logret_1() |> logret_m()|> head()
                     TR
1980-01-31  0.066181907
1980-02-29  0.004914015
1980-03-31 -0.130734188
1980-04-30  0.049056157
1980-05-30  0.056862862
1980-06-30  0.034571344
logret_a <- partial(aggr_fn, fn = apply.yearly)
wilsh |> logret_1() |> logret_a()|> head()
                    TR
1980-12-31  0.29031019
1981-12-31 -0.04016604
1982-12-31  0.17271270
1983-12-30  0.21065206
1984-12-31  0.03026366
1985-12-31  0.28156585
wilsh |> logret_1() |> logret_a() |> ret() |> head()
                    TR
1980-12-31  0.33684211
1981-12-31 -0.03937008
1982-12-31  0.18852459
1983-12-30  0.23448276
1984-12-31  0.03072626
1985-12-31  0.32520325

Exercise 4


Question 1

What is the value of the first weekly log returns in gold that is not “NA”?

Enter the answer using six decimal places, i.e., n.nnnnnn (where n is an integer). If this is a negative number, please add a minus (“-”) sign in front. If this is a positive number, there is no need to add a plus sign in front.

gold |> logret_1() |> logret_w()|> head(n=1) |> round(digits=6)
                 TR
1980-01-04 0.049683

Question 2

What is the date of the first monthly log returns in gold?

Enter the answer using  the format YYYY-MM-DD.

gold |> logret_1() |> logret_m()|> head(n=1) |> round(digits=6)
                 TR
1980-01-31 0.154534

Question 3

What is the date of the first quarterly discrete returns in gold?

Enter the answer using  the format YYYY-MM-DD.

gold |> logret_1() |> logret_q()|> ret() |> head(n=1) |> round(digits=6)
                  TR
1980-03-31 -0.116175

Question 4

What is the value of the last  yearly discrete returns in gold?

Enter the answer using six decimal places, i.e., n.nnnnnn (where n is an integer). If this is a negative number, please add a minus (“-”) sign in front. If this is a positive number, there is no need to add a plus sign in front.

gold |> logret_1() |> logret_a()|> ret() |> tail(n=1) |> round(digits=6)
                 TR
2017-12-28 0.126625

Quiz 1

Retrieving the series

us2yen <- getSymbols("DEXJPUS", src='FRED', auto.assign = FALSE);us2yen
           DEXJPUS
1971-01-04  357.73
1971-01-05  357.81
1971-01-06  357.86
1971-01-07  357.87
1971-01-08  357.82
1971-01-11  357.95
1971-01-12  358.06
1971-01-13  358.44
1971-01-14  358.40
1971-01-15  358.40
       ...        
2023-04-24  134.41
2023-04-25  133.96
2023-04-26  133.72
2023-04-27  134.04
2023-04-28  135.99
2023-05-01  137.35
2023-05-02  136.46
2023-05-03  135.31
2023-05-04  133.76
2023-05-05  134.85
us2yen <- na.omit(us2yen); us2yen
           DEXJPUS
1971-01-04  357.73
1971-01-05  357.81
1971-01-06  357.86
1971-01-07  357.87
1971-01-08  357.82
1971-01-11  357.95
1971-01-12  358.06
1971-01-13  358.44
1971-01-14  358.40
1971-01-15  358.40
       ...        
2023-04-24  134.41
2023-04-25  133.96
2023-04-26  133.72
2023-04-27  134.04
2023-04-28  135.99
2023-05-01  137.35
2023-05-02  136.46
2023-05-03  135.31
2023-05-04  133.76
2023-05-05  134.85
us2yen <- us2yen["1979-12-31/2017-12-31"]; us2yen
           DEXJPUS
1979-12-31  240.30
1980-01-02  238.45
1980-01-03  238.35
1980-01-04  234.80
1980-01-07  231.55
1980-01-08  234.75
1980-01-09  234.95
1980-01-10  235.80
1980-01-11  236.05
1980-01-14  236.20
       ...        
2017-12-15  112.68
2017-12-18  112.40
2017-12-19  112.98
2017-12-20  113.29
2017-12-21  113.42
2017-12-22  113.30
2017-12-26  113.20
2017-12-27  113.28
2017-12-28  112.85
2017-12-29  112.69
yen2us <- 1/us2yen; yen2us
               DEXJPUS
1979-12-31 0.004161465
1980-01-02 0.004193751
1980-01-03 0.004195511
1980-01-04 0.004258944
1980-01-07 0.004318722
1980-01-08 0.004259851
1980-01-09 0.004256225
1980-01-10 0.004240882
1980-01-11 0.004236391
1980-01-14 0.004233700
       ...            
2017-12-15 0.008874689
2017-12-18 0.008896797
2017-12-19 0.008851124
2017-12-20 0.008826904
2017-12-21 0.008816787
2017-12-22 0.008826125
2017-12-26 0.008833922
2017-12-27 0.008827684
2017-12-28 0.008861320
2017-12-29 0.008873902

Daily Return yen2us

logret

yen2us |> logret_1() |> head()
                 DEXJPUS
1980-01-02  0.0077284980
1980-01-03  0.0004194631
1980-01-04  0.0150060937
1980-01-07  0.0139382550
1980-01-08 -0.0137252852
1980-01-09 -0.0008516075

ret

yen2us |> logret_1() |> ret() |> head()
                 DEXJPUS
1980-01-02  0.0077584399
1980-01-03  0.0004195511
1980-01-04  0.0151192504
1980-01-07  0.0140358454
1980-01-08 -0.0136315229
1980-01-09 -0.0008512449

Questions

Question 1

What is the date of the first observation of the daily log returns that is not “NA”?

Enter the answer using the format YYYY-MM-DD.


Question 2

What is the value of the first observation of the daily log returns that is not “NA”?

Enter the answer using six decimal places, i.e., n.nnnnnn (where n is an integer). If this is a negative number, please add a minus (“-”) sign in front. If this is a positive number, there is no need to add a plus sign in front.

yen2us |> logret_1() |> head(n=1) |> round(digits=6)
            DEXJPUS
1980-01-02 0.007728

Question 3

What is the date of the first observation of the monthly discrete returns ?

Enter the answer using the format YYYY-MM-DD.

Question 4

What is the value of the first observation of the monthly discrete returns ?

Enter the answer using six decimal places, i.e., n.nnnnnn (where n is an integer). If this is a negative number, please add a minus (“-”) sign in front. If this is a positive number, there is no need to add a plus sign in front.

yen2us |> logret_1() |> logret_m() |> ret() |> head(n=1) |>  round(digits=6)
            DEXJPUS
1980-01-31 0.004053


Question 5

What is the date of the last observation of the quarterly log returns ?

Enter the answer using the format YYYY-MM-DD.


Question 6

What is the value of the last observation of the quarterly log returns?

Enter the answer using six decimal places, i.e., n.nnnnnn (where n is an integer). If this is a negative number, please add a minus (“-”) sign in front. If this is a positive number, there is no need to add a plus sign in front.

yen2us |> logret_1() |> logret_q() |>  tail(n=1) |> round(digits=6)
             DEXJPUS
2017-12-29 -0.000444


Question 7

What is the date of the last observation of the yearly discrete returns ?

Enter the answer using the format YYYY-MM-DD.

Question 8

What is the value of the last observation of the yearly discrete returns?

Enter the answer using six decimal places, i.e., n.nnnnnn (where n is an integer). If this is a negative number, please add a minus (“-”) sign in front. If this is a positive number, there is no need to add a plus sign in front.

yen2us |> logret_1() |> logret_a() |> ret() |>  tail(n=1) |> round(digits=6)
            DEXJPUS
2017-12-29 0.036294