# When Should the Fire Alarm Go Off: A model for optimal thresholds

In this post I consider a model where there is a possible bad outcome, and the chance of it happening is related to something we can measure. We need to be able to set a threshold for these measurements, such that if a measurement exceeds a threshold then we decide to act; to prepare for or treat the bad outcome. This seems like a pretty basic idea, so I expect that either most results will be trivial, or that someone has already done this before.

A basic example of this would be a disease, where if a patient reaches a threshold for the number and severity of symptoms then the doctor decides to prescribe a medicine. Or the current pandemic, where in February 2020 if governments had a more accurate view of both the chance and severity of a global pandemic they may have decided to put in place seemingly drastic measures to stop worse outcomes in the future.  
This should also apply to important risks in the future, where it is important to know the probabilities and magnitudes so that we can adequately prepare. We need to know where to sensibly set the threshold which tells us when to act.

A helpful metaphor I will use here is a building with a sprinkler system. We want the sprinklers to turn on when there is a fire (there is a huge cost if the building burns down), but also we don't want the sprinklers constantly going off whenever there's dust in the air because this also has a cost associated with it.

When deciding whether something is going to happen, and whether to act there are 4 options:

*   True Positive (TP), where the event happens but luckily we decided to act
    *   The building is on fire, but the sprinklers turned on and saved everything
*   True Negative (TN), where nothing happens and we didn't do anything
    *   No fire, no sprinklers
*   False Positive (FP), where nothing happens and we act unnecessarily
    *   The building is not on fire, but the sprinklers turned on anyway
*   False Negative (FN), where the event happens and we didn't act
    *   The building is on fire, but the sprinklers didn't turn on and the building burned down

The chance of each of these options will depend on where we set our threshold. If the threshold is very low then we will have a lot of True Positives, but will also have to incur the costs of more False Positives. If the threshold is very high then we will have less False Positives, but will have more False Negatives. 

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

Mathematical Model
------------------

We can decide where to put the threshold by calculating the expected value ($EV$) at a given threshold.

$EV = p(TP)\cdot V(TP) +p(TN)\cdot V(TN)+p(FP)\cdot V(FP) + p(FN) \cdot V(FN)$

Where $p(\cdot)$ is the probability of each outcome, and $V(\cdot)$ is the return or value of each outcome.

We can express the value of each outcome quite simply, using 2 positive constants:  
- $D$ is the cost we have to pay if we don't treat the disease or avert the disaster  
 \- The cost of the building burning down  
- $T$ is the cost of the treatment or the preparations  
 \- The cost of the sprinkler system going off

For the True Positive case we are in the world where the disaster would have happened but we successfully stop it in exchange for cost $T$, so rather than paying $D$ we pay $T$  instead, so $V(TP)=-T$.

For the True Negative case, we do nothing and nothing happens, so very simply $V(TN)=0$.

For the False Positive case, we spend $T$ and nothing happens so $V(FP)=-T$.

For the False Negative case, the disaster happens and costs us $D$, so $V(FN)=-D$.

And hence our expected value is

$EV = p(TP)(-T) +p(FP)(-T) + p(FN) (-D)$

The expected value depends on the probabilities which depend on where we set our threshold $t$,

$p(TP)=P_P\int_t^\infty \psi_P(\theta)d\theta$

$p(FP)=P_N\int_t^\infty \psi_N(\theta)d\theta$

$p(FN)=P_P\int_{-\infty}^t \psi_P(\theta)d\theta$

Where $\psi_P(\theta)$ and $\psi_n(\theta)$ are the distributions over the thing we're measuring, $\theta$. For example $\theta$ could be the concentration of particles in the air, as this increases there is a higher chance that the building is on fire. $\theta$ could also be an indicator of more complicated and abstract things; the reproductive number of a virus, or the number of warships in the South China Sea. $\psi_P(\theta)$ and $\psi_n(\theta)$ are both normalized to 1 here, and the actual background chance of a positive event is $P_P$, and hence the chance of a negative event (nothing happens) is $P_N=1-P_P$. $\psi_P(\theta)$ can be thought of as "given that the event happens, how likely is each each value of $\theta$".

If we want to be good Bayesians about it we could say $\psi_P(\theta)=p(\theta | \text{Event happens})$ and $\psi_N(\theta)=p(\theta | \text{Event doesn't happen})$.

Finding the Optimal Threshold
-----------------------------

We should choose the value of the threshold $t$ which maximizes the expected value. We can write the expected value as a function of $t$

$EV(t) = -T  P_P \int_t^\infty \psi_P(\theta)d\theta-T  P_N \int_t^\infty \psi_N(\theta)d\theta-D  P_P \int_{-\infty}^t \psi_P(\theta)d\theta$

We can find the maximum of this by taking the derivative with respect to $t$  
and setting it equal to 0.

$\frac{d}{dt}EV(t)= T  P_P  \psi_P(t) +T  P_N  \psi_N(t) - D P_P  \psi_P(t)=0$

Which rearranges to give us

$\frac{\psi_P(t)}{\psi_N(t)}=\frac{1-P_P}{P_P  (D/T-1)}$

The threshold $t$ will increase as the factor on the right hand side increases. Interestingly, for given distributions $\psi_P$ and $\psi_N$, the optimal threshold $t$ only depends on the probability of the event $P_P$, and the ratio between the costs of the event and the treatment $D/T$. If we express the expected value in units of the treatment cost (divide by $T$), we can see that it also only depends on these variables:

$EV(t) = - P_P \int_t^\infty \psi_P(\theta)d\theta- (1-P_P) \int_t^\infty \psi_N(\theta)d\theta-D/T  P_P \int_{-\infty}^t \psi_P(\theta)d\theta$

Perhaps obviously, this model tells us the ways in which we can be acting rationally (maximizing expected utility), but choose a suboptimal threshold because we are misinformed:  
\- We have an incorrect assessment of the relative cost of the disaster versus the treatment $D/T$  
\- We can have an incorrect assessment of the background probability of an event happening $P_P$  
\- Our probability distributions $\psi_P$ and $\psi_N$ can be wrong; for example, the variance of smoke concentration when there is a fire could be larger than we thought.

Toy Example and Results
-----------------------

For the a very simple example, we can take the distributions $\psi_P$ and $\psi_N$ to be normal distributions with standard deviations of 1,

$\psi_P(t)=N_{\mu_P}(t)=\frac{1}{\sqrt{2\pi}}\exp (-\tfrac{1}{2}(t-\mu_P)^2)$

$\psi_N(t)=N_{\mu_N}(t)=\frac{1}{\sqrt{2\pi}}\exp (-\tfrac{1}{2}(t-\mu_N)^2)$

Using these we can solve for the threshold value $t$ which maximizes expected utility

$t = \frac{1}{\mu_P-\mu_N}\ln\left( \frac{1-P_P}{P_P  (D/T-1)}\right)+\frac{1}{2} \frac{\mu_P^2-\mu_N^2}{\mu_P-\mu_N}$

### Expected Value for Different Threshold Values

We can plot the expected value as a function of the threshold, to see how it changes. If the threshold is set too low, then we lose utility due to acting unnecessarily, while if it is too high then we lose utility because disasters happen which we could have prevented. The optimal $t$ calculated above is at the 'sweet spot' as we can see here:

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

The top graph is the expected value as a function of the threshold $t$, with the optimal threshold marked by the green line. Here we have $D/T=400$ and $P_P=1\%$, the distributions $\psi_P$ and $\psi_N$ are plotted below, and we can see that the optimal threshold cuts off almost all of the $\psi_P$ distribution.

We can also look at how things change as our distributions change, specifically as the overlap between them decreases. 

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

Expected value as a function of the threshold $t$ (top row) for pairs of distributions $\psi_P$ and $\psi_N$ (bottom row). The parameters are $D/T=500$ and $P_P=1\%$. The green line shows the optimal threshold, and the red lines show the threshold we would choose if we over or underestimated $P_P$ by a factor of 20.

When there is a lot of overlap - when it is difficult to discriminate between the positive and negative situations - there isn't much of a peak in the expected value around the 'sweet spot'. This is because we can't really tell between the two outcomes, and and so being very risk averse (low threshold) is a good plan. In this regime, if underestimating the probability of a the bad event can have very bad outcomes, while overestimating doesn't really effect things.  
  
As the distributions get further apart (we can discriminate more easily) we get a peak at the optimal threshold, and this peak gets wider as the distributions get further apart. This is because we can more easily place the threshold where it catches all the positive (bad) events while not reacting when we don't need to.

### Effect of Incorrectly Estimating the Likelihood of an Event

We can also more thoroughly investigate the consequences of under or overestimating the probability of an event. These consequences will vary depending on the regime we are in, specifically on the values of $D/T$ and $P_P$. It is useful to sweep through a wide range of these parameters. We investigate values of $D/T$ ranging from 1 to $10^5$. For the probability $P_P$ we can instead use the odds ratio $P_P/(1-P_P)$, which allows us to investigate very small probabilities (this is important because we should probably care about events which are very unlikely but have large effects). We explore $P_P/(1-P_P)$ over a range from $10^{-3}$ up to almost certainty. We calculate the difference in expected value if we choose the optimal threshold ($EV_{best}$), versus the threshold we would choose if we under/overestimated the odds by a factor of 20 ($EV_{under/overestimate}$).

$\Delta EV = EV_{best} - EV_{under/overestimate}$

$\Delta EV$ will be negative because we are choosing a suboptimal threshold. The lost utility will be due either from allowing bad events to happen without treatment, or from acting unnecessarily. 

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

The change in expected value if we under or over estimate the odds of a bad event by a factor of 20, over a range of values for the cost $D/T$ and the true odds $P_P/(1-P_P)$. For this example, $\psi_P$ and $\psi_N$ are normal distributions with standard deviations of 1, and means of 1 and -1 respectively.

We can clearly see that (for these parameters) the consequences of  underestimating the likelihood of a bad event can be much worse than the consequences of overestimating. The consequences are especially severe when the bad event is very unlikely but is very bad if it does happen.  

Feel free to examine this model, play around with parameters, or find mistakes here:  
[https://colab.research.google.com/github/peterbarnettnz/risk\_neglect/blob/main/Risk\_false\_positive\_negative.ipynb](https://colab.research.google.com/github/peterbarnettnz/risk_neglect/blob/main/Risk_false_positive_negative.ipynb)

Thanks to Gurkenglas for pointing out some bad reasoning about the expected value math.