关于数据挖掘:Matlab用BUGS马尔可夫区制转换Markov-switching随机波动率模型序列蒙特卡罗MH采样分析时间序列数据

25次阅读

共计 2828 个字符,预计需要花费 8 分钟才能阅读完成。

原文链接:http://tecdat.cn/?p=24498

在这个例子中,咱们思考马尔可夫转换随机稳定率模型。

统计模型

让  是因变量和  未察看到的对数稳定率 . 随机稳定率模型定义如下 

区制变量  遵循具备转移概率的二态马尔可夫过程

 示意均值的正态分布  和方差 .

BUGS 语言统计模型

文件“ssv.bug”的内容:

file = 'ssv.bug'; % BUGS 模型文件名

model
{x\[1\] ~ dnorm(mm\[1\], 1/sig^2)
  y\[1\] ~ dnorm(0, exp(-x\[1\]))

  for (t in 2:tmax)
  {c\[t\] ~ dcat(ifelse(c\[t-1\]==1, pi\[1,\], pi\[2,\]))
    mm\[t\] <- alp\[1\] * (c\[t\]==1) + alp\[2\]*(c\[t\]==2) + ph*x\[t-1\]

装置

  1. 下载 Matlab 最新版本
  2. 将存档解压缩到某个文件夹中
  3. 将程序文件夹增加到 Matlab 搜寻门路
addpath(path)

通用设置

lightblue 
lightred 

% 设置随机数生成器的种子以实现可重复性
if eLan 'matlab', '7.2')
    rnd('state', 0)
else
    rng('default')
end

加载模型和数据

模型参数

tmax = 100;
sig = .4;

解析编译 BUGS 模型,以及样本数据

model(file, data, 'sample', true);
data = model;

绘制数据

figure('nae', 'Lrtrs')
plot(1:tmax, dt.y)

Biips 序列蒙特卡罗 SMC

运行 SMC

n_part = 5000; % 粒子数
  {'x'}; % 要监控的变量
 smc =  samples(npart);

算法的诊断。

diag   (smc);

 

绘图平滑 ESS

sem(ess)

plot(1:tmax, 30*(tmax,1), '--k')

绘制加权粒子

for ttt=1:tttmax
    va = unique(outtt.x.s.vaues(ttt,:));

    wegh = arrayfun(@(x) sum(outtt.x.s.weittt(ttt, outtt.x.s.vaues(ttt,:) == x)), va);

    scatttttter(ttt\*ones(size(va)), va, min(50, .5\*n_parttt*wegh), 'r',...
        'markerf', 'r')
end

汇总统计

summary(out, 'pro', \[.025, .975\]);

绘图滤波预计

mean = susmc.x.f.mean;
xfqu = susmc.x.f.quant;
h = fill(\[1:tmax, tmax:-1:1\], \[xfqu{1}; flipud(xfqu{2})\], 0);

plot(1:tmax, mean,)
plot(1:tmax, data.x_true)

绘图平滑预计

mean = smcx.s.mean;
quant = smcx.s.quant;

plot(1:t_max, mean,  3)
plot(1:t\_max, data.x\_true, 'g')

边际滤波和平滑密度

kde = density(out);
for k=1:numel(time)
    tk = time(k);
    plot(kde.x.f(tk).x, kde.x.f(tk).f);
    hold on
    plot(kde.x.s(tk).x, kde.x.s(tk).f, 'r');
    plot(data.xtrue(tk));
    box off
end

Biips 粒子独立 Metropolis-Hastings

PIMH 参数

thi= 1;
nprt = 50;

运行 PIMH

init(moel, vaibls);
upda(obj, urn, npat); % 预烧迭代
sample(obj,...
    nier, npat, 'thin', thn);

一些汇总统计

summary(out, 'prs');

后均值和分位数

mean = sumx.man;
quant = su.x.qunt;

hold on
plot(1:tax, man, 'r', 'liith', 3)
plot(1:tax, xrue, 'g')

MCMC 样本的形迹

for k=1:nmel(timndx)
    tk = tieinx(k);
    sublt(2, 2, k)
    plot(outm.x(tk, :), 'liedh', 1)
    hold on
    plot(0, d_retk), '*g');
    box off
end

后验直方图

for k=1:numel(tim_ix)
    tk = tim_ix(k);
    subplot(2, 2, k)
    hist(o_hx(tk, :), 20);
    h = fidobj(gca, 'ype,'ptc');    hold on
    plot(daau(k), 0, '*g');
   
    box off
end

后验的核密度估计

pmh = desity(otmh);
for k=1:numel(tenx)
    tk = tim_ix(k);
    subplot(2, 2, k)
    plot(x(t).x, dpi.x(tk).f, 'r');
    hold on
    plot(xtrue(tk), 0, '*g');
    box off
end

Biips 敏感性剖析

咱们想钻研对参数值的敏感性 

算法参数

n= 50; % 粒子数
para = {'alpha}; % 咱们要钻研灵敏度的参数
 % 两个重量的值网格
pvs = {A(:, B(:';

应用 SMC 运行灵敏度剖析

smcs(modl, par, parvlu, npt);

绘制对数边际似然和惩办对数边际似然率

surf(A, B, reshape(ouma_i, sizeA)
box off


最受欢迎的见解

1. 用 R 语言模仿混合制排队随机服务排队零碎

2.R 语言中应用排队论预测等待时间

3.R 语言中实现马尔可夫链蒙特卡罗 MCMC 模型

4.R 语言中的马尔科夫机制转换 (Markov regime switching) 模型模型 ”)

5.matlab 贝叶斯隐马尔可夫 hmm 模型

6. 用 R 语言模仿混合制排队随机服务排队零碎

7.Python 基于粒子群优化的投资组合优化

8.R 语言马尔可夫转换模型钻研交通伤亡人数事变预测

9. 用机器学习辨认一直变动的股市情况——隐马尔可夫模型的利用

正文完
 0