Assignment 7
Contents
17.32. Assignment 7#
Instructions: This problem set should be done in a group.
Your group was assigned at orientation and you can find it in blackbaord as well.
You will use this same group to do your final project.
Answer each question in the designated space below.
After you are done, save and upload in blackboard.
Please check that you are submitting the correct file. One way to avoid mistakes is to save it with a different name.
Comments:
Make sure that everything is compiled before saving it.
Obviously feel free to add as many cells as needed in each section.
Cells should not be too large. Keep code organized and clean.
17.32.1. Write your names and simon emails#
Please write names below:
[Name,email]:
[Name,email]:
[Name,email]:
[Name,email]:
[Name,email]:
17.33. Exercises#
Exercise 1
In order to proceed you need the file url="https://github.com/amoreira2/Lectures/blob/main/assets/data/crspm2005_2020.pkl?raw=true"
.
Start by importing the data, parsing the date appropriately.
Using the momentum code we developed in chapter 13.2 as a starting point, I would like you to make it a general function that takes the following parameters as inputs:
lookback period.
how many months to skip between signal and portfolio formation.
number of groups, i.e., the number of portfolios.
weighting scheme: equal-weighted, market-cap-weighted, volume-weighted.
The signal is the cumulative returns in the lookback window.
The code should output a time-series of returns of the portfolio that goes long the top group (the one in the top of the signal distribution) and shorts the bottom group (the one with stocks from the bottom of the signal distribution).
Name this function strategy
.
TIP:
This is the same data set that we used in Chapter 13.1 to build market cap weights. So you should look there how things were setup and of course, the same data set we used in chapter 13.2.
It is your reponsibility to import this properly and adjust to any aspects of the dataset. For example, make sure you take absolute values of price to ensure that market cap is positive.
# your code below
Exercise 2
We will now show that the code works. Do a for loop, where you skip one month (and not two!), and you grow the lookback period from 1 to 60 months. Record the Sharpe Ratio for each of these lookbacks. Plot the SR of the long-short portfolio as a function of the lookback period ( y-axis SR, x-axis lookback period).
Discuss what you found.
TIP: This code is likely to run for several minutes. First do it for a list of several months (e.g., 1, 3, 6, 12 months) and then after verifying that the code works extend it to all months between 1 and 60.
# your code below
# your discussion below
Exercise 3
Repeat what you did in 2.2, but now skipping 2 months
Discuss what you found.
# your code below
# your discussion below
Exercise 4
Do a for loop, where you do the standard momentum strategy (lookback 10, skip 2 months) using the following number of groups: 2,3,5,10,20, and 50 . Record the Sharpe Ratio for each of these. Plot the SR as a function of the number of groups.
Discuss what you found.
# your code below
# your discussion below
Exercise 5
Show that your code works, by doing a for loop, where you do the standard momentum strategy (lookback 10, skip 2, 10 groups) using all the three weighting schemes (Equal weighted, Value weighted and Volume weighted). Record and plot the Sharpe ratio
Discuss what you found.
# your code below
# your discussion below
Exercise 6
Does our strategy beat the CAPM?
The goal here is to construct a function that for a given time-series of a strategy excess returns it estimates the strategy alpha and beta with respect to the market excess return. It then reports the following objects
Annualized average excess returns
Annualized standard deviation
Annualize Sharpe Ratio
CAPM Beta
annualized CAPM alpha
Annualized Appraisal ratio
Maximum Sharpe Ratio obtained by investing optimally in the strategy and the market
Annualized standard deviation of strategy systematic risk
Annualized standard deviation of the strategy idiosyncratic risk
To do so you will need to import also the market portfolio from this data set url="https://raw.githubusercontent.com/amoreira2/Lectures/main/assets/data/MonthlyFactors.csv"
and merge with the time-series of your strategy.
TIPS:
To do that you will need to first construct your portfolios (so you have a time-series of returns for each), and then in the end, once you have the different portfolios in the columns and the dates in the rows, you will merge on date with the data frame that has the market returns.
It is very important that you import the monthly factors data set correctly for the merge to work
We have done that multiple times by now.
also make sure that these returns are in the same units as your portfolio
The function should look like that
output=perfeval(df,factor)
where df is a dataframe containing your strategy excess returns or long-short returns, output is a Dataframe with 9 columns containing the desired outputs (name each column properly), and factor is the dataframe containing the market excess return. Please note that the data has the market returns and you need to compute the market excess return before merging.
The function must merge df and factor, then run an OLS regression, and the output the desired quantities above.
Show that the function works by applying it to the standard Momentum strategy with value-weighted scheme (where you should simply use two functions, one built in question 2.1 to construct the strategy and the function that you just build to do performance evaluation).
# your code below
Exercise 7 Searching for the best strategy
Now you have two functions. The first function construct a strategy, the second evaluates.
Propose an algorithm that searches across the parameters of your strategy function for the best strategy across your dimension of choice. (No need to actually do it)
The perfeval
function outputs several performance metrics. Choose one or combinations of the various metrics. Please explain your choice of metric. Explain briefly the idea behind your search algorithm.
# your answer below
Exercise 8
What is the main pitfall/weakness of the approach taken in Exercise 7 to find the best strategy ?
Explain how you could modify this approach to improve the methodology with respect to this weakness (no need to do it!).
# your answer below
Exercise 9
Can you change the strategy function that you build in Exercise 1 to construct portfolios based on the last N months volatility?
Demonstrating that it works by applying the function to 10 groups using a rolling windown of 24 months to estimate the volatility signal.
# your code below
Exercise 10
Compute the expected returns, volatilities and Sharpe ratios of the 10 portfolios. Discuss your findings.
# your code below
# your discussion below