Commit 6e7090f1 authored by Frédéric Karamé's avatar Frédéric Karamé
Browse files

factorize some codes across options.

parent 2995562e
...@@ -3,7 +3,6 @@ function [LIK,lik] = gaussian_mixture_filter(ReducedForm,Y,start,ParticleOptions ...@@ -3,7 +3,6 @@ function [LIK,lik] = gaussian_mixture_filter(ReducedForm,Y,start,ParticleOptions
% variables distributions with gaussian mixtures. Gaussian Mixture allows reproducing % variables distributions with gaussian mixtures. Gaussian Mixture allows reproducing
% a wide variety of generalized distributions (when multimodal for instance). % a wide variety of generalized distributions (when multimodal for instance).
% Each gaussian distribution is obtained whether % Each gaussian distribution is obtained whether
% - with a Smolyak quadrature à la Kronrod & Paterson (Heiss & Winschel 2010, Winschel & Kratzig 2010).
% - with a radial-spherical cubature % - with a radial-spherical cubature
% - with scaled unscented sigma-points % - with scaled unscented sigma-points
% A Sparse grid Kalman Filter is implemented on each component of the mixture, % A Sparse grid Kalman Filter is implemented on each component of the mixture,
...@@ -72,8 +71,8 @@ if isempty(init_flag) ...@@ -72,8 +71,8 @@ if isempty(init_flag)
number_of_observed_variables = length(mf1); number_of_observed_variables = length(mf1);
number_of_structural_innovations = length(ReducedForm.Q); number_of_structural_innovations = length(ReducedForm.Q);
G = ParticleOptions.mixture_state_variables; % number of GM components in state G = ParticleOptions.mixture_state_variables; % number of GM components in state
I = ParticleOptions.mixture_structural_shocks ; % number of GM components in structural noise I = 1 ; %ParticleOptions.mixture_structural_shocks ; % number of GM components in structural noise
J = ParticleOptions.mixture_measurement_shocks ; % number of GM components in observation noise J = 1 ; %ParticleOptions.mixture_measurement_shocks ; % number of GM components in observation noise
Gprime = G*I ; Gprime = G*I ;
Gsecond = G*I*J ; Gsecond = G*I*J ;
number_of_particles = ParticleOptions.number_of_particles; number_of_particles = ParticleOptions.number_of_particles;
...@@ -116,8 +115,9 @@ Q_lower_triangular_cholesky = reduced_rank_cholesky(Q)'; ...@@ -116,8 +115,9 @@ Q_lower_triangular_cholesky = reduced_rank_cholesky(Q)';
StateWeights = ones(1,G)/G ; StateWeights = ones(1,G)/G ;
StateMu = ReducedForm.StateVectorMean*ones(1,G) ; StateMu = ReducedForm.StateVectorMean*ones(1,G) ;
StateSqrtP = zeros(number_of_state_variables,number_of_state_variables,G) ; StateSqrtP = zeros(number_of_state_variables,number_of_state_variables,G) ;
temp = reduced_rank_cholesky(ReducedForm.StateVectorVariance)' ;
for g=1:G for g=1:G
StateSqrtP(:,:,g) = reduced_rank_cholesky(ReducedForm.StateVectorVariance)' ; StateSqrtP(:,:,g) = temp ;
end end
StructuralShocksWeights = ones(1,I)/I ; StructuralShocksWeights = ones(1,I)/I ;
...@@ -197,25 +197,26 @@ for t=1:sample_size ...@@ -197,25 +197,26 @@ for t=1:sample_size
StateParticles,H,const_lik,1/number_of_particles,... StateParticles,H,const_lik,1/number_of_particles,...
1/number_of_particles,ReducedForm,ThreadsOptions) ; 1/number_of_particles,ReducedForm,ThreadsOptions) ;
% calculate importance weights of particles % calculate importance weights of particles
SampleWeights = SampleWeights.*IncrementalWeights ; % SampleWeights = SampleWeights.*IncrementalWeights ;
SampleWeights = IncrementalWeights/number_of_particles ;
SumSampleWeights = sum(SampleWeights,1) ; SumSampleWeights = sum(SampleWeights,1) ;
SampleWeights = SampleWeights./SumSampleWeights ; SampleWeights = SampleWeights./SumSampleWeights ;
lik(t) = log(SumSampleWeights) ; lik(t) = log(SumSampleWeights) ;
% First possible state point estimates % First possible state point estimates
%estimate(t,:,1) = SampleWeights*StateParticles' ; %estimate(t,:,1) = SampleWeights*StateParticles' ;
% Resampling if needed of required % Resampling if needed of required
Neff = 1/sum(bsxfun(@power,SampleWeights,2)) ; % Neff = neff(SampleWeights) ;
if (ParticleOptions.resampling.status.generic && Neff<.5*sample_size) || ParticleOptions.resampling.status.systematic % if (ParticleOptions.resampling.status.generic && Neff<.5*sample_size) || ParticleOptions.resampling.status.systematic
ks = ks + 1 ; % ks = ks + 1 ;
StateParticles = resample(StateParticles',SampleWeights,ParticleOptions)' ; % StateParticles = resample(StateParticles',SampleWeights,ParticleOptions)' ;
StateVectorMean = mean(StateParticles,2) ; % StateVectorMean = mean(StateParticles,2) ;
StateVectorVarianceSquareRoot = reduced_rank_cholesky( (StateParticles*StateParticles')/number_of_particles - StateVectorMean*(StateVectorMean') )'; % StateVectorVarianceSquareRoot = reduced_rank_cholesky( (StateParticles*StateParticles')/number_of_particles - StateVectorMean*(StateVectorMean') )';
SampleWeights = 1/number_of_particles ; % SampleWeights = 1/number_of_particles ;
elseif ParticleOptions.resampling.status.none % elseif ParticleOptions.resampling.status.none
StateVectorMean = StateParticles*sampleWeights ; % StateVectorMean = StateParticles*sampleWeights ;
temp = sqrt(SampleWeights').*StateParticles ; % temp = sqrt(SampleWeights').*StateParticles ;
StateVectorVarianceSquareRoot = reduced_rank_cholesky( temp*temp' - StateVectorMean*(StateVectorMean') )'; % StateVectorVarianceSquareRoot = reduced_rank_cholesky( temp*temp' - StateVectorMean*(StateVectorMean') )';
end % end
% Use the information from particles to update the gaussian mixture on state variables % Use the information from particles to update the gaussian mixture on state variables
[StateMu,StateSqrtP,StateWeights] = fit_gaussian_mixture(StateParticles,StateMu,StateSqrtP,StateWeights,0.001,10,1) ; [StateMu,StateSqrtP,StateWeights] = fit_gaussian_mixture(StateParticles,StateMu,StateSqrtP,StateWeights,0.001,10,1) ;
%estimate(t,:,3) = StateWeights*StateMu' ; %estimate(t,:,3) = StateWeights*StateMu' ;
......
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