# More on Simulating Conditional Probabilities

Simulation is a key technique used in this book to explore random phenomena. Thus, it is important to understand the simulation techniques we use for different scenarios. Consider again the Magician's Coin problem, and let $H_i$ denote the event that the coin comes up heads on the $i$th flip. In {doc}`simulating-cond-probs`, we investigated $P(H_2|H_1)$, the probability of getting Heads on a second flip of the chosen coin given that heads was observed on the first flip.  If we apply the definition of conditional probability from {doc}`definition`, then we can calculate this probability as 


<!--
\begin{equation}
P(H_2|H_1) = \frac{P(H_1 \cap H_2)}{P(H_1)}.
\label{eq:p-h2-given-h1}
\end{equation}
-->
$$
P(H_2|H_1) = \frac{P(H_1 \cap H_2)}{P(H_1)}.
$$ (eq:p-h2-given-h1)

However, in {doc}`simulating-cond-probs`, we did not estimate either $P(H_1 \cap H_2)$ or $P(H_1)$.  Instead, we counted the proportion of times that we observed the outcome $H_2$ when $H_1$ was the outcome of the first flip.  Let's see why these produce the same result.

Let's see how we would estimate the probabilities on the right-hand side of {eq}`eq:p-h2-given-h1`. We will use the following notation for the counters we have in the simulation:
* $N$ is the total number of simulation trials,
* $N_1$ is the number of simulation trials in which the outcome of flip 1 was Heads (i.e., the number of times $H_1$ occurred), and
* $N_{12}$ is the number of simulation trials in which the outcomes of both flip 1 and flip 2 were Heads (i.e., the number of times $H_1 \cap H_2$ was observed.  

Now, we can estimate the probability of $H_1$ using its relative frequency,

$$
P(H_1) = \frac{N_1}{N}.
$$

Similarly, we can estimate the probability of $H_1 \cap H_2$ using its relative frequency,

$$ 
P(H_1 \cap H_2) =\frac{N_{12}}{N}.
$$

If we substitute these into {eq}`eq:p-h2-given-h1`, we get

$$
P(H_2|H_1) = \frac{ \frac{N_{12}}{N} }{ \frac{N_1}{N} }= \frac{ N_{12} }{ N_1 }.
$$

I.e., the result is the same as the approach taken in {doc}`simulating-cond-probs`. In general, we can directly estimate the conditional probability of an event $B$ given an event $A$ using nested if statements to:
* Count the number of trials in which $A$ occurred. Call this number $N_A$
* Within those trials in which  $A$ occurred, count the number of trials in which $B$ also occurred. Call this number $N_{AB}$.
* Estimate the conditional probability of $B$ occurring given that $A$ occurred as 

$$
P(B|A) \approx \frac{N_{AB}}{N_A}
$$

```{warning}


Note that when designing simulation to estimate conditional probabilities, you must be especially careful in selecting the number of trials to be simulated. 

In simulations of single events, if we think we know the approximate probability of the event, we can use that probability to estimate the number of trials needed to estimate it. For example, if we are estimating a probability $P(A)$ that we believe is close to 0.1, then we can estimate that the event $A$ occurs approximately once in every $1/P_A=10$ trials. If we capture 100 instances of $A$ occurring, we will get a good estimate of $P(A)$, so we might use this information to simulate $100/P_A=1000$ trials.

This approach will not work to estimate a conditional probability!

<span style="color:red;font-weight:bold">It is not sufficient to use only a conditional probability to estimate the number of trials needed.</span>

Let's use an example to show why this is the case. Suppose $A$ and  $B$ are events for which we wish to estimate the conditional probability $P(B|A)$. Consider the scenario where $P(A)=10^{-3}$ and $P(A \cap B= 9 \times 10^{-4}$. Then 

$$
P(B|A) = \frac{P(A \cap B)}{P(A)} = \frac{9 \times 10^{-4}}{10^{-3}} = 0.9.
$$

If we used only the knowledge of $P(B|A)$, then we might use $100/P(B|A) \approx 111$ trials. But remember that in the simulation, we estimate the conditional probability as

$$
P(B|A) \approx \frac{N_{AB}}{N_A}.
$$
We can estimate the values of these counters as follows. (We will formalize the meaning of these estimates later in the book when we talk about expected values/ensemble means.) $N_A$ will be approximately equal to the number of trials times $P(A)$, so it will be approximately equal to $111 P(A)= 0.11$. In other words, in 111 trials, most of the time we will not see any event $A$ occur. The estimated value of counter $N_{AB}$ will be the estimated number of trials in which we see both $A$ and $B$ occur, which will be approximately equal to $111 P(A \cap B) \approx 0.1$. **If we only use  the conditional probability information, we may severely underestimate the number of trials required to observe a sufficient number of events.**

$\Rightarrow$ When determining a sufficient number of trials for a simulation of a conditional probability $P(B|A)$, we must determine the required number of trials from the (estimated) value of $P(A \cap B)$, if known. If no information about $P(A \cap B)$ is available, the value of $N_{AB}$ should be checked at the end of the simulation to make sure that a sufficient number of events (at least 10; 100 is better) were observed.

Because of this effect, some people will code their simulation to run until $N_{AB}$ reaches a specific threshold. This will bias the estimate of the $P(B|A)$ for reasons that are beyond the scope of this book. However, the bias will be small if threshold is large (>100).






```


```python

```

675906bd58b9651a54f9d1f7158a61eb908d390c
ipynb
Jupyter Notebook
06-conditional-prob/more-on-simulating.ipynb
jmshea/intro-data-science-for-engineers
7bfcd7125d9bac42054bbfc5ad67c1ddcd7dfd8b
FSFAP
2021-09-09T14:12:23.000Z
2021-09-09T14:12:23.000Z
06-conditional-prob/more-on-simulating.ipynb
jmshea/intro-data-science-for-engineers
7bfcd7125d9bac42054bbfc5ad67c1ddcd7dfd8b
FSFAP
06-conditional-prob/more-on-simulating.ipynb
jmshea/intro-data-science-for-engineers
7bfcd7125d9bac42054bbfc5ad67c1ddcd7dfd8b
FSFAP
Qwen/Qwen-72B
1. YES
2. YES