-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathResample.m
More file actions
26 lines (21 loc) · 993 Bytes
/
Resample.m
File metadata and controls
26 lines (21 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
%% Resampling function
function [x_new, q_new, idx] = Resample(x, q, resampling_strategy)
Ns = length(q); % Ns = number of particles
switch resampling_strategy
case 'multinomial_resampling'
with_replacement = true;
idx = randsample(1:Ns, Ns, with_replacement, q);
case 'systematic_resampling'
% this is performing latin hypercube sampling on q
edges = min([0 cumsum(q)'],1); % protect against accumulated round-off
edges(end) = 1; % get the upper edge exact
u1 = rand/Ns;
% this works like the inverse of the empirical distribution and returns
% the interval where the sample is to be found
[~, idx] = histc(u1:1/Ns:1, edges);
otherwise
error('Resampling strategy not implemented')
end;
x_new = x(:,idx); % extract new particles
q_new = repmat(1/Ns, 1, Ns); % now all particles have the same weight
end