此示例显示了如何在状态空间模型中估计状态的随机,自回旋系数。也就是说,此示例使用“ ZIG-ZAG”估计方法对状态空间模型参数估计进行贝叶斯视图。
假设两个国家( and 在th)代表两个国家的净出口e end of the year.
is a unit root process with a disturbance variance of 。
是一个AR(1)过程,具有未知,随机系数和干扰方差 。
An observation ( )is the exact sum of the two net exports. That is, the net exports of the individual states are unknown.
象征性地,真正的状态空间模型是
从以下方式模拟100年的净出口:
具有差异的平均零高斯噪声系列的单元根过程 。
An AR(1) process with an autoregressive coefficient of 0.6 and a mean zero, Gaussian noise series that has variance 。
。
Create an observation series by summing the two net exports per year.
rng(100);% For reproducibilityT = 150; sigma1 = 0.1; sigma2 = 0.2; phi = 0.6; u1 = randn(T,1)*sigma1; x1 = cumsum(u1); Mdl2 = arima('ar',,,,phi,'方差',sigma2^2,'Constant',0);x2 =仿真(mdl2,t,'y0',0);y=X1+X2;数字;plot([x1 x2 y]) legend('x_1',,,,'x_2',,,,'y',,,,'Location',,,,'最好的');ylabel('净出口');Xlabel('时期');
Treat 好像它是未知的和随机的,并使用Zig-Zag方法恢复其分布。实现Zig-Zag方法:
1.选择一个初始值 在(-1,1)的间隔中,表示 。
2。Create the true state-space model, that is, anSSM
代表数据生成过程的模型对象。
3. Use the simulation smoother (simsmooth
)to draw a random path from the distribution of the second smoothed states. Symbolically,
。
4.创建另一个具有此形式的状态空间模型
In words, 是静态状态和 is an "observed" series with time varying coefficient 。
5.使用模拟更顺畅地从平滑的分布中绘制随机路径 系列。象征性地 ,,,,where 涵盖了真实状态空间模型和观测值的结构。 是静态的,因此您可以保留一个值( )。
6.重复步骤2-5多次并存储 each iteration.
7. Perform diagnostic checks on the simulation series. That is, construct:
Trace plots to determine the burn in period and whether the Markov chain is mixing well.
自相关图确定需要删除多少平局才能获得混合良好的马尔可夫链。
8.其余系列表示从后部分布中得出的 。You can compute descriptive statistics, or plot a histogram to determine the qualities of the distribution.
Specify initial values, preallocate, and create the true state-space model.
phi0 = -0.3;PHI初始值的%z = 1000;% Number of times to iterate the zig-zag methodphiz = [phi0;Nan(Z,1)];% Preallocatea = [1 0;0 nan];b = [sigma1;sigma2];C = [1 1];mdl = ssm(a,b,c,'statepe',,,,[2; 0]);
MDL是一个SSM
模型对象。这南
充当占位符
。
曲折方法的迭代步骤2-5。
为了j = 2 :( z + 1);% Draw a random path from smoothed x_2 series.Xz=simsmooth(Mdl,y,“参数”,,,,phiz(j-1));% The second column of xz is a draw from the posterior distribution of x_2.% Create the intermediate state-space model.az = 1;bz = 0;cz = num2cell(xz((1:(end -end -1)),2));dz = sigma2;mdlz = SSM(AZ,BZ,CZ,DZ,'statepe',2);%从平滑的PHIZ系列中绘制一个随机路径。phizvec = simsmooth(Mdlz,xz(2:end,2)); phiz(j) = phizvec(1);%phiz(j)是从PHI的后验分布中抽取的结尾
phiz
是马尔可夫链。在分析后验分布之前
,,,,you should assess whether to impose a burn-in period, or the severity of the autocorrelation in the chain.
Draw a trace plot for the first 100, 500, and all of the random draws.
vec = [100 500 Z]; figure;为了j = 1:3;子图(3,1,j)图(phiz(1:vec(j)));标题('Trace Plot for \phi');Xlabel(“仿真号码”);轴tight;结尾
According to the first plot, transient effects die down after about 20 draws. Therefore, a short burn-in period should suffice. The plot of the entire simulation shows that the series settles around a center.
删除前20个绘制后,绘制该系列的自相关函数。
倦怠= 21:z;数字;Autocorr(Phiz(倦怠));
这autocorrelation function dies out rather quickly. It doesn't seem like autocorrelation in the chain is an issue.
确定后部分布的质量 通过计算模拟统计数据并绘制简化的随机抽奖集的直方图。
xbar =平均值(phiz(倦怠))
XBAR = 0.5104
XSTD = STD(PHIZ(倦怠))
Xstd = 0.0988
ci = norminv([0.025,0.975],xbar,xstd);% 95% confidence interval数字;histogram(phiz(burnOut),'Normalization',,,,'PDF');H = GCA;抓住on;simx = linspace(H.Xlim(1),H.Xlim(2),100);simpdf = normpdf(simx,xbar,xstd);情节(Simx,Simpdf,'k',,,,'LineWidth',2);h1 = plot([xbar xbar],h.YLim,'r',,,,'LineWidth',2);h2 =图([0.6 0.6],h.ylim,,'g',,,,'LineWidth',2);H3 =图([CI(1)CI(1)],H.Ylim,,' - r',,,,...[CI(2)CI(2)],H.ylim,,' - r',,,,'LineWidth',2);图例([H1 H2 H3(1)],{'Simulation Mean',,,,“真正的卑鄙”,,,,'95% CI'}); h.XTick = sort([h.XTick xbar]); h.XTickLabel{h.XTick == xbar} = xbar; h.XTickLabelRotation = 90;
Method: Maximum likelihood (fminunc) Sample size: 150 Logarithmic likelihood: -10.1434 Akaike info criterion: 22.2868 Bayesian info criterion: 25.2974 | Coeff Std Err t Stat Prob ------------------------------------------------------- c(1) | 0.53590 0.19183 2.79360 0.00521 | | Final State Std Dev t Stat Prob x(1) | -0.85059 0.00000 -6.45811e+08 0 x(2) | 0.00454 0 Inf 0
后分布 大约正常,平均值和标准偏差约为0.51和0.1。真实的卑鄙 为0.6,它小于模拟平均值右侧的一个标准偏差。
计算最大似然估计
。也就是说
作为固定但未知参数,然后估算Mdl
使用卡尔曼过滤器和最大似然。
[〜,estparams] =估计(MDL,Y,PHI0)
estParams = 0.5359
Mle的 为0.54。这两个估计都在一个标准偏差或标准误差范围内 。