32 #ifndef VSMC_RNG_DISCRETE_DISTRIBUTION_HPP 33 #define VSMC_RNG_DISCRETE_DISTRIBUTION_HPP 38 #define VSMC_RUNTIME_ASSERT_RNG_DISCRETE_DISTRIBUTION_POSITIVE(flag) \ 39 VSMC_RUNTIME_ASSERT( \ 40 (flag), "**DiscreteDistribution** WEIGHTS ARE NOT NON-NEGATIVE") 47 template <
typename IntType>
62 template <
typename InputIter>
63 param_type(InputIter first, InputIter last) : probability_(first, last)
69 : probability_(weights.begin(), weights.end())
74 template <
typename UnaryOperation>
76 UnaryOperation unary_op)
78 probability_.reserve(count);
79 double delta = (xmax - xmin) / static_cast<double>(count);
81 for (std::size_t i = 0; i != count; ++i)
82 probability_.push_back(
83 unary_op(xmin + static_cast<double>(i) * delta));
92 return param1.probability_ == param2.probability_;
98 return !(param1 == param2);
101 template <
typename CharT,
typename Traits>
103 std::basic_ostream<CharT, Traits> &os,
const param_type ¶m)
108 os << param.probability_;
113 template <
typename CharT,
typename Traits>
115 std::basic_istream<CharT, Traits> &is,
param_type ¶m)
125 if (is_positive(probability, sum)) {
126 mul(probability.size(), 1 / sum, probability.data(),
128 param.probability_ = std::move(probability);
130 is.setstate(std::ios_base::failbit);
144 if (probability_.size() == 0)
149 bool flag = is_positive(probability_, sum);
152 is_positive(probability_, sum);
154 mul(probability_.size(), 1 / sum, probability_.data(),
155 probability_.data());
164 for (std::size_t i = 0; i != probability.size(); ++i) {
165 sum += probability[i];
166 if (probability[i] < 0)
170 return flag && sum > 0;
176 template <
typename InputIter>
186 template <
typename UnaryOperation>
188 std::size_t count,
double xmin,
double xmax, UnaryOperation &&unary_op)
189 :
param_type(count, xmin, xmax,
std::forward<UnaryOperation>(unary_op))
196 : param_(
std::move(param))
204 return param_.size() == 0 ? 0 : param_.size() - 1;
209 template <
typename RNGType>
213 rng, param_.probability_.begin(), param_.probability_.end(),
true);
234 template <
typename RNGType,
typename InputIter>
236 bool normalized =
false)
const 239 typename std::iterator_traits<InputIter>::value_type;
242 value_type u =
u01(rng);
246 1 / std::accumulate(first, last, static_cast<value_type>(0));
249 while (first != last) {
250 accw += *first * mulw;
262 while (first != last) {
276 return dist1.param_ == dist2.param_;
282 return !(dist1 == dist2);
285 template <
typename CharT,
typename Traits>
294 template <
typename CharT,
typename Traits>
298 is >> std::ws >> dist.param_;
311 #endif // VSMC_RNG_DISCRETE_DISTRIBUTION_HPP std::vector< T, Alloc > Vector
std::vector with Allocator as default allocator
void mul(std::size_t n, const float *a, const float *b, float *y)
param_type(InputIter first, InputIter last)
friend bool operator==(const distribution_type &dist1, const distribution_type &dist2)
DiscreteDistribution(InputIter first, InputIter last)
param_type(std::size_t count, double xmin, double xmax, UnaryOperation unary_op)
result_type operator()(RNGType &rng) const
RealType u01(UIntType u) noexcept
Convert uniform unsigned integers to floating points within [0, 1].
param_type(std::initializer_list< double > weights)
DiscreteDistribution(param_type &¶m)
friend bool operator!=(const distribution_type &dist1, const distribution_type &dist2)
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
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, distribution_type &dist)
Draw a single sample given weights.
friend bool operator!=(const param_type ¶m1, const param_type ¶m2)
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const distribution_type &dist)
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, param_type ¶m)