Boost C++ Libraries: Ticket #5095: incorrect results from hypergeometric pdf https://svn.boost.org/trac10/ticket/5095 <p> #include &lt;boost/math/distributions/hypergeometric.hpp&gt; #include &lt;boost/math/policies/policy.hpp&gt; #include &lt;iostream&gt; </p> <p> using namespace std; using namespace boost; </p> <p> int main() { </p> <blockquote> <p> unsigned N = 16086184; unsigned n = 256004; unsigned Q = 251138; math::hypergeometric_distribution&lt;double&gt; hyper(n, Q, N); cout &lt;&lt; math::pdf&lt;double&gt;(hyper, 4000) &lt;&lt; " " &lt;&lt; math::pdf&lt;double&gt;(hyper, 4001) &lt;&lt; " " &lt;&lt; math::pdf&lt;double&gt;(hyper, 4002) &lt;&lt; "\n"; </p> </blockquote> <blockquote> <p> return 0; </p> </blockquote> <p> } </p> <p> Output: 0.00640003 1.11519e-09 0.00638443 The value for 4001 is incorrect (according to <a class="ext-link" href="http://stattrek.com/Tables/Hypergeometric.aspx"><span class="icon">​</span>http://stattrek.com/Tables/Hypergeometric.aspx</a>). In fact, every value where k is odd appears to be incorrect. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5095 Trac 1.4.3 David Koes <dkoes@…> Wed, 19 Jan 2011 17:06:17 GMT attachment set https://svn.boost.org/trac10/ticket/5095 https://svn.boost.org/trac10/ticket/5095 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">hyper.cpp</span> </li> </ul> <p> example </p> Ticket David Koes <dkoes@…> Wed, 19 Jan 2011 17:26:05 GMT <link>https://svn.boost.org/trac10/ticket/5095#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:1</guid> <description> <p> As a workaround, if the lgamma backup version of hypergeometric_pdf_lanczos_imp is used instead, correct results are obtained. </p> <p> In /usr/local/include/boost/math/distributions/detail/hypergeometric_pdf.hpp: 414c414 &lt; result = detail::hypergeometric_pdf_lanczos_imp(value_type(), x, r, n, N, evaluation_type(), forwarding_policy()); --- </p> <blockquote class="citation"> <blockquote> <p> result = detail::hypergeometric_pdf_lanczos_imp(value_type(), x, r, n, N, lanczos::undefined_lanczos(), forwarding_policy()); </p> </blockquote> </blockquote> </description> <category>Ticket</category> </item> <item> <author>David Koes <dkoes@…></author> <pubDate>Wed, 19 Jan 2011 17:27:13 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5095#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:2</guid> <description> <p> Properly formatted diff of boost/math/distributions/detail/hypergeometric_pdf.hpp </p> <pre class="wiki">414c414 &lt; result = detail::hypergeometric_pdf_lanczos_imp(value_type(), x, r, n, N, evaluation_type(), forwarding_policy()); --- &gt; result = detail::hypergeometric_pdf_lanczos_imp(value_type(), x, r, n, N, lanczos::undefined_lanczos(), forwarding_policy()); </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Thu, 20 Jan 2011 12:35:39 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5095#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:3</guid> <description> <p> I've been unable to reproduce here either with VC10 or with gcc-4.4 on Ubuntu Linux, what compiler/platform are you on? </p> <p> The output I see from your program is: 0.00640003 0.00639305 0.00638443 which I believe agrees with the online calculator. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 20 Jan 2011 13:01:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5095#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:4</guid> <description> <p> gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) on Ubuntu 10.4 </p> <p> Also, on Ubuntu 10.10 with boost version 1.40 and gcc version 4.4.5 (<a class="missing wiki">Ubuntu/Linaro</a> 4.4.4-14ubuntu5) and OS X (10.6) with boost version 1.41 and gcc version 4.2.1 (Apple Inc. build 5664). </p> <p> Perhaps for some reason your installation is already falling back on the lgamma version of hypergeometric_pdf_lanczos_imp? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 20 Jan 2011 13:22:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5095#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:5</guid> <description> <p> No it's calling for example: </p> <p> hypergeometric_pdf_lanczos_imp&lt;long double,boost::math::lanczos::lanczos13m53,boost::math::policies::policy&lt;boost::math::policies::promote_float&lt;0&gt;,boost::math::policies::promote_double&lt;0&gt;,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy,boost::math::policies::default_policy&gt; &gt;(double <span class="underline">formal, unsigned int x, unsigned int r, unsigned int n, unsigned int N, double </span>formal, double <span class="underline">formal); </span></p> <p> I notice that you're using an out of date Boost distribution, can you: </p> <ul><li>Please try the last release and if that still fails, then: </li><li>Let me have the program output when BOOST_MATH_INSTRUMENT is #defined when building? </li></ul><p> Thanks, John. </p> </description> <category>Ticket</category> </item> <item> <author>David Koes <dkoes@…></author> <pubDate>Thu, 20 Jan 2011 14:48:33 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5095 https://svn.boost.org/trac10/ticket/5095 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">BMIdump</span> </li> </ul> <p> program output with BOOST_MATH_INSTRUMENT defined </p> Ticket David Koes <dkoes@…> Thu, 20 Jan 2011 14:49:13 GMT <link>https://svn.boost.org/trac10/ticket/5095#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:6</guid> <description> <p> The Ubuntu 10.04 box has boost 1.45 on it. I also tried it on two other machines with the version of boost that they already have. I have also grabbed and built the svn trunk on the OS X machine, and tried removing /usr/local/include/boost and reinstalling on the 10.4 machine. I still get the failure. </p> <p> I have attached the output with BOOST_MATH_INSTRUMENT defined. </p> <p> Thanks. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 20 Jan 2011 16:46:43 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5095#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:7</guid> <description> <p> Thanks for trying that, I really appreciate the help you're putting in to get to the bottom of this, unfortunately I still can't see what's gone wrong (other than something has), I'm also confused because there's really nothing in the code that should rely on the evenness of the random variable! </p> <p> I'm attaching an updated hypergeometric_pdf.hpp with a lot more instrumentation, can I get you to try again? Just the failing case this time - otherwise the output is going to be huge! :( </p> <p> Many thanks, </p> <p> Still confused yours, John. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 20 Jan 2011 16:47:43 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5095 https://svn.boost.org/trac10/ticket/5095 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">hypergeometric_pdf.hpp</span> </li> </ul> Ticket anonymous Thu, 20 Jan 2011 16:49:06 GMT <link>https://svn.boost.org/trac10/ticket/5095#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:8</guid> <description> <p> Forgot to say - if the program output is too large to attach here - zip it up and mail me direct at john at johnmaddock.co.uk </p> <p> Thanks! John. </p> </description> <category>Ticket</category> </item> <item> <author>David Koes <dkoes@…></author> <pubDate>Thu, 20 Jan 2011 16:54:40 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5095 https://svn.boost.org/trac10/ticket/5095 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">BMIdumpmore.gz</span> </li> </ul> <p> even more instrumentation (bad value only, gzipped) </p> Ticket David Koes <dkoes@…> Thu, 20 Jan 2011 17:07:57 GMT <link>https://svn.boost.org/trac10/ticket/5095#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:9</guid> <description> <p> Attached. Here are some interesting results that may partially explain the reproducibility problem: </p> <pre class="wiki">dkoes@quasar:~/tmp$ g++ hyper.cpp; ./a.out 1.11519e-09 dkoes@quasar:~/tmp$ g++ -m32 hyper.cpp; ./a.out 0.00639305 dkoes@quasar:~/tmp$ g++ -mfpmath=387 hyper.cpp; ./a.out 0.00639305 dkoes@quasar:~/tmp$ g++ -mfpmath=sse hyper.cpp; ./a.out 1.11519e-09 dkoes@quasar:~/tmp$ g++ -mno-sse hyper.cpp; ./a.out In file included from /usr/local/include/boost/config/no_tr1/cmath.hpp:21, from /usr/local/include/boost/math/policies/error_handling.hpp:15, from /usr/local/include/boost/math/distributions/detail/common_error_handling.hpp:12, from /usr/local/include/boost/math/distributions/hypergeometric.hpp:12, from hyper.cpp:1: /usr/include/c++/4.4/cmath: In function ‘double std::abs(double)’: /usr/include/c++/4.4/cmath:94: error: SSE register return with SSE disabled 1.11519e-09 dkoes@quasar:~/tmp$ g++ -mno-sse2 hyper.cpp; ./a.out 1.11519e-09 dkoes@quasar:~/tmp$ g++ -mno-sse3 hyper.cpp; ./a.out 1.11519e-09 dkoes@quasar:~/tmp$ g++ -mno-sse4 hyper.cpp; ./a.out 1.11519e-09 dkoes@quasar:~/tmp$ g++ -DBOOST_MATH_SPECIAL_FUNCTIONS_LANCZOS_SSE2 hyper.cpp; ./a.out 1.11519e-09 </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 20 Jan 2011 18:33:12 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5095#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:10</guid> <description> <p> Thanks for that, some of the values in the "exponents" table are getting truncated, I'm attaching an updated header with some extra typecasts present to try and prevent this, can you give this a try? </p> <p> Thanks, John. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 20 Jan 2011 18:33:49 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5095 https://svn.boost.org/trac10/ticket/5095 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">hypergeometric_pdf.2.hpp</span> </li> </ul> Ticket David Koes <dkoes@…> Thu, 20 Jan 2011 18:37:38 GMT <link>https://svn.boost.org/trac10/ticket/5095#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:11</guid> <description> <p> Bingo. That fixed it. Thanks! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Fri, 21 Jan 2011 12:03:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5095#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:12</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/68347" title="Added note about fixed bug. Refs #5095.">[68347]</a>) Added note about fixed bug. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5095" title="#5095: Bugs: incorrect results from hypergeometric pdf (closed: fixed)">#5095</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 21 Jan 2011 12:53:58 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5095#comment:13 https://svn.boost.org/trac10/ticket/5095#comment:13 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> This was fixed in revision <a class="changeset" href="https://svn.boost.org/trac10/changeset/68346" title="Add more debug/diagnostic info and fix bug that manifests itself on ...">[68346]</a>. </p> Ticket John Maddock Sat, 22 Jan 2011 17:31:32 GMT <link>https://svn.boost.org/trac10/ticket/5095#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5095#comment:14</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/68367" title="Merge fix for issue 5095. Refs #5095.">[68367]</a>) Merge fix for issue 5095. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5095" title="#5095: Bugs: incorrect results from hypergeometric pdf (closed: fixed)">#5095</a>. </p> </description> <category>Ticket</category> </item> </channel> </rss>