python金融代写

from __future__ import division
from __future__ import print_function
import numpy as np
from pandas import Series, DataFrame
from pandas.tseries.offsets import MonthEnd
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import statsmodels.api as sm
import statsmodels.formula.api as smf
from scipy.stats import ttest_1samp

import datetime
plt.rc('figure', figsize=(16, 10))
from numpy.linalg import inv
from numpy import dot
import re

FINM 8006 Assignment 2

Due Friday 19, October, 2018

Instructions

  • You have the option of completing your assignment directly in this notebook. You should have Jupyter (conda install jupyter) installed if you want to use this.
  • You can alternatively write your answers in the editor of your choosing (Words, etc.), and run your code in Python or R in a separate file.
  • If you want to use R, I suggest you use R studio and R Markdown
  • Some parts contains hints in code, you don’t neccesary need to follow the exact hint. But please follow the naming convention as suggested.

Quick Jupyter notebook

  • You can add or delete cell as you go
  • Cells can be a Markdown cell in which you write your texts and maths. Your math can be typed in latex symbols using $$ math $$ in a markdown cell.
  • Default cells are code cells in which you run your python code.
  • When you are ready to run a (markdown or code) cell, press RETURN + SHIFT at the same time to run the cell.
  • Jupyter uses Ipython, so that your codes can be run cell by cell.

Set up and Questions

Asset Pricing in Chinese Markets

Many have commented that the Chinese stock market is very different from the more mature markets. In this assignment, you will explore some aspects of the asset pricing in Chinese markets.

Loading data

You are given three data sets from year 2000: ‘csmar_be.csv’ has the December book value (BE) of firms (STKCD). ‘csmarstk0016.csv’ has monthly market value (ME) and return (ret) information of individual stocks. Data is cleaned to exclude micro stoks and penny stocks. ‘csmarmktretrf.csv’ has market and risk free returns. Market return here is proxied by returns on the SSE.

In [2]:
# December Book Equity where BE >0
decbe = pd.read_csv('csmar_be.csv')
decbe.date = pd.to_datetime(decbe.date.astype('str'), format='%Y-%m-%d') 
decbe = decbe.sort_values(["STKCD", 'date', 'BE'])
decbe = decbe.groupby(['STKCD', 'date']).last().reset_index()
decbe.head(3)
Out[2]:
STKCD date BE
0 1 1999-12-31 2.900831e+09
1 1 2000-12-31 4.738884e+09
2 1 2001-12-31 3.627669e+09
In [3]:
# markettype 1 Shanghai A, 4, Shengzhen A
stkret =pd.read_csv('csmarstk0016.csv')
stkret.TRDMNT = pd.to_datetime(stkret.TRDMNT.astype('str'), format='%Y-%m-%d') 
stkret = stkret.loc[stkret.ret.notnull()]
stkret.head(3)
Out[3]:
STKCD MARKETTYPE TRDMNT ME ret
0 1 4 2000-01-31 2.875573e+10 0.061891
1 1 4 2000-02-29 2.842984e+10 -0.011333
2 1 4 2000-03-31 2.850743e+10 0.002729
In [4]:
mktret = pd.read_csv('csmarmktretrf.csv')
mktret.TRDMNT = pd.to_datetime(mktret.TRDMNT.astype('str'), format='%Y-%m-%d') 
mktret = mktret[['TRDMNT', 'mktret', 'rf']].set_index('TRDMNT')
mktret.head(3)
Out[4]:
mktret rf
TRDMNT
2000-01-31 0.123413 0.001856
2000-02-29 0.118776 0.001856
2000-03-31 0.049308 0.001856

Preliminaries

  • Q(1) Assume power utility and relative risk aversion is 3, and constent expected market excess return, what is the optimal proportion of wealth to invest in the risky (stock) assets? (you will invest in a market portfolio).
  • Q(1b) Your answer above may be different from the typical answer in the U.S. market, explain what mainly drives this difference?

Beta sorts and Fama-Macbeth style estimations

  • Q(2) For each stock, obtain the CAPM beta over the whole sample. Sort stocks into 20 portfolios according to beta estimates, then do a Fama-Macbeth style estimation test.

Forming Portfolios

According to size and MTB.

Starting from ‘2000-7-31’, from July to end of June of each year, categorize each stock to form a 5X5 portfolios of size and book-to-market. Size and book-to-market are defined as in Fama Frech (1993). You may want to create a ‘year’ variable that designate portfolio formation year.

  • Q(3a) Show the CAPM alpha and beta of these portfolios
  • Q(3b) Calculate mean portfolio return and compare them with the CAPM estimates, explain any patterns in the data.
  • Q(3c) Plot the mean excess return against CAPM predicted return, plot both within size (BM) plots and the within BM (size) plots.

Constructing SMB and HML factors

Construct 2×3 portfolios of size and BTM according to the Shanghai Stock Exchange cutoff (MARKETTYPE==1). Calculate the value-weighted returns of the portfolios and then construct SMB and HML factors following Fama French (93) procedure.

  • Q(4a) Joinning factors with 25 portfolio returns, run FF 3 factor model. Does the coefficients estimates make sense?
  • Q(4b) plot the Mean portfolio return against the Fama-French 3 factor predicted return again for both Within-BM Size Plot and Within-Size BM Plot.
  • Q(4c) How do the graphs compare to the earlier CAPM plots? In the U.S. data, the most severe violation of CAPM happens along BTM portfolios, What about China? Can you explain why this factor comes up so significant in the Chinese markets?

Momentum

  • Q5 If you form a hedging porfolio of buying the top decile winners and shorting the bottom decile losers according to the last 11 month returns, how does your porfolio perform? Plot the hedging portfolio returns in the future 1 to 6 months.
In [ ]: