Load Packages
Resolving package versions...
Updating `~/.julia/environment/v1.2/Project.toml`
[no changes]
Updating `~/.julia/environment/v1.2/Manifest.toml`
[no changes]
Resolving package versions...
Updating `~/.julia/environment/v1.2/Project.toml`
[no changes]
Updating `~/.julia/environment/v1.2/Manifest.toml`
[no changes]
Resolving package versions...
Updating `~/.julia/environment/v1.2/Project.toml`
[no changes]
Updating `~/.julia/environment/v1.2/Manifest.toml`
[no changes]
Resolving package versions...
Updating `~/.julia/environment/v1.2/Project.toml`
[no changes]
Updating `~/.julia/environment/v1.2/Manifest.toml`
[no changes]
Resolving package versions...
Updating `~/.julia/environment/v1.2/Project.toml`
[no changes]
Updating `~/.julia/environment/v1.2/Manifest.toml`
[no changes]
Time Series Analysis
In this problem set we will manipulate time series and perform simple transformations thereof. The following cell will load a time series of Morgan Stanley's, Tesla's and Microsoft's stock price data. Make sure you've put file TeslaMsMsft.csv
in folder Data
.
Task 1 (25 points)
Calculate log-returns of an equally weighted portfolio containing each stock at every point in time as percent per day (that is, a 5.5% return on some day should be expressed as 5.5 and not as 0.055), print out 10 the first and the 10 last observations thereof and plot the whole series on the timeline. Use a TimeArray
object for these computations and the function percentchange()
.
Task 2 (15 points)
Calculate the average daily return of the portfolio that you constructed in Task 1 (in percent per day) on Wednesdays before and after 2015-06-03 (you can include this date in both samples).
Hint: use the time series object that you have created in Task 1.
Average daily return
Before 2015-06-03: -0.110
After 2015-06-03: 0.166
Task 3 (30 points)
Part a)
Write a function that will the calculate rolling downside standard deviation defined as follows:
where is the set of dates when the returns was below the minimum acceptable return (MAR), and is the number of such days. should be estimated as the sample mean without the restriction of only looking at negative returns (calculated in a rolling fashion, using a window of 252 days). As an illustratrive example, compare to the Sortino ratio where is calculated using instead of , see - note that the Sortino ratio is different from the rolling downside standard deviation that you are required to calculate in this example.
Hint 1: First, try experimenting with a MAR of 0.0%.
Hint 2: Use the floating array object stocks_f
to calculate the return on the equally weighted portfolio across the three stocks.
Hing 3: Use a loop to calculate the downside standard deviation in a rolling fashion.
Part b)
Plot the 252-day rolling downside standard deviation versus the 252-day rolling conventional standard deviation. For . Convert log returns to percentages by multiplying them with 100.
Hint 1: use stocks_f
to calculate the return on an equally weighted portfolio across the three stocks for both the conventional rolling standard deviation and for the rolling downside standard deviation.
Hint 2: to create a date axis, use Date.(??,"dd/mm/yyyy")
.
Task 4 (10 points)
Calculate a time series of weekly median, mean, maxima and minima of daily portfolio returns. Index the new series by the beginning of the week. So, the output for the median should resemble this (column names are arbitrary), and similarly for the minimum and maximum:
date | value |
---|---|
... | ... |
2015-12-28 | [median of December 2015 Week4 daily returns] |
2016-01-04 | [median of January 2016 Week1 daily returns] |
... | ... |
Plot the result on the timeline.
Hint: apply the function collapse()
to time series object. This can be useful when dealing with different rebalancing frequencies for trading strategies.
443×5 DataFrame
│ Row │ timestamp │ Median │ Mean │ Max │ Min │
│ │ Date │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼────────────┼──────────────┼──────────────┼─────────────┼─────────────┤
│ 1 │ 2011-01-04 │ 0.00493394 │ 0.00666084 │ 0.022071 │ -0.00529549 │
│ 2 │ 2011-01-10 │ -0.00376679 │ -0.00503772 │ 0.0140007 │ -0.0203044 │
│ 3 │ 2011-01-18 │ -0.0032586 │ -0.00718511 │ 0.0134114 │ -0.0356347 │
│ 4 │ 2011-01-24 │ 0.00101789 │ -8.67064e-5 │ 0.0216992 │ -0.0347594 │
│ 5 │ 2011-01-31 │ -0.000516247 │ 0.00033717 │ 0.00631662 │ -0.00614089 │
│ 6 │ 2011-02-07 │ 0.00217286 │ -0.00117085 │ 0.0232569 │ -0.0277166 │
│ 7 │ 2011-02-14 │ -0.0045857 │ 0.00114239 │ 0.0315025 │ -0.0134715 │
│ 8 │ 2011-02-22 │ 0.0059103 │ -0.00312136 │ 0.0171246 │ -0.0414307 │
│ 9 │ 2011-02-28 │ 0.00182152 │ -0.00111418 │ 0.0101047 │ -0.0144135 │
│ 10 │ 2011-03-07 │ -0.00037457 │ -0.00332594 │ 0.00754612 │ -0.0235829 │
⋮
│ 433 │ 2019-04-15 │ 0.00535715 │ 0.00488494 │ 0.0145813 │ -0.00575588 │
│ 434 │ 2019-04-22 │ -0.00761874 │ -0.00610785 │ 0.00637285 │ -0.0138491 │
│ 435 │ 2019-04-29 │ 0.0113672 │ 0.00513951 │ 0.0233694 │ -0.0183474 │
│ 436 │ 2019-05-06 │ -0.00432232 │ -0.00753438 │ 0.000105108 │ -0.0249613 │
│ 437 │ 2019-05-13 │ 0.0020786 │ -0.0115878 │ 0.0101163 │ -0.0438535 │
│ 438 │ 2019-05-20 │ -0.0062877 │ -0.00914982 │ 0.00526402 │ -0.0255146 │
│ 439 │ 2019-05-28 │ -0.00671867 │ -0.00865737 │ -0.00103822 │ -0.0201539 │
│ 440 │ 2019-06-03 │ 0.0118653 │ 0.0138742 │ 0.0493237 │ -0.0215199 │
│ 441 │ 2019-06-10 │ 0.0081971 │ 0.00403089 │ 0.0209915 │ -0.0219156 │
│ 442 │ 2019-06-17 │ 0.00435038 │ 0.0055287 │ 0.0139058 │ -0.00463457 │
│ 443 │ 2019-06-24 │ 0.00289738 │ -0.000651402 │ 0.00978499 │ -0.0197159 │
Task 5 (10 points)
This excercise is for ambitious students who would like to receive a high mark. Plot the autocorrelation function (up to lag 100) for the returns on the equally weighted portfolio that you have calculated including 99% confidence bands.
Task 6 (10 points)
Suppose that you want to index the daily returns on your equally weighted portfolio not by the second but by the first day. That is you want the time series to start on 2011-01-03 rather than 2011-01-04, which was done automatically when applying diff(log())
to a time array. The task is to shift all dates by one day backward.
Print out the first and last few rows of the modified time series object using the functions head()
and tail()
.
Hint: start by looking at the Julia documentation of TimeSeries.jl
.