9 min read

Exponential Tilt

Applying information theory in the real world.

Doppel recently dropped a little puzzle, in their challenge for 3Blue1Brown's audience, that takes information-theoretic concepts and applies them to a real world use case. This puzzle is a detection problem where you, as a member of a cracked security team, are tasked with building an adversarial understanding of a lightweight detector that was built for identifying phishing attempts.

Here is the problem in its entirety:

Setup. You're a cracked security team defending your users against a known phisher. Each day, the two of you are locked in a cat-and-mouse fight. The attacker sends a phishing campaign and chooses how often to use each of nn persuasive phrases in their messages — things like "urgent", "verify account", "free gift", "security alert", "payroll".

The attacker selects a distribution p=(p1,p2,,pn)\mathbf{p} = (p_1, p_2, \ldots, p_n) over phrases T={t1,t2,,tn}T = \{t_1, t_2, \ldots, t_n\}, where pi=P(attacker uses ti)p_i = P(\text{attacker uses } t_i).

Each phrase tit_i has an intrinsic social-engineering payoff sis_i: if a message using tit_i reaches a user unflagged, it succeeds with probability sis_i. If the message is flagged or fails to convince, payoff =0= 0.

Your detector. You don't have the bandwidth to inspect every message perfectly — these phrases appear innocently in normal traffic too. Instead, you run a lightweight detector that monitors phrase frequencies. Given a baseline distribution q=(q1,q2,,qn)\mathbf{q} = (q_1, q_2, \ldots, q_n) representing natural phrase frequencies, you raise an alarm when:

ipilog ⁣(piqi)>C\sum_i p_i \log\!\left(\frac{p_i}{q_i}\right) > C

for some constant CC. (You might recognize this quantity.)

The question. What distribution p\mathbf{p} should the attacker choose to maximize their expected payoff?

Hint: the optimal strategy isn't deterministic.

I am going to walk through the best way to solve this puzzle with some callouts to resources in areas that I have struggled with in the past. As a little teaser, we will work together to find out that the optimal attack is neither mimicking normal traffic nor betting everything on the highest payoff phrase. We are going to see that exponentially tilting our baseline towards more effective phrases, using KL divergence as our measure of budget, is the best way to maximize the attacker’s payoff.

As a quick note, this problem only requires you to know KL Divergence and one technique from multivariable calc, both of which I touch on below.

This problem states that we need to put ourselves in the mind of the attacker. We need to come up with a distribution, pp, that maximizes our expected payoff. But what does that mean in practice?

Really we are just looking for

maxpipisisubject to various constraints\max_{p} \sum_i p_i s_i \quad \text{subject to various constraints}

But actually converting this into something we can use will require us to pull apart and put back together the expression just a little bit.

The Detector

Looking closer at the detector

ipilog ⁣(piqi)>C\sum_i p_i \log\!\left(\frac{p_i}{q_i}\right) > C

it becomes apparent that we can break this into two more familiar parts

ipilog ⁣(piqi)=(ipilogqi)(ipilogpi)\sum_i p_i \log\!\left(\frac{p_i}{q_i}\right) = \left(-\sum_i p_i \log q_i\right) - \left(-\sum_i p_i \log p_i\right)

This small change on its own may not look like a lot, but what does it remind you of?

If you were thinking, this looks suspiciously like KL divergence, D(pq)D(p \,\|\, q), it's because it is!

The first term ipilogqi-\sum_i p_i \log q_i is cross-entropy H(p,q)H(p, q). This is measuring the average cost of encoding messages from pp using qq's codebook.

The second term ipilogpi-\sum_i p_i \log p_i is Shannon entropy, H(p)H(p), or the cost of encoding those same messages using the best possible encoding for pp.

So the full expression D(pq)=H(p,q)H(p)D(p \,\|\, q) = H(p, q) - H(p) is measuring the difference between the two. We are capturing the excess cost the attacker pays for messages drawn according to pp when using a code optimized for qq.

Since we know the detector will alert when DKL(pq)>CD_{KL}(p \,\|\, q) > C, we have an upper limit on how far we can push pp before going over. Now we can rewrite our goal in clearer terms as

DKL(pq)CD_{KL}(p \,\|\, q) \le C

And even more clearly as DKL(pq)=CD_{KL}(p \,\|\, q) = C because, in the relevant non-deterministic regime, the optimal solution uses the entire KL budget. Therefore the fully constrained problem becomes

p=argmaxpipisisubject toDKL(pq)=C,ipi=1,pi0p^* = \arg\max_{p} \sum_i p_i s_i \quad \text{subject to} \quad D_{KL}(p \,\|\, q) = C, \quad \sum_i p_i = 1, \quad p_i \ge 0

interactive figure 01

The price of the wrong codebook

D(p ‖ q) = H(p, q) − H(p)

0%
p = q · no overpayment rare phrases used more often

width = how often p uses the phrase

height = −log₂qᵢ bits under q’s codebook

p widths
q baseline
urgent
q 32.0% → p 32.0% q-code 1.64 bits KL term +0.000b
verify account
q 24.0% → p 24.0% q-code 2.06 bits KL term +0.000b
free gift
q 12.0% → p 12.0% q-code 3.06 bits KL term +0.000b
security alert
q 22.0% → p 22.0% q-code 2.18 bits KL term +0.000b
payroll
q 10.0% → p 10.0% q-code 3.32 bits KL term +0.000b

same messages, two average bills

each color is one phrase’s contribution
D = 0.000 bits
q’s codebook
2.200b
p’s ideal code
2.200b
The skyline follows the codebook geometry in the 3Blue1Brown example: width is pᵢ and height is −log₂qᵢ, so area contributes to H(p, q). The essay’s derivation leaves the log base implicit; all four visualizations use log₂ so divergence and C are reported in bits. A different base rescales those quantities without changing the optimizer once C is rescaled.

Maximizing Payoff

Now that we know what we are optimizing against, it becomes much easier to attack the problem. Put in plain language, we need to find the best distribution, pp^*, that allows us to maximize our payoff from the function ipisi\sum_i p_i s_i when pp has a KL divergence budget of CC.

This is where the other technique we talked about earlier comes into play!

We can incorporate our constraints into a Lagrangian

L(p,λ,ν)=ipisiλ(ipilog ⁣(piqi)C)ν(ipi1)\mathcal{L}(p, \lambda, \nu) = \sum_i p_i s_i - \lambda\left(\sum_i p_i \log\!\left(\frac{p_i}{q_i}\right) - C\right) - \nu\left(\sum_i p_i - 1\right)

where λ0\lambda \ge 0 and νR\nu \in \mathbb{R}, and we assume pip_i and qi>0q_i > 0.

From there we just need to differentiate with respect to pip_i and set it equal to 0

Lpi=siλ(log ⁣(piqi)+1)ν=0\frac{\partial \mathcal{L}}{\partial p_i} = s_i - \lambda\left(\log\!\left(\frac{p_i}{q_i}\right) + 1\right) - \nu = 0

The stationarity condition requires Lpi=0\frac{\partial \mathcal{L}}{\partial p_i} = 0 for every ii to help us find candidates for the maximum. At the optimum, no infinitesimally small changes in pp can improve the constrained payoff, so every partial derivative of the Lagrangian we have put together must be 0. By setting the partial derivative equal to 0, we can satisfy the stationarity condition and continue on our way by solving for pip_i.

pi=qiesi/λeν/λ1p_i = q_i e^{s_i/\lambda} \cdot e^{-\nu/\lambda - 1}

This is the part that tripped me up when solving it myself so I'll take this next bit slow. We need to now normalize our equation to find pp^*. To do that means taking what we have and putting it over the sum of all our messages, ii.

pi(λ)=qiesi/λjqjesj/λp_i(\lambda) = \frac{q_i e^{s_i/\lambda}}{\sum_j q_j e^{s_j/\lambda}}

You may be asking yourself, "what happened to the eν/λ1e^{-\nu/\lambda - 1} term from the previous equation?" The truth is that after normalization, constants are factored out. The eν/λ1e^{-\nu/\lambda - 1} is independent of ii and appears in every numerator weight and their sum, so it cancels out.

But dividing by λ\lambda is not that pretty, so we can make some effort to make this more readable by re-parameterizing β=1λ\beta = \frac{1}{\lambda}. This actually makes the interpretation easier as well. If we assume that the KL constraint is active (which is needed in this non-deterministic regime), and that payoffs are not identical (this is where intuition comes in), then β>0\beta > 0.

pi(β)=qieβsiZ(β),Z(β)=jqjeβsjp_i(\beta) = \frac{q_i e^{\beta s_i}}{Z(\beta)}, \qquad Z(\beta) = \sum_j q_j e^{\beta s_j}

We have found the shape of the solution but haven't quite finished yet. This is the family of candidate distributions that we need to narrow down to find the best possible candidate. Different values of β\beta will produce different distributions:

  • When β=0\beta = 0, we are effectively saying that p=qp = q. If this is true then every attack would slide under the detector, but we wouldn't have found the maximum possible payouts.
  • As β\beta gets larger, more probability shifts towards phrases with higher payoff sis_i. However, we cannot increase β\beta forever or else we risk over-spending our KL budget, CC.

interactive figure 02

The aggression dial

pᵢ(β) = qᵢeβsᵢ / Z(β)

β = 2.06
β = 0 · hide β → 5 · greed

KL budget · bits

DKL(p(β) ‖ q) = 0.260 bits

alarm: off

β* spends the full budget without crossing it

Illustrative baseline frequencies and payoffs. The bars use the exact exponential-tilt formula and measure KL divergence in bits.

Because KL divergence increases with β\beta, we solve the scalar equation at exactly the point we run out of budget. To get the exact right level, we can do this by looking for β>0\beta > 0 such that

DKL(p(β)q)=CD_{KL}(p(\beta) \,\|\, q) = C

That's where the unique β\beta will result in our final solution β\beta^*. Incorporated into the equation

pi=qieβsiZ(β),Z(β)=jqjeβsjp_i^* = \frac{q_i e^{\beta^* s_i}}{Z(\beta^*)}, \qquad Z(\beta^*) = \sum_j q_j e^{\beta^* s_j}

Where β\beta^* is selected so that DKL(p(β)q)=CD_{KL}(p(\beta^*) \,\|\, q) = C

What this Equation Represents

Now that we have this cool equation, what the heck does it mean? In this section, I am going to break down each variable within the equation as it relates to our original problem and explain what happens when you tweak it.

pip_i^* is the optimal probability for phrase tit_i and the collection of all pip_i^* results in the pp^* distribution of phrases. It can be thought of as the perfect balance between resembling ordinary traffic and favoring useful phrases (those tit_i with high payoffs, sis_i).

qiq_i is the frequency that a phrase tit_i typically appears according to our baseline qq. It is how often the phrase typically appears in a regular stream of messages.

sis_i is the success probability for the use of a phrase tit_i in a message.

β\beta is the same as 1λ\frac{1}{\lambda} and is the "aggression dial" for the whole distribution. When β\beta is 0, p=qp = q which means the distribution looks exactly like normal traffic. Great for hiding your attacks, but not the best you can do when it comes to achieving high payoffs.

interactive figure 03

Why the dial stops

payoff + DKL bits over β

β = 2.06
β = 0 · hidden β → 5 · alarm
payoff 0.637 KL 0.260 bits
Expected payoff and KL divergence in bits as beta increasesExpected payoff rises and flattens. KL divergence, measured in bits, rises from zero and crosses the detectability budget at beta star, 2.06. Either aggression slider dial controls the tracking dots on both curves.payoff / KL bits0β → 5C = 0.26 bitsβ* 2.06alarm — visible, not reachablepayoff banked
The same illustrative phrase distribution as Figure 2. β* is where the KL curve reaches C = 0.26 bits; higher-payoff tilts to the right trigger the alarm.

We can put together two variables (β\beta, sis_i) to get the factor that rewards more effective phrases. This looks like eβsie^{\beta s_i}.

qieβsiq_i e^{\beta s_i} — Adding in qiq_i allows us to multiply our baseline probability by those payoff preferences. This is the exponential tilt where this type of equation gets its name from. Among all distributions with limited KL divergence from qq, the exponential tilt is the most efficient way to increase expected payoff. We are effectively spending the budget on favoring high-value phrases.

Z(β)Z(\beta) is our normalization constant that gets all the probabilities in the distribution to sum to 1.

CC is the constant that sets the whole thing in motion. In other words, it is the detectability budget our attacker can use to maximize their payoff.

What This Means for Defenders Like Doppel

While attackers can manipulate pip_i for any of their phrases, tit_i, there are only three levers that defenders can control, TT, qq, and CC within this toy model. In this section, we will walk through each and talk about the costs of Type I and Type II errors.

qq is our modeled expectations for how often we will see any of the loaded phrases from tt in regular, benign traffic. A naive approach to building qq is to sample how often each phrase in T={t1,t2,,tn}T = \{t_1, t_2, \ldots, t_n\} occurs within your current dataset.

A slightly less naive approach curates the dataset by filtering out all the phishing attempts that likely already exist within it (shout out to data labelers who do this kind of thankless work day in and day out!) This results in a clean qq model of how often those phrases should appear.

This assumes we have a good handle on most of TT. If we never think to add in a phrase tunkt_{\mathrm{unk}}, (in other words tunkTt_{\mathrm{unk}} \notin T) our detector could never trigger and attackers will just hammer that phrase until they succeed in their goal.

As defenders, we can adjust the qiq_i for any individual phrase in hopes of more quickly catching divergences that arise from the pp distribution we will see in production. We can also adjust all of qq by calibrating our dataset, smoothing, additional labeling, or selecting new/better datasets. By appropriately setting qq, we can effectively shape the attacker’s optimal response pp^* to catch bad actors and estimate the p^\hat{p} that we will see. But this comes at a cost (more on this in a moment). A good intuition for the effects of manipulating qq is that setting qi=0q_i = 0 means that any instance of tit_i will trigger the alert (KL divergence would be infinite and always past the threshold of CC.) It is probably good to note here that the whole of qq still sums to 1, so lowering qiq_i moves the center of the KL ball, essentially making phrase tit_i more anomalous. It in effect shifts sensitivity for that phrase.

We can also manipulate CC, or the amount of divergence we allow before we trigger an alert on a given distribution. Lowering the constant makes us more sensitive to perturbations in the distributions, effectively shrinking the attacker’s maximum achievable expected payoff by limiting the attacker's KL divergence budget. However, this comes at the same cost as many forms of adjusting qq.

Taking benign traffic as the null hypothesis, making the detector more sensitive (e.g., lowering qiq_i or lowering CC) will catch a lot more attacks, but it comes at the cost of increased Type I errors. All those false positives result in an annoying experience for our end users and complaints pointed at the defenders.

Making the detector less sensitive (leaving qiq_i as is, raising CC) would stop many of those false positives from happening, but more true positives would sneak by as false negatives. These kinds of errors can be catastrophic, so many defenders resolve to a more risk-averse position that is overly sensitive but mostly avoids Type II errors.

A real-world example of this is the heat Anthropic is catching from their strict cyber and bio safeguards. Even in something as innocuous as this essay (in which I had asked Claude Fable to check my notations), I triggered the safeguards multiple times. This tradeoff is at the heart of most threshold-based detection software no matter the sophistication. It appears in everything from lightweight detectors like this problem to ML and DL-based classifiers with millions to billions or trillions of parameters.

interactive figure 04

C is the control knob

lower C → more sensitive

C = 0.26 bits
tighten · more alarms loosen · more misses
Detector operating tradeoff controlled by CTightening C moves the detector up and to the right along an illustrative operating curve. More attacks are caught, but more benign traffic is falsely flagged. The risk-averse point is marked at C equals 0.26 bits.attacks caught ↑0%false positives → 50%risk-averseC is too permissive
false positives 27.5%

Type I · benign traffic flagged

missed attacks 5.5%

Type II · attacks that slip through

An illustrative operating curve; Doppel's measured performance may differ. Lowering C catches more attacks and accepts more false positives; C = 0.26 bits marks the risk-averse point described above.