mercredi 19 octobre 2016

is there any function to do sampling proportional to values in a vector?

I would be surprised if such function did not exist. Assume a vector of 3 values, that I would want to sample from:

vec = 2,3,5

The straightforward way of doing it is to sum all the values in the vector,

normalizer = 2+3+5 = 10

and normalize each individual value using the sum,

probability_vec = 0.2, 0.3, 0.5 

and then covert it to cumulative vector,

cumulative_vec = 0.2, 0.5, 1

and then drawing a random number between [0,1] to sample one of the three values. My question is, is there any function in GSL or boost that does this?




Aucun commentaire:

Enregistrer un commentaire