Suppose we have a set x
of N
values {x_i; i=1,...,N}
and a set of some associated probabilities {w_i; i=1,...,N}
.
We want to get from the set x
, a new set x^
of N
values {x^_i; i=1,...,N}
by choosing each value x_i
from the setx
according to the probability w_i
. How do we code that (i.e. a pseudo code algorithm, that can be translated to any language).
EDIT: python code:
def resample(self,x,w):
N = len(w)
new_x = empty(N)
c = cumsum(w)
for i in range(N):
r = random()
for j in range(N):
if( j == N-1 ):
new_x[i] = x[j]
break
else:
if( (c[j] <= r) and (r < c[j+1]) ):
new_x[i] = x[j+1]
break
new_w = ones(N,dtype=float)/N
return new_x, new_w