# Adversarial attacks and optimal control

*Meta: After a fun little motivating section, this post goes deep into the mathematical weeds. This thing aims to explore the mathematical properties of adversarial attacks from first principles. Perhaps other people are not as confused about this point as I was, but hopefully, the arguments are still useful and/or interesting to some. I'd be curious to hear if I'm reinventing the wheel.*

**TL;DR:** 
===========

1.  When we invest the appropriate effort, the probability of random catastrophic events tends to decrease exponentially with a rate given by the[^6z98wkknhdh] rate function.
2.  One way of estimating the rate function is to solve an optimal control problem, where we have to determine the "least surprising" way that the catastrophic event comes about. The rate function equals the catastrophic event's surprise (in [nats](https://en.wikipedia.org/wiki/Nat_(unit))).
3.  Thus, intuitively, as we invest the effort to decrease the probability of random catastrophic events, the "difficulty" of performing an adversarial attack only increases linearly.

Real talk and real estate
=========================

Zillow is an American tech real-estate marketplace company that recently had (what the experts call) a small [snafu](https://en.wikipedia.org/wiki/SNAFU). They decided they were done *just* being a marketplace and started [buying up homes, completing light renovations, and then selling them with a profit](https://www.businessinsider.com/ibuyer-defunct-why-did-zillow-stop-buying-houses-2021-11). The whole thing went poorly; they bought houses too expensive and had to sell at a loss, [costing the company $420 million and leading to large lay-offs](https://www.nytimes.com/2021/11/02/business/zillow-q3-earnings-home-flipping-ibuying.html).

This story is not very interesting[^0w586uujp4ik] for anyone not directly involved. The reason I remember the whole affair is a [Twitter thread](https://twitter.com/ESYudkowsky/status/1456279486840721414?s=20&t=kqTdW87iwMg3f9zhqYKQ_w) that also caught the attention of Eliezer:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/8d72f51c57b8b34480eb33098d3121cb26d36bcc7b461f0f.png)

The thread proceeds to lay out how Zillow relied too much on their [automatic estimates](https://en.wikipedia.org/wiki/Zillow#Zestimate), which ([ex-ante](https://universalprior.substack.com/p/slightly-advanced-decision-theory?s=w#:~:text=is%20coming%20from.-,Diverse%20detours.,-On%20the%20topic)) looked good on average, but (ex-post) manifested in the worst possible way[^ggofla7z9wi]. Or, in the words of the OP:

> "\[They mistook\] an adversarial environment for a random one."

I have approximately zero knowledge about Zillow or the real estate business, so I can't comment on whether this actually is what went wrong (edit: see [this comment](https://www.lesswrong.com/posts/KtCJNw93KHg7MSSvw/adversarial-attacks-and-optimal-control?commentId=ZY6Qn95QC2muoKg5m) for a more informed take!). But the distinction between a **random** and an **adversarial** environment applies beyond the Zillow example and is relevant for research in AI Safety: from [adversarial examples](https://en.wikipedia.org/wiki/Adversarial_machine_learning), via [adversarial training](https://www.lesswrong.com/posts/A9tJFJY7DsGTFKKkh/high-stakes-alignment-via-adversarial-training-redwood), all the way to conceivable [existential risk scenarios](https://en.wikipedia.org/wiki/AI_box). It is the distinction between a world where it's fine to go outside because [getting struck by lightning is sufficiently unlikely](https://www.cdc.gov/disasters/lightning/victimdata.html) and a world where [somebody is trying to smite you](https://unsongbook.com/chapter-3-on-a-cloud-i-saw-a-child/#:~:text=ACROSS%20ALL%20SEPHIROT.-,I%20THINK%20SOMEBODY%20BOILED%20A%20GOAT%20IN%20ITS%20MOTHER%E2%80%99S%20MILK.%20IT%20IS%20ALWAYS%20THAT.%20I%20KEEP%20TELLING%20PEOPLE%20NOT%20TO%20DO%20IT%2C%20BUT%20NOBODY%20LISTENS,-.%E2%80%9D).

This post works towards achieving a mathematical understanding of what distinguishes [these two worlds](https://people.wou.edu/~shawd/mediocristan--extremistan.html). Perhaps other people are not as confused about this point as I was, but hopefully, the arguments are still useful and/or interesting to some. I'll introduce a bit of [extreme value theory.](https://en.wikipedia.org/wiki/Extreme_value_theory) Then I'll demonstrate a neat connection to [control theory](https://en.wikipedia.org/wiki/Control_theory) via [large deviation theory](https://en.wikipedia.org/wiki/Large_deviations_theory), which shows that an exponentially decreasing risk of failure translates into a linearly increasing difficulty of adversarial attacks.

A random environment
====================

I'll go over the basics quickly: When you (i.e. Zillow) think of your environment as random, each house you buy is essentially a coin flip with a biased coin. If you make sure that 

1.  the expected value of a deal is positive and 
2.  that all the deals are ([mostly)](https://en.wikipedia.org/wiki/Martingale_central_limit_theorem) independent of each other, 

then you're guaranteed to accrue profits *eventually*. 

![](https://firebasestorage.googleapis.com/v0/b/firescript-577a2.appspot.com/o/imgs%2Fapp%2Fjans-ideas%2FdLzMNgvild.png?alt=media&token=f803e7e3-1784-41f9-94b3-57e4b22b3599)

An illustration of how a sequence of house buys will end up net positive if repeated sufficiently long.

This guarantee is the [law of large numbers](https://en.wikipedia.org/wiki/Law_of_large_numbers) from probability theory 101 and probably not too revolutionary for you, dear reader. But what is the probability of things going very badly? What is the probability of an [extreme event](https://en.wikipedia.org/wiki/Extreme_value_theory) or a [large deviation](https://en.wikipedia.org/wiki/Large_deviations_theory)? It should decrease (given the law of large numbers), but how *fast* does it decrease?

Let's continue the example of buying and selling houses and say that there is a probability $p$  that a given deal ends up profitable. The worst-case probability that *all* your deals go poorly is $(1-p)^N$, which goes to $0$ exponentially fast as we increase the number of deals $N$.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/4e0c0a9d66db1868b56cf125b8aab41ce111739d62ab5b1a.png)

It turns out that this toy example [generalizes a lot,](https://en.wikipedia.org/wiki/Cram%C3%A9r%27s_theorem_%28large_deviations%29) and the probability that the sum of independent random variables $X_i$ is less than some fraction $\xi$ times $N$ ~~always~~[^ql3axbg9j6q] goes down exponentially fast in $N$:

$$
P\left(\sum_i X_i \leq N \xi \right)\approx \exp \left(-N \mathcal{J}(\xi)\right)
$$

The function $\mathcal{J}(\xi)$ is called the "[rate function,](https://en.wikipedia.org/wiki/Rate_function)" and it determines the speed with which extreme events get unlikely.

How to compute the rate function[^qshbiz56ueg]? That is what we'll explore in the next section. There is a "[standard](https://en.wikipedia.org/wiki/Cram%C3%A9r%27s_theorem_(large_deviations))" way of calculating $\mathcal{J}(\xi)$ which is a bit involved and opaque[^sm77jinmtt] for my taste; I prefer a slightly non-standard derivation[^l74ltltu7pc] of arriving at the rate function, which gives deeper insight into how adversarial attacks and extreme events relate.

Average surprise and optimal control
====================================

As I like to say: [there is, of course, another way of looking at this.](https://youtu.be/1bSPNboKCzM?t=966) We can think of the sequence of house deals as a sequence of random events that fluctuate around the probability of failure $1-p$. 

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/0e9f7babbb45f6ac7b5a6495af94f544adabdf7f451ae87e.png)

For a truly random sequence of events, we can compute the [**average surprise**](https://en.wikipedia.org/wiki/Entropy_(information_theory)) by averaging the information content of all events:

$$
I_p([x_1,\dots,x_N])=\frac{1}{N} \sum_i I_p(x_i),
$$

where the [**information content**](https://en.wikipedia.org/wiki/Information_content) of an individual event is given as

$$
I_p(x_i)=-\ln P_p(x_i),
$$

and the probability measure $P_p(x)$ is a Bernoulli measure, $P_p(x)=\begin{cases}p &: x = 1 \ 1-p &: x = 0\end{cases}$. In the picture, the probability of a random Bernoulli event is the distance from the dashed line, $P_p(x_i) = |x_i - (1-p)|$. Consequently, the information content is $I_p(x_i)=-\ln |x_i - (1-p)|$ and the average information content of the sequence is $-\frac{1}{N}\sum_i \ln |x_i - (1-p)|$.[^siwikxzu1ds]

Why am I bringing this up? [It turns out](https://en.wikipedia.org/wiki/Sanov%27s_theorem) that the rate function $\mathcal{J}(\xi)$ is the solution to [an optimal control problem](https://en.wikipedia.org/wiki/Freidlin%E2%80%93Wentzell_theorem) that involves the **average excess surprise**. In particular[^a7gmale7hl],

$$
\mathcal{J}(\xi ) = \inf_{\sum_i x_i = \xi N} \left[I_p([x_1,\dots,x_N]) - I_\xi([x_1,\dots,x_N])\right]
$$

The intuition behind this equation is: If we want to know the rate function for an extreme event, $[x_1,\dots,x_N]$ we get to pick how the extreme event comes about (that's the $\inf$). The only thing we have to respect is that the extreme event *does,* in fact, come about ($\sum_i x_i = \xi N$). And then we try to make it so that the [average excess surprise](https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence), $I_p([x_1,\dots,x_N]) - I_\xi([x_1,\dots,x_N])$, is as small as possible.

Let's see how this plays out in our house deal example. We want to determine the probability that the sum of independent random variables $X_i$ is less than some success fraction $\xi$ times $N$, $P(\sum_i X_i \leq N\xi)$, i.e. that a certain fraction $1-\xi$ of our $N$ house deals go badly.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/67706c705ae94e6a116a18455ad4dd9108b088cdb5b08de3.png)

Now *we* get to pick how the extreme event, $[x_1,\dots, x_N]$, comes about. I went ahead and already did that in the illustration. I picked it so that the first $(1-\xi)N$ deals go wrong, and only the remaining $\xi N$ deals go fine. This configuration satisfies the constraint that $\sum_i x_i = \xi N$ and (thanks to commutativity[^xj69oaxtug9]) the average excess surprise of this configuration is identical to that of all possible permutations. Therefore, this configuration's average excess surprise is already the infimum of all possible configurations that satisfy the constraint. Given the expression we wrote above for the average information content of a Bernoulli sequence, we can write the average excess surprise as

$$
\mathcal{J}(\xi ) = -\frac{1}{N}\sum_i \ln |x_i - (1-p)| + \frac{1}{N}\sum_i \ln |x_i - (1-\xi)|.
$$

Since we know that $x_1,\dots,x_{\xi N}$ are equal to $1$ and that $x_{\xi N+1},\dots,x_N$ are equal to $0$, we can simplify the expression for $\mathcal{J}(\xi )$ by splitting up the sums and regrouping,

$$
\mathcal{J}(\xi ) = \xi \ln\frac{\xi}{p} + (1-\xi) \ln \frac{1-\xi}{1-p}.
$$

You might recognize this as the KL-divergence between two Bernoulli random variables, $D_{\text{KL}}(\Xi||X) = \mathbb{E}(\Xi \ln \frac{\Xi}{X} )$, one with success probability $p$, the other with success probability $\xi$. When we plug in the extreme event that *all* the deals go poorly, $\xi = 0$, the rate function reduces to $\mathcal{J}(\xi) = -\ln (1-p)$, and the probability of this extreme event is

$$
P\left(\sum_i X_i = 0 \right)\approx \exp \left(-N \mathcal{J}(\xi)\right) = (1-p)^N,
$$

as it should be.

An adversarial environment
==========================

Quick recap: 

1.  When we find ourselves in a random environment, then we can leverage the law of large numbers to push the probability of failure close to zero by trying sufficiently many times[^x5080d4w3xo], $N$.
2.  If we want to find out *how fast* the probability of failure goes to zero, we can find that out by solving an optimal control problem where we need to determine the least surprising way failure might come about, $\mathcal{J}(\xi)$. 
3.  The probability of failure then goes to zero exponentially fast, proportional to how surprised we would be and the number of tries, $\exp (- N \mathcal{J}(\xi))$.

So far, so good, but... what is that sound in the distance[^h8hct6yaho]?

As general as the theorems mentioned above are in terms of the underlying distributions[^on02knohgks], they **centrally rely on the assumption that the environment is** ***random***. This was not the case for Zillow, where homeowners and real estate agencies selling houses had [additional information](https://en.wikipedia.org/wiki/Caveat_emptor) to select bad deals for Zillow systematically. Instead of pushing the risk of total failure strongly toward zero, Zillow only increasingly exposed itself to an adversary with more insight into the situation.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/11e07961fd0ad99b24f766d72597ff7b60b9c56f2307e09b.png)

While the **probability of complete failure,**  $\exp (- N \mathcal{J}(\xi))$, **falls exponentially**, the **amount of surprise required to bring it about**, $N \mathcal{J}(\xi)$, **scales linearly!** A few things of note:

1.  The unit of "surprise" is [nats](https://en.wikipedia.org/wiki/Nat_(unit)), a measure of information. This interpretation maps only weakly onto the house buying example (where one [bit](https://rdrr.io/cran/infotheo/man/natstobits.html#:~:text=Details,log2(e)%20%3D%201.442695.) of information might indicate whether a given deal is bad for Zillow). Still, it has a much more straightforward interpretation when we think about set-ups where [information passes through a single channel and can be arbitrarily constrained](https://en.wikipedia.org/wiki/AI_box).
2.  Similarly, we can interpret the expected excess surprise, $I_p([x_1,\dots,x_N]) - I_\xi([x_1,\dots,x_N])$, as a functional on a state space spanned by $[x_1,\dots,x_N]$. The fact that we compute the infimum over this functional turns the problem into an instance of [Hamilton's principle](https://en.wikipedia.org/wiki/Hamilton%27s_principle) \- the "true" evolution of the sequence of states is stationary w.r.t. the "action" given by the excess surprise. Through this lens, we're close to "[intelligence as optimization power](https://www.alignmentforum.org/tag/really-powerful-optimization-process)", which characterizes intelligence as the ability to steer reality into a small subset of possible states. But I can't quite put the pieces in the right place to make the connection click.
3.  Since we compute $N \mathcal{J}(\xi)$ as the solution to an optimal control problem that minimizes excess surprise, **it is the lower bound on the amount of surprise required**. Catastrophe can also come about in more surprising ways, but [highly improbable events tend to happen in the least improbable way](https://www.stat.cmu.edu/~cshalizi/754/2006/notes/lecture-30.pdf). On the plus side, catastrophe *cannot* come about with less surprise, which might have applications for the [set-ups with constrained communication](https://en.wikipedia.org/wiki/AI_box) mentioned in point 1.
4.  The team at Redwood Research recently [released their report on their research on high-stakes alignment](https://www.alignmentforum.org/posts/A9tJFJY7DsGTFKKkh/high-stakes-alignment-via-adversarial-training-redwood#:~:text=With%20a%20conservative,rate%20of%20injuriousness).), where they trained a filter to reduce the probability of harmful output from 2.5% in the training set to 0.003% after applying the filter to 0.002% after adversarial training. While OOM reduction in failure probability *feels* comforting, this only helps when the adversary is not actively trying to trick the filter (from 3.6 nats to 10.8 nats of information). This is consistent with their finding that their tool-assisted search for adversarial examples was 1000x more effective than naive sampling.

These factors combined make me excited about exploring this direction further to find reliable estimates of the lower bound of input required to bring about catastrophe[^ddia94okpmw]. I'm also curious if there is more structure to the similarity between the information content of an adversarial attack and the characterization of intelligence in terms of optimization processes. If you know more about this or can point me to some literature on the topic, please let me know!

[^6z98wkknhdh]:  (appropriately named) 

[^0w586uujp4ik]: How much is $420 million again? My gut feeling is that this type of snafu happens somewhere in the world every other week. 

[^ggofla7z9wi]: In the same way that the maximum of a set of random variables can be overwhelmingly good in expectation, the minimum of the same set is terrible (in expectation). That's the power of choice. 

[^ql3axbg9j6q]: Well, not always. That's the point of this post. 

[^qshbiz56ueg]: you ask, presumably on the edge of your seat. 

[^sm77jinmtt]: (you need the logarithm of the moment generating function of your random variable, \(\lambda(\theta) = \ln \mathbb{E}(\exp(\theta X_i))\), and then you need to compute the Legendre-Fenchel transformation, \(\mathcal{J}(\xi)= \sup _{\theta >0}[ \theta \xi -\lambda (\theta)]\).) 

[^l74ltltu7pc]: I don't have a reference that follows exactly the same path I choose, but I enjoy the texts by Touchette 2011 and Paninski 2006. 

[^siwikxzu1ds]: Absolute values are a bit icky, but since we have the \(\ln\) we can be a bit sneaky and write \(-\frac{1}{N}\frac{1}{2} \sum_i \ln ||x_i - (1-p)||^2\) instead. This now has an uncanny similarity to the Freidlin-Wentzell rate function, which can't be a coincidence. 

[^a7gmale7hl]: (I recommend staring at that for a while.) 

[^xj69oaxtug9]: When the control problem gets more involved and the individual time points are not interchangeable this step gets a bit trickier and we have to recruit some mathematical machinery. 

[^x5080d4w3xo]: Given that the expected value is positive ofc. 

[^h8hct6yaho]: source 

[^on02knohgks]: and any additional structure you might want to slap on 

[^ddia94okpmw]: Perhaps we can manage to reliably stay below that bound?