17.20. Assignment 4#

Instructions: This problem set should be done in group!!

Make sure you have a group assignet in blackboard otherwise submission will not work

Only the LAST submission by a group member will be graded!

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.

17.21. Write your name and simon email#

Please write names below

  • [Name]:

  • [email]:

17.22. Discussion Forum Assignment#

Do the CAPM assigment on the discussion forum

https://edstem.org/us/courses/30665/discussion/1987508

17.23. Exercises#

Exercise 1, import data

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).

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option('display.max_rows',10,'display.precision',2)  # display 10 rows and 2 decimals 

# your code below

Exercise 2, clean data

Do the followings to clean the data.

  1. use the function pd.to_datetime with the option format= to transform the column date into date format

  2. set the column date as the new index

  3. Drop the ‘Market’ column (sometimes extra columns with “unnamed” column names are also imported. Drop those as well).

  4. print the first 5 rows

# your code below

Exercise 3

  1. For each stock, compute the full sample time-series standard deviation of returns

  2. For each date, compute the average return across stocks

Hint: use the axis option

# For each stock, compute the full sample time-series standard deviation of returns
# For each date, compute the average return across stocks

Exercise 4

  1. 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.

  2. Call equalweights(5) to verify that it works. Output should look like array([0.2, 0.2, 0.2, 0.2, 0.2])

def equalweights(N):
    # your code below
    
    
equalweights(5)

Exercise 5

  1. Create a function that takes a positive integer N and dataframe df and returns a data frame with only the first N columns. Name this function firstNstocks.

  2. Call firstNstocks(5) to verify that it works.

def firstNstocks(N,df=df):
    # your code below
    
    
firstNstocks(5)

Exercise 6

Using the two functions you created above, compute the portfolio returns of an equal weighted portfolio that invests in the first 5 stocks.

Hint: 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=\sum_i^N w_{i}R_{i,t}.\]
# the dimension need to match: (180,5) & (5,)

Exercise 7

Now use the code above to create a function that takes an integer and a dataframe df and returns full sample standard deviation of an equal weighted portfolio formed with the first N stocks. Call this function portfolio_std. Call the function with an N=5 to verify that it works.

def portfolio_std(N,df=df):
    # your code below

portfolio_std(5)

Exercise 8

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:

  1. create an empty dataframe with index given by range(5,50): Results=pd.DataFrame([],index=range(5,51),columns=['std'])

  2. loop over the index of Results, use the function portforlio_std to calculate the standard deviation, and save the output

Results=pd.DataFrame([],index=range(5,51),columns=['std'])
for n in Results.index:
    # your code below
    
Results

Exercise 9

Plot the standard deviaitons on the y-axis and the number of stocks on x-axis. If you created your Results dataframe correctly, this step should be easy.

# your code below

Exercise 10

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.

# your comments below

In the following exercises, we will decompose the fraction of the portfolio variance that comes from individual variances and covariances.

Exercise 11

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 column Var.

# your code below

Results.head()

Exercise 12

Now we will compute the total variation due to individual variances.

Variance of a portfolio is

\[Var(\sum_i w_i R_i)=\sum_i\sum_j w_iw_jcov(R_i,R_j)\]

Which can be written as

\[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(\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}\]

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 were 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 computes this value of each portfolio formed from 5 to 50 stocks and store it in the new column Var_share of the dataframe Results.

for n in Results.index:
    # your code below
    
Results.head()

Exercise 13

Now compute the covariance share by subtrating the variance share from the total variance, store it in Results with the column name Cov_share.

# your code below

Results.head()

Exercise 14

Now add two additional columns with the ratios of the Var_share and Cov_share to the total Variance. Call them Var_ratio and Cov_ratio. Plot these two new columns as a function of the number of stocks in each portfolio (y-axis should be ratios and x-axis should be the number of stocks).

# your code below

Exercise 15

Comment on the shape of the plots.

# your comments below

Exercise 16

  1. create a dataframe covm that includes the covariance matrix across all 50 stocks.

  2. create a matrix that has the variances on the diagonal of the covariance matrix you just created, but has zeros as the off-diagonal elements.

Hint: use np.diag

# your code below