importance_sampling.m 1.1 KB
Newer Older
1
2
function State_Particles = importance_sampling(StateMuPost,StateSqrtPPost,StateWeightsPost,numP)

Stéphane Adjemian's avatar
Stéphane Adjemian committed
3
% Copyright (C) 2013-2017 Dynare Team
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

Stéphane Adjemian's avatar
Stéphane Adjemian committed
20
[Xdim,Gsecond] = size(StateMuPost) ;
21
u = rand(numP,1);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
22
[Nc,comp] = histc(u, cumsum([0; StateWeightsPost]));
23
24
State_Particles = zeros(Xdim,numP);
for k=1:Gsecond
Stéphane Adjemian's avatar
Stéphane Adjemian committed
25
26
    idx = bsxfun(@eq,comp,k*ones(size(comp))) ;
    State_Particles(:,idx) = StateSqrtPPost(:,:,k)*randn(Xdim,Nc(k));
27
end
Stéphane Adjemian's avatar
Stéphane Adjemian committed
28
State_Particles= State_Particles + StateMuPost(:,comp);