Numerically Stable P-Norms
Consider the p-norm
In python this translates to:
text
from numpy import array
def norm1(x, p):
"First-pass implementation of p-norm."
return (np.abs(x)**p).sum() ** (1./p)Now, suppose causes overflow (for some ). This will occur for sufficiently large or sufficiently large —even if is representable (i.e., not NaN or ).
For example:
text
>>> big = 1e300
>>> x = array([big])
>>> norm1(x, p=2)
[ inf] # expected: 1e+300This fails because we can't square big
text
>>> np.array([big])**2
[ inf]A Little Math
There is a way to avoid overflowing because of a few large .
Here's a little fact about p-norms: for any and
We'll use the following version (harder to remember)
Don't believe it? Here's some algebra:
\begin{eqnarray*}
|| \boldsymbol{x} ||_p
&=& \left( \sum_i |x_i|^p \right)^{\frac{1}{p}} \\
&=& \left( \sum_i \frac{|\alpha|^p}{|\alpha|^p} \cdot |x_i|^p \right)^{\frac{1}{p}} \\
&=& |\alpha| \cdot \left( \sum_i \left( \frac{|x_i| }{|\alpha|} \right)^p \right)^{\frac{1}{p}} \\
&=& |\alpha| \cdot \left( \sum_i \left| \frac{x_i }{\alpha} \right|^p \right)^{\frac{1}{p}} \\
&=& |\alpha| \cdot || \boldsymbol{x} / \alpha ||_p
\end{eqnarray*}
Back to Numerical Stability
Suppose we pick . Now, the largest number we have to take the power of is one—making it very difficult to overflow on the account of . This should remind you of the infamous log-sum-exp trick.
text
def robust_norm(x, p):
a = np.abs(x).max()
return a * norm1(x / a, p)Now, our example from before works :-)
text
>>> robust_norm(x, p=2)
1e+300Remarks
- It appears as if
scipy.linalg.normis robust to overflow, whilenumpy.linalg.normis not. Note thatscipy.linalg.normappears to be a bit slower. - The
logsumexptrick is nearly identical, but operates in the log-domain, i.e., . You can implement both tricks with the same code, if you use different number classes for log-domain and real-domain—a trick you might have seen before.
text
from arsenal.math import logsumexp
from numpy import log, exp, abs
>>> p = 2
>>> x = array([1,2,4,5])
>>> logsumexp(log(abs(x)) * p) / p
1.91432069824
>>> log(robust_norm(x, p))
1.91432069824