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:
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.
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
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.
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 sixdecimal 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 sixdecimal 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_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)
What is the value of the firstweekly log returns in gold that is not “NA”?
Enter the answer using sixdecimal 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.
What is the value of the lastyearly discrete returns in gold?
Enter the answer using sixdecimal 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.
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 sixdecimal 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 sixdecimal 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.
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 sixdecimal 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.
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 sixdecimal 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.