博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pd.stats.ols.MovingOLS以及替代
阅读量:4293 次
发布时间:2019-05-27

本文共 1381 字,大约阅读时间需要 4 分钟。

方法 

 

model = pd.stats.ols.MovingOLS(y=df.y, x=df.x, window_type='rolling',window=1000, intercept=True)

在pandas2.x中去掉了

替换的代码如下:

window = 1000 a = np.array([np.nan] * len(df)) b = [np.nan] * len(df) # If betas required. y_ = df.y.values x_ = df[['x']].assign(constant=1).values for n in range(window, len(df)):       y = y_[(n - window):n]       X = x_[(n - window):n]       # betas = Inverse(X'.X).X'.y       betas = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)       y_hat = betas.dot(x_[n, :])      a[n] = y_hat     b[n] = betas.tolist() # If betas required.或者:df=df.dropna() #uncomment this line to drop nanswindow = 5df['a']=None #constant df['b1']=None #beta1 df['b2']=None #beta2 for i in range(window,len(df)):     temp=df.iloc[i-window:i,:]      RollOLS=sm.OLS(temp.loc[:,'Y'],sm.add_constant(temp.loc[:,['time','X']])).fit()          df.iloc[i,df.columns.get_loc('a')]=RollOLS.params[0]      df.iloc[i,df.columns.get_loc('b1')]=RollOLS.params[1]     df.iloc[i,df.columns.get_loc('b2')]=RollOLS.params[2]当然,也有人自己写了一个模型解决这个问题,如下:# Rolling regressions from pyfinance.ols import OLS, RollingOLS, PandasRollingOLS y = data.usd x = data.drop('usd', axis=1) window = 12 # months model = PandasRollingOLS(y=y, x=x, window=window) print(model.beta.head())参考:主要是stackoverflow里面的2个网址https://www.e-learn.cn/content/wangluowenzhang/754972https://e-learn.cn/content/wangluowenzhang/192368

 

转自:

转载地址:http://jwfws.baihongyu.com/

你可能感兴趣的文章
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
点击button实现Storyboard中TabBar Controller的tab切换
查看>>
Xcode 的正确打开方式——Debugging
查看>>
打包app出现的一个问题
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>
Expanding User-Defined Runtime Attributes in Xcode with Objective-C
查看>>
iOS7 UITabBar自定义选中图片显示为默认蓝色的Bug
查看>>
提升UITableView性能-复杂页面的优化
查看>>
25 iOS App Performance Tips & Tricks
查看>>
那些好用的iOS开发工具
查看>>
iOS最佳实践
查看>>
使用CFStringTransform将汉字转换为拼音
查看>>
更轻量的 View Controllers
查看>>
Chisel-LLDB命令插件,让调试更Easy
查看>>
时间格式化hh:mm:ss和HH:mm:ss区别
查看>>
When to use Delegation, Notification, or Observation in iOS
查看>>
Objective-C Autorelease Pool 的实现原理
查看>>
编程语言大牛王垠:编程的智慧,带你少走弯路
查看>>
ios指令集以及基于指令集的app包压缩策略
查看>>