snoplusuk / echidna

MIT License
4 stars 12 forks source link

Floating point issues when checking if prior is in parameter values #128

Closed ashleyrback closed 8 years ago

ashleyrback commented 8 years ago

I've re-enabled this check for all parameter values, including log-scale signal rates, as I think it does make sense to enforce this.

However when creating log-space arrays of values for signal rates, that I define to ensure this criterion is met, e.g.:

# For given high, prior and (odd) bins values
low = prior ** 2. / high
values =  numpy.logspace(numpy.log(low), numpy.log(high), bins, base=numpy.e)

I still get the error:

ValueError: Prior not in values array. This can be achieved with an odd number of bins and symmetric low and high values about the prior.

We test the condition with:

indices = numpy.where(values == self._prior)[0]
if len(indices) == 0:
    raise ValueError

but == has the potential for floating point errors in comparisons, so I'm wondering if we should change to something like:

if not numpy.any(
        numpy.around(values / prior, 12) == numpy.around(1., 12)):
    raise ValueError

which should avoid this.

ashleyrback commented 8 years ago

Done!