# Unweighted and Weighted Means

```python
import numpy as np
import scipy.stats as stats
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.patches as mpathces
#from matplotlib.pyplot import figure, show
#from matplotlib.ticker import MaxNLocator
```

## Maximum Likelihood Estimator motivated "derivations"

### Unweighted Means

If we make $n$ idential statistically independent (isi) measurments of a random varaible $x$, such that the measurements collected form data $\vec{x} = \left\{x_i, \cdots, x_n\right\}$, from a Gaussian (Normal) distribution,

\begin{equation}
L\left(\vec{x}; \vec{\theta}\right) = \prod_{i=1}^{n} f(x_i; \mu, \sigma) = \frac{1}{(2\pi)^{n/2} \sigma^{n}} \exp\left(-\frac{1}{2\sigma^2} \sum_{i=1}^{n} \left(x_i - \mu\right)^2 \right)
\end{equation}

then

\begin{equation}
-\ln L = \frac{n}{2} \ln\left(2\pi\right) + n \ln \sigma + \frac{1}{2\sigma^2} \sum_{i=1}^{n}\left(x_i - \mu\right)^2
\end{equation}

and so $L$ is maximized with respect to a variable $\alpha$ when $-\ln L$ is minimized,

\begin{equation*}
\frac{\partial \left(-\ln L\right)}{\partial \alpha} = 0.
\end{equation*}

Thus, $L$ is maximized when

\begin{equation*}
\frac{\partial \left(-\ln L\right)}{\partial \mu} = -\frac{1}{\sigma^2} \sum_{i=1}^{n}\left(x_i - \mu\right) = 0,
\end{equation*}

which occurs for

\begin{equation*}
\sum_{i=1}^{n} x_i = n \mu,
\end{equation*}

such that the best estimate for true parameter $\mu$ is

\begin{equation}
\boxed{\hat{\mu} = \frac{1}{n} \sum_{i=1}^{n} x_i = \bar{x}\,}\,,
\end{equation}

and $L$ is maximized when

\begin{equation*}
\frac{\partial \left(-\ln L\right)}{\partial \sigma} = \frac{n}{\sigma} - \frac{1}{\sigma^3} \sum_{i=1}^{n} \left(x_i - \mu\right) = 0,
\end{equation*}

which occurs for

\begin{equation*}
n\sigma^2 = \sum_{i=1}^{n} \left(x_i - \mu\right)^2,
\end{equation*}

which is

\begin{equation*}
\sigma = \sqrt{\frac{1}{n}\sum_{i=1}^{n} \left(x_i - \mu\right)^2}.
\end{equation*}

However, $\mu$ is an unknown true parameter, and the best estimate of it is $\hat{\mu}$, which is in no
manner required to be equal to $\mu$. Thus, the best estimate of $\sigma$ is

\begin{equation}
\boxed{\hat{\sigma}_{\hat{\mu}} = \sqrt{\frac{1}{n}\sum_{i=1}^{n} \left(x_i - \hat{\mu}\right)^2} = \sqrt{\frac{1}{n}\sum_{i=1}^{n} \left(x_i - \bar{x}\,\right)^2}\,}\,.
\end{equation}

If the seperation from the mean of each observation, $\left(x_i - \bar{x}\right) = \delta x = \text{constant}$, are the same then the uncertainity on the mean is found to be

\begin{equation*}
\sigma_{\hat{\mu}} = \frac{\delta x}{\sqrt{n}},
\end{equation*}

which is often refered to as the "standard error".

---
So, for a population of measurements sampled from a distribution, it can be said that the sample mean is
$$\mu = \frac{1}{n} \sum_{i=1}^{n} x_i = \bar{x},$$

and the standard deviation of the sample is

\begin{equation*}
\sigma = \sqrt{\frac{1}{n}\sum_{i=1}^{n} \left(x_i - \bar{x}\,\right)^2}.
\end{equation*}

---

### Weighted Means

Assume that $n$ individual measurments $x_i$ are spread around (unknown) true value $\theta$ according to a Gaussian distribution, each with known width $\sigma_i$.

This then leads to the likelihood function

\begin{equation*}
L(\theta) = \prod_{i=1}^{n} \frac{1}{\sqrt{2\pi}\sigma_i} \exp\left(-\frac{\left(x_i - \theta\right)^2}{2\sigma_i^2} \right)
\end{equation*}

and so negative log-likelihood

\begin{equation}
-\ln L = \frac{1}{2} \ln\left(2\pi\right) + \ln \sigma_i + \frac{1}{2\sigma_i^2} \sum_{i=1}^{n}\left(x_i - \theta\right)^2.
\end{equation}

As before, $L$ is maximized with respect to a variable $\alpha$ when $-\ln L$ is minimized,

\begin{equation*}
\frac{\partial \left(-\ln L\right)}{\partial \alpha} = 0,
\end{equation*}

and so $L$ is maximized with respect to $\theta$ when

\begin{equation*}
\frac{\partial \left(-\ln L\right)}{\partial \theta} = -\sum_{i=1}^{n} \frac{x_i - \theta}{\sigma_i^2} = 0,
\end{equation*}

which occurs for

\begin{equation*}
\sum_{i=1}^{n} \frac{x_i}{\sigma_i^2} = \theta \sum_{i=1}^{n} \frac{1}{\sigma_i^2},
\end{equation*}

which is

\begin{equation}
\hat{\theta} = \frac{\displaystyle\sum_{i=1}^{n} \frac{x_i}{\sigma_i^2}}{\displaystyle\sum_{i=1}^{n}\frac{1}{\sigma_i^2}}.
\end{equation}

Note that by defining "weights" to be

\begin{equation*}
w_i = \frac{1}{\sigma_1^2},
\end{equation*}

this can be expressed as

\begin{equation}
\boxed{\hat{\theta} = \frac{\displaystyle\sum_{i=1}^{n} w_i\, x_i}{\displaystyle\sum_{i=1}^{n}w_i}},
\end{equation}

making the term "weighted mean" very transparent.

To find the standard deviation on the weighted mean, we first look to the variance, $\sigma^2$. [4]

\begin{align*}
\sigma^2 &= \text{E}\left[\left(\hat{\theta} - \text{E}\left[\hat{\theta}\right]\right)^2\right] \\
    &= \text{E}\left[\left(\frac{\displaystyle\sum_{i=1}^{n} w_i\, x_i}{\displaystyle\sum_{i=1}^{n}w_i} - \text{E}\left[\frac{\displaystyle\sum_{i=1}^{n} w_i\, x_i}{\displaystyle\sum_{i=1}^{n}w_i}\right]\,\right)^2\right] \\
    &= \frac{1}{\displaystyle\left(\sum_{i=1}^{n} w_i\right)^2} \text{E} \left[ \displaystyle\left(\sum_{i=1}^{n} w_i\,x_i\right)^2 - 2 \displaystyle\left(\sum_{i=1}^{n} w_i\,x_i\right) \displaystyle\left(\sum_{i=j}^{n} w_j\, \text{E}\left[x_j\right]\right) + \displaystyle\left(\sum_{i=1}^{n} w_i\, \text{E}\left[x_i\right]\right)^2 \right] \\
    &= \frac{1}{\displaystyle\left(\sum_{i=1}^{n} w_i\right)^2} \text{E} \left[ \sum_{i,j}^{n} w_i\, x_i w_j\, x_j - 2 \sum_{i,j}^{n} w_i\, x_i w_j\, \text{E}\left[x_j\right] + \sum_{i,j}^{n} w_i\, \text{E}\left[x_i\right] w_j\, \text{E}\left[x_j\right] \right] \\
    &= \frac{1}{\displaystyle\left(\sum_{i=1}^{n} w_i\right)^2} \sum_{i,j}^{n} w_i w_j \left( \text{E}\left[ x_i x_j \right] - 2 \text{E}\left[ x_i \right]\text{E}\left[ x_j \right] + \text{E}\left[ x_i \right]\text{E}\left[ x_j \right] \right) \\
    &= \frac{1}{\displaystyle\left(\sum_{i=1}^{n} w_i\right)^2} \sum_{i,j}^{n} w_i w_j \left( \text{E}\left[ x_i x_j \right] - \text{E}\left[ x_i \right]\text{E}\left[ x_j \right] \right) \\
    &= \frac{1}{\displaystyle\left(\sum_{i=1}^{n} w_i\right)^2} \sum_{i,j}^{n} w_i w_j \,\text{Cov}\left( x_i, x_j \right) = \left\{
\begin{array}{ll}
\frac{\displaystyle1}{\displaystyle\left(\sum_{i=1}^{n} w_i\right)^2} \displaystyle\sum_{i}^{n} \left( w_i \sigma_i \right)^2\,, & x_i \text{ and } x_j \text{ statistically independent}, \\
0\,, &\text{ otherwise},
\end{array}
\right. \\
    &= \frac{\displaystyle\sum_{i}^{n} \left( \sigma_i^{-2} \sigma_i \right)^2}{\displaystyle\left(\sum_{i=1}^{n} w_i\right)^2} = \frac{\displaystyle\sum_{i}^{n} w_i}{\displaystyle\left(\sum_{i=1}^{n} w_i\right)^2} \\
    &= \frac{\displaystyle 1}{\displaystyle\sum_{i=1}^{n} w_i}
\end{align*}

Thus, it is seen that the standard deviation on the weighted mean is

\begin{equation}
\boxed{\sigma_{\hat{\theta}} = \sqrt{\frac{\displaystyle 1}{\displaystyle\sum_{i=1}^{n} w_i}} = \left(\displaystyle\sum_{i=1}^{n} \frac{1}{\sigma_i^2}\right)^{-1/2}}\,.
\end{equation}

Notice that in the event that the uncertainties are uniform for each observation, $\sigma_i = \delta x$, the above yields the same result as the unweighted mean. $\checkmark$

After this aside it is worth pointing out that [1] have a very elegant demonstration that

\begin{equation*}
\sigma_{\hat{\theta}} = \left(\frac{\partial^2\left(- \ln L\right)}{\partial\, \theta^2}\right)^{-1/2} = \left(\displaystyle\sum_{i=1}^{n} \frac{1}{\sigma_i^2}\right)^{-1/2}.
\end{equation*}

---
So, the average of $n$ measurements of quantity $\theta$, with individual measurments, $x_i$, Gaussianly distributed about (unknown) true value $\theta$ with known width $\sigma_i$, is the weighted mean

\begin{equation*}
\hat{\theta} = \frac{\displaystyle\sum_{i=1}^{n} w_i\, x_i}{\displaystyle\sum_{i=1}^{n}w_i},
\end{equation*}

with weights $w_i = \sigma_i^{-2}$, with standard deviation on the weighted mean

\begin{equation*}
\sigma_{\hat{\theta}} = \sqrt{\frac{\displaystyle 1}{\displaystyle\sum_{i=1}^{n} w_i}} = \left(\displaystyle\sum_{i=1}^{n} \frac{1}{\sigma_i^2}\right)^{-1/2}.
\end{equation*}

---

## Specific Examples

Given the measurments

\begin{equation*}
\vec{x} = \left\{10, 9, 11\right\}
\end{equation*}

with uncertanties
$$\vec{\sigma_x} = \left\{1, 2, 3\right\}$$

```python
x_data = [10,9,11]
x_uncertainty = [1,2,3]
```

```python
numerator = sum(x/(sigma_x ** 2) for x, sigma_x in zip(x_data, x_uncertainty))
denominator = sum(1/(sigma_x ** 2) for sigma_x in x_uncertainty)

print("hand calculated weighted mean: {}".format(numerator/denominator))
```

    hand calculated weighted mean: 9.897959183673468

Using [NumPy's `average` method](https://docs.scipy.org/doc/numpy/reference/generated/numpy.average.html)

```python
# unweighted mean
np.average(x_data)
```

    10.0

```python
x_weights = [1/(uncert ** 2) for uncert in x_uncertainty]
# weighted mean
weighted_mean = np.average(x_data, weights=x_weights)
print(weighted_mean)
```

    9.897959183673468

```python
# no method to do this in NumPy!?
sigma = np.sqrt(1/np.sum(x_weights))
print("hand calculated uncertaintiy on weighted mean: {}".format(sigma))
```

    hand calculated uncertaintiy on weighted mean: 0.8571428571428571

```python
# A second way to find the uncertainty on the weighted mean
summand = sum((x * w) for x, w in zip(x_data, x_weights))
np.sqrt(np.average(x_data, weights=x_weights)/summand)
```

    0.8571428571428571

Let's plot the data now and take a look at the results

```python
def draw_weighted_mean(data, errors, w_mean, w_uncert):
    plt.figure(1)

    # the data to be plotted
    x = [i+1 for i in range(len(data))]

    x_min = x[x.index(min(x))]
    x_max = x[x.index(max(x))]

    y = data
    y_min = y[y.index(min(y))]
    y_max = y[y.index(max(y))]

    err_max = errors[errors.index(max(errors))]

    # plot data
    plt.errorbar(x, y, xerr=0, yerr=errors, fmt='o', color='black')
    # plot weighted mean
    plt.plot((x_min,x_max), (w_mean,w_mean), color = 'blue')
    # plot uncertainty on weighted mean
    plt.plot((x_min,x_max), (w_mean - w_uncert,w_mean - w_uncert), color = 'gray', linestyle = '--')
    plt.plot((x_min,x_max), (w_mean + w_uncert,w_mean + w_uncert), color = 'gray', linestyle = '--')

    # Axes
    plt.xlabel('Individual measurements')
    plt.ylabel('Value of measruement')
    # view range
    epsilon = 0.1
    plt.xlim(x_min-epsilon, x_max+epsilon)
    plt.ylim([y_min - err_max, 1.5 * y_max + err_max])

    #ax = figure().gca()
    #ax.xaxis.set_major_locator(MaxNLocator(integer=True))

    # Legends
    wmean_patch = mpathces.Patch(color = 'blue', label = "Weighted mean: $\mu={:0.3f}$".format(w_mean))
    uncert_patch = mpathces.Patch(color = 'gray', label = "Uncertainty on the weighted mean: $\pm{:0.3f}$".format(w_uncert))
    plt.legend(handles=[wmean_patch, uncert_patch])

    plt.show()
```

```python
draw_weighted_mean(x_data, x_uncertainty, weighted_mean, sigma)
```

Now let's do this again but with data that are Normally distributed about a mean value

```python
true_mu = np.random.uniform(3,9)
true_sigma = np.random.uniform(0.1,2.0)
n_samples = 20

samples = np.random.normal(true_mu, true_sigma, n_samples).tolist()
gauss_errs = np.random.normal(2, 0.4, n_samples).tolist()

weights = [1/(uncert ** 2) for uncert in gauss_errs]

draw_weighted_mean(samples, gauss_errs, np.average(samples, weights=weights), np.sqrt(1/np.sum(weights)))
```

## References

1. [_Data Analysis in High Energy Physics_](http://eu.wiley.com/WileyCDA/WileyTitle/productCd-3527410589.html), Behnke et al., 2013, $\S$ 2.3.3.1
2. [_Statistical Data Analysis_](http://www.pp.rhul.ac.uk/~cowan/sda/), Glen Cowan, 1998
3. University of Marlyand, Physics 261, [Notes on Error Propagation](http://www.physics.umd.edu/courses/Phys261/F06/ErrorPropagation.pdf)
4. Physics Stack Exchange, [_How do you find the uncertainty of a weighted average?_](https://physics.stackexchange.com/questions/15197/how-do-you-find-the-uncertainty-of-a-weighted-average)


# Chapter 2 Exercises
In this notebook we will go through the exercises of chapter 2 of Introduction to Stochastic Processes with R by Robert Dobrow.

```R
library(expm)
```

    Loading required package: Matrix

    Attaching package: ‘expm’

    The following object is masked from ‘package:Matrix’:

        expm

## 2.1
A Markov chain has transition Matrix
$$
p=\left(\begin{array}{cc}
0.1 & 0.3&0.6\\
0 & 0.4& 0.6 \\
0.3 & 0.2 &0.5
\end{array}\right)
$$
With initial distribution $\alpha =(0.2,0.3,0.5)$. Find the following:

a) $P(X_7=3|X_6=2)$

b) $P(X_9=2|X_1=2,X_5=1,X_7=3)$

c) $P(X_0=3|X_1=1)$

d) $E[X_2]$

```R
a=c(0.2,0.3,0.5)
p = matrix(c(0.1,0.3,0.6,0,0.4,0.6,0.3,0.2,0.5),ncol=3,byrow = TRUE)
p[2,3]
```

0.6

```R
(p%*%p)[3,2]
```

0.27

```R
a[3]*p[3,1]/(a[1]*p[1,1]+a[2]*p[2,1]+a[3]*p[3,1])
```

0.882352941176471

```R
e=0
pr = a%*%p%*%p
for (i in 1:ncol(pr))
    e = e+(i)*pr[1,i]
e
```

2.363

## 2.1
A Markov chain has transition Matrix
$$
p=\left(\begin{array}{cc}
0 & 1/2&1/2\\
1 & 0& 0 \\
1/3 & 1/3 &1/3
\end{array}\right)
$$
With initial distribution $\alpha =(1/2,0,1/2)$. Find the following:

a) $P(X_2=1|X_1=3)$

b) $P(X_1=3,X_2=1)$

c) $P(X_1=3|X_2=1)$

d) $P(X_9=1|X_1=3, X_4=1, X_7=2)$

```R
a=c(1/2,0,1/2)
p = matrix(c(0,1/2,1/2,1,0,0,1/3,1/3,1/3),ncol=3,byrow = T)
p[3,1]
```

0.333333333333333

```R
(a%*%p)[1,3]*p[3,1]
```

0.138888888888889

```R
(a%*%p)[1,3]*p[3,1]/((a%*%p)[1,1]*p[1,1]+(a%*%p)[1,2]*p[2,1]+(a%*%p)[1,3]*p[3,1])
```

0.25

```R
(p%*%p)[2,1]
```

0

## 2.3
Consider the Wright-Fisher model with population k=3. If the initial population has one A allele, what is the probability that there are no alleles at time 3.

```R
mat = c()
k = 3
for(j in 0:k){
    vec = c()
    for(i in (0:k)){
        vec=c(vec,choose(k, i)*(j/k)**i*(1-j/k)**(k-i))
    }
    mat=c(mat,vec)
}
p=matrix(mat,nrow=k+1,byrow = T)
(p%^%3)[2,1]
```

    Loading required package: Matrix

    Attaching package: ‘expm’

    The following object is masked from ‘package:Matrix’:

        expm

0.516689529035208

## 2.4
For the Generalñ two-state chain with transformation matrix
$$\boldsymbol{P}=\left(\begin{array}{cc}
1-p & p\\
q & 1-q
\end{array}\right)$$

And initial distribution $\alpha=(\alpha_1,\alpha_2)$, find the following:

(a) the two step transition matrix

(b) the distribution of $X_1$

### Answer
(a)

For this case the result comes from doing the matrix multiplication once i.e. finding $\boldsymbol{P}*\boldsymbol{P}$ that is:
$$\boldsymbol{P}*\boldsymbol{P}=\left(\begin{array}{cc}
(1-p)^2+pq & (1-p)p+(1-q)p\\
(1-p)q+(1-q)q & (1-q)^2+pq
\end{array}\right)$$

(b)
in this case we need to take into account alpha which would be $X_0$ and then multiply with the transition matrix, then:

$\boldsymbol{\alpha}*\boldsymbol{P}$ that is:
$$\boldsymbol{\alpha}*\boldsymbol{P}=(
(1-p)\alpha_1+q\alpha_2 , p\alpha_1+(1-q)\alpha_2)$$

## 2.5
Consider a random walk on $\{0,...,k\}$, which moves left and right with respective probabilities q and p. If the walk is at 0 it transitions to 1 on the next step. If the walk is at k it transitions to k-1 on the next step. This is calledrandom walk with reflecting bounderies. Assume that $k=3,q=1/4, p =3/4$ and the initial distribution is uniform. For the following, use technology if needed.

(a) Exhibit the transition Matrix

(b) Find $P(X_7=1|X_0=3,X_2=2,X_4=2)$

(c) Find $P(X_3=1,X_5=3)$

### Answer
(a) $$\boldsymbol{Pr}=\left(\begin{array}{cc}
0 & 1 & 0 & 0 \\
1/4 & 0 & 3/4 & 0\\
0 & 1/4 & 0 & 3/4 \\
0 & 0 & 1 & 0
\end{array}\right)$$

(b) since it is a Markov Chain, then $P(X_7=1|X_0=3,X_2=2,X_4=2)=P(X_7=1|X_4=2)=\boldsymbol{Pr}^3_{2,1}$

```R
p = matrix(c(0,1,0,0,1/4,0,3/4,0,0,1/4,0,3/4,0,0,1,0),nrow =4,byrow = T)
(p%^%3)[3,2]
```

0.296875

(c) $P(X_3=1,X_5=3)=(\alpha \boldsymbol{Pr}^3)_{1}*\boldsymbol{Pr}^2_{1,3}$

```R
a=c(1/4,1/4,1/4,1/4)
(a%*%(p%^%3))[1,2]*(p%*%p)[2,4]
```

0.103271484375

## 2.6
A tetrahedron die has four faces labeled 1,2,3 and 4. In repeated independent rolls of the die $R_0, R_1,...,$ let $X_n=max\{R_0,...,R_n\}$ be the maximum value after $n+1$ rolls, for $n\geq0$

(a) Give an intuitive argument for why $X_0, X_1, ... $ is a Markov Chain and exhibit its transition Matrix

(b) find $P(X_3 \geq 3)$

### Answer
(a) This is a Markov Chain since the value of the maximum in all the rolls does only depend on the last value given of the throw this is because the max value should not changhe based on past rolls of this given event, then $P(X_n=i|X_{n-1}=j,...)=P(X_n=i|X_{n-1}=j)$
and the transition matrix is:
$$\boldsymbol{Pr}=\left(\begin{array}{cc}
1/4 & 1/4 & 1/4 & 1/4 \\
0 & 1/2 & 1/4 & 1/4 \\
0 & 0 & 3/4 & 1/4 \\
0 & 0 & 0 & 1
\end{array}\right)$$

(b) We know that the tetrahedron has unoiform probability to get the initial value, then $\alpha=(1/4,1/4,1/4,1/4)$
then we want $\alpha*\boldsymbol{Pr}^3$

```R
p = matrix(c(1/4,1/4,1/4,1/4,0,1/2,1/4,1/4,0,0,3/4,1/4,0,0,0,1),nrow=4,byrow = T)
a=c(1/4,1/4,1/4,1/4)
sum((a%*%(p%^%3))[1,-(1:2)])
```

0.9375

## 2.7
Let $X_0, X_1,...$ be a Markov chain with transition matrix $P$. Let $Y_n=X_{3n}$, for $n = 0,1,2,...$ Show that $Y_0,Y_1,...$ is a Markov chain and exhibit its transition Matrix.

### Answer
Let's unravel $P(Y_k|Y_{k-1},...,Y_0)$

$$P(Y_k|Y_{k-1},...,Y_0)=P(X_{3k}|X_{3k-3},...,X_0)$$

But since $X_0, X_1,...$ is a Markov Chain, then $P(X_{3k}|X_{3k-3},...,X_0) = P(X_{3k}|X_{3k-3}) = P(Y_k|Y_{k-1})$
This means that $Y_0,Y_1,...$ is also a Markov chain.

and then the transition matrix for Y is $P_Y=P_X^3$

## 2.8
Give the Markov transition matrix for a random walk on the weighted graph in the next figure:

### Answer
$$\boldsymbol{Pr}=\left(\begin{array}{cc}
0 & 1/3 & 0 & 0 & 2/3 \\
1/10 & 1/5 & 1/5 & 1/10 & 2/5 \\
1/2 & 1/3 & 0 & 1/6 & 0\\
0 & 1/2 & 1/2 & 0 & 0\\
1/3 & 2/3 & 0 & 0 & 0
\end{array}\right)$$

## 2.8
Give the Markov transition matrix for a random walk on the weighted graph in the next figure:

### Answer
$$\boldsymbol{Pr}=\left(\begin{array}{cc}
0 & 0 & 3/5 & 0 & 2/5 \\
1/7 & 2/7 & 0 & 0 & 4/7 \\
0 & 2/9 & 2/3 & 1/9 & 0\\
0 & 1 & 0 & 0 & 0\\
3/4 & 0 & 0 & 1/4 & 0
\end{array}\right)$$

## 2.10
Consider a Markov Chain with transition Matrix

$$\boldsymbol{Pr}=\left(\begin{array}{cc}
0 & 3/5 & 1/5 & 2/5 \\
3/4 & 0 & 1/4 & 0 \\
1/4 & 1/4 & 1/4 & 1/4\\
1/4 & 0 & 1/4 & 1/2
\end{array}\right)$$
(a) Exhibit the directed, weighted transition graph for the chain

(b) The transition graph for this chain can be given as a weighted graph without directed edges. Exhibit the graph

(a)

(b)

## 2.11
You start with five dice. Roll all the dice and put aside those dice that come up 6. Then roll the remaining dice, putting aside thise dice that come up 6. and so on. Let $X_n$ be the number of dice that are sixes after n rolls.

(a)  Describe the transition matrix $\boldsymbol{Pr}$ for this Markov chain

(b) Find the probability of getting all sixes by the third play.

(c) what do you expect $\boldsymbol{Pr}^{100}$ to look like? Use tech to confirm your answer **(Good that we are doing this in jupyter notebook haha)**

(a) Note that the space for $X_n$ is $0,1,2,3,4,5$

$$\boldsymbol{Pr}=\left(\begin{array}{cc}
1 & 0 & 0 & 0 & 0 & 0 \\
1/6 & 5/6 & 0 & 0 & 0 & 0\\
\frac{1^2}{6^2}{2\choose 2} & \frac{5*1}{6^2}{2\choose 1} & \frac{5^2}{6^2}{2\choose 0} & 0 & 0 & 0\\
\frac{1^3}{6^3}{3\choose 3} & \frac{5*1^2}{6^3}{3\choose 2} & \frac{5^2*1}{6^3}{3\choose 1} & \frac{5^3}{6^3}{3\choose 0} & 0 & 0\\
\frac{1^4}{6^4}{4\choose 4} & \frac{5*1^3}{6^4}{4\choose 3} & \frac{5^2*1^2}{6^4}{4\choose 2} & \frac{5^3*1}{6^4}{4\choose 1} & \frac{5^4}{6^4}{4\choose 0} & 0\\
\frac{1^5}{6^5}{5\choose 5} & \frac{5*1^4}{6^5}{5\choose 4} & \frac{5^2*1^3}{6^5}{5\choose 3} & \frac{5^3*1^2}{6^5}{5\choose 2} & \frac{5^4*1}{6^5}{5\choose 1} & \frac{5^5}{6^5}{5\choose 0}\\
\end{array}\right) = \left(\begin{array}{cc}
1 & 0 & 0 & 0 & 0 & 0 \\
\frac{1}{6} & \frac{5}{6} & 0 & 0 & 0 & 0\\
\frac{1}{36} & \frac{10}{36} & \frac{25}{36} & 0 & 0 & 0\\
\frac{1}{216} & \frac{15}{216} & \frac{75}{216} & \frac{125}{216} & 0 & 0\\
\frac{1}{1296} & \frac{20}{1296} & \frac{150}{1296} & \frac{500}{1296} & \frac{625}{1296} & 0\\
\frac{1}{7776} & \frac{25}{7776} & \frac{250}{7776} & \frac{1250}{7776} & \frac{3125}{7776} & \frac{3125}{7776}\\
\end{array}\right)$$

It is interesting that the distribution when $X_n=i$ is the components of the binomial expansion $(1/6+5/6)^i$

(b)

```R
p = matrix(c(1, 0, 0, 0, 0, 0,
                      1/6, 5/6, 0, 0, 0, 0, 1/36, 10/36, 25/36, 0, 0, 0,
                      1/216, 15/216, 75/216, 125/216, 0, 0,
                      1/1296, 20/1296, 150/1296, 500/1296, 625/1296, 0,
                      1/7776, 25/7776, 250/7776, 1250/7776 ,3125/7776, 3125/7776),nrow=6,byrow=T)
(p%^%3)[6,1]
```

0.0132720560113747

(c) I would expect that there is basically 100 percent posibility that there are 0 dices left without regarding how many dices you started with.

```R
round(p%^%100)
```

1	0	0	0	0	0

1	0	0	0	0	0

1	0	0	0	0	0

1	0	0	0	0	0

1	0	0	0	0	0

1	0	0	0	0	0

## 2.12
two urns contain k balls each. At the beginning of the experiment the urn on the left countains k red balls and the one on the right contains k blue balls. At each step, pick a ball at random from each urn and excange them. Let $X_n$ be the number of blue balls left in the urn on the left (Note that $X_0=0, X_1 = 1$). Argue the process is a Markov Chain. Find the transition matrix. This model is called the Bernoulli - Laplace process.

### Answer
This process has to be a random process because the only thing that matters at step n is to know how many blue balls are in the urn on the left. This means that even if you know all the history only the last state is the one that matters to know the probability for the next step.

$$P = \left(\begin{array}{cc}
0 & 1 & 0 & ... &0 & 0 & 0 \\
\frac{1}{k} & 0 & \frac{k-1}{k} & ... & 0 & 0 & 0\\
0 & \frac{2}{k} & 0 & ... & 0 & 0 & 0\\
... & ... & ... & ... & ... & ... & ...\\
0 & 0 & 0 & ... & \frac{k-1}{k} & 0 & \frac{1}{k}\\
0 & 0 & 0 & ... & 0 & 1 & 0\\
\end{array}\right)$$

## 2.13
see the move-to-front process in Example 2.10. Here is anorher way to organize the bookshelf. when a book is returned it is put bacl on the library shelf one position forward from where it was originally. If the book at the fron of the shelf is returned it is put back at the front of the shelf. This, if the order of books is (a,b,c,d,e) and the book d is picjed, the new order is (a,b,d,c,e). This reorganization method us called the *transposition* or *move-ahead-1* scheme. Give the transition matrix for the transportation scheme for a shelf with three books.

### Answer
remember the states are $(abc, acb, bac,bca, cab, cba)$
$$P = \left(\begin{array}{cc}
0 & p_c & p_b &p_a & 0 & 0 \\
p_b & 0 & 0 & 0 & p_c & p_a\\
p_a & p_b & 0 & p_c & 0 & 0\\
0 & 0 & p_a & 0 & p_b & p_c\\
p_c & p_a & 0 & 0 & 0 & p_b\\
0 & 0 & p_c & p_b & p_a & 0
\end{array}\right)$$

## 2.14
There are k songs in Mary's music player. The player is set to *Shuffle* mode, which plays songs uniformly at random, sampling with replacement. Thus, repeats are possible. Let $X_n$ denote the number of *unique* songs that have been heard after the nth play.

(a) show that $X_0,X_1, ...$ is a Markov chain and give the transition matrix

(b) if Mary has four songs on her music player, find the probability that all songs are heard after six plays.

### Answer
(a)
imagine that you have $P(X_n|X_{n-1},...,X_0)$ note that $X_{n+1}=max\{\xi_{n+1},X_n\}$ where $\xi_{n+1}$ is the result of the n+1 and it is independent to all $X_i$, $0\leq i\leq n$, then this means that $X_0,...,X_n$ is a Markov chain with transition matrix:
$$P = \left(\begin{array}{cc}
1/k & (k-1)/k & 0 & ... &0 & 0 & 0 \\
0 & 2/k & (k-2)/k & ... & 0 & 0 & 0\\
0 & 0 & 3/k & ... & 0 & 0 & 0\\
... & ... & ... & ... & ... & ... & ...\\
0 & 0 & 0 & ... & 0 & (k-1)/k & 1/k\\
0 & 0 & 0 & ... & 0 & 0 & 1\\
\end{array}\right)$$

## 2.15
Assume that $X_0,X_1,...$ is a two state Markov chain in $\mathcal{S}=\{0,1\}$ with transition matrix:

$$P=\left(\begin{array}{cc}
1-p & p  \\
q & 1-q
\end{array}\right)$$

The present state of the chain only depends on the previous state. We can create a bivariate process that looks back two time periods by the following contruction. Let $Z_n=(X_{n-1},X_n)$, for $n\geq1$. The sequence $Z_1,Z_2,...$ is a Markov chain with state space $\mathcal{S}X\mathcal{S}={(0,0),(1,0),(0,1),(1,1)}$ Give the transition matrix of the new chain.

### Answer
$$P = \left(\begin{array}{cc}
1-p & p & 0 & 0 \\
0 & 0 & q & 1-q \\
1-p & p & 0 & 0 \\
0 & 0 & q & 1-q
\end{array}\right)$$

## 2.16
Assume that  $P$  is a stochastic matrix with equal rows. Show that $P^n=P$, for all $n\geq1$

### Answer
let's then see $P$ as:
$$P = \left(\begin{array}{cc}
p_1 & p_2 & ... & p_k \\
p_1 & p_2 & ... & p_k \\
p_1 & p_2 & ... & p_k \\
... & ... & ... & ... \\
p_1 & p_2 & ... & p_k \\
p_1 & p_2 & ... & p_k \\
\end{array}\right)$$

First let's calculate $P^2$, then

$$P^2 = \left(\begin{array}{cc}
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
... & ... & ... & ... \\
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
p_1*p_1+p_2*p_1+...+p_k*p_1 & p_1*p_2+p_2*p_2+...+p_k*p_2 & ... & p_1*p_k+p_2*p_k+...+p_k*p_k \\
\end{array}\right)=
\left(\begin{array}{cc}
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
... & ... & ...  \\
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
p_1*(p_1+p_2+...+p_k) & ... & p_k(p_1+p_2+...+p_k) \\
\end{array}\right)=
\left(\begin{array}{cc}
p_1*(1) & ... & p_k(1) \\
p_1*(1) & ... & p_k(1) \\
p_1*(1) & ... & p_k(1) \\
... & ... & ...  \\
p_1*(1) & ... & p_k(1) \\
p_1*(1) & ... & p_k(1) \\
\end{array}\right) = P$$

Then let's see what happens for $P^3$, then
$$P^3=P^2*P=P*P=P^2=P$$

Assume it is true for $P^n$ and see what happens at $P^{n+1}$
$$P^{n+1}=P^n*P=P*P=P^2=P$$
Then this means that  $P^n=P$, for all $n\geq1$

## 2.17
Let **$P$** be the stochastic matrix. Show that $\lambda = 1$ is an eigenvalue of **$P$**. What is the associated eigenvector?

## Answer
Let's first think that it is clear that the rows all sum to one, and remember that an eigenvector asociated to an eigen value looks of the form:
$$A\overline{x}=\lambda \overline{x}$$

it is easy to note that the eigenvector we are looking at is the vector $\overline{x}=(1,1,...,1)^T$ this means that the eigenvalue asociated to this is also one

we can check this by doing the multiplication. Say we have
$$A= \left(\begin{array}{cc}
p_{1,1} & p_{1,2} & ... & p_{1,k} \\
p_{2,1} & p_{2,2} & ... & p_{2,k} \\
... & ... & ... & ... \\
p_{k,1} & p_{k,2} & ... & p_{k,k} \\
\end{array}\right)$$
where A is an stochastic matrix. Then:
$$Ax= \left(\begin{array}{cc}
p_{1,1} & p_{1,2} & ... & p_{1,k} \\
p_{2,1} & p_{2,2} & ... & p_{2,k} \\
... & ... & ... & ... \\
p_{k,1} & p_{k,2} & ... & p_{k,k} \\
\end{array}\right)\left(\begin{array}{cc}
1 \\
1 \\
...  \\
1\\
\end{array}\right)=
\left(\begin{array}{cc}
p_{1,1} + p_{1,2} + ... + p_{1,k} \\
p_{2,1} + p_{2,2} + ... + p_{2,k} \\
...  \\
p_{k,1} + p_{k,2} + ... + p_{k,k} \\
\end{array}\right)=
\left(\begin{array}{cc}
1 \\
1 \\
...  \\
1\\
\end{array}\right)$$

The way to prove this is to remember that if $A$ is an square matrix, then the eigenvalues of A are the same as its transpose's. Then, let's check if 1 is an eigenvalue by doing $det(A^T-\lambda I)$ where $\lambda$ is the eigenvalue (in this case 1) and $I$ is the identity matrix, when doing this we get that:
$$A-I= \left(\begin{array}{cc}
p_{1,1} -1 & p_{1,2} & ... & p_{1,k} \\
p_{2,1} & p_{2,2}- 1 & ... & p_{2,k} \\
... & ... & ... & ... \\
p_{k,1} & p_{k,2} & ... & p_{k,k}-1 \\
\end{array}\right)$$

And since we are dealing with the transpose, then if we sum all rows to the first row (i.e. $R_1 -> R_1+R_2+...+R_k$), then all values in the first row are zero, which means that the matrix is linearly dependand and $det(A^T- 1I)=0$, which means that 1 is an eigenvalue for $A$

## 2.18
A stochastic matrix is called *doubly* stochastic if its columns sum to 1. Let $X_0,X_1,...$ be a Markov chain on $\{ 1,...,k \}$ with doubly stochastic transition matrix and initial distribution that is uniform on $\{ 1,...,k \}$. Show that the distribution of $X_n$ is uniform on $\{ 1,...,k \}$, for all $n\geq0$

### Answer
Let's remember that then the distribution at $X_n$ is $\alpha P^n$, let's see what happens at $n=1$

$n=1$
$$X_1=\alpha P = \left(\begin{array}{cc}
1/k &1/k&...& 1/k
\end{array}\right)\left(\begin{array}{cc}
p_{1,1} & p_{1,2} & ... & p_{1,k} \\
p_{2,1} & p_{2,2} & ... & p_{2,k} \\
...  \\
p_{k,1} & p_{k,2} & ... & p_{k,k} \\
\end{array}\right)=\left(\begin{array}{cc}
p_{1,1}*1/k + p_{2,1}*1/k + ... + p_{k,1}*1/k \\
p_{1,2}*1/k + p_{2,2}*1/k + ... + p_{k,2}*1/k \\
...  \\
p_{1,k}*1/k + p_{2,k}*1/k + ... + p_{k,k}*1/k \\
\end{array}\right)^T=\left(\begin{array}{cc}
(p_{1,1} + p_{2,1} + ... + p_{k,1})*1/k\\
(p_{1,2} + p_{2,2} + ... + p_{k,2})*1/k \\
...  \\
(p_{1,k} + p_{2,k} + ... + p_{k,k})*1/k \\
\end{array}\right)^T = \left(\begin{array}{cc}
(1)*1/k\\
(1)*1/k \\
...  \\
(1)*1/k \\
\end{array}\right)^T=\alpha
$$
This last step is due to the matrix being double stochastic

Now let's $n=2$
$$X_1=\alpha P^2 = (\alpha P)*P=\alpha*p=\alpha$$

Then we assume it happens for $n$, let's see then for $n+1$
$$X_{n+1}=\alpha P^{n+1} = (\alpha P^n)*P=\alpha*p=\alpha$$

## 2.19
Let **$P$** be the transition matrix of a Markov chain on $k$ states. Let **$I$** denote the $kXk$ identity matrix. consider the matrix

$$\boldsymbol{Q}=(1-p)\boldsymbol{I}+p\boldsymbol{P}\text{  , for }0<p<1$$
show that **$Q$** is a stochastic matrix. Give the probabilistic interpretation for the dynamics of a markov chain governed by the **$Q$** matrix in terms of the original Markov chain.

### Answer
Let's construct $(1-p)\boldsymbol{I}+p\boldsymbol{P}$ and we need to check for two main things. First, $0\leq p_{i,j}\leq 1$, this is clear, since $P$ already holds this property and you are just multiplying it by two numbers that sum 1 and are less than 1. Now we need to check if $\sum_j p_{i,j}=1$, then seing the matrix:
$$(1-p)\boldsymbol{I}+p\boldsymbol{P}=\left(\begin{array}{cc}
p_{1,1}*p +(1-p) & p_{1,2}*p & ... & p_{1,k}*p \\
p_{2,1}*p & p_{2,2}*p+(1-p) & ... & p_{2,k}*p \\
... & ... & ... & ... \\
p_{k,1}*p & p_{k,2}*p & ... & p_{k,k}*p+(1-p) \\
\end{array}\right)$$
we have for row i

$p_{i,1}*p + p_{1,2}*p + ...+ p_{i,i}p+(1-p)+...+ p_{1,k}*p = p*(p_{i,1} + p_{1,2} + ...+ p_{1,k})+1-p=p*(1)+1-p=1 $

then $Q$ is a stochastic matrix.

As I see it this matrix $Q$ the only value it is modifying is the probability of transitioning from state $i$ to state $i$. so this linear combination could be used when you want to give more probability to return to the same state you are at step $n$. you can notice that the probability of going back to state $i$ when you are at state $i$ is due to the fact that is the only other state that has a real value greater than zero being added to it.

## 2.20
Let $X_0,X_1,...$ be a Markov chain with transition matrix

$$P=\left(\begin{array}{cc}
0 & 1 & 0 \\
0 & 0 & 1 \\
p & 1-p & 0
\end{array}\right)$$
for $0<p<1$. Let $g$ be the function defined by:

$$
  g(x) =
  \begin{cases}
                                   0 & \text{if $x=1$} \\
                                   1 & \text{if $x=2,3$}
  \end{cases}
$$

Let $Y_n=g(X_n)$ for $n\geq0$. Show that $Y_0,Y_1,...$ is not a Markov chain

### Answer

Let's proof this by contradiction. if we assume $Y_n$ is a Markov chain, then $P(Y_n|Y_{n-1},...,Y_0)=P(Y_n|Y_{n-1})$, but let's check this next two scenarios

## 2.21
Let $P$ and $Q$ be two transition matricies on the same state space processes, both started in some initial state $i$.

In the process $\#1$ a coin is flipped, then if it lands head, the process unfolds according to $P$. If it lands tails, the process unfolds according to $Q$.

In process $\#2$, at each step a coin is flipped. If it lands heads, the next step is chosen from $P$. If it lands tails, the next step is chosen from $Q$.

Thus, in $\#1$, one coin is initially flipped, which governsthe entire evolution of the process and in $\#2$ a coin is flipped at each step to decide the next step in the process.

Decide whether either of these processes is a Markov chain, if it is exhibit its transition matrix, if not, then explain why not.

### Answer
$#1$

For example 1 we have that it is not a Markov chain and the reason for this is because in the first step the this means that the distribution of $X_0$ is different from the distribution of $X_1$ which means that we cannot have a transition matrix for all states, although it is nice to see that once the heads is chosen, then the next steps do follow a Markov chain with either $P$ or $Q$ as their matrices.

$#2$

For example 2 we have that it is a Markov chain and the reason for this is because we can define the transition Matrix $M=0.5P+0.5Q$ as the new matrix that follows this process.

```R
## This could be a class.
ChooseNextStep<-function(x, mat){
    tmp_x = x%*%mat
    return(list("tmp_x" = tmp_x, "xi" = sample(0:(length(tmp_x)-1),size=1,prob=tmp_x)))
}

Simulation_21<-function(case, P=matrix(c(0.2,0.8,0.2,0.8),nrow=2,byrow=T),
                        Q=matrix(c(0.8,0.2,0.8,0.2),nrow=2,byrow=T), simulations=1000, steps = 5, p=0.5){
    "
    This is a function for the simulation stated in 2.21 from the book, it receives the
    two matrices and the case you are talking for either one or two and calculates its state matrices for the first
    steps we assume initial distributions uniform
    "
    rows = nrow(P)
    cols = ncol(P)
    initial_dis = rep(1/cols, cols)
    sim = matrix(NA, nrow=simulations, ncol=steps)
    for(i in 1:simulations){
        res = c()
        result = list("tmp_x"=initial_dis)
        for(j in 1:steps){
            if(case == 1){
                if(j == 1){
                    coin = runif(1)
                    if(coin < p){
                        # Follows P
                        distribution = "P"
                        result = ChooseNextStep(result$tmp_x,P)
                    }
                    else{
                        distribution = "Q"
                        result = ChooseNextStep(result$tmp_x,Q)
                    }
                }else{
                    if(distribution=="P"){
                        result = ChooseNextStep(result$tmp_x,P)
                    }else{
                        result = ChooseNextStep(result$tmp_x,Q)
                    }
                }
            }else if(case == 2){
                coin = runif(1)
                if(coin < p){
                    # Follows P
                    result = ChooseNextStep(result$tmp_x,P)
                }else{
                    result = ChooseNextStep(result$tmp_x,Q)
                }
            }
            res=c(res,result$xi)
        }
        sim[i,]=res
    }
    return(sim)
}

CalcMatrixForStep<-function(sim, step=1){
    res0 = sim[sim[,(step-1)]==0,step]
    res1 = sim[sim[,(step-1)]==1,step]
    counts0 = table(res0)
    counts1 = table(res1)
    prob0 = as.matrix(counts0)[,1]/length(res0)
    prob1 = as.matrix(counts1)[,1]/length(res1)
    return(matrix(c(prob0,prob1),nrow=2, byrow=T))
}

GetTransitionMatrix<-function(results){
    matrices = vector("list", ncol(results)-1)
    for(i in 2:ncol(results)){
        matrices[[i-1]]=CalcMatrixForStep(results,i)
    }
    return(matrices)
}
```

```R
sim = Simulation_21(1, simulations=100000)
elems=GetTransitionMatrix(sim)
for(elem in 1:length(elems)){
    print(elems[[elem]])
}
```

              [,1]      [,2]
    [1,] 0.6780377 0.3219623
    [2,] 0.3194483 0.6805517
              [,1]      [,2]
    [1,] 0.6770189 0.3229811
    [2,] 0.3195829 0.6804171
              [,1]      [,2]
    [1,] 0.6781104 0.3218896
    [2,] 0.3224084 0.6775916
              [,1]      [,2]
    [1,] 0.6824933 0.3175067
    [2,] 0.3197514 0.6802486

```R
sim = Simulation_21(1, simulations=100000)
elems=GetTransitionMatrix(sim)
for(elem in 1:length(elems)){
    print(elems[[elem]])
}
```

              [,1]      [,2]
    [1,] 0.6791643 0.3208357
    [2,] 0.3200032 0.6799968
              [,1]      [,2]
    [1,] 0.6815790 0.3184210
    [2,] 0.3216778 0.6783222
              [,1]      [,2]
    [1,] 0.6787224 0.3212776
    [2,] 0.3204197 0.6795803
              [,1]      [,2]
    [1,] 0.6801432 0.3198568
    [2,] 0.3227839 0.6772161

```R
sim = Simulation_21(2, simulations=100000)
elems=GetTransitionMatrix(sim)
for(elem in 1:length(elems)){
    print(elems[[elem]])
}
```

              [,1]      [,2]
    [1,] 0.4968499 0.5031501
    [2,] 0.4951701 0.5048299
              [,1]      [,2]
    [1,] 0.4975908 0.5024092
    [2,] 0.4989583 0.5010417
              [,1]      [,2]
    [1,] 0.5009633 0.4990367
    [2,] 0.5030894 0.4969106
              [,1]      [,2]
    [1,] 0.5004283 0.4995717
    [2,] 0.5001506 0.4998494

```R
sim = Simulation_21(2, simulations=100000)
elems=GetTransitionMatrix(sim)
for(elem in 1:length(elems)){
    print(elems[[elem]])
}
```

              [,1]      [,2]
    [1,] 0.5018999 0.4981001
    [2,] 0.4975168 0.5024832
              [,1]      [,2]
    [1,] 0.4973985 0.5026015
    [2,] 0.4989006 0.5010994
              [,1]      [,2]
    [1,] 0.4994078 0.5005922
    [2,] 0.4992926 0.5007074
              [,1]      [,2]
    [1,] 0.5010313 0.4989687
    [2,] 0.5040048 0.4959952

## 2.22
Show by induction:

(a) $1+3+5+...+(2n-1)=n^2$

(b) $1^2+2^2+...+n^2=n(n+1)(2n+1)/6$

(c) for all real $x>-1$, $(1+x)^n\geq 1+nx$

### Answer
$#a$

For $n=1$

$$
\begin{align}
2(1)-1 &= 1 \\&=1^2
\end{align}$$

Assume it works for $n-1$

$$
\begin{align}
1+3+...+(2n-3)  &= (n-1)^2 \\
                &=n^2 -2n+1
\end{align}$$

Then for $n$
$$
\begin{align}
1+3+...+(2n-3)+(2n-1)  &= n^2 -2n+1 +2n-1\\
                &=n^2
\end{align}$$

Therefore is proved.

$#b$

For $n=1$

$$
\begin{align}
1(1+1)(2*1+1)/6 &= 6/6 \\&=1^2
\end{align}$$

Assume it works for $n-1$

$$
\begin{align}
1^2+2^2+...+(n-1)n^2&=(n-1)(n)(2n-1)/6
\end{align}$$

Then for $n$
$$
\begin{align}
1^2+2^2+...+n^2 &=\frac{(n-1)(n)(2n-1)}{6}+n^2\\
                &=\frac{2n^-3n^2+n+6n^2}{6}\\
                &=\frac{n(2n^2+3n+1)}{6}\\
                &=\frac{n(n+1)(2n+1)}{6}
\end{align}$$

Therefore is proved.

$#c$

For this is important to notice that for $x>-1$ means that $(1+x)>0$ for all $x$. Now let's see what happens for $n=1$

$$
\begin{align}
(1+x)^1 &= (1+1*x)\\
1+x &\geq 1+x
\end{align}$$

Assume it works for $n-1$

$$
\begin{align}
(1+x)^{n-1}&\geq1+(n-1)x
\end{align}$$

Then for $n$
$$
\begin{align}
(1+x)^{n} &= (1+x)^{n-1}*(1+x)\\
          &\geq 1+(n-1)x*(1+x)\\
\\ \text{this last step because $1+x > 0$} \\ \\
1+(n-1)x*(1+x)    &=1+(n-1)x+x+(n-1)x^2\\
                  &=1+n*x+ (n-1)x^2\\
\\ \text{but since $(n-1)x^2\geq 0$ for all $x>1$}\\ \\
1+n*x+ (n-1)x^2 &\geq 1+n*x \\
\\ \text{=>} \\ \\
(1+x)^{n} &\geq 1+n*x+ (n-1)x^2 \\
          &\geq 1+n*x \\
\\ \text{by transitivity relation} \\ \\
(1+x)^{n} &\geq 1+n*x \\
\end{align}$$

Therefore is proved.

## 2.23
Simulate the first 20 letters (vowel/consonant) of the Pushkin poem Markov chain of Example 2.2.

$$P=\left(\begin{array}{cc}
0.175 & 0.825 \\
0.526 & 0.474
\end{array}\right)$$

### Answer

```R
PushkinLetters<-function(P=matrix(c(0.175,0.825,0.526,0.474),nrow=2, byrow = T),
                         init= c(8.638/20, 11.362/20),simulations=10){
    results = list()
    for(i in 1:simulations){
        results=c(results, list(OneSimulation(init=init, P=P)))
    }
    return(results)
}
OneSimulation<-function(steps = 20, init, P){
    sim = c()
    sim = samp(sim, init)
    for(i in 2:steps){
        sim=samp(sim, c(P[sim[i-1]+1,]))
    }
    return(parseSimulation(sim))
}
samp<-function(simlist, dist){
    vowel = 0
    consonant = 1
    if (runif(1)<dist[1]){
        simlist=c(simlist,vowel)
    }else{
        simlist=c(simlist,consonant)
    }
    return(simlist)
}
parseSimulation<-function(toParse){
    toParse[toParse==1]="Consonant"
    toParse[toParse==0]="vowel"
    return(toParse)
}

```

```R
PushkinLetters()
```

'vowel'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'vowel'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'vowel'

'Consonant'

'vowel'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'vowel'

'Consonant'

'vowel'

'Consonant'

'vowel'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'Consonant'

'Consonant'

'vowel'

'Consonant'

'Consonant'

'vowel'

'Consonant'

## 2.24
Simulate 50 steps of the random walk on the graph in Figure2.1. Repeat the simulation 10 times. How many of your simulations end at vertex c? Compare with the exact long-term probability the walk visits c.

With transition matrix uniform across vertex as described in the frog example, then:
$$P=\left(\begin{array}{cc}
0 & 1 & 0 & 0 & 0 & 0\\
1/4 & 0 & 1/4 & 1/4 & 1/4 & 0\\
0 & 1/4 & 0 & 1/4 & 1/4 & 1/4\\
0 & 1/4 & 1/4 & 0 & 1/4 & 1/4\\
0 & 1/3 & 1/3 & 1/3 & 0 & 0\\
0 & 0 & 1/2 & 1/2 & 0 & 0
\end{array}\right)$$

### Answer

```R
FrogJump<-function(P=matrix(c(0 , 1 , 0 , 0 , 0 , 0,
                    1/4 , 0 , 1/4 , 1/4 , 1/4 , 0,
                    0 , 1/4 , 0 , 1/4 , 1/4 , 1/4,
                    0 , 1/4 , 1/4 , 0 , 1/4 , 1/4,
                    0 , 1/3 , 1/3 , 1/3 , 0 , 0,
                    0 , 0 , 1/2 , 1/2 , 0 , 0),nrow=6,byrow=T),
                   init= rep(1/6,6), simulations=10){
    results = list()
    for(i in 1:simulations){
        results=c(results,list(OneSimulation(init=init,P=P)))
    }
    return(results)
}
OneSimulation<-function(steps = 50, init, P){
    sim = c()
    sim = samp(sim, init)
    for(i in 2:steps){
        sim=c(sim,sample(0:(length(c(P[sim[i-1]+1,]))-1),size=1,prob=c(P[sim[i-1]+1,])))
    }
    return(sim)
}

```

```R
sapply(FrogJump(), tail, 1)
```

1

2

1

0

4

4

5

1

3

4

```R
P=matrix(c(0 , 1 , 0 , 0 , 0 , 0,
        1/4 , 0 , 1/4 , 1/4 , 1/4 , 0,
        0 , 1/4 , 0 , 1/4 , 1/4 , 1/4,
        0 , 1/4 , 1/4 , 0 , 1/4 , 1/4,
        0 , 1/3 , 1/3 , 1/3 , 0 , 0,
        0 , 0 , 1/2 , 1/2 , 0 , 0), nrow=6, byrow=T)
(P%^%30)[1,]
```

0.0555559496995727

0.222221212660565

0.22222272669261

0.22222272669261

0.166666666977107

0.111110717277535

## 2.25
The behavior of dolphins in the presence of tour boats in Patagonia, Argentina is studied in Dans et al. (2012). A Markov chain model is developed, with state space consisting of five primary dolphin activities (socializing, traveling, milling, feeding, and resting). The following transition matrix is
obtained.

$$P=\left(\begin{array}{cc}
0.84 & 0.11 & 0.01 & 0.04 & 0 \\
0.03 & 0.8 & 0.04 & 0.1 & 0.03 \\
0.01 & 0.15 & 0.7 & 0.07 & 0.07 \\
0.03 & 0.19 & 0.02 & 0.75 & 0.01 \\
0.03 & 0.09 & 0.05 & 0 & 0.83
\end{array}\right)$$

### Answer

```R
(matrix(c(
0.84 , 0.11 , 0.01 , 0.04 , 0,
0.03 , 0.8 , 0.04 , 0.1 , 0.03,
0.01 , 0.15 , 0.7 , 0.07 , 0.07,
0.03 , 0.19 , 0.02 , 0.75 , 0.01,
0.03 , 0.09 , 0.05 , 0 , 0.83), nrow=5, byrow=T
)%^%100)[1,]
```

0.147835822368841

0.414925374905324

0.0955597006624145

0.216380599307721

0.125298502755702

## 2.26
In computer security applications, a honeypot is a trap set on a network to detect and counteract computer hackers. Honeypot data are studied in Kimou et al. (2010) using Markov chains. The authors obtain honeypot data from a central database and observe attacks against four computer ports—80, 135, 139, and 445—over 1 year. The ports are the states of a Markov chain along with a state corresponding to no port is attacked. Weekly data are monitored, and the port most often attacked during the week is recorded. The estimated Markov transition matrix for weekly attacks is
$$P=\left(\begin{array}{cc}
0 & 0 & 0 & 0 & 1 \\
0 & 8/13 & 3/13 & 1/13 & 1/13 \\
1/16 & 3/16 & 3/8 & 1/4 & 1/8 \\
0 & 1/11 & 4/11 & 5/11 & 1/11 \\
0 & 1/8 & 1/2 & 1/8 & 1/4
\end{array}\right)$$
with initial distribution $\alpha = (0, 0, 0, 0, 1)$.

(a) Which are the least and most likely attacked ports after 2 weeks?

(b) Find the long-term distribution of attacked ports.

### Answer

```R
P=matrix(c(
0 , 0 , 0, 0 , 1,
0 , 8/13 , 3/13 , 1/13, 1/13 ,
1/16 , 3/16 , 3/8 , 1/4, 1/8 ,
0 , 1/11 , 4/11 , 5/11 , 1/11,
0 , 1/8 , 1/2 , 1/8 , 1/4 ), nrow=5,byrow=T)
a=c(0,0,0,0,1)
parser=c(80,135, 139, 445, 'No attack')
```

```R
cat(sprintf("the most likely port to be attacked after two weeks is: %s,
the least likely port to be attacked after two weeks is: %s ", parser[which.max((a%*%(P%^%2)))],
parser[which.min((a%*%(P%^%2)))]))
```

    the most likely port to be attacked after two weeks is: 139,
    the least likely port to be attacked after two weeks is: 135

```R
print("the long last distribution is:")
print((P%^%100)[1,])
```

    [1] "the long last distribution is:"
    [1] 0.02146667 0.26693333 0.34346667 0.22733333 0.14080000

## 2.27
See gamblersruin.R. Simulate gambler’s ruin for a gambler with initial stake $\$2$, playing a fair game.

(a) Estimate the probability that the gambler is ruined before he wins $\$5$.

(b) Construct the transition matrix for the associated Markov chain. Estimate
the desired probability in (a) by taking high matrix powers.

(c) Compare your results with the exact probability.

### Answer

```R
randomWalk<-function(init_money=50 , prob=1/2, min_state=0, max_state=100, outcomes = c(-1,1)){
    "
    this is is the function that performs one random walk, if you want to perform the simulation you
    can run the gambling walk function beforehand.
    Take into account the taking variables, where the outcomes are the possible results of loosing or earning money
    the min and max state respresent the values where the simulation would stop.

    the outputs are sent as a list so the $ comand can be used.
    "
    money = init_money
    walk = c(money)
    win = F
    while (T){
        money = money+sample(outcomes, 1, prob = c(1-prob,prob))
        walk= c(walk, money)
        if ((money <= min_state) || (money >= max_state)){
            win=(money >= max_state)
            break
        }
    }
    my_list <- list("steps" = length(walk), "won" = win, "walk" = walk)
    return(my_list)
}
GamblingWalk<-function(init_money=50, prob=1/2, min_state=0, max_state=100, outcomes = c(-1,1), sim=100){
    "
    this is is the main function that performs the simulation.
    Take into account the taking variables, where the outcomes are the possible results of loosing or earning money
    the min and max state respresent the values where the simulation would stop.

    the outputs are sent as a list so the $ comand can be used.
    "
    results = c()
    for(i in 0:sim){
        res = randomWalk(init_money, prob, min_state, max_state, outcomes)
        results = c(results,res$won)
    }
    my_list <- list("results" = sum(results)/length(results), "walk" = res$walk)
    return(my_list)

}
```

```R
walkSim = GamblingWalk(init_money = 2, max_state = 5, sim=10000)
print(1-walkSim$results)
```

    [1] 0.5929407

(b) The transition Matrix is given by:

$$P=\left(\begin{array}{cc}
1 & 0 & 0 & 0 & 0 & 0 \\
1/2 & 0 & 1/2 & 0 & 0 & 0 \\
0 & 1/2 & 0 & 1/2 & 0 & 0 \\
0 & 0 & 1/2 & 0 & 1/2 & 0 \\
0 & 0 & 0 & 1/2 & 0 & 1/2 \\
0 & 0 & 0 & 0 & 0 & 1 \\
\end{array}\right)$$

```R
P=matrix(c(1 , 0 , 0 , 0 , 0 , 0,
            1/2 , 0 , 1/2 , 0 , 0 , 0,
            0 , 1/2 , 0 , 1/2 , 0 , 0,
            0 , 0 , 1/2 , 0 , 1/2 , 0,
            0 , 0 , 0 , 1/2 , 0 , 1/2,
            0 , 0 , 0 , 0 , 0 ,1),nrow=6,byrow=T)
a=c(0,0,1,0,0,0)
round(a%*%(P%^%100), 2)
```

0.6	0  	0  	0  	0  	0.4

(c) As seen from the results from (a) and (b) we can see that the simulation is a bit off, even after 10,000 simulations.

```R

```


# Systems of Equations
Imagine you are at a casino, and you have a mixture of £10 and £25 chips. You know that you have a total of 16 chips, and you also know that the total value of chips you have is £250. Is this enough information to determine how many of each denomination of chip you have?

Well, we can express each of the facts that we have as an equation. The first equation deals with the total number of chips - we know that this is 16, and that it is the number of £10 chips (which we'll call ***x*** ) added to the number of £25 chips (***y***).

The second equation deals with the total value of the chips (£250), and we know that this is made up of ***x*** chips worth £10 and ***y*** chips worth £25.

Here are the equations

\begin{equation}x + y = 16 \end{equation}
\begin{equation}10x + 25y = 250 \end{equation}

Taken together, these equations form a *system of equations* that will enable us to determine how many of each chip denomination we have.

## Graphing Lines to Find the Intersection Point
One approach is to determine all possible values for x and y in each equation and plot them.

A collection of 16 chips could be made up of 16 £10 chips and no £25 chips, no £10 chips and 16 £25 chips, or any combination between these.

Similarly, a total of £250 could be made up of 25 £10 chips and no £25 chips, no £10 chips and 10 £25 chips, or a combination in between.

Let's plot each of these ranges of values as lines on a graph:

```python
%matplotlib inline
from matplotlib import pyplot as plt

# Get the extremes for number of chips
chipsAll10s = [16, 0]
chipsAll25s = [0, 16]

# Get the extremes for values
valueAll10s = [25,0]
valueAll25s = [0,10]

# Plot the lines
plt.plot(chipsAll10s,chipsAll25s, color='blue')
plt.plot(valueAll10s, valueAll25s, color="orange")
plt.xlabel('x (£10 chips)')
plt.ylabel('y (£25 chips)')
plt.grid()

plt.show()
```

Looking at the graph, you can see that there is only a single combination of £10 and £25 chips that is on both the line for all possible combinations of 16 chips and the line for all possible combinations of £250. The point where the line intersects is (10, 6); or put another way, there are ten £10 chips and six £25 chips.

### Solving a System of Equations with Elimination
You can also solve a system of equations mathematically. Let's take a look at our two equations:

\begin{equation}x + y = 16 \end{equation}
\begin{equation}10x + 25y = 250 \end{equation}

We can combine these equations to eliminate one of the variable terms and solve the resulting equation to find the value of one of the variables. Let's start by combining the equations and eliminating the x term.

We can combine the equations by adding them together, but first, we need to manipulate one of the equations so that adding them will eliminate the x term. The first equation includes the term ***x***, and the second includes the term ***10x***, so if we multiply the first equation by -10, the two x terms will cancel each other out. So here are the equations with the first one multiplied by -10:

\begin{equation}-10(x + y) = -10(16) \end{equation}
\begin{equation}10x + 25y = 250 \end{equation}

After we apply the multiplication to all of the terms in the first equation, the system of equations look like this:

\begin{equation}-10x + -10y = -160 \end{equation}
\begin{equation}10x + 25y = 250 \end{equation}

Now we can combine the equations by adding them. The ***-10x*** and ***10x*** cancel one another, leaving us with a single equation like this:

\begin{equation}15y = 90 \end{equation}

We can isolate ***y*** by dividing both sides by 15:

\begin{equation}y = \frac{90}{15} \end{equation}

So now we have a value for ***y***:

\begin{equation}y = 6 \end{equation}

So how does that help us? Well, now we have a value for ***y*** that satisfies both equations. We can simply use it in either of the equations to determine the value of ***x***. Let's use the first one:

\begin{equation}x + 6 = 16 \end{equation}

When we work through this equation, we get a value for ***x***:

\begin{equation}x = 10 \end{equation}

So now we've calculated values for ***x*** and ***y***, and we find, just as we did with the graphical intersection method, that there are ten £10 chips and six £25 chips.

You can run the following Python code to verify that the equations are both true with an ***x*** value of 10 and a ***y*** value of 6.

```python
x = 10
y = 6
print ((x + y == 16) & ((10*x) + (25*y) == 250))
```

    True

MathsToML/Module01-Equations, Graphs, and Functions/01-03-Systems of Equations.ipynb


# Introduction to LP Problems - standard form

## Diet problem
you have a list of food, their macronutrients and the price. You want to get the most out of your food intake for the lowest cost possible

(Per portion)

|Product|Energy (kcal)|Proteins (g)|Calcium (mg)|Price (cent)|
|------|-----|-----|-----|-----|
|Oats|110|4|2|2|25|
|Chicken|205|32|12|130|
|Egg|160|13|54|85|
|Milk|160|8|285|70 |
|Cake|420 |22 | 95|
|Bean|260|14|80|98|

Suppose you require 2000kcal, 55g proteins and 800mg of calcium. Write the LP and the write it in its standard form:

## More problems
### 1
\begin{equation}
\min 3x_1+8x_2+4x_3\\
\textrm{subject to} \begin{cases}
x_1+x_2 \ge 8\\
2x_1-3x_2 \le 0\\
x_2 \ge 9 \\
x_1,x_2 \ge 0
\end{cases}
\end{equation}
### 2
\begin{equation}
\max 3x_1+2x_2-x_3+x_4\\
\textrm{subject to} \begin{cases}
x_1 + 2x_2 + x_3 - x_4 \le 5\\
-2x_1 - 4 x_2 + x_3 + x_4 \le -1\\
x_1\ge 0,x_2 \le 0
\end{cases}
\end{equation}

### 3
\begin{equation}
\min x_1 - x_2 + x_3\\
\textrm{subject to} \begin{cases}
x_1 + 2x_2 - x_3 \le 3\\
-x_1 + x_2 + x_3 \ge 2\\
x_1 - x_2 = 10\\
x_1 \ge 0, x_2 \le 0
\end{cases}
\end{equation}

### 4
\begin{equation}
\max x_1 - x_2 + x_3\\
\textrm{subject to} \begin{cases}
x_1 + 2x_2 - x_3 \le 3\\
-x_1 + x_2 + x_3 \ge 2\\
x_1 - x_2 = 10\\
x_1 \ge 0, x_2 \le 0
\end{cases}
\end{equation}

```julia

```


# Eigenvalues and Eigenvectors; or
## How Matrices Really Work

This notebook will first focus on a geometric understanding of eigenvalues and eigenvectors, then introduce some more advanced applications relevant to mathematical modelling. It is intended as a practical introduction, not a rigorous theoretical exposition.

Numerical examples will make use of NumPy and its older sybling, SciPy.

### Imports

```python
import numpy as np
from scipy import linalg
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
```

### A motivating example

The concept of taking the square of a square matrix is straightforward (to a person familiar with matrix multiplication).

Say
\begin{align*}
A = \left(\begin{matrix}8 & 5\\6 & 1\end{matrix}\right),
\end{align*}
so
\begin{align*}
A^2 = A\times A=\left(\begin{matrix}94 & 45\\54 & 31\end{matrix}\right).
\end{align*}

But what if you wanted to raise 2 to the power of a matrix?
\begin{align*}
2^A = \text{?}.
\end{align*}

What does that even mean?

### What is an eigenvalue?

For a matrix $A$, any non-zero vector $\mathbf{v}$ and scalar $w$ such that
\begin{align*}
A\mathbf{v} = w\mathbf{v},
\end{align*}
$w$ is an eigenvalue of $A$ and $\mathbf{v}$ is an eigenvector of $A$. $w$ and $\mathbf{v}$ are an eigenvalue-eigenvector pair. (Practically everyone uses the symbol $\lambda$ to represent eigenvalues, but in Python `lambda` is a reserved keyword, so we will use $w$ throughout.)

### Who cares?

First let's see what matrices do, geometrically speaking.

Start by plotting a grid.

```python
x, y = np.meshgrid(np.arange(-2, 2.1, 0.5), np.arange(-2, 2, 0.5))
xy_vectors = np.array([x.flatten(), y.flatten()])
xy = pd.DataFrame({'x': xy_vectors[0], 'y': xy_vectors[1]})
xy['quadrant'] = -np.sign(xy['x'] * xy['y'])
fig, ax = plt.subplots(figsize=(5, 5))
ax = sns.scatterplot(data=xy, x='x', y='y', hue='quadrant',
                     legend=False, palette=plt.get_cmap('viridis'), ax=ax)
ax.set_aspect('equal')
```

Now generate a random matrix. Ok, this is a random matrix with specific properties, but mostly so the pictures look nice.

```python
while True:
    V = np.random.randint(1, 10, 4).reshape((2, 2))
    if not np.isclose(np.linalg.det(V), 0):
        break
w = (1.5, 2)
A = np.linalg.inv(V) @ np.diag(w) @ V
print('Transformation matrix A:')
print(A)
```

    Transformation matrix A:
    [[ 2.07446809  0.09574468]
     [-0.44680851  1.42553191]]

### Matrices move points around
Plot what happens to each point in the grid $\mathbf{x}$ when it is multiplied by $A$. So plot $A\mathbf{x}$ for each $\mathbf{x}$ in the grid.

```python
transformed_vectors = A @ xy_vectors
transformed_xy = pd.DataFrame(
    {'x': transformed_vectors[0], 'y': transformed_vectors[1]})
transformed_xy['quadrant'] = xy['quadrant']

fig, ax = plt.subplots(figsize=(5, 5))
ax = sns.scatterplot(data=xy, x='x', y='y', hue='quadrant', alpha=0.3,
                     legend=False, palette=plt.get_cmap('viridis'), ax=ax)
ax = sns.scatterplot(data=transformed_xy, x='x', y='y', hue='quadrant',
                     legend=False, palette=plt.get_cmap('viridis'), ax=ax)
ax.set_aspect('equal')
```

Multiplying by the _inverse_ matrix moves the points back to their original locations.

```python
untransformed_vectors = np.linalg.inv(A) @ transformed_vectors
untransformed_xy = pd.DataFrame(
    {'x': untransformed_vectors[0], 'y': untransformed_vectors[1]})
untransformed_xy['quadrant'] = xy['quadrant']

fig, ax = plt.subplots(figsize=(5, 5))
ax = sns.scatterplot(data=transformed_xy, x='x', y='y', hue='quadrant', alpha=0.3,
                     legend=False, palette=plt.get_cmap('viridis'), ax=ax)
ax = sns.scatterplot(data=untransformed_xy, x='x', y='y', hue='quadrant',
                     legend=False, palette=plt.get_cmap('viridis'), ax=ax)
ax.set_aspect('equal')
```

Let's look more closely at what happens in the original transformation $A\mathbf{x}$ by plotting a vector for each transformation.

```python
fig, ax = plt.subplots(figsize=(5, 5))
delta_x = transformed_xy['x'] - xy['x']
delta_y = transformed_xy['y'] - xy['y']
q = ax.quiver(xy['x'], xy['y'], delta_x, delta_y,
              angles='xy', scale_units='xy', scale=1,
              color='purple')
ax = sns.scatterplot(data=transformed_xy, x='x', y='y', hue='quadrant',
                     legend=False, palette=plt.get_cmap('viridis'), ax=ax)
ax.set_aspect('equal')
```

### Enter the eigenvectors
Plot the eigenvalues multiplied by the eigenvectors ($w\mathbf{v}$) for each eigenvalue-eigenvector pair. Note that in the direction of the eigenvectors, the transformation just moves the points further from (or closer to) the origin, without changing their bearings. Note also that the magnitude of the scaling is the eigenvalue.

We have obtained the eigenvalues and eigenvectors using `np.linalg.eig`. Eigenvectors are only determined up to scalar multiples, so NumPy chooses to scale them to be 1 unit long.

```python
w, V = np.linalg.eig(A)
assert np.allclose(w.imag, 0)
w = w.real
V = V.real
V =  w * V
print('w1:', w[0])
print('V1:', V[:,0])
print('w2:', w[1])
print('V2:', V[:,1])
fig, ax = plt.subplots(figsize=(5, 5))
q = ax.quiver(xy['x'], xy['y'], delta_x, delta_y,
              angles='xy', scale_units='xy', scale=1,
              color='purple', alpha=0.5)
ax = sns.scatterplot(data=transformed_xy, x='x', y='y', hue='quadrant', alpha=0,
                     legend=False, palette=plt.get_cmap('viridis'), ax=ax)
q = ax.quiver((0,0), (0,0), V[0], V[1], angles='xy', scale_units='xy', scale=1, color='blue')
ax.set_aspect('equal')
```

### Again, who cares?

Well, any vector $\mathbf{x}$ can be restated as the sum of eigenvectors of $A$. That is,
\begin{align*}
\mathbf{x} = a\mathbf{v}_1 + b\mathbf{v}_2
\end{align*}
So
\begin{align*}
A\mathbf{x} = A(a\mathbf{v}_1 + b\mathbf{v}_2) = aA\mathbf{v}_1 + bA\mathbf{v}_2 = aw_1\mathbf{v}_1 + bw_2\mathbf{v}_2.
\end{align*}

So multiplying by a matrix is like expressing a vector as eigenvector components, then scaling those components. Regardless of the initial $\mathbf{x}$ vector, the components are always scaled by the eigenvalues.

Knowing a matrix's eigenvalues is therefore fundamental to understanding what the matrix "does".

We can figure out $a$ and $b$ by forming the matrix $V=\left(\begin{matrix}\mathbf{v}_1&\mathbf{v}_2\end{matrix}\right)$ and noting that
\begin{align}
\mathbf{x} = a\mathbf{v}_1 + b\mathbf{v}_2 = V\left(\begin{matrix}a\\b\end{matrix}\right),
\end{align}
so
\begin{align*}
\left(\begin{matrix}a\\b\end{matrix}\right) = V^{-1}\mathbf{x}.
\end{align*}

### Eigendecomposition

To put it another way, in the 2x2 case,
\begin{align*}
A\mathbf{v}_1 = w_1\mathbf{v}_1 \quad\text{and}\quad\mathbf{v}_2 = w_2\mathbf{v}_2,
\end{align*}
or
\begin{align*}
AV = \left(\begin{matrix}A\mathbf{v}_1&A\mathbf{v}_2\end{matrix}\right) = \left(\begin{matrix}w_1\mathbf{v}_1&w_2\mathbf{v}_2\end{matrix}\right) =
V\left(\begin{matrix}w_1&0\\0&w_2\end{matrix}\right).
\end{align*}

Multiplying from the right by $V^{-1}$ yields
\begin{align*}
A = V\left(\begin{matrix}w_1&0\\0&w_2\end{matrix}\right)V^{-1},
\end{align*}
which formalises what we saw above. Multiplying from the left by $A$ is like multiplying by $V^{-1}$ to transform the vector into a coordinate space where the basis is the eigenvectors of $A$, scaling the resulting vector by the eigenvalues, then transforming the result back to the original basis by multiplying by $V$.

If it is possible to decompose a matrix $A = VDV^{-1}$, where $D$ is a diagonal matrix of eigenvalues, we say that $A$ is _diagonalisable_. This result generalises for $m\times m$ matrices.

### Some useful facts about eigenvalues and eigenvectors

- Eigenvalues can be complex, and the complex eigenvalues of a real matrix occur in conjugate pairs.
- Eigenvectors that correspond to distinct eigenvalues are linearly independent.
- If two eigenvalues are equal, there can be two eigenvectors for that eigenvalue, and any linear combination of those eigenvectors is also an eigenvector.

### So what's this about $2^A$?

Now that we can decompose diagonalisable matrices, we can write
\begin{align*}
A^2 = VDV^{-1}VDV^{-1} = VDIDV^{-1} = VD^2V^{-1},
\end{align*}
so the square of a matrix has the same eigenvectors as the original matrix, and with eigenvalues that are the squares of the original eigenvalues (remember that $D$ is diagonal). Hopefully it's not too much of a stretch to see that we also get
\begin{align*}
A^n = VD^nV{-1}
\end{align*}
for any whole number $n$.

So if $x$ is a scalar, the Taylor expansion of $2^x$ around $x=0$ is
\begin{align*}
2^x = \sum_{n=0}^\infty \frac{(\ln 2)^nx^n}{n!}.
\end{align*}

From what we now know about eigenvalues and eigenvectors, we can apply that straight away our $m\times m$ matrix $A$,
\begin{align*}
2^A &= \sum_{n=0}^\infty \frac{(\ln 2)^nA^n}{n!} \\
&= \sum_{n=0}^\infty \frac{(\ln 2)^nVD^nV^{-1}}{n!} \\
&= V\sum_{n=0}^\infty \frac{(\ln 2)^nD^n}{n!}V^{-1} \\
&= V\left(\begin{matrix}2^{w_1}&0&\ldots&0\\
0&2^{w_2}&\ldots&0\\
\vdots&\vdots&\ddots&\vdots\\
0&0&\ldots&2^{w_m}\end{matrix}\right)V^{-1}\\
&= V2^DV^{-1}.
\end{align*}

The same goes for any function with a Taylor expansion. For instance for diagonalisable $A$, $\exp(A)=V\exp(A)V^{-1}$ and $\ln(A)=V\ln(A)V^{-1}$. $\exp(A)$ has a dizzying array of applications.

It turns out that if you want to calculate these things numerically, there are better ways than taking the eigendecomposition, but SciPy has that covered.

```python
print(linalg.expm(A))  # matrix exponential
```

    [[ 7.82206821  0.55672986]
     [-2.59807266  4.04867696]]

```python
print(linalg.logm(A))  # matrix logarithm
```

    [[ 0.73599345  0.05508806]
     [-0.2570776   0.36261884]]

```python
print(linalg.funm(A, lambda x: 2**x))  # 2^A
```

    [[ 4.17448958  0.22434374]
     [-1.04693746  2.65393755]]

... or alternatively ...

```python
print(linalg.expm(np.log(2)*A))
```

    [[ 4.17448958  0.22434374]
     [-1.04693746  2.65393755]]


### 1. Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

```python
get_sum = lambda x,y: x+y
get_product = lambda x,y: x*y
get_substract = lambda x,y: x-y
get_division = lambda x,y: x/y
is_bigger_than = lambda x,y: x if x>y else y
is_smaller_than = lambda x,y: x if x<y else y

reduce(get_sum, filter(lambda x: x%3 ==0 or x%5 == 0, range(1,1001)))
```

    234168

### 2. Even Fibonacci Numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

```python
is_even = lambda x: x%2 == 0
is_odd  = lambda x: x%2 != 0

def next_fibonacci_added(list):
    return list[-1]+list[-2]

def generate_fibonacci(limit):
    list = [1,2]
    while next_fibonacci_added(list) <= limit:
        list.append(next_fibonacci_added(list))
    return list

list = generate_fibonacci(4000000)
print reduce(get_sum, filter(is_even, list))
```

    4613732

### 3. Largest Prime Factor

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

```python
def primes_sieve(limit):
    """ Returns  a list of primes < n """
    sieve = [True] * limit
    for i in xrange(3,int(limit**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*len(sieve[i*i::2*i])
    return [2] + [i for i in xrange(3,limit,2) if sieve[i]]

factors_of_x_in_list = lambda num, input_list: filter(lambda x: num%x == 0, input_list)
factors_of_x = lambda x: filter(lambda i: x % i == 0, range(2, int(x / 2)) )
closest_odd_to_sqrt = lambda number: int(number**0.5)+1 if is_odd(int(number**0.5)+1) else int(number**0.5)

def is_prime(limit):
    if limit == 2:
        return True
    if limit%2 == 0:
        return False
    for i in xrange(3,int(limit**0.5)+1,2):
        if limit%i == 0:
            return False
    return True

# limit = 600851475143
# factors = factors_of_x(limit)
# print filter(is_prime, factors)[-1]

from sympy import primefactors
print primefactors(600851475143)[-1]
```

    6857

### 4. Largest Palindrome
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

```python
def is_palindrome(x):
    x=str(x)
    if len(x) == 1:
        return True
    for i in range(len(x)/2):
        if x[i] != x[-1-i]:
            return False
    return True

already_checked = []
palindromes = []
for i in xrange(999,900,-1):
    for k in xrange(999, 900, -1):
        product = i*k
        if product not in already_checked:
            already_checked.append(product)
            if is_palindrome(i*k):
                palindromes.append(product)
sorted(palindromes)

```

    [819918, 824428, 861168, 886688, 888888, 906609]

### 5. Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

```python
primes = primes_sieve(20)
prime_dict = {}
for i in primes:
    prime_dict[i]=0

def next_prime_factor(number):
    for i in primes:
        if number%i == 0:
            return i

number = 20
for number in range(2,21):
    prime_factors = []
    while number >=2:
        npf = next_prime_factor(number)
        prime_factors.append(npf)
        number /= npf

    for i in primes:
        prime_count = len(filter(lambda x: x==i, prime_factors))
        prime_dict[i]= prime_count if prime_count > prime_dict[i] else prime_dict[i]

final_product = 1
for i in prime_dict:
    final_product *= i**prime_dict[i]
print final_product
```

    232792560

### 6. Sum square difference

The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

```python
print reduce(get_sum, range(1,101))**2 -  reduce(get_sum, map(lambda x: x**2, range(1,101)))
```

    25164150

### 7. 10001st prime
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

```python
primes_sieve(1000000)[10000]
```

    104743

### 8.Next Largest product in a series
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

```python
number ="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
products = []
for i in range(len(number)):
    a_set = number[i:i+14]
    products.append(reduce(get_product, map(lambda char: int(char), a_set)))
print reduce(is_bigger_than, products)

```

    70573265280

### 9. Special Pythagorean triplet
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

```python
squares_list = filter(lambda x: x** 0.5 == int(x**0.5), range(1,1000000))

```

```python
squares_list[:10]
squares_set = set(squares_list)
triplets = []
for index,square in enumerate(squares_list):
    for another_square in squares_list[index+1:]:
        if another_square + square in squares_set:
            triplets.append((square, another_square, square+another_square))

for number in filter(lambda x: x[0]**(.5) + x[1]**(.5) + x[2]**(.5) == 1000, triplets)[0] :
    print number**(.5)

```

    200.0
    375.0
    425.0

### 10. Summation of primes
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

```python
reduce(get_sum,filter(lambda x: x<2000000, primes_sieve(10000000)))
```

    142913828922

### 11. Largest product in a grid
Problem 11
In the 20×20 grid below, four numbers along a diagonal line have been marked in red.

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 \n
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 \n
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 \n
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 \n
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 \n
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 \n
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 \n
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 \n
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 \n
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 \n
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 \n
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 \n
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 \n
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 \n
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 \n
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 \n
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 \n
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 \n
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 \n
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 \n

The product of these numbers is 26 × 63 × 78 × 14 = 1788696.

What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?

```python
grid = [[8,2,22,97,38,15,0,40,0,75,4,5,7,78,52,12,50,77,91,8],
[49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,4,56,62,0],
[81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,3,49,13,36,65],
[52,70,95,23,4,60,11,42,69,24,68,56,1,32,56,71,37,2,36,91],
[22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80],
[24,47,32,60,99,3,45,2,44,75,33,53,78,36,84,20,35,17,12,50],
[32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70],
[67,26,20,68,2,62,12,20,95,63,94,39,63,8,40,91,66,49,94,21],
[24,55,58,5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72],
[21,36,23,9,75,0,76,44,20,45,35,14,0,61,33,97,34,31,33,95],
[78,17,53,28,22,75,31,67,15,94,3,80,4,62,16,14,9,53,56,92],
[16,39,5,42,96,35,31,47,55,58,88,24,0,17,54,24,36,29,85,57],
[86,56,0,48,35,71,89,7,5,44,44,37,44,60,21,58,51,54,17,58],
[19,80,81,68,5,94,47,69,28,73,92,13,86,52,17,77,4,89,55,40],
[4,52,8,83,97,35,99,16,7,97,57,32,16,26,26,79,33,27,98,66],
[88,36,68,87,57,62,20,72,3,46,33,67,46,55,12,32,63,93,53,69],
[4,42,16,73,38,25,39,11,24,94,72,18,8,46,29,32,40,62,76,36],
[20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,4,36,16],
[20,73,35,29,78,31,90,1,74,31,49,71,48,86,81,16,23,57,5,54],
[1,70,54,71,83,51,54,69,16,92,33,48,61,43,52,1,89,19,67,48]]

indeces=[]
# horzontal and vertical
for i in range(20):
    for j in range(17):
        indeces.append([(i,j),(i,j+1),(i,j+2),(i,j+3)])
        indeces.append([(j,i),(j+1,i),(j+2,i),(j+3,i)])

# \
for i in range(17):
    for j in range(17):
        indeces.append([(i,j),(i+1,j+1),(i+2,j+2),(i+3,j+3)])
# /
for i in range(17):
    for j in range(3,20):
        indeces.append([(i,j),(i+1,j-1), (i+2,j-2), (i+3,j-3)])

def get_nums_from_matrix(matrix, list_of_indeces):
    list_of_numbers = []
    for i in list_of_indeces:
        x = i[0]
        y = i[1]
        list_of_numbers.append(matrix[x][y])
    return list_of_numbers

ll_numbers = []
for i in indeces:
    ll_numbers.append(get_nums_from_matrix(grid,i))

print reduce(is_bigger_than, map(lambda x: reduce(lambda a,b: a*b , x), ll_numbers))

```

    70600674

### 12. Highly divisible triangular number
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

```python
# >>> import itertools
# >>> list2d = [[1,2,3],[4,5,6], [7], [8,9]]
# >>> merged = list(itertools.chain.from_iterable(list2d))

import itertools

def factors(n):
    a_list = []
    for i in ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0):
        a_list.extend(i)
    return sorted(a_list)

def binary_search(array, target):
    lower = 0
    upper = len(array)
    while lower < upper:
        x = lower + (upper - lower) // 2
        val = array[x]
        if target == val:
            return x
        elif target > val:
            if lower == x:
                break
            lower = x
        elif target < val:
            upper = x

tri_numbers = []
for number in range(1,1000000):
    new_tri = tri_numbers[-1] + number if len(tri_numbers) != 0 else number
    tri_numbers.append(new_tri)

# for i in range(500000,600000):
#     if len(factors(tri_numbers[i]))>500:
#         print tri_numbers[i]
#         break
# for number in reversed(tri_numbers):
#     get_factors(number)
# for i in tri_numbers[-1:1]:
#     if len(print len(factors(tri_numbers[-1]))
# reduce(list.__add__, [[1, 2, 3], [4, 5], [6, 7, 8]], [])

```

```python
count = 0
for i in tri_numbers:
    if len(factors(i))>=500:
        count +=1
        print i
    if count ==10:
        break
```

    76576500
    103672800
    137373600
    147911400
    163723560
    214980480
    228648420
    231221760
    236215980
    250891200

### Large sum

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690

```python
list_of_numbers = ["37107287533902102798797998220837590246510135740250",
"46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629",
"91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676",
"89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738",
"44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276",
"70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331",
"64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407",
"58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390",
"35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829",
"71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604",
"53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721",
"45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412",
"81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483",
"62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209",
"55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515",
"80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581",
"16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030",
"87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210",
"69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271",
"65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307",
"94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271",
"25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962",
"24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403",
"34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583",
"11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533",
"67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774",
"76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967",
"23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501",
"18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381",
"34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635",
"29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922",
"62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519",
"86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308",
"82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500",
"42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789",
"32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337",
"62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504",
"99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724",
"76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852",
"87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786",
"71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116",
"66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714",
"85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297",
"64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011",
"94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699",
"15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353",
"44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954",
"81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608",
"22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617",
"72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722",
"53503534226472524250874054075591789781264330331690"]

print str(reduce(get_sum, map(lambda x: int(x[:10]), list_of_numbers)))[:10]
print str(reduce(get_sum, map(lambda x: int(x[:11]), list_of_numbers)))[:10]
print str(reduce(get_sum, map(lambda x: int(x[:50]), list_of_numbers)))[:10]

```

    5537376229
    5537376230
    5537376230

### Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

```python
def get_collatz(x):
    if type(x) != type([]):
        input_list = [x]
    else:
        input_list = x
    if input_list[-1] == 1:
        return x

    if input_list[-1]%2 == 0:
        input_list.append(input_list[-1]/2)
    else:
        input_list.append(input_list[-1]*3 +1)
    return get_collatz(input_list)

cache = {}
longest_seq = 0
longest_gen = 0
for i in range(2,1000000):
    if i in cache:
        continue
    collatz = get_collatz(i)
    for index,number in enumerate(collatz):
        if number not in cache:
            cache[number] = len(collatz) - index
            if longest_seq < cache[number]:
                longest_seq = cache[number]
                longest_gen = number

print longest_gen, longest_seq
```

    837799 525

```python
[13][-1]%2 ==0
```

    False

### Lattice paths

Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20×20 grid?

```python

```

### Power digit sum

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?

```python

```

### Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.

```python

```

### Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3
7 4
2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

```python

```

### Counting Sundays

You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

```python
a_set = {2, 4, 5, 9, 12, 21, 30, 51, 76, 127, 195}
b_set = {1, 2, 3, 5, 6, 8, 9, 12, 15, 17, 18, 21}
print a_set.symmetric_difference(b_set)
print b_set.symmetric_difference(a_set)
```

    set([1, 3, 4, 6, 8, 76, 15, 17, 18, 195, 127, 30, 51])
    set([3, 1, 195, 4, 6, 8, 76, 15, 17, 18, 51, 30, 127])

```python

```


```
%pylab inline
```

    Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
    For more information, type 'help(pylab)'.

```
from sympy import Symbol, fresnels, fresnelc, oo, I, re, im, series, Rational, sin, cos, exp, plot
from sympy.plotting import plot, plot_parametric
from matplotlib.pyplot import figsize
```

Plot of the two Fresnel integrals $S(x)$ and $C(x)$

```
x = Symbol("x")
```

```
plot(fresnels(x), fresnelc(x), (x, 0, 8))
```

The Cornu spiral defined as the parametric curve $u(t),v(t) := C(t), S(t)$

```
figsize(8,8)
plot_parametric(fresnelc(x), fresnels(x))
```

Compute and plot the leading order behaviour around $x=0$

```
ltc = series(fresnelc(x), x, n=2).removeO()
lts = series(fresnels(x), x, n=4).removeO()
```

```
lts, ltc
```

    (pi*x**3/6, x)

```
figsize(4,4)
plot(fresnels(x), lts, (x, 0, 1))
plot(fresnelc(x), ltc, (x, 0, 1))
```

Compute and plot the asymptotic series expansion at $x=\infty$

```
# Series expansion at infinity is not implemented yet
#ats = series(fresnels(x), x, oo)
#atc = series(fresnelc(x), x, oo)
```

```
# However we can use the well known values
ats = Rational(1,2) - cos(pi*x**2/2)/(pi*x)
atc = Rational(1,2) + sin(pi*x**2/2)/(pi*x)
```

```
figsize(4,4)
plot(fresnels(x), ats, (x, 6, 8))
plot(fresnelc(x), atc, (x, 6, 8))
```
Another nice example of a parametric plot

```
alpha = Symbol("alpha")
r = 3.0
circ = r*exp(1.0j*alpha)
```

```
figsize(8,8)
plot_parametric(re(fresnelc(circ)), im(fresnelc(circ)), (alpha, 0, 2*pi))
```

```

```