在matlab中,我想拟合一个分段回归,并找到x轴上第一个变点发生的位置。例如,对于下面的数据,输出可能是changepoint=20
(我实际上不想绘制它,只想得到变化点)。
data = [1 4 4 3 4 0 0 4 5 4 5 2 5 10 5 1 4 15 4 9 11 16 23 25 24 17 31 42 35 45 49 54 74 69 63 46 35 31 27 15 10 5 10 4 2 4 2 2 3 5 2 2];
x = 1:52;
plot(x,data,'.')
字符串
3条答案
按热度按时间92dk7w1h1#
如果您有信号处理工具箱,则可以直接使用
findchangepts
函数(有关文档,请参阅https://www.mathworks.com/help/signal/ref/findchangepts.html):字符串
在这种情况下,变化点的索引是22。
数据图及其变化点以红色圈出:
的数据
xytpbqjk2#
我知道这是一个老问题,但我只是想提供一些额外的想法。在Maltab中,我实现的一个替代方案是贝叶斯变点检测算法,该算法不仅估计变点的数量和位置,还报告变点的发生概率。在当前的实现中,它只处理类似时间序列的数据(也称为1D顺序数据)。有关该工具的更多信息,请参阅此FileExchange条目(https://www.mathworks.com/matlabcentral/fileexchange/72515-bayesian-changepoint-detection-time-series-decomposition)。
下面是它在示例数据中的快速应用:
字符串
下面是由printbeast()提供的变更点摘要:
型
以下是图形输出。检测到三个主要的变化点:
的数据
ogq8wdun3#
你可以使用sgolayfilt函数,也就是多项式拟合数据,或者重现OLS方法:http://www.utdallas.edu/~herve/Abdi-LeastSquares06-pretty.pdf(有 a+bx 符号而不是 ax+b)
对于 ax+B 的线性拟合:
如果用长度为2n+1的常数向量替换x:[-n,…0…n]在每一步中,您将获得以下用于滑动回归系数的代码:
字符串
请注意,在此代码中,B表示数据的滑动平均值,a是最小二乘斜率估计值(一阶导数)。