티스토리 뷰

카테고리 없음

binormial probability 계산

newpolaris 2019. 12. 9. 14:21

https://www.geeksforgeeks.org/binomial-random-variables/

// C++ program to compute Binomial Probability 
#include <iostream> 
#include <cmath> 
using namespace std; 

// function to calculate nCr i.e., number of  
// ways to choose r out of n objects 
int nCr(int n, int r) 
{ 
    // Since nCr is same as nC(n-r) 
    // To decrease number of iterations 
    if (r > n / 2) 
        r = n - r; 

    int answer = 1; 
    for (int i = 1; i <= r; i++) { 
        answer *= (n - r + i); 
        answer /= i; 
    } 

    return answer; 
} 

// function to calculate binomial r.v. probability 
float binomialProbability(int n, int k, float p) 
{ 
    return nCr(n, k) * pow(p, k) * 
                pow(1 - p, n - k); 
} 

간단하지만,

n = 100 정도에도 오버플로우 난다

python의 경우,

>>> binom.pmf(300, 10000, 0.03)
0.0233799093035578

boost의 결과는,

double의 유효자리는 16자리라 한다

0.023379909303099091766

https://stackoverflow.com/questions/1095650/how-can-i-efficiently-calculate-the-binomial-cumulative-distribution-function

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크