/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include
#include "Probability.h"
namespace Probability
{
float binomial_gte1(int n, float p)
{
return 1.0f - std::pow(1.0f-p, n);
}
float randFloat()
{
return static_cast(rand())/RAND_MAX;
}
SmallKBinomialGenerator::SmallKBinomialGenerator(unsigned int n, float p, unsigned int maxK_)
{
maxK = maxK_;
cdf = new float[maxK];
float *pdf = new float[maxK];
// initial values, k=0:
float pTerm = std::pow(1.0f-p, static_cast(n)); // the p^k * (1-p)^(n-k) term
unsigned int coeffN = 1, coeffD = 1; // (N / D) evaluates to the same result as the n!/(k!(n-k)!) term
for (unsigned int k=0; k