Numerical Stability: Log-Sum-Exp
When dealing with probabilities, especially in log-space, sums of exponentials can lead to numerical underflow or overflow. For example, computing can be problematic if are very large (overflow) or very small (underflow). The "Log-Sum-Exp" trick provides a numerically stable way to compute this value.
The trick is based on the identity:
where . By subtracting the maximum value, the exponents become non-positive, preventing overflow. Furthermore, at least one term will be , preventing all terms from vanishing to zero due to underflow.
Your task is to implement the logsumexp function using this trick.
Implementation Details:
* logsumexp(x): Takes a 1D NumPy array x of log-probabilities or log-unnormalized scores.
* It should return a single scalar representing the numerically stable .
Verification:
1. Test with a simple array x = np.array([1, 2, 3]).
2. Test with an array containing large values, e.g., x = np.array([1000, 1001, 1002]).
3. Test with an array containing small (negative) values, e.g., x = np.array([-1000, -1001, -1002]).
4. Compare your results with scipy.special.logsumexp for various inputs. Ensure the output is very close (e.g., difference less than 1e-10).