拓端tecdat|Matlab用BUGS馬爾可夫區(qū)制轉(zhuǎn)換Markov switching隨機(jī)波動率SV模型、序列蒙
原文鏈接:http://tecdat.cn/?p=24498
原文出處:拓端數(shù)據(jù)部落公眾號
在這個例子中,我們考慮馬爾可夫轉(zhuǎn)換隨機(jī)波動率模型。
統(tǒng)計模型
讓?
?是因變量和?
?未觀察到的對數(shù)波動率?
. 隨機(jī)波動率模型定義如下?
區(qū)制變量?
?遵循具有轉(zhuǎn)移概率的二態(tài)馬爾可夫過程
?表示均值的正態(tài)分布?
?和方差?
.
BUGS語言統(tǒng)計模型
文件“ssv.bug”的內(nèi)容:
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]
安裝
下載Matlab最新版本
將存檔解壓縮到某個文件夾中
將程序文件夾添加到 Matlab 搜索路徑
addpath(path)
通用設(shè)置
lightblue
lightred
% 設(shè)置隨機(jī)數(shù)生成器的種子以實(shí)現(xiàn)可重復(fù)性
if eLan 'matlab', '7.2')
rnd('state', 0)
else
rng('default')
end
加載模型和數(shù)據(jù)
模型參數(shù)
tmax = 100;
sig = .4;
解析編譯BUGS模型,以及樣本數(shù)據(jù)
model(file, data, 'sample', true);
data = model;
繪制數(shù)據(jù)
figure('nae', 'Lrtrs')
plot(1:tmax, dt.y)
Biips 序列蒙特卡羅SMC
運(yùn)行SMC
n_part = 5000; % 粒子數(shù)
{'x'}; % 要監(jiān)控的變量
smc = ?samples(npart);
算法的診斷。
diag ? (smc);
?
繪圖平滑 ESS
sem(ess)
plot(1:tmax, 30*(tmax,1), '--k')
繪制加權(quán)粒子
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
匯總統(tǒng)計
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 粒子獨(dú)立 Metropolis-Hastings
PIMH 參數(shù)
thi= 1;
nprt = 50;
運(yùn)行 PIMH
init(moel, vaibls);
upda(obj, urn, npat); % 預(yù)燒迭代
sample(obj,...
nier, npat, 'thin', thn);
一些匯總統(tǒng)計
summary(out, 'prs');
后均值和分位數(shù)
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 敏感性分析
我們想研究對參數(shù)值的敏感性?
算法參數(shù)
n= 50; % 粒子數(shù)
para = {'alpha}; % 我們要研究靈敏度的參數(shù)
% 兩個分量的值網(wǎng)格
pvs = {A(:, B(:';
使用 SMC 運(yùn)行靈敏度分析
smcs(modl, par, parvlu, npt);
繪制對數(shù)邊際似然和懲罰對數(shù)邊際似然率
surf(A, B, reshape(ouma_i, sizeA)
box off
最受歡迎的見解
1.用R語言模擬混合制排隊隨機(jī)服務(wù)排隊系統(tǒng)
2.R語言中使用排隊論預(yù)測等待時間
3.R語言中實(shí)現(xiàn)馬爾可夫鏈蒙特卡羅MCMC模型
4.R語言中的馬爾科夫機(jī)制轉(zhuǎn)換(Markov regime switching)模型
5.matlab貝葉斯隱馬爾可夫hmm模型
6.用R語言模擬混合制排隊隨機(jī)服務(wù)排隊系統(tǒng)
7.Python基于粒子群優(yōu)化的投資組合優(yōu)化
8.R語言馬爾可夫轉(zhuǎn)換模型研究交通傷亡人數(shù)事故預(yù)測
9.用機(jī)器學(xué)習(xí)識別不斷變化的股市狀況——隱馬爾可夫模型的應(yīng)用