32 #ifndef VSMC_RNG_DISCRETE_DISTRIBUTION_HPP 33 #define VSMC_RNG_DISCRETE_DISTRIBUTION_HPP 39 #define VSMC_RUNTIME_ASSERT_RNG_DISCRETE_DISTRIBUTION_POSITIVE(flag) \ 40 VSMC_RUNTIME_ASSERT( \ 41 (flag), "**DiscreteDistribution** WEIGHTS ARE NOT NON-NEGATIVE") 48 template <
typename IntType>
63 template <
typename InputIter>
65 : probability_(first, last)
71 : probability_(weights.begin(), weights.end())
76 template <
typename UnaryOperation>
78 UnaryOperation unary_op)
80 probability_.reserve(count);
81 double delta = (xmax - xmin) / static_cast<double>(count);
83 for (std::size_t i = 0; i != count; ++i)
84 probability_.push_back(
85 unary_op(xmin + static_cast<double>(i) * delta));
94 if (param1.probability_.size() != param2.probability_.size())
97 for (std::size_t i = 0; i != param1.probability_.size(); ++i)
99 param1.probability_[i], param2.probability_[i]))
108 return !(param1 == param2);
111 template <
typename CharT,
typename Traits>
113 std::basic_ostream<CharT, Traits> &os,
const param_type ¶m)
118 os << param.probability_.size() <<
' ';
119 for (std::size_t i = 0; i != param.probability_.size(); ++i)
120 os << param.probability_[i] <<
' ';
125 template <
typename CharT,
typename Traits>
127 std::basic_istream<CharT, Traits> &is,
param_type ¶m)
138 for (std::size_t i = 0; i != n; ++i)
139 is >> std::ws >> probability[i];
143 if (is_positive(probability, sum)) {
144 mul(probability.size(), 1 / sum, probability.data(),
146 param.probability_ = std::move(probability);
148 is.setstate(std::ios_base::failbit);
162 if (probability_.size() == 0)
167 bool flag = is_positive(probability_, sum);
170 is_positive(probability_, sum);
172 mul(probability_.size(), 1 / sum, probability_.data(),
173 probability_.data());
182 for (std::size_t i = 0; i != probability.size(); ++i) {
183 sum += probability[i];
184 if (probability[i] < 0)
188 return flag && sum > 0;
194 template <
typename InputIter>
196 : param_(first, last)
205 template <
typename UnaryOperation>
207 std::size_t count,
double xmin,
double xmax, UnaryOperation &&unary_op)
208 :
param_type(count, xmin, xmax,
std::forward<UnaryOperation>(unary_op))
215 : param_(
std::move(param))
223 return param_.size() == 0 ? 0 : param_.size() - 1;
228 template <
typename RNGType>
232 rng, param_.probability_.begin(), param_.probability_.end(),
true);
253 template <
typename RNGType,
typename InputIter>
255 bool normalized =
false)
const 258 typename std::iterator_traits<InputIter>::value_type;
261 value_type u = runif(rng);
265 1 / std::accumulate(first, last, static_cast<value_type>(0));
268 while (first != last) {
269 accw += *first * mulw;
281 while (first != last) {
300 #endif // VSMC_RNG_DISCRETE_DISTRIBUTION_HPP
Standard uniform distribution with open/closed variants.
void mul(std::size_t n, const float *a, const float *b, float *y)
param_type(InputIter first, InputIter last)
DiscreteDistribution(InputIter first, InputIter last)
typename std::conditional< std::is_scalar< T >::value, std::vector< T, AlignedAllocator< T >>, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
bool is_equal(const T &a, const T &b)
param_type(std::size_t count, double xmin, double xmax, UnaryOperation unary_op)
#define VSMC_DEFINE_RNG_DISTRIBUTION_OPERATORS
result_type operator()(RNGType &rng) const
param_type(std::initializer_list< double > weights)
DiscreteDistribution(param_type &¶m)
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const param_type ¶m)
DiscreteDistribution(std::initializer_list< double > weights)
result_type operator()(RNGType &rng, InputIter first, InputIter last, bool normalized=false) const
Draw sample with external probabilities.
DiscreteDistribution< IntType > distribution_type
Vector< double > probability() const
DiscreteDistribution(const param_type ¶m)
#define VSMC_RUNTIME_ASSERT_RNG_DISCRETE_DISTRIBUTION_POSITIVE(flag)
DiscreteDistribution(std::size_t count, double xmin, double xmax, UnaryOperation &&unary_op)
friend bool operator==(const param_type ¶m1, const param_type ¶m2)
Vector< double > probability() const
Draw a single sample given weights.
friend bool operator!=(const param_type ¶m1, const param_type ¶m2)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, param_type ¶m)