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

fix bugs on sorting variables.

test different cases before doing resampling
parent f0fcfa3b
......@@ -55,42 +55,67 @@ function new_particles = multivariate_smooth_resampling(weights,particles,number
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) frederic DOT karame AT univ DASH evry DOT fr
% AUTHOR(S) frederic DOT karame AT univ DASH lemans DOT fr
% stephane DOT adjemian AT univ DASH lemans DOT fr
number_of_particles = length(weights);
number_of_states = size(particles,2);
number = number_of_particles/number_of_partitions ;
tout = sort([particles weights],1) ;
tout = sortrows([particles weights],1) ;
particles = tout(:,1:number_of_states) ;
weights = tout(:,1+number_of_states) ;
cum_weights = cumsum(weights) ;
new_particles = zeros(number_of_particles,number_of_states) ;
indx = 1:number_of_particles ;
for i=1:number_of_partitions
if i==number_of_partitions
tmp = bsxfun(@ge,cum_weights,(i-1)/number_of_partitions) ;
kp = indx( tmp ) ;
else
tmp = bsxfun(@and,bsxfun(@ge,cum_weights,(i-1)/number_of_partitions),bsxfun(@lt,cum_weights,i/number_of_partitions)) ;
kp = indx( tmp ) ;
end
if numel(kp)>2
Np = length(kp) ;
wtilde = [ ( number_of_partitions*( cum_weights(kp(1)) - (i-1)/number_of_partitions) ) ;
( number_of_partitions*weights(kp(2:Np-1)) ) ;
( number_of_partitions*(i/number_of_partitions - cum_weights(kp(Np)-1) ) ) ] ;
if number_of_partitions>1
cum_weights = cumsum(weights) ;
indx = 1:number_of_particles ;
for i=1:number_of_partitions
if i==number_of_partitions
tmp = bsxfun(@gt,cum_weights,(i-1)/number_of_partitions) ;
kp = indx( tmp ) ;
else
tmp = bsxfun(@and,bsxfun(@gt,cum_weights,(i-1)/number_of_partitions),bsxfun(@lt,cum_weights,i/number_of_partitions)) ;
kp = indx( tmp ) ;
end
if numel(kp)>2
Np = length(kp) ;
wtilde = [ ( number_of_partitions*( cum_weights(kp(1)) - (i-1)/number_of_partitions) ) ;
( number_of_partitions*weights(kp(2:Np-1)) ) ;
( number_of_partitions*(i/number_of_partitions - cum_weights(kp(Np)-1) ) ) ] ;
elseif numel(kp)==2
Np = length(kp) ;
wtilde = [ ( number_of_partitions*( cum_weights(kp(1)) - (i-1)/number_of_partitions) ) ;
( number_of_partitions*(i/number_of_partitions - cum_weights(kp(Np)-1) ) ) ] ;
elseif numel(kp)==1
new_particles = ones(number,1).*particles(kp,:) ;
return ;
else
% probleme
end
test = sum(wtilde) ;
disp(test)
new_particles = zeros(number_of_particles,number_of_states) ;
new_particles_j = zeros(number,number_of_states) ;
for j=1:number_of_states
particles_j = particles(kp,j) ;
if j>1
tout = sort( [ particles_j wtilde],1) ;
tout = sortrows( [ particles_j wtilde],1) ;
particles_j = tout(:,1) ;
wtilde = tout(:,2) ;
end
new_particles_j(:,j) = univariate_smooth_resmp(wtilde,particles_j,number) ;
new_particles_j(:,j) = univariate_smooth_resampling(wtilde,particles_j,number) ;
end
new_particles((i-1)*number+1:i*number,:) = new_particles_j;
end
end
else
new_particles = zeros(number,number_of_states) ;
for j=1:number_of_states
if j>1
tout = sortrows( [ particles(:,j) weights],1) ;
particles_j = tout(:,1) ;
weights_j = tout(:,2) ;
else
particles_j = particles(:,j) ;
weights_j = weights ;
end
new_particles(:,j) = univariate_smooth_resampling(weights_j,particles_j,number) ;
end
end
\ No newline at end of file
Supports Markdown
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