Commit 04ad104b authored by Frédéric Karamé's avatar Frédéric Karamé

Fix a bug in likelihood calculation.

parent cdc7f6dd
......@@ -64,7 +64,7 @@ end
% Get initial condition for the state vector.
StateVectorMean = ReducedForm.StateVectorMean;
StateVectorVarianceSquareRoot = reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
StateVectorVarianceSquareRoot = chol(ReducedForm.StateVectorVariance)';%reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
state_variance_rank = size(StateVectorVarianceSquareRoot,2);
Q_lower_triangular_cholesky = chol(Q)';
if pruning
......@@ -76,7 +76,7 @@ end
set_dynare_seed('default');
% Initialization of the likelihood.
const_lik = log(2*pi)*number_of_observed_variables;
const_lik = log(2*pi)*number_of_observed_variables +log(det(H));
lik = NaN(sample_size,1);
LIK = NaN;
......@@ -125,11 +125,11 @@ for t=1:sample_size
tmp = tmp + nodes_weights(i)*local_state_space_iteration_2(yhat,nodes(i,:)*ones(1,number_of_particles),ghx,ghu,constant,ghxx,ghuu,ghxu,ThreadsOptions.local_state_space_iteration_2);
end
end
PredictedObservedMean = weights*(tmp(mf1,:)');
%PredictedObservedMean = weights*(tmp(mf1,:)');
PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean');
PredictedObservedVariance = bsxfun(@times,weights,dPredictedObservedMean)*dPredictedObservedMean' +H;
wtilde = exp(-.5*(const_lik+log(det(PredictedObservedVariance))+sum(PredictionError.*(PredictedObservedVariance\PredictionError),1))) ;
%dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean');
%PredictedObservedVariance = bsxfun(@times,weights,dPredictedObservedMean)*dPredictedObservedMean' +H;
wtilde = exp(-.5*(const_lik+sum(PredictionError.*(H\PredictionError),1))) ;
tau_tilde = weights.*wtilde ;
sum_tau_tilde = sum(tau_tilde) ;
lik(t) = log(sum_tau_tilde) ;
......@@ -148,11 +148,11 @@ for t=1:sample_size
tmp = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,ThreadsOptions.local_state_space_iteration_2);
end
StateVectors = tmp(mf0,:);
PredictedObservedMean = mean(tmp(mf1,:),2);
%PredictedObservedMean = mean(tmp(mf1,:),2);
PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean);
PredictedObservedVariance = (dPredictedObservedMean*dPredictedObservedMean')/number_of_particles + H;
lnw = exp(-.5*(const_lik+log(det(PredictedObservedVariance))+sum(PredictionError.*(PredictedObservedVariance\PredictionError),1)));
%dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean);
%PredictedObservedVariance = (dPredictedObservedMean*dPredictedObservedMean')/number_of_particles + H;
lnw = exp(-.5*(const_lik+sum(PredictionError.*(H\PredictionError),1)));
wtilde = lnw.*factor ;
weights = wtilde/sum(wtilde);
end
......
......@@ -113,7 +113,7 @@ else
StateVectorMean = PredictedStateMean + KalmanFilterGain*(obs - PredictedObservedMean);
StateVectorVariance = PredictedStateVariance - KalmanFilterGain*PredictedObservedVariance*KalmanFilterGain';
StateVectorVariance = .5*(StateVectorVariance+StateVectorVariance');
StateVectorVarianceSquareRoot = reduced_rank_cholesky(StateVectorVariance)';
StateVectorVarianceSquareRoot = chol(StateVectorVariance)';%reduced_rank_cholesky(StateVectorVariance)';
end
ProposalStateVector = StateVectorVarianceSquareRoot*randn(size(StateVectorVarianceSquareRoot,2),1)+StateVectorMean ;
......
......@@ -57,9 +57,9 @@ function [LIK,lik] = conditional_particle_filter(ReducedForm,Y,start,ParticleOpt
% AUTHOR(S) frederic DOT karame AT univ DASH lemans DOT fr
% stephane DOT adjemian AT univ DASH lemans DOT fr
persistent init_flag mf0 mf1
persistent init_flag mf1
persistent number_of_particles
persistent sample_size number_of_state_variables number_of_observed_variables
persistent sample_size number_of_observed_variables
% Set default
if isempty(start)
......@@ -68,10 +68,10 @@ end
% Set persistent variables.
if isempty(init_flag)
mf0 = ReducedForm.mf0;
%mf0 = ReducedForm.mf0;
mf1 = ReducedForm.mf1;
sample_size = size(Y,2);
number_of_state_variables = length(mf0);
%number_of_state_variables = length(mf0);
number_of_observed_variables = length(mf1);
init_flag = 1;
number_of_particles = ParticleOptions.number_of_particles ;
......@@ -84,14 +84,14 @@ if isempty(H)
H = 0;
H_lower_triangular_cholesky = 0;
else
H_lower_triangular_cholesky = reduced_rank_cholesky(H)';
H_lower_triangular_cholesky = chol(H)'; %reduced_rank_cholesky(H)';
end
% Get initial condition for the state vector.
StateVectorMean = ReducedForm.StateVectorMean;
StateVectorVarianceSquareRoot = reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
state_variance_rank = size(StateVectorVarianceSquareRoot,2);
Q_lower_triangular_cholesky = reduced_rank_cholesky(Q)';
Q_lower_triangular_cholesky = chol(Q)'; %reduced_rank_cholesky(Q)';
% Set seed for randn().
set_dynare_seed('default');
......
......@@ -43,9 +43,10 @@ prior = probability2(st_t_1,sqr_Pss_t_t_1,particles) ;
% likelihood
yt_t_1_i = measurement_equations(particles,ReducedForm,ThreadsOptions) ;
eta_t_i = bsxfun(@minus,obs,yt_t_1_i)' ;
yt_t_1 = sum(yt_t_1_i*weigths1,2) ;
tmp = bsxfun(@minus,yt_t_1_i,yt_t_1) ;
Pyy = bsxfun(@times,weigths2',tmp)*tmp' + H ;
%yt_t_1 = sum(yt_t_1_i*weigths1,2) ;
%tmp = bsxfun(@minus,yt_t_1_i,yt_t_1) ;
%Pyy = bsxfun(@times,weigths2',tmp)*tmp' + H ;
Pyy = H ;
sqr_det = sqrt(det(Pyy)) ;
foo = (eta_t_i/Pyy).*eta_t_i ;
likelihood = exp(-0.5*sum(foo,2))/(normconst*sqr_det) + 1e-99 ;
......
......@@ -118,5 +118,5 @@ else
StateVectorMean = PredictedStateMean + KalmanFilterGain*PredictionError;
StateVectorVariance = PredictedStateVariance - KalmanFilterGain*PredictedObservedVariance*KalmanFilterGain';
StateVectorVariance = .5*(StateVectorVariance+StateVectorVariance');
StateVectorVarianceSquareRoot = reduced_rank_cholesky(StateVectorVariance)';
StateVectorVarianceSquareRoot = chol(StateVectorVariance)'; %reduced_rank_cholesky(StateVectorVariance)';
end
\ No newline at end of file
......@@ -66,12 +66,12 @@ if isempty(H)
end
% Initialization of the likelihood.
const_lik = log(2*pi)*number_of_observed_variables;
const_lik = log(2*pi)*number_of_observed_variables +log(det(H)) ;
lik = NaN(sample_size,1);
% Get initial condition for the state vector.
StateVectorMean = ReducedForm.StateVectorMean;
StateVectorVarianceSquareRoot = reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
StateVectorVarianceSquareRoot = chol(ReducedForm.StateVectorVariance)';%reduced_rank_cholesky(ReducedForm.StateVectorVariance)';
if pruning
StateVectorMean_ = StateVectorMean;
StateVectorVarianceSquareRoot_ = StateVectorVarianceSquareRoot;
......@@ -103,12 +103,13 @@ for t=1:sample_size
else
tmp = local_state_space_iteration_2(yhat,epsilon,ghx,ghu,constant,ghxx,ghuu,ghxu,ThreadsOptions.local_state_space_iteration_2);
end
PredictedObservedMean = tmp(mf1,:)*transpose(weights);
%PredictedObservedMean = tmp(mf1,:)*transpose(weights);
PredictionError = bsxfun(@minus,Y(:,t),tmp(mf1,:));
dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean);
PredictedObservedVariance = bsxfun(@times,dPredictedObservedMean,weights)*dPredictedObservedMean' + H;
if rcond(PredictedObservedVariance) > 1e-16
lnw = -.5*(const_lik+log(det(PredictedObservedVariance))+sum(PredictionError.*(PredictedObservedVariance\PredictionError),1));
%dPredictedObservedMean = bsxfun(@minus,tmp(mf1,:),PredictedObservedMean);
%PredictedObservedVariance = bsxfun(@times,dPredictedObservedMean,weights)*dPredictedObservedMean' + H;
%PredictedObservedVariance = H;
if rcond(H) > 1e-16
lnw = -.5*(const_lik+sum(PredictionError.*(H\PredictionError),1));
else
LIK = NaN;
return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment