程序代写代做代考 python This excercise is written in python 3

This excercise is written in python 3

FINM 8006 Assignment 2¶
Due 14 May 2019

Instructions¶
• You can completing your assignment directly in this notebook (just insert cells in the .ipynb file). You should have Jupyter notebook installed in order to use this.
• There are some code helpers in the notebook, you don’t necessary need to follow. You can insert as many cells as you want.
• Please convert your notebook to html (‘jupyter nbconvert AS1GroupX.ipynb’) and upload the html file for submission. Your notebook must contain the complete codes.
In [1]:
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
from statsmodels.iolib.summary2 import summary_col
import matplotlib.pyplot as plt
%matplotlib inline
pd.core.common.is_list_like = pd.api.types.is_list_like
import pandas_datareader.data as web
import datetime
from scipy import stats
import random

Data¶

The data includes 200 stocks daily and monthly returns, their annual book value of equity, and factor returns.

• daily return.
In [2]:
dsf = pd.read_pickle(‘dsf_random_200.pkl’)
dsf.head()
Out[2]:

permno
date
ret
0
10145.0
1984-01-03
-0.002242
1
10145.0
1984-01-04
-0.008989
2
10145.0
1984-01-05
0.009070
3
10145.0
1984-01-06
-0.020225
4
10145.0
1984-01-09
-0.004587

• monthly return, prc is price, shrout is share outstanding in 1000s.
In [3]:
msf = pd.read_pickle(‘msf_random_200.pkl’)
msf[‘month’] = msf[‘date’].dt.to_period(‘M’)
msf.head()
Out[3]:

permno
date
ret
prc
shrout
month
0
10145.0
1984-01-31
-0.056054
52.625
53459.0
1984-01
1
10145.0
1984-02-29
-0.066983
48.500
53459.0
1984-02
2
10145.0
1984-03-30
0.061856
51.500
53459.0
1984-03
3
10145.0
1984-04-30
0.036408
53.375
53459.0
1984-04
4
10145.0
1984-05-31
-0.049415
33.375
80189.0
1984-05

• daily and monthly factors.
In [4]:
ffdaily = pd.read_pickle(‘ffdaily.pkl’)
ffdaily.head()
Out[4]:

date
mktrf
smb
hml
rf
0
1984-01-03
-0.0047
0.0049
0.0010
0.00036
1
1984-01-04
0.0146
-0.0067
-0.0006
0.00036
2
1984-01-05
0.0126
-0.0013
-0.0038
0.00036
3
1984-01-06
0.0042
0.0041
0.0006
0.00036
4
1984-01-09
-0.0022
0.0029
0.0047
0.00036
In [5]:
ffmonthly = pd.read_pickle(‘ffmonthly.pkl’)
ffmonthly.head()
Out[5]:

date
mktrf
smb
hml
rf
dateff
0
1984-01-01
-0.0192
-0.0043
0.0763
0.0076
1984-01-31
1
1984-02-01
-0.0482
-0.0171
0.0336
0.0071
1984-02-29
2
1984-03-01
0.0063
0.0007
0.0048
0.0073
1984-03-30
3
1984-04-01
-0.0051
-0.0120
0.0129
0.0081
1984-04-30
4
1984-05-01
-0.0597
0.0003
0.0026
0.0078
1984-05-31

• Book value of equity data, datadate is the date of earning period end.
In [6]:
# be is book equity, you can ignore gvkey, which is an identifier.
# be is in millions
comp = pd.read_pickle(‘be_random_200.pkl’)
comp.head()
Out[6]:

gvkey
datadate
be
permno
429
001300
1983-12-31
2403.0
10145.0
430
001300
1984-12-31
2707.0
10145.0
431
001300
1985-12-31
5794.0
10145.0
432
001300
1986-12-31
3677.0
10145.0
433
001300
1987-12-31
3129.0
10145.0

Value and Size Premium¶

Use monthly return and merge in book equity data, you form portfolio at the end of June each year. Use last year book equity and last December market value to calculate BTM, and use last December market value of equity as size measure. For each month, sort stocks into quintile according to BTM and Size independently.

• Q1 (10Pt)
Run CAPM regression for quintiles of size-sort portfolio and value-sort portfolio, and calculate CAPM alphas. Compare alphas of top and bottom portfolios of the sortings with two-sample T-test. Is there size premium and value premium in this sample?
In [ ]:

IVOL¶

Idiosyncratic volatility (IVOL) of the month can be measured from volatility of residuals from a daily data regression of a factor model. You should use lag idiosyncratic volatility as expected idiosyncratic volatility in this exercise.

• Q2.1 (10pt)
Suppose the model you use is a 3 factor model. Sort stocks in each month into decile according to IVOL, plot the portfolio return against IVOL. How does returns change according to IVOL?
In [ ]:

• Q2.2 (10pt)
Characteristic regression. Run cross sectionally for each month of stock returns on stock beta, log(size), BTM and IVOL, then do a T-test on the series of IVOL coefficients, how is IVOL priced?
In [ ]:

• Q2.3 (5pt)
Form hedging portfolio by longing highest decile and shorting lowerest decile. What’s the mean return of your hedging portfolio? Is your hedging portfolio profitable?
In [ ]:

• Q2.4 (15pt)
Let your hedging portfolio return be your new IVOL factor. Do a Fama-Macbeth regression of individual stock returns on market return factor, SMB, HML and the new IVOL factor, test if the IVOL factor is positive and significant.
In [ ]:

• Q2.5 (10pt)
What does traditional theory say about the pricing of IVOL? How should IVOL be priced if investors cannot form diversified portfolio all the time due to some constraits?
In [ ]: