# Simulate an arbitrary indictive dipole using qucat

## 1 Generating the circuit

This new features of qucat allows to insert an arbitrary purely inductive dipole, simplified at an arbitrary order. Only the labels must be provided.

Here's what it looks like when constructing a circuit with the GUI :

Using the general feature "Dipole" is only possible without specifying any energy : Only labels must be provided, so the values must be given when performing a specific calculation.

Once printed, it will look like this :

The diode-like form shows the orientation of the dipole.

The number of labels the dipole has corresponds to the order at which it is simulated : Here there are 3 labels, so it will take into account the fourth first orders, as qucat usually does :

$$U_{dipole}(\phi) = h(\frac{E2}{2!}\phi^2 + \frac{E3}{3!}\phi^3 + \frac{E4}{4!}\phi^4)$$

In this expression, the $Ei$ are provided in Hertz.

If this dipole is a Josephson element, we have, forgetting the constant term and expanding at order fourth :

$$U_{J}(\phi) = E_J(1 - \cos\phi) = \frac{E_J}{2}\phi^2 - \frac{E_J}{4!}\phi^4$$

So in this case : $E2 = \frac{E_J}{h}, E3 = 0, E4 = -\frac{E_J}{h}$.

Notes :
- The dipoles are polarized, since odd orders are non zero in general. The orientation matters.
- One cannot set an element to strictly zero to prevent qucat errors. Provide very negligible values.

## 2 Hamiltonian

Once the hamiltonian decomposed into eigenmodes, the hamiltonian of the circuit writes :

$$ H = \sum_m{\hbar \omega_m \hat{a}^\dagger_m \hat{a}_m} + \sum_j \sum_{n\geq 3}\frac{\partial^{n} U_j}{\partial \phi^{n}}|_0\hat{\phi}_j^n$$

with :
- $\hat{\phi}_j$ being the phase across the j-th non linear element
- $m$ the modes of the circuit
- $\omega_m/2\pi$ the frequency of the $m$-th mode
- $\hat{a}_m$ the annihilator of the $m$-th mode
- $U_j(\phi)$ the inductive energy of the j-th non linear dipole, assuming its minimum is for $\phi = 0$

Using $\hat{\phi}_j =  \sum_m \phi_{zpf, m, j}\hat{\phi}_m = \sum_m \phi_{zpf, m, j}(\hat{a}^\dagger_m + \hat{a}_m)$ :

$$ H = \sum_m{\hbar \omega_m \hat{a}^\dagger_m \hat{a}_m} + \sum_j \sum_{n\geq 3}\frac{\partial^{n} U_j}{\partial \phi^{n}}[\sum_m \phi_{zpf, m, j}(\hat{a}^\dagger_m + \hat{a}_m)]^n$$

At fourth order, following qucat formalism (applying rotating wave approximation) and adding the third order term:

$$ H = \hbar\sum_m \big[( \omega_m - \sum_{n}\frac{\chi_{mn}}{2})\hat{a}_m^\dagger\hat{a}_m
-\frac{A_m}{2} \hat{a}_m^\dagger \hat{a}_m^\dagger \hat{a}_m \hat{a}_m
-\sum_{n}\chi_{mn}\hat{a}_m^\dagger \hat{a}_m \hat{a}_n^\dagger \hat{a}_n\big]
+ \sum_{m, n, o}(\gamma_{mno}^* \hat{a}_m^\dagger \hat{a}_n^\dagger \hat{a}_o + \gamma_{mno} \hat{a}_m \hat{a}_n \hat{a}_o^\dagger)$$

The modified version of qucat returns also the mixing terms $\gamma_{mno}$. Note that only the terms with $m\neq n, n\neq o, m\neq o$ are likely to conserve the energy.

For further orders, I added a function returning the full hamiltonian as a sympy expression. The simplifications using the rotating wawe approximation for example can then be perfomed afterwards.

## Example : the SNAIL

The SNAIL is a dipole containing three little Josephson juntions on one side, and one big on the other side :

*SNAIL scheme taken from N. E. Frattini, V. V. Sivak, A. Lingenfelter, S. Shankar et M. H. Devoret : Optimizing the Nonlinearity and Dissipation of a SNAIL Parametric Amplifier for Dynamic Range. Physical Review Applied, 10(5):1–17, 2018.*

Its inductive energy writes :

$$U_{SNAIL}(\phi) = -E_J(\alpha \cos\phi + 3 \cos \frac{\phi_0-\phi}{3})$$

It depends on :
- the flux bias in its loop
- a characteristic Josphson energy E_J
- a characteristic coeeficient $\alpha$

Having $\phi_0$, $E_J$ and $\alpha$, one can find the minimum of this inductive energy and develop it around it : The developpement will give the coeficient to feed qucat with :

$$U_{SNAIL}|_{\alpha, E, phi_{ext}}(\phi) = \frac{E2}{2}(\phi-\phi_{min})^2 + \frac{E3}{6}(\phi-\phi_{min})^3 + \frac{E4}{24}(\phi-\phi_{min})^4$$

```python
from scipy.constants import *
import matplotlib.pyplot as plt
from scipy.optimize import *
import numpy as np
import sympy as sp
import qucat as qc

## Parameters for the circuit
order = 4 #order at which the SNAIL will be expanded
alpha = 0.12
phi_0 = h/(2*e) #the flux quantum
L = 1e-8 #the Jospehson inductance of the small junctions
E_J = 1/h*phi_0**2/(4*pi)**2/L #the Josephson energy of the small junctions, in Hz
C1 = 10e-14 #capacities of the circuit
C2 = 15e-14
C3 = 20e-14

#We use sympy to create the derivatives of the energy
phi = sp.Symbol("phi")
a = sp.Symbol("a")
E = sp.Symbol("E")
phi_ext = sp.Symbol("phi_ext")

U_sym = -E*(a*sp.cos(phi) + 3*sp.cos((phi_ext-phi)/3))
dnU_sym =[sp.diff(U_sym, phi, i) for i in range(order+1)]#Symbolic expression for the derivatives
dnU = [sp.lambdify([phi, E, a, phi_ext], c) for c in dnU_sym] #creates the python functions for the derivatives:
#dnU[i](E, a, phi_ext, phi)

def make_dict(E, a, phi_ext): #constructs the dictionnary to give as keyword arguments
    labels = {"C1":C1, "C2":C2, "C3":C3, "L":L}
    phi_min = (minimize(dnU[0], 11, method = 'L-BFGS-B' , bounds = [(-1, 7*np.pi)],args = (1, alpha, phi_ext)).x)[0]%(6*np.pi) #The point from which the potential will be expanded
    for i,c in enumerate(dnU[2::]):
        labels["E"+str(i+2)] = E*c(1, a, phi_ext, phi_min)
    return labels

def make_dict_vect(E, a, phi_ext): #same function but vectorized, to sweep phi_ext
    labels = {"C1":[C1 for c in phi_ext],
              "C2":[C2 for c in phi_ext],
              "C3":[C3 for c in phi_ext],
              "L":[L for c in phi_ext]}
    for i, c in enumerate(dnU[2::]): #initialisation of the lists
        labels["E"+str(i+2)] = []

    phi_min = [0]

    for c in phi_ext:
        phi_min.append((minimize(dnU[0], 11, method = 'L-BFGS-B' , bounds = [(-1, 7*np.pi)],args = (1, alpha, c)).x)[0]%(6*np.pi))
    phi_min.pop(0) #I struggled with the optimization, these parameters work for this example.

    for i,c in enumerate(phi_min):
        for j, d in enumerate(dnU[2::]):
            labels["E"+str(j+2)] = labels["E"+str(j+2)]+[d(c, E, a, phi_ext[i])]
    return labels
```

At this point, one can simply need to construct the circuit (the one given in 'Generating the circuit", with every element being given a label, not a value :

```python
circuit = qc.GUI("Circuit_example.txt")
```

Let's check the evolution of the derivatives at the minimum as a function of $\phi_{ext}$ :

```python
phi_exts = np.linspace(0,6*np.pi, 1000)

labels = make_dict_vect(E_J, alpha, phi_exts) #labels is a dictionnary where each key corresponds to a list of values

plt.plot(phi_exts, np.array(labels["E2"])/1e9, label = "E2")
plt.plot(phi_exts, np.array(labels["E3"])/1e9,  label = "E3")
plt.plot(phi_exts, np.array(labels["E4"])/1e9,  label = "E4")
plt.legend()
```

The unbiased SNAIL has no third order term since it's has a parity in the flux.
Let's look at the frequencies, given by E2 :

```python
freqs = circuit.eigenfrequencies(**labels)/1e9

plt.xlabel(r"$\phi/_{ext}\pi}$")
plt.ylabel("Eigenfrequencies (GHz)")
plt.title("Eigenfrequencies as a function of $\phi_{ext}$")
[plt.plot(phi_exts/np.pi, c) for c in freqs]

```

They follow the evolution of E2, which is expected.

Let's look at the three waves mixing term :

```python
three_waves = circuit.three_waves(0, 1, 2, **labels)/1e6 #In MHz

plt.xlabel(r"$\phi/\pi}$")
plt.ylabel("Three waves mixing term (MHz)")
plt.title("Three waves mixing term (MHz) as a function of the imposed phase")
plt.plot(phi_exts/np.pi, three_waves)

```

It has the same shape as the E3 term.

And now, the self-Kerr terms :

```python
kerr_coeffs = circuit.kerr(**labels)/1e6 # in MHz

plt.xlabel(r"$\phi/\pi}$")
plt.ylabel("Self Kerr coefficient (MHz)")
plt.title("Self Kerr coefficients as a function of the exterior phase")
plt.legend()
[plt.plot(phi_exts/np.pi, kerr_coeffs[i, i], label = "Mode "+ str(i)) for i in range(len(kerr_coeffs))]
plt.legend()

```

We can clearly see the existence of the sweet spot, point where all the Kerr terms vanish, but the three waves mixing term still exists, allowing to make a parametric amplifier without beeing limited by the Kerr terms, cf *SNAIL scheme taken from N. E. Frattini, V. V. Sivak, A. Lingenfelter, S. Shankar et M. H. Devoret : Optimizing the Nonlinearity and Dissipation of a SNAIL Parametric Amplifier for Dynamic Range. Physical Review Applied, 10(5):1–17, 2018.*

The other features of qucat apply otherwise : the f_k_A_chi function, getting the full hamiltonian with qutip. It is now possible to get the hamiltonian with sympy :

```python
circuit.hamiltonian_sym(**make_dict(E_J, alpha, np.pi/7), modes = 'all')
```

Note that the expression isn't simplified, and uses the bosonic operators from the sympy.physics.secondquant library. To simplify it (normal ordering for example), one needs to further analyse it by themselves, I didn't found any built-in function to do this with bosonic operators (it exists for fermionic operators though).

The Keywords arguments are the same as for the hamiltonian function that works with qutip. However, to further Taylor expand the hamiltonian, one should have specified the corresponding number of labels for each arbitray dipole in the circuit. In our example, we cannot go further than the fourth order, unless we decide to provide more labels.

One can also, for example, select the modes to be taken into account.


### Action of boundary and co-boundary maps on a chain

__*Definition.*__ An abstract simplicial complex $K$ is a collection of finite sets that is closed
under set inclusion, i.e. if $\sigma \in K$ and $\tau \subseteq \sigma$, then $\tau \in K$.

__*Definition.*__ The boundary operator $\partial_d : C_d(K) \rightarrow C_{d-1}(K)$ is the linear function defined for each oriented $d$-simplex $\sigma = [v_0, ..., v_d]$ by
\begin{equation}
\partial_d (\sigma) = \partial_d [v_0, ..., v_d] = \sum_{i=0}^d (-1)^i [v_0, ..., \hat{v}_i, ..., v_d],
\end{equation}
where $[v_0, ... \hat{v}_i, ..., v_d]$ is the subset of $[v_0, ..., v_d]$ obtained by removing the vertex $v_i$.

Let $S_d(K)$ be the set of all oriented $d$-simplices of the simplicial complex $K$ (i.e. the set of basis elements of $C_d(K)$), and let $\tau in S_{d-1}(K)$. Then define the two sets
\begin{align}
S^+_d (K) &= \{ \sigma \in S_d(K) \, | \text{ the coefficient of } \tau \text{ in } \partial_d (\sigma) \text{ is } +1 \} \\
S^-_d (K) &= \{ \sigma \in S_d(K) \, | \text{ the coefficient of } \tau \text{ in } \partial_d (\sigma) \text{ is } -1 \}
\end{align}

__*Lemma (see [1])*__ Let $\partial^*_d$ be the adjoint of the boundary operator $\partial_d$, and let $\tau \in S_{d-1}(K)$. Then
\begin{equation}
\partial^*_d (\tau) = \sum_{\sigma' \in S^+_d (K, \tau)} \sigma' - \sum_{\sigma'' \in  S^-_d (K, \tau)} \sigma''.
\end{equation}

In this section we are going to see how boundary maps act on

```python

```

## References

1. [R.Gustavson, Laplacians of Covering Complexes](https://scholar.rose-hulman.edu/cgi/viewcontent.cgi?article=1099&context=rhumj)
2. [From Topological Data Analysis to Deep Learning: No Pain No Gain](https://towardsdatascience.com/from-tda-to-dl-d06f234f51d)
3. [S. Mukherjee and J. Steenbergen, Random walks on simplicial complexes and harmonics](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5324709/)

4. [Michael T. Schaub, Austin R. Benson, Paul Horn, Gabor Lippner, Ali Jadbabaie, Random Walks on Simplicial Complexes and the normalized Hodge 1-Laplacian](https://arxiv.org/pdf/1807.05044.pdf)
5. [GUDHI Library](http://gudhi.gforge.inria.fr/doc/latest/index.html)

6. [R. Forman, Bochner's Method for Cell Complexes and Combinatorial Ricci Curvature](https://link.springer.com/content/pdf/10.1007%2Fs00454-002-0743-x.pdf)


# Lecture 12: Discrete vs Continuous Distributions

## Discrete vs Continous Random Variables

So, we've completed our whirlwind introduction of discrete random variables, covering the following:

1. Bernoulli
1. Binomial
1. Hypergeometric
1. Geometric
1. Negative Binomial
1. Poisson

As we now move into continous random variables, let's create a table to compare/contrast important random variable properties and concepts.

| Discrete      | Continuous    |
| ------------- |---------------|
| $X$           | $X$           |
| PMF $P(X=x)$  | PDF $f_x(x) = F^\prime(x)$<br/>note $P(X=x)=0$ |
| CDF $F_x(x)=P(X \le x)$| CDF $F_x(x) = P(X \le x)$ |
| $\mathbb{E}(X) = \sum_{x} x P(X=x)$  | $\mathbb{E}(X) = \int_{-\infty}^{\infty} x f(x)dx$  |
| $Var(X) = \mathbb{E}X^2 - \mathbb{E}(X)^2$ | $Var(X) = \mathbb{E}X^2 - \mathbb{E}(X)^2$ |
| $SD(X) = \sqrt{Var(X)}$ | $SD(X) = \sqrt{Var(X)}$ |
| LOTUS $\mathbb{E}(g(x)) = \sum_{x} g(x) P(X=x)$ | LOTUS $\mathbb{E}( g(x) ) = \int_{-\infty}^{\infty} g(x) f(x)dx$ |

----

## Probability Density Function

In the _discrete_ case, we could calculate probability by __summing__ (counting) the discrete elements, since each element represents a bit of mass. The probability would be the total of all elements concerned, divided by total mass.

But we cannot count discrete elements in the _continuous_ case. Instead, we __integrate__ the density function over a range to get probability mass per _area_.

> __Definition__
> A random variable $X$ has PDF $f(x)$ if for all $a$ and $b$
>
> \\begin{align}
>   & P(a \le x \le b) = \int_a^b f(x) dx \\\\
> \\end{align}

### Test for validity

Note that to be a valid PDF,

1. $f(x) \ge 0$
1. $\int_{-\infty}^{\infty} f(x) = 1$

### Probability at a _point_ is 0

- $a = b \Rightarrow \int_{a}^{a} f(x)dx = 0$

### Density $\times$ Length

But for some point $x_0$ and some _very, very small value_ $\epsilon$, we can derive $f(x_0) \epsilon \approx P(X \in (x_0-\frac{\epsilon}{2}, x_0+\frac{\epsilon}{2})$

----

## Cumulative Distribution Function

### Deriving CDF from PDF

If continuous r.v. $X$ has PDF $f$, the CDF is

\begin{align}
  F(x) &= P(X \le x) \\
       &= \int_{-\infty}^{x} f(t) dt \\
  \\
  \Rightarrow P(a \le x \le b) &= \int_{a}^{b} f(x)dx \\
  &= F(b) - F(a) \\
\end{align}

### Deriving PDF from CDF

If continuous r.v. $X$ has CDF $F$ (and $X$ is continuous), the PDF is

\begin{align}
  f(x) &= F^\prime(x) & &\text{ by the Fundamental Theorem of Calculus} \\
\end{align}

----

## Variance

Mean only tells you where the average is. Another useful statistic is the _variance_ of a random variable, which tells you how the random variable is spread out around the mean.

In other words, _variance_ answers the question _How far is $X$ from its mean, on average?_

> __Definition__ Variance is a measure of how a random variable is spread about its mean.
>
> \\begin{align}
>   Var(X) &= \mathbb{E}(X - \mathbb{E}X)^2 & \quad \text{or alternatively} \\\\
>          \\\\
>          &= \mathbb{E}X^2 - 2X(\mathbb{E}X) + \mathbb{E}(X^2) & \quad \text{by Linearity}\\\\
>          &= \boxed{\mathbb{E}X^2 - \mathbb{E}(X)^2}
> \\end{align}

Sometimes the second form of variance is easier to use.

Note that the formula variance is the same for both discrete and continuous r.v.

But you might be wondering right now how to calculate $\mathbb{E}X^2$. We will get to that in a bit...

----

## Standard Deviation

But note that variance is expressed in terms of units __squared__. _Standard deviation_ is sometimes easier to use than variance, as it is given in the original units.

> __Definition__ The _standard deviation_ the square root of the variance.
>
> \\begin{align}
>   SD(X) &= \sqrt{Var(X)}
> \\end{align}

Note that like variance, the formula for standard deviation is the same for both discrete and continuous r.v.

----

## Uniform Distribution

### Description

The simplest and perhaps the most famous continuous distribution. Given starting point $a$ and ending point $b$, probability $\propto$ length.

### Notation

$X \sim \mathcal{Unif}(a,b)$

### Parameters

- $a$ start of the segment, $a < b$
- $b$ end of the segment, $b > a$

### Probability density function

\begin{align}
  f(x) &=
    \begin{cases}
    c & \quad \text{ if } a \le x \le b \\
    0 & \quad \text{ otherwise }
  \end{cases} \\
  \\
  \\
  1 &= \int_{a}^{b} c dx \\
  \Rightarrow c &= \boxed{\frac{1}{b-a}}
\end{align}

### Cumulative distribution function

\begin{align}
  F(x) &= \int_{-\infty}^{x} f(t)dt \\
       &= \int_{a}^{x} f(t)dt \\
       &=
         \begin{cases}
           0 & \quad \text{if } x \lt a \\
           \frac{x-a}{b-a} & \quad \text{if } a \lt x \lt b \\
           1 & \quad \text{if } x \gt b
         \end{cases} \\
\end{align}

So this means that as $X$ increase, its probability increase likewise in a _linear_ fashion.

### Expected value

For continuous r.v.

\begin{align}
  \mathbb{E}(X) &= \int_{a}^{b} x \frac{1}{b-a} dx \\
  &=\left. \frac{x^2}{2(b-a)} ~~ \right\vert_{a}^{b} \\
  &= \frac{(b^2-a^2)}{2(b-a)} \\
  &= \boxed{\frac{b+a}{2}}
\end{align}

### Variance

Remember that lingering doubt about $\mathbb{E}X^2$?

Let random variable $Y = X^2$.

\begin{align}
  \mathbb{E}X^2 &= \mathbb{E}(Y) \\
                &\stackrel{?}{=} \int_{-\infty}^{\infty} x^2 f(x) dx & &\text{since we need the PDF of Y..?}
\end{align}

----

## Law of the Unconscious Statistician (LOTUS)

Actually, that last bit of wishful thinking is correct and will work ieginn both the discrete and continuous cases.

In general for continuous r.v.

\begin{align}
  \mathbb{E}( g(x) ) = \int_{-\infty}^{\infty} g(x) f(x)dx
\end{align}

And likewise for discrete r.v.

\begin{align}
  \mathbb{E}(g(x)) = \sum_{x} g(x) P(X=x)
\end{align}

### Variance of  $U \sim \mathcal{Unif}(0,1)$

\begin{align}
  \mathbb{E}(U) &= \frac{1}{b-a} \\
                &= \frac{1}{2}   \\
  \\
  \\
  \mathbb{E}U^2 &= \int_{0}^{1} u^2 \underbrace{f(u) du}_{1} \\
                &= \left.\frac{u^3}{3} ~~ \right\vert_{0}^{1} \\
                &= \frac{1}{3} \\
  \\
  \\
  \Rightarrow Var(U) &= \mathbb{E}U^2 - \mathbb{E}(U)^2 \\
                     &= \frac{1}{3} - \left(\frac{1}{2}\right)^2 \\
                     &= \frac{1}{12}
\end{align}

----

## Universality of the Uniform

Given an arbitrary CDF $F$ and the uniform $\mathcal{U} \sim \mathcal{Unif}(0,1)$, it is possible to simulate a draw from the continuous r.v. of the CDF $F$.

Assume:

1. $F$ is strictly increasing
1. $F$ is continuous as a function

If we define $X = F^{-1}(U)$. Then $X \sim F$.

\begin{align}
  P(X \le x) &= P(F^{-1}(U) \le x) \\
             &= P(U \le F(x)) \\
             &= F(x) & \quad \text{ since } P(U \le u) \propto 1~~  \blacksquare
\end{align}

----


# Simplification

```python
from sympy import *
x, y, z = symbols('x y z')
init_printing()
```

For each exercise, fill in the function according to its docstring.

## Polynomial/Rational Function Simplification

In each exercise, apply specific simplification functions to get the desired result.

```python
def polysimp1(expr):
    """
    >>> polysimp1(cos(x)*sin(x) + cos(x))
    (sin(x) + 1)*cos(x)
    >>> polysimp1(cos(x)*sin(x) + cos(x) + 1)
    (sin(x) + 1)*cos(x) + 1
    """
    return collect(expr, cos(x))
```

```python
polysimp1(cos(x)*sin(x) + cos(x))
```

```python
polysimp1(cos(x)*sin(x) + cos(x) + 1)
```

```python
def polysimp2(expr):
    """
    >>> polysimp2((2*x + 1)/(x**2 + x))
    1/(x + 1) + 1/x
    >>> polysimp2((x**2 + 3*x + 1)/(x**3 + 2*x**2 + x))
    1/(x**2 + 2*x + 1) + 1/x
    """
    return apart(expr, x)
```

```python
polysimp2((2*x + 1)/(x**2 + x))
```

```python
polysimp2((x**2 + 3*x + 1)/(x**3 + 2*x**2 + x))
```

## Powers

In each exercise, apply specific simplification functions to get the desired result.

```python
def powersimp1(expr):
    """
    >>> powersimp1(exp(x)*(exp(y) + 1))
    exp(x) + exp(x + y)
    """
    return powsimp(expand(expr))
```

```python
powersimp1(exp(x)*(exp(y) + 1))
```

```python
def powersimp2(expr):
    """
    >>> powersimp2(2**x*x**x)
    (2*x)**x
    >>> powersimp2(x**x*x**x)
    (x**2)**x
    """
    return powsimp(expr, force=True)
```

```python
powersimp2(2**x*x**x)
```

```python
powersimp2(x**x*x**x)
```

```python
def powersimp3(expr):
    """
    >>> a, b, c = symbols('a b c')
    >>> powersimp3((a**b)**c)
    a**(b*c)
    >>> powersimp3((a**b)**(c + 1))
    a**(b*c + b)
    """
    return powdenest(expand_power_exp(expr), force=True)
```

```python
a, b, c = symbols('a b c')
expand_power_exp((a**b)**(c + 1))
```

```python
powersimp3((a**b)**c)
```

```python
powersimp3((a**b)**(c + 1))
```

## Logs

```python
def logsimp1(expr):
    """
    >>> a, b = symbols('a b', positive=True)
    >>> logsimp1(log(x**y*a**b))
    y*log(x) + log(a**b)
    >>> logsimp1(log(x*y*a*b))
    log(x) + log(y) + log(a*b)
    """
    return logcombine(expand_log(expr, force=True))
```

```python
a, b = symbols('a b', positive=True)
```

```python
logsimp1(log(x**y*a**b))
```

```python
logsimp1(log(x*y*a*b))
```

## Miscellaneous

```python
def miscsimp1(expr):
    """
    >>> miscsimp1(sin(x + y))
    2*(-tan(x/2)**2 + 1)*tan(y/2)/((tan(x/2)**2 + 1)*(tan(y/2)**2 + 1)) + 2*(-tan(y/2)**2 + 1)*tan(x/2)/((tan(x/2)**2 + 1)*(tan(y/2)**2 + 1))
    """
    return expand_trig(expr).rewrite(tan)
```

```python
miscsimp1(sin(x + y))
```

```python
def miscsimp2(expr):
    """
    >>> miscsimp2(gamma(x + 4))
    x**4*gamma(x) + 6*x**3*gamma(x) + 11*x**2*gamma(x) + 6*x*gamma(x)
    """
    return expand(expr, func=True)
```

```python
miscsimp2(gamma(x + 4))
```

## Continued Fractions

If we do not cover this, see http://docs.sympy.org/latest/tutorial/simplification.html#example-continued-fractions

```python
def list_to_frac(l):
    expr = Integer(0)
    for i in reversed(l[1:]):
        expr += i
        expr = 1/expr
    return l[0] + expr
```

```python
a0, a1, a2, a3, a4 = symbols('a0:5')
```

Determine the list used to create the continued fraction $$\frac{a_{0} a_{1} a_{2} a_{3} a_{4} + a_{0} a_{1} a_{2} + a_{0} a_{3} a_{4} + a_{0} + a_{1} a_{2} a_{3} + a_{1} a_{3} a_{4} + a_{1} + a_{3}}{a_{0} a_{1} a_{2} a_{4} + a_{0} a_{4} + a_{1} a_{2} + a_{1} a_{4} + 1}.$$

```python
def continued_frac():
    """
    Determine the original list used to create the fraction.

    Return the original list from this function.

    >>> orig_frac = (a0*a1*a2*a3*a4 + a0*a1*a2 + a0*a3*a4 + a0 + a1*a2*a3 + a1*a3*a4 + a1 + a3)/(a0*a1*a2*a4 + a0*a4 + a1*a2 + a1*a4 + 1)
    >>> pprint(orig_frac)
    a₀⋅a₁⋅a₂⋅a₃⋅a₄ + a₀⋅a₁⋅a₂ + a₀⋅a₃⋅a₄ + a₀ + a₁⋅a₂⋅a₃ + a₁⋅a₃⋅a₄ + a₁ + a₃
    ─────────────────────────────────────────────────────────────────────────
                     a₀⋅a₁⋅a₂⋅a₄ + a₀⋅a₄ + a₁⋅a₂ + a₁⋅a₄ + 1
    >>> cancel(list_to_frac(continued_frac())) == orig_frac
    True
    """
    return [a3, a4, a0, a2, a1]
```

```python
orig_frac = (a0*a1*a2*a3*a4 + a0*a1*a2 + a0*a3*a4 + a0 + a1*a2*a3 + a1*a3*a4 + a1 + a3)/(a0*a1*a2*a4 + a0*a4 + a1*a2 + a1*a4 + 1)
```

```python
orig_frac
```

```python
cancel(list_to_frac(continued_frac())) == orig_frac
```

    True

```python

```


Chapter 2
======
______

This chapter introduces more PyMC syntax and design patterns, and ways to think about how to model a system from a Bayesian perspective. It also contains tips and data visualization techniques for assessing goodness-of-fit for your Bayesian model.

## A little more on PyMC

### Parent and Child relationships

To assist with describing Bayesian relationships, and to be consistent with PyMC's documentation, we introduce *parent and child* variables.

*  *parent variables* are variables that influence another variable.

*  *child variable* are variables that are affected by other variables, i.e. are the subject of parent variables.

A variable can be both a parent and child. For example, consider the PyMC code below.

```
import pymc as mc

parameter = mc.Exponential("poisson_param", 1)
data_generator = mc.Poisson("data_generator", parameter)
data_plus_one = data_generator + 1
```

`parameter` controls the parameter of `data_generator`, hence influences its values. The former is a parent of the latter. By symmetry, `data_generator` is a child of `parameter`.

Likewise, `data_generator` is a parent to the variable `data_plus_one` (hence making `data_generator` both a parent and child variable). Although it does not look like one, `data_plus_one` should be treated as a PyMC variable as it is a *function* of another PyMC variable, hence is a child variable to `data_generator`.

This nomenclature is introduced to help us describe relationships in PyMC modeling. You can access a variables children and parent variables using the `children` and `parents` attributes attached to variables.

```
print "Children of `parameter`: "
print parameter.children
print "\nParents of `data_generator`: "
print data_generator.parents
print "\nChildren of `data_generator`: "
print data_generator.children
```

    Children of `parameter`:
    set([<pymc.distributions.Poisson 'data_generator' at 0x10d3c3f50>])

    Parents of `data_generator`:
    {'mu': <pymc.distributions.Exponential 'poisson_param' at 0x10a0461d0>}

    Children of `data_generator`:
    set([<pymc.PyMCObjects.Deterministic '(data_generator_add_1)' at 0x10d3d0790>])

Of course a child can  have more than one parent, and a parent can have many children.

### PyMC Variables

All PyMC variables also expose a `value` attribute. This method produces the *current* (possibly random) internal value of the variable. If the variable is a child variable, its value changes given the variable's parents' values. Using the same variables from before:

```
print "parameter.value =", parameter.value
print "data_generator.value =", data_generator.value
print "data_plus_one.value =", data_plus_one.value
```

    parameter.value = 0.0541359000392
    data_generator.value = 0
    data_plus_one.value = 1

PyMC is concerned with two types of programming variables: `stochastic` and `deterministic`.

*  *stochastic variables* are variables that are not deterministic, i.e., even if you knew all the values of the variables' parents (if it even has any parents), it would still be random. Included in this category are instances of classes `Poisson`, `DiscreteUniform`, and `Exponential`.

*  *deterministic variables* are variables that are not random if the variables' parents were known. This might be confusing at first: a quick mental check is *if I knew all of variable `foo`'s parent variables, I could determine what `foo`'s value is.*

We will detail each below.

#### Initializing Stochastic variables

Initializing a stochastic variable requires a `name` argument, plus additional parameters that are class specific. For example:

`some_variable = mc.DiscreteUniform("discrete_uni_var", 0, 4)`

where 0, 4 are the `DiscreteUniform`-specific upper and lower bound on the random variable. The [PyMC docs](http://pymc-devs.github.com/pymc/distributions.html) contain the specific parameters for stochastic variables. (Or use `??` if you are using IPython!)

The `name` attribute is used to retrieve the posterior distribution later in the analysis, so it is best to use a descriptive name. Typically, I use the Python variable's name as the name.

For multivariable problems, rather than creating a Python array of stochastic variables, addressing the `size` keyword in the call to a `Stochastic` variable creates multivariate array of (independent) stochastic variables. The array behaves like a Numpy array when used like one, and references to its `value` attribute return Numpy arrays.

The `size` argument also solves the annoying case where you may have many variables $\beta_i, \; i = 1,...,N$ you wish to model. Instead of creating arbitrary names and variables for each one, like:

    beta_1 = mc.Uniform("beta_1", 0, 1)
    beta_2 = mc.Uniform("beta_2", 0, 1)
    ...

we can instead wrap them into a single variable:

    betas = mc.Uniform("betas", 0, 1, size=N)

#### Calling `random()`
We can also call on a stochastic variable's `random()` method, which (given the parent values) will generate a new, random value. Below we demonstrate this using the texting example from the previous chapter.

```
lambda_1 = mc.Exponential("lambda_1", 1)  # prior on first behaviour
lambda_2 = mc.Exponential("lambda_2", 1)  # prior on second behaviour
tau = mc.DiscreteUniform("tau", lower=0, upper=10)  # prior on behaviour change

print "lambda_1.value = %.3f" % lambda_1.value
print "lambda_2.value = %.3f" % lambda_2.value
print "tau.value = %.3f" % tau.value
print

lambda_1.random(), lambda_2.random(), tau.random()

print "After calling random() on the variables..."
print "lambda_1.value = %.3f" % lambda_1.value
print "lambda_2.value = %.3f" % lambda_2.value
print "tau.value = %.3f" % tau.value
```

    lambda_1.value = 2.970
    lambda_2.value = 0.037
    tau.value = 1.000

    After calling random() on the variables...
    lambda_1.value = 1.145
    lambda_2.value = 0.338
    tau.value = 9.000

The call to `random` stores a new value into the variable's `value` attribute. In fact, this new value is stored in the computer's cache for faster recall and efficiency.

### **Warning**: *Don't update stochastic variables' values in-place.*

Straight from the PyMC docs, we quote [4]:

> `Stochastic` objects' values should not be updated in-place. This confuses PyMC's caching scheme... The only way a stochastic variable's value should be updated is using statements of the following form:

        A.value = new_value

> The following are in-place updates and should **never** be used:

        A.value += 3
        A.value[2,1] = 5
        A.value.attribute = new_attribute_value

#### Deterministic variables

Since most variables you will be modeling are stochastic, we distinguish deterministic variables with a `pymc.deterministic` wrapper. (If you are unfamiliar with Python wrappers (also called decorators), that's no problem. Just prepend the `pymc.deterministic` decorator before the variable declaration and you're good to go. No need to know more. ) The declaration of a deterministic variable uses a Python function:

    @mc.deterministic
    def some_deterministic_var(v1=v1,):
         #jelly goes here.

For all purposes, we can treat the object `some_deterministic_var` as a variable and not a Python function.

Prepending with the wrapper is the easiest way, but not the only way, to create deterministic variables. This is not completely true: elementary operations, like addition, exponentials etc. implicitly create deterministic variables. For example, the following returns a deterministic variable:

```
type(lambda_1 + lambda_2)
```

    pymc.PyMCObjects.Deterministic

The use of the `deterministic` wrapper was seen in the previous chapter's text-message example.  Recall the model for $\lambda$ looked like:

$$
\lambda =
\cases{
\lambda_1  & \text{if } t \lt \tau \cr
\lambda_2 & \text{if } t \ge \tau
}
$$

And in PyMC code:

```
n_data_points = 5  # in CH1 we had ~70 data points

@mc.deterministic
def lambda_(tau=tau, lambda_1=lambda_1, lambda_2=lambda_2):
    out = np.zeros(n_data_points)
    out[:tau] = lambda_1  # lambda before tau is lambda1
    out[tau:] = lambda_2  # lambda after tau is lambda1
    return out
```

Clearly, if $\tau, \lambda_1$ and $\lambda_2$ are known, then $\lambda$ is known completely, hence it is a deterministic variable.

Inside the deterministic decorator, the `Stochastic` variables passed in behave like scalars or Numpy arrays (if multivariable), and *not* like `Stochastic` variables. For example, running the following:

    @mc.deterministic
    def some_deterministic(stoch=some_stochastic_var):
        return stoch.value**2

will return an `AttributeError` detailing that `stoch` does not have a `value` attribute. It simply needs to be `stoch**2`. During the learning phase, it's the variable's `value` that is repeatedly passed in, not the actual variable.

Notice in the creation of the deterministic function we added defaults to each variable used in the function. This is a necessary step, and all variables *must* have default values.

### Including observations in the Model

At this point, it may not look like it, but we have fully specified our priors. For example, we can ask and answer questions like "What does my prior distribution of $\lambda_1$ look like?"

```
%pylab inline
figsize(12.5, 4)

samples = [lambda_1.random() for i in range(20000)]
hist(samples, bins=70, normed=True, histtype="stepfilled")
plt.title("Prior distribution for $\lambda_1$")
plt.xlim(0, 8)
```

To frame this in the notation of the first chapter, though this is a slight abuse of notation, we have specified $P(A)$. Our next goal is to include data/evidence/observations $X$ into our model.

PyMC stochastic variables have a keyword argument `observed` which accepts a boolean (`False` by default). The keyword `observed` has a very simple role: fix the variable's current value, i.e. make `value` immutable. We have to specify an initial `value` in the variable's creation, equal to the observations we wish to include, typically an array (and it should be an Numpy array for speed). For example:

```
data = np.array([10, 5])
fixed_variable = mc.Poisson("fxd", 1, value=data, observed=True)
print "value: ", fixed_variable.value
print "calling .random()"
fixed_variable.random()
print "value: ", fixed_variable.value
```

    value:  [10  5]
    calling .random()
    value:  [10  5]

This is how we include data into our models: initializing a stochastic variable to have a *fixed value*.

To complete our text message example, we fix the PyMC variable `observations` to the observed dataset.

```
# We're using some fake data here
data = np.array([10, 25, 15, 20, 35])
obs = mc.Poisson("obs", lambda_, value=data, observed=True)
print obs.value
```

    [10 25 15 20 35]

### Finally...

We wrap all the created variables into a `mc.Model` class. With this `Model` class, we can analyze the variables as a single unit. This is an optional step, as the fitting algorithms can be sent an array of the variables rather than a `Model` class. I may or may not use this class in future examples ;)

```
model = mc.Model([obs, lambda_, lambda_1, lambda_2, tau])
```

## Modeling approaches

A good starting thought to Bayesian modeling is to think about *how your data might have been generated*. Position yourself in an omniscient position, and try to imagine how *you* would recreate the dataset.

In the last chapter we investigated text message data. We begin by asking how our observations may have been generated:

1.  We started by thinking "what is the best random variable to describe this count data?" A Poisson random variable is a good candidate because it can represent count data. So we model the number of sms's received as sampled from a Poisson distribution.

2.  Next, we think, "Ok, assuming sms's are Poisson-distributed, what do I need for the Poisson distribution?" Well, the Poisson distribution has a parameters $\lambda$.

3.  Do we know $\lambda$? No. In fact, we have a suspicion that there are *two* $\lambda$ values, one for the earlier behaviour and one for the latter behaviour. We don't know when the behaviour switches though, but call the switchpoint $\tau$.

4. What is a good distribution for the two $\lambda$s? The exponential is good, as it assigns probabilities to positive real numbers. Well the exponential distribution has a parameter too, call it $\alpha$.

5.  Do we know what the parameter $\alpha$ might be? No. At this point, we could continue and assign a distribution to $\alpha$, but it's better to stop once we reach a set level of ignorance: whereas we have a prior belief about $\lambda$, ("it probably changes over time", "it's likely between 10 and 30", etc.), we don't really have any strong beliefs about $\alpha$. So it's best to stop here.

    What is a good value for $\alpha$ then? We think that the $\lambda$s are between 10-30, so if we set $\alpha$ really low (which corresponds to larger probability on high values) we are not reflecting our prior well. Similar, a too-high alpha misses our prior belief as well. A good idea for $\alpha$ as to reflect our belief is to set the value so that the mean of $\lambda$, given $\alpha$, is equal to our observed mean. This was shown in the last chapter.

6. We have no expert opinion of when $\tau$ might have occurred. So we will suppose $\tau$ is from a discrete uniform distribution over the entire timespan.

Below we give a graphical visualization of this, where arrows denote `parent-child` relationships. (provided by the [Daft Python library](http://daft-pgm.org/) )

PyMC, and other probabilistic programming languages, have been designed to tell these data-generation *stories*. More generally, B. Cronin writes [5]:

> Probabilistic programming will unlock narrative explanations of data, one of the holy grails of business analytics and the unsung hero of scientific persuasion. People think in terms of stories - thus the unreasonable power of the anecdote to drive decision-making, well-founded or not. But existing analytics largely fails to provide this kind of story; instead, numbers seemingly appear out of thin air, with little of the causal context that humans prefer when weighing their options.

### Same story; different ending.

Interestingly, we can create *new datasets* by retelling the story.
For example, if we reverse the above steps, we can simulate a possible realization of the dataset.

1\. Specify when the user's behaviour switches by sampling from $\text{DiscreteUniform}(0, 80)$:

```
tau = mc.rdiscrete_uniform(0, 80)
print tau
```

    3

2\. Draw $\lambda_1$ and $\lambda_2$ from an $\text{Exp}(\alpha)$ distribution:

```
alpha = 1./20.
lambda_1, lambda_2 = mc.rexponential(alpha, 2)
print lambda_1, lambda_2
```

    7.13164480429 27.2122611088

3\.  For days before $\tau$, represent the user's received SMS count by sampling from $\text{Poi}(\lambda_1)$, and sample from  $\text{Poi}(\lambda_2)$ for days after $\tau$. For example:

```
data = np.r_[mc.rpoisson(lambda_1, tau), mc.rpoisson(lambda_2, 80 - tau)]
```

4\. Plot the artificial dataset:

```
plt.bar(np.arange(80), data, color="#348ABD")
plt.bar(tau-1, data[tau - 1], color="r", label="user behaviour changed")
plt.xlabel("Time (days)")
plt.ylabel("count of text-msgs received")
plt.title("Artificial dataset")
plt.xlim(0, 80)
plt.legend()
```

It is okay that our fictional dataset does not look like our observed dataset: the probability is incredibly small it indeed would. PyMC's engine is designed to find good parameters, $\lambda_i, \tau$, that maximize this probability.

The ability to generate artificial dataset is an interesting side effect of our modeling, and we will see that this ability is a very important method of Bayesian inference. We produce a few more datasets below:

```
def plot_artificial_sms_dataset():
    tau = mc.rdiscrete_uniform(0, 80)
    alpha = 1./20.
    lambda_1, lambda_2 = mc.rexponential(alpha, 2)
    data = np.r_[mc.rpoisson(lambda_1, tau), mc.rpoisson(lambda_2, 80 - tau)]
    plt.bar(np.arange(80), data, color="#348ABD")
    plt.bar(tau - 1, data[tau-1], color="r", label="user behaviour changed")
    plt.xlim(0, 80)

figsize(12.5, 5)
plt.title("More example of artificial datasets")
for i in range(4):
    plt.subplot(4, 1, i)
    plot_artificial_sms_dataset()

```

Later we will see how we use this to make predictions and test the appropriateness of our models.

#####Example: Bayesian A/B testing

A/B testing is a statistical design pattern for determining the difference of effectiveness between two different treatments. For example, a pharmaceutical company is interested in the effectiveness of drug A vs drug B. The company will test drug A on some fraction of their trials, and drug B on the other fraction (this fraction is often 1/2, but we will relax this assumption). After performing enough trials, the in-house statisticians sift through the data to determine which drug yielded better results.

Similarly, front-end web developers are interested in which design of their website yields more sales or some other metric of interest. They will route some fraction of visitors to site A, and the other fraction to site B, and record if the visit yielded a sale of not. The data is recorded (in real-time), and analyzed afterwards.

Often, the post-experiment analysis is done using something called a hypothesis test like *difference of means test* or *difference of proportions test*. This involves often misunderstood quantities like a "Z-score" and even more confusing "p-values" (please don't ask). If you have taken a statistics course, you have probably been taught this technique (though not necessarily *learned* this technique). And if you were like me, you may have felt uncomfortable with their derivation -- good: the Bayesian approach to this problem is much more natural.

### A Simple Case

As this is a hacker book, we'll continue with the web-dev example. For the moment, we will focus on the analysis of site A only. Assume that there is some true $0 \lt p_A \lt 1$ probability that users who, upon shown site A, eventually purchase from the site. This is the true effectiveness of site A. Currently, this quantity is unknown to us.

Suppose site A was shown to $N$ people, and $n$ people purchased from the site. One might conclude hastly that $p_A = \frac{n}{N}$. Unfortunately, the *observed frequency* $\frac{n}{N}$ does not necessarily equal $p_A$ -- there is a difference between the *observed frequency* and the *true frequency* of an event. The true frequency can be interpreted as the probability of an event occurring. For example, the true frequency of rolling a 1 on a 6-sided die is $\frac{1}{6}$. Knowing the true frequency of events like:

- fraction of users who make purchases,
- frequency of social attributes,
- percent of internet users with cats etc.

are common requests we ask of Nature. Unfortunately, often Nature hides the true frequency from us and we must *infer* it from observed data.

The *observed frequency* is then the frequency we observe: say rolling the die 100 times you may observe 20 rolls of 1. The observed frequency, 0.2, differs from the true frequency, $\frac{1}{6}$. We can use Bayesian statistics to infer probable values of the true frequency using an appropriate prior and observed data.

With respect to our A/B example, we are interested in using what we know, $N$ (the total trials administered) and $n$ (the number of conversions), to estimate what $p_A$, the true frequency of buyers, might be.

To setup a Bayesian model, we need to assign prior distrbutions to our unknown quantities. *A priori*, what do we think $p_A$ might be? For this example, we have no strong conviction about $p_A$, so for now, let's assume $p_A$ is uniform over [0,1]:

```
import pymc as mc

# The parameters are the bounds of the Uniform.
p = mc.Uniform('p', lower=0, upper=1)
```

Had we had stronger beliefs, we could have expressed them in the prior above.

For this example, consider $p_A = 0.05$, and $N = 1500$ users shown site A, and we will simulate whether the user made a purchase or not. To simulate this from $N$ trials, we will use a *Bernoulli* distribution: if  $ X\ \sim \text{Ber}(p)$, then $X$ is 1 with probability $p$ and 0 with probability $1 - p$. Of course, in practice we do not know $p_A$, but we will use it here to simulate the data.

```
#set constants
p_true = 0.05  # remember, this is unknown.
N = 1500

# sample N Bernoulli random variables from Ber(0.05).
# each random variable has a 0.05 chance of being a 1.
# this is the data-generation step
occurrences = mc.rbernoulli(p_true, N)

print occurrences  # Remember: Python treats True == 1, and False == 0
print occurrences.sum()
```

    [False  True False ...,  True False False]
    73

The observed frequency is:

```
# Occurrences.mean is equal to n/N.
print "What is the observed frequency in Group A? %.4f" % occurrences.mean()
print "Does this equal the true frequency? %s" % (occurrences.mean() == p_true)
```

    What is the observed frequency in Group A? 0.0487
    Does this equal the true frequency? False

We combine the observations into the PyMC `observed` variable, and run our inference algorithm:

```
#include the observations, which are Bernoulli
obs = mc.Bernoulli("obs", p, value=occurrences, observed=True)

#To be explained in chapter 3
mcmc = mc.MCMC([p, obs])
mcmc.sample(18000, 1000)
```

    [****************100%******************]  18000 of 18000 complete

We plot the posterior distribution of the unknown $p_A$ below:

```
figsize(12.5, 4)
plt.title("Posterior distribution of $p_A$, the true effectiveness of site A")
plt.vlines(p_true, 0, 90, linestyle="--", label="true $p_A$ (unknown)")
plt.hist(mcmc.trace("p")[:], bins=25, histtype="stepfilled", normed=True)
plt.legend()
```

Our posterior distribution puts most weight near the true value of $p_A$, but also some weights in the tails. This is a measure of how uncertain we should be, given our observations. Try changing the number of observations, `N`, and observe how the posterior distribution changes.

### *A* and *B* Together

A similar anaylsis can be done for site B's response data to determine the analgous $p_B$. But what we are really interested in is the *difference* between $p_A$ and $p_B$. Let's infer $p_A$, $p_B$, *and* $\text{delta} = p_A - p_B$, all at once. We can do this using PyMC's deterministic variables. (We'll assume for this exercise that $p_B = 0.04$, so $\text{delta} = 0.01$, $N_B = 200$ (signifcantly less than $N_A$) and we will simulate site B's data like we did for site A's data )

```
import pymc as mc
figsize(12, 4)

#these two quantities are unknown to us.
true_p_A = 0.05
true_p_B = 0.04

#notice the unequal sample sizes -- no problem in Bayesian analysis.
N_A = 1500
N_B = 750

#generate some observations
observations_A = mc.rbernoulli(true_p_A, N_A)
observations_B = mc.rbernoulli(true_p_B, N_B)
print "Obs from Site A: ", observations_A[:30].astype(int), "..."
print "Obs from Site B: ", observations_B[:30].astype(int), "..."
```

    Obs from Site A:  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0] ...
    Obs from Site B:  [0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0] ...

```
print observations_A.mean()
print observations_B.mean()
```

    0.0413333333333
    0.0453333333333

```
# Set up the pymc model. Again assume Uniform priors for p_A and p_B.
p_A = mc.Uniform("p_A", 0, 1)
p_B = mc.Uniform("p_B", 0, 1)

# Define the deterministic delta function. This is our unknown of interest.
@mc.deterministic
def delta(p_A=p_A, p_B=p_B):
    return p_A - p_B

# Set of observations, in this case we have two observation datasets.
obs_A = mc.Bernoulli("obs_A", p_A, value=observations_A, observed=True)
obs_B = mc.Bernoulli("obs_B", p_B, value=observations_B, observed=True)

# To be explained in chapter 3.
mcmc = mc.MCMC([p_A, p_B, delta, obs_A, obs_B])
mcmc.sample(20000, 1000)
```

    [****************100%******************]  20000 of 20000 complete

Below we plot the posterior distributions for the three unknowns:

```
p_A_samples = mcmc.trace("p_A")[:]
p_B_samples = mcmc.trace("p_B")[:]
delta_samples = mcmc.trace("delta")[:]
```

```
figsize(12.5, 10)

#histogram of posteriors

ax = plt.subplot(311)

plt.xlim(0, .1)
plt.hist(p_A_samples, histtype='stepfilled', bins=25, alpha=0.85,
         label="posterior of $p_A$", color="#A60628", normed=True)
plt.vlines(true_p_A, 0, 80, linestyle="--", label="true $p_A$ (unknown)")
plt.legend(loc="upper right")
plt.title("Posterior distributions of $p_A$, $p_B$, and delta unknowns")

ax = plt.subplot(312)

plt.xlim(0, .1)
plt.hist(p_B_samples, histtype='stepfilled', bins=25, alpha=0.85,
         label="posterior of $p_B$", color="#467821", normed=True)
plt.vlines(true_p_B, 0, 80, linestyle="--", label="true $p_B$ (unknown)")
plt.legend(loc="upper right")

ax = plt.subplot(313)
plt.hist(delta_samples, histtype='stepfilled', bins=30, alpha=0.85,
         label="posterior of delta", color="#7A68A6", normed=True)
plt.vlines(true_p_A - true_p_B, 0, 60, linestyle="--",
           label="true delta (unknown)")
plt.vlines(0, 0, 60, color="black", alpha=0.2)
plt.legend(loc="upper right")
```

Notice that as a result of `N_B < N_A`, i.e. we have less data from site B, our posterior distribution of $p_B$ is fatter, implying we are less certain about the true value of $p_B$ than we are of $p_A$.

With respect to the posterior distribution of $\text{delta}$, we can see that the majority of the distribution is above $\text{delta}=0$ =0$, implying there site A's response is likely better than site B's response. The probability this inference is incorrect is easily computable:

```
# Count the number of samples less than 0, i.e. the area under the curve
# before 0, represent the probability that site A is worse than site B.
print "Probability site A is WORSE than site B: %.3f" % \
    (delta_samples < 0).mean()

print "Probability site A is BETTER than site B: %.3f" % \
    (delta_samples > 0).mean()
```

    Probability site A is WORSE than site B: 0.709
    Probability site A is BETTER than site B: 0.291

If this probability is too high for comfortable decision-making, we can perform more trials on site B (as site B has less samples to begin with, each additional data point for site B contributes more inferential "power" than each additional data point for site A).

Try playing with the parameters `true_p_A`, `true_p_B`, `N_A`, and `N_B`, to see what the posterior of $\text{delta}$ looks like. Notice in all this, the difference in sample sizes between site A and site B was never mentioned: it naturally fits into Bayesian analysis.

I hope the readers feel this style of A/B testing is more natural than hypothesis testing, which the latter has probably confused more than helped practitioners. Later in this book, we will see two extensions of this model: the first to help dynamically adjust for bad sites, and the second will improve the speed of this computation by reducing the analysis to a single equation.

## An algorithm for human deceit

Social data has an additional layer of interest as people are not always honest with responses, which adds a further complication into inference. For example, simply asking individuals "Have you ever cheated on a test?" will surely contain some rate of dishonesty. What you can say for certain is that the true rate is less than your observed rate (assuming individuals lie *only* about *not cheating*; I cannot imagine one who would admit "Yes" to cheating when in fact they hadn't cheated).

To present an elegant solution to circumventing this dishonesty problem, and to demonstrate Bayesian modeling, we first need to introduce the binomial distribution.

### The Binomial Distribution

The binomial distribution is one of the most popular distributions, mostly because of its simplicity and usefulness. Unlike the other distributions we have encountered thus far in the book, the binomial distribution has 2 parameters: $N$, a positive integer representing $N$ trials or number of instances of potential events, and $p$, the probability of an event occurring in a single trial. Like the Poisson distribution, it is a discrete distribution, but unlike the Poisson distribution, it only weighs integers from $0$ to $N$. The mass distribution looks like:

$$P( X = k ) =  {{N}\choose{k}}  p^k(1-p)^{N-k}$$

If $X$ is a binomial random variable with parameters $p$ and $N$, denoted $X \sim \text{Bin}(N,p)$, then $X$ is the number of events that occurred in the $N$ trials (obviously $0 \le X \le N$). The larger $p$ is (while still remaining between 0 and 1), the more events are likely to occur. The expected value of a binomial is equal to $Np$. Below we plot the mass probability distribution for varying parameters.

```
figsize(12.5, 4)

import scipy.stats as stats
binomial = stats.binom

parameters = [(10, .4), (10, .9)]
colors = ["#348ABD", "#A60628"]

for i in range(2):
    N, p = parameters[i]
    _x = np.arange(N + 1)
    plt.bar(_x - 0.5, binomial.pmf(_x, N, p), color=colors[i],
            edgecolor=colors[i],
            alpha=0.6,
            label="$N$: %d, $p$: %.1f" % (N, p),
            linewidth=3)

plt.legend(loc="upper left")
plt.xlim(0, 10.5)
plt.xlabel("$k$")
plt.ylabel("$P(X = k)$")
plt.title("Probability mass distributions of binomial random variables")
```

The special case when $N = 1$ corresponds to the Bernoulli distribution. There is another connection between Bernoulli and Binomial random variables. If we have $X_1, X_2, ... , X_N$ Bernoulli random variables with the same $p$, then $Z = X_1 + X_2 + ... + X_N \sim \text{Binomial}(N, p )$.

The expected value of a Bernoulli random variable is $p$. This can be seen by noting the more general Binomial random variable has expected value $Np$ and setting $N=1$.

##### Example: Cheating among students

We will use the binomial distribution to determine the frequency of students cheating during an exam. If we let $N$ be the total number of students who took the exam, and assuming each student is interviewed post-exam (answering without consequence), we will receive integer $X$ "Yes I did cheat" answers. We then find the posterior distribution of $p$, given $N$, some specified prior on $p$, and observed data $X$.

This is a completely absurd model. No student, even with a free-pass against punishment, would admit to cheating. What we need is a better *algorithm* to ask students if they had cheated. Ideally the algorithm should encourage individuals to be honest while preserving privacy. The following proposed algorithm is a solution I greatly admire for its ingenuity and effectiveness:

> In the interview process for each student, the student flips a coin, hidden from the interviewer. The student agrees to answer honestly if the coin comes up heads. Otherwise, if the coin comes up tails, the student (secretly) flips the coin again, and answers "Yes, I did cheat" if the coin flip lands heads, and "No, I did not cheat", if the coin flip lands tails. This way, the interviewer does not know if a "Yes" was the result of a guilty plea, or a Heads on a second coin toss. Thus privacy is preserved and the researchers receive honest answers.

I call this the Privacy Algorithm. One could of course argue that the interviewers are still receiving false data since some *Yes*'s are not confessions but instead randomness, but an alternative perspective is that the researchers are discarding approximately half of their original dataset since half of the responses will be noise. But they have gained a systematic data generation process that can be modeled. Furthermore, they do not have to incorporate (perhaps somewhat naively) the possibility of deceitful answers. We can use PyMC to dig through this noisy model, and find a posterior distribution for the true frequency of liars.

Suppose 100 students are being surveyed for cheating, and we wish to find $p$, the proportion of cheaters. There are a few ways we can model this in PyMC. I'll demonstrate the most explicit way, and later show a simplified version. Both versions arrive at the same inference. In our data-generation model, we sample $p$, the true proportion of cheaters, from a prior. Since we are quite ignorant about $p$, we will assign it a $\text{Uniform}(0,1)$ prior.

```
import pymc as mc

N = 100
p = mc.Uniform("freq_cheating", 0, 1)
```

Again, thinking of our data-generation model, we assign Bernoulli random variables to the 100 students: 1 implies they cheated and 0 implies they did not.

```
true_answers = mc.Bernoulli("truths", p, size=N)
```

If we carry out the algorithm, the next step that occurs is the first coin-flip each student makes. This can be modeled again by sampling 100 Bernoulli random variables with $p=1/2$: denote a 1 as a *Heads* and 0 a *Tails*.

```
first_coin_flips = mc.Bernoulli("first_flips", 0.5, size=N)
print first_coin_flips.value
```

    [ True False  True False False False False  True  True  True  True  True
      True False  True  True  True False False False  True  True  True False
     False False  True False False  True  True False False  True  True False
     False  True  True  True  True False  True False False False  True False
      True False False False False False  True  True  True False False False
      True  True  True False  True  True False  True  True  True False False
     False  True  True False False  True  True False False False False  True
     False  True False False  True False  True  True  True False False  True
     False  True False False]

Although *not everyone* flips a second time, we can still model the possible realization of second coin-flips:

```
second_coin_flips = mc.Bernoulli("second_flips", 0.5, size=N)
```

Using these variables, we can return a possible realization of the *observed proportion* of "Yes" responses. We do this using a PyMC `deterministic` variable:

```
@mc.deterministic
def observed_proportion(t_a=true_answers,
                        fc=first_coin_flips,
                        sc=second_coin_flips):

    observed = fc*t_a + (1-fc)*sc
    return observed.sum() / float(N)
```

The line `fc*t_a + (1-fc)*sc` contains the heart of the Privacy algorithm. Elements in this array are 1 *if and only if* i) the first toss is heads and the student cheated or ii) the first toss is tails, and the second is heads, and are 0 else. Finally, the last line sums this vector and divides by `float(N)`, produces a proportion.

```
observed_proportion.value
```

    0.47999999999999998

Next we need a dataset. After performing our coin-flipped interviews the researchers received 35 "Yes" responses. To put this into a relative perspective, if there truly were no cheaters, we should expect to see on average 1/4 of all responses being a "Yes" (half chance of having first coin land Tails, and another half chance of having second coin land Heads), so about 25 responses in a cheat-free world. On the other hand, if *all students cheated*, we should expected to see on approximately 3/4 of all response be "Yes".

The researchers observe a Binomial random variable, with `N = 100` and `p = observed_proportion` with `value = 35`:

```
X = 35

observations = mc.Binomial("obs", N, observed_proportion, observed=True,
                           value=X)
```

Below we add all the variables of interest to a `Model` container and run our black-box algorithm over the model.

```
model = mc.Model([p, true_answers, first_coin_flips,
                  second_coin_flips, observed_proportion, observations])

# To be explained in Chapter 3!
mcmc = mc.MCMC(model)
mcmc.sample(40000, 15000)
```

    [****************100%******************]  120000 of 120000 complete

```
figsize(12.5, 3)
p_trace = mcmc.trace("freq_cheating")[:]
plt.hist(p_trace, histtype="stepfilled", normed=True, alpha=0.85, bins=30,
         label="posterior distribution", color="#348ABD")
plt.vlines([.05, .35], [0, 0], [5, 5], alpha=0.3)
plt.xlim(0, 1)
plt.legend()
```

With regards to the above plot, we are still pretty uncertain about what the true frequency of cheaters might be, but we have narrowed it down to a range between 0.05 to 0.35 (marked by the solid lines). This is pretty good, as *a priori* we had no idea how many students might have cheated (hence the uniform distribution for our prior). On the other hand, it is also pretty bad since there is a .3 length window the true value most likely lives in. Have we even gained anything, or are we still too uncertain about the true frequency?

I would argue, yes, we have discovered something. It is implausible, according to our posterior, that there are *no cheaters*, i.e. the posterior assigns low probability to $p=0$. Since we started with an uniform prior, treating all values of $p$ as equally plausible, but the data ruled out $p=0$ as a possibility, we can be confident that there were cheaters.

This kind of algorithm can be used to gather private information from users and be *reasonably* confident that the data, though noisy, is truthful.

### Alternative PyMC Model

Given a value for $p$ (which from our god-like position we know), we can find the probability the student will answer yes:

\begin{align}
P(\text{"Yes"}) = & P( \text{Heads on first coin} )P( \text{cheater} ) + P( \text{Tails on first coin} )P( \text{Heads on second coin} ) \\\\
& = \frac{1}{2}p + \frac{1}{2}\frac{1}{2}\\\\
& = \frac{p}{2} + \frac{1}{4}
\end{align}

Thus, knowing $p$ we know the probability a student will respond "Yes". In PyMC, we can create a deterministic function to evaluate the probability of responding "Yes", given $p$:

```
p = mc.Uniform("freq_cheating", 0, 1)

@mc.deterministic
def p_skewed(p=p):
    return 0.5*p + 0.25
```

I could have typed `p_skewed  = 0.5*p + 0.25` instead for a one-liner, as the elementary operations of addition and scalar multiplication will implicitly create a `deterministic` variable, but I wanted to make the deterministic boilerplate explicit for clarity's sake.

If we know the probability of respondents saying "Yes", which is `p_skewed`, and we have $N=100$ students, the number of "Yes" responses is a binomial random variable with parameters `N` and `p_skewed`.

This is were we include our observed 35 "Yes" responses. In the declaration of the `mc.Binomial`, we include `value = 35` and `observed = True`.

```
yes_responses = mc.Binomial("number_cheaters", 100, p_skewed,
                            value=35, observed=True)
```

Below we add all the variables of interest to a `Model` container and run our black-box algorithm over the model.

```
model = mc.Model([yes_responses, p_skewed, p])

# To Be Explained in Chapter 3!
mcmc = mc.MCMC(model)
mcmc.sample(25000, 2500)
```

    [****************100%******************]  25000 of 25000 complete

```
figsize(12.5, 3)
p_trace = mcmc.trace("freq_cheating")[:]
plt.hist(p_trace, histtype="stepfilled", normed=True, alpha=0.85, bins=30,
         label="posterior distribution", color="#348ABD")
plt.vlines([.05, .35], [0, 0], [5, 5], alpha=0.2)
plt.xlim(0, 1)
plt.legend()
```

### More PyMC Tricks

#### Protip: *Lighter* deterministic variables with `Lambda` class

Sometimes writing a deterministic function using the `@mc.deterministic` decorator can seem like a chore, especially for a small function. I have already mentioned that elementary math operations *can* produce deterministic variables implicitly, but what about operations like indexing or slicing? Built-in `Lambda` functions can handle this with the elegance and simplicity required. For example,

    beta = mc.Normal("coefficients", 0, size=(N, 1))
    x = np.random.randn((N, 1))
    linear_combination = mc.Lambda(lambda x=x, beta=beta: np.dot(x.T, beta))

#### Protip: Arrays of PyMC variables
There is no reason why we cannot store multiple heterogeneous PyMC variables in a Numpy array. Just remember to set the `dtype` of the array to `object` upon initialization. For example:

```
N = 10
x = np.empty(N, dtype=object)
for i in range(0, N):
    x[i] = mc.Exponential('x_%i' % i, (i+1)**2)
```

The remainder of this chapter examines some practical examples of PyMC and PyMC modeling:

#####Example: Challenger Space Shuttle Disaster <span id="challenger"/>

On January 28, 1986, the twenty-fifth flight of the U.S. space shuttle program ended in disaster when one of the rocket boosters of the Shuttle Challenger exploded shortly after lift-off, killing all seven crew members. The presidential commission on the accident concluded that it was caused by the failure of an O-ring in a field joint on the rocket booster, and that this failure was due to a faulty design that made the O-ring unacceptably sensitive to a number of factors including outside temperature. Of the previous 24 flights, data were available on failures of O-rings on 23, (one was lost at sea), and these data were discussed on the evening preceding the Challenger launch, but unfortunately only the data corresponding to the 7 flights on which there was a damage incident were considered important and these were thought to show no obvious trend. The data are shown below (see [1]):

```
figsize(12.5, 3.5)
np.set_printoptions(precision=3, suppress=True)
challenger_data = np.genfromtxt("data/challenger_data.csv", skip_header=1,
                                usecols=[1, 2], missing_values="NA",
                                delimiter=",")
#drop the NA values
challenger_data = challenger_data[~np.isnan(challenger_data[:, 1])]

#plot it, as a function of tempature (the first column)
print "Temp (F), O-Ring failure?"
print challenger_data

plt.scatter(challenger_data[:, 0], challenger_data[:, 1], s=75, color="k",
            alpha=0.5)
plt.yticks([0, 1])
plt.ylabel("Damage Incident?")
plt.xlabel("Outside temperature (Fahrenheit)")
plt.title("Defects of the Space Shuttle O-Rings vs temperature")

```

It looks clear that *the probability* of damage incidents occurring increases as the outside temperature decreases. We are interested in modeling the probability here because it does not look like there is a strict cutoff point between temperature and a damage incident occurring. The best we can do is ask "At temperature $t$, what is the probability of a damage incident?". The goal of this example is to answer that question.

We need a function of temperature, call it $p(t)$, that is bounded between 0 and 1 (so as to model a probability) and changes from 1 to 0 as we increase temperature. There are actually many such functions, but the most popular choice is the *logistic function.*

$$p(t) = \frac{1}{ 1 + e^{ \;\beta t } } $$

In this model, $\beta$ is the variable we are uncertain about. Below is the function plotted for $\beta = 1, 3, -5$.

```
figsize(12, 3)

def logistic(x, beta):
    return 1.0 / (1.0 + np.exp(beta * x))

x = np.linspace(-4, 4, 100)
plt.plot(x, logistic(x, 1), label=r"$\beta = 1$")
plt.plot(x, logistic(x, 3), label=r"$\beta = 3$")
plt.plot(x, logistic(x, -5), label=r"$\beta = -5$")
plt.legend()
```

But something is missing. In the plot of the logistic function, the probability changes only near zero, but in our data above the probability changes around 65 to 70. We need to add a *bias* term to our logistic function:

$$p(t) = \frac{1}{ 1 + e^{ \;\beta t + \alpha } } $$

Some plots are below, with differing $\alpha$.

```
def logistic(x, beta, alpha=0):
    return 1.0 / (1.0 + np.exp(np.dot(beta, x) + alpha))

x = np.linspace(-4, 4, 100)

plt.plot(x, logistic(x, 1), label=r"$\beta = 1$", ls="--", lw=1)
plt.plot(x, logistic(x, 3), label=r"$\beta = 3$", ls="--", lw=1)
plt.plot(x, logistic(x, -5), label=r"$\beta = -5$", ls="--", lw=1)

plt.plot(x, logistic(x, 1, 1), label=r"$\beta = 1, \alpha = 1$",
         color="#348ABD")
plt.plot(x, logistic(x, 3, -2), label=r"$\beta = 3, \alpha = -2$",
         color="#A60628")
plt.plot(x, logistic(x, -5, 7), label=r"$\beta = -5, \alpha = 7$",
         color="#7A68A6")

plt.legend(loc="lower left")
```

Adding a constant term $\alpha$ amounts to shifting the curve left or right (hence why it is called a *bias*. )

Let's start modeling this in PyMC. The $\beta, \alpha$ parameters have no reason to be positive, bounded or relatively large, so they are best modeled by a *Normal random variable*, introduced next.

### Normal distributions

A Normal random variable, denoted $X \sim N(\mu, 1/\tau)$, has a distribution with two parameters: the mean, $\mu$, and the *precision*, $\tau$. Those familiar with the Normal distribution already have probably seen $\sigma^2$ instead of $\tau$. They are in fact reciprocals of each other. The change was motivated by simpler mathematical analysis and is an artifact of older Bayesian methods. Just remember: The smaller $\tau$, the larger the spread of the distribution (i.e. we are more uncertain); the larger $\tau$, the tighter the distribution (i.e. we are more certain). Regardless, $\tau$ is always positive.

The probability density function of a $N( \mu, 1/\tau)$ random variable is:

$$ f(x | \mu, \tau) = \sqrt{\frac{\tau}{2\pi}} \exp\left( -\frac{\tau}{2} (x-\mu)^2 \right) $$

We plot some different density functions below.

```
import scipy.stats as stats

nor = stats.norm
x = np.linspace(-8, 7, 150)
mu = (-2, 0, 3)
tau = (.7, 1, 2.8)
colors = ["#348ABD", "#A60628", "#7A68A6"]
parameters = zip(mu, tau, colors)

for _mu, _tau, _color in parameters:
    plt.plot(x, nor.pdf(x, _mu, scale=1./_tau),
             label="$\mu = %d,\;\\tau = %.1f$" % (_mu, _tau), color=_color)
    plt.fill_between(x, nor.pdf(x, _mu, scale=1./_tau), color=_color,
                     alpha=.33)

plt.legend(loc="upper right")
plt.xlabel("$x$")
plt.ylabel("density function at $x$")
plt.title("Probability distribution of three different Normal random \
variables")
```

A Normal random variable can be take on any real number, but the variable is very likely to be relatively close to $\mu$. In fact, the expected value of a Normal is equal to its $\mu$ parameter:

$$ E[ X | \mu, \tau] = \mu$$

and its variance is equal to the inverse of $\tau$:

$$Var( X | \mu, \tau ) = \frac{1}{\tau}$$

Below we continue our modeling of the Challenger space craft:

```
import pymc as mc

temperature = challenger_data[:, 0]
D = challenger_data[:, 1]  # defect or not?

#notice the`value` here. We explain why below.
beta = mc.Normal("beta", 0, 0.001, value=0)
alpha = mc.Normal("alpha", 0, 0.001, value=0)

@mc.deterministic
def p(t=temperature, alpha=alpha, beta=beta):
    return 1.0 / (1. + np.exp(beta*t + alpha))

```

We have our probabilities, but how do we connect them to our observed data? A *Bernoulli* random variable with parameter $p$, denoted $\text{Ber}(p)$, is a random variable that takes value 1 with probability $p$, and 0 else. Thus, our model can look like:

$$ \text{Defect Incident, $D_i$} \sim \text{Ber}( \;p(t_i)\; ), \;\; i=1..N$$

where $p(t)$ is our logistic function and $t_i$ are the temperatures we have observations about. Notice in the above code we had to set the values of `beta` and `alpha` to 0. The reason for this is that if `beta` and `alpha` are very large, they make `p` equal to 1 or 0. Unfortunately, `mc.Bernoulli` does not like probabilities of exactly 0 or 1, though they are mathematically well-defined probabilities. So by setting the coefficient values to `0`, we set the variable `p` to be a reasonable starting value. This has no effect on our results, nor does it mean we are including any additional information in our prior. It is simply a computational caveat in PyMC.

```
p.value
```

    array([ 0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
            0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
            0.5])

```
# connect the probabilities in `p` with our observations through a
# Bernoulli random variable.
observed = mc.Bernoulli("bernoulli_obs", p, value=D, observed=True)

model = mc.Model([observed, beta, alpha])

# Mysterious code to be explained in Chapter 3
map_ = mc.MAP(model)
map_.fit()
mcmc = mc.MCMC(model)
mcmc.sample(120000, 100000, 2)
```

    [****************100%******************]  120000 of 120000 complete

We have trained our model on the observed data, now we can sample values from the posterior. Let's look at the posterior distributions for $\alpha$ and $\beta$:

```
alpha_samples = mcmc.trace('alpha')[:, None]  # best to make them 1d
beta_samples = mcmc.trace('beta')[:, None]

figsize(12.5, 6)

#histogram of the samples:
plt.subplot(211)
plt.title(r"Posterior distributions of the variables $\alpha, \beta$")
plt.hist(beta_samples, histtype='stepfilled', bins=35, alpha=0.85,
         label=r"posterior of $\beta$", color="#7A68A6", normed=True)
plt.legend()

plt.subplot(212)
plt.hist(alpha_samples, histtype='stepfilled', bins=35, alpha=0.85,
         label=r"posterior of $\alpha$", color="#A60628", normed=True)
plt.legend()

```

All samples of $\beta$ are greater than 0. If instead the posterior was centered around 0, we may suspect that $\beta = 0$, implying that temperature has no effect on the probability of defect.

Similarly, all $\alpha$ posterior values are negative and far away from 0, implying that it is correct to believe that $\alpha$ is significantly less than 0.

Regarding the spread of the data, we are very uncertain about what the true parameters might be (though considering the low sample size and the large overlap of defects-to-nondefects this behaviour is perhaps expected).

Next, let's look at the *expected probability* for a specific value of the temperature. That is, we average over all samples from the posterior to get a likely value for $p(t_i)$.

```
t = np.linspace(temperature.min() - 5, temperature.max()+5, 50)[:, None]
p_t = logistic(t.T, beta_samples, alpha_samples)

mean_prob_t = p_t.mean(axis=0)
```

```
figsize(12.5, 4)

plt.plot(t, mean_prob_t, lw=3, label="average posterior \nprobability \
of defect")
plt.plot(t, p_t[0, :], ls="--", label="realization from posterior")
plt.plot(t, p_t[-2, :], ls="--", label="realization from posterior")
plt.scatter(temperature, D, color="k", s=50, alpha=0.5)
plt.title("Posterior expected value of probability of defect; \
plus realizations")
plt.legend(loc="lower left")
plt.ylim(-0.1, 1.1)
plt.xlim(t.min(), t.max())
plt.ylabel("probability")
plt.xlabel("temperature")
```

Above we also plotted two possible realizations of what the actual underlying system might be. Both are equally likely as any other draw. The blue line is what occurs when we average all the 20000 possible dotted lines together.

An interesting question to ask is for what temperatures are we most uncertain about the defect-probability? Below we plot the expected value line **and** the associated 95% intervals for each temperature.

```
from scipy.stats.mstats import mquantiles

# vectorized bottom and top 2.5% quantiles for "confidence interval"
qs = mquantiles(p_t, [0.025, 0.975], axis=0)
plt.fill_between(t[:, 0], *qs, alpha=0.7,
                 color="#7A68A6")

plt.plot(t[:, 0], qs[0], label="95% CI", color="#7A68A6", alpha=0.7)

plt.plot(t, mean_prob_t, lw=1, ls="--", color="k",
         label="average posterior \nprobability of defect")

plt.xlim(t.min(), t.max())
plt.ylim(-0.02, 1.02)
plt.legend(loc="lower left")
plt.scatter(temperature, D, color="k", s=50, alpha=0.5)
plt.xlabel("temp, $t$")

plt.ylabel("probability estimate")
plt.title("Posterior probability estimates given temp. $t$")
```

The *95% credible interval*, or 95% CI, painted in purple, represents the interval, for each temperature, that contains 95% of the distribution. For example, at 65 degrees, we can be 95% sure that the probability of defect lies between 0.25 and 0.75.

More generally, we can see that as the temperature nears 60 degrees, the CI's spread out over [0,1] quickly. As we pass 70 degrees, the CI's tighten again. This can give us insight about how to proceed next: we should probably test more O-rings around 60-65 temperature to get a better estimate of probabilities in that range. Similarly, when reporting to scientists your estimates, you should be very cautious about simply telling them the expected probability, as we can see this does not reflect how *wide* the posterior distribution is.

### What about the day of the Challenger disaster?

On the day of the Challenger disaster, the outside temperature was 31 degrees Fahrenheit. What is the posterior distribution of a defect occurring,  given this temperature? The distribution is plotted below. It looks almost guaranteed that the Challenger was going to be subject to defective O-rings.

```
figsize(12.5, 2.5)

prob_31 = logistic(31, beta_samples, alpha_samples)

plt.xlim(0.995, 1)
plt.hist(prob_31, bins=1000, normed=True, histtype='stepfilled')
plt.title("Posterior distribution of probability of defect, given $t = 31$")
plt.xlabel("probability of defect occurring in O-ring")

```

### Is our model appropriate?

The skeptical reader will say "You deliberately chose the logistic function for $p(t)$ and the specific priors. Perhaps other functions or priors will give different results. How do I know I have chosen a good model?" This is absolutely true. To consider an extreme situation, what if I had chosen the function $p(t) = 1,\; \forall t$, which guarantees a defect always occurring: I would have again predicted disaster on January 28th. Yet this is clearly a poorly chosen model. On the other hand, if I did choose the logistic function for $p(t)$, but specified all my priors to be very tight around 0, likely we would have very different posterior distributions. How do we know our model is an expression of the data? This encourages us to measure the model's **goodness of fit**.

We can think: *how can we test whether our model is a bad fit?* An idea is to compare observed data (which if we recall is a *fixed* stochastic variable) with artificial dataset which we can simulate. The rationale is that if the simulated dataset does not appear similar, statistically, to the observed dataset, then likely our model is not accurately represented the observed data.

Previously in this Chapter, we simulated artificial dataset for the SMS example. To do this, we sampled values from the priors. We saw how varied the resulting datasets looked like, and rarely did they mimic our observed dataset. In the current example,  we should sample from the *posterior* distributions to create *very plausible datasets*. Luckily, our Bayesian framework makes this very easy. We only need to create a new `Stochastic` variable, that is exactly the same as our variable that stored the observations, but minus the observations themselves. If you recall, our `Stochastic` variable that stored our observed data was:

    observed = mc.Bernoulli( "bernoulli_obs", p, value=D, observed=True)

Hence we create:

    simulated_data = mc.Bernoulli("simulation_data", p)

Let's simulate 10 000:

```
simulated = mc.Bernoulli("bernoulli_sim", p)
N = 10000

mcmc = mc.MCMC([simulated, alpha, beta, observed])
mcmc.sample(N)
```

    [****************100%******************]  10000 of 10000 complete

```
figsize(12.5, 5)

simulations = mcmc.trace("bernoulli_sim")[:]
print simulations.shape

plt.title("Simulated dataset using posterior parameters")
figsize(12.5, 6)
for i in range(4):
    ax = subplot(4, 1, i+1)
    plt.scatter(temperature, simulations[1000*i, :], color="k",
                s=50, alpha=0.6)

```

Note that the above plots are different (if you can think of a cleaner way to present this, please send a pull request and answer [here](http://stats.stackexchange.com/questions/53078/how-to-visualize-bayesian-goodness-of-fit-for-logistic-regression)!).

We wish to assess how good our model is. "Good" is a subjective term of course, so results must be relative to other models.

We will be doing this graphically as well, which may seem like an even less objective method. The alternative is to use *Bayesian p-values*. These are still subjective, as the proper cutoff between good and bad is arbitrary. Gelman emphasises that the graphical tests are more illuminating [7] than p-value tests. We agree.

The following graphical test is a novel data-viz approach to logistic regression. The plots are called *separation plots*[8]. For a suite of models we wish to compare, each model is plotted on an individual separation plot. I leave most of the technical details about separation plots to the very accessible [original paper](http://mdwardlab.com/sites/default/files/GreenhillWardSacks.pdf), but I'll summarize their use here.

For each model, we calculate the proportion of times the posterior simulation proposed a value of 1 for a particular temperature, i.e. compute $P( \;\text{Defect} = 1 | t, \alpha, \beta )$ by averaging. This gives us the posterior probability of a defect at each data point in our dataset. For example, for the model we used above:

```
posterior_probability = simulations.mean(axis=0)
print "posterior prob of defect | realized defect "
for i in range(len(D)):
    print "%.2f                     |   %d" % (posterior_probability[i], D[i])
```

    posterior prob of defect | realized defect
    0.51                     |   0
    0.19                     |   1
    0.26                     |   0
    0.34                     |   0
    0.42                     |   0
    0.11                     |   0
    0.08                     |   0
    0.20                     |   0
    0.95                     |   1
    0.75                     |   1
    0.19                     |   1
    0.02                     |   0
    0.42                     |   0
    0.99                     |   1
    0.42                     |   0
    0.04                     |   0
    0.19                     |   0
    0.00                     |   0
    0.03                     |   0
    0.01                     |   0
    0.04                     |   1
    0.03                     |   0
    0.94                     |   1

Next we sort each column by the posterior probabilities:

```
ix = np.argsort(posterior_probability)
print "probb | defect "
for i in range(len(D)):
    print "%.2f  |   %d" % (posterior_probability[ix[i]], D[ix[i]])
```

    probb | defect
    0.00  |   0
    0.01  |   0
    0.02  |   0
    0.03  |   0
    0.03  |   0
    0.04  |   1
    0.04  |   0
    0.08  |   0
    0.11  |   0
    0.19  |   1
    0.19  |   0
    0.19  |   1
    0.20  |   0
    0.26  |   0
    0.34  |   0
    0.42  |   0
    0.42  |   0
    0.42  |   0
    0.51  |   0
    0.75  |   1
    0.94  |   1
    0.95  |   1
    0.99  |   1

We can present the above data better in a figure: I've wrapped this up into a `separation_plot` function.

```
from separation_plot import separation_plot

figsize(11., 1.5)
separation_plot(posterior_probability, D)
```

The snaking-line is the sorted probabilities, blue bars denote defects, and empty space (or grey bars for the optimistic readers) denote non-defects.  As the probability rises, we see more and more defects occur. On the right hand side, the plot suggests that as the posterior probability is large (line close to 1), then more defects are realized. This is good behaviour. Ideally, all the blue bars *should* be close to the right-hand side, and deviations from this reflect missed predictions.

The black vertical line is the expected number of defects we should observe, given this model. This allows the user to see how the total number of events predicted by the model compares to the actual number of events in the data.

It is much more informative to compare this to separation plots for other models. Below we compare our model (top) versus three others:

1. the perfect model, which predicts the posterior probability to be equal 1 if a defect did occur.
2. a completely random model, which predicts random probabilities regardless of temperature.
3. a constant model:  where $P(D = 1 \; | \; t) = c, \;\; \forall t$. The best choice for $c$ is the observed frequency of defects, in this case 7/23.

```
figsize(11., 1.25)

# Our temperature-dependent model
separation_plot(posterior_probability, D)
plt.title("Temperature-dependent model")

# Perfect model
# i.e. the probability of defect is equal to if a defect occurred or not.
p = D
separation_plot(p, D)
plt.title("Perfect model")

# random predictions
p = np.random.rand(23)
separation_plot(p, D)
plt.title("Random model")

# constant model
constant_prob = 7./23*np.ones(23)
separation_plot(constant_prob, D)
plt.title("Constant-prediction model")
```

In the random model, we can see that as the probability increases there is no clustering of defects to the right-hand side. Similarly for the constant model.

The perfect model, the probability line is not well shown, as it is stuck to the bottom and top of the figure. Of course the perfect model is only for demonstration, and we cannot infer any scientific inference from it.

##### Exercises

1\. Try putting in extreme values for our observations in the cheating example. What happens if we observe 25 affirmative responses? 10? 50?

2\. Try plotting $\alpha$ samples versus $\beta$ samples.  Why might the resulting plot look like this?

```
#type your code here.
figsize(12.5, 4)

plt.scatter(alpha_samples, beta_samples, alpha=0.1)
plt.title("Why does the plot look like this?")
plt.xlabel(r"$\alpha$")
plt.ylabel(r"$\beta$")
```

### References

-  [1] Dalal, Fowlkes and Hoadley (1989),JASA, 84, 945-957.
-  [2] German Rodriguez. Datasets. In WWS509. Retrieved 30/01/2013, from http://data.princeton.edu/wws509/datasets/#smoking.
-  [3] McLeish, Don, and Cyntha Struthers. STATISTICS 450/850 Estimation and Hypothesis Testing. Winter 2012. Waterloo, Ontario: 2012. Print.
-  [4] Fonnesbeck, Christopher. "Building Models." PyMC-Devs. N.p., n.d. Web. 26 Feb 2013. <http://pymc-devs.github.com/pymc/modelbuilding.html>.
- [5] Cronin, Beau. "Why Probabilistic Programming Matters." 24 Mar 2013. Google, Online Posting to Google . Web. 24 Mar. 2013. <https://plus.google.com/u/0/107971134877020469960/posts/KpeRdJKR6Z1>.
- [6] S.P. Brooks, E.A. Catchpole, and B.J.T. Morgan. Bayesian animal survival estimation. Statistical Science, 15: 357–376, 2000
- [7] Gelman, Andrew. "Philosophy and the practice of Bayesian statistics." British Journal of Mathematical and Statistical Psychology. (2012): n. page. Web. 2 Apr. 2013.
- [8] Greenhill, Brian, Michael D. Ward, and Audrey Sacks. "The Separation Plot: A New Visual Method for Evaluating the Fit of Binary Models." American Journal of Political Science. 55.No.4 (2011): n. page. Web. 2 Apr. 2013.

```
from IPython.core.display import HTML

def css_styling():
    styles = open("../styles/custom.css", "r").read()
    return HTML(styles)
css_styling()
```

<style>
    @font-face {
        font-family: "Computer Modern";
        src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');
    }
    div.cell{
        width:800px;
        margin-left:16% !important;
        margin-right:auto;
    }
    h1 {
        font-family: Helvetica, serif;
    }
    h4{
        margin-top:12px;
        margin-bottom: 3px;
       }
    div.text_cell_render{
        font-family: Computer Modern, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
        line-height: 145%;
        font-size: 130%;
        width:800px;
        margin-left:auto;
        margin-right:auto;
    }
    .CodeMirror{
            font-family: "Source Code Pro", source-code-pro,Consolas, monospace;
    }
    .prompt{
        display: None;
    }
    .text_cell_render h5 {
        font-weight: 300;
        font-size: 22pt;
        color: #4057A1;
        font-style: italic;
        margin-bottom: .5em;
        margin-top: 0.5em;
        display: block;
    }

    .warning{
        color: rgb( 240, 20, 20 )
        }
</style>

```

```


# INF-285 / ILI-285
## Desafío 5 v1.1
### SCT 2020-1

## Instrucciones
* El desafío es individual, por lo cual se considera que todo el código entregado es de elaboración propia y no ha sido compartido de forma alguna.
* Las consultas sobre el desafío se deben realizar por medio de la plataforma Aula. **No está permitido publicar código en consultas de Aula**.
* El desafío debe ser realizado en Jupyter Notebook (Python3) utilizando este archivo como base.
* Debe utilizar arreglos de NumPy junto a las funciones y bibliotecas que se ponen a disposición en este archivo.
* Se debe respetar las firmas de las funciones, sus parámetros y retornos. Para eso se incluye un detalle de cada función tanto de las entregadas como las que deberá implementar. En caso de no seguir estas instrucciones, el desafío no podrá evaluarse.
* Se evaluará la correcta utilización de librerias NumPy, SciPy, entre otras, así como la correcta implementación de algoritmos de forma vectorizada.
* Evitar la impresión de mensajes salvo que se solicite en el enunciado.
* El archivo de entrega debe denominarse **ROL-desafio-numero.ipynb**. **De no respetarse este formato existirá un descuento de 50 puntos**.
* La fecha de entrega es el **4 de agosto a las 12:00 hrs**.

```python
import numpy as np
import scipy.linalg as spla
import matplotlib.pyplot as plt
```

## Introducción

El objetivo de este desafío es evaluar su capacidad de "analista de datos" con un problema de regresión.
Suponga que tiene el conjunto de datos $\{(x_1,y_1), (x_2, y_2), \dots, (x_m, y_m)\}$ para los cuales necesitamos ajustar un modelo del tipo $f(x, \boldsymbol{\beta})$ con $\boldsymbol{\beta}=\beta_1, \beta_2, \dots, \beta_n$, los parámetros del modelo. Considere por ejemplo que un modelo lineal sería:

\begin{equation}
    f(x, \boldsymbol{\beta})=\beta_1 + \beta_2\,x.
\end{equation}

Los datos provienen de algun instrumento de medición desconocido y se encuentran en la siguiente dirección: **[datos](https://github.com/sct-utfsm/INF-285/tree/master/desafios/data)**

## Tarea

La tarea es utilizar su creatividad para construir un modelo que se ajuste a los datos entregados. Para explorar visualmente los datos y posteriormente analizar su ajuste se pone a disposición la función ```plot(x_i, y_i, y_fit)``` que recibe los datos $x_i$, $y_i$, y en el caso que tenga su ajuste $y_{fit}$.

```python
def plot(x_i, y_i, y_fit=None):
    """
    Plot data

    Parameters
    -----------
    x_i     : (m, ) array
             Data x_i to fit
    y_i     : (m, ) array
             Data y_i to fit
    y_fit   : (m, ) array or None
             Model output

    Returns
    -------
    None
    """
    plt.figure(figsize=(12, 6))
    plt.plot(x_i, y_i, 'r.', label='Data')
    if y_fit is not None:
        plt.plot(x_i, y_fit, 'b-', label='Model')
    plt.xlabel(r'$x$')
    plt.ylabel(r'$y$')
    plt.grid(True)
    plt.legend()
    plt.show()
```

### Evaluación

Para evaluar su modelo, utilizaremos el *error cuadrático relativo* definido como:
\begin{equation}
    SE_{rel} = \frac{\displaystyle \sum_{i=1}^m (y_i - f(x_i, \boldsymbol{\beta}))^2}{\displaystyle \sum_{i=1}^{m}y_i^2} = \frac{\displaystyle\sum_{i=1}^m (y_i - \hat{y}_i)^2}{\displaystyle \sum_{i=1}^{m}y_i^2}.
\end{equation}

Implemente la función ```SErel(y_i, y_fit)``` que reciba los valores de $y_i$ y los datos ajustados por su modelo $\hat{y}_i$ y retorne el valor del error relativo indicado.

```python
def SErel(y_i, y_fit):
    """
    Relative SE computation

    Parameters
    -----------
    y_i     : (m, ) array
             Data y_i to fit
    y_fit   : (n, ) array
             Model output

    Returns
    -------
    SE_rel  : float
             Relative SE value
    """
    ...
```

Su nota se calculará utilizando el valor obtenido por el $SE_{rel}$ asociado a su modelo de la siguiente manera:

\begin{equation}
    nota(SE_{rel}) =
    \begin{cases}
        100 & \text{si} \quad 0.08 \leq SE_{rel} \leq 0.15 \\
        round\left(\frac{835 - 900\,SE_{rel} }{7}\right) & \text{si} \quad SE_{rel} > 0.15\\
        0 & \text{si} \quad SE_{rel} < 0.08
    \end{cases}
\end{equation}

Se considera un ajuste deficiente si utiliza simplemente una función lineal para el ajuste, así que entre mejor sea su modelo, mejor será su nota ;). En el caso que el *error relativo* sea muy pequeño se puede considerar casi una interpolación, por lo tanto se evalúa con nota mínima. Puede utilizar la función adjunta para analizar la capacidad de su modelo.

```python
def nota(se_rel):
    if se_rel < 0.08: # Are you sure it's not an interpolation?
        return 0
    elif 0.08 <= se_rel <= 0.15:
        return 100
    else:
        return np.round((835 - 900 * se_rel) / 7)
```

### Ajuste

Como hemos revisado en clases, podemos ajustar los parámetros de un modelo utilizando mínimos cuadrados. Para esto tenemos dos opciones:

####  Resolver las ecuaciones normales:

\begin{equation}
    A^*\, A \mathbf{x} = A^*\, \mathbf{b}
\end{equation}

#### Utilizar $QR$:

1. Aplicar $QR$ reducido para obtener $A = \hat{Q}\,\hat{R}$
2. Resolver $\hat{R}\,\mathbf{x}=\hat{Q}^*\,\mathbf{b}$

Para ambos casos recuerde que $A$ dependerá del modelo que usted proponga, $\mathbf{x}=\boldsymbol{\beta}$ y $\mathbf{b}$ es el vector asociado a $y_i$. Implemente la función ```modelFit(A, b, method)``` que reciba $A$, $\mathbf{b}$ y ```method``` para indicar si se utilizarán las *ecuaciones normales* o $QR$, y retorne los parámetros $\boldsymbol{\beta}$ del modelo que está ajustando.

```python
def modelFit(A, b, method):
    """
    Curve fitting

    Parameters
    -----------
    A        : (m, n) array
              A matrix of overdetermined system of equations
    b        : (m, ) array
              b vector of overdetermined system of equations
    method   : string {'NE', 'QR'}
              Solving method with Normal Equations 'NE' or QR factorization'QR'

    Returns
    -------
    beta     : (n, ) array
              Model parameters
    """
    ...
```

----

## Desarrollo

### Cargar datos

```python
# Load data
x_i = ...
y_i = ...
```

### Modelo

Como ejemplo se incluye un modelo lineal... Confiamos en que usted será más creativo en la construcción de su propuesta. Puede hacer uso de combinaciones de modelos vistos en clases, por ejemplo:
\begin{equation}
    f(x, \beta_1, \beta_2, \dots) = \beta_1 + \beta_2\,x^2 + \beta_2\,x\,\cos(2\pi x) + \dots
\end{equation}

```python
# Modify this function. It is only an example
def model(b1, b2, x):
    return b1 + b2 * x
```

Construcción de $A$ y $\mathbf{b}$

```python
A = ...
b = ...
```

Obtención de parámetros...

```python
beta = ...
```

```python
y_fit = ...
```

```python
# plot(x_i, y_i, y_fit)
```

Evaluación de su propuesta

```python
se = 0
```

```python
print(se, nota(se))
```