siat 3.10.132__py3-none-any.whl → 3.10.133__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- siat/__init__.py +0 -0
- siat/allin.py +0 -0
- siat/assets_liquidity.py +0 -0
- siat/beta_adjustment.py +0 -0
- siat/beta_adjustment_china.py +0 -0
- siat/blockchain.py +0 -0
- siat/bond.py +0 -0
- siat/bond_base.py +0 -0
- siat/bond_china.py +0 -0
- siat/bond_zh_sina.py +0 -0
- siat/capm_beta.py +0 -0
- siat/capm_beta2.py +0 -0
- siat/compare_cross.py +0 -0
- siat/copyrights.py +0 -0
- siat/cryptocurrency.py +0 -0
- siat/economy.py +0 -0
- siat/economy2.py +0 -0
- siat/esg.py +0 -0
- siat/event_study.py +0 -0
- siat/exchange_bond_china.pickle +0 -0
- siat/fama_french.py +0 -0
- siat/fin_stmt2_yahoo.py +0 -0
- siat/financial_base.py +0 -0
- siat/financial_statements.py +0 -0
- siat/financials.py +0 -0
- siat/financials2.py +0 -0
- siat/financials_china.py +0 -0
- siat/financials_china2.py +0 -0
- siat/fund.py +0 -0
- siat/fund_china.pickle +0 -0
- siat/fund_china.py +0 -0
- siat/future_china.py +0 -0
- siat/google_authenticator.py +0 -0
- siat/grafix.py +0 -0
- siat/holding_risk.py +0 -0
- siat/luchy_draw.py +0 -0
- siat/market_china.py +0 -0
- siat/markowitz.py +0 -0
- siat/markowitz2.py +0 -0
- siat/markowitz2_20250704.py +0 -0
- siat/markowitz2_20250705.py +0 -0
- siat/markowitz_simple.py +0 -0
- siat/ml_cases.py +0 -0
- siat/ml_cases_example.py +0 -0
- siat/option_china.py +0 -0
- siat/option_pricing.py +0 -0
- siat/other_indexes.py +0 -0
- siat/risk_adjusted_return.py +0 -0
- siat/risk_adjusted_return2.py +0 -0
- siat/risk_evaluation.py +0 -0
- siat/risk_free_rate.py +0 -0
- siat/sector_china.py +0 -0
- siat/security_price2.py +0 -0
- siat/security_prices.py +40 -2
- siat/security_trend.py +0 -0
- siat/security_trend2.py +0 -0
- siat/stock.py +0 -0
- siat/stock_advice_linear.py +0 -0
- siat/stock_base.py +0 -0
- siat/stock_china.py +0 -0
- siat/stock_info.pickle +0 -0
- siat/stock_prices_kneighbors.py +0 -0
- siat/stock_prices_linear.py +0 -0
- siat/stock_profile.py +0 -0
- siat/stock_technical.py +0 -0
- siat/stooq.py +0 -0
- siat/transaction.py +0 -0
- siat/translate.py +0 -0
- siat/valuation.py +0 -0
- siat/valuation_china.py +0 -0
- siat/var_model_validation.py +0 -0
- siat/yf_name.py +0 -0
- {siat-3.10.132.dist-info/licenses → siat-3.10.133.dist-info}/LICENSE +0 -0
- {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/METADATA +232 -235
- siat-3.10.133.dist-info/RECORD +78 -0
- {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/WHEEL +1 -1
- {siat-3.10.132.dist-info → siat-3.10.133.dist-info}/top_level.txt +0 -1
- build/lib/build/lib/siat/__init__.py +0 -75
- build/lib/build/lib/siat/allin.py +0 -137
- build/lib/build/lib/siat/assets_liquidity.py +0 -915
- build/lib/build/lib/siat/beta_adjustment.py +0 -1058
- build/lib/build/lib/siat/beta_adjustment_china.py +0 -548
- build/lib/build/lib/siat/blockchain.py +0 -143
- build/lib/build/lib/siat/bond.py +0 -2900
- build/lib/build/lib/siat/bond_base.py +0 -992
- build/lib/build/lib/siat/bond_china.py +0 -100
- build/lib/build/lib/siat/bond_zh_sina.py +0 -143
- build/lib/build/lib/siat/capm_beta.py +0 -783
- build/lib/build/lib/siat/capm_beta2.py +0 -887
- build/lib/build/lib/siat/common.py +0 -5360
- build/lib/build/lib/siat/compare_cross.py +0 -642
- build/lib/build/lib/siat/copyrights.py +0 -18
- build/lib/build/lib/siat/cryptocurrency.py +0 -667
- build/lib/build/lib/siat/economy.py +0 -1471
- build/lib/build/lib/siat/economy2.py +0 -1853
- build/lib/build/lib/siat/esg.py +0 -536
- build/lib/build/lib/siat/event_study.py +0 -815
- build/lib/build/lib/siat/fama_french.py +0 -1521
- build/lib/build/lib/siat/fin_stmt2_yahoo.py +0 -982
- build/lib/build/lib/siat/financial_base.py +0 -1160
- build/lib/build/lib/siat/financial_statements.py +0 -598
- build/lib/build/lib/siat/financials.py +0 -2339
- build/lib/build/lib/siat/financials2.py +0 -1278
- build/lib/build/lib/siat/financials_china.py +0 -4433
- build/lib/build/lib/siat/financials_china2.py +0 -2212
- build/lib/build/lib/siat/fund.py +0 -629
- build/lib/build/lib/siat/fund_china.py +0 -3307
- build/lib/build/lib/siat/future_china.py +0 -551
- build/lib/build/lib/siat/google_authenticator.py +0 -47
- build/lib/build/lib/siat/grafix.py +0 -3636
- build/lib/build/lib/siat/holding_risk.py +0 -867
- build/lib/build/lib/siat/luchy_draw.py +0 -638
- build/lib/build/lib/siat/market_china.py +0 -1168
- build/lib/build/lib/siat/markowitz.py +0 -2363
- build/lib/build/lib/siat/markowitz2.py +0 -3150
- build/lib/build/lib/siat/markowitz2_20250704.py +0 -2969
- build/lib/build/lib/siat/markowitz2_20250705.py +0 -3158
- build/lib/build/lib/siat/markowitz_simple.py +0 -373
- build/lib/build/lib/siat/ml_cases.py +0 -2291
- build/lib/build/lib/siat/ml_cases_example.py +0 -60
- build/lib/build/lib/siat/option_china.py +0 -3069
- build/lib/build/lib/siat/option_pricing.py +0 -1925
- build/lib/build/lib/siat/other_indexes.py +0 -409
- build/lib/build/lib/siat/risk_adjusted_return.py +0 -1576
- build/lib/build/lib/siat/risk_adjusted_return2.py +0 -1900
- build/lib/build/lib/siat/risk_evaluation.py +0 -2218
- build/lib/build/lib/siat/risk_free_rate.py +0 -351
- build/lib/build/lib/siat/sector_china.py +0 -4140
- build/lib/build/lib/siat/security_price2.py +0 -727
- build/lib/build/lib/siat/security_prices.py +0 -3408
- build/lib/build/lib/siat/security_trend.py +0 -402
- build/lib/build/lib/siat/security_trend2.py +0 -646
- build/lib/build/lib/siat/stock.py +0 -4284
- build/lib/build/lib/siat/stock_advice_linear.py +0 -934
- build/lib/build/lib/siat/stock_base.py +0 -26
- build/lib/build/lib/siat/stock_china.py +0 -2095
- build/lib/build/lib/siat/stock_prices_kneighbors.py +0 -910
- build/lib/build/lib/siat/stock_prices_linear.py +0 -386
- build/lib/build/lib/siat/stock_profile.py +0 -707
- build/lib/build/lib/siat/stock_technical.py +0 -3305
- build/lib/build/lib/siat/stooq.py +0 -74
- build/lib/build/lib/siat/transaction.py +0 -347
- build/lib/build/lib/siat/translate.py +0 -5183
- build/lib/build/lib/siat/valuation.py +0 -1378
- build/lib/build/lib/siat/valuation_china.py +0 -2076
- build/lib/build/lib/siat/var_model_validation.py +0 -444
- build/lib/build/lib/siat/yf_name.py +0 -811
- build/lib/siat/__init__.py +0 -75
- build/lib/siat/allin.py +0 -137
- build/lib/siat/assets_liquidity.py +0 -915
- build/lib/siat/beta_adjustment.py +0 -1058
- build/lib/siat/beta_adjustment_china.py +0 -548
- build/lib/siat/blockchain.py +0 -143
- build/lib/siat/bond.py +0 -2900
- build/lib/siat/bond_base.py +0 -992
- build/lib/siat/bond_china.py +0 -100
- build/lib/siat/bond_zh_sina.py +0 -143
- build/lib/siat/capm_beta.py +0 -783
- build/lib/siat/capm_beta2.py +0 -887
- build/lib/siat/common.py +0 -5360
- build/lib/siat/compare_cross.py +0 -642
- build/lib/siat/copyrights.py +0 -18
- build/lib/siat/cryptocurrency.py +0 -667
- build/lib/siat/economy.py +0 -1471
- build/lib/siat/economy2.py +0 -1853
- build/lib/siat/esg.py +0 -536
- build/lib/siat/event_study.py +0 -815
- build/lib/siat/fama_french.py +0 -1521
- build/lib/siat/fin_stmt2_yahoo.py +0 -982
- build/lib/siat/financial_base.py +0 -1160
- build/lib/siat/financial_statements.py +0 -598
- build/lib/siat/financials.py +0 -2339
- build/lib/siat/financials2.py +0 -1278
- build/lib/siat/financials_china.py +0 -4433
- build/lib/siat/financials_china2.py +0 -2212
- build/lib/siat/fund.py +0 -629
- build/lib/siat/fund_china.py +0 -3307
- build/lib/siat/future_china.py +0 -551
- build/lib/siat/google_authenticator.py +0 -47
- build/lib/siat/grafix.py +0 -3636
- build/lib/siat/holding_risk.py +0 -867
- build/lib/siat/luchy_draw.py +0 -638
- build/lib/siat/market_china.py +0 -1168
- build/lib/siat/markowitz.py +0 -2363
- build/lib/siat/markowitz2.py +0 -3150
- build/lib/siat/markowitz2_20250704.py +0 -2969
- build/lib/siat/markowitz2_20250705.py +0 -3158
- build/lib/siat/markowitz_simple.py +0 -373
- build/lib/siat/ml_cases.py +0 -2291
- build/lib/siat/ml_cases_example.py +0 -60
- build/lib/siat/option_china.py +0 -3069
- build/lib/siat/option_pricing.py +0 -1925
- build/lib/siat/other_indexes.py +0 -409
- build/lib/siat/risk_adjusted_return.py +0 -1576
- build/lib/siat/risk_adjusted_return2.py +0 -1900
- build/lib/siat/risk_evaluation.py +0 -2218
- build/lib/siat/risk_free_rate.py +0 -351
- build/lib/siat/sector_china.py +0 -4140
- build/lib/siat/security_price2.py +0 -727
- build/lib/siat/security_prices.py +0 -3408
- build/lib/siat/security_trend.py +0 -402
- build/lib/siat/security_trend2.py +0 -646
- build/lib/siat/stock.py +0 -4284
- build/lib/siat/stock_advice_linear.py +0 -934
- build/lib/siat/stock_base.py +0 -26
- build/lib/siat/stock_china.py +0 -2095
- build/lib/siat/stock_prices_kneighbors.py +0 -910
- build/lib/siat/stock_prices_linear.py +0 -386
- build/lib/siat/stock_profile.py +0 -707
- build/lib/siat/stock_technical.py +0 -3305
- build/lib/siat/stooq.py +0 -74
- build/lib/siat/transaction.py +0 -347
- build/lib/siat/translate.py +0 -5183
- build/lib/siat/valuation.py +0 -1378
- build/lib/siat/valuation_china.py +0 -2076
- build/lib/siat/var_model_validation.py +0 -444
- build/lib/siat/yf_name.py +0 -811
- siat-3.10.132.dist-info/RECORD +0 -218
build/lib/siat/risk_free_rate.py
DELETED
@@ -1,351 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
"""
|
3
|
-
版权:王德宏,北京外国语大学国际商学院
|
4
|
-
功能:
|
5
|
-
1、基于股票或股票组合计算无风险收益率
|
6
|
-
2、绘制无风险收益率的变化趋势图:日,周,月
|
7
|
-
3、与实际的无风险收益率比较
|
8
|
-
版本:1.0,2021-2-6
|
9
|
-
"""
|
10
|
-
|
11
|
-
#==============================================================================
|
12
|
-
#关闭所有警告
|
13
|
-
import warnings; warnings.filterwarnings('ignore')
|
14
|
-
import siat.common as com
|
15
|
-
from siat.translate import *
|
16
|
-
#==============================================================================
|
17
|
-
#==============================================================================
|
18
|
-
#==============================================================================
|
19
|
-
def compare_rf(rf1,col1,rf2,col2,fromdate,todate,power=0,zeroline=True,twinx=False):
|
20
|
-
"""
|
21
|
-
功能:比较两个无风险收益率的时间序列,并绘制趋势线
|
22
|
-
"""
|
23
|
-
#检查日期期间的合理性
|
24
|
-
result,start,end=com.check_period(fromdate,todate)
|
25
|
-
if not result:
|
26
|
-
print(" Error(compare_rf): invalide date period from",fromdate,"to",todate)
|
27
|
-
return None
|
28
|
-
|
29
|
-
#检查并筛选两个无风险收益率的时间序列
|
30
|
-
if rf1 is None:
|
31
|
-
print(" Error(compare_rf): 1st risk-free-rate series is empty")
|
32
|
-
return None
|
33
|
-
if rf2 is None:
|
34
|
-
print(" Error(compare_rf): 2nd risk-free-rate series is empty")
|
35
|
-
return None
|
36
|
-
df1a=rf1[rf1.index >= start]
|
37
|
-
df1b=df1a[df1a.index <= end]
|
38
|
-
df1b[col1+'%']=df1b[col1]*100.0
|
39
|
-
|
40
|
-
df2a=rf2[rf2.index >= start]
|
41
|
-
df2b=df2a[df2a.index <= end]
|
42
|
-
df2b[col2+'%']=df2b[col2]*100.0
|
43
|
-
|
44
|
-
#绘制对比图
|
45
|
-
import siat.grafix as g
|
46
|
-
ticker1=g.ticker_name(df1b['ticker'][0])
|
47
|
-
colname1=col1+'%'
|
48
|
-
label1=col1+'%'
|
49
|
-
ticker2=g.ticker_name(df2b['ticker'][0])
|
50
|
-
colname2=col2+'%'
|
51
|
-
label2=col2+'%'
|
52
|
-
ylabeltxt='无风险收益率%'
|
53
|
-
titletxt="基于CAPM计算的无风险收益率变化趋势"
|
54
|
-
|
55
|
-
import datetime; today = datetime.date.today()
|
56
|
-
rf1note=df1b['footnote'][0]
|
57
|
-
rf2note=df2b['footnote'][0]
|
58
|
-
footnote="无风险收益率1:"+str(rf1note)+ \
|
59
|
-
"\n无风险收益率2:"+str(rf2note)+ \
|
60
|
-
"\n数据来源:雅虎财经, "+str(today)
|
61
|
-
|
62
|
-
g.plot_line2(df1b,ticker1,colname1,label1, \
|
63
|
-
df2b,ticker2,colname2,label2, \
|
64
|
-
ylabeltxt,titletxt,footnote, \
|
65
|
-
power=power,zeroline=zeroline,twinx=twinx)
|
66
|
-
|
67
|
-
return
|
68
|
-
|
69
|
-
if __name__=='__main__':
|
70
|
-
df1=get_rf_capm('AAPL','^GSPC','2018-1-1','2020-12-31',window=40)
|
71
|
-
df2=get_rf_capm('MSFT','^GSPC','2018-1-1','2020-12-31',window=40)
|
72
|
-
compare_rf(df1,'Rf',df2,'Rf','2019-1-1','2019-12-31')
|
73
|
-
compare_rf(df1,'Rf',df2,'Rf','2019-1-1','2019-12-31',twinx=True)
|
74
|
-
|
75
|
-
df1['Rf_20']=df1['Rf'].rolling(window=20).mean()
|
76
|
-
compare_rf(df1,'Rf',df1,'Rf_20','2019-1-1','2019-12-31')
|
77
|
-
|
78
|
-
#==============================================================================
|
79
|
-
|
80
|
-
def calc_rolling_cumret(dfc,col,period='Weekly'):
|
81
|
-
"""
|
82
|
-
传入日收益率col的数据表dfc
|
83
|
-
传出不同期间的累计收益率序列cumret
|
84
|
-
"""
|
85
|
-
df=dfc.copy()
|
86
|
-
#检查period类型
|
87
|
-
periodlist = ["Weekly","Biweekly","Monthly","Quarterly","Semiannual","Annual"]
|
88
|
-
if not (period in periodlist):
|
89
|
-
print(" Error(calc_rolling_cumret): only supports:",periodlist)
|
90
|
-
return None
|
91
|
-
|
92
|
-
#换算期间对应的实际交易天数
|
93
|
-
perioddays=[5,10,20,60,120,240]
|
94
|
-
rollingnum=perioddays[periodlist.index(period)]
|
95
|
-
|
96
|
-
import numpy as np
|
97
|
-
df['logdret']=np.log(df[col]+1)
|
98
|
-
df['cumret']=np.exp(df['logdret'].rolling(rollingnum).sum())-1.0
|
99
|
-
|
100
|
-
return df['cumret']
|
101
|
-
|
102
|
-
#==============================================================================
|
103
|
-
|
104
|
-
if __name__ =="__main__":
|
105
|
-
col='Rf'
|
106
|
-
limits=[0.01,0.01]
|
107
|
-
|
108
|
-
def winsor(df,col,limits=[0.01,0.01]):
|
109
|
-
"""
|
110
|
-
功能:对于数据表df1中的列col进行下1%(参数1)和上1%(参数2)的处理
|
111
|
-
"""
|
112
|
-
import numpy as np
|
113
|
-
from scipy.stats.mstats import winsorize
|
114
|
-
|
115
|
-
a = np.array(df[col])
|
116
|
-
aw=winsorize(a,limits=limits)
|
117
|
-
|
118
|
-
return aw.data
|
119
|
-
#==============================================================================
|
120
|
-
if __name__=='__main__':
|
121
|
-
ticker='399001.SZ'
|
122
|
-
mktidx='000300.SS'
|
123
|
-
fromdate='2020-1-1'
|
124
|
-
todate='2020-12-31'
|
125
|
-
window=240
|
126
|
-
|
127
|
-
def get_rf_capm(ticker,mktidx,fromdate,todate,window=240,sharelist=[]):
|
128
|
-
"""
|
129
|
-
功能:计算无风险收益率的时间序列
|
130
|
-
ticker:股票或股票组合
|
131
|
-
mktidx:股票市场指数
|
132
|
-
fromdate:开始时间
|
133
|
-
todate:截止时间
|
134
|
-
window:每次回归的样本个数
|
135
|
-
sharelist:第一个参数为投资组合的持股比例,默认为等权重;为单个股票时无用
|
136
|
-
"""
|
137
|
-
#检查日期期间的合理性
|
138
|
-
result,start,end=com.check_period(fromdate,todate)
|
139
|
-
if not result:
|
140
|
-
print(" Error(get_rf_capm): invalide date period from",fromdate,"to",todate)
|
141
|
-
return None
|
142
|
-
|
143
|
-
#提前开始日期,留出回归窗口
|
144
|
-
start1=com.date_adjust(start,adjust=-window*2)
|
145
|
-
|
146
|
-
#获得股票或股票组合的历史收益率
|
147
|
-
import siat.security_prices as sp
|
148
|
-
#单个股票情形
|
149
|
-
if isinstance(ticker,str):
|
150
|
-
spdf=sp.get_prices(ticker,start1,end)
|
151
|
-
#股票组合情形
|
152
|
-
if isinstance(ticker,list):
|
153
|
-
if sharelist == []:
|
154
|
-
num=len(ticker)
|
155
|
-
sharelist=[1]*num
|
156
|
-
spdf=sp.get_prices_portfolio(ticker,sharelist,start1,end)
|
157
|
-
if spdf is None:
|
158
|
-
print(" Error(get_rf_capm): info not found or unavailable for",ticker)
|
159
|
-
return None
|
160
|
-
|
161
|
-
spdf['Stock_dailyRet']=spdf['Close'].pct_change()
|
162
|
-
|
163
|
-
#获取市场指数的历史收益率
|
164
|
-
rmdf=sp.get_prices(mktidx,start1,end)
|
165
|
-
if rmdf is None:
|
166
|
-
print(" Error(get_rf_capm): info not found or unavailable for",mktidx)
|
167
|
-
return None
|
168
|
-
rmdf['Market_dailyRet']=rmdf['Close'].pct_change()
|
169
|
-
|
170
|
-
#合并股票(组合)与市场指数的收益率为一个数据集
|
171
|
-
import pandas as pd
|
172
|
-
df=pd.merge(rmdf['Market_dailyRet'],spdf['Stock_dailyRet'],how='inner',left_index=True,right_index=True)
|
173
|
-
df['Date']=df.index.strftime("%Y-%m-%d")
|
174
|
-
|
175
|
-
datelist_ts=list(df.index)
|
176
|
-
datelist=list(df['Date'])
|
177
|
-
fromdate=start.strftime("%Y-%m-%d")
|
178
|
-
#start_pos=lookup_datelist(datelist,fromdate,direction='more')
|
179
|
-
todate=end.strftime("%Y-%m-%d")
|
180
|
-
#end_pos=lookup_datelist(datelist,todate,direction='less')
|
181
|
-
|
182
|
-
#用于滚动的日期期间
|
183
|
-
datelist_rolling=[]
|
184
|
-
for d in datelist:
|
185
|
-
if (d >= fromdate) and (d <= todate):
|
186
|
-
datelist_rolling=datelist_rolling+[d]
|
187
|
-
|
188
|
-
#滚动回归
|
189
|
-
if sharelist == []:
|
190
|
-
#footnote=[ticker,mktidx,fromdate,todate,window]
|
191
|
-
footnote=[ticker,mktidx,window]
|
192
|
-
else:
|
193
|
-
#footnote=[ticker,mktidx,fromdate,todate,window,sharelist]
|
194
|
-
footnote=[ticker,mktidx,window,sharelist]
|
195
|
-
betas=pd.DataFrame(columns=('date','Beta','alpha','R-sqr','p-value','sig','Rf','ticker','footnote'))
|
196
|
-
from scipy import stats
|
197
|
-
import numpy as np
|
198
|
-
|
199
|
-
for d in datelist_rolling:
|
200
|
-
pos2=lookup_datelist(datelist,d)
|
201
|
-
pos1=pos2 - window
|
202
|
-
sdate2=datelist_ts[pos2]
|
203
|
-
sdate1=datelist_ts[pos1]
|
204
|
-
|
205
|
-
sampledf=df[df.index >= sdate1].copy()
|
206
|
-
sampledf=sampledf[sampledf.index < sdate2]
|
207
|
-
|
208
|
-
(beta,alpha,r_value,p_value,std_err)= \
|
209
|
-
stats.linregress(sampledf['Market_dailyRet'],sampledf['Stock_dailyRet'])
|
210
|
-
sig=sig_stars(p_value)
|
211
|
-
try:
|
212
|
-
rf=alpha/(1-beta)
|
213
|
-
except:
|
214
|
-
rf=np.nan
|
215
|
-
|
216
|
-
row=pd.Series({'date':sdate2,'Beta':beta,'alpha':alpha, \
|
217
|
-
'R-sqr':r_value**2,'p-value':p_value,'sig':sig,'Rf':rf,'ticker':ticker,'footnote':footnote})
|
218
|
-
try:
|
219
|
-
betas=betas.append(row,ignore_index=True)
|
220
|
-
except:
|
221
|
-
betas=betas._append(row,ignore_index=True)
|
222
|
-
|
223
|
-
betas.set_index('date',inplace=True)
|
224
|
-
|
225
|
-
return betas
|
226
|
-
|
227
|
-
if __name__=='__main__':
|
228
|
-
df1=get_rf_capm('399001.SZ','000001.SS','2020-1-1','2020-12-31',window=240)
|
229
|
-
df1['Rf'].plot()
|
230
|
-
|
231
|
-
df2=get_rf_capm('000001.SS','399001.SZ','2020-1-1','2020-12-31',window=240)
|
232
|
-
df2['Rf'].plot()
|
233
|
-
|
234
|
-
df3=get_rf_capm('000001.SS','000300.SS','2020-1-1','2020-12-31',window=240)
|
235
|
-
df3['Rf'].plot()
|
236
|
-
|
237
|
-
df4=get_rf_capm('AAPL','^GSPC','2020-1-1','2020-12-31',window=240)
|
238
|
-
df4['Rf'].plot()
|
239
|
-
|
240
|
-
df5=get_rf_capm('^DJI','^GSPC','2020-1-1','2020-12-31',window=240)
|
241
|
-
df5['Rf'].plot()
|
242
|
-
#==============================================================================
|
243
|
-
if __name__=='__main__':
|
244
|
-
datelist=['2019-12-30','2019-12-31','2020-01-02','2020-01-03','2020-01-06']
|
245
|
-
adate='2020-01-01'
|
246
|
-
adate='2020-01-04'
|
247
|
-
direction='less'
|
248
|
-
direction='more'
|
249
|
-
|
250
|
-
def lookup_datelist(datelist,adate,direction='less'):
|
251
|
-
"""
|
252
|
-
功能:在日期列表datelist查找与日期adate最接近日期的位置
|
253
|
-
direction='more':若无匹配的日期,则往日期增加的方向查找最接近日期的位置
|
254
|
-
direction='less':若无匹配的日期,则往日期减少的方向查找最接近日期的位置
|
255
|
-
"""
|
256
|
-
i=0
|
257
|
-
found=False
|
258
|
-
while not found:
|
259
|
-
try:
|
260
|
-
pos=datelist.index(adate)
|
261
|
-
except:
|
262
|
-
if direction == 'more':
|
263
|
-
i=i+1
|
264
|
-
else:
|
265
|
-
i=i-1
|
266
|
-
adate=com.date_adjust(adate,adjust=i)
|
267
|
-
else:
|
268
|
-
found=True
|
269
|
-
|
270
|
-
return pos
|
271
|
-
|
272
|
-
#==============================================================================
|
273
|
-
if __name__=='__main__':
|
274
|
-
p_value=0.07
|
275
|
-
p_value=0.02
|
276
|
-
p_value=0.0009
|
277
|
-
criteria='accounting'
|
278
|
-
criteria='financial'
|
279
|
-
|
280
|
-
def sig_stars(p_value,criteria='accounting'):
|
281
|
-
"""
|
282
|
-
功能:基于p_value给出星号的个数
|
283
|
-
p_value:显著性水平
|
284
|
-
criteria='accounting':默认的显著性基准,<0.1为一颗星;若为'financial',<0.05为一颗星
|
285
|
-
"""
|
286
|
-
sig=''
|
287
|
-
if criteria == 'accounting':
|
288
|
-
if p_value < 0.1: sig='*'*1
|
289
|
-
if p_value < 0.05: sig='*'*2
|
290
|
-
if p_value < 0.01: sig='*'*3
|
291
|
-
else:
|
292
|
-
if p_value < 0.05: sig='*'*1
|
293
|
-
if p_value < 0.01: sig='*'*2
|
294
|
-
if p_value < 0.001: sig='*'*3
|
295
|
-
|
296
|
-
return sig
|
297
|
-
|
298
|
-
if __name__=='__main__':
|
299
|
-
sig_stars(0.1)
|
300
|
-
sig_stars(0.07)
|
301
|
-
sig_stars(0.04)
|
302
|
-
sig_stars(0.009)
|
303
|
-
|
304
|
-
#==============================================================================
|
305
|
-
def calc_capm_rf(rmdf,rdf):
|
306
|
-
"""
|
307
|
-
功能:CAPM回归
|
308
|
-
返回:截距项,贝塔系数,无风险收益率
|
309
|
-
"""
|
310
|
-
#OLS回归
|
311
|
-
from scipy import stats
|
312
|
-
(beta,alpha,r_value,p_value,std_err)=stats.linregress(rmdf,rdf)
|
313
|
-
|
314
|
-
rf=alpha/(1-beta)
|
315
|
-
|
316
|
-
return [beta,alpha,r_value,p_value,std_err,rf]
|
317
|
-
|
318
|
-
#==============================================================================
|
319
|
-
#==============================================================================
|
320
|
-
#==============================================================================
|
321
|
-
#==============================================================================
|
322
|
-
if __name__=='__main__':
|
323
|
-
start='2018-1-1'
|
324
|
-
end='2020-12-31'
|
325
|
-
scope='US'
|
326
|
-
freq='daily'
|
327
|
-
|
328
|
-
def get_rf_kfdl(start,end,scope='US',freq='daily'):
|
329
|
-
"""
|
330
|
-
功能:从Kenneth R. French's Data Library获得无风险收益率
|
331
|
-
start/end:日期期间
|
332
|
-
scope:国家/地区,支持美国/北美/欧洲/日本/不含日本的亚太/不含美国的全球。全球
|
333
|
-
freq:支持日/月/年收益率,其中美国还支持周数据
|
334
|
-
返回:无风险收益率,市场收益率
|
335
|
-
"""
|
336
|
-
import siat.fama_french as ff
|
337
|
-
factor='FF3'
|
338
|
-
df=ff.get_ff_factors(start,end,scope,factor,freq)
|
339
|
-
|
340
|
-
df['Market_dailyRet']=df['Mkt-RF']+df['RF']
|
341
|
-
df['Rf']=df['RF']
|
342
|
-
df['ticker']=scope
|
343
|
-
footnote=[scope,freq]
|
344
|
-
df['footnote']=df['ticker'].apply(lambda x:footnote)
|
345
|
-
|
346
|
-
df1=df[['Market_dailyRet','Rf','ticker','footnote']]
|
347
|
-
|
348
|
-
return df1
|
349
|
-
|
350
|
-
|
351
|
-
#==============================================================================
|