Boost C++ Libraries: Ticket #9183: quantile for poisson distribution - precision issue? https://svn.boost.org/trac10/ticket/9183 <p> The following (there are others) returns an "unexpected" result: </p> <p> #define BOOST_MATH_DISCRETE_QUANTILE_POLICY integer_round_up <em> #define BOOST_MATH_DISCRETE_QUANTILE_POLICY real </em></p> <p> #include &lt;cstdlib&gt; #include &lt;cstdio&gt; </p> <p> #include &lt;boost/math/distributions/poisson.hpp&gt; </p> <p> int main() { </p> <blockquote> <p> double m=54.3; double x=66.0; </p> </blockquote> <blockquote> <p> boost::math::poisson_distribution&lt;&gt; dist( m ); </p> </blockquote> <blockquote> <p> std::printf( </p> <blockquote> <p> "\nqpois(ppois(66,54.3),54.3) = %d\n\n", boost::math::quantile( </p> <blockquote> <p> dist, boost::math::cdf( dist, x ) ) ); </p> </blockquote> </blockquote> </blockquote> <blockquote> <p> return 0 </p> </blockquote> <p> } </p> <p> The "expected" answer is 66 but 67 is being returned. </p> <p> I am aware of Ticket 8308 (<a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/8308"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/8308</a>). I do not think R is "rounding to the nearest", and as far as I am aware, it has been consistent with </p> <blockquote> <p> quantile(p)=infimum {x|cdf(x)&gt;=p} </p> </blockquote> <p> which is what "we" needed/expected. </p> <p> If we change the policy to give real output---on my Ubuntu 12.04 (Intel 14.0 C++ with g++ 4.7.3), the result is 6.600000000000001e+01. So I wonder if this is just a (finite) precision/rounding issue ... in that it is really 66 to some "fuzz". </p> <p> I have (also) tried this with revision 86054 of the trunk. </p> <p> Thank you! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9183 Trac 1.4.3 anonymous Mon, 30 Sep 2013 18:14:49 GMT <link>https://svn.boost.org/trac10/ticket/9183#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9183#comment:1</guid> <description> <p> I'm not surprised that that doesn't work as you expected - as you said it's a rounding issue - the calculated cdf is necessarily an approximation, and if you then try to invert that the chances of getting exactly 66 (or some other integer) back are basically nil. I guess we could check to see if the real-valued result is very near to an integer and then double check to see if that integer still fulfills the specified requirements, but I'm betting that might not work in this case either... </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Wed, 02 Oct 2013 17:23:22 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/9183 https://svn.boost.org/trac10/ticket/9183 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">math-discrete-distro.patch</span> </li> </ul> Ticket John Maddock Wed, 02 Oct 2013 17:26:03 GMT <link>https://svn.boost.org/trac10/ticket/9183#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9183#comment:2</guid> <description> <p> Can you try the attached patch? I think it fixes this and gives somewhat more sane results in the corner cases. Note however that it's still not possible to round trip in the general case - particularly when the probability is very close to 1 (within a few epsilon) as in that situation there may be multiple values for the random variable which all give the same CDF value. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 03 Oct 2013 13:01:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9183#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9183#comment:3</guid> <description> <p> Dear John, </p> <p> Thank you very much for the fixes. Yes, things look good here (I will run through even more tests later). </p> <p> By "in the general case", do you mean when x (in the example above) is real, or that the "round trip" is also "bad" even when x is integral? Only the latter case is important to us. </p> <p> Best regards, HS </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 03 Oct 2013 16:03:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9183#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9183#comment:4</guid> <description> <blockquote class="citation"> <p> By "in the general case", do you mean when x (in the example above) is real, or that the "round trip" is also "bad" even when x is integral? Only the latter case is important to us. </p> </blockquote> <p> When x is integral and large there may be multiple (all integral) values of x which all yield the same cdf, this usually occurs when the cdf is a couple of epsilon less than 1, but I found a few cases for very small cdf values as well. There's nothing that can be done about those cases - other than moving to a type with more precision, or, in the case where the cdf is near one, using complements instead. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Tue, 08 Oct 2013 17:17:29 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9183#comment:5 https://svn.boost.org/trac10/ticket/9183#comment:5 <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> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/86205" title="Improve quantiles of discrete distributions to round trip integers ...">[86205]</a>) Improve quantiles of discrete distributions to round trip integers more often. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9183" title="#9183: Support Requests: quantile for poisson distribution - precision issue? (closed: fixed)">#9183</a>. </p> Ticket John Maddock Sat, 12 Oct 2013 09:41:01 GMT <link>https://svn.boost.org/trac10/ticket/9183#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9183#comment:6</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/86259" title="Apply patch for issue #9183. Refs #9183.">[86259]</a>) Apply patch for issue <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9183" title="#9183: Support Requests: quantile for poisson distribution - precision issue? (closed: fixed)">#9183</a>. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9183" title="#9183: Support Requests: quantile for poisson distribution - precision issue? (closed: fixed)">#9183</a>. </p> </description> <category>Ticket</category> </item> </channel> </rss>