Error while fitting a time varying state space model
Info
This question is closed. Reopen it to edit or answer.
Show older comments
I am trying to fit a time varying state space model. Essentially, I have mixed frequency data, which entails to a time varying observation matrix. I think I have coded up correctly (looking up http://www.mathworks.com/help/econ/estimate-a-time-varying-state-space-model.html). However, I am getting the following error:
Length of the observation vector in period 2 must agree with the number of rows in matrix C.
I am attaching the code and data if someone would like to have a look at that.
% Function that develops the model
function [A,B,C,D,Mean0,Cov0,StateType] = ParamFun_monthly_small(p,T)
% A
A1 = zeros(8,8);
A1(1,1) = p(1); A1(1,2) = p(2);
A1(2,1) = 1;
A1(3,3) = 1; A1(3,4) = 1;
A1(4,4) = 1;
A1(5,5) = 1; A1(5,6) = 1;
A1(6,6) = 1;
A1(7,1) = p(3); A1(7,2) = p(4);
A1(8,8) = 1;
%%B (use exponential of variance parameters to ensure non negativity)
B1 = zeros(8,8);
B1(1,1) = exp(p(5));
B1(4,4) = exp(p(6));
B1(6,6) = exp(p(7));
B1(7,7) = exp(p(8));
B1(8,8) = exp(p(9));
%%C
C1 = zeros(3,8);
C2 = zeros(2,8);
C1 = {[1 0 1 0 0 0 0 0;
p(10) p(11) 0 0 1 0 0 0;
0 0 0 0 0 0 1 1]};
C2 = {[p(10) p(11) 0 0 1 0 0 0;
0 0 0 0 0 0 1 1]};
C_augment = [C1; C2; C2];
%%D
D1 = zeros(3,3);
D2 = zeros(2,3);
D_augment = [D1; D2; D2];
Mean0 = [];
Cov0 = [];
StateType = [0 0 2 2 2 2 0 2];
A = A1;
B = B1;
C = repmat(C_augment,T/3,1);
D = 0; %repmat(D_augment,T/3,1);
end
% Main code that fits the model
data = xlsread('U:\SGN_Tirupam_Current\local-natural-rate\new\Data\Interwrk\monthly_data_model.xlsx');
% year quarter gdp cpi ea jc u eu ue un ne nu eep ea_1995q1 ea_1998q1 ea_2000q1 jc_1997q3
%Order of variable for the model = y eu ue un ne nu eep u
names = {'gdp' 'u' 'cpi'};
obs = [data(:,3) data(:,7) data(:,4) ];
T = length(obs);
obs = obs(3:T-1,:); %so that first row has all observations
Mdl = ssm(@(params) ParamFun_monthly_small(params,T));
load phi.mat phi omega beta;
% remove coeffs of pi,u,ea,jc
% phi([2,3,10,11],:) = [];
p0 = zeros(11,1);
p0(1) = phi(1,1);
p0(2) = phi(1,2);
p0(3) = beta(1);
p0(4) = beta(2);
p0(5) = log(0.01);
p0(6) = log(0.01);
p0(7) = log(0.01);
p0(8) = log(0.01);
p0(9) = log(0.01);
p0(10) = omega(1);
p0(11) = omega(2);
%no measurement error as of now - we can introduce it in u OR ea OR both
%depending on how much can be identified
tic
options = optimoptions('fminunc','MaxFunEval',2000);
[m,estParams,~,logL,Output] = ...
estimate(Mdl,obs,p0,'Display','full','Options',options);
toc
state = smooth(m,obs);
end
1 Comment
Walter Roberson
on 17 Jul 2015
Which line is the error being reported on?
Answers (0)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!