jeudi 26 octobre 2017

randomly re-sample vector of tuples with weights in C++

I was wondering if somebody could help.

I'm looking for a fairly straightforward and ideally fast way of sampling from a vector of tuples with weights.

e.g. say I have a vector of tuples, each containing a value and a corresponding weight/probability:

vector<tuple<int, double>> foo = { {1,0.04},{2,0.8},{ 3,0.01 },{ 4,0.03 },{ 
5,0.1 },{ 6,0.9 } };

I want to go through foo and randomly re-sample based on the weights, so I would end up with a vector (either a new vector or replacing the elements in foo) that in this case, is problematically mostly going to be 2 and 6. e.g.

 vector<tuple<int, double>> bar = { {2,0.8},{2,0.8},{ 6,0.9},{ 6,0.9 },{ 
6,0.9 },{ 6,0.9 } };

I'm sure this is pretty straight forward using something like std::discrete_distribution, although I've yet to figure out exactly how.




Aucun commentaire:

Enregistrer un commentaire