有人知道在Python中实现递归最小二乘函数的简单方法吗?
我想要一种快速的方法来在每次更新时从输入信号中回归出线性漂移([1 2 ... n],其中 n 是到目前为止的时间点数量)。 RLS 通常用于执行此操作,因为计算时间不会随着时间点数量的增加而增加。
RLS算法在Python Padasip库中实现。您可以在github上查看代码:Padasip源代码
或者您可以直接使用该库。请参阅 文档 了解 Padasip RLS 算法
一条线与数据 t[], x[] 的最小二乘拟合由下式给出
x = xbar + (C/V)*(t-tbar)
where
xbar = Sum{ x[i]} / N
tbar = sum{ t[i]} / N
V = Sum{ (t[i]-tbar)^2 } / N
C = Sum{ (x[i]-xbar)*(t[i]-tbar) } / N
您可以像这样增量计算 xbar、tbar、V 和 C:
最初
N = 0
xbar = tbar = C = V = 0
合并数据 t,x:
N += 1
f = 1.0/N
dx = x - xbar
dt = t - tbar
xbar += f*dx
tbar += f*dt
V = (1.0-f)*(V + f*dt*dt)
C = (1.0-f)*(C + f*dx*dt)
请注意,直到您至少有两个数据点 V 才会为零,因此没有线条。另请注意,每个 x[] 都可以是一个向量;只要 xbar 和 C 也作为向量计算,相同的公式就有效。
参见文档 statsmodels.api.RecursiveLS(endog, exog, 尝试使用 beef.csv
申请#-------------------------------------------------------------------------------
# Purpose: apply Recursive Least Square (RLS) filter to investigate parameter instability.
#
# Author: https://deepnote.com/@jayjburgess/Price-Elasticity-For-Demand-Analysis-3d47c7aa-a59a-4724-816c-72f8b270b074
#-------------------------------------------------------------------------------
from statsmodels.compat import lzip
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.formula.api import ols
beef = pd.read_csv('beef.csv')
print(beef.head(10))
beef_model = ols("Quantity ~ Price", data=beef).fit()
print(beef_model.summary())
fig = plt.figure(figsize=(8,6))
fig = sm.graphics.plot_partregress_grid(beef_model, fig=fig)
plt.show()
beef['Year'] = pd.to_datetime(beef['Year'], format="%Y")
from pandas.tseries.offsets import *
beef['Date'] = beef.apply(lambda x:(x['Year'] + BQuarterBegin(x['Quarter'])), axis=1)
beef.drop(['Year', 'Quarter'], axis=1, inplace=True)
beef.set_index('Date', inplace=True)
beef.head(10)
endog = beef['Quantity']
# To the regressors in the dataset, we add a column of ones for an intercept
exog = sm.add_constant(beef['Price'])
# !!! >>> OP: "simple way to implement a recursive least squares function in Python"
## Recursive Least Square (RLS) filter to investigate parameter instability.
mod = sm.RecursiveLS(endog, exog)
res = mod.fit()
print(res.summary())
fig = res.plot_cusum(figsize=(10,6));
plt.show()
##In the plot above, the CUSUM statistic does not move outside of the 5% significance bands,
## so we fail to reject the null hypothesis of stable parameters at the 5% level.
附注回忆一下数学
RLS 是 BLUE(最佳线性无偏估计)的特例, 本身就是卡尔曼滤波器的特例。