# Carhart四因子模型A股实证（附源码）

2020/06/08 13:39

01

02



# 动量因子UMD

def split_UMD(

x):

x.

loc[

x[

'UMD'] >=

x.UMD.quantile(

0.7),

'group_UMD'] =

'UMD_H'

x.

loc[

x[

'UMD'] <

x.UMD.quantile(

0.3),

'group_UMD'] =

'UMD_L'

return

x

UMD = price.pivot(

index =

'stockcode',

values =

'price').pct_change(

11).shift(

1).fillna(

0)

UMD = UMD.stack().reset_index()

UMD = UMD.

rename(columns = {UMD.columns[

2]:

'UMD'})

f[

'group_UMD'] =

'UMD_M'

f = pd.merge(

f,UMD,left_on =[

'stockcode',

'stockcode',

f =

f .groupby([

'ym']).apply(split_UMD)





# UMD因子

UMD_ret = f.groupby([

'group_UMD']).apply(lambda x:x.ret.mean())

UMD_ret = UMD_ret.reset_index()

UMD_ret = UMD_ret.rename(columns = {UMD_ret.columns[-

1]:

'ret'})

UMD_ret_pivot = UMD_ret.pivot(index =

'group_UMD',values =

'ret')

UMD = UMD_ret_pivot[

'UMD_H'] - UMD_ret_pivot[

'UMD_L']





Carhart4 = pd.concat([SMB,HML,UMD],axis =

1)

Carhart4 = Carhart4.reset_index()

Carhart4.columns = [

'SMB',

'HML',

'UMD']

Carhart4[

lambda x:x.year*

100 + x.month)





### 四因子回归

x = f25.

loc[:,[

'SMB',

'HML',

'UMD',

'mkt_rf',

'Intercept']].

values

r2 = []

betas = []

t = []

p = []

for i in

range(

25):# i =

0

y = f25.

loc[:,f25.columns[i+

1]].

values

mod =

sm.OLS(

y,

x).fit()

r2.

append([f25.columns[i+

1],

mod.rsquared])

betas.

append([f25.columns[i+

1]] +

list(

mod.params))

t.

append([f25.columns[i+

1]] +

list(

mod.tvalues))

p.

append([f25.columns[i+

1]] +

list(

mod.pvalues))

p = pd.DataFrame(

p,columns = [

'group',

'SMB',

'HML',

'UMD',

'mkt_rf',

'Intercept'])

t = pd.DataFrame(t,columns = [

'group',

'SMB',

'HML',

'UMD',

'mkt_rf',

'Intercept'])

betas = pd.DataFrame(betas,columns = [

'group',

'SMB',

'UMD',

'HML',

'mkt_rf',

'Intercept'])

r2 = pd.DataFrame(r2,columns = [

'group',

'r2'])

p_percent_car4 = (

p.iloc[:,

1:]<

0.05).mean()

alpha = betas[[

'group',

'Intercept']]

alpha[

'g_BM'] = alpha.group.apply(lambda

x:

x[

2])

alpha[

'g_SIZE'] = alpha.group.apply(lambda

x:

x[-

1])

alpha = alpha.pivot(

index =

'g_SIZE',columns =

'g_BM',

values =

'Intercept')

alpha = alpha.reset_index()

alpha = alpha.

rename(columns = {

'g_SIZE':

'',

alpha.columns[

1]:

'small BM',

alpha.columns[-

1]:

'big BM'})

alpha.iloc[

0,

0] =

'small SIZE'

alpha.iloc[-

1,

0] =

'big SIZE'

alpha



03

Carhart M M. On persistence in mutual fund performance[J]. The Journal of finance, 1997, 52(1): 57-82.

0
0 收藏

C4
2020/10/13 16:46

1 评论
0 收藏
0