16.9. Assignment 2#
16.10. Write your name and simon email#
Please write names below
[Name, student ID, email]:
[Name, student ID, email]:
[Name, student ID, email]:
16.11. Exercises#
We will start by redoing the dataset construction of Assignment 1. ( Feel free to use your previous code!)
Start by importing pandas, numpy, maplotlib, and loading the data set.
The dataset has address
url='https://github.com/amoreira2/Fin418/raw/refs/heads/main/assets/data/Retuns50stocks.xlsx'
I strongly recommend you download first and look at the data set.
You should use read_excel
to get the data that contains t0 stocks plus the market
See here:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html .
Do the followings:
Import this dataframe as
df
Use “skip_rows” to skip the initial rows before the data.
Figure out what is the code for missing value and change the option
na_values
appropriately
Change the name of the column with the date information to “Date”
Use
to_datetime
so python understand the column date as a datetime object (you will have to use the option format)Set date as index
convert the date from the start of the month to end of the month.
Drop any date with a missing observations (it will be just one date)
Find the market in the columns and save the stocks in a data frame (df) and the market in a different data frame called df_market
# your code below
Exercise 1. Portfolio Moments
Construct a portfolio that equal weights the first five stocks. Compute this portfolio variance in this sample.
# your code below
Exercise 2. Annualize it
Report this number in yearly units
Exercise 3. Function 1
Construct a function that takes as input the number of stocks in the portfolio, lets call that parameter N, and outputs the (yearly) variance of the portfolio that equal weight the first N stocks
# your code below
Exercise 4. Function 2
Construct a function that takes as input the number of stocks in the portfolio and also, lets call that parameter N, and outputs the (yearly) variance of the portfolio that equal weight N randomly picked stocks
# your code below
Exercise 5. A simulation
Construct a function that that takes as an input the number of stocks N and then use the function above (excercise 4) to simulate 100 different portfolios and output the average of these 100 portfolios
# your code below
Exercise 6. A plot
Call the function above for N=1 to N=50 and plot the variance as function of N
# your code below
Exercise 7. the market
Add the market variance to the plot as an horizontal line to the plot you made above
# your code below
Exercise 8. Interpretation
Explain what you see in the plot. Try to articulate what you think is happening
your answer here
Exercise 9. A decomposition
We now want to build a function that outputs only the terms due to the variance terms
That is, instead of computing \(Var(WR)=W@Var(R)@W=\sum_i\sum_jW_iW_jCov(R_i,R_j)\) which gives you the variance of a portfolio with weight W,
I want you do compute \(\sum_iW_i^2Var(R_i)\), this component of the portfolio variance that comes only from the variance terms
Then subtract this term from the total variance term
Show the three lines in a plot as functions of N (1. the total variance (and also the market like in exercise 7), 2. the component coming only from the variance terms, and 3. the residual (the component due to the covariance terms) )
# your code below
Exercise 10. Interpretation 2
looking at this new plot, what do you learn? What does that imply about portfolio construction?
your answer here
Exercise 11. Factor betas
Randomly select 10 stocks from df
and draw a figure that plots the returns of these industries along with the market return.
Just by looking, which seems seems to have the highest beta?
Why is it important to measure beta correctly? i.e. explain how you can use beta to improve a trade that you have in one of these companies
# Your code below
Your answer here
Exercise 12. Factor betas 2
Run regressions of all stocks in df
on the market return you saved in df_market
. Include intercepts in the regressions.
Just follow the code below to run regressions.
The coefficients to the market return are the betas to each industry.
Now suppose you are a fund manager and you have a mandate to keep your beta equal to 0.5.
Provide portfolio weights based on the regressions above to hit the mandate beta.
Give at least five such portfolios that satisfy that restriction. At least one of these portfolios has to play in more than one stock at a time.
import statsmodels.api as sm
X = # Put your market return here
X = sm.add_constant(X) # Adds a constant term to the predictor
y = # Put industry returns here (perform regressions one-by-one)
model = sm.OLS(y, X).fit(dropna=True)
print(model.summary())
# Your code below
Exercise 13. Factor risk
Find beta-hedged portfolio returns using the five mandate portfolios you provided in Exercise 12.
Draw a plot drawing these portfolio returns together.(i.e. spy in the x-axis, hedge portfolio in the y)
Do they show any co-movement? Can you say they are all “risk free”? In what sense they are free of risk and what sense they are not?
# Your code below
Your answer here
Exercise 14. Alphas
Let us assume that the risk-free rate is fixed at 0. (recall that we are working with returns here, so the intercept is not alpha, but alpha+(1-beta)(average rf))
Then, the intercepts to regression in Exercise 12 can be interpreted as alphas
Find the largest alpha stock and the lowest alpha stock from Exercise 12.
If you were trying to pick one trade, would picking up the asset with the highest alpha the best that we can do?
What other consideration might be relevant for your “best” strategy
# Your code below
Your answer here
Exercise 15. optional
Suppose you pick the portfolio that is best according to the criteria you outline above. Is that the best that we can do? Discuss how you could further improve your portfolio