# Visual demonstration of Optimizer's curse

*Epistemic status: I am currently taking* [*this course*](https://agentfoundations.study/)*, and after reading* [*this post on Goodhart's curse,*](https://arbital.greaterwrong.com/p/goodharts_curse?l=6g4) *I wanted to create some visuals to better understand the described effect.*

Chapter 1: Optimization
=======================

Veronica is a stock trader, and her job is to buy stocks, and then sell it at a specific time during the month. Her objective is to maximize profit. Here is the graph of how much money she will make if she sells at some specific time:

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

This can be called Veronica’s *utility function* $U(x)$, so if she sells at a time $x$, she makes $U(x)$ dollars.[^7d7omjn91gj]

But she does not have access to this function. She only has access to a giant supercomputer, which works like this: she presses the button, the computer chooses some random moment in the month $x$ and calculates $U(x)$. Running this computer takes a lot of time, so she can only observe a handful of points. After that, she can analyze this data to make a decision about the selling time.

Her current algorithm is as follows:

> Algorithm 1
> 
> 1.  Run the computer $N$ times, generating $N$ pairs of the type $(x_{i}, U(x_{i}))$.
> 2.  Locate the maximum using argmax: $\DeclareMathOperator*{\argmax}{argmax} x_{max} = \argmax \limits_{x_i \in \{x_1, \dotsc ,x_N\}}[U(x_{i})]$
> 3.  Sell stocks at the time $x_{max}$ and receive $U(x_{max})$ dollars.

Here is the picture of how one such scenario might look like:

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

The number $N$ of observed points (aka the number of times Veronica pressed the button) will be referred to as 'optimization power'. The higher this number, the closer (on average) her proxy maxima will be to the actual maximum.

Here are 1,000 such experiments, in each of which the optimization power was 5. The bar chart is the probability density of the proxy maxima chosen by the Algorithm 1.[^0zio58817efn] We can see that points are clustering around the true maximum point ($x=0.25$). The “average value” is the average of the utility function $U$ Veronica obtains from those proxy maxima (the expected amount of money she makes with this algorithm). It is noticeably smaller than the actual maximum value she can achieve.

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

With the lowest optimization power ($N = 1$) Veronica doesn't gain any information, and is her guess is equivalent to a random choice (since she is choosing from a single randomly selected point).

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

Here is what happens if she cranks up the optimization power (runs the forecasting supercomputer many times).

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

Her expected profit is now much closer to the actual maximum profit. Additionally, the distribution is much narrower: now proxy maxima are, on average much closer to the actual maximum.

Here is a graph of the dependence of the average value on the optimization power.

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

Notice how it increases monotonically (which shouldn't be surprising).

Chapter 2: Optimizer's curse
============================

In reality, forecasting distant events is hard, and the further away the event, the harder it becomes. To account for that, we can add noise in Veronica's measurements of utility. In this example it would be the error in computed profits; and the further in time we try to make predictions, the harder it is, and that is reflected with purple area on the plot. In my model, what Veronica gets from the computer is the actual value of $U$ at that point $+$ gaussian noise. So now, the result of the computer's prediction is $(x_i, \widetilde{U}(x_i))$, where $\widetilde{U}(x_i)$ is a noisy estimation of $U(x_i)$. Purple area represents $1\sigma$ interval, and $\sigma(x) = x^4$, so it increases with time. Notice that this is an unbiased estimation: on average, with a lot of samples, she would get closer and closer to the actual value of $U$.

Here's how one of these scenarios might play out.

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

In this case, Veronica's proxy maximum lies near the actual maximum. However, that is not always the case.

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

Sometimes, (as in example above) flukes from the noisy area are higher than the signal from the actual near-maximum area, and her chosen proxy maximum is way off.

Now let’s look at a distribution of proxy maxima (1,000 experiments).

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

The average value decreased a bit, but that is to be expected, we introduced some obstacle. But notice how the distribution is bimodal: the noise we introduced caused some of Veronica's estimations of maximum to be completely off. That’s because when she samples, some of the points, which are located in the high-variance area have high noise-induced values, and this is not balanced with points with low noise-induced value, because those low points effectively do not “participate in the competition”. This effect is called *Optimizer's curse*: tendency of optimizers to seek high areas with high variance.

But surely, with the help of bigger optimization power Veronica can fix this, that helped her to make expected utility bigger before, right? But no! In fact (with the assumptions we made) it gets worse:

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

An increase in optimization power resulted in a lower average value because, with more samples, the probability that the highest point from a noisy area exceeds the actual maximum of the utility function increases. Here’s the graph of the average value with respect to optimization power. At some point, higher optimization power is worse than random sampling.

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

Chapter 3: Bayesian remedy
==========================

Now, before we see Veronica using Bayes theorem with her task, let's take a step back and look at an easy example of applying bayesian updates.

Roman is a physicist running an experiment. He observes the decay of some particle. He does not know the [half-life period](https://en.wikipedia.org/wiki/Half-life) $t_{1/2}$ of this particle, and needs to infer it from observations.

Every time he sees a particle decay, he records how long it took. The probability density of this time is $\rho(t|t_{1/2}) = \dfrac{1}{t_{1/2}}\exp(\dfrac{t}{t_{1/2}})$. This is called [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution).

He knows that $t_{1/2}$ is somewhere between 0.5 and 2 seconds. His prior distribution on $\rho(t_{1/2})$ might look something like this:

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

Each possible value of the $t_{1/2}$ is a *hypothesis*,  and the prior distribution assigns some probability (in our case probability density) to each hypothesis. After Roman gets an observation $t_1$, he updates his beliefs about how likely each hypothesis is using Bayes's theorem:[^y3w09m11yo]

$\rho(t_{1/2}|t_1)= \dfrac{\rho(t_{1/2})\rho(t_1|t_{1/2})}{P(t_1)} = \dfrac{1}{P(t_1)}\cdot\dfrac{2}{3}\mathbb{1}_{[0.5;2]} (t_{1/2})\cdot\dfrac{1}{t_{1/2}}\exp(-\dfrac{t_1}{t_{1/2}})$[^f5l2lo1xfxr]

The term in the denominator is a constant and can be calculated using normalization.

After the first observation, Roman's posterior distribution looks like this:

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

Then Roman can call the resulting distribution his new prior distribution (and he can do that because observations are uncorrelated) and keep doing the process. Here is a GIF of how that would look like:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/ed8d34277a7ec4e51ddf3becfd77f2351199a95d9e6d4aae.gif)

It gets narrower and higher,and in the limit, there should be a high peak around the true value of $t_{1/2}$ (in our case $t_{1/2}=1$ sec).

His algorithm is as follows:

> Algorithm 2
> 
> 1.  Set some prior distribution  $\rho_0(t_{1/2})$
> 2.  Get the observation $t_i$ of a decay time of a specific particle.
> 3.  Calculate posterior distribution using Bayes theorem: $ \rho_i(t_{1/2}|t_i)= \dfrac{\rho_i(t_{1/2})\rho(t_i|t_{1/2})}{P(t_i)}$
> 4.  Set this posterior distribution as the new prior distribution and return to step 2.
>     
>     $\rho_{i+1}(t_{1/2}) = \rho_i(t_{1/2}|t_i)$
>     

Now let's get back to Veronica and her stock trading problem.

To get the bayesian method working, Veronica needs a space of hypotheses. Each hypothesis is a distribution which generates observations, and with each observation she would narrow down this space of hypothesis. 

Before she runs a computer, she has some idea of what the utility function might look like. She thinks that it is a smooth function with a single peak, but she does not know the value of this peak or where it is on the $x$ axis. Also, she accounts for a potential noise in predictions from the computer.

Colormap of one such hypothesis (which shows with what probability each pair $(x, \widetilde{U}(x))$ can be outputed) with a specific value of peak hight $h=0.7$ and $x$-coordinate of this peak $\mu=0.3$ would look like this (the blacker the region, the higher the probability):

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

Each hypothesis is defined by values of $\mu$ and $h$. For each pair ($\mu, h$) we can have a probability distribution on $(x, \widetilde{U}(x))$. The formula for each hypothesis is:

$\rho(x, y|\mu, h) = \dfrac{1}{\sqrt{2 \pi \sigma_y}}\exp \left(-\dfrac{(y-y_0(x, \mu))}{2\sigma_y^2} \right)$, where $y_0(x, \mu) = h\exp \left(\dfrac{-(x-\mu)^2}{2\sigma_x^2}\right)$

($\sigma_x$ and $\sigma_y$ are chosen beforehand, and equal 0.07 and 0.2 accordingly). The formula is a bit messy, but essentially it is a function with one peak, blurred with noise. 

We can show all of these hypotheses and the probabilities Veronica is assigning to them on a colormap:

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

This is Veronica's prior distribution on $(\mu, h)$-hypotheses. Veronica is skeptical of high peaks, so she puts low prior probability on them. The formula for her prior distribution:

$\rho(\mu, h) = \dfrac{1}{\sqrt{2 \pi \sigma_{p}}} \exp( -\dfrac{h^2}{2\sigma_p^2})$

($\sigma_p$ is the magnitude of her skepticism of high peaks, in our case $\sigma_p = 1$).

It is worth noting that the actual distribution she gets from the computer is not represented by any hypothesis. But this wouldn't be much of a problem in this setting.

Now Veronica can apply Bayes formula using the observations (in this case, observation is a prediction $(x_i, \widetilde{U}(x_i))$ she recieves from a computer, in the next formula they would be noted as $(x,y)$).

$\rho(\mu, h| x, y) = \dfrac{\rho( x, y|\mu, h) \rho(\mu, h)}{P(x, y)}$

She can make updates each time she gets an observation, just like Roman in the previous example.

Here is a GIF of a 50 consecutive updates (the red dot represents the observation, and the green dot represents the hypothesis that is closest to the actual distribution we get from the computer):

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/animations/b8ee5b8acf541ac6479debf45244811a2484550461312cb5.gif)

After this process, Veronica can take the most likely hypothesis and use its $\mu$ value as her best guess for when to sell stocks. Here's her algorithm:

> Algorithm 3
> 
> 1.  Set some prior distribution  $\rho_0(\mu, h)$
> 2.  Get the observation $(x_i, \widetilde{U}(x_i))$ of a prediction from a computer.
> 3.  Calculate posterior distribution using Bayes theorem: $\rho_{i}(\mu, h|x_i, y_i)= \dfrac{\rho_i(\mu, h)\rho(x_i, y_i|\mu, h)}{P(x_i, y_i)}$
> 4.  Set this posterior distribution as new prior distribution and go to step 2. Repeat N times.
>     
>     $\rho_{i+1}(\mu, h)=\rho_{i}(\mu, h|x_i, y_i)$
>     
> 5.  Get the most likely hypothesis from last distribution and sell stocks at $ \mu_{max}$.
>     
>     $\DeclareMathOperator*{\argmax}{argmax} \mu_{max} = \argmax \limits_{\mu \in [0, 1]}[\rho_{N}(\mu, h)]$
>     

Here is the distribution of these values with 5 updates.

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

And here is the distribution with 50 updates.

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

And here is the graph of the average value depending on the number of updates.

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

This graph is now again monotonic. Optimizer's curse is cured with Bayesian remedy.

Optimizer's curse arises not only with the kind of optimizer I showed here. It could also be a problem with SGD and other optimizers.

Chapter 4: Goodhart's curse
===========================

Now I want to ask a question I had while reading the course materials. Here’s a quote from a post on [Goodhart's Curse](https://arbital.greaterwrong.com/p/goodharts_curse?l=6g4):

> Even if $U$ is locally an unbiased estimator of $V$, optimizing $U$ will seek out what *we* would regard as "errors in the definition", places where $U$ diverges upward from $V$. Optimizing for a high $U$ may implicitly seek out regions where $U-V$ is high; that is, places where $V$ is lower than $U$. This may especially include regions of the outcome space or policy space where the value learning system was subject to great variance; that is, places where the value learning worked poorly or ran into a snag.

Now here is my question: if $U$ is unbiased estimator of $V$ (that is, if we sample a lot of $U_i(x_0)$ for some fixed $x_0$ and average them out, we get $\sum_{i=1}^N \frac{1}{N} U_i\left(x_0\right)=V\left(x_0\right)$), what do we mean by $U-V$? If we mean  $E[U-V]$ (expected value), than this is is 0 everywhere, if we mean the random variable $U-V$, then what do we mean by areas where it is high?

[^7d7omjn91gj]: Note that this is not a realistic graph of how much money Veronica would make if she sold some stock, this function is chosen for ease of demonstration. 

[^0zio58817efn]: The probability density of distributions is not to the scale on y-axis, I scaled them down so everything would fit in one plot. 

[^y3w09m11yo]: The formula for Bayes theorem is as follows:\(P(A|B) = \dfrac{P(B|A)P(A)}{P(B)}\)For explanation for what each of these terms means, refer to this post, or this post, or this video. I will assume you understand it. 

[^f5l2lo1xfxr]: Where \(\mathbb{1}_A(x)\) is an indicator function