几种最小二乘法及python代码:ELS、TLS、RLS

1.ARMAX模型

在这里插入图片描述

下面各章节,我就是使用上面公式的符号,其中y是输出,u是输入,e是噪声。有m个输出y,r个输入u。

进一步精简为:

Y=Pθ+E

其中:Y为要预测的部分,P为已知数据(包括y的t-1前数据,u的t-1前数据等),E是噪声。

参见:
1)线性模型:AR、MA、ARMA、ARMAX、ARX、ARARMAX、OE、BJ等:
https://blog.csdn.net/zephyr_wang/article/details/128589032
2)正交最小二乘法求解NARMAX:
https://blog.csdn.net/zephyr_wang/article/details/128667425

2.增广最小二乘法Extended Least Squares method

当噪声均值为0时,最小二乘法参数估计算法为无偏估计,为了解决最小二乘法参数估计的有偏性,将噪声模型的辨识同时考虑进去,就是增广最小二乘法。

所以ELSM方法主要关注ARMAX噪声E的影响,方法如下:
1)首先用普通二乘法求解ARMAX模型(不考虑噪声E)参数θ;
2)然后用θ和输入P计算出预测y_hat;
3)然后求残差,y_hat减实际y。这个残差有点等于噪声E。
4)然后将残差和P拼接在一起,继续求解ARMAX(有了残差,即考虑噪声e)参数θ。循环执行第2-4步。

迭代的停止条件可以有两种:
1)参数θ不再变化或者变化微小
2)残差(噪音E)不再变化或者变化微小

python代码如下:

		#psi : The information matrix of the model.
 		e = y[max_lag:, ] - np.dot(psi, theta)#支持多输出
        #e = y[max_lag:, 0].reshape(-1, 1) - np.dot(psi, theta)
        for i in range(30):#为什么循环30次呢?
            #e = np.concatenate([np.zeros([max_lag, 1]), e], axis=0)
            e = np.concatenate([np.zeros([max_lag, y.shape[1]]), e], axis=0)#支持多输出

            lagged_data = self.build_output_matrix(e, elag)

            e_regressors = self.basis_function.fit(
                lagged_data, max_lag, predefined_regressors=None
            )

            psi_extended = np.concatenate([psi, e_regressors], axis=1)
            unbiased_theta = getattr(self, self.estimator)(psi_extended, y)
            # e = y[max_lag:, 0].reshape(-1, 1) - np.dot(
            #     psi_extended, unbiased_theta.reshape(-1, 1)
            # )
            #支持多输出
            e = y[max_lag:, ] - np.dot(
                psi_extended, unbiased_theta
            )

        #return unbiased_theta[0 : theta.shape[0], 0].reshape(-1, 1)
        return unbiased_theta[0: theta.shape[0]] #支持多输出

可以参见知乎的文章:https://zhuanlan.zhihu.com/p/87996248

3.总体最小二乘法Total Least Squares method

在最小二乘方法中,系数矩阵P被认为是不含有误差的。然而在实际情况中,系数矩阵中的变量往往也包含观测值,因此不可避免地会被误差污染。为同时考虑系数矩阵P和观测向量Y中的误差,提出了总体最小二乘法。

Y=Pθ+E变为Y+ΔY=(P+ΔP)θ

奇异值可以求解最小二乘法,即奇异值总体最小二乘法(singular value decomposition total least squares,SVDTLS)。python代码如下:

#psi : The information matrix of the model.
y = y[self.max_lag :, 0].reshape(-1, 1)
full = np.hstack((psi, y))
n = psi.shape[1]
u, s, v = np.linalg.svd(full, full_matrices=True)
theta = -v.T[:n, n:] / v.T[n:, n:]
return theta.reshape(-1, 1)

4.递推/递归最小二乘法Recursive Least Squares method

递推最小二乘法:当矩阵维数增加时,矩阵求逆运算计算量过大,而且不适合在线辨识。为了减少计算量,并且可以实时地辨识出动态系统的特性,可以将最小二乘法转换成参数递推的估计。

递推最小二乘法主要思路是数据逐步进入ARMAX模型,而不是一下子把所有数据送入ARMAX。

对于公式:
Y=Pθ+E
其中:Y为要预测的部分,P为已知数据(包括y的t-1前数据,u的t-1前数据等),E是噪声。

暂时先不考虑噪声E,则Y=Pθ。

根据最小二乘法,我们知道模型参数:在这里插入图片描述

递推算法如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

即不断的迭代计算Q,从而不断迭代计算模型参数θ。Q的初始值可以为对角线矩阵(np.eye)。

python代码如下:
其中下面的p就是上面推算的Q,theta即θ。只是多了个k_numerator 。

		#psi : The information matrix of the model.
		y, n_theta, n, theta, self.xi = self._initial_values(y, psi)

        p = np.eye(n_theta) / self._delta

        for i in range(2, n):
            psi_tmp = psi[i, :].reshape(-1, 1)
            k_numerator = self._lam ** (-1) * p.dot(psi_tmp)
            k_denominator = 1 + self._lam ** (-1) * psi_tmp.T.dot(p).dot(psi_tmp)
            k = np.divide(k_numerator, k_denominator)
            self.xi[i, 0] = y[i, 0] - np.dot(psi_tmp.T, theta[:, i - 1])
            theta[:, i] = list(theta[:, i - 1].reshape(-1, 1) + k.dot(self.xi[i, 0]))

            p1 = p.dot(psi[i, :].reshape(-1, 1)).dot(psi[i, :].reshape(-1, 1).T).dot(p)
            p2 = (
                psi[i, :].reshape(-1, 1).T.dot(p).dot(psi[i, :].reshape(-1, 1))
                + self._lam
            )

            p_numerator = p - np.divide(p1, p2)
            p = np.divide(p_numerator, self._lam)

        self.theta_evolution = theta.copy()
        return theta[:, -1].reshape(-1, 1)
     

5.限定记忆的最小二乘法(Fixed Memory Least Squares Method)

限定记忆的最小二乘法(Fixed Memory Least Squares Method) 总是使用最近N组观测数据,每次估计时,当添入一组新的观测数据时,就丢弃一组最老的数据。因此,也叫做加矩形窗的最小二乘法。这种算法更适合于时变系统和克服数据饱和。

6.加权最小二乘法

根据实际问题考虑每个求和项的重要程度,即加权值w。
在这里插入图片描述
均方误差(MSE)是一种加权最小二乘,它的权值是概率。
LMS算法的准则是使均方误差达到最小,即期望信号与滤波器实际输出之差的平方的期望值达到最小,并且依据这个准则来修改权系数向量w,这被称为MSE准则。

7.备注

代码主要来源于SysIdentPy:
SysIdentPy is an open-source Python module for System Identification using NARMAX models built on top of numpy and is distributed under the 3-Clause BSD license. SysIdentPy provides an easy-to-use and flexible framework for building Dynamical Nonlinear Models for time series and dynamic systems.

猜你喜欢

转载自blog.csdn.net/zephyr_wang/article/details/128792156