jeudi 5 mars 2015

Bug in density calculation std::piecewise_constant_distribution?

It seems that std::piecewise_constant_distribution computes the densities wrongly , at least with GCC and its standard library.


According to http://ift.tt/1B7rpnb: The densities should be computed as: enter image description here


Checking this manually reveals the bug!


This can be seen here: http://ift.tt/1B7rrLF


The source code related to this is found in /usr/include/c++/4.8/bits/random.tcc (on linux) and the extract of the initialization function _M_initialize called by the constructor shows that there is something incorrect here:



const double __sum = std::accumulate(_M_den.begin(),
_M_den.end(), 0.0);

__detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
__sum); <----- WRONG

// THIS is not the cummulative distribution (since the above normalization does not give the probability of the intervalls!)
_M_cp.reserve(_M_den.size());
std::partial_sum(_M_den.begin(), _M_den.end(),
std::back_inserter(_M_cp));


// Make sure the last cumulative probability is one.
_M_cp[_M_cp.size() - 1] = 1.0;



// Dividing here by the interval length is WRONG!!!

for (size_t __k = 0; __k < _M_den.size(); ++__k)
_M_den[__k] /= _M_int[__k + 1] - _M_int[__k];

Aucun commentaire:

Enregistrer un commentaire