花露水易燃代码 卡曼滤波(贝叶斯估计)——Matlab-金融计量学家
发布时间: 2019-08-13 浏览: 309
代码 卡曼滤波(贝叶斯估计)——Matlab-金融计量学家
封面图片源自网络
接下来,我将分期介绍我最新的研究成果《区制转移混频Nelson-Siegel期限结构模型及实证分析》。今天我们介绍论文中贝叶斯估计方法下卡曼滤波的matlab实现。广义模型如下:
具体化的模型如下:
下面我们来介绍代码:
function AT_draw = draw_kalman_filter(lambda,SINGMA_H,OMIGA_ff,Phi搜神传粤语 ,p00,p11游千惠 ,mu1,mu0,stt_tr,YDATA碧丝兰卡,YY_index)
maturity = [3 6 9 12 24 36 48 60 72 84 96 108 120];
k1 = length(maturity);
% YDATA= xlsread('YDATA.xls');
% YY_index = isnan(YDATA);
T = 198;
k2=3;
Lambda = zeros(k1,3);
tau=maturity';
Lambda(:,1)=ones(k1,1);
Lambda(:,2)=(1-exp(-tau*lambda))./(tau*lambda);
Lambda(:,3)=(1-exp(-tau*lambda))./(tau*lambda)-exp(-lambda*tau);
ZZ = zeros(k1+k2父子神探,8); %%%define the coefficient of measurement equation
ZZ(1:k1,1:3) = Lambda; ZZ(k1+1,4) = 1; ZZ(k1+2,5) = 1; %% for high frequency macroeconomics
ZZ(k1+k2,6:end) =[1/3 1/3 1/3];
%% define the covariance matrix of measurement equation%%%
sigma_B=SINGMA_H*ones(k1丽科吉 ,1);
SIGMA=diag(sigma_B);
SIGMA_B=zeros(k1+k2,k1+k2);
SIGMA_B(1:k1,1:k1)=SIGMA;
%% define the coefficient of transition equation %%%
F=zeros(8,8);
F(1:3+k2,1:3+k2)=Phi';
F(3+k2+1:end,3+k2:7)=eye(2);
%% define the covariance matrix of transition equation%%%
OMIG=zeros(8范小勤 ,8);
OMIG(1:3+k2,1:3+k2)=OMIGA_ff;
%% steady state prob %%
prob_1=(1-p00)/(2-p11-p00);
prob_0=1-prob_1;
%% define mu %%%
mu_s1 = [mu1 0 0]';
mu_s0 = [mu0 0 0]';
stt = stt_tr';
%% the kalman filter %%
BATA_00 = [2 -1 -1 -1 15 8 8 8]';
% P_0=inv(eye(7^2)-kron(F,F))*vec(OMIG);
% P_00=[P_0(1:7),P_0(8:14),P_0(15:21)乌青体,P_0(22:28),P_0(29:35),P_0(36:42)花香飘满衣,P_0(43:49)];
% BATA_00 = pinv(eye(7)-F)*mu; %% singular ?
% BATA_00 = [2;-1;-1;-1;8;8;8];
P_00=diag([ones(3,吴幼坚1);0.5*ones(k2+2,1)]);
BATA=zeros(8东湖星岛,T);
PATA = zeros(8,8,T);
for t=1:T ;
%%% prediction
% BATA_01=F*BATA_00+mu1*stt(t,1)+mu0*(1-stt(t,1))-F*(mu1*stt(t-1,1)+mu0*(1-stt(t-1,1)));
% BATA_11=F*BATA_00+mu
BATA_01=F*BATA_00+mu_s1*stt(t相公十四,1)+mu_s0*(1-stt(t,1));
P_01=F*P_00*F'+OMIG;
P_01=0.5*(P_01+P_01'); %%% method like minisoda %%%
% Define New Data (ND) and New Z matrix (NZ)
NY = delif(YDATA(t,花露水易燃 :)'普汇云通 ,YY_index(t,:)');
NZ = delif(ZZ,YY_index(t,:)');
NSIGMA=delif_doub(SIGMA_B,YY_index(t梦见河水上涨,:)');
yita_01=NY-NZ*BATA_01;
f_01=NZ*P_01*NZ'+NSIGMA;
f_01=0.5*(f_01+f_01'); %%% method like minisoda %%%
%%% %%%% updation
BATA_00=BATA_01+P_01*NZ'*pinv( f_01)*yita_01;
P_00=P_01-P_01*NZ'*pinv( f_01)*NZ*P_01;
BATA(:,t)=BATA_00;
PATA(:凑贝网,:,t) = P_00;
end
BAt_mat = BATA;
BPt_mat = PATA;
AT_draw = zeros(T,8);
%% the last iteration %%
% singular value decomposition
[u s v] = svd(BPt_mat(:,:,T));
Pchol = u*sqrt(s);
AT_draw(end,:) = BAt_mat(:桑芽 ,T)'+(Pchol*randn(8,1))';
for i = 1:T-1
BAtt = BAt_mat(:,T-i);
BPtt = BPt_mat(:,:,T-i);
% the covariance of prediction erro
BPhat = F * BPtt * F' + OMIG;
BPhat = 0.5*(BPhat+BPhat');
[up sp vp] = svd(BPhat);
inv_sp = zeros(size(sp,1),size(sp,1));
for rr=1:size(sp,1)
if sp(rr,rr)>1e-12;
inv_sp(rr,rr)=1./sp(rr淮安视听网,rr);
end
end
inv_BPhat = up*inv_sp*vp';
% Bnut = BAt_mat(:,T-i+1)-F*BAtt -mu1*stt(t,1)-mu0*(1-stt(t,1))+F*(mu1*stt(t-1,1)+mu0*(1-stt(t-1大唐小郎中,1)));
Bnut = BAt_mat(:,T-i+1)-F*BAtt -mu_s1*stt(t混元教主 ,1)-mu_s0*(1-stt(t,1));
% updating %
Amean = BAtt + (BPtt*F')*inv_BPhat*Bnut;
Pmean = BPtt - (BPtt*F')*inv_BPhat*(BPtt*F')';
% singular value decomposition
[um sm vm] = svd(Pmean);
Pmchol = um*sqrt(sm);
AT_draw(T-i,:) = (Amean+Pmchol*randn(8,1))';
end
AT_draw=AT_draw';
end
封面图片源自网络
接下来,我将分期介绍我最新的研究成果《区制转移混频Nelson-Siegel期限结构模型及实证分析》。今天我们介绍论文中贝叶斯估计方法下卡曼滤波的matlab实现。广义模型如下:
具体化的模型如下:
下面我们来介绍代码:
function AT_draw = draw_kalman_filter(lambda,SINGMA_H,OMIGA_ff,Phi搜神传粤语 ,p00,p11游千惠 ,mu1,mu0,stt_tr,YDATA碧丝兰卡,YY_index)
maturity = [3 6 9 12 24 36 48 60 72 84 96 108 120];
k1 = length(maturity);
% YDATA= xlsread('YDATA.xls');
% YY_index = isnan(YDATA);
T = 198;
k2=3;
Lambda = zeros(k1,3);
tau=maturity';
Lambda(:,1)=ones(k1,1);
Lambda(:,2)=(1-exp(-tau*lambda))./(tau*lambda);
Lambda(:,3)=(1-exp(-tau*lambda))./(tau*lambda)-exp(-lambda*tau);
ZZ = zeros(k1+k2父子神探,8); %%%define the coefficient of measurement equation
ZZ(1:k1,1:3) = Lambda; ZZ(k1+1,4) = 1; ZZ(k1+2,5) = 1; %% for high frequency macroeconomics
ZZ(k1+k2,6:end) =[1/3 1/3 1/3];
%% define the covariance matrix of measurement equation%%%
sigma_B=SINGMA_H*ones(k1丽科吉 ,1);
SIGMA=diag(sigma_B);
SIGMA_B=zeros(k1+k2,k1+k2);
SIGMA_B(1:k1,1:k1)=SIGMA;
%% define the coefficient of transition equation %%%
F=zeros(8,8);
F(1:3+k2,1:3+k2)=Phi';
F(3+k2+1:end,3+k2:7)=eye(2);
%% define the covariance matrix of transition equation%%%
OMIG=zeros(8范小勤 ,8);
OMIG(1:3+k2,1:3+k2)=OMIGA_ff;
%% steady state prob %%
prob_1=(1-p00)/(2-p11-p00);
prob_0=1-prob_1;
%% define mu %%%
mu_s1 = [mu1 0 0]';
mu_s0 = [mu0 0 0]';
stt = stt_tr';
%% the kalman filter %%
BATA_00 = [2 -1 -1 -1 15 8 8 8]';
% P_0=inv(eye(7^2)-kron(F,F))*vec(OMIG);
% P_00=[P_0(1:7),P_0(8:14),P_0(15:21)乌青体,P_0(22:28),P_0(29:35),P_0(36:42)花香飘满衣,P_0(43:49)];
% BATA_00 = pinv(eye(7)-F)*mu; %% singular ?
% BATA_00 = [2;-1;-1;-1;8;8;8];
P_00=diag([ones(3,吴幼坚1);0.5*ones(k2+2,1)]);
BATA=zeros(8东湖星岛,T);
PATA = zeros(8,8,T);
for t=1:T ;
%%% prediction
% BATA_01=F*BATA_00+mu1*stt(t,1)+mu0*(1-stt(t,1))-F*(mu1*stt(t-1,1)+mu0*(1-stt(t-1,1)));
% BATA_11=F*BATA_00+mu
BATA_01=F*BATA_00+mu_s1*stt(t相公十四,1)+mu_s0*(1-stt(t,1));
P_01=F*P_00*F'+OMIG;
P_01=0.5*(P_01+P_01'); %%% method like minisoda %%%
% Define New Data (ND) and New Z matrix (NZ)
NY = delif(YDATA(t,花露水易燃 :)'普汇云通 ,YY_index(t,:)');
NZ = delif(ZZ,YY_index(t,:)');
NSIGMA=delif_doub(SIGMA_B,YY_index(t梦见河水上涨,:)');
yita_01=NY-NZ*BATA_01;
f_01=NZ*P_01*NZ'+NSIGMA;
f_01=0.5*(f_01+f_01'); %%% method like minisoda %%%
%%% %%%% updation
BATA_00=BATA_01+P_01*NZ'*pinv( f_01)*yita_01;
P_00=P_01-P_01*NZ'*pinv( f_01)*NZ*P_01;
BATA(:,t)=BATA_00;
PATA(:凑贝网,:,t) = P_00;
end
BAt_mat = BATA;
BPt_mat = PATA;
AT_draw = zeros(T,8);
%% the last iteration %%
% singular value decomposition
[u s v] = svd(BPt_mat(:,:,T));
Pchol = u*sqrt(s);
AT_draw(end,:) = BAt_mat(:桑芽 ,T)'+(Pchol*randn(8,1))';
for i = 1:T-1
BAtt = BAt_mat(:,T-i);
BPtt = BPt_mat(:,:,T-i);
% the covariance of prediction erro
BPhat = F * BPtt * F' + OMIG;
BPhat = 0.5*(BPhat+BPhat');
[up sp vp] = svd(BPhat);
inv_sp = zeros(size(sp,1),size(sp,1));
for rr=1:size(sp,1)
if sp(rr,rr)>1e-12;
inv_sp(rr,rr)=1./sp(rr淮安视听网,rr);
end
end
inv_BPhat = up*inv_sp*vp';
% Bnut = BAt_mat(:,T-i+1)-F*BAtt -mu1*stt(t,1)-mu0*(1-stt(t,1))+F*(mu1*stt(t-1,1)+mu0*(1-stt(t-1大唐小郎中,1)));
Bnut = BAt_mat(:,T-i+1)-F*BAtt -mu_s1*stt(t混元教主 ,1)-mu_s0*(1-stt(t,1));
% updating %
Amean = BAtt + (BPtt*F')*inv_BPhat*Bnut;
Pmean = BPtt - (BPtt*F')*inv_BPhat*(BPtt*F')';
% singular value decomposition
[um sm vm] = svd(Pmean);
Pmchol = um*sqrt(sm);
AT_draw(T-i,:) = (Amean+Pmchol*randn(8,1))';
end
AT_draw=AT_draw';
end
- 文章归档
-
- 2020年11月 (60)
- 2020年10月 (312)
- 2020年9月 (304)
- 2020年8月 (315)
- 2020年7月 (314)
- 2020年6月 (292)
- 2020年5月 (316)
- 2020年4月 (303)
- 2020年3月 (312)
- 2020年2月 (282)
- 2020年1月 (312)
- 2019年12月 (312)
- 2019年11月 (196)
- 2019年10月 (240)
- 2019年9月 (292)
- 2019年8月 (221)
- 2019年7月 (90)
- 2019年6月 (98)
- 2019年5月 (102)
- 2019年4月 (1607)
- 2019年3月 (2200)
- 2019年2月 (359)
- 2019年1月 (382)
- 2018年12月 (213)
- 2018年11月 (299)
- 2018年10月 (300)
- 2018年9月 (302)
- 2018年8月 (302)
- 2018年7月 (312)
- 2018年6月 (301)
- 2018年5月 (313)
- 2018年4月 (305)
- 2018年3月 (315)
- 2018年2月 (287)
- 2018年1月 (303)
- 2017年12月 (312)
- 2017年11月 (297)
- 2017年10月 (306)
- 2017年9月 (311)
- 2017年8月 (309)
- 2017年7月 (164)
- 2017年6月 (86)
- 2017年5月 (105)
- 2017年4月 (105)
- 2017年3月 (101)
- 2017年2月 (93)
- 2017年1月 (88)
- 2016年12月 (92)
- 2016年11月 (92)
- 2016年10月 (94)
- 2016年9月 (93)
- 2016年8月 (86)
- 2016年7月 (92)
- 2016年6月 (87)
- 2016年5月 (91)
- 2016年4月 (101)
- 2016年3月 (102)
- 2016年2月 (92)
- 2016年1月 (93)
- 2015年12月 (95)
- 2015年11月 (90)
- 2015年10月 (97)
- 2015年9月 (100)
- 2015年8月 (91)
- 2015年7月 (95)
- 2015年6月 (86)
- 2015年5月 (98)
- 2015年4月 (96)
- 2015年3月 (94)
- 2015年2月 (85)
- 2015年1月 (94)
- 2014年12月 (87)
- 2014年11月 (79)
- 2014年10月 (104)
- 2014年9月 (90)
- 2014年8月 (83)
- 2014年7月 (99)
- 2014年6月 (87)
- 2014年5月 (94)
- 2014年4月 (74)