Proof by Simulation
by Simji (審遲)

Converging to insight, one iteration at a time, being thoroughly convinced, at least for the time being...

Basic Properties of the Log-Mean-Exp Operator

Overview

Consider any $\eta \in \Reals \setminus \{ 0 \}$ Sometimes, $\eta$ is called the temperature because of analogues from thermodynamics. and vector $q = [q_1, \ldots, q_n] \in \Reals^n$. Define the log-mean-exp operator

$$ \LME_{\eta}[q] := \frac{1}{\eta}\log \Big( \sum_i p_i e^{\eta q_i} \Big) $$

where the $p_i$ are positive weights summing to one.

Let $q_{\min}$ and $q_{\max}$ denote the minimal and maximal values of $q$.

Intuitive Interpretation #

This (differential) operator smoothly interpolates between key statistics as $\eta$ varies as demonstrated in the following statement. Image Visualisation of $\LME_\eta[q]$ for varying temperatures. In this example $q := [e^{-1}, \varphi \approx 1.618, e, \pi]$ with $p = [0.1, 0.2, 0.4, 0.3]$.

Proposition

For fixed $q$ whose values are not all identical the log-mean-exp operator increases monotonically with respect to $\eta \in \Reals \setminus \{ 0 \}$. Moreover,

  • $$ \lim_{\eta \rightarrow \infty} \LME_\eta[q] = q_{\max} \label{eq:lmelimit1} $$
  • $$ \lim_{\eta \rightarrow -\infty} \LME_\eta[q] = q_{\min} \label{eq:lmelimit2} $$
  • $$ \lim_{\eta \rightarrow 0} \LME_\eta[q] = \Exp_p[q] :=\sum_{i}^{n} p_i q_i. \label{eq:lmelimit3} $$

Proof. Monotonicity. Let $K(\eta) = \log(\sum_i p_i e^{\eta q_i})$, which is the cumulant-generating function of a discrete random variable $Q$ taking values $q_i$ with probability $p_i$. The operator can then be written as:

$$ f(\eta) = \frac{K(\eta)}{\eta}. $$

Taking the derivative with respect to $\eta$:

$$ \frac{d}{d\eta} f(\eta) = \frac{\eta K'(\eta) - K(\eta)}{\eta^2}. $$

Because $K(\eta)$ is strictly convex whenever the values $q_i$ are not all identical, 1It is easy and mundane to show that $K''(\eta) = \Var_\eta(Q) > 0$. any tangent line lies strictly below the function. Evaluating the first-order convexity condition at $0$:

$$ K(0) > K(\eta) + K'(\eta)(0 - \eta). $$

Since $\eta^2 > 0$ for all $\eta \ne 0$, the derivative $\frac{d}{d\eta} f(\eta) > 0$ for all $\eta \ne 0$.

Asymptotic Limits. Let $I_{\max}$ and $I_{\min}$ be the set of indices for which $q$ attains its maximal or minimal value. To verify $\eqref{eq:lmelimit1}$ and $\eqref{eq:lmelimit2}$ observe that

$$ \LME_\eta[q] =q_{\max} + \frac{1}{\eta} \log \Big( \sum_{i \in I_{\max}} p_i + \sum_{j \notin I_{\max}} p_j e^{\eta (q_j - q_{\max})} \Big) \xrightarrow[\eta \rightarrow \infty]{} q_{\max} $$

$$ \LME_\eta[q] =q_{\min} + \frac{1}{\eta} \log \Big( \sum_{i \in I_{\min}} p_i + \sum_{j \notin I_{\min}} p_j e^{\eta (q_j - q_{\min})} \Big) \xrightarrow[\eta \rightarrow -\infty]{} q_{\min}. $$

Finally, $\eqref{eq:lmelimit3}$ is a straightforward application of L’hôpital’s rule. ■

Remark

Given the above result, by defining $\LME_\eta[q] := \sum_i p_i q_i$ at $\eta = 0$, the operator becomes continuous and well-defined across the entire real line ($\eta \in \mathbb{R}$).

Computation #

Overflow and the “shift trick” #

To avoid numerical overflow when computing the log-mean-exp operator—especially when $\eta$ or $q_i$ are large—use the “shift trick”.

The trick works by factoring out the maximum exponent $e^{\eta q_{\max}}$ so that the largest term inside the exponential becomes $e^0 = 1$, preventing float overflow. That is,

$$\LME_{\eta}[q] = \frac{1}{\eta} \log \Big( \sum_i \pi_i e^{\eta q_i} \Big) = q_{\max} + \frac{1}{\eta} \log \Big( \sum_i \pi_i e^{\eta (q_i - q_{\max})} \Big)$$

Since $q_i - q_{\max} \le 0$ for all $i$, every exponent $\eta (q_i - q_{\max}) \le 0$ (for $\eta > 0$). This guarantees that $e^{\eta (q_i - q_{\max})} \in (0, 1]$, making overflow impossible.

Underflow #

Partial underflow is less catastrophic since it simply means that the subsets of terms $e^{\eta(q_i - q_{\max})}$ underflowing has a negligible contribution. However, total underflow—i.e. where all the terms underflow to zero—is a critical failure since we end up computing $\log(0)$ which evaluates to negative infinity (i.e. NaN). There is a large risk of this happening if $\eta$ is too large.

Single-pass streaming (no prior maximum calculation) #

Scanning an array twice—once to find $q_{\max}$ and once to compute the shifted sum—is inefficient for large $n$ since it requires duplicating effort.

Instead, we can compute $\LME_\eta[q]$ in a single pass dynamically without pre-computing $q_{\max}$.

The high-level logic is akin, for instance, to Welford’s algorithm. As you process elements $q_k$ and weights $\pi_k$ one by one from $k = 1$ to $n$, maintain two tracking variables:

Then, for each new incoming item $(q_k, \pi_k)$, update the running maximum $m_k = \max(m_{k-1}, q_k)$ and update the running sum via the recurrence relation

$$ s_k = s_{k-1} \cdot e^{\eta(m_{k-1} - m_k)} + \pi_k \cdot e^{\eta(q_k - m_k)} \quad \text{for } k = 2, 3, \ldots, n. $$

It is straightforward to verify that this recurrence relation yields $s_k = \sum_{i=1}^k \pi_i e^{\eta(q_i - m_{k-1})}$ via induction. After processing all $n$ items, the final log-mean-exp is obtained simply by scaling back out of the log-sum:

$$\LME_\eta[q] = m_n + \frac{1}{\eta} \log(s_n).$$