## Homework

### Ice Cream Sales in Inferenceville

You have been hired to investigate a disturbing connection between ice cream sales and crime in Inferenceville. You are given a report that describes the joint distribution over random variable $S$, representing ice cream sales, and random variable $C$, representing crime. Each variable takes on a value of “low" or “high", which we'll represent with $0$ and $1$ respectively. The joint distribution (estimated from data) is as follows:



(a) Are random variables $S$ and $C$ independent?


[$\times    $] Yes <br>
[$\checkmark$] No


```python
def is_independent(p_X_Y):
    """
    Returns true if the given join prbability distribution is independet.
    
    >>> import numpy as np
    >>> p_X_Y = np.array([[0.4, 0.1], [0.25, 0.25]])
    >>> is_independent(p_X_Y)
    False
    
    >>> p_X_Y = np.array([[0.72, 0.08], [0.18, 0.02]])
    >>> is_independent(p_X_Y)
    True
    """

    import numpy as np
    p_X = p_X_Y.sum(axis=1)
    p_Y = p_X_Y.sum(axis=0)
    Δ = np.outer(p_X, p_Y) - p_X_Y
    return (np.linalg.norm(Δ, np.inf) - 0.00001) < 0

if __name__ == "__main__":
    import doctest
    doctest.testmod()
```


```python
from sympy import init_printing
init_printing()
```


```python
import numpy as np
p_S_C = np.array([[0.4, 0.1], [0.25, 0.25]])
is_independent(p_S_C)
```




    False



(b) After further investigation, you discover information about the temperature, represented by $T$. This random variable also takes on values $0$ or $1$ corresponding again to “low" and “high". You are able to obtain the conditional distribution $p_{S,C \mid T}(s,c \mid t)$, shown below.



Are random variables $S$ and $C$ conditionally independent given $T$?


[$\checkmark$] Yes <br>
[$\times    $] No


```python
import numpy as np
p_T_1 = np.array([[0.72, 0.08], [0.18, 0.02]])
p_T_2 = np.array([[0.08, 0.12], [0.32, 0.48]])
is_independent(p_T_1) and is_independent(p_T_2)
```




    True



(c) Determine the distribution $p_T$ from the tables above. Express your answer as a Python dictionary. The keys should be the Python integers $0$ and $1$.

Assuming $p_T(0) = p$ then $p_T(1) = (1-p)$. Now by the law of total probability, 

$$\begin{align}
p_{S,C}(0,0) &= p_{S,C\mid T}(0,0 | 0)p_T(0) + p_{S,C\mid T}(0,0 | 1)p_T(1) \\
         0.4 &= 0.72p + 0.08(1-p)\\ 
\end{align}$$


```python
from sympy.solvers import solve
from sympy import Symbol
p = Symbol('p')
p = solve(0.08*(1-p) + 0.72*p - 0.4, p)[0]; p
```


```python
p_T = {0: p, 1: 1-p}; p_T
```


```python

```

9c6fbea1bcef8eaa6605c857fb4f6ea610bd2393
ipynb
Jupyter Notebook
week03/04 Homework.ipynb
infimath/Computational-Probability-and-Inference
e48cd52c45ffd9458383ba0f77468d31f781dc77
MIT
2019-04-04T03:07:47.000Z
2019-04-04T03:07:47.000Z
week03/04 Homework.ipynb
infimath/Computational-Probability-and-Inference
e48cd52c45ffd9458383ba0f77468d31f781dc77
MIT
week03/04 Homework.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