Boost C++ Libraries: Ticket #9672: PDF and CDF of a laplace distribution throwing domain_error https://svn.boost.org/trac10/ticket/9672 <p> Hello, </p> <p> I would expect the following to behave just like a normal distribution (or a cauchy distribution) at x=+-infinity: </p> <pre class="wiki">// #define BOOST_MATH_DOMAIN_ERROR_POLICY ignore_error #include &lt;cstdlib&gt; #include &lt;cstdio&gt; #include &lt;limits&gt; #include &lt;boost/math/distributions/cauchy.hpp&gt; #include &lt;boost/math/distributions/laplace.hpp&gt; #include &lt;boost/math/distributions/normal.hpp&gt; static const double INFTY = std::numeric_limits&lt; double &gt;::infinity(); template&lt; typename T &gt; void TestBoundary( const char* distName, const T&amp; t ) { std::printf( "%15s ---------------------\n" "%15s %'*g %'*g\n", distName, "pdf", boost::math::pdf( t, -INFTY ), 10, boost::math::pdf( t, INFTY ), 10 ); std::printf( "%15s %'*g %'*g\n", "cdf", boost::math::cdf( t, -INFTY ), 10, boost::math::cdf( t, INFTY ), 10 ); std::printf( "%15s %'*g %'*g\n", "co-cdf", boost::math::cdf( boost::math::complement( t, -INFTY ) ), 10, boost::math::cdf( boost::math::complement( t, INFTY ) ), 10 ); }; int main() { std::printf( "%15s %10s %10s\n", "Distribution", "x=-infty", "x=+infty" ); double _location = 3.2; double _scale = 0.7; /* TestBoundary( "Normal", boost::math::normal_distribution&lt;&gt;( _location, _scale ) ); TestBoundary( "Cauchy", boost::math::cauchy_distribution&lt;&gt;( _location, _scale ) ); */ // To NOT throw domain_error exception at infinity? TestBoundary( "Laplace", boost::math::laplace_distribution&lt;&gt;( _location, _scale ) ); return EXIT_SUCCESS; } </pre><p> Simply compile with "g++ filename.cpp" and obtain the following when the resulting executable is ran: </p> <pre class="wiki">terminate called after throwing an instance of 'boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;std::domain_error&gt; &gt;' what(): Error in function boost::math::pdf(const laplace_distribution&lt;d&gt;&amp;, d)): Random variate x is inf, but must be finite! Abort </pre><p> If a random variable is documented to have a domain [-infty, +infty]---notice the closed interval, does it mean that the implementation will honor it (in that it would return the valid PDF or CDF at any value of the domain)---see the line above the graph of the PDF on <a href="http://www.boost.org/doc/libs/1_55_0/libs/math/doc/html/math_toolkit/dist_ref/dists/laplace_dist.html">http://www.boost.org/doc/libs/1_55_0/libs/math/doc/html/math_toolkit/dist_ref/dists/laplace_dist.html</a>; I assumed it meant to say the "domain of the random variable" instead of "the range of the random variable" (the last expression under <em>Non-member Accessors</em> paragraph has it right). </p> <p> Thank you.<br /> HS </p> <p> P.S. The following is from "git log -2" inside libs/math: </p> <pre class="wiki">commit ec438ff39d855f810e236dbaf14b8755f7157869 Author: Beman &lt;bdawes@acm.org&gt; Date: Sun Dec 1 09:14:12 2013 -0500 End-of-line normalization. Most normalization was taken care of by .gitattributes, but a few files originally committed to svn with incorrect eol tags need explicit normalization. See .gitattributes man page and extensive list discussion. commit fb52d2de42ff7e1cbc61103a25fbc82c5e034a90 Author: John Maddock &lt;john@johnmaddock.co.uk&gt; Date: Thu Oct 24 08:35:41 2013 +0000 Fix initialization of power series so that we don't get a spurious overflow error from tgamma when the res ult is actually zero. [SVN r86415] </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9672 Trac 1.4.3 John Maddock Mon, 17 Feb 2014 09:56:32 GMT owner changed https://svn.boost.org/trac10/ticket/9672#comment:1 https://svn.boost.org/trac10/ticket/9672#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">John Maddock</span> to <span class="trac-author">Paul A. Bristow</span> </li> </ul> <p> Originally we did not allow non-finite arguments anywhere, however, in the last release that should have been - mostly - fixed to allow infinite random variables when that made sense. Paul, I think you made those changes, were these distributions included? </p> Ticket Paul A. Bristow Mon, 17 Feb 2014 14:44:20 GMT <link>https://svn.boost.org/trac10/ticket/9672#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:2</guid> <description> <p> I have reordered the checks in laplace.hpp to now allow + and - infinity. </p> <p> Please can I email you a revised version for you to check that it meets your needs? Please email me at pbristow@… </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 17 Feb 2014 16:34:06 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9672#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:3</guid> <description> <p> Paul, you can attach files direct to this ticket. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Paul A. Bristow</dc:creator> <pubDate>Mon, 17 Feb 2014 16:47:58 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/9672 https://svn.boost.org/trac10/ticket/9672 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">laplace.hpp</span> </li> </ul> Ticket HS <tan@…> Tue, 18 Feb 2014 06:19:29 GMT <link>https://svn.boost.org/trac10/ticket/9672#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9672#comment:1" title="Comment 1">johnmaddock</a>: </p> <blockquote class="citation"> <p> Originally we did not allow non-finite arguments anywhere, however, in the last release that should have been - mostly - fixed to allow infinite random variables when that made sense. Paul, I think you made those changes, were these distributions included? </p> </blockquote> <p> Dear John, </p> <p> Thank you for your comment. </p> <p> I actually would expect every distribution function to be defined on the whole real line. And since each distribution function is often for us a "terminal function"---eg. determining P-values or critical values of statistical tests, I would also expect its implementation to support infinities (due to overflow when calculating a test statistic) and NaN (from 0/0 say). However, I do expect the distribution parameters to take on valid finite values only---conceptually, we cannot use a "normal distribution with infinite mean"; here, I would expect an implementation to throw the domain_error like your team is doing, or---my preference---to return NaN. </p> <p> But that is just my opinion as a statistician. </p> <p> With best regards,<br /> HS </p> </description> <category>Ticket</category> </item> <item> <author>HS <tan@…></author> <pubDate>Tue, 18 Feb 2014 06:26:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9672#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9672#comment:2" title="Comment 2">pbristow</a>: </p> <blockquote class="citation"> <p> I have reordered the checks in laplace.hpp to now allow + and - infinity. </p> <p> Please can I email you a revised version for you to check that it meets your needs? Please email me at pbristow@… </p> </blockquote> <p> Dear Paul, </p> <p> Thank you very much for the fixes; it has cleared my test deck. </p> <p> With best regards,<br /> HS </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Paul A. Bristow</dc:creator> <pubDate>Tue, 18 Feb 2014 10:24:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9672#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:6</guid> <description> <p> Good :-) </p> <p> Useful to have a professional statistician's view on when infinity should always be supported. </p> <p> In a sense, we already provide a way of achieving this view policies. If you implement a policy to ignore domain and or overflow errors, then you will get a NaN. You can achieve this now but studying the examples of using policies. </p> <p> When Boost.Math was first developed, the implementation of infinity and NaN was poorly and/or inconsistently supported, so our instinct was to duck the issues. </p> <p> But then John devised the 'policies' control of what happens when overflow or domain error is encountered. (This allows us to mimic what happens when you have C view of error handling and error return codes). </p> <p> Also other parts of Boost.Math have been extended to make it appear that infinity and NaN are fully supported. So we have started to permit infinity in some cases, but not all because of the work involved to changes things (and the many tests) in all the many functions. It has become a bit labyrinthine as a consequence. I'll put a fuller review on my TODO list. </p> <p> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Paul A. Bristow</dc:creator> <pubDate>Tue, 18 Feb 2014 17:33:47 GMT</pubDate> <title>status, milestone changed; keywords, resolution set https://svn.boost.org/trac10/ticket/9672#comment:7 https://svn.boost.org/trac10/ticket/9672#comment:7 <ul> <li><strong>keywords</strong> laplace added </li> <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> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.56.0</span> </li> </ul> <p> Fixed and pushed to develop. </p> <p> SHA-1: bf40c602967e3f87a375b3dc9b684245e270c2c1 </p> <ul><li>Random variate can be infinite. </li></ul> Ticket HS <tan@…> Thu, 20 Feb 2014 07:53:37 GMT <link>https://svn.boost.org/trac10/ticket/9672#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:8</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9672#comment:6" title="Comment 6">pbristow</a>: </p> <blockquote class="citation"> <p> Good :-) </p> <p> Useful to have a professional statistician's view on when infinity should always be supported. </p> <p> In a sense, we already provide a way of achieving this view policies. If you implement a policy to ignore domain and or overflow errors, then you will get a NaN. You can achieve this now but studying the examples of using policies. </p> <p> When Boost.Math was first developed, the implementation of infinity and NaN was poorly and/or inconsistently supported, so our instinct was to duck the issues. </p> <p> But then John devised the 'policies' control of what happens when overflow or domain error is encountered. (This allows us to mimic what happens when you have C view of error handling and error return codes). </p> <p> Also other parts of Boost.Math have been extended to make it appear that infinity and NaN are fully supported. So we have started to permit infinity in some cases, but not all because of the work involved to changes things (and the many tests) in all the many functions. It has become a bit labyrinthine as a consequence. I'll put a fuller review on my TODO list. </p> </blockquote> <p> Dear Paul, </p> <p> Sorry for the late response---trying to get use to modular-boost and "git submodule". </p> <p> Thanks for the reminder about policies---it is a beautiful pattern and I feel it is very well implemented in boost::math. </p> <p> Yes, I do use my own policies. While exceptions and/or error numbers are great tools in programming, I (as a statistician) do not find them useful here for me with distributions: for example, if any of the distribution parameters are invalid, then any caller (programmer or actual user) should get a "non-answer" (implemented by silent NaN which is wonderful since it "propagates")---no need to "leak" an exception; and each "proper" distribution (meaning all its parameters are valid) should also returns NaN when the input value is NaN. </p> <p> Thanks for including +-infinity as possible input values ... they do represent the sets of "large" (and "small") numbers ... and we do want to return 0 as the P-value when a test statistic is "infinite" (or "large" in mathematical sense). </p> <p> Let me know if you would like an extra pair of eyes to look over your proposed changes: I am not very good in reading code but I think I am a better reader of documents. :) </p> <p> With best regards,<br /> HS </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 05 Dec 2017 18:40:08 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9672#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:9</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/9672#comment:2" title="Comment 2">Paul A. Bristow</a>: </p> <blockquote class="citation"> <p> I have reordered the checks in laplace.hpp to now allow + and - infinity. </p> <p> Please can I email you a revised version for you to check that it meets your needs? Please email me at pbristow@… </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>merciu</dc:creator> <pubDate>Tue, 05 Dec 2017 18:41:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9672#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:10</guid> <description> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 05 Dec 2017 19:06:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9672#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:11</guid> <description> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 09 May 2018 03:20:43 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9672#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:12</guid> <description> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 09 May 2018 03:22:03 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9672#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9672#comment:13</guid> <description> </description> <category>Ticket</category> </item> </channel> </rss>