26inline unsigned int bin(
const unsigned int n,
const unsigned int k) {
28 throw std::runtime_error(
"binomial coefficient higher than degree");
30 if (k > n / 2)
return bin(n, n - k);
31 return n * bin(n - 1, k - 1) / k;
37template <
typename Numeric =
double>
40 Bern(
const unsigned int m,
const unsigned int i)
41 : m_minus_i(m - i), i_(i), bin_m_i_(bin(m, i)) {}
48 Numeric operator()(
const Numeric u)
const {
49 if (!(u >= 0. && u <= 1.)) {
50 throw std::invalid_argument(
"u needs to be betwen 0 and 1.");
52 return bin_m_i_ * (pow(u, i_)) * pow((1 - u), m_minus_i);
58 virtual bool operator==(
const Bern& other)
const {
67 virtual bool operator!=(
const Bern&
other)
const {
return !(*
this ==
other); }
76 friend class boost::serialization::access;
77 template <
class Archive>
82 ar& boost::serialization::make_nvp(
"m_minus_i",
m_minus_i);
83 ar& boost::serialization::make_nvp(
"i",
i_);
84 ar& boost::serialization::make_nvp(
"bin_m_i",
bin_m_i_);