Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample from discrete distribution using weights in Eigen Vector

I have a set of weights in an Eigen::VectorXd and would like to draw samples from the range of indices using these values are probabilities. If weights is a std::vector I can do this:

std::random_device rd;
std::mt19937 rng(rd());
std::discrete_distribution<int> dist(weights.begin(), weights.end());
int val = dist(rng);

What is the best way to do this when weights is an Eigen::VectorXd? Can it be done without copying the vector and without writing the sampler myself?

like image 347
Flash Avatar asked Jun 23 '26 12:06

Flash


1 Answers

You could use pointers for Eigen::VectorXd

std::discrete_distribution<int> dist(weights.data(), weights.data()+weights.size());
like image 165
kangshiyin Avatar answered Jun 25 '26 01:06

kangshiyin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!