脚本宝典收集整理的这篇文章主要介绍了拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
原文链接:http://tecdat.cn/?p=24498
原文出处:拓端数据部落公众号
在这个例子中,我们考虑马尔可夫转换随机波动率模型。
统计模型
让
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/4cacc5a8d4a2e3e6576916893f6c9193.png)
是因变量和
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/eb7e11141ebed48ae14faadfb2d90bf9.png)
未观察到的对数波动率
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/4cacc5a8d4a2e3e6576916893f6c9193.png)
. 随机波动率模型定义如下
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/6cc11809f0efbda823f8e85f1e560a01.png)
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/b5b8d4eae42838ab4ca9d4611f9b52ca.png)
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/8fd2f605056e04957192814852e1f4bd.png)
区制变量
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/159ccaa2428e42a47c772013976d5859.png)
遵循具有转移概率的二态马尔可夫过程
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/a20027ef19374412df8348a26a32635e.png)
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/267d3d62cff86ec76e65420ca649f080.png)
表示均值的正态分布
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/848d8611103364df1c73fe1b74c5b36f.png)
和方差
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/fd530be5d8eb823c1696058399acc5db.png)
.
BUGS语言统计模型
文件“ssv.bug”的内容:
-
file = 'ssv.bug'; % BUGS模型文件名
-
-
model
-
{
-
x[1] ~ dnorm(mm[1], 1/sig^2)
-
y[1] ~ dnorm(0, e
xp(-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]
安装
- 下载Matlab最新版本
- 将存档解压缩到某个文件夹中
- 将程序文件夹添加到 Matlab 搜索路径
addpath(path)
通用设置
-
-
lightblue
-
-
-
% 设置随机数生成器的种子以实现可重复性
-
-
-
else
-
rng('default')
-
end
加载模型和数据
模型参数
-
tmax = 100;
-
sig = .4;
解析编译BUGS模型,以及样本数据
-
model(file, data, 's
ample', true);
-
data = model;
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/24100a04549a4a7cb853297e799d7759.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_10,color_FFFFFF,t_70,g_se,x_16)
绘制数据
-
figure('nae', 'Lrtrs')
-
plot(1:tmax, dt.y)
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/ae21c49f7719417a94a538743208b40b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
Biips 序列蒙特卡罗SMC
运行SMC
-
n_part = 5000; % 粒子数
-
{'x'}; % 要监控的变量
-
smc = samples(npart);
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/7ffd60b3d1724a53b33852f6c5318317.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_20,color_FFFFFF,t_70,g_se,x_16)
算法的诊断。
diag (smc);
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/e84f447b6a1d49dd87d06e0560ff75b6.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_9,color_FFFFFF,t_70,g_se,x_16)
绘图平滑 ESS
-
-
-
-
plot(1:tmax, 30*(tmax,1), '--k')
-
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/2e2154d9f0fd4220ab026ea493188da1.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
绘制加权粒子
-
-
for ttt=1:tttmax
-
va =
unique(outtt.x.s.vaues(ttt,:));
-
-
wegh = arrayfun(@(x) sum(outtt.x.s.we
ITtt(ttt, outtt.x.s.vaues(ttt,:) == x)), va);
-
-
scatttttter(ttt*ones(size(va)), va, min(50, .5*n_parttt*wegh), 'r',
...
-
'markerf', 'r')
-
end
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/9407126a90c64cdfa8e342056aedbd8b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
汇总统计
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)
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/20c2b19a138b442695738163c5e8a9f0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
绘图平滑估计
-
-
mean = smcx.s.mean;
-
quant = smcx.s.quant;
-
-
plot(1:t_max, mean, 3)
-
plot(1:t_max, data.x_true, 'g')
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/b1c71c1f678743858572cc872d7ea40d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
边际滤波和平滑密度
-
-
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
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/426cad93360e468eb5439e53b6ee2022.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
Biips 粒子独立 Metropolis-Hastings
PIMH 参数
-
-
thi= 1;
-
nprt = 50;
运行 PIMH
-
-
upda(obj, urn, npat); % 预烧迭代
-
sample(obj,...
-
nier, npat, 'thin', thn);
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/ab890cfeca7042b3a897af2ee13ebad4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_20,color_FFFFFF,t_70,g_se,x_16)
一些汇总统计
summary(out, 'prs');
后均值和分位数
-
mean = sumx.man;
-
quant = su.x.qunt;
-
-
hold on
-
plot(1:tax, man, 'r', 'liith', 3)
-
plot(1:tax, xrue, 'g')
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/c507bf75ce86452da766b0cb90541e27.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
MCMC 样本的踪迹
-
-
for k=1:nmel(timndx)
-
-
-
plot(outm.x(tk, :), 'liedh', 1)
-
hold on
-
plot(0, d_retk), '*g');
-
box off
-
end
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/5a24a768fef941aeb8ed3df9ea8cfdba.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
后验直方图
-
for k=1:numel(tim_ix)
-
tk = tim_ix(k);
-
subplot(2, 2, k)
-
hist(o_hx(tk, :), 20);
-
h = fidobj(gca, 'y
PE, 'ptc'); hold on
-
plot(daau(k), 0, '*g');
-
-
box off
-
end
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/f3c8c4b1ae7a41d7bcc361646fa4743a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
后验的核密度估计
-
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
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/347b61e21d9f466a88304c69122abd68.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
Biips 敏感性分析
我们想研究对参数值的敏感性
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/img_convert/e3e43d334ae536d27c6fc0c4908ff051.png)
算法参数
-
n= 50; % 粒子数
-
para = {'alpha}; % 我们要研究灵敏度的参数
-
% 两个分量的值网格
-
使用 SMC 运行灵敏度分析
smcs(modl, par, parvlu, npt);
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/21e16b344bbe47d3a7c7879deff87428.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_20,color_FFFFFF,t_70,g_se,x_16)
绘制对数边际似然和惩罚对数边际似然率
-
surf(A, B, re
Shape(ouma_i, sizeA)
-
box off
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/818b75ea39ba4c58b7f342946c1b0e1a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouT56uv56CU56m25a6k,size_17,color_FFFFFF,t_70,g_se,x_16)
![拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据 拓端tecdat|Matlab用BUGS马尔可夫区制转换Markov switching随机波动率SV模型、序列蒙特卡罗SMC、Metropolis Hastings采样分析时间序列数据](https://pic.wangt.cc/download/pic_router.php?path=https://img-blog.csdnimg.cn/02ee709c4aa54123b228572071806ce7.png)
最受欢迎的见解
1.用R语言模拟混合制排队随机服务排队系统
2.R语言中使用排队论预测等待时间
3.R语言中实现马尔可夫链蒙特卡罗MCMC模型
4.R语言中的马尔科夫机制转换(Markov regime switching)模型
5.matlab贝叶斯隐马尔可夫hmm模型
6.用R语言模拟混合制排队随机服务排队系统
7.Python基于粒子群优化的投资组合优化
8.R语言马尔可夫转换模型研究交通伤亡人数事故预测
9.用机器学习识别不断变化的股市状况——隐马尔可夫模型的应用