## Medical Diagnosis with Costs

In this exercise, we continue off our running medical diagnosis example, now looking at a decision variant of it where we need to decide whether to treat a patient, and there are associated costs.

We will be using both conditional expectation and the expectation of the function of a random variable. In fact, we need to combine the two! For random variables $X$ and $Y$ for which we know (or have computed) the conditional distribution $p_{X\mid Y}(\cdot \mid y)$, and for any function f such that $f(x)$ is a real number for every $x$ in the alphabet of $X$, then

$$\mathbb {E}[f(X)\mid Y=y] = \sum _ x f(x) p_{X\mid Y}(x\mid y).$$
 
(See if you can figure out why this is true from our earlier coverage of expectation!)

We previously computed the probability that someone has a infection given that a test for the infection turned out positive. But how do we translate this into a decision of whether or not we should treat the person? Ideally, we should always choose to treat people who, in truth, have the infection and choose “don't treat" for people without the infection. However, it is possible that we may treat actually healthy people, a false positive that leads to unnecessary medical costs. As well, we may fail to treat people with the infection, a false negative that could require more expensive treatment later on.

We can encode these costs into the following “cost table" (or cost function) $C(\cdot , \cdot )$, where $C(x,d)$ is the cost when the patient's true infection state is $x$ (“healthy" or “infected"), and we make decision $d$ (“treat" or “don't treat"). For our problem, here is the cost function:

| Truth $x$ | Decision $d$ | $C(x,d)$ |
|-----------|--------------|---------:|
| healthy   | don't treat  |      \$0 |
| healthy   | treat        |  \$20000 |
| infected  | don't treat  |  \$50000 |
| infected  | treat        |  \$20000 |

This cost table corresponds to a situation where the treatment costs \$20000, regardless of whether the patient's true state is healthy or infected. If the “don't treat" choice is made, then there are two potential costs: \$0 if the patient is indeed healthy, or \$50000 if the patient actually has the infection, perhaps corresponding to a more expensive future medical procedure needed if the infection is not treated now.

Recall the medical diagnosis setup from before, summarized in these tables:

| $X$      | Probability | $p_{Y\mid X}$ | healthy | infected |
|----------|-------------|---------------|---------|----------|
| healthy  | $0.999$     | positive      | $0.01$  | $0.99$   |
| infected | $0.001$     | negative      | $0.99$  | $0.01$   |

Let's suppose we've observed $Y=\text {positive}$. To use the cost table to help us make a decision, note that there are two possible realizations of the world given that $Y=\text {positive}$:

- First, it could be that $X=\text{infected}$ given $Y=\text {positive}$. This realization has probability $p_{X\mid Y}(\text {infected}\mid \text {positive})$. Under this realization of the world, by looking at the last two rows in the above cost table:

| Decision $d$|    Cost |
|-------------|--------:|
| treat       | \$20000 |
| don't treat | \$50000 |

- Second, it could be that $X=\text {healthy}$ given $Y=\text {positive}$. This realization has probability $p_{X\mid Y}(\text {healthy}\mid \text {positive})=1-p_{X\mid Y}(\text {infected}\mid \text {positive})$. Under this realization of the world, by looking at the two first rows in the cost table:

| Decision $d$ |    Cost |
|--------------|--------:|
| treat        | \$20000 |
| don't treat  |     \$0 |

Thus, putting together the pieces:

If we decide $d=\text{don't treat}$, then the cost is a random variable and is given by the probability table $p_{C(X, \text {don't treat}) \mid Y}(\cdot \mid \text {positive})$ below:

| Probability                              |    Cost |
|------------------------------------------|--------:|
| $p_{X\mid Y}$(infected $\mid$ positive)  | \$50000 |
| $p_{X\mid Y}$(healthy $\mid$ positive)   |     \$0 |

**Question:** What is $\mathbb {E}[C(X, \text {don't treat}) \mid Y = \text {positive}]$?

Express your answer as a function of $a \triangleq p_{X\mid Y}(\text {infected}\mid \text {positive})$ (unless a does not appear in the answer). Do not plug in the numerical value for a that you determined in a previous exercise, and also do not put a dollar sign in your answer.

**Answer:** The conditional expectection will be 

$$\begin{align} 
\mathbb {E}[C(X, \text {don't treat}) \mid Y = \text {positive}] 
=& C(\text{healthy, don't treat})  \times p_{X\mid Y}(\text {healthy}\mid \text {positive}) \\
 & + C(\text{infected, don't treat}) \times p_{X\mid Y}(\text {infected}\mid \text {positive})\\
=&0\times (1-a) + 50000\times a \\
=&50000 a
\end{align}$$

If we decide $d=\text {treat}$, then the cost is a random variable and is given by the probability table $p_{C(X, \text {treat}) \mid Y}(\cdot \mid \text {positive})$.

**Question:** What is $\mathbb {E}[C(X, \text {treat}) \mid Y = \text {positive}]$? (Hint: Figure out what the probability table $p_{C(X, \text {treat}) \mid Y}(\cdot \mid \text {positive})$ is first. See if you can figure out how we obtained the probability table $p_{C(X, \text {don't treat}) \mid Y}(\cdot \mid \text {positive})$ in the previous part.)

Express your answer as a function of $a \triangleq p_{X\mid Y}(\text {infected}\mid \text {positive})$ (unless a does not appear in the answer). Do not plug in the numerical value for a that you determined in a previous exercise. Express your answer as a function of $a$, where $a \triangleq p_{X\mid Y}(\text {infected}\mid \text {positive})$.

**Answer:** If we treat we have to pay \$20000 in both cases. Hence 20000 is the expectation. 

Given that $Y=\text {positive}$, to choose between which of the two decisions $d=\text {treat}$ and $d=\text {don't treat}$ to make, we can choose whichever decision $d$ that has the lower expected cost $\mathbb {E}[C(X,d)\mid Y=\text {positive}]$ (note that it could be that the expected costs are the same, in which case we could just break the tie arbitrarily).

Look at your answers to the previous two parts. You should notice that “don't treat" has the lowest possible cost when a is at most some threshold $t$, which you will now determine.

Phrased in terms of an equation, with variable a defined as before $a \triangleq p_{X\mid Y}(\text {infected}\mid \text {positive})$, there is a threshold $t$ for which

$$\begin{eqnarray}
\arg\min_{d \in \{\text{treat}, \text{don't treat}\}}
  \mathbb{E}[C(X, d) \mid Y = \text{positive}]
&=&
\begin{cases}
\text{don't treat} & \text{if } a \le t, \\
\text{treat} & \text{if } a > t,
\end{cases}
\end{eqnarray}$$

where we are breaking the tie in favor of not treating when $a=t$.

Determine what the threshold $t$ is. (Please provide an exact answer.)

$t$= {{t}} 


```python
t=20000/50000;t
```




    0.4



Let's look at what happens if instead we had observed $y = \text {negative}$.

Determine what variable $b$ and threshold $s$ should be in the below equation:

$$\begin{eqnarray}
\arg\min_{d \in \{\text{treat}, \text{don't treat}\}}
  \mathbb{E}[C(X, d) \mid Y = \text{negative}]
&=&
\begin{cases}
\text{don't treat} & \text{if } b \le s, \\
\text{treat} & \text{if } b > s.
\end{cases}
\end{eqnarray}$$

$b=$ ?

$[\times    ]$ pX∣Y(healthy∣positive) <br>
$[\times    ]$ pX∣Y(infected∣positive)<br>
$[\times    ]$ pX∣Y(healthy∣negative) <br>
$[\checkmark]$ pX∣Y(infected∣negative) 

- In case of $d=$ "treat", we have to spend \$20000 in both cases hence expectation will be 20000. 
- In case of $d=$ "don't treat", we have to spend \$0 when persion is healthy and \$50000 when the persion is infected. 
    <br> Hence $E = 50000b + 0(1-b) = 50000b$.
    
Threshold $s$ = {{s}}


```python
s = 20000/50000
```

$s$= {{s}}

Putting together the pieces, the decision rule that achieves the lowest cost can be written as:

$$\begin{eqnarray}
\widehat{x}(y)
&=&\begin{cases}
\text{don't treat} & y=\text{positive}\text{ and }a \le 0.4,\\
\text{treat} & y=\text{positive}\text{ and }a > 0.4,\\
\text{don't treat} & y=\text{negative}\text{ and }b \le 0.4,\\
\text{treat} & y=\text{negative}\text{ and }b > 0.4,
\end{cases}
\end{eqnarray}$$

where $a$ and $b$ are from the previous parts.

Now finally consider when $a=0.09016$ and $b=0.00001$ (these correspond to values from the previous exercises on this medical diagnosis problem setup). What is the optimal treatment plan given the cost table that we have introduced?

If we observe that $Y=\text{positive}$, then the decision with lowest expected cost is:

[$\times    $] treat<br>
[$\checkmark$] don't treat

If we observe that $Y=\text{negative}$, then the decision with lowest expected cost is:

[$\times    $] treat<br>
[$\checkmark$] don't treat

Of course, if $a$ and $b$ had been different, then the decisions we would make to minimize expected cost could change! In fact, this problem shows just how good the medical test needs to be designed before we can even justify treating the patient. A bad test would be one where the optimal decision for minimizing expected cost does not use the result of the test at all — we might as well not do the test!

## Expectations of Multiple Random Variables

How does expected value work when we have many random variables? In this exercise, we look at some specific cases that draw out some important properties involving expected values.

Let's look at when there are two random variables $X$ and $Y$ with alphabets $\mathcal{X}$ and $\mathcal{Y}$ respectively. Then how expectation is defined for multiple random variables is as follows: For any function $f:\mathcal{X}×\mathcal{Y}→R$,

$$\mathbb {E}[f(X, Y)] = \sum _{x \in \mathcal{X}, y\in \mathcal{Y}} f(x, y) p_{X,Y}(x, y).$$
 
(Note that “$\sum _{x \in \mathcal{X}, y\in \mathcal{Y}}$" can also be written as “$\sum _{x \in \mathcal{X}}\sum _{y \in \mathcal{Y}}$".)

For example:

$$\mathbb {E}[X + Y] = \sum _{x \in \mathcal{X}, y\in \mathcal{Y}} (x + y) p_{X,Y}(x, y).$$
 
Let's look at some properties of the expected value of the sum of multiple random variables.

First, as a warm-up, show that:

$$\mathbb {E}[X + Y] = \mathbb {E}[X] + \mathbb {E}[Y].$$
 
This equality is called linearity of expectation and it holds regardless of whether $X$ and $Y$ are independent.

We previously saw that a binomial random variable with parameters $n$ and $p$ can be thought of as how many heads there are in $n$ tosses where the probability of heads is $p$.

A different way to view a binomial random variable is that it is the sum of $n$ i.i.d. Bernoulli random variables each of parameter $p$. As a reminder, a Bernoulli random variable is $1$ with probability $p$ and $0$ otherwise. Suppose that $X_1,X_2,\ldots,X_n$ are i.i.d. $\text{Bernoulli}(p)$ random variables, and $S=\sum_{i=1}^nX_i$.

**Question:** What is $\mathbb{E}[X_i]?$

**Answer:** $\mathbb{E}[X_i] = 1p + 0(1-p)= p$

**Question:** What is $\mathbb{E}[S]?$

**Answer:** $\mathbb{E}[S] = \sum_{i=1}^n\mathbb{E}[X_i]=np$

A widely useful random variable is the indicator random variable $1(⋅)$, defined as

$$\begin{eqnarray}
\mathbb{1}(\mathcal{S})
&=&
\begin{cases}
1 & \text{if }\mathcal{S}\text{ happens}, \\
0 & \text{otherwise}
\end{cases}
\end{eqnarray}$$

where $S$ is an event.

**Question:** Can a Bernoulli random variable corresponding to a coin flip be written as an indicator random variable?

**Answer:** Yes, we can take $\mathcal{S} = \{\text{'H'}\}$

**Question:** Suppose a monkey types 1,000,000,000 characters using the 26 letters of the alphabet. The monkey selects each character independently and is equally likely to choose any of the 26 letters. What is the expected number of occurrences of the word “caesar"?

Hint: First write up the number of occurrences of the word “caesar" as the sum of indicator random variables.

**Solution:** Let $Z_i$ be the indicator random variable that the word “caesar" starts at position $i$. Note that

$$\begin{align} \mathbb {E}[Z_ i]	=& \mathbb {P}(\mbox{the word ``caesar'' starts at position }i)	\\ 	 
(\text {by independence})=& \mathbb {P}(i\mbox{-th letter is ``c''})\mathbb {P}((i+1)\mbox{-th letter is ``a''})	\\
& \quad \cdots \mathbb {P}((i+5)\mbox{ -th letter is ``r''})	\\ 	 
=& \underbrace{\frac{1}{26}\times \frac{1}{26}\times \cdots \times \frac{1}{26}}_{6\text { times}} \\ 	 
=& \frac{1}{(26)^{6}}.\end{align}$$

Then the number of occurrences of “caesar" is given by $Y=\sum _{i=1}^{1,000,000,000-5}Z_ i$, so

$$\mathbb {E}[Y] = \mathbb {E}[\textstyle \sum _{i=1}^{999,999,995}Z_ i] = \textstyle \sum _{i=1}^{999,999,995}\mathbb {E}[Z_ i] = \boxed {999,999,995\times \frac{1}{(26)^{6}}}.$$
 
Note that in the second equality we used the fact that expectation is linear, even though $X_i$'s are dependent. This is because the linearity of expectation doesn't require independence.

**Question:** Consider a standard 52-card deck that has been shuffled. What is the expected number of times for which a king in the deck is followed by another king? For example, if all four kings are next to each other, then we'd say that there are 3 kings that are followed by another king.

**Solution:** Let $i$ be an index between two cards in the deck, i.e., $i=1$ refers to the index between cards $1$ and $2$. Thus, the range of possible $i$ is $i=1,2,\cdots ,51$. Define an indicator random variable $X_ i$ that is $1$ when the cards at index $i$ and $i+1$ are both kings. Note that

$$\mathbb {E}[X_ i]=\mathbb {P}(i\text {-th card is a king},(i+1)\mbox{-th card is a king})=\frac{4}{52}\cdot \frac{3}{51}.$$
 
Then the number of kings followed by another king is $Y=\sum _{i=1}^{51}X_ i$, so 
$$\mathbb {E}[Y]=51\cdot \frac{4}{52}\cdot \frac{3}{51} =\frac{12}{52}=\boxed {\frac{3}{13}}.$$

**Question:** If $X$ and $Y$ are independent, is it true that $\mathbb {E}[XY] = \mathbb {E}[X]\mathbb {E}[Y]$?

**Solution:** Yes,

$$\begin{align}
\mathbb {E}[XY]	=& \sum _{x}\sum _{y}xyp_{X,Y}(x,y)	\\ 	 
(\text {by independence})	=& \sum _{x}\sum _{y}xyp_{X}(x)p_{Y}(y)	\\ 	 
=& \sum _{x}\sum _{y}(xp_{x}(x))(yp_{Y}(y))	 \\	 
=& \Big(\sum _{x}xp_{x}(x)\Big)\Big(\sum _{y}(yp_{Y}(y))\Big)	\\ 	 
=& \mathbb {E}[X]\mathbb {E}[Y].	 
\end{align}$$

Finally, let's look at variance again. As a reminder

$$\text {var}(X) = \mathbb {E}[(X-\mathbb {E}[X])^2].$$

**Question:** If $X$ and $Y$ are independent, is it true that $\text {var}(X + Y) = \text {var}(X) + \text {var}(Y)$?

**Solution:** Yes.

To show this. We'll be using linearity of expectation repeatedly. To keep the notation here from getting cluttered with the squaring, let $\mu _{X}=\mathbb {E}[X]$ and $\mu _{Y}=\mathbb {E}[Y]$. First off, note that

$$\begin{align}\displaystyle \text {var}(X) =& \mathbb {E}[(X-\mu _{X})^{2}]\\	 
=& \mathbb {E}[X^{2}-2\mu _{X}X+\mu _{X}^{2}]\\	 	 
=&\mathbb {E}[X^{2}]-2\mu _{X}\underbrace{\mathbb {E}[X]}_{\mu _{X}}+\mu _{X}^{2}\\	 	 
=&\mathbb {E}[X^{2}]-\mu _{X}^{2}.\\
\end{align}$$

Similarly,

$$\text {var}(Y)=\mathbb {E}[Y^{2}]-\mu _{Y}^{2},$$
 
and

$$\text {var}(X+Y) =\mathbb {E}[(X+Y)^{2}]-(\mu _{X}+\mu _{Y})^{2}.$$
 
Let's simplify the right-hand side above:

$$\begin{align}\text {var}(X+Y)	=& \mathbb {E}[(X+Y)^{2}]-(\mu _{X}+\mu _{Y})^{2}\\	 	 
=& \mathbb {E}[X^{2}+2XY+Y^{2}]-\mu _{X}^{2}-2\mu _{X}\mu _{Y}-\mu _{Y}^{2}	\\ 	 
=& \mathbb {E}[X^{2}]+2\underbrace{\mathbb {E}[XY]}_{=\mu _{X}\mu _{Y}\text { by indep.}}+\mathbb {E}[Y^{2}]-\mu _{X}^{2}-2\mu _{X}\mu _{Y}-\mu _{Y}^{2}\\	 	 
=&\displaystyle \underbrace{\mathbb {E}[X^{2}]-\mu _{X}^{2}}_{\text {var}(X)}+\underbrace{\mathbb {E}[Y^{2}]-\mu _{Y}^{2}}_{\text {var}(Y)}.	 
\end{align}$$

**Question:** What is the variance of a $\text {Ber}(p)$ random variable?

**Solution:** For $X_ i \sim \text {Ber}(p)$, we have

$$\begin{align}\mathbb {E}[(X_ i-\underbrace{\mathbb {E}[X_ i]}_{p})^{2}]=&(1-p)^{2}\cdot p+(0-p)^{2}\cdot (1-p)	 =& (1-2p+p^{2})p+p^{2}(1-p)\\	 	 
=&p-2p^{2}+p^{3}+p^{2}-p^{3}\\	 	 
=&p-p^{2}\\	 	 
=&\boxed {p(1-p)}.\end{align}$$

**Question:** Recall that a binomial random variable $S$ that has a $\text {Binomial}(n, p)$ distribution can be written as $S = \sum _{i=1}^ n X_ i$ where the $X_ i$'s are i.i.d. $\text {Ber}(p)$ random variables. What is the variance of $S$?

**Solution:** Since the $X_ i$'s are independent,

$$\text {var}(S) =\text {var}\Big(\sum _{i=1}^ n X_ i\Big) =\sum _{i=1}^ n \text {var}(X_ i) =\boxed {n p (1-p)}.$$


```python

```

8da8e3183b70108ff5c4f779acb08f77393c49d1
ipynb
Jupyter Notebook
week04/02 Exercise on Decision Making.ipynb
infimath/Computational-Probability-and-Inference
e48cd52c45ffd9458383ba0f77468d31f781dc77
MIT
2019-04-04T03:07:47.000Z
2019-04-04T03:07:47.000Z
week04/02 Exercise on Decision Making.ipynb
infimath/Computational-Probability-and-Inference
e48cd52c45ffd9458383ba0f77468d31f781dc77
MIT
week04/02 Exercise on Decision Making.ipynb
infimath/Computational-Probability-and-Inference
e48cd52c45ffd9458383ba0f77468d31f781dc77
MIT
2021-02-27T05:33:49.000Z
2021-02-27T05:33:49.000Z
Qwen/Qwen-72B
1. YES
2. YES