/*
     Description: 二项式分布相关函数

     Copyright : All right reserved by ICT

     Author : Zhang Zhonghai
     Date : 2025/12/26
*/
#pragma once

#include <math.h>
#include <float.h>
#include <limits>
#include "saddle_pe.h"

struct BinomialDistribution {
    /** The number of trials. */
    int numberOfTrials;
    /** The probability of success. */
    double probabilityOfSuccess;

    BinomialDistribution(int trials, double p) {
        probabilityOfSuccess = p;
        numberOfTrials = trials;
    }

    double logProbability(int x) {
        if (numberOfTrials == 0) {
            return (x == 0) ? 0. : -std::numeric_limits<double>::infinity();
        }
        double ret;
        if (x < 0 || x > numberOfTrials) {
            ret = -std::numeric_limits<double>::infinity();
        } else {
            ret = SaddlePointExpansion::logBinomialProbability(x, numberOfTrials, probabilityOfSuccess, 1.0 - probabilityOfSuccess);
        }
        return ret;
    }
};
