【突变检验方法二】MATLAB实现贝叶斯突变检测

MATLAB实现贝叶斯突变检测

  • 1 贝叶斯突变检测
  • 2 原理
  • 3 MATLAB相关代码
    • 3.1 调用函数
    • 3.2 案例
  • 参考
  • 另:其它语言实现贝叶斯突变检测

1 贝叶斯突变检测

贝叶斯突变检测属于概率突变检测方法,其特点是能给出突变点的概率分布图。

2 原理

3 MATLAB相关代码

3.1 调用函数

Matlab贝叶斯突变检测1:调用函数

function P=BMDCP(Y,R,M)
%% Bayesian Method for Detecting Change Points
%% 输入参数列表
% Y        时间序列原始数据
% R        控制参数Var0/Var,Var为全序列方差,Var0为均值方差,R应大于等于4
% M        端点间隔,取值3~5
%% 输入参数列表
% P        突变点的概率分布
%% 第一步:数据归一化处理
%归一化处理的目的在于避免浮点计算时溢出,属程序设计时考虑的问题,它不影响结果
Y=Y./max(Y);
%% 第二步:计算后验分布的数字特征
N=length(Y);%序列的长度
Mu0=mean(Y);%全序列的均值
Var=var(Y);%全序列方差
MuA=zeros(N,1);
MuB=zeros(N,1);
VarB=zeros(N,1);
for i=M:(N-M)
YA=Y(1:i);%左子序列
YB=Y((i+1):N);%右子序列
VarA(i)=Var/(1/R+i);
VarB(i)=Var/(1/R+N-i);
end
MuA(1:(M-1))=MuA(M);
MuB(1:(M-1))=MuB(M);
MuA((N-M+1):N)=MuA(N-M);
MuB((N-M+1):N)=MuB(N-M);
%% 第三步:计算后验概率分布
P=zeros(N,1);
for k=M:(N-M)
A=0;
for i=1:k
% 注意:这里对似然函数进行了取对数处理,也是为了防止CPU溢出
a=log((1/sqrt(2*pi*Var))*exp(-(Y(i)-MuA(i))^2/(2*Var)));
end
B=0;
for j=(k+1):N
b=log((1/sqrt(2*pi*Var))*exp(-(Y(j)-MuB(j))^2/(2*Var)));
end
P(k)=A+B;
end
%% 第四步:计算突变点的概率分布图
P=exp(P);
P=P/sum(P);
maxP=max(P);
bar(1:N,P);
axis([1,N,0,1.2*maxP]);
hold on
p0=(1/N)*ones(N,1);
plot(1:N,p0,'--r');
legend('后验概率','先验概率');
xlabel('k (month)','FontName','TimesNewRoman','FontSize',12);
ylabel('P(k)','FontName','TimesNewRoman','Fontsize',12);

Matlab贝叶斯突变检测2:主函数

%% Get a test dataset
% Download testDat.mat from gitHub and then specify the path where the file is saved.
% The folder used below is just a placeholder.
load('testData.mat');
%% Example1: Take a look at the downloaded dataset
% "testData" contains two time series: simData and landsat
% First, let us look at simData. It is a struct variable consisting of
% ".Y":          A simulated time-series signal
% ".trueSeason": The true seasonal component used in the simulation
% ".trueTrend":  The true trend component used in the simulation
% Of particulate note, this is the simulated time series used for Example 1 
% in our RSE paper (Zhao et al., 2019).
Y     = simData.Y;
S_true = simData.trueSeason;
T_true = simData.trueTrend;
clf
subplot(3,1,1)
plot(Y);
hold on;
plot(S_true+T_true);
subplot(3,1,2);
plot(S_true);
subplot(3,1,3);
plot(T_true);
%% Set up the parameters needed for the BEAST algorithm
% Some of these parameters are the model specficiation parameters of BEAST
% (e.g., minSeasonOrder, maxSeasonOrder, minSetpDist_trend,
% minSepDist_Season); other parameters are just some input variables to
% control simulation behaviors or program outputs (e.g., samples,
% thinningFactor, seed, computeCredible).
% 
opt.period    = 24;  
opt.minSeasonOrder = 1;
opt.maxSeasonOrder = 6;
opt.minTrendOrder=0;
opt.maxTrendOrder=1;
opt.minSepDist_Trend  =  24;
opt.minSepDist_Season =  24;
opt.maxKnotNum_Trend =  8;
opt.maxKnotNum_Season = 8; 
opt.maxMoveStepSize   = 40;
opt.samples = 10000;
opt.thinningFactor = 1;
opt.burnin = 200;
opt.chainNumber=2;
opt.resamplingTrendOrderProb=0.2;
opt.resamplingSeasonOrderProb=0.17;
opt.omissionValue=-999;
opt.seed=100;
opt.computeCredible=0;
opt.computeSlopeSign=1;
opt.algorithm='beast';
opt.computeHarmonicOrder=1;
opt.computeTrendOrder=1;
opt.computeChangepoints=1;
%opt.timeDimensionIndex=3;
%% Run BEAST on "Y"
tic
out=beast_default(Y, opt);
toc
%% Plot the result
clf
%plot the detected seasonal component
subplot(6,1,1); plot(out.s)     
%plot the seasonal-changepoint probability (i.e., the probability of observinng a seasoanl changepoint over time)
subplot(6,1,2); plot(out.sProb) 
%Go back to Subplot#1 and plot the most likely locations of seasonal
%changepoints (scp)
%------------------------------------------------------------------------------------------
subplot(6,1,1);
% out.scp saves the most probabe scp locations. It is filled with NaNs if
% the detected scp number is less than the specified
%"opt.maxKnotNum_Season".
numOfScp = length( find(~isnan(out.scp) )) ;
hold on;
for index=1:numOfScp
    scpLoc = out.scp(index);
    plot([scpLoc,scpLoc],[-2,2] ,'r');
end
%------------------------------------------------------------------------------------------
%BEAST also estimates the harmonic orders needed to sufficiently to
%approximate the seasoanl component. The result is outputted to out.horder.
%"horder" varies with time.
subplot(6,1,3);plot(out.horder);
%plot the detected trend
subplot(6,1,4);plot(out.t); 
%------------------------------------------------------------------------------------------
hold on;
% out.tcp saves the most probabe locations of trend changepoints (tcp). It is filled with NaNs if
% the detected tcp number is less than the specified "opt.maxKnotNum_Trend".
numOfTcp = length( find(~isnan(out.tcp) )) ;
hold on;
for index=1:numOfTcp
    tcpLoc = out.tcp(index);
    plot([tcpLoc,tcpLoc],[-.5,.5] ,'r');
end
%------------------------------------------------------------------------------------------
%plot the tcp probability (i.e., the probability of detecting a tcp over time)
subplot(6,1,5);plot(out.tProb);  
%BEAST also estimates the trend order needed to sufficiently to
%approximate the seasoanl component. The result is outputted to out.torder.
%"torder" varies with time.
% By "trend order", the linear segment can be either a constant (zero-th
% order) or a sloped line (1st order). "torder" gives the mean order over
% the sampled models.
subplot(6,1,6);plot(out.torder);  

3.2 案例

参考

1.代码参考-Matlab、R语言、Python贝叶斯突变检测

另:其它语言实现贝叶斯突变检测

1.R语言实现贝叶斯突变检测

你可能感兴趣的:(#,MATLAB实现各种基础方法,matlab)