#%% md
请看最后
Assigment 4
Instructions: This problem set should be done individually
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.
#%% md
Write your name and your email
Please write names below
- [Name]:
- [email]:
#%% md
Exercises
#%% md
1. Start by importing pandas, numpy and loading the data set.
The dataset has address
url="https://raw.githubusercontent.com/amoreira2/Lectures/main/assets/data/Retuns50stocks.csv"
First look in your browser what this file looks like and what type it has, then use the appropriate pd.read_XX
function to import it
Name df
this dataset, then print print(df)
.
#%%
#%% md
2. There is a column called date. We would like this to be our index and that pandas recognize it as date. Lets start by transforming it in the date format.
Start by using the function pd.to_datetime
on this column. Did it intepret it correctly?
In the case it did, you are done, simply assign this new variable to the column date
In the case it did not, use the option format=
to specify the format of the data along the lines explained in chapter 6.8.
After you are done, call the method .info()
on the dataframe so we can see if things are right.
#%%
#%% md
3. You might notice that there is a ‘Market’ column that was imported (sometimes extra columns with "unnamed column names are also imported. Drop those as well). Use the method .drop to drop it. Then use .info to show it is dropped.
#%%
#%% md
4. Now set date as the index. After you do that, show the last 5 rows of the dataframe so you can see if things were done correctly.
#%%
#%% md
5. Compute the full sample time-series standard deviation of returns for each stock
#%%
#%% md
6. For each date, compute the mean return across stocks
tip: you might find useful the axis
option
#%%
#%% md
7. Create a function that takes as input a positive integer N and returns an array of N length where each entry is 1/N
Name this function equalweights
.
Call equalweights(5)
so you would see it works if it returns an array of
[
1
5
,
1
5
,
1
5
,
1
5
,
1
5
]
[\frac{1}{5},\frac{1}{5},\frac{1}{5},\frac{1}{5},\frac{1}{5}]
[51,51,51,51,51]
#%%
#%% md
8. Create a function that takes a positive integer N and dataframe DF and returns the a data frame with only the first N columns.
name this function firstNstocks
Call firstNstocks(5)
so you can see it works
#%%
#%% md
9. Using the two function you created above, compute the portfolio returns of an equal weighted portfolio that invests in the first 5 stocks
Tip: the result should be a time-series sequence of returns. You want to use the @
dot product operation and recall that the return of a portfolio is
r t p = W @ R t = ∑ i N w i R i , t r_t^p=W @ R_t=\sum_i^N w_{i}R_{i,t} rtp=W@Rt=i∑NwiRi,t
#%%
#%% md
10. Now use the code above to create a function that take an integer and a dataframe and returns full sample standard deviation of a equal weighted portfolio formed with the first N stocks
call this function portfolio_std
call the function with an N=5 to show it works
#%%
#%% md
11. Use a for loop and the function above to compute the standard deviation of equal weighted portfolios starting with the first 5 stocks, 6 stocks, all the way up to all 50 stocks.
store the result of each loop in a dataframe called Results.Do this as follows: create an empty dataframe with index given by range(5,50)
pd.DataFrame([],index=range(5,50),columns=['std'])
#%%
#%% md
12. Plot the standard deviaitons in the y-axis and the number of stocks in x-axis. If you created your Results dataframe correctly, this step should be easy.
#%%
#%% md
13. Eye-balling the graph, does it look like adding more and more stocks will diversify away all of the standard deviation? Why or why not? Comment on the shape of the function.
#%%
#%% md
In the following exercises, we will now decompose the fraction of the portfolio variance that comes from individual variance and covariances.
#%% md
14. We already have the standard deviation of the portfolios. Create a column in Results with the portfolio variance. This should be easy if you know the connection between standard deviaiton and variance. Call this columns ‘Var’
#%%
#%% md
15. Now we will compute the total variation due to individual variances.
Variance of a portfolio is
V a r ( ∑ i w i R i ) = ∑ i ∑ j w i w j c o v ( R i , R j ) Var(\sum_i w_i R_i)=\sum_i\sum_j w_iw_jcov(R_i,R_j) Var(i∑wiRi)=i∑j∑wiwjcov(Ri,Rj)
Which can be written as
V a r ( ∑ i w i R i ) = ∑ i = j ∑ j w i w j c o v ( R i , R j ) + ∑ i ≠ j ∑ j w i w j c o v ( R i , R j ) Var(\sum_i w_i R_i)=\sum_{i=j}\sum_j w_iw_jcov(R_i,R_j)+ \sum_{i\neq j}\sum_j w_iw_jcov(R_i,R_j) Var(i∑wiRi)=i=j∑j∑wiwjcov(Ri,Rj)+i=j∑j∑wiwjcov(Ri,Rj)
V a r ( ∑ i w i R i ) = ∑ i w i 2 v a r ( R i ) ⏟ variance component + ∑ i ≠ j ∑ j w i w j c o v ( R i , R j ) ⏟ covariance component Var(\sum_i w_i R_i)= \underbrace{\sum_i w_i^2var(R_i)}_\textrm{variance component}+ \underbrace{\sum_{i\neq j}\sum_j w_iw_jcov(R_i,R_j)}_\textrm{covariance component} Var(i∑wiRi)=variance component i∑wi2var(Ri)+covariance component i=j∑j∑wiwjcov(Ri,Rj)
So the first component is variance component and the second is the covariance component.
To see this, just think about what the second component would be if all stocks are uncorrelated.
So lets compute the first term.
Using the function you defined above you can easily construct the vector of sqaured weights equalweights(N)**2
and the vector of first N stocks variances firstNstocks(N).var()
Now simply do the dot product of these objects to create the variance component for the portfolio.
Once you get this to work for one portfolio, simply create a for loop that compute this value of each portfolio formed from 5 to 50 stocks and store it in the new column Var_share
of the dataframe Results
#%%
#%% md
16. Now compute the covariance share by subtrating the variance share from the total variance, store it in Results with the column name Cov_share
#%%
#%% md
17. Now add two addiitonal columns with the ratios of the Var_share and Cov_share by the total Variance. Call this Var_ratio and Cov_ratio. Plot these two new columns as a function of the number of stocks in each portfolios ( y-axis should be ratios and x-axis should be the number of stocks)
#%%
#%% md
18. Comment on the shape of the plots.
#%% md
#%% md
19. Compute the covariance matrix across all 50 stocks
#%%
#%% md
20. Create a matrix that has the variances in the diagonal as the covariance matrix you just created, but has zeros on the off-diagonal terms .
You should use diag
and only use the diag
method on the covariance matrix you just created.
Hint: diag
is from numpy
liabray.
#%%
部分结果展示:
需要支持请联系VX vx_xuxx