我创建了一个
ols模块来模拟熊猫的不赞成使用的模块
MovingOLS;它是在这里。
它具有三个核心类:
OLS
:静态(单窗口)普通最小二乘回归。输出是NumPy数组RollingOLS
:滚动(多窗口)普通最小二乘回归。输出是高维NumPy数组。PandasRollingOLS
:将RollingOLS
pandas Series&Dataframes的结果包装起来。旨在模仿已弃用的熊猫模块的外观。
请注意,该模块是包的一部分(我目前正在上传到PyPi中),它需要一次包间导入。
上面的前两类完全在NumPy中实现,主要使用矩阵代数。
RollingOLS还充分利用广播的优势。属性主要模仿statsmodels的OLS
RegressionResultsWrapper。
一个例子:
import urllib.parseimport pandas as pdfrom pyfinance.ols import PandasRollingOLS# You can also do this with pandas-datareader; here's the hard wayurl = "https://fred.stlouisfed.org/graph/fredgraph.csv"syms = { "TWEXBMTH" : "usd", "T10Y2YM" : "term_spread", "GOLDAMGBD228NLBM" : "gold",}params = { "fq": "Monthly,Monthly,Monthly", "id": ",".join(syms.keys()), "cosd": "2000-01-01", "coed": "2019-02-01",}data = pd.read_csv( url + "?" + urllib.parse.urlenpre(params, safe=","), na_values={"."}, parse_dates=["DATE"], index_col=0).pct_change().dropna().rename(columns=syms)print(data.head())# usd term_spread gold# DATE # 2000-02-01 0.012580 -1.409091 0.057152# 2000-03-01 -0.000113 2.000000 -0.047034# 2000-04-01 0.005634 0.518519 -0.023520# 2000-05-01 0.022017 -0.097561 -0.016675# 2000-06-01 -0.010116 0.027027 0.036599y = data.usdx = data.drop('usd', axis=1)window = 12 # monthsmodel = PandasRollingOLS(y=y, x=x, window=window)print(model.beta.head()) # Coefficients excluding the intercept# term_spread gold# DATE # 2001-01-01 0.000033 -0.054261# 2001-02-01 0.000277 -0.188556# 2001-03-01 0.002432 -0.294865# 2001-04-01 0.002796 -0.334880# 2001-05-01 0.002448 -0.241902print(model.fstat.head())# DATE# 2001-01-01 0.136991# 2001-02-01 1.233794# 2001-03-01 3.053000# 2001-04-01 3.997486# 2001-05-01 3.855118# Name: fstat, dtype: float64print(model.rsq.head()) # R-squared# DATE# 2001-01-01 0.029543# 2001-02-01 0.215179# 2001-03-01 0.404210# 2001-04-01 0.470432# 2001-05-01 0.461408# Name: rsq, dtype: float64
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)