Boost C++ Libraries: Ticket #10110: boost::random::negative_binomial_distribution unnecessary restriction to IntType for parameter k https://svn.boost.org/trac10/ticket/10110 <p> Hi. </p> <p> In the implementation of boost::random::negative_binomial_distribution there is an unnecessary restriction to <a class="missing wiki">IntType</a> for parameter k. </p> <p> A double parameter k will be casted to integer resulting in a different distribution than desired. If one compiles with -Wconversion a warning is given: warning: conversion to ‘int’ from ‘double’ may alter its value [-Wconversion] see attached example_file. This can lead to unwanted realization of a negative binomial distribution for non integer parameters k. </p> <p> Further there are no assertion checks on the parameters in instantiating a boost::random::negative_binomial_distribution object. These assertions are checked during the sampling process via boost::random::gamma_distribution. There (and in general for negative binomial distribution) the parameter k must be strictly greater than zero and not as mentioned in the documentation "Requires: k &gt;=0". </p> <p> This can easily be extended by changing <a class="missing wiki">IntType</a> to <a class="missing wiki">RealType</a> in 5 occurrences regarding the parameter k. </p> <p> Thank you, Max </p> <p> P.S.: I compiled the example file with: g++ -Wconversion -o negbin_test -std=c++11 negbin_test.cpp </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10110 Trac 1.4.3 Maximilian Kleinert <kleinert.max@…> Tue, 10 Jun 2014 20:24:12 GMT attachment set https://svn.boost.org/trac10/ticket/10110 https://svn.boost.org/trac10/ticket/10110 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">negbin_test.cpp</span> </li> </ul> <p> Example of double to int conversion in negative_binomial_distribution </p> Ticket Steven Watanabe Thu, 26 Feb 2015 01:02:41 GMT <link>https://svn.boost.org/trac10/ticket/10110#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10110#comment:1</guid> <description> <p> I'm on the fence about this. This generalization is reasonable, but it is not consistent with std::negative_binomial_distribution and the difference is subtle enough that it could result in non-obvious bugs if someone were to try replacing boost::random with std. </p> </description> <category>Ticket</category> </item> <item> <author>Maximilian Kleinert <kleinert.max@…></author> <pubDate>Tue, 10 Mar 2015 22:21:33 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/10110 https://svn.boost.org/trac10/ticket/10110 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test.cpp</span> </li> </ul> <p> Test paramter assertions </p> Ticket Maximilian Kleinert <kleinert.max@…> Tue, 10 Mar 2015 22:23:40 GMT attachment set https://svn.boost.org/trac10/ticket/10110 https://svn.boost.org/trac10/ticket/10110 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test.2.cpp</span> </li> </ul> <p> Test paramter assertions </p> Ticket Maximilian Kleinert <kleinert.max@…> Tue, 10 Mar 2015 22:26:31 GMT attachment set https://svn.boost.org/trac10/ticket/10110 https://svn.boost.org/trac10/ticket/10110 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">comparison.txt</span> </li> </ul> <p> Comparison of assertion tests </p> Ticket Maximilian Kleinert <kleinert.max@…> Tue, 10 Mar 2015 22:29:19 GMT <link>https://svn.boost.org/trac10/ticket/10110#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10110#comment:2</guid> <description> <p> Hi Steven, </p> <p> thanks for your reply. I think that this implicit cast can lead to non-obvious bugs as well, since it changes the distribution in case for non integer parameter k. The standard only describes the negative binomial distribution for trials, e.g. integer parameters. So the behavior for a non integer value is unspecified by the standard. In practice, e.g. claim number modeling, real positive parameters are widely used. To avoid the implicit cast bug it would be possible to delete the constructor for non integer types. Then still no real parameters are usable (in fact they are not proper right now) but the compiler will reject it if someone tries. </p> <p> Regarding the issue about the assertions. I did some tests comparing std::negative_binomial_distribution and the one from boost. Summarizing, the parameter checks are not very satisfying either, see the attached comparison.txt. </p> <p> In my opinion it makes no sense that a negative_binomial_distribution with invalid parameters can be constructed and then may or may not fail during the sampling process. Especially in the case k=0 were -2<sup>31 is produced which can happen if someone calculates with the (in principal) valid parameter k = 0.99. </sup></p> <p> I produced these results with g++-4.9.real (Ubuntu 4.9.1-16ubuntu6) 4.9.1 and compiled the attached example with: g++ -D_GLIBCXX_DEBUG -std=c++11 -o test test.cpp and without the _GLIBCXX_DEBUG flag. </p> <p> It would be great if both implementations of the negative binomial distribution will improve. </p> <p> Thank you, Max </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Tue, 10 Mar 2015 22:52:46 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10110#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10110#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/10110#comment:2" title="Comment 2">Maximilian Kleinert &lt;kleinert.max@…&gt;</a>: </p> <blockquote class="citation"> <p> The standard only describes the negative binomial distribution for trials, e.g. integer parameters. So the behavior for a non integer value is unspecified by the standard. </p> </blockquote> <p> Not correct. The standard specifies that it takes an integer parameter. It also states that a floating point number can be converted to an integer. Therefore the behavior when passing a floating point number is specified even if it isn't useful. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Wed, 11 Mar 2015 02:28:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10110#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10110#comment:4</guid> <description> <p> k = 0 and p = 1 are degenerate cases which should be supported. </p> </description> <category>Ticket</category> </item> <item> <author>Maximilian Kleinert <kleinert.max@…></author> <pubDate>Wed, 11 Mar 2015 08:07:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10110#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10110#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/10110#comment:3" title="Comment 3">steven_watanabe</a>: </p> <blockquote class="citation"> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/10110#comment:2" title="Comment 2">Maximilian Kleinert &lt;kleinert.max@…&gt;</a>: </p> <blockquote class="citation"> <p> The standard only describes the negative binomial distribution for trials, e.g. integer parameters. So the behavior for a non integer value is unspecified by the standard. </p> </blockquote> <p> Not correct. The standard specifies that it takes an integer parameter. It also states that a floating point number can be converted to an integer. Therefore the behavior when passing a floating point number is specified even if it isn't useful. </p> </blockquote> <p> Ok. So the only possibility would be to add an extra distribution, e.g. Polya distribution, which is essentially the same except that it allows a real positive parameter k? </p> </description> <category>Ticket</category> </item> <item> <author>Maximilian Kleinert <kleinert.max@…></author> <pubDate>Wed, 11 Mar 2015 08:28:41 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10110#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10110#comment:6</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/10110#comment:4" title="Comment 4">steven_watanabe</a>: </p> <blockquote class="citation"> <p> k = 0 and p = 1 are degenerate cases which should be supported. </p> </blockquote> <p> The case p = 1 (regardless of k) is not supported by the boost version: The gamma distribution fails because the parameter beta = (1-p)/p is not greater than zero. The standard version fails if compiled with -D_GLIBCXX_DEBUG using g++. </p> <p> The working draft standard (N4926) page 965 requires: 0 &lt; p &lt;= 1 and 0 &lt; k. </p> <p> Boost and standard version (except compiled with -D_GLIBCXX_DEBUG) return in the case p = 0 the sample -2147483648 which is not very useful. </p> </description> <category>Ticket</category> </item> </channel> </rss>