Boost超几何测试 我试图在C++中进行超几何测试作为练习,但每次都得到不同的答案。我的问题是,我是否在某个地方使用了导致pdf截短的int,或者我只是做了不正确的测试

Boost超几何测试 我试图在C++中进行超几何测试作为练习,但每次都得到不同的答案。我的问题是,我是否在某个地方使用了导致pdf截短的int,或者我只是做了不正确的测试,c++,r,C++,R,我的示例问题:假设我有100个大理石,其中8个是红色的。我画了30个弹珠,我想知道至少有5个弹珠是红色的概率是多少 #include <boost\math\distributions\hypergeometric.hpp> #include <cstdlib> #include <boost\math\policies\policy.hpp> #include <iostream> /* *Desc

我的示例问题:假设我有100个大理石,其中8个是红色的。我画了30个弹珠,我想知道至少有5个弹珠是红色的概率是多少

    #include <boost\math\distributions\hypergeometric.hpp>
    #include <cstdlib>
    #include <boost\math\policies\policy.hpp>
    #include <iostream>  


/*
    *Description:  Perform hyper geometric test on value.
    * Paramater val1: is r defective"sucess"
    * Parameter val2: is N-r total objects in population - success
    * Parameter val3: is Number of objects.
    * Paramater val4: Is K number of events
    */
    void hypergeometrictest(int n1_val, int n2_val, int n3_val, int n4_val)
    {
        double pdf, cdf;
        unsigned int n1 = n1_val;
        unsigned int n2 = n2_val;
        unsigned int t = n3_val;
        unsigned int k = n4_val;
        printf("These are values: %i, %i, %i, %i \n", n1, n2, t, k);

        // hg_dist(r,n,N)
        //r = success
        //n = 
        boost::math::hypergeometric_distribution<double> hg_dist(n1, t, n1 + n2);
        pdf = boost::math::pdf<double>(hg_dist, k);
        cdf = boost::math::cdf<double>(hg_dist, k);

        std::cout << "This is my PDF: " << pdf << std::endl;
        std::cout << "This is my CDF: " << cdf << std::endl;

    }

    int main() {

        hypergeometrictest(8, 92, 30, 5 );

        //working on windows PC but remove if on Unix
        system("pause");
        return 0;

    }
在R中,我进行了相同的超几何测试来检查,得到了不同的数字:

   phyper(4, 8, 92, 30, lower.tail=FALSE)
[1] 0.05042297

试图抓住问题

第一个问题是C++中实现的是等价于< /p> 哪个输出

[1] 0.949577
此标志表示您假设以下情况

lower.tail:逻辑;如果为真(默认),则概率为P[X≤x] ,否则,P[x>x]

有关这方面的进一步参考,请参见

第二个问题是,您使用了不同的数字,即
hypergeometrictest(8,92,30,5)
而不是
hypergeometrictest(8,92,30,4)

解决方案

所以要解决这个问题,你必须把补码取为1。为了计算CDF并使用正确的数字

cdf = 1. - boost::math::cdf<double>(hg_dist, k);
cdf=1boost::math::cdf(hg_dist,k);

你会得到
这是我的CDF:0.050423
(顺便说一下,这是正确的答案)。这显然与
1-phyper(…,lower.tail=TRUE)
phyper(…,lower.tail=FALSE)
试图抓住问题

第一个问题是C++中实现的是等价于< /p> 哪个输出

[1] 0.949577
此标志表示您假设以下情况

lower.tail:逻辑;如果为真(默认),则概率为P[X≤x] ,否则,P[x>x]

有关这方面的进一步参考,请参见

第二个问题是,您使用了不同的数字,即
hypergeometrictest(8,92,30,5)
而不是
hypergeometrictest(8,92,30,4)

解决方案

所以要解决这个问题,你必须把补码取为1。为了计算CDF并使用正确的数字

cdf = 1. - boost::math::cdf<double>(hg_dist, k);
cdf=1boost::math::cdf(hg_dist,k);

你会得到
这是我的CDF:0.050423
(顺便说一下,这是正确的答案)。很明显,这与
1-phyper(…,lower.tail=TRUE)
phyper(…,lower.tail=FALSE)

非常好的分解,包含了很多信息wow@pi我在这方面被难倒了一段时间。这太清楚了。非常好的分解,包含了很多信息。哇@PI我有一段时间被难住了。这很清楚。