How to use the DeflatedData option in bnlssm package
이전 댓글 표시
I hope to use the Bayesian Non-linear state space model in the matlab which names bnlssm. I set a simple model to learn it. I want to add predictors variables, that is, use the DeflatedData option, but it keeps failing. I would like to ask how to use this option. Below is my code.
data = readtable('C:\Users\Xiaoxuan\SSE.csv');
head(data);
SSE = data.close;
dts = data.time;
dts = datetime(dts, 'InputFormat','MM/dd/yyyy');
T = numel(SSE);
retsp500 = price2ret(SSE);
y = retsp500 - mean(retsp500);
retsse = price2ret(SSE);
retdts = dts(2:end);
Z = [data.high];
figure
plot(dts,SSE)
title("China SSE Closing Prices")
ylabel("Closing Price")
PriorMdl = bnlssm(@(theta)paramMap(theta,y,Z),@flatLogPrior,ObservationForm="distribution", ...
Multipoint=["A" "LogY"]);
theta0 = [0.2 0 0.5 0.7 0 1 1];
lower = [-1; -Inf; 0; 0; -Inf; -Inf; -Inf];
upper = [1; Inf; Inf; Inf; Inf; Inf; Inf];
burnin = 1e4;
thin = 25;
rng(1)
PosteriorMdl = estimate(PriorMdl,y,theta0,Lower=lower,Upper=upper, ...
NumParticles=500,Hessian="opg",SortParticles=false,BurnIn=burnin,Thin=thin);
function [A,B,LogY,Mean0,Cov0,StateType,DeflatedY] = paramMap(theta,y,Z)
A = @(x) theta(2) + theta(1).*x;
B = theta(3);
LogY = @(y,x) -0.5*log(2*pi) - x - 0.5*log(theta(4)) - 0.5*(y-theta(5))*(y-theta(5)).*exp(-2.*x)/theta(4);
Mean0 = theta(2)./(1 - theta(1));
Cov0 = (theta(3)^2)./(1 - theta(1)^2);
StateType = 0;
DeflatedY = y - Z*[theta(6); theta(7)];
end
function logprior = flatLogPrior(theta)
% flatLogPrior computes the log of the flat prior density for the three
% variables in theta. Log probabilities for parameters outside the
% parameter space are -Inf.
paramcon = zeros(numel(theta),1);
% theta(1) is the lag 1 term in a stationary AR(1) model. The
% value needs to be within the unit circle.
paramcon(1) = abs(theta(1)) >= 1 - eps;
% alpha must be finite
paramcon(2) = ~isfinite(theta(2));
% Standard deviation of the state disturbance theta(3) must be positive.
paramcon(3) = theta(3) <= eps;
% Standard deviation of the state disturbance theta(3) must be positive.
paramcon(4) = theta(4) <= eps;
% alpha must be finite
paramcon(5) = ~isfinite(theta(5));
% alpha must be finite
paramcon(6) = ~isfinite(theta(6));
% alpha must be finite
paramcon(7) = ~isfinite(theta(7));
if sum(paramcon) > 0
logprior = -Inf;
else
logprior = 0; % Prior density is proportional to 1 for all values
% in the parameter space.
end
end
댓글 수: 2
Yuanqing
2024년 9월 27일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Univariate Discrete Distributions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
