How to Use Numpy Logaddexp in Python?

Numpy Logaddexp

With a series of articles in AskPython elaborating on the various functions available within the numpy library of Python, here’s another one with combines logarithms and exponentials! Let’s get things started by first importing the numpy library using the following code.

import numpy as np

Thereafter, we shall explore the logaddexp( ) function further through each of the following sections.

  • Syntax of the logaddexp( ) function
  • Why use logaddexp(a,b) instead of log(exp(a) + exp(b))?
  • Using logaddexp( ) on N-Dimensional Arrays

Syntax of the logaddexp( ) function

The logaddexp( ) function adds the exponentials of two scalars or arrays and then deduces the natural logarithm of the resulting value. Following is the syntax of the logaddexp( ) function which contains both the mandatory and the optional inputs required for its functioning.

numpy. logaddexp(x1, x2, out=None, *, where=True, dtype=None)

where,

  • x1, x2 – Scalars or N-dimensional arrays for whose sum the logarithm is to be calculated
  • out – an optional construct set to none by default, but could be used to store the results in the desired array which is of the same size as the output
  • * kwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function
  • where – an optional construct which is used to calculate the universal function (ufunc) at the given position when set to True (default setting) or not calculated when set to False
  • dtype – an optional construct used to specify the data type which is being used

Why use logaddexp(a,b) instead of log(exp(a) + exp(b))?

We can also find the logarithm of the sum of exponentials by summing the given inputs using the exp( ) function within the log( ) function. So, let’s find out why there’s an exclusive function to perform the same set of operations.

a = 1.03
b = 0.0006
np.log(np.exp(a)+ np.exp(b))
np.logaddexp(a,b)

Following are the results of the above code.

Comparing Results
Comparing Results

Both results are the same with these values. But, what if we reduce ‘x’ way lesser and put it through the same functions? Let’s find out what happens then!

a = 1.03e-5
b = 6e-7
np.log(np.exp(a)+ np.exp(b))
np.logaddexp(a,b)
Comparing Results When A B LT 0
Comparing Results when a,b << 0

With a closer look, it could be seen that the last decimal digit ain’t the same! The main reason behind this is that when the inputs are very small to exceed the normal range of the floating point numbers, then summing up the exponentials within the logarithm function doesn’t return accurate results. Thusly, logaddexp( ) is predominantly used in statistics for returning accurate results, when the calculated probabilities of events are very small.


Using logaddexp( ) on N-Dimensional Arrays

In this section let’s try deploying the logaddexp( ) function only at select positions of the N-dimensional array.

ar1 = np.array([[1e-10, 8e-3, 0.09],
                [5.007, 33e-5, 2e-7]])
ar2 = np.array([[2e-6, 10e-3, 9e-4],
                [0.006, 4e-9, 7e-6]])
np.logaddexp(ar1, ar2, where = [[False, True, True],
                                 [True, True, False]])
Using Logaddexp On N Dimensional Arrays
Using Logaddexp on N-Dimensional Arrays

Since the where option has been exercised, it could be seen that the results returned only in the positions given as True are valid & those elsewhere are erroneous. Don’t believe it? Let’s assign this to an output array & see what becometh of it.

r = np.zeros((2,3))
np.logaddexp(ar1, ar2, where = [[False, True, True],
                                [True, True, False]], out = r)
Zeros Returned On False Positions
Zeros Returned on False Positions

Only the values returned by the function remain whilst the others, being erroneous, are replaced with zeros by the rather wiser out option.


Conclusion

Now that we have reached the end of this article, hope it has elaborated on how to use the logaddexp( ) function from the numpy library. Here’s another article that details the usage of log1p( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est faucere!