Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Mark Song
particles
Commits
f66b6b6e
Commit
f66b6b6e
authored
Dec 15, 2014
by
Stéphane Adjemian
Browse files
Replaced DynareOptions by ParticleOptions and ThreadsOptions.
parent
7ad84f3f
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/conditional_filter_proposal.m
View file @
f66b6b6e
function
[
ProposalStateVector
,
Weights
]
=
conditional_filter_proposal
(
ReducedForm
,
obs
,
StateVectors
,
SampleWeights
,
Q_lower_triangular_cholesky
,
H_lower_triangular_cholesky
,
H
,
Dynare
Options
,
normconst2
)
function
[
ProposalStateVector
,
Weights
]
=
conditional_filter_proposal
(
ReducedForm
,
obs
,
StateVectors
,
SampleWeights
,
Q_lower_triangular_cholesky
,
H_lower_triangular_cholesky
,
H
,
ParticleOptions
,
Threads
Options
,
normconst2
)
%
% Computes the proposal for each past particle using Gaussian approximations
% for the state errors and the Kalman filter
...
...
@@ -73,11 +73,11 @@ if isempty(init_flag2)
init_flag2
=
1
;
end
if
Dynar
eOptions
.
particle
.
proposal_approximation
.
cubature
||
Dynar
eOptions
.
particle
.
proposal_approximation
.
montecarlo
if
Particl
eOptions
.
proposal_approximation
.
cubature
||
Particl
eOptions
.
proposal_approximation
.
montecarlo
[
nodes
,
weights
]
=
spherical_radial_sigma_points
(
number_of_structural_innovations
);
weights_c
=
weights
;
elseif
Dynar
eOptions
.
particle
.
proposal_approximation
.
unscented
[
nodes
,
weights
,
weights_c
]
=
unscented_sigma_points
(
number_of_structural_innovations
,
Dynar
eOptions
);
elseif
Particl
eOptions
.
proposal_approximation
.
unscented
[
nodes
,
weights
,
weights_c
]
=
unscented_sigma_points
(
number_of_structural_innovations
,
Particl
eOptions
);
else
error
(
'Estimation: This approximation for the proposal is not implemented or unknown!'
)
end
...
...
@@ -85,12 +85,12 @@ end
epsilon
=
Q_lower_triangular_cholesky
*
(
nodes
'
)
;
yhat
=
repmat
(
StateVectors
-
state_variables_steady_state
,
1
,
size
(
epsilon
,
2
))
;
tmp
=
local_state_space_iteration_2
(
yhat
,
epsilon
,
ghx
,
ghu
,
constant
,
ghxx
,
ghuu
,
ghxu
,
DynareOptions
.
thread
s
.
local_state_space_iteration_2
);
tmp
=
local_state_space_iteration_2
(
yhat
,
epsilon
,
ghx
,
ghu
,
constant
,
ghxx
,
ghuu
,
ghxu
,
ThreadsOption
s
.
local_state_space_iteration_2
);
PredictedStateMean
=
tmp
(
mf0
,:)
*
weights
;
PredictedObservedMean
=
tmp
(
mf1
,:)
*
weights
;
if
Dynar
eOptions
.
particle
.
proposal_approximation
.
cubature
||
Dynar
eOptions
.
particle
.
proposal_approximation
.
montecarlo
if
Particl
eOptions
.
proposal_approximation
.
cubature
||
Particl
eOptions
.
proposal_approximation
.
montecarlo
PredictedStateMean
=
sum
(
PredictedStateMean
,
2
)
;
PredictedObservedMean
=
sum
(
PredictedObservedMean
,
2
)
;
dState
=
bsxfun
(
@
minus
,
tmp
(
mf0
,:),
PredictedStateMean
)
'.*
sqrt
(
weights
)
;
...
...
@@ -117,7 +117,7 @@ else
end
ProposalStateVector
=
StateVectorVarianceSquareRoot
*
randn
(
size
(
StateVectorVarianceSquareRoot
,
2
),
1
)
+
StateVectorMean
;
ypred
=
measurement_equations
(
ProposalStateVector
,
ReducedForm
,
Dynare
Options
)
;
ypred
=
measurement_equations
(
ProposalStateVector
,
ReducedForm
,
Threads
Options
)
;
foo
=
H_lower_triangular_cholesky
\
(
obs
-
ypred
)
;
likelihood
=
exp
(
-
0.5
*
(
foo
)
'*
foo
)/
normconst2
+
1e-99
;
Weights
=
SampleWeights
.*
likelihood
;
src/conditional_particle_filter.m
View file @
f66b6b6e
function
[
LIK
,
lik
]
=
conditional_particle_filter
(
ReducedForm
,
Y
,
start
,
Dynare
Options
)
function
[
LIK
,
lik
]
=
conditional_particle_filter
(
ReducedForm
,
Y
,
start
,
ParticleOptions
,
Threads
Options
)
%
% Evaluates the likelihood of a non-linear model with a particle filter
% - the proposal is built using the Kalman updating step for each particle.
...
...
@@ -74,7 +74,7 @@ if isempty(init_flag)
number_of_state_variables
=
length
(
mf0
);
number_of_observed_variables
=
length
(
mf1
);
init_flag
=
1
;
number_of_particles
=
Dynar
eOptions
.
particle
.
number_of_particles
;
number_of_particles
=
Particl
eOptions
.
number_of_particles
;
end
% Get covariance matrices
...
...
@@ -108,14 +108,14 @@ SampleWeights = ones(1,number_of_particles)/number_of_particles ;
for
t
=
1
:
sample_size
for
i
=
1
:
number_of_particles
[
StateParticles
(:,
i
),
SampleWeights
(
i
)]
=
...
conditional_filter_proposal
(
ReducedForm
,
Y
(:,
t
),
StateParticles
(:,
i
),
SampleWeights
(
i
),
Q_lower_triangular_cholesky
,
H_lower_triangular_cholesky
,
H
,
Dynare
Options
,
normconst2
)
;
conditional_filter_proposal
(
ReducedForm
,
Y
(:,
t
),
StateParticles
(:,
i
),
SampleWeights
(
i
),
Q_lower_triangular_cholesky
,
H_lower_triangular_cholesky
,
H
,
ParticleOptions
,
Threads
Options
,
normconst2
)
;
end
SumSampleWeights
=
sum
(
SampleWeights
)
;
lik
(
t
)
=
log
(
SumSampleWeights
)
;
SampleWeights
=
SampleWeights
.
/
SumSampleWeights
;
if
(
Dynar
eOptions
.
particle
.
resampling
.
status
.
generic
&&
neff
(
SampleWeights
)
<
Dynar
eOptions
.
particle
.
resampling
.
threshold
*
sample_size
)
||
Dynar
eOptions
.
particle
.
resampling
.
status
.
systematic
if
(
Particl
eOptions
.
resampling
.
status
.
generic
&&
neff
(
SampleWeights
)
<
Particl
eOptions
.
resampling
.
threshold
*
sample_size
)
||
Particl
eOptions
.
resampling
.
status
.
systematic
ks
=
ks
+
1
;
StateParticles
=
resample
(
StateParticles
',SampleWeights'
,
Dynar
eOptions
)
'
;
StateParticles
=
resample
(
StateParticles
',SampleWeights'
,
Particl
eOptions
)
'
;
SampleWeights
=
ones
(
1
,
number_of_particles
)/
number_of_particles
;
end
end
...
...
src/measurement_equations.m
View file @
f66b6b6e
function
measure
=
measurement_equations
(
StateVectors
,
ReducedForm
,
Dynare
Options
)
function
measure
=
measurement_equations
(
StateVectors
,
ReducedForm
,
Threads
Options
)
% Copyright (C) 2013 Dynare Team
%
...
...
@@ -27,7 +27,7 @@ constant = ReducedForm.constant(mf1,:);
state_variables_steady_state
=
ReducedForm
.
state_variables_steady_state
;
number_of_structural_innovations
=
length
(
ReducedForm
.
Q
);
yhat
=
bsxfun
(
@
minus
,
StateVectors
,
state_variables_steady_state
)
;
measure
=
local_state_space_iteration_2
(
yhat
,
zeros
(
number_of_structural_innovations
,
size
(
yhat
,
2
)),
ghx
,
ghu
,
constant
,
ghxx
,
ghuu
,
ghxu
,
DynareOptions
.
thread
s
.
local_state_space_iteration_2
);
measure
=
local_state_space_iteration_2
(
yhat
,
zeros
(
number_of_structural_innovations
,
size
(
yhat
,
2
)),
ghx
,
ghu
,
constant
,
ghxx
,
ghuu
,
ghxu
,
ThreadsOption
s
.
local_state_space_iteration_2
);
src/unscented_sigma_points.m
View file @
f66b6b6e
function
[
nodes
,
W_m
,
W_c
]
=
unscented_sigma_points
(
n
,
Dynar
eOptions
)
function
[
nodes
,
W_m
,
W_c
]
=
unscented_sigma_points
(
n
,
Particl
eOptions
)
%
% Computes nodes and weigths for a scaled unscented transform cubature
% INPUTS
...
...
@@ -31,10 +31,10 @@ function [nodes,W_m,W_c] = unscented_sigma_points(n,DynareOptions)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
lambda
=
(
Dynar
eOptions
.
particle
.
unscented
.
alpha
^
2
)
*
(
n
+
Dynar
eOptions
.
particle
.
unscented
.
kappa
)
-
n
;
lambda
=
(
Particl
eOptions
.
unscented
.
alpha
^
2
)
*
(
n
+
Particl
eOptions
.
unscented
.
kappa
)
-
n
;
nodes
=
[
zeros
(
n
,
1
)
(
sqrt
(
n
+
lambda
)
.*
([
eye
(
n
)
-
eye
(
n
)])
)
]
'
;
W_m
=
lambda
/(
n
+
lambda
)
;
W_c
=
W_m
+
(
1
-
Dynar
eOptions
.
particle
.
unscented
.
alpha
^
2
+
Dynar
eOptions
.
particle
.
unscented
.
beta
)
;
W_c
=
W_m
+
(
1
-
Particl
eOptions
.
unscented
.
alpha
^
2
+
Particl
eOptions
.
unscented
.
beta
)
;
temp
=
ones
(
2
*
n
,
1
)/(
2
*
(
n
+
lambda
))
;
W_m
=
[
W_m
;
temp
]
;
W_c
=
[
W_c
;
temp
]
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment