Boost C++ Libraries: Ticket #12688: Boost..Accumulators test/median.cpp testing method is flawed https://svn.boost.org/trac10/ticket/12688 <p> I am going to stick with Stats terminology, because I get confused. I am going to use the word <em>sample</em> to mean a collection of data points or draws or observations, not a single one. </p> <ol><li>In the test, you generate a sample of 100,000 observations from N(1,1), and check if the medians computed by the implementations are close enough to the true population median of 1. </li></ol><p> That is flawed: The algorithms are for calculating the median of the set of numbers you have. Therefore, the medians they produce should be compared to the actual median of the sample you just generated. The median of that sample can be obtained by storing the draws in a <code>vector</code> and getting the exact median from that. </p> <p> This is not hard to do. I am not, however, submitting a pull request to do that because I am not convinced this approach is the right way to test the algorithms. </p> <ol start="2"><li>In the case of P<sup>2</sup>, the original paper contains a worked-out example by the authors using 20 observations. They show exactly what the algorithm should produce at each step. Your implementation does not match those steps. </li></ol><p> The following short program demonstrates this: </p> <pre class="wiki">#include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;utility&gt; #include &lt;vector&gt; #include &lt;boost/accumulators/accumulators.hpp&gt; #include &lt;boost/accumulators/statistics/stats.hpp&gt; #include &lt;boost/accumulators/statistics/median.hpp&gt; namespace bacc = boost::accumulators; int main(void) { bacc::accumulator_set&lt;double, bacc::stats&lt;bacc::tag::median(bacc::with_p_square_quantile)&gt; &gt; acc; // See http://www.cse.wustl.edu/~jain/papers/psqr.htm // First five observations acc(0.02); acc(0.5); acc(0.74); acc(3.39); acc(0.83); const std::vector&lt;std::pair&lt;double, double&gt; &gt; jain_chlamtac { { 22.37, 0.74 }, { 10.15, 0.74 }, { 15.43, 2.18 }, { 38.62, 4.75 }, { 15.92, 4.75 }, { 34.60, 9.28 }, { 10.28, 9.28 }, { 1.47, 9.28 }, { 0.40, 9.28 }, { 0.05, 6.30 }, { 11.39, 6.30 }, { 0.27, 6.30 }, { 0.42, 6.30 }, { 0.09, 4.44 }, { 11.37, 4.44 }, }; for (auto p: jain_chlamtac) { acc(p.first); std::cout &lt;&lt; "calculated= " &lt;&lt; bacc::median(acc) &lt;&lt; "\texpected= " &lt;&lt; p.second &lt;&lt; '\n'; } return 0; } </pre><p> This produces </p> <pre class="wiki">calculated= 0.74 expected= 0.74 calculated= 0.74 expected= 0.74 calculated= 2.06167 expected= 2.18 calculated= 4.55176 expected= 4.75 calculated= 4.55176 expected= 4.75 calculated= 9.15196 expected= 9.28 calculated= 9.15196 expected= 9.28 calculated= 9.15196 expected= 9.28 calculated= 9.15196 expected= 9.28 calculated= 6.17976 expected= 6.3 calculated= 6.17976 expected= 6.3 calculated= 6.17976 expected= 6.3 calculated= 6.17976 expected= 6.3 calculated= 4.24624 expected= 4.44 calculated= 4.24624 expected= 4.44 </pre><p> More a more detailed exposition, see my blog post <a class="ext-link" href="https://www.nu42.com/2016/12/cpp-boost-median-test.html"><span class="icon">​</span>https://www.nu42.com/2016/12/cpp-boost-median-test.html</a> </p> <p> This is the first time I am looking at Boost.Accumulators and I haven't figured out everything yet, so I do not have a fix (and I can't promise I am going to have a fix soon), but I thought I would get the ball rolling. Maybe the issue will be more transparent to you. </p> <p> To summarize: </p> <ol><li>The implementations should be tested against cases where we know what they are supposed to do. </li></ol><ol start="2"><li>It looks like there is a problem in the implementation of the P<sup>2</sup> algorithm. </li></ol><p> HTH, </p> <p> -- Sinan </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12688 Trac 1.4.3 A. Sinan Unur <sinan@…> Wed, 14 Dec 2016 16:38:43 GMT <link>https://svn.boost.org/trac10/ticket/12688#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12688#comment:1</guid> <description> <p> Actually, looking again at Table I in the paper, there is the possibility that intermediate calculations were made with rounded figures, and that may explain explain the discrepancy. I need to verify my paper &amp; pencil calculations again before I can say that conclusively. </p> <p> -- Sinan </p> </description> <category>Ticket</category> </item> <item> <author>A. Sinan Unur <sinan@…></author> <pubDate>Wed, 14 Dec 2016 17:43:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12688#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12688#comment:2</guid> <description> <p> This is the final actually: As I was verifying intermediate calculations by hand, I noticed that the body of the paper lists the first five observations as </p> <blockquote> <p> 0.02, 0.5, 0.74, 3.99, 0.83 </p> </blockquote> <p> whereas they use </p> <blockquote> <p> 0.02, 0.15, 0.74, 3.99, 0.83 </p> </blockquote> <p> to produce Table I. </p> <p> With that adjustment, the program: </p> <pre class="wiki">#include &lt;algorithm&gt; #include &lt;cstdio&gt; #include &lt;string&gt; #include &lt;utility&gt; #include &lt;vector&gt; #include &lt;boost/accumulators/accumulators.hpp&gt; #include &lt;boost/accumulators/statistics/stats.hpp&gt; #include &lt;boost/accumulators/statistics/median.hpp&gt; namespace bacc = boost::accumulators; int main(void) { bacc::accumulator_set&lt;double, bacc::stats&lt;bacc::tag::median(bacc::with_p_square_quantile)&gt; &gt; acc; // See http://www.cse.wustl.edu/~jain/papers/psqr.htm // First five observations acc(0.02); acc(0.15); acc(0.74); acc(3.39); acc(0.83); const std::vector&lt;std::pair&lt;double, double&gt; &gt; jain_chlamtac { {22.37, 0.74}, {10.15, 0.74}, {15.43, 2.18}, {38.62, 4.75}, {15.92, 4.75}, {34.60, 9.28}, {10.28, 9.28}, {1.47, 9.28}, {0.40, 9.28}, {0.05, 6.30}, {11.39, 6.30}, {0.27, 6.30}, {0.42, 6.30}, {0.09, 4.44}, {11.37, 4.44}, }; for (auto p: jain_chlamtac) { acc(p.first); std::printf("calculated= %.3f\texpected= %.2f\n", bacc::median(acc), p.second); } return 0; } </pre><p> produces the output: </p> <pre class="wiki">calculated= 0.740 expected= 0.74 calculated= 0.740 expected= 0.74 calculated= 2.178 expected= 2.18 calculated= 4.753 expected= 4.75 calculated= 4.753 expected= 4.75 calculated= 9.275 expected= 9.28 calculated= 9.275 expected= 9.28 calculated= 9.275 expected= 9.28 calculated= 9.275 expected= 9.28 calculated= 6.297 expected= 6.30 calculated= 6.297 expected= 6.30 calculated= 6.297 expected= 6.30 calculated= 6.297 expected= 6.30 calculated= 4.441 expected= 4.44 calculated= 4.441 expected= 4.44 </pre><p> which is definitely good enough. </p> <p> I sent an email to Dr. Jain pointing out the discrepancy between the text of the paper and the numbers used to produce Table I. </p> <p> This removes my concern that there was something subtly wrong with the implementation in <code>p_square_quantile.hpp</code> (I did re-write the key sections several times trying to figure out what it might be) and establishes that the implementation can replicate the output in the original paper. </p> <p> I still think the test needs to be improved, but now I feel free to focus on that. </p> <p> I will submit a pull request as soon as I have some time to put it together. </p> <p> HTH, </p> <p> -- Sinan </p> </description> <category>Ticket</category> </item> </channel> </rss>