Code Monkey home page Code Monkey logo

Comments (5)

stephanlachnit avatar stephanlachnit commented on July 29, 2024

I think overall option 4 provides the best balance between precision, performance, ease of implementation and platform compatibility.

Unfortunately, this is not safe either:

=============================== warnings summary ===============================
.pybuild/cpython3_3.11_iminuit/build/tests/test_cost.py::test_multinominal_chi2
.pybuild/cpython3_3.11_iminuit/build/tests/test_without_numba.py::test_no_numba[_safe_log-args0]
.pybuild/cpython3_3.11_iminuit/build/tests/test_without_numba.py::test_no_numba[_safe_log-args0]
.pybuild/cpython3_3.11_iminuit/build/tests/test_without_numba.py::test_no_numba[_unbinned_nll-args2]
.pybuild/cpython3_3.11_iminuit/build/tests/test_without_numba.py::test_no_numba[multinominal_chi2-args3]
.pybuild/cpython3_3.11_iminuit/build/tests/test_without_numba.py::test_no_numba[poisson_chi2-args5]
  /home/stephan/Projects/debian/iminuit/iminuit/.pybuild/cpython3_3.11_iminuit/build/iminuit/cost.py:119: RuntimeWarning: divide by zero encountered in log
    return np.clip(np.log(x), -1e3, None)

from iminuit.

stephanlachnit avatar stephanlachnit commented on July 29, 2024

Okay, I can up with another option:

np.log(np.maximum(np.finfo(np.float64).smallest_subnormal, x))

It takes the smallest subnormal of the 64-bit float representation and has with no loss in precision. Drawback is that this returns at least float64 type, no matter if x is float32. I have to check whether this actually works though.

from iminuit.

HDembinski avatar HDembinski commented on July 29, 2024

@stephanlachnit Thank you for this great and deep analysis, this all makes sense to me.

Why do you think that np.log(np.finfo(np.float64).smallest_subnormal + x) would not work?

Adding is a bit faster, but if your version is better for compatibility, then we should use that.

In [7]: x = np.linspace(0, 20, 1000)

In [8]: %timeit np.log(np.finfo(np.float64).smallest_subnormal + x)
6.51 µs ± 69.2 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [9]: %timeit np.log(np.maximum(np.finfo(np.float64).smallest_subnormal, x))
6.64 µs ± 54.8 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

from iminuit.

stephanlachnit avatar stephanlachnit commented on July 29, 2024

Why do you think that np.log(np.finfo(np.float64).smallest_subnormal + x) would not work?

Adding is a bit faster, but if your version is better for compatibility, then we should use that.

If x is negative for some reason, the operation is not safe.

from iminuit.

HDembinski avatar HDembinski commented on July 29, 2024

Good point.

from iminuit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.