Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a vector of "discrete_distribution" c++

Tags:

c++

random

vector

I'm trying to make a simulation of something like a Markov chain and using discrete_distribution to simulate the change of state s_i to s_j. But of course, this is a Matrix, not a vector. So I try.

std::vector <uint16_t> v {{...},
                          {...},
                           ...
                          {...},};

std::vector <std::discrete_distribution <uint16_t> > distr(n, std::distribution <uint16_t> (v.begin(), v.end()) );

but this doesn't work.

note: if I try just 1 vector, this is a vector of uint16_t works

// CHANGE v by v[0]
std::vector<std::discrete_distribution <uint64_t>> distr(1, std::discrete_distribution <uint64_t> (vecs[0].begin(), vecs[0].end()));

based on this answer

I know that

std::vector <std::discrete_distribution <uint16_t> > distr(n, std::distribution <uint16_t> (v.begin(), v.end()) );

is not correct, but I say about the change v1 to v. to demonstrate that is possible use a vector of discrete distributions

like image 493
Moises Rojo Avatar asked Dec 28 '17 20:12

Moises Rojo


People also ask

What is discrete distribution in C++?

std::discrete_distribution produces random integers on the interval [0, n), where the probability of each individual integer i is defined as w i /S, that is the weight of the ith integer divided by the sum of all n weights. std::discrete_distribution satisfies all requirements of RandomNumberDistribution

How to fit a discrete empirical distribution in R?

However, we can fit a discrete empirical distribution to the proportions observed in the sample. Using the table () command, we can summarize the counts. Then, by dividing by 40 (the total number of observations), we can get the proportions as show in the R R listing.

How do you do discrete distribution in unary_op?

Effectively calls discrete_distribution(weights.begin(), weights.end()) . 4) Constructs the distribution with count weights that are generated using function unary_op. Each of the weights is equal to w and i ∈ {0, ..., count−1}. xmin and xmax must be such that δ > 0.

What are the three ways to construct a distribution in Python?

1) Default constructor. Constructs the distribution with a single weight p= {1}. This distribution will always generate ​0​ . 2) Constructs the distribution with weights in the range [first, last). If first == last, the effects are the same as of the default constructor. 3) Constructs the distribution with weights in weights.


2 Answers

You can use list initialization to initialize nested vectors. E.g.:

std::vector<std::vector<int>> v{
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
like image 195
Vittorio Romeo Avatar answered Nov 14 '22 20:11

Vittorio Romeo


I find a way to do it like in this answer

using this template

template<typename T>
void set_distributions(std::vector< std::discrete_distribution <T> > &distr,  const std::vector< std::vector<T> > &vecs){
  for (uint64_t i = 0; i < vecs.size(); ++i) {
    distr.push_back( std::discrete_distribution <uint64_t> (vecs[i].begin(), vecs[i].end()) );
  }

}

and with this function, when you have emptied vectors of distributions

std::vector<std::discrete_distribution <uint64_t>> distr;
  set_distributions(distr, vecs);
like image 40
Moises Rojo Avatar answered Nov 14 '22 21:11

Moises Rojo