Skip to content

Commit

Permalink
add binary distributions for double and integer
Browse files Browse the repository at this point in the history
  • Loading branch information
gonuke committed Oct 28, 2024
1 parent 8249fa7 commit 534ad5e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/random_number_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class RandomNumberGenerator {
friend class NormalDoubleDist;
friend class PoissonDoubleDist;
friend class ExponentialDoubleDist;
friend class BinaryDoubleDist;
friend class FixedIntDist;
friend class UniformIntDist;
friend class NormalIntDist;
friend class BinomialIntDist;
friend class NegativeBinomialIntDist;
friend class PoissonIntDist;
friend class ExponentialIntDist;
friend class BinaryIntDist;

private:
/// Returns a random number for use in a distribution
Expand Down Expand Up @@ -176,6 +178,25 @@ class ExponentialDoubleDist : public DoubleDistribution {
virtual double lambda() { return lambda_; }
};


/// Binary distribution requires twp options and a probability
class BinaryDoubleDist : public DoubleDistribution {
private:
double choice_a_, choice_b_, prob_a_;
public:

BinaryDoubleDist(double choice_a, double choice_b, double prob_a) :
choice_a_(choice_a), choice_b_(choice_b), prob_a_(prob_a) {
if (prob_a_ < 0) {
throw ValueError("Probability of choice A must be positive");
}
};
virtual double sample() {
return RandomNumberGenerator::random_01() <= prob_a_ ? choice_a_ : choice_b_; }
virtual double prob() { return prob_a_; }
};


class IntDistribution {
public:
typedef boost::shared_ptr<IntDistribution> Ptr;
Expand Down Expand Up @@ -306,6 +327,25 @@ class ExponentialIntDist : public IntDistribution {
virtual double lambda() { return lambda_; }
};

/// Binary distribution requires twp options and a probability
class BinaryInteDist : public IntDistribution {
private:
int choice_a_, choice_b_;
double prob_a_;
public:

BinaryDoubleDist(int choice_a, int choice_b, double prob_a) :
choice_a_(choice_a), choice_b_(choice_b), prob_a_(prob_a) {
if (prob_a_ < 0) {
throw ValueError("Probability of choice A must be positive");
}
};
virtual int sample() {
return RandomNumberGenerator::random_01() <= prob_a_ ? choice_a_ : choice_b_; }
virtual double prob() { return prob_a_; }
};


}

#endif // CYCLUS_SRC_RNG_H

0 comments on commit 534ad5e

Please sign in to comment.