CSMAR下载数据
变量定义
- volatility:月频率。从 月开始的每日收益的标准差。
代码
import numpy as np
import pandas as pd
RET = pd.read_stata('DailyRet.dta')
RET = RET[['Stkcd', 'Trddt','Dretwd']]
def My_YM(Set,Col):
Set['DATE'] = Set[Col].astype(str).replace('\-', '', regex=True)
Set['Yearmon'] = Set['DATE'].astype(int) // 100
My_YM(RET,'Trddt')
#%%
RETS = RET[['Dretwd', 'Stkcd','Yearmon']
].groupby(['Stkcd','Yearmon']
).std().reset_index()
RETS['volatility'] = RETS['Dretwd'].shift(1)
RETS = RETS.dropna()
RETS[['Stkcd','Yearmon','volatility']].to_csv(
"RETS_volatility_treated.csv",
encoding='utf_8_sig',index = False)输出结果
Stkcd Yearmon volatility
1 000001 199105 0.051700
2 000001 199106 0.001431
3 000001 199107 0.003160
4 000001 199108 0.004634
5 000001 199109 0.128755
... ... ...
668441 900957 202111 0.011754
668442 900957 202112 0.009716
668443 900957 202201 0.011050
668444 900957 202202 0.010643
668445 900957 202203 0.006570
[657069 rows x 3 columns](完)
参考文献
- Mr Figurant:资产定价笔记04:中国股市中的机器学习-附录C
|
|