```python
%run ../../common/import_all.py

from common.setup_notebook import set_css_style, setup_matplotlib, config_ipython
config_ipython()
setup_matplotlib()
set_css_style()
```

# (Some of) the most famous distributions

Let's go look at some of the most famous/common distributions you can see around. Not exhaustive and maybe you could even say that what's most popular is a matter of who you are.

Now that we're at it, let's also calculate expected value and variance (at least) for these ones. For what those things are, have a look at the notebook about moments of a distribution in this same section!

## Easy-peasy: the Uniform

Given a continuous variable $X$ taking values in interval $\in [a,b]$, a *uniform* distribution is one where every possible value has the same probability. Its pdf is simply

$$
p(x) = \frac{1}{b-a} \ ,
$$

because you have 1 case over the total possible cases, which is the width of the interval.

The expected value is

$$
\mathbb{E}[X] = \int_a^b \text{d} x \ \frac{1}{b-a} = \frac{b+a}{2} \ ,
$$

which, as expected (!), corresponds to the middle point of the interval because given that every point is equiprobable, this is where we fall by averaging values.

The variance is

$$
\begin{align}
Var[X] &= \int_a^b \text{d} x \ x \Big(x - \frac{1}{b-a}\Big)^2  \\
&= \int_a^b \text{d} x \ x^3 - 2x^2\frac{1}{b-a} + \frac{x}{(b-a)^2} \\
&= \frac{b^4 - a^4}{4} - \frac{2}{3} \frac{b^3 - a^3}{b-a} + \frac{b^2-a^2}{2(b-a)^2} \\
&= \frac{(b^2 - a^2)(b^2 + a^2)}{4} - \frac{2}{3} \frac{(b-a)(b^2 + ab + a^2)}{b-a} + \frac{b+a}{2(b-a)} \\
& = \ \frac{(b-a)^2}{12} \ .
\end{align}
$$

## Success or failure: the Bernoulli

Let's consider a binary variable $X \in \{0,1\}$, so that it can take the two values 1 (which we'll call the *success*) or 0 (which we'll call the *failure*). The prototype of this is the result of flipping of a coin. Let's also call $\mu$ the probability of the success so that, by definition

$$
P(X=1) = \mu \ ; P(X=0) = 1 - \mu \ ,
$$

so that the [pmf](probfunctions-histogram.ipynb#The-PMF) (it is a discrete variable) can be expressed as

$$
p(x;\mu) = \mu^x(1-\mu)^{1-x}
$$

because when we have $x=1$ we are left with $\mu$ and when we have $x=0$ we are left with $1-\mu.$

Such distribution has  expected value

$$
\mathbb{E}[X] = \sum_{x \in \{0,1\}} x \mu^x(1-\mu)^{1-x} = 0 + 1\mu(1-\mu)^0 = \mu
$$

and variance

$$
Var[X] =  \sum_{x \in \{0,1\}} x^2 \mu^x(1-\mu)^{1-x} - \mu^2 = \mu - \mu^2 = \mu(1-\mu)
$$

The Bernoulli distribution is a special case of a binomial distribution for a single observation, see below!

## More successes and more failures: the Binomial

The binomial distribution describes the probability of observing $k$ occurrences of $x=1$ in a set of $n$ samples from a Bernoulli distribution. $\mu$ is the probability of  observing $x=1$. The pmf will be then

$$
p(x;\mu) = {{n}\choose{k}} \mu^k (1-\mu)^{n-k} \ ,
$$

because we have ${{n}\choose{k}}$ ways of creating groups of $k$ from $n$ values and because each extraction is a Bernoulli.

The expected value is

$$
\mathbb{E}[X] = n \mu
$$

and the variance is

$$
Var[X] =  n \mu (1- \mu)
$$

Head to [Wikipedia](https://en.wikipedia.org/wiki/Binomial_distribution) for the proofs.

## Extending all that^: the Multinomial

It is a multivariate generalisation of the binomial and gives the distribution over counts $m_k$ for a $k$-state discrete variable to be in state $k$ given a total of observations $n$.

An example is the extraction of $n$ balls of $k$ different colours from a bag, replacing the extracted ball after each draw. The pmf reads

$$
p(m_1, m_2, \ldots, m_k, \mu_1, \mu_2, \ldots, \mu_k, n) =  {{n}\choose{m_1 m_2 \ldots m_k}}  \mu_1^{m_1} \mu_2^{m_2} \ldots \mu_k^{m_k}
$$

and we have

$$
\mathbb{E}[m_k]  = n \mu_k \ ,
$$

$$
Var[m_k] = n \mu_k(1-\mu_k)
$$

## He majesty the Gaussian

The gaussian distribution (after C F Gauss) is also called a *normal* distribution or, in some cases, bell curve (from its shape). Let $\mu$ be the expected value and $\sigma$ the standard deviation,

$$
p(x; \mu, \sigma) =  \frac{1}{\sqrt{2 \pi \sigma^2}} e^{-\frac{1}{2 \sigma^2} (x-\mu)^2}
$$

It is usually indicated as $\mathcal N(\mu, \sigma^2)$, where the $\mathcal N$ stands for "normal".

## The Beta

Given a continuous variable $x \in [0,1]$, the distribution is parametrized by  $\alpha, \beta > 0$ which define its shape.

$$
p(x; \alpha, \beta) =  \mathcal{N} x^{\alpha-1}(1-x)^{\beta-1} \ ,
$$

where $\mathcal{N}$ is the normalisation constant:

$$
\mathcal{N} = \frac{\Gamma(\alpha + \beta)}{\Gamma(\alpha) \Gamma(\beta)} = \frac{1}{\int d u u^{\alpha -1}(1-u)^{\beta-1}} \ ,
$$

with $\Gamma$ the gamma function (extension of the factorial to real and complex numbers), defined as

$$
\Gamma(t) = \int_0^\infty x^{t-1} e^{-x} dx
$$

and

$$
\frac{\Gamma(\alpha + \beta)}{\Gamma(\alpha) \Gamma(\beta)} = \frac{1}{B(\alpha, \beta)} \ ,
$$

$B$ being the  beta function.

The beta distribution is the conjugate prior of the Bernoulli distribution for which $\alpha$ and $\beta$ are the prior number of observations $x=1$ and $x=0$. When $\alpha=\beta=1$, it reduces to a uniform distribution.

## The Student's t

*Student* was the pseudonym of W Gosset, which we can all consider ourselves very grateful to, given all his work in statistics. He was working at the Guinness brewery in Dublin, produces various intellectual findings while working with beer data, but had to publish under a false name due to company's regulations, and he chose "Student".

This distribution arises when estimating the mean of a normally distributed population in situations where the sample size is mall and the population standard deviation is unknown. Hence, it describes a sample extracted from said population: the larger the sample, the more the distribution resembles the normal.

$$
p(x; \nu) = \frac{\Gamma(\frac{\nu+1}{2})}{\sqrt{\nu \pi} \Gamma(\frac{\nu}{2})}  \Big(1 + \frac{x^2}{\nu}\Big)^{-\frac{\nu+1}{2}}
$$

$\nu$ is the number of degrees of freedom. For $\nu=1$, the distribution reduces to the  Cauchy distribution.

## The elegant Chi-squared, $\chi^2$

It is the distribution (with $k$ degrees of freedom) of the sum of the squares of $k$ independent standardised normal variables $z_i$ (that is, normal variables standardised to have mean 0 and standard deviation 1). It is a special case of the $\Gamma$ distribution.

$$
Q = \sum_1^k z_i^2 \ ,
$$

So

$$
Q \sim \chi^2(k)
$$

and depends on the degrees of freedom.

## The Poisson: events in time or space

It is a discrete probability distribution and describes the probability that a given number of events occurs in a fixed interval of time and/or space if they are known to occur with a certain (known) average rate and independently of the time and/or distance of the last event.

An example is the mail you receive per day. Suppose on average you receive 4 mails per day. Assuming that the events "mail arriving" are independent, then it is reasonable to assume that the number of mails received each day follows a Poissonian. Other examples are the number of people in a queue at a given time of the day or the number of goals scored in a world cup match, see below.

$$
P(k) = \frac{\lambda^k e^{-\lambda}}{k!} \ ,
$$

where $k = 0, 1, 2, \ldots$ is the number of events in an interval and $\lambda$ the average number of such events in the same interval.

The expected value is

$$
\mathbb{E}[k] = \sum_{k \geq 0}  k \frac{\lambda^k e^{-\lambda}}{k!} = \sum_{k \geq 1} \lambda \frac{\lambda^{k-1}}{(k-1)!} e^{-\lambda} = \lambda e^{-\lambda} e^\lambda = \lambda
$$

and the variance is

$$
\begin{align*}
Var[k] &= \mathbb{E}[k^2] - \mathbb{E}^2[k] \\
         &=  \sum^{k \geq 0} k^2 \frac{\lambda^k e^{-\lambda}}{k!} - \lambda^2 \\
         &= \lambda e^{-\lambda} \sum_{k \geq 1} k \frac{\lambda^{k-1}}{(k-1)!} - \lambda^2 \\
         &= \lambda e^{-\lambda} \Big[ \sum_{k \geq 1} (k-1) \frac{\lambda^{k-1}}{(k-1)!} + \sum_{k \geq 1} \frac{\lambda^{k-1}}{(k-1)!} - \lambda^2 \Big] \\
         &= \lambda \Big[ \lambda \sum_{k \geq 2} \frac{1}{(k-2)!} \lambda^{k-2} + \sum_{k \geq 1} \frac{1}{(k-1)!} \lambda^{k-1} - \lambda^2 \Big] \\
         &= \lambda e^{-\lambda} \Big[ \lambda \sum_{k \geq 2} \frac{1}{(k-2)!} \lambda^{k-2} + \sum_{k \geq 1} \frac{1}{(k-1)!} \lambda^{k-1}  - \lambda^2\Big] \\
         &= \lambda e^{-\lambda} \Big[ \lambda \sum_{i \geq 0} \frac{1}{i!}\lambda^i + \sum_{j \geq 0} \frac{1}{j!} \lambda^j \Big] \\
         &= \lambda e^{- \lambda} [\lambda e^\lambda  + e^\lambda] - \lambda^2 = \lambda^2 + \lambda - \lambda^2 = \lambda
\end{align*}
$$

So expected value and variance are the same and equal to the average rate of occurrence.

The Poisson distribution is appropriate if

* the events are independent, _i.e._, the occurrence of one of them does not affect the probability that a second one occurs;
* the rate st which events occur is constant;
* two events cannot occur at the same time;
* the probability of an occurrence of an event in an interval is proportional to the length of the interval

**Example**

Knowing from historical data that the average number of goals scored in a world football match is 2.5, and because the phenomenon can be described by a Poissonian, we have

$$
P(k \text{ goals in a match}) = \frac{2.5^k e^{-2.5}}{k!} \ ,
$$

and we can calculate the expected value and teh variance as above.

An example of a phenomenon which violates the Poissonian assumptions would be the number of students arriving at the student union: the rate is not constant (as it is low during class time and high between class times) and events are co-occurring (students tend to come in groups).

## The Dirichlet

It is a continuous multivariate distribution and the generalisation of the beta distribution, typically denoted as $\text{Dir}(\alpha)$, $\alpha$ being the parametrising vector such that $\alpha = (\alpha_i), \alpha_i \in \mathbb{R}, \alpha_i > 0 \forall i$. It is usually used as a prior in bayesian statistics as it is the conjugate prior of the multinomial distribution.

A Dirichlet distribution of order $k \geq 2$ with parameters $\alpha_i$ has the probability density function

$$
f(x_1, \ldots, x_k; \alpha_1, \ldots, \alpha_k) = \frac{1}{B(\alpha)} \Pi_{i=1}^k x_i^{\alpha_i - 1}
$$

with $B$ being the beta function in $\mathbb{R}^{k-1}$ and $x$ living on the open $(k-1)$-dimensional simplex $x_1, \ldots, x_k > 0$, $x_1 + \ldots + x_{k-1} < 1$, $x_k = 1 - x_1 - \ldots - x_{k-1}$.

## The power of the power-law

The power-law is a great one, a deserves a bit of commentary.

### What's a power law, in general

A power law is, in general, a mathematical function of type

$$
f(x) \propto x^\alpha \ ,
$$

so that in a log-log plot it will appear as a straight line because

$$
f(x) = Ax^\alpha \Rightarrow \log(f(x)) = \log A + \alpha \log x \ .
$$

Let's look at it.

```python
# Plotting a power function

x = np.array([i for i in np.arange(0.1, 1.1, 0.01)])
y = np.array([item**-0.3 for item in x])

plt.plot(x, y, label='$y = x^{-0.3}$')
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.xticks([i for i in np.arange(0, 1.1, 0.1)])
plt.title("A power law")
plt.legend()
plt.show();
```

## Power law distributions

Newman's paper in [[1]](#1) is a great source about the topic and lots of what will be presented here is re-elaborated from there. It is also a very clearly written and enjoyable paper. The Wikipedia page [[3]](#3) on the topic is also quite well written.

We talk of data distributed power law when the quantity we are measuring has a probability which is a power of the quantity itself, so that a power-law distribution has a probability density function typically written as

$$
p(x) = A x^{-\alpha} \ ,
$$

where $A$ is the normalisation constant.

We put a minus sign in front of the exponent (with respect to what we wrote above for a generic power-law function) as we're thinking of a decreasing relation.

There must be a minimum value $x_{min}$, otherwise it wouldn't be normalisable (the area under the curve would diverge, see figure above).

As for the exponent, we need to have $\alpha > 1$ for it to be a pdf, hence integrable, because by definition,

$$
\begin{align}
1 &= \int_{x_{min}}^\infty p(x) \text{d} x \\
&= A \int_{x_{min}}^\infty x^{-\alpha} \text{d} x \\
&= \frac{A}{-\alpha + 1} \Big[x^{-\alpha + 1}\Big]_{x_{min}}^\infty \\
\end{align}
$$

which is only non-diverging when $\alpha > 1$. From here, the normalisation constant is $A = (\alpha - 1) \ x_{min}^{\alpha-1}$.

### Where you find them

Many observed phenomena in several disciplines follow power-law distributions, examples are the Zipf in _linguistics_, the Pareto in _economics_, the _Taylor_ in ecology, ... In a typical situation though, it is the tail of a distribution which is power-law.

In [[5]](#5) Mandelbrot and Taleb exquisitely talk about when using a gaussian paradigm for a power law phenomenon can lead to disaster in finance.

### What makes them interesting

* the absence of a typical _scale_: while other distributions (the gaussian being the king example) will have a typical value which can be used as representative of the distribution (along with an error), power laws are scale-free: values can span several orders of magnitude

* _scale invariance_: if the independent variable is rescaled by a factor, the law only gets affected by proportional scaling, so a consequence, this means that all power laws with the same exponents are rescaled versions of each other:

$$p(cx) = A (cx)^{-\alpha} = A c^{-\alpha} x^{-\alpha} \ ,$$

* _long tail_: in a power law, the tail is fat, meaning that the frequency of the highly frequent items is higher than it would be in other distributions This is brilliantly explained in this blog post by Panos Ipeirotis [[4]](#4), where he points out that despite the fact that a power law is often described as a distribution where "there are a lot of very unfrequent items and a few very frequent ones", this is a distortion of reality: what is striking of a power law is that actually the very frequent items occur much more frequently than they would in other distributions.

### Moments

The _mean_ of a power law is only defined when $\alpha > 2$:

$$
\begin{align}
\mathrm{E}[x] = \bar x &= A \int_{x_{min}}^\infty x x^{-\alpha} \text{d} x \\
&= A \int_{x_{min}}^\infty x^{-\alpha + 1} \text{d} x \\
&= \frac{A}{-\alpha + 2} \Big[x^{-\alpha + 2}\Big]_{x_{min}}^\infty
\end{align}
$$

The _variance_ of a power law is only defined when $\alpha > 3$:

$$
\begin{align}
\mathrm{Var}[x] &= A \int_{x_{min}}^\infty (x-\bar x)^2 x^{-\alpha} \text{d} x \\
&= A \int_{x_{min}}^\infty x^{-\alpha + 2} + (\bar x)^2 x^{-\alpha} - 2 x^{-\alpha-1} \bar x \text{d} x
\end{align}
$$

### Plotting it

A simple plot in log-log scale of the histogram of data helps the eye spot potential power-law behaviours, but this alone is a very unreliable way of identifying the distribution. Because there is much less data in the tail than there is in the head of the distribution, the tail will appear very noisy.

A better way is to plot the data histogrammed, but with logarithmic binning, and in log-log scale. This helps reduce the noisy tail problem.

## References

To be honest these are all about power-laws.

1.  M E Newman, [Power laws, Pareto distributions and Zipf’s law](https://arxiv.org/abs/cond-mat/0412004), *Contemporary physics* 46.5, 2005
2.  J Alstott, E Bullmore, D Plenz, [powerlaw: a Python package for analysis of heavy-tailed distributions](http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0085777), *PloS ONE* 9.1, 2014
3.  [Wikipedia on power laws](https://en.wikipedia.org/wiki/Power_law)
4.  P Ipeirotis, [Misunderstandings of power laws](http://www.behind-the-enemy-lines.com/2008/01/misunderstandings-of-power-law.html)
5.  B Mandelbrot, N N Taleb, [How the Finance Gurus Get Risk All Wrong](http://archive.fortune.com/magazines/fortune/fortune_archive/2005/07/11/8265256/index.htm), Fortune magazine, Jul 2005

```python

```


---
layout: page
title: Risco
nav_order: 6
---

[](https://colab.research.google.com/github/icd-ufmg/icd-ufmg.github.io/blob/master/_lessons/06-risco.ipynb)

# Risco
{: .no_toc .mb-2 }

Entendendo a importância da média
{: .fs-6 .fw-300 }

{: .no_toc .text-delta }
Resultados Esperados

1. Revisar conceitos de Probabilidade ligados a média
1. Entendimento da lei dos grandes números
1. Entendimento de um erro quadrado
1. Entendimento inicial do teorema central do limite

---
**Sumário**
1. TOC
{:toc}
---

```python
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as ss
```

```python
plt.style.use('seaborn-colorblind')

plt.rcParams['figure.figsize']  = (16, 10)
plt.rcParams['axes.labelsize']  = 20
plt.rcParams['axes.titlesize']  = 20
plt.rcParams['legend.fontsize'] = 20
plt.rcParams['xtick.labelsize'] = 20
plt.rcParams['ytick.labelsize'] = 20
plt.rcParams['lines.linewidth'] = 4
```

```python
plt.ion()
```

```python
def despine(ax=None):
    if ax is None:
        ax = plt.gca()
    # Hide the right and top spines
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

    # Only show ticks on the left and bottom spines
    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')
```

```python
np.random.seed(98)
```

## Introdução

Vamos iniciar esta aula com um conjunto de dados sintéticos. Os mesmos vão ser compostos de uma amostra de uma distribuição Normal. Para fazer tal amostra, use a biblioteca `numpy.random`. Abaixo, nossos dados são gerados a partir de uma distribuição Beta.

```python
data = np.random.beta(3, 2, size=50)
data
```

    array([0.42286448, 0.43952776, 0.56410659, 0.52424368, 0.87894774,
           0.26145645, 0.74002066, 0.42867438, 0.8295208 , 0.52435508,
           0.44742948, 0.47979578, 0.60098265, 0.76409969, 0.23832042,
           0.75521248, 0.85631425, 0.43665272, 0.1120301 , 0.58452727,
           0.66005119, 0.18514747, 0.47336954, 0.4862915 , 0.69066877,
           0.80754802, 0.64454602, 0.28349298, 0.90644721, 0.74051758,
           0.77360182, 0.4609454 , 0.57215881, 0.5802893 , 0.51898876,
           0.91762035, 0.81602591, 0.6226551 , 0.46126971, 0.94869495,
           0.83256912, 0.67050373, 0.6758471 , 0.66876075, 0.78683386,
           0.26062848, 0.35454389, 0.65312894, 0.78611324, 0.48682953])

Agora, vamos visualizar os dados. Observe alguns pontos:

1. Dos dados apenas não sabemos a distribuição.
1. Parece um pouco com uma coisa multimodal, mas não é.
1. Sabemos que não é! Pois geramos uma beta.

```python
plt.subplot(2, 1, 1)
plt.hist(data, edgecolor='k');
plt.ylabel('# Linhas')
plt.xlabel('X')
despine()

plt.subplot(2, 1, 2)
plt.scatter(data, np.ones(len(data)), s=80, edgecolor='k')
ax = plt.gca()
ax.set_yticklabels([])
plt.xlabel('X')
despine()

plt.tight_layout(pad=0)
```

### Lei dos Grandes Números

**Do Wikipedia:** A lei dos grandes números (LGN) é um teorema fundamental da teoria da probabilidade, que descreve o resultado da realização da mesma experiência repetidas vezes. De acordo com a LGN, a média aritmética dos resultados da realização da mesma experiência repetidas vezes tende a se aproximar do valor esperado à medida que mais tentativas se sucederem. Em outras palavras, quanto mais tentativas são realizadas, mais a probabilidade da média aritmética dos resultados observados irá se aproximar da probabilidade real.

A prova da lei dos grandes números requer um conhecimento da [Desigualdade de Chebyshev](https://en.wikipedia.org/wiki/Chebyshev%27s_inequality). Não vamos provar no curso. Os que fizerem probabilidade devem conhecer a prova. Caso não tenha feito, não se preocupe, não vamos precisar fazer a prova na mão. Precisamos apenas saber do enunciado:

Considerando $X_1, X_2, \cdots X_n$ uma sequência infinita de variáveis aleatórias i.i.d. com valor esperado $E[X_i]  = E[X_{i-1}]$ = $\mu$. Além do mais, a média amostral de cada VA é: $\overline{X}_n=\frac1n(X_1+\cdots+X_n)$.

A lei dos grandos números fala que:
$$\lim_{n \to \infty} P\left ( \left| \overline{X}_n - \mu \right | < \varepsilon \right ) = 1$$

__Como interpretar a lei:__ Com dados suficientes, a probabilidade de que a média dos meus dados $\overline{X}_n$ se aproxime da média real da população $\mu$ dentro de um fator $\varepsilon>0$ tende para um. Note que $\varepsilon>0$, ou seja, nunca teremos a média real da população. Sempre estamos dentro de erro, pequeno, mas positivo.

Abaixos vamos ver a mesma! Do primeiro plot observe como a média converge com mais amostras.

```python
mu = 3 / (3 + 5)
xax = np.arange(2, 10000)
yax = []
diff = []
for size in xax:
    data = np.random.beta(3, 2, size=size)
    yax.append(data.mean())
    diff.append((mu - data.mean())**2) # Diferença ao quadrado
plt.plot(xax, yax)
plt.ylabel('Média Amostral')
plt.xlabel('Tamanho da Amostra')
despine()
```

Abaixo temos o erro ao quadrado. Note que nunca é zero. A lei não fala isto, fala que a distância fica dentro de um epsilon. A lei dos grandes números não fala em erro ao quadrado, foi feito aqui pois liga com a definição de Risco de um estimador (mais na frente).

```python
plt.plot(xax, diff)
plt.ylabel('(Média Amostral - Média Real)$^2$')
plt.xlabel('Tamanho da Amostra')
despine()
```

### Valor Esperado do Estimador

Observe que nosso ponto de partida foi uma única amostra. Para amostras diferentes da mesma população nosso estimador pode ser comportar diferente. Como podemos entender tal efeito? Simples, vamos gerar várias amostras! Para cada uma, temos um estimador diferente.

**Notação:** $X_1, X_2, X_3, X_4, \cdots, X_n \sim Beta(3, 2)$

Por clareza, vamos assumir que cada amostra tem um tamanho igual (e.g., 50). Podemos então entender cada amostra como sendo gerado de uma mesma variável aleatória (a beta). O sinal $\sim$ significa amostre dados. Assim, cada amostra é independente e distribuída de forma identica (todas são geradas de uma Beta(3, 2)).

Abaixo mostro o histograma de 1000 estimadores da média amostral. Como que chegamos no gráfico?
1. Gere $X_1, X_2, X_3, X_4, \cdots, X_{100}$
1. Compute a média de cada $X_i$
1. Plote

### Um estimador Bom

```python
estimativas = []
for _ in range(1000):
    data = np.random.beta(3, 2, size=50)
    estimativas.append(data.mean())
```

Com tais amostras podemos explorar os conceitos de viés e variancia de um estimador. Note que não estamos falando dos dados, sim dos valores estimados. A intuição aqui é que: Cada amostra de dados leva para uma estimativa diferente. Quão próximo serão tais estimativas de um valor real? Como tais estimativas variam?

No gráfico abaixo é possível ver que o estimador fica em torno do valor real $\mu = 0.6$.

```python
plt.hist(estimativas, edgecolor='k');
plt.xlabel(r'$\hat{\theta}$')
plt.ylabel('Freq em 1000 amostras')
plt.xlim(.5, .7)
despine()
```

Agora vamos fazer um estimador com amostras de tamanho 10000. Observe como estamos mais perto do valor real. É esperado, pela lei dos grandes números, quanto maior a quantidade de dados mais perto ficamos.

```python
estimativas = []
for _ in range(1000):
    data = np.random.beta(3, 2, size=30)
    estimativas.append(data.mean())
```

```python
plt.hist(estimativas, edgecolor='k');
plt.xlabel(r'$\hat{\theta}$')
plt.ylabel('Freq em 1000 amostras')
plt.xlim(.5, .7)
despine()
```

O primeiro gráfico acima tem um viés baixo e variância alta. Note como o plot é mais disperso do que o segundo. Tal segundo, em contrapartida, tem variância baixa. Este é um efeito de aumentar o número de pontos. Vamos explorar um pouco mais tal efeito agora.

### (Detour) Um estimador Ruim

Em suma, os dois plots giram em torno do valor real, ou seja, não são viesados. Parece óbvio pois você já sabe que a média da amostra é um bom estimador da média da população. Isto foi definido na lei dos grandes números acima.

De qualquer forma, nada impede uma estimativa de outros estimadores tolos. Abaixo temos o estimador $\hat{\theta_b}$. O mesmo é a média geométrica de três pontos. Observe como o estimador é viesado!

$\hat{\theta_b} = \sqrt{X_1 * X_2 * X_3}$

```python
estimativas = []
for _ in range(1000):
    data = np.random.beta(3, 2, size=300)
    estimativas.append(np.sqrt(data.prod()))
```

Note que o mesmo tem alto viés e alta variância.

```python
plt.hist(estimativas, edgecolor='k', bins=200);
plt.xlabel(r'$\hat{\theta}$')
plt.ylabel('Freq em 1000 amostras')
plt.xlim(0, 1e-34)
despine()
```

## Caracterizando um Estimador

Lembre-se de nossas suposições até agora: assumimos que existe um valor real da população. Tal valor é a nossa média $\mu = 0.6$. Podemos chamar tal média de nosso objetivo $\theta^{*} = \mu$. Nosso modelo estima esse parâmetro. Para tal, usamos a variável $\hat{\theta}$ para denotar nossa estimativa. Gostaríamos de usar os dados coletados para determinar o valor que $\hat{\theta}$ deveria ter.

### Uma Função de Perda

Para decidir com precisão qual é o melhor valor de $\hat{\theta}$, definimos uma função de perda. Uma função de perda é uma função matemática que leva em uma estimativa $\hat{\theta}$ e os pontos em nosso conjunto de dados. Tal função tem uma única resposta real $L(\hat{\theta}) \in \mathbb{R}$. Quão menor esse valor, melhor é a nossa estimativa.

Algumas funções de perda são:

$$L(\hat{\theta}) = \frac{1}{n}\sum_{i=1}^{n} (\hat{\theta} - x_i)^2$$

e

$$L(\hat{\theta}) = \frac{1}{n}\sum_{i=1}^{n} |\hat{\theta} - x_i|$$

A primeira define um erro quadrado médio. A segunda define o erro absoluto médio. Vamos entender as duas:

```python
def mse(theta, data):
    return ((data - theta) ** 2).mean()
```

```python
def mae(theta, data):
    return np.abs(data - theta).mean()
```

```python
data = np.random.beta(3, 2, size=50)
```

Observe como o MSE, erro quadrado médio, tem valor mínimo na média 0.6.

```python
xax = np.arange(0.01, 1.2, 0.01)
yax = []
for theta in xax:
    yax.append(mse(theta, data))
plt.plot(xax, yax)
despine()
plt.ylabel('MSE')
plt.xlabel(r'$\hat{\theta}$')
```

**Valor ótimo do MSE** Podemos provar que o MSE retorna a média. Para isto, basta derivar a função e igualar a derivada a zero.

\begin{align}
L(\hat{\theta}) = \frac{1}{n}\sum_{i=1}^{n} (\hat{\theta} - x_i)^2 \\
\end{align}

Vamos derivar. Podemos fazer uso da regra da cadeia ou usar a expansão da forma quadrática. Vou seguir a segunda linha para ser mais explícito.

\begin{align}
L(\hat{\theta}) = \frac{1}{n}\sum_{i=1}^{n} \hat{\theta}^2 - 2\theta x_i + x_i^2 \\
{\delta L \over \delta\hat{\theta}} = \frac{1}{n}\sum_{i=1}^{n} \hat{2\theta} - 2x_i
\end{align}

Agora podemos igualar para zero.

\begin{align}
{\delta L \over \delta\theta} = \sum_{i=1}^{n} \hat{2\theta} - 2x_i \\
\sum_{i=1}^{n} \hat{2\theta} - 2x_i  = 0 \\
\sum_{i=1}^{n} \hat{2\theta} = \sum_{i=1}^{n} 2x_i \\
2 n \hat{\theta} = 2 \sum_{i=1}^{n} x_i \\
\hat{\theta} = n^{-1} \sum_{i=1}^{n} x_i = \bar{x}
\end{align}

*Assim, um estimador de MSE retorna a média, sempre.*

**MAE, mean absolute error, ou erro médio absoluto**

O mesmo não é verdade para o MAE. Primeiro, observe a forma da função abaixo. O valor ótimo, menor, não é mais a média.

```python
xax = np.arange(0.01, 1.2, 0.01)
yax = []
for theta in xax:
    yax.append(mae(theta, data))
plt.plot(xax, yax)
despine()
plt.ylabel('MAE')
plt.xlabel(r'$\hat{\theta}$')
```

Podemos mostrar que ao mimizar o MAE, retornamos a mediana:

\begin{align}
L(\hat{\theta}) = n^{-1}\sum_{i=1}^{n} |\hat{\theta} - x_i| \\
\text{Vamos derivar. Faça uso de:} \\
\frac{\delta |x|}{\delta x} = sign(x)
\end{align}

Sign indica o sinal de x. Por exemplo: Sign(-20) = -1. Sign(99) = 1. Sign(0) = 0.

Primeiro vamos quebrar a expressão em três. (1) Valores menores do que theta; (2) Valores maiores; e, (3) Valores iguais. Além do mais, vamos assumir que a derivada da função absoluta existe em zero. Além de assumir que existe, assumimos que a mesma é igual a zero.

\begin{aligned} L(\hat{\theta}) &=\sum_{i=1}^{n}\left|\hat{\theta}-x_{i}\right| \\ &=\sum_{\theta<x_i}\left|\hat{\theta}-x_{i}\right|+\sum_{\hat{\theta}=x_{i}}\left|\hat{\theta}-x_{i}\right|+\sum_{\hat{\theta}> x_i}|\hat{\theta}-x_i| \end{aligned}

Derivando ficamos com três partes. Lembre-se que a derivada da função absoluto é apenas o sinal (acima).

\begin{aligned} {\delta L \over \delta \hat{\theta}} = \sum_{\theta<x_i}-1+\sum_{\hat{\theta}=x_{i}}0+\sum_{\hat{\theta}> x_i}1
\end{aligned}

Igualando com zero chegamos em uma expressão esquisita.

$$\sum_{\theta<x_i}1 =\sum_{\hat{\theta}> x_i}1$$

Como interpretar a mesma? Observe que de um lado contamos os números negativos. Do outro, os positivos. O valor ótimo tem contagens iguais dos dois lados. Que número mágico separa métade dos valores de um lado e metade dos outros? A mediana!

*Ao minimizar o MAE ficamos com a mediana!*

## Risco

O risco de um estimador é o valor esperado da perda do modelo em pontos escolhidos aleatoriamente. No nosso cenário, a população consiste de uma distribuição Beta. Usamos a variável aleatória $X$ para representar uma amostra: $X \sim Beta(3, 2)$. O risco então visa responder, qual a média do erro para tal amostra? Vamos definir o risco usando MSE.

Usando nossa notação, o risco $R(\hat{\theta})$ do nosso estimador é:

$$R(\hat{\theta}) = \mathbb{E}[(X - \hat{\theta})^2]$$

Na expressão acima, usamos a perda MSE que fornece o erro interno da expectativa. Podemos definir outros riscos, mas esse é bem caracterizado (conseguimos entender o mesmo). O risco é uma função de $\hat{\theta}$, pois podemos mudar $\hat{\theta}$ como quisermos.

Diferentemente da perda, o uso do risco nos permite argumentar sobre a precisão do modelo na população em geral. Se nosso modelo atingir um risco baixo, ele fará previsões precisas sobre pontos da população a longo prazo. Por outro lado, se nosso modelo tiver um risco alto, ele geralmente terá um desempenho ruim nos dados da população.

Naturalmente, gostaríamos de escolher o valor de $\hat{\theta}$ que torna o risco o mais baixo possível. Usamos a variável $\theta^{*}$ para representar o valor ótimo.

Seção sendo escrita. Leia: https://www.textbook.ds100.org/ch/12/prob_risk.html. Use o translate caso necessite!


```python
# This mounts your Google Drive to the Colab VM.
from google.colab import drive
drive.mount('/content/drive')

# TODO: Enter the foldername in your Drive where you have saved the unzipped
# assignment folder, e.g. 'cs231n/assignments/assignment1/'
FOLDERNAME = None
assert FOLDERNAME is not None, "[!] Enter the foldername."

# Now that we've mounted your Drive, this ensures that
# the Python interpreter of the Colab VM can load
# python files from within it.
import sys
sys.path.append('/content/drive/My Drive/{}'.format(FOLDERNAME))

# This downloads the CIFAR-10 dataset to your Drive
# if it doesn't already exist.
%cd /content/drive/My\ Drive/$FOLDERNAME/cs231n/datasets/
!bash get_datasets.sh
%cd /content/drive/My\ Drive/$FOLDERNAME
```

# Batch Normalization
One way to make deep networks easier to train is to use more sophisticated optimization procedures such as SGD+momentum, RMSProp, or Adam. Another strategy is to change the architecture of the network to make it easier to train. One idea along these lines is batch normalization, proposed by [1] in 2015.

To understand the goal of batch normalization, it is important to first recognize that machine learning methods tend to perform better with input data consisting of uncorrelated features with zero mean and unit variance. When training a neural network, we can preprocess the data before feeding it to the network to explicitly decorrelate its features. This will ensure that the first layer of the network sees data that follows a nice distribution. However, even if we preprocess the input data, the activations at deeper layers of the network will likely no longer be decorrelated and will no longer have zero mean or unit variance, since they are output from earlier layers in the network. Even worse, during the training process the distribution of features at each layer of the network will shift as the weights of each layer are updated.

The authors of [1] hypothesize that the shifting distribution of features inside deep neural networks may make training deep networks more difficult. To overcome this problem, they propose to insert into the network layers that normalize batches. At training time, such a layer uses a minibatch of data to estimate the mean and standard deviation of each feature. These estimated means and standard deviations are then used to center and normalize the features of the minibatch. A running average of these means and standard deviations is kept during training, and at test time these running averages are used to center and normalize features.

It is possible that this normalization strategy could reduce the representational power of the network, since it may sometimes be optimal for certain layers to have features that are not zero-mean or unit variance. To this end, the batch normalization layer includes learnable shift and scale parameters for each feature dimension.

[1] [Sergey Ioffe and Christian Szegedy, "Batch Normalization: Accelerating Deep Network Training by Reducing
Internal Covariate Shift", ICML 2015.](https://arxiv.org/abs/1502.03167)

```python
# Setup cell.
import time
import numpy as np
import matplotlib.pyplot as plt
from cs231n.classifiers.fc_net import *
from cs231n.data_utils import get_CIFAR10_data
from cs231n.gradient_check import eval_numerical_gradient, eval_numerical_gradient_array
from cs231n.solver import Solver

%matplotlib inline
plt.rcParams["figure.figsize"] = (10.0, 8.0)  # Set default size of plots.
plt.rcParams["image.interpolation"] = "nearest"
plt.rcParams["image.cmap"] = "gray"

%load_ext autoreload
%autoreload 2

def rel_error(x, y):
    """Returns relative error."""
    return np.max(np.abs(x - y) / (np.maximum(1e-8, np.abs(x) + np.abs(y))))

def print_mean_std(x,axis=0):
    print(f"  means: {x.mean(axis=axis)}")
    print(f"  stds:  {x.std(axis=axis)}\n")
```

    =========== You can safely ignore the message below if you are NOT working on ConvolutionalNetworks.ipynb ===========
    	You will need to compile a Cython extension for a portion of this assignment.
    	The instructions to do this will be given in a section of the notebook below.

```python
# Load the (preprocessed) CIFAR-10 data.
data = get_CIFAR10_data()
for k, v in list(data.items()):
    print(f"{k}: {v.shape}")
```

    X_train: (49000, 3, 32, 32)
    y_train: (49000,)
    X_val: (1000, 3, 32, 32)
    y_val: (1000,)
    X_test: (1000, 3, 32, 32)
    y_test: (1000,)

# Batch Normalization: Forward Pass
In the file `cs231n/layers.py`, implement the batch normalization forward pass in the function `batchnorm_forward`. Once you have done so, run the following to test your implementation.

Referencing the paper linked to above in [1] may be helpful!

```python
# Check the training-time forward pass by checking means and variances
# of features both before and after batch normalization

# Simulate the forward pass for a two-layer network.
np.random.seed(231)
N, D1, D2, D3 = 200, 50, 60, 3
X = np.random.randn(N, D1)
W1 = np.random.randn(D1, D2)
W2 = np.random.randn(D2, D3)
a = np.maximum(0, X.dot(W1)).dot(W2)

print('Before batch normalization:')
print_mean_std(a,axis=0)

gamma = np.ones((D3,))
beta = np.zeros((D3,))

# Means should be close to zero and stds close to one.
print('After batch normalization (gamma=1, beta=0)')
a_norm, _ = batchnorm_forward(a, gamma, beta, {'mode': 'train'})
print_mean_std(a_norm,axis=0)

gamma = np.asarray([1.0, 2.0, 3.0])
beta = np.asarray([11.0, 12.0, 13.0])

# Now means should be close to beta and stds close to gamma.
print('After batch normalization (gamma=', gamma, ', beta=', beta, ')')
a_norm, _ = batchnorm_forward(a, gamma, beta, {'mode': 'train'})
print_mean_std(a_norm,axis=0)
```

    Before batch normalization:
      means: [ -2.3814598  -13.18038246   1.91780462]
      stds:  [27.18502186 34.21455511 37.68611762]

    After batch normalization (gamma=1, beta=0)
      means: [5.32907052e-17 5.49560397e-17 9.71445147e-18]
      stds:  [0.99999999 1.         1.        ]

    After batch normalization (gamma= [1. 2. 3.] , beta= [11. 12. 13.] )
      means: [11. 12. 13.]
      stds:  [0.99999999 1.99999999 2.99999999]

```python
# Check the test-time forward pass by running the training-time
# forward pass many times to warm up the running averages, and then
# checking the means and variances of activations after a test-time
# forward pass.

np.random.seed(231)
N, D1, D2, D3 = 200, 50, 60, 3
W1 = np.random.randn(D1, D2)
W2 = np.random.randn(D2, D3)

bn_param = {'mode': 'train'}
gamma = np.ones(D3)
beta = np.zeros(D3)

for t in range(50):
  X = np.random.randn(N, D1)
  a = np.maximum(0, X.dot(W1)).dot(W2)
  batchnorm_forward(a, gamma, beta, bn_param)

bn_param['mode'] = 'test'
X = np.random.randn(N, D1)
a = np.maximum(0, X.dot(W1)).dot(W2)
a_norm, _ = batchnorm_forward(a, gamma, beta, bn_param)

# Means should be close to zero and stds close to one, but will be
# noisier than training-time forward passes.
print('After batch normalization (test-time):')
print_mean_std(a_norm,axis=0)
```

    After batch normalization (test-time):
      means: [-0.03927354 -0.04349152 -0.10452688]
      stds:  [1.01531428 1.01238373 0.97819988]

# Batch Normalization: Backward Pass
Now implement the backward pass for batch normalization in the function `batchnorm_backward`.

To derive the backward pass you should write out the computation graph for batch normalization and backprop through each of the intermediate nodes. Some intermediates may have multiple outgoing branches; make sure to sum gradients across these branches in the backward pass.

Once you have finished, run the following to numerically check your backward pass.

```python
# Gradient check batchnorm backward pass.
np.random.seed(231)
N, D = 4, 5
x = 5 * np.random.randn(N, D) + 12
gamma = np.random.randn(D)
beta = np.random.randn(D)
dout = np.random.randn(N, D)

bn_param = {'mode': 'train'}
fx = lambda x: batchnorm_forward(x, gamma, beta, bn_param)[0]
fg = lambda a: batchnorm_forward(x, a, beta, bn_param)[0]
fb = lambda b: batchnorm_forward(x, gamma, b, bn_param)[0]

dx_num = eval_numerical_gradient_array(fx, x, dout)
da_num = eval_numerical_gradient_array(fg, gamma.copy(), dout)
db_num = eval_numerical_gradient_array(fb, beta.copy(), dout)

_, cache = batchnorm_forward(x, gamma, beta, bn_param)
dx, dgamma, dbeta = batchnorm_backward(dout, cache)

# You should expect to see relative errors between 1e-13 and 1e-8.
print('dx error: ', rel_error(dx_num, dx))
print('dgamma error: ', rel_error(da_num, dgamma))
print('dbeta error: ', rel_error(db_num, dbeta))
```

    dx error:  1.7029261167605239e-09
    dgamma error:  7.420414216247087e-13
    dbeta error:  2.8795057655839487e-12

# Batch Normalization: Alternative Backward Pass
In class we talked about two different implementations for the sigmoid backward pass. One strategy is to write out a computation graph composed of simple operations and backprop through all intermediate values. Another strategy is to work out the derivatives on paper. For example, you can derive a very simple formula for the sigmoid function's backward pass by simplifying gradients on paper.

Surprisingly, it turns out that you can do a similar simplification for the batch normalization backward pass too!

In the forward pass, given a set of inputs $X=\begin{bmatrix}x_1\\x_2\\...\\x_N\end{bmatrix}$,

we first calculate the mean $\mu$ and variance $v$.
With $\mu$ and $v$ calculated, we can calculate the standard deviation $\sigma$  and normalized data $Y$.
The equations and graph illustration below describe the computation ($y_i$ is the i-th element of the vector $Y$).

\begin{align}
& \mu=\frac{1}{N}\sum_{k=1}^N x_k  &  v=\frac{1}{N}\sum_{k=1}^N (x_k-\mu)^2 \\
& \sigma=\sqrt{v+\epsilon}         &  y_i=\frac{x_i-\mu}{\sigma}
\end{align}

The meat of our problem during backpropagation is to compute $\frac{\partial L}{\partial X}$, given the upstream gradient we receive, $\frac{\partial L}{\partial Y}.$ To do this, recall the chain rule in calculus gives us $\frac{\partial L}{\partial X} = \frac{\partial L}{\partial Y} \cdot \frac{\partial Y}{\partial X}$.

The unknown/hard part is $\frac{\partial Y}{\partial X}$. We can find this by first deriving step-by-step our local gradients at
$\frac{\partial v}{\partial X}$, $\frac{\partial \mu}{\partial X}$,
$\frac{\partial \sigma}{\partial v}$,
$\frac{\partial Y}{\partial \sigma}$, and $\frac{\partial Y}{\partial \mu}$,
and then use the chain rule to compose these gradients (which appear in the form of vectors!) appropriately to compute $\frac{\partial Y}{\partial X}$.

If it's challenging to directly reason about the gradients over $X$ and $Y$ which require matrix multiplication, try reasoning about the gradients in terms of individual elements $x_i$ and $y_i$ first: in that case, you will need to come up with the derivations for $\frac{\partial L}{\partial x_i}$, by relying on the Chain Rule to first calculate the intermediate $\frac{\partial \mu}{\partial x_i}, \frac{\partial v}{\partial x_i}, \frac{\partial \sigma}{\partial x_i},$ then assemble these pieces to calculate $\frac{\partial y_i}{\partial x_i}$.

You should make sure each of the intermediary gradient derivations are all as simplified as possible, for ease of implementation.

After doing so, implement the simplified batch normalization backward pass in the function `batchnorm_backward_alt` and compare the two implementations by running the following. Your two implementations should compute nearly identical results, but the alternative implementation should be a bit faster.

```python
np.random.seed(231)
N, D = 100, 500
x = 5 * np.random.randn(N, D) + 12
gamma = np.random.randn(D)
beta = np.random.randn(D)
dout = np.random.randn(N, D)

bn_param = {'mode': 'train'}
out, cache = batchnorm_forward(x, gamma, beta, bn_param)

t1 = time.time()
dx1, dgamma1, dbeta1 = batchnorm_backward(dout, cache)
t2 = time.time()
dx2, dgamma2, dbeta2 = batchnorm_backward_alt(dout, cache)
t3 = time.time()

print('dx difference: ', rel_error(dx1, dx2))
print('dgamma difference: ', rel_error(dgamma1, dgamma2))
print('dbeta difference: ', rel_error(dbeta1, dbeta2))
print('speedup: %.2fx' % ((t2 - t1) / (t3 - t2)))
```

    dx difference:  7.202578502291986e-13
    dgamma difference:  0.0
    dbeta difference:  0.0
    speedup: 2.00x

# Fully Connected Networks with Batch Normalization
Now that you have a working implementation for batch normalization, go back to your `FullyConnectedNet` in the file `cs231n/classifiers/fc_net.py`. Modify your implementation to add batch normalization.

Concretely, when the `normalization` flag is set to `"batchnorm"` in the constructor, you should insert a batch normalization layer before each ReLU nonlinearity. The outputs from the last layer of the network should not be normalized. Once you are done, run the following to gradient-check your implementation.

**Hint:** You might find it useful to define an additional helper layer similar to those in the file `cs231n/layer_utils.py`.

```python
np.random.seed(231)
N, D, H1, H2, C = 2, 15, 20, 30, 10
X = np.random.randn(N, D)
y = np.random.randint(C, size=(N,))

# You should expect losses between 1e-4~1e-10 for W,
# losses between 1e-08~1e-10 for b,
# and losses between 1e-08~1e-09 for beta and gammas.
for reg in [0, 3.14]:
  print('Running check with reg = ', reg)
  model = FullyConnectedNet([H1, H2], input_dim=D, num_classes=C,
                            reg=reg, weight_scale=5e-2, dtype=np.float64,
                            normalization='batchnorm')

  loss, grads = model.loss(X, y)
  print('Initial loss: ', loss)

  for name in sorted(grads):
    f = lambda _: model.loss(X, y)[0]
    grad_num = eval_numerical_gradient(f, model.params[name], verbose=False, h=1e-5)
    print('%s relative error: %.2e' % (name, rel_error(grad_num, grads[name])))
  if reg == 0: print()
```

    Running check with reg =  0
    Initial loss:  2.2611955101340957
    W1 relative error: 1.10e-04
    W2 relative error: 3.35e-06
    W3 relative error: 3.92e-10
    b1 relative error: 1.39e-09
    b2 relative error: 5.55e-09
    b3 relative error: 8.26e-11
    beta1 relative error: 7.85e-09
    beta2 relative error: 1.89e-09
    gamma1 relative error: 7.47e-09
    gamma2 relative error: 3.35e-09

    Running check with reg =  3.14
    Initial loss:  5.884829928987633
    W1 relative error: 1.98e-06
    W2 relative error: 2.29e-06
    W3 relative error: 6.29e-10
    b1 relative error: 2.78e-09
    b2 relative error: 2.22e-08
    b3 relative error: 2.10e-10
    beta1 relative error: 6.32e-09
    beta2 relative error: 3.48e-09
    gamma1 relative error: 5.94e-09
    gamma2 relative error: 4.14e-09

# Batch Normalization for Deep Networks
Run the following to train a six-layer network on a subset of 1000 training examples both with and without batch normalization.

```python
np.random.seed(231)

# Try training a very deep net with batchnorm.
hidden_dims = [100, 100, 100, 100, 100]

num_train = 1000
small_data = {
  'X_train': data['X_train'][:num_train],
  'y_train': data['y_train'][:num_train],
  'X_val': data['X_val'],
  'y_val': data['y_val'],
}

weight_scale = 2e-2
bn_model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization='batchnorm')
model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=None)

print('Solver with batch norm:')
bn_solver = Solver(bn_model, small_data,
                num_epochs=10, batch_size=50,
                update_rule='adam',
                optim_config={
                  'learning_rate': 1e-3,
                },
                verbose=True,print_every=20)
bn_solver.train()

print('\nSolver without batch norm:')
solver = Solver(model, small_data,
                num_epochs=10, batch_size=50,
                update_rule='adam',
                optim_config={
                  'learning_rate': 1e-3,
                },
                verbose=True, print_every=20)
solver.train()
```

    Solver with batch norm:
    (Iteration 1 / 200) loss: 2.340974
    (Epoch 0 / 10) train acc: 0.107000; val_acc: 0.115000
    (Epoch 1 / 10) train acc: 0.314000; val_acc: 0.267000
    (Iteration 21 / 200) loss: 2.039365
    (Epoch 2 / 10) train acc: 0.384000; val_acc: 0.280000
    (Iteration 41 / 200) loss: 2.041103
    (Epoch 3 / 10) train acc: 0.493000; val_acc: 0.308000
    (Iteration 61 / 200) loss: 1.753903
    (Epoch 4 / 10) train acc: 0.530000; val_acc: 0.307000
    (Iteration 81 / 200) loss: 1.246584
    (Epoch 5 / 10) train acc: 0.573000; val_acc: 0.313000
    (Iteration 101 / 200) loss: 1.320590
    (Epoch 6 / 10) train acc: 0.634000; val_acc: 0.338000
    (Iteration 121 / 200) loss: 1.157329
    (Epoch 7 / 10) train acc: 0.684000; val_acc: 0.326000
    (Iteration 141 / 200) loss: 1.138006
    (Epoch 8 / 10) train acc: 0.771000; val_acc: 0.323000
    (Iteration 161 / 200) loss: 0.664357
    (Epoch 9 / 10) train acc: 0.805000; val_acc: 0.344000
    (Iteration 181 / 200) loss: 0.819612
    (Epoch 10 / 10) train acc: 0.790000; val_acc: 0.322000

    Solver without batch norm:
    (Iteration 1 / 200) loss: 2.302332
    (Epoch 0 / 10) train acc: 0.129000; val_acc: 0.131000
    (Epoch 1 / 10) train acc: 0.283000; val_acc: 0.250000
    (Iteration 21 / 200) loss: 2.041970
    (Epoch 2 / 10) train acc: 0.316000; val_acc: 0.277000
    (Iteration 41 / 200) loss: 1.900473
    (Epoch 3 / 10) train acc: 0.373000; val_acc: 0.282000
    (Iteration 61 / 200) loss: 1.713156
    (Epoch 4 / 10) train acc: 0.390000; val_acc: 0.310000
    (Iteration 81 / 200) loss: 1.662209
    (Epoch 5 / 10) train acc: 0.434000; val_acc: 0.300000
    (Iteration 101 / 200) loss: 1.696060
    (Epoch 6 / 10) train acc: 0.535000; val_acc: 0.345000
    (Iteration 121 / 200) loss: 1.557987
    (Epoch 7 / 10) train acc: 0.530000; val_acc: 0.304000
    (Iteration 141 / 200) loss: 1.432189
    (Epoch 8 / 10) train acc: 0.628000; val_acc: 0.340000
    (Iteration 161 / 200) loss: 1.033917
    (Epoch 9 / 10) train acc: 0.661000; val_acc: 0.342000
    (Iteration 181 / 200) loss: 0.885684
    (Epoch 10 / 10) train acc: 0.701000; val_acc: 0.331000

Run the following to visualize the results from two networks trained above. You should find that using batch normalization helps the network to converge much faster.

```python
def plot_training_history(title, label, baseline, bn_solvers, plot_fn, bl_marker='.', bn_marker='.', labels=None):
    """utility function for plotting training history"""
    plt.title(title)
    plt.xlabel(label)
    bn_plots = [plot_fn(bn_solver) for bn_solver in bn_solvers]
    bl_plot = plot_fn(baseline)
    num_bn = len(bn_plots)
    for i in range(num_bn):
        label='with_norm'
        if labels is not None:
            label += str(labels[i])
        plt.plot(bn_plots[i], bn_marker, label=label)
    label='baseline'
    if labels is not None:
        label += str(labels[0])
    plt.plot(bl_plot, bl_marker, label=label)
    plt.legend(loc='lower center', ncol=num_bn+1)

plt.subplot(3, 1, 1)
plot_training_history('Training loss','Iteration', solver, [bn_solver], \
                      lambda x: x.loss_history, bl_marker='o', bn_marker='o')
plt.subplot(3, 1, 2)
plot_training_history('Training accuracy','Epoch', solver, [bn_solver], \
                      lambda x: x.train_acc_history, bl_marker='-o', bn_marker='-o')
plt.subplot(3, 1, 3)
plot_training_history('Validation accuracy','Epoch', solver, [bn_solver], \
                      lambda x: x.val_acc_history, bl_marker='-o', bn_marker='-o')

plt.gcf().set_size_inches(15, 15)
plt.show()
```

# Batch Normalization and Initialization
We will now run a small experiment to study the interaction of batch normalization and weight initialization.

The first cell will train eight-layer networks both with and without batch normalization using different scales for weight initialization. The second layer will plot training accuracy, validation set accuracy, and training loss as a function of the weight initialization scale.

```python
np.random.seed(231)

# Try training a very deep net with batchnorm.
hidden_dims = [50, 50, 50, 50, 50, 50, 50]
num_train = 1000
small_data = {
  'X_train': data['X_train'][:num_train],
  'y_train': data['y_train'][:num_train],
  'X_val': data['X_val'],
  'y_val': data['y_val'],
}

bn_solvers_ws = {}
solvers_ws = {}
weight_scales = np.logspace(-4, 0, num=20)
for i, weight_scale in enumerate(weight_scales):
    print('Running weight scale %d / %d' % (i + 1, len(weight_scales)))
    bn_model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization='batchnorm')
    model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=None)

    bn_solver = Solver(bn_model, small_data,
                  num_epochs=10, batch_size=50,
                  update_rule='adam',
                  optim_config={
                    'learning_rate': 1e-3,
                  },
                  verbose=False, print_every=200)
    bn_solver.train()
    bn_solvers_ws[weight_scale] = bn_solver

    solver = Solver(model, small_data,
                  num_epochs=10, batch_size=50,
                  update_rule='adam',
                  optim_config={
                    'learning_rate': 1e-3,
                  },
                  verbose=False, print_every=200)
    solver.train()
    solvers_ws[weight_scale] = solver
```

    Running weight scale 1 / 20
    Running weight scale 2 / 20
    Running weight scale 3 / 20
    Running weight scale 4 / 20
    Running weight scale 5 / 20
    Running weight scale 6 / 20
    Running weight scale 7 / 20
    Running weight scale 8 / 20
    Running weight scale 9 / 20
    Running weight scale 10 / 20
    Running weight scale 11 / 20
    Running weight scale 12 / 20
    Running weight scale 13 / 20
    Running weight scale 14 / 20
    Running weight scale 15 / 20
    Running weight scale 16 / 20

    E:\Mo\Univercity\internship\cs231n\assignments\assignment2\cs231n\layers.py:156: RuntimeWarning: overflow encountered in exp
      ex = np.exp(x)
    E:\Mo\Univercity\internship\cs231n\assignments\assignment2\cs231n\layers.py:157: RuntimeWarning: invalid value encountered in true_divide
      p = (ex.T / np.sum(ex, axis=1)).T
    E:\Mo\Univercity\internship\cs231n\assignments\assignment2\cs231n\layers.py:158: RuntimeWarning: divide by zero encountered in log
      loss = -np.sum(np.log(p[np.arange(num_train), y])) / num_train

    Running weight scale 17 / 20
    Running weight scale 18 / 20
    Running weight scale 19 / 20
    Running weight scale 20 / 20

```python
# Plot results of weight scale experiment.
best_train_accs, bn_best_train_accs = [], []
best_val_accs, bn_best_val_accs = [], []
final_train_loss, bn_final_train_loss = [], []

for ws in weight_scales:
  best_train_accs.append(max(solvers_ws[ws].train_acc_history))
  bn_best_train_accs.append(max(bn_solvers_ws[ws].train_acc_history))

  best_val_accs.append(max(solvers_ws[ws].val_acc_history))
  bn_best_val_accs.append(max(bn_solvers_ws[ws].val_acc_history))

  final_train_loss.append(np.mean(solvers_ws[ws].loss_history[-100:]))
  bn_final_train_loss.append(np.mean(bn_solvers_ws[ws].loss_history[-100:]))

plt.subplot(3, 1, 1)
plt.title('Best val accuracy vs. weight initialization scale')
plt.xlabel('Weight initialization scale')
plt.ylabel('Best val accuracy')
plt.semilogx(weight_scales, best_val_accs, '-o', label='baseline')
plt.semilogx(weight_scales, bn_best_val_accs, '-o', label='batchnorm')
plt.legend(ncol=2, loc='lower right')

plt.subplot(3, 1, 2)
plt.title('Best train accuracy vs. weight initialization scale')
plt.xlabel('Weight initialization scale')
plt.ylabel('Best training accuracy')
plt.semilogx(weight_scales, best_train_accs, '-o', label='baseline')
plt.semilogx(weight_scales, bn_best_train_accs, '-o', label='batchnorm')
plt.legend()

plt.subplot(3, 1, 3)
plt.title('Final training loss vs. weight initialization scale')
plt.xlabel('Weight initialization scale')
plt.ylabel('Final training loss')
plt.semilogx(weight_scales, final_train_loss, '-o', label='baseline')
plt.semilogx(weight_scales, bn_final_train_loss, '-o', label='batchnorm')
plt.legend()
plt.gca().set_ylim(1.0, 3.5)

plt.gcf().set_size_inches(15, 15)
plt.show()
```

## Inline Question 1:
Describe the results of this experiment. How does the weight initialization scale affect models with/without batch normalization differently, and why?

## Answer:
[FILL THIS IN]

# Batch Normalization and Batch Size
We will now run a small experiment to study the interaction of batch normalization and batch size.

The first cell will train 6-layer networks both with and without batch normalization using different batch sizes. The second layer will plot training accuracy and validation set accuracy over time.

```python
def run_batchsize_experiments(normalization_mode):
    np.random.seed(231)

    # Try training a very deep net with batchnorm.
    hidden_dims = [100, 100, 100, 100, 100]
    num_train = 1000
    small_data = {
      'X_train': data['X_train'][:num_train],
      'y_train': data['y_train'][:num_train],
      'X_val': data['X_val'],
      'y_val': data['y_val'],
    }
    n_epochs=10
    weight_scale = 2e-2
    batch_sizes = [5,10,50]
    lr = 10**(-3.5)
    solver_bsize = batch_sizes[0]

    print('No normalization: batch size = ',solver_bsize)
    model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=None)
    solver = Solver(model, small_data,
                    num_epochs=n_epochs, batch_size=solver_bsize,
                    update_rule='adam',
                    optim_config={
                      'learning_rate': lr,
                    },
                    verbose=False)
    solver.train()

    bn_solvers = []
    for i in range(len(batch_sizes)):
        b_size=batch_sizes[i]
        print('Normalization: batch size = ',b_size)
        bn_model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=normalization_mode)
        bn_solver = Solver(bn_model, small_data,
                        num_epochs=n_epochs, batch_size=b_size,
                        update_rule='adam',
                        optim_config={
                          'learning_rate': lr,
                        },
                        verbose=False)
        bn_solver.train()
        bn_solvers.append(bn_solver)

    return bn_solvers, solver, batch_sizes

batch_sizes = [5,10,50]
bn_solvers_bsize, solver_bsize, batch_sizes = run_batchsize_experiments('batchnorm')
```

    No normalization: batch size =  5
    Normalization: batch size =  5
    Normalization: batch size =  10
    Normalization: batch size =  50

```python
plt.subplot(2, 1, 1)
plot_training_history('Training accuracy (Batch Normalization)','Epoch', solver_bsize, bn_solvers_bsize, \
                      lambda x: x.train_acc_history, bl_marker='-^', bn_marker='-o', labels=batch_sizes)
plt.subplot(2, 1, 2)
plot_training_history('Validation accuracy (Batch Normalization)','Epoch', solver_bsize, bn_solvers_bsize, \
                      lambda x: x.val_acc_history, bl_marker='-^', bn_marker='-o', labels=batch_sizes)

plt.gcf().set_size_inches(15, 10)
plt.show()
```

## Inline Question 2:
Describe the results of this experiment. What does this imply about the relationship between batch normalization and batch size? Why is this relationship observed?

## Answer:
[FILL THIS IN]

# Layer Normalization
Batch normalization has proved to be effective in making networks easier to train, but the dependency on batch size makes it less useful in complex networks which have a cap on the input batch size due to hardware limitations.

Several alternatives to batch normalization have been proposed to mitigate this problem; one such technique is Layer Normalization [2]. Instead of normalizing over the batch, we normalize over the features. In other words, when using Layer Normalization, each feature vector corresponding to a single datapoint is normalized based on the sum of all terms within that feature vector.

[2] [Ba, Jimmy Lei, Jamie Ryan Kiros, and Geoffrey E. Hinton. "Layer Normalization." stat 1050 (2016): 21.](https://arxiv.org/pdf/1607.06450.pdf)

## Inline Question 3:
Which of these data preprocessing steps is analogous to batch normalization, and which is analogous to layer normalization?

1. Scaling each image in the dataset, so that the RGB channels for each row of pixels within an image sums up to 1.
2. Scaling each image in the dataset, so that the RGB channels for all pixels within an image sums up to 1.
3. Subtracting the mean image of the dataset from each image in the dataset.
4. Setting all RGB values to either 0 or 1 depending on a given threshold.

## Answer:
[FILL THIS IN]

# Layer Normalization: Implementation

Now you'll implement layer normalization. This step should be relatively straightforward, as conceptually the implementation is almost identical to that of batch normalization. One significant difference though is that for layer normalization, we do not keep track of the moving moments, and the testing phase is identical to the training phase, where the mean and variance are directly calculated per datapoint.

Here's what you need to do:

* In `cs231n/layers.py`, implement the forward pass for layer normalization in the function `layernorm_forward`.

Run the cell below to check your results.
* In `cs231n/layers.py`, implement the backward pass for layer normalization in the function `layernorm_backward`.

Run the second cell below to check your results.
* Modify `cs231n/classifiers/fc_net.py` to add layer normalization to the `FullyConnectedNet`. When the `normalization` flag is set to `"layernorm"` in the constructor, you should insert a layer normalization layer before each ReLU nonlinearity.

Run the third cell below to run the batch size experiment on layer normalization.

```python
# Check the training-time forward pass by checking means and variances
# of features both before and after layer normalization.

# Simulate the forward pass for a two-layer network.
np.random.seed(231)
N, D1, D2, D3 =4, 50, 60, 3
X = np.random.randn(N, D1)
W1 = np.random.randn(D1, D2)
W2 = np.random.randn(D2, D3)
a = np.maximum(0, X.dot(W1)).dot(W2)

print('Before layer normalization:')
print_mean_std(a,axis=1)

gamma = np.ones(D3)
beta = np.zeros(D3)

# Means should be close to zero and stds close to one.
print('After layer normalization (gamma=1, beta=0)')
a_norm, _ = layernorm_forward(a, gamma, beta, {'mode': 'train'})
print_mean_std(a_norm,axis=1)

gamma = np.asarray([3.0,3.0,3.0])
beta = np.asarray([5.0,5.0,5.0])

# Now means should be close to beta and stds close to gamma.
print('After layer normalization (gamma=', gamma, ', beta=', beta, ')')
a_norm, _ = layernorm_forward(a, gamma, beta, {'mode': 'train'})
print_mean_std(a_norm,axis=1)
```

    Before layer normalization:
      means: [-59.06673243 -47.60782686 -43.31137368 -26.40991744]
      stds:  [10.07429373 28.39478981 35.28360729  4.01831507]

    After layer normalization (gamma=1, beta=0)
      means: [ 4.81096644e-16 -7.40148683e-17  2.22044605e-16 -5.92118946e-16]
      stds:  [0.99999995 0.99999999 1.         0.99999969]

    After layer normalization (gamma= [3. 3. 3.] , beta= [5. 5. 5.] )
      means: [5. 5. 5. 5.]
      stds:  [2.99999985 2.99999998 2.99999999 2.99999907]

```python
# Gradient check batchnorm backward pass.
np.random.seed(231)
N, D = 4, 5
x = 5 * np.random.randn(N, D) + 12
gamma = np.random.randn(D)
beta = np.random.randn(D)
dout = np.random.randn(N, D)

ln_param = {}
fx = lambda x: layernorm_forward(x, gamma, beta, ln_param)[0]
fg = lambda a: layernorm_forward(x, a, beta, ln_param)[0]
fb = lambda b: layernorm_forward(x, gamma, b, ln_param)[0]

dx_num = eval_numerical_gradient_array(fx, x, dout)
da_num = eval_numerical_gradient_array(fg, gamma.copy(), dout)
db_num = eval_numerical_gradient_array(fb, beta.copy(), dout)

_, cache = layernorm_forward(x, gamma, beta, ln_param)
dx, dgamma, dbeta = layernorm_backward(dout, cache)

# You should expect to see relative errors between 1e-12 and 1e-8.
print('dx error: ', rel_error(dx_num, dx))
print('dgamma error: ', rel_error(da_num, dgamma))
print('dbeta error: ', rel_error(db_num, dbeta))
```

    dx error:  1.4336160411201157e-09
    dgamma error:  4.519489546032799e-12
    dbeta error:  2.276445013433725e-12

# Layer Normalization and Batch Size

We will now run the previous batch size experiment with layer normalization instead of batch normalization. Compared to the previous experiment, you should see a markedly smaller influence of batch size on the training history!

```python
ln_solvers_bsize, solver_bsize, batch_sizes = run_batchsize_experiments('layernorm')

plt.subplot(2, 1, 1)
plot_training_history('Training accuracy (Layer Normalization)','Epoch', solver_bsize, ln_solvers_bsize, \
                      lambda x: x.train_acc_history, bl_marker='-^', bn_marker='-o', labels=batch_sizes)
plt.subplot(2, 1, 2)
plot_training_history('Validation accuracy (Layer Normalization)','Epoch', solver_bsize, ln_solvers_bsize, \
                      lambda x: x.val_acc_history, bl_marker='-^', bn_marker='-o', labels=batch_sizes)

plt.gcf().set_size_inches(15, 10)
plt.show()
```

## Inline Question 4:
When is layer normalization likely to not work well, and why?

1. Using it in a very deep network
2. Having a very small dimension of features
3. Having a high regularization term

## Answer:
[FILL THIS IN]


```python
from IPython.display import Image
Image('../../../python_for_probability_statistics_and_machine_learning.jpg')
```

[Python for Probability, Statistics, and Machine Learning](https://www.springer.com/fr/book/9783319307152)

```python
from __future__ import division
%pylab inline
```

    Populating the interactive namespace from numpy and matplotlib

    WARNING: pylab import has clobbered these variables: ['f', 'clf', 'test']
    `%matplotlib` prevents importing * from pylab and numpy

```python
from pprint import pprint
import textwrap
import sys, re
old_displayhook = sys.displayhook
def displ(x):
   if x is None: return
   print "\n".join(textwrap.wrap(repr(x).replace(' ',''),width=80))

sys.displayhook=displ
```

So far, we have considered parametric methods that reduce inference
or prediction to parameter-fitting. However, for these to work, we had to
assume a specific functional form for the unknown probability distribution of
the data. Nonparametric methods eliminate the need to assume a specific
functional form by generalizing to classes of functions.

## Kernel Density Estimation

We have already made heavy use of this method with the histogram, which is a
special case of kernel density estimation.  The histogram can be considered the
crudest and most useful nonparametric method, that estimates the underlying
probability distribution of the data.

To be formal and place the histogram on the same footing as our earlier
estimations, suppose that $\mathscr{X}=[0,1]^d$ is the $d$ dimensional unit
cube and that $h$ is the *bandwidth* or size of a *bin* or sub-cube. Then,
there are $N\approx(1/h)^d$ such bins, each with volume $h^d$, $\lbrace
B_1,B_2,\ldots,B_N \rbrace$. With all this in place, we can write the histogram
has a probability density estimator of the form,

$$
\hat{p}_h(x) = \sum_{k=1}^N \frac{\hat{\theta}_k}{h} I(x\in B_k)
$$

 where

$$
\hat{\theta}_k=\frac{1}{n} \sum_{j=1}^n I(X_j\in B_k)
$$

 is the fraction of data points ($X_k$) in each bin, $B_k$. We want to
bound the bias and variance of $\hat{p}_h(x)$. Keep in mind that we are trying
to estimate a function of $x$, but the set of all possible probability
distribution functions is extremely large and hard to manage. Thus, we need
to restrict our attention to the following class of probability distribution of
so-called Lipschitz functions,

$$
\mathscr{P}(L) = \lbrace p\colon \vert p(x)-p(y)\vert \le L \Vert x-y\Vert, \forall \: x,y \rbrace
$$

 Roughly speaking, these are the density
functions whose slopes (i.e., growth rates)  are bounded by $L$.
It turns out that the bias of the histogram estimator is bounded in the
following way,

$$
\int\vert p(x)-\mathbb{E}(\hat{p}_h(x))\vert dx \le L h\sqrt{d}
$$

  Similarly, the variance is bounded by the following,

$$
\mathbb{V}(\hat{p}_h(x)) \le \frac{C}{n h^d}
$$

 for some constant $C$. Putting these two facts together means that the
risk is bounded by,

$$
R(p,\hat{p}) = \int \mathbb{E}(p(x) -\hat{p}_h(x))^2 dx \le L^2 h^2 d + \frac{C}{n h^d}
$$

 This upper bound is minimized by choosing

$$
h = \left(\frac{C}{L^2 n d}\right)^\frac{1}{d+2}
$$

 In particular, this means that,

$$
\sup_{p\in\mathscr{P}(L)} R(p,\hat{p}) \le C_0 \left(\frac{1}{n}\right)^{\frac{2}{d+2}}
$$

 where the constant $C_0$ is a function of $L$. There is a theorem
[[wasserman2004all]](#wasserman2004all) that shows this bound in tight, which basically means
that the histogram is a really powerful probability density estimator for
Lipschitz functions with risk that goes as
$\left(\frac{1}{n}\right)^{\frac{2}{d+2}}$.  Note that this class of functions
is not necessarily smooth because the Lipschitz condition admits step-wise and
other non-smooth functions. While this is a reassuring result, we typically do
not know which function class (Lipschitz or not) a particular probability
belongs to ahead of time.  Nonetheless, the rate at which the risk changes with
both dimension $d$ and $n$ samples would be hard to understand without this
result.  [Figure](#fig:nonparametric_001) shows the probability distribution
function of the $\beta(2,2)$ distribution compared to computed histograms for
different values of $n$. The box plots on each of the points show how the
variation in each bin of the histogram reduces with increasing $n$. The risk
function $R(p,\hat{p})$ above is based upon integrating the squared difference
between the histogram (as a piecewise function of $x$) and the probability
distribution function.

**Programming Tip.**

The corresponding IPython notebook has the complete source code that generates
[Figure](#fig:nonparametric_001); however, the following snippet
is the main element of the code.

```python
def generate_samples(n,ntrials=500):
    phat = np.zeros((nbins,ntrials))
    for k in range(ntrials):
        d = rv.rvs(n)
        phat[:,k],_=histogram(d,bins,density=True)
    return phat
```

The code uses the `histogram` function from Numpy.
To be consistent with the risk function $R(p,\hat{p})$, we have to make sure
the `bins` keyword argument is formatted correctly using a sequence of
bin-edges instead of just a single integer. Also, the `density=True` keyword
argument normalizes the histogram appropriately so that the comparison between
it and the probability distribution function of the simulated beta distribution
is correctly scaled.

The box plots on each of the points show how the variation in each bin of the histogram reduces with increasing $n$.

## Kernel Smoothing

We can extend our methods to other function classes using kernel functions.
A one-dimensional smoothing kernel is a smooth function $K$ with
the following properties,

$$
\begin{align*}
\int K(x) dx &= 1 \\\
\int x K(x) dx &= 0 \\\
0< \int x^2 K(x) dx &< \infty \\\
\end{align*}
$$

 For example, $K(x)=I(x)/2$ is the boxcar kernel, where $I(x)=1$
when $\vert x\vert\le 1$ and zero otherwise. The kernel density estimator is
very similar to the histogram, except now we put a kernel function on every
point as in the following,

$$
\hat{p}(x)=\frac{1}{n}\sum_{i=1}^n \frac{1}{h^d} K\left(\frac{\Vert x-X_i\Vert}{h}\right)
$$

 where $X\in \mathbb{R}^d$. [Figure](#fig:nonparametric_002) shows an
example of a kernel density estimate using a Gaussian kernel function,
$K(x)=e^{-x^2/2}/\sqrt{2\pi}$. There are  five data points shown by the
vertical lines in the upper panel. The dotted lines show the individual $K(x)$
function at each of the data points. The lower panel shows the overall kernel
density estimate, which is the scaled sum of the upper panel.

There is an important technical result in [[wasserman2004all]](#wasserman2004all) that
states that kernel density estimators are minimax in the sense we
discussed in the maximum likelihood the section ref{ch:stats:sec:mle}. In
broad strokes, this means that the analogous risk for the kernel
density estimator is approximately bounded by the following factor,

$$
R(p,\hat{p}) \lesssim n^{-\frac{2 m}{2 m+d}}
$$

 for some constant $C$ where $m$ is a factor related to bounding
the derivatives of the probability density function. For example, if the second
derivative of the density function is bounded, then $m=2$. This means that
the convergence rate for this estimator decreases with increasing dimension
$d$.

The upper panel shows the individual kernel functions placed at each of the data points. The lower panel shows the composite kernel density estimate which is the sum of the individual functions in the upper panel.

### Cross-Validation

As a practical matter, the tricky part of the kernel density estimator (which
includes the histogram as a special case) is that we need to somehow compute
the bandwith $h$ term using data. There are several rule-of-thumb methods that
for some common kernels, including Silverman's rule and Scott's rule for
Gaussian kernels. For example, Scott's factor is to simply compute $h=n^{
-1/(d+4) }$ and Silverman's is $h=(n (d+2)/4)^{ (-1/(d+4)) }$. Rules of
this kind are derived by assuming the underlying probability density
function is of a certain family (e.g., Gaussian), and then deriving the
best $h$ for a certain type of kernel density estimator, usually equipped
with extra functional properties (say, continuous derivatives of a
certain order). In practice, these rules seem to work pretty well,
especially for uni-modal probability density functions.  Avoiding these
kinds of assumptions means computing the bandwith from data directly and that is where
cross validation comes in.

Cross-validation is a method to estimate the bandwidth from the data itself.
The idea is to write out the following Integrated Squared Error (ISE),

$$
\begin{align*}
\texttt{ISE}(\hat{p}_h,p)&=\int (p(x)-\hat{p}_h(x))^2 dx\\\
                             &= \int \hat{p}_h(x)^2 dx - 2\int p(x) \hat{p}_h dx + \int p(x)^2 dx
\end{align*}
$$

 The problem with this expression is the middle term [^last_term],

[^last_term]: The last term is of no interest because we are
only interested in relative changes in the ISE.

$$
\int p(x)\hat{p}_h dx
$$

 where $p(x)$ is what we are trying to estimate with $\hat{p}_h$. The
form of the last expression looks like an expectation of $\hat{p}_h$ over the
density of $p(x)$, $\mathbb{E}(\hat{p}_h)$. The approach is to
approximate this with the mean,

$$
\mathbb{E}(\hat{p}_h) \approx \frac{1}{n}\sum_{i=1}^n \hat{p}_h(X_i)
$$

 The problem with this approach is that $\hat{p}_h$ is computed using
the same data that the approximation utilizes.  The way to get around this is
to split the data into two equally sized chunks $D_1$, $D_2$; and then compute
$\hat{p}_h$ for a sequence of different $h$ values over the $D_1$ set. Then,
when we apply the above approximation for the data ($Z_i$) in the $D_2$ set,

$$
\mathbb{E}(\hat{p}_h) \approx \frac{1}{\vert D_2\vert}\sum_{Z_i\in D_2} \hat{p}_h(Z_i)
$$

  Plugging this approximation back into the integrated squared error
provides the objective function,

$$
\texttt{ISE}\approx \int \hat{p}_h(x)^2 dx-\frac{2}{\vert D_2\vert}\sum_{Z_i\in D_2} \hat{p}_h(Z_i)
$$

 Some code will make these steps concrete. We will need some tools from
Scikit-learn.

```python
from sklearn.cross_validation import train_test_split
from sklearn.neighbors.kde import KernelDensity
```

 The `train_test_split` function makes it easy to split and
keep track of the $D_1$ and $D_2$ sets we need for cross validation. Scikit-learn
already has a powerful and flexible implementation of kernel density estimators.
To compute the objective function, we need some
basic numerical integration tools from Scipy. For this example, we
will generate samples from a $\beta(2,2)$ distribution, which is
implemented in the `stats` submodule in Scipy.

```python
import numpy as np
np.random.seed(123456)
```

```python
from scipy.integrate import quad
from scipy import stats
rv= stats.beta(2,2)
n=100                 # number of samples to generate
d = rv.rvs(n)[:,None] # generate samples as column-vector
```

**Programming Tip.**

The use of the `[:,None]` in the last line formats the Numpy array returned by
the `rvs` function into a Numpy vector with a column dimension of one. This is
required by the `KernelDensity` constructor because the column dimension is
used for different features (in general) for Scikit-learn. Thus, even though we
only have one feature, we still need to comply with the structured input that
Scikit-learn relies upon. There are many ways to inject the additional
dimension other than using `None`. For example, the more cryptic, `np.c_`, or
the less cryptic `[:,np.newaxis]` can do the same, as can the `np.reshape`
function.

 The next step is to split the data into two  halves and loop over
each of the $h_i$ bandwidths to create a separate kernel density estimator
based on the $D_1$ data,

```python
train,test,_,_=train_test_split(d,d,test_size=0.5)
kdes=[KernelDensity(bandwidth=i).fit(train)
        for i in [.05,0.1,0.2,0.3]]
```

**Programming Tip.**

Note that the single underscore symbol in Python refers to the last evaluated
result.  the above code unpacks the tuple returned by `train_test_split` into
four elements.  Because we are only interested in the first two, we assign the
last two to the underscore symbol. This is a stylistic usage to make it clear
to the reader that the last two elements of the tuple are unused.
Alternatively, we could assign the last two elements to a pair of dummy
variables that we do not use later, but then  the reader skimming the code may
think that those dummy variables are relevant.

 The last step is to loop over the so-created kernel density estimators
and compute the objective function.

```python
import numpy as np
for i in kdes:
    f = lambda x: np.exp(i.score_samples(x))
    f2 = lambda x: f(x)**2
    print 'h=%3.2f\t %3.4f'%(i.bandwidth,quad(f2,0,1)[0]
                             -2*np.mean(f(test)))
```

    h=0.05	 -1.1323
    h=0.10	 -1.1336
    h=0.20	 -1.1330
    h=0.30	 -1.0810

**Programming Tip.**

The lambda functions defined in the last block are necessary because
Scikit-learn implements the return value of the kernel density estimator as a
logarithm via the `score_samples` function. The numerical quadrature function
`quad` from Scipy computes the $\int \hat{p}_h(x)^2 dx$ part of the objective
function.

```python
%matplotlib inline

from __future__ import division
from matplotlib.pylab import subplots
fig,ax=subplots()
xi = np.linspace(0,1,100)[:,None]
for i in kdes:
    f=lambda x: np.exp(i.score_samples(x))
    f2 = lambda x: f(x)**2
    _=ax.plot(xi,f(xi),label='$h$='+str(i.bandwidth))

_=ax.set_xlabel('$x$',fontsize=28)
_=ax.set_ylabel('$y$',fontsize=28)
_=ax.plot(xi,rv.pdf(xi),'k:',lw=3,label='true')
_=ax.legend(loc=0)
ax2 = ax.twinx()
_=ax2.hist(d,20,alpha=.3,color='gray')
_=ax2.axis(ymax=50)
_=ax2.set_ylabel('count',fontsize=28)
fig.tight_layout()
#fig.savefig('fig-statistics/nonparametric_003.png')
```

Each line above is a different kernel density estimator for the given bandwidth as an approximation to the true density function. A plain histogram is imprinted on the bottom for reference.

Scikit-learn has many more advanced tools to automate this kind of
hyper-parameter (i.e., kernel density bandwidth) search. To utilize these
advanced tools, we need to format the current problem slightly differently by
defining the following wrapper class.

```python
class KernelDensityWrapper(KernelDensity):
    def predict(self,x):
        return np.exp(self.score_samples(x))
    def score(self,test):
        f = lambda x: self.predict(x)
        f2 = lambda x: f(x)**2
        return -(quad(f2,0,1)[0]-2*np.mean(f(test)))
```

 This is tantamount to reorganizing the above previous code
into functions that Scikit-learn requires. Next, we create the
dictionary of parameters we want to search over (`params`) below
and then start the grid search with the `fit` function,

```python
from sklearn.grid_search import GridSearchCV
params = {'bandwidth':np.linspace(0.01,0.5,10)}
clf = GridSearchCV(KernelDensityWrapper(), param_grid=params,cv=2)
clf.fit(d)
print clf.best_params_
```

    {'bandwidth': 0.17333333333333334}

  The grid search iterates over all the elements in the `params`
dictionary and reports the best bandwidth over that list of parameter values.
The `cv` keyword argument above specifies that we want to split the data
into two equally-sized sets for training and testing. We can
also examine the values of the objective function for each point
on the grid as follow,

```python
from pprint import pprint
pprint(clf.grid_scores_)
```

    [mean: 0.60758, std: 0.07695, params: {'bandwidth': 0.01},
     mean: 1.06325, std: 0.03866, params: {'bandwidth': 0.064444444444444443},
     mean: 1.11859, std: 0.02093, params: {'bandwidth': 0.11888888888888888},
     mean: 1.13187, std: 0.01397, params: {'bandwidth': 0.17333333333333334},
     mean: 1.12007, std: 0.01043, params: {'bandwidth': 0.22777777777777777},
     mean: 1.09186, std: 0.00794, params: {'bandwidth': 0.28222222222222221},
     mean: 1.05391, std: 0.00601, params: {'bandwidth': 0.33666666666666667},
     mean: 1.01126, std: 0.00453, params: {'bandwidth': 0.39111111111111108},
     mean: 0.96717, std: 0.00341, params: {'bandwidth': 0.44555555555555554},
     mean: 0.92355, std: 0.00257, params: {'bandwidth': 0.5}]

**Programming Tip.**

The `pprint` function makes the standard output prettier. The only reason for
using it here is to get it to look good on the printed page.  Otherwise, the
IPython notebook handles the visual rendering of output embedded in the
notebook via  its internal `display` framework.

 Keep in mind that the grid search examines multiple folds for cross
validation to compute the above means and standard deviations. Note that there
is also a `RandomizedSearchCV` in case you would rather specify a distribution
of parameters instead of a list. This is particularly useful for searching very
large parameter spaces where an exhaustive grid search would be too
computationally expensive. Although kernel density estimators are easy to
understand and have many attractive analytical properties, they become
practically prohibitive for large, high-dimensional data sets.

## Nonparametric Regression Estimators

Beyond estimating the underlying probability density, we can use nonparametric
methods to compute estimators of the underlying function that is generating the
data.  Nonparametric regression estimators of the following form are known as
linear smoothers,

$$
\hat{y}(x) =  \sum_{i=1}^n \ell_i(x) y_i
$$

 To understand the performance of these smoothers,
we can define the risk as the following,

$$
R(\hat{y},y) = \mathbb{E}\left( \frac{1}{n} \sum_{i=1}^n (\hat{y}(x_i)-y(x_i))^2 \right)
$$

 and find the best $\hat{y}$ that minimizes this. The problem with
this metric is that we do not know $y(x)$, which is why we are trying to
approximate it with $\hat{y}(x)$. We could construct an estimation by using the
data at hand as in the following,

$$
\hat{R}(\hat{y},y) =\frac{1}{n} \sum_{i=1}^n (\hat{y}(x_i)-Y_i)^2
$$

 where we have substituted the data $Y_i$ for the unknown function
value, $y(x_i)$. The problem with this approach is that we are using the data
to estimate the function and then using the same data to evaluate the risk of
doing so. This kind of double-dipping leads to overly optimistic estimators.
One way out of this conundrum is to use leave-one-out cross validation, wherein
the $\hat{y}$ function is estimated using all but one of the data pairs,
$(X_i,Y_i)$. Then, this missing data element is used to estimate the above
risk. Notationally, this is written as the following,

$$
\hat{R}(\hat{y},y) =\frac{1}{n} \sum_{i=1}^n (\hat{y}_{(-i)}(x_i)-Y_i)^2
$$

 where $\hat{y}_{(-i)}$ denotes computing the estimator without using
the $i^{th}$ data pair. Unfortunately, for anything other than relatively small
data sets, it quickly becomes computationally prohibitive to use leave-one-out
cross validation in practice. We'll get back to this issue shortly, but let's
consider a concrete example of such a nonparametric smoother.

## Nearest Neighbors Regression

The simplest possible nonparametric regression method is the $k$-nearest
neighbors regression. This is easier to explain in words than to write out in
math. Given an input $x$, find the closest one of the $k$ clusters that
contains it and then return the mean of the data values in that cluster. As a
univariate example, let's consider the following *chirp* waveform,

$$
y(x)=\cos\left(2\pi\left(f_o x + \frac{BW x^2}{2\tau}\right)\right)
$$

 This waveform is important in high-resolution radar applications.
The $f_o$ is the start frequency and $BW/\tau$ is the frequency slope of the
signal. For our example, the fact that it is nonuniform over its domain is
important. We can easily create some data by sampling the
chirp as in the following,

```python
import numpy as np
from numpy import cos, pi
xi = np.linspace(0,1,100)[:,None]
xin = np.linspace(0,1,12)[:,None]
f0 = 1 # init frequency
BW = 5
y = cos(2*pi*(f0*xin+(BW/2.0)*xin**2))
```

 We can use this data to construct a simple nearest neighbor
estimator using Scikit-learn,

```python
from sklearn.neighbors import KNeighborsRegressor
knr=KNeighborsRegressor(2)
knr.fit(xin,y)
```

    KNeighborsRegressor(algorithm='auto', leaf_size=30, metric='minkowski',
              metric_params=None, n_jobs=1, n_neighbors=2, p=2,
              weights='uniform')

**Programming Tip.**

Scikit-learn has a fantastically consistent interface. The `fit` function above
fits the model parameters to the data. The corresponding `predict` function
returns the output of the model given an arbitrary input. We will spend a lot
more time on Scikit-learn in the machine learning chapter. The `[:,None]` part
at the end is just injecting a column dimension into the array in order to
satisfy the dimensional requirements of Scikit-learn.

```python
from matplotlib.pylab import subplots
fig,ax=subplots()
yi = cos(2*pi*(f0*xi+(BW/2.0)*xi**2))
_=ax.plot(xi,yi,'k--',lw=2,label=r'$y(x)$')
_=ax.plot(xin,y,'ko',lw=2,ms=11,color='gray',alpha=.8,label='$y(x_i)$')
_=ax.fill_between(xi.flat,yi.flat,knr.predict(xi).flat,color='gray',alpha=.3)
_=ax.plot(xi,knr.predict(xi),'k-',lw=2,label='$\hat{y}(x)$')
_=ax.set_aspect(1/4.)
_=ax.axis(ymax=1.05,ymin=-1.05)
_=ax.set_xlabel(r'$x$',fontsize=24)
_=ax.legend(loc=0)
fig.set_tight_layout(True)
#fig.savefig('fig-statistics/nonparametric_004.png')
```

The dotted line shows the chirp signal and the solid line shows the nearest neighbor estimate. The gray circles are the sample points that we used to fit the nearest neighbor estimator. The shaded area shows the gaps between the estimator and the unsampled chirp.

 [Figure](#fig:nonparametric_004) shows the sampled signal (gray
circles) against the values generated by the nearest neighbor estimator (solid
line). The dotted line is the full unsampled chirp signal, which increases in
frequency with $x$. This is important for our example because it adds a
non-stationary aspect to this problem in that the function gets progressively
wigglier with increasing $x$.  The area between the estimated curve and the
signal is shaded in gray. Because the nearest neighbor estimator uses only two
nearest neighbors, for each new $x$, it finds the two adjacent $X_i$ that
bracket the $x$ in the training data and then averages the corresponding $Y_i$
values to compute the estimated value. That is, if you take every adjacent pair
of sequential gray circles in the Figure, you find that the horizontal solid line
splits the pair on the vertical axis. We can adjust the number of
nearest neighbors  by changing the constructor,

```python
knr=KNeighborsRegressor(3)
knr.fit(xin,y)
```

    KNeighborsRegressor(algorithm='auto', leaf_size=30, metric='minkowski',
              metric_params=None, n_jobs=1, n_neighbors=3, p=2,
              weights='uniform')

```python
fig,ax=subplots()
_=ax.plot(xi,yi,'k--',lw=2,label=r'$y(x)$')
_=ax.plot(xin,y,'ko',lw=2,ms=11,color='gray',alpha=.8,label='$y(x_i)$')
_=ax.fill_between(xi.flat,yi.flat,knr.predict(xi).flat,color='gray',alpha=.3)
_=ax.plot(xi,knr.predict(xi),'k-',lw=2,label='$\hat{y}(x)$')
_=ax.set_aspect(1/4.)
_=ax.axis(ymax=1.05,ymin=-1.05)
_=ax.set_xlabel(r'$x$',fontsize=24)
_=ax.legend(loc=0)
fig.set_tight_layout(True)
#fig.savefig('fig-statistics/nonparametric_005.png')
```

 which produces the following corresponding [Figure](#fig:nonparametric_005).

This is the same as [Figure](#fig:nonparametric_004) except that here there are three nearest neighbors used to build the estimator.

For this example, [Figure](#fig:nonparametric_005) shows that with
more nearest neighbors the fit performs poorly, especially towards the end of
the signal, where there is increasing variation, because the chirp is not
uniformly continuous.

Scikit-learn provides many tools for cross validation. The following code
sets up the tools for leave-one-out cross validation,

```python
from sklearn.cross_validation import LeaveOneOut
loo=LeaveOneOut(len(xin))
```

 The `LeaveOneOut` object is an iterable that produces a set of
disjoint indices of the data --- one for fitting the model (training set)
and one for evaluating the model (testing set), as shown
in the next short sample,

```python
pprint(list(LeaveOneOut(3)))
```

    [(array([1, 2]), array([0])),
     (array([0, 2]), array([1])),
     (array([0, 1]), array([2]))]

 The next block loops over the disjoint sets of training and test
indicies iterates provided by the `loo` variable to evaluate
the estimated risk, which is accumulated in the `out` list.

```python
out=[]
for train_index, test_index in loo:
    _=knr.fit(xin[train_index],y[train_index])
    out.append((knr.predict(xi[test_index])-y[test_index])**2)

print 'Leave-one-out Estimated Risk: ',np.mean(out),
```

    Leave-one-out Estimated Risk:  1.03517136627

 The last line in the code above reports leave-one-out's estimated
risk.

Linear smoothers of this type can be rewritten in using the following matrix,

$$
\mathscr{S} = \left[ \ell_i(x_j) \right]_{i,j}
$$

 so that

$$
\hat{\mathbf{y}} = \mathscr{S} \mathbf{y}
$$

   where $\mathbf{y}=\left[Y_1,Y_2,\ldots,Y_n\right]\in \mathbb{R}^n$
and $\hat{ \mathbf{y}
}=\left[\hat{y}(x_1),\hat{y}(x_2),\ldots,\hat{y}(x_n)\right]\in \mathbb{R}^n$.
This leads to a quick way to approximate leave-one-out cross validation as the
following,

$$
\hat{R}=\frac{1}{n}\sum_{i=1}^n\left(\frac{y_i-\hat{y}(x_i)}{1-\mathscr{S}_{i,i}}\right)^2
$$

 However, this does not reproduce the approach in the code above
because it assumes that each $\hat{y}_{(-i)}(x_i)$ is consuming one fewer
nearest neighbor than $\hat{y}(x)$.

We can get this $\mathscr{S}$ matrix from the `knr` object as in the following,

```python
_= knr.fit(xin,y) # fit on all data
S=(knr.kneighbors_graph(xin)).todense()/float(knr.n_neighbors)
```

 The `todense` part reformats the sparse matrix that is
returned into a regular Numpy `matrix`. The following shows a subsection
of this $\mathcal{S}$ matrix,

```python
print S[:5,:5]
```

    [[ 0.33333333  0.33333333  0.33333333  0.          0.        ]
     [ 0.33333333  0.33333333  0.33333333  0.          0.        ]
     [ 0.          0.33333333  0.33333333  0.33333333  0.        ]
     [ 0.          0.          0.33333333  0.33333333  0.33333333]
     [ 0.          0.          0.          0.33333333  0.33333333]]

 The sub-blocks show the windows of the the `y` data that are being
processed by the nearest neighbor estimator. For example,

```python
print np.hstack([knr.predict(xin[:5]),(S*y)[:5]])#columns match
```

    [[ 0.55781314  0.55781314]
     [ 0.55781314  0.55781314]
     [-0.09768138 -0.09768138]
     [-0.46686876 -0.46686876]
     [-0.10877633 -0.10877633]]

 Or, more concisely checking all entries for approximate equality,

```python
print np.allclose(knr.predict(xin),S*y)
```

    True

 which shows that the results from the nearest neighbor
object and the matrix multiply match.

**Programming Tip.**

Note that because we formatted the returned $\mathscr{S}$ as a Numpy matrix, we
automatically get the matrix multiplication instead of default element-wise
multiplication in the `S*y` term.

## Kernel Regression

For estimating the probability density, we started with the histogram and moved
to the more general kernel density estimate. Likewise, we can also extend
regression from nearest neighbors to kernel-based regression using the
*Nadaraya-Watson* kernel regression estimator.  Given a bandwith $h>0$, the
kernel regression estimator is defined as the following,

$$
\hat{y}(x)=\frac{\sum_{i=1}^n K\left(\frac{x-x_i}{h}\right) Y_i}{\sum_{i=1}^n K \left( \frac{x-x_i}{h} \right)}
$$

 Unfortunately, Scikit-learn does not implement this
regression estimator; however, Jan Hendrik Metzen  makes a compatible
version available on `github.com`.

```python
xin = np.linspace(0,1,20)[:,None]
y = cos(2*pi*(f0*xin+(BW/2.0)*xin**2)).flatten()
```

```python
from kernel_regression import KernelRegression
```

 This code makes it possible to internally optimize over the bandwidth
parameter using leave-one-out cross validation by specifying a grid of
potential bandwidth values (`gamma`), as in the following,

```python
kr = KernelRegression(gamma=np.linspace(6e3,7e3,500))
kr.fit(xin,y)
```

    KernelRegression(gamma=6002.0040080160325, kernel='rbf')

 [Figure](#fig:nonparametric_006) shows the kernel estimator (heavy
black line) using the Gaussian kernel compared to the nearest neighbor
estimator (solid light black line). As before, the data points are shown as
circles. [Figure](#fig:nonparametric_006) shows that the kernel estimator can
pick out the sharp peaks that are missed by the nearest neighbor estimator.

The heavy black line is the Gaussian kernel estimator. The light black line is the nearest neighbor estimator. The data points are shown as gray circles. Note that unlike the nearest neighbor estimator, the Gaussian kernel estimator is able to pick out the sharp peaks in the training data.

Thus, the difference between nearest neighbor and kernel estimation is that the
latter provides a smooth moving averaging of points whereas the former provides
a discontinuous averaging. Note that kernel estimates suffer near the
boundaries where there is mismatch between the edges and the kernel
function. This problem gets worse in higher dimensions because the data
naturally drift towards the boundaries (this is a consequence of the *curse of
dimensionality*). Indeed, it is not possible to simultaneously maintain local
accuracy (i.e., low bias) and a generous neighborhood (i.e., low variance). One
way to address this problem is to create a local polynomial regression using
the kernel function as a window to localize a region of interest. For example,

$$
\hat{y}(x)=\sum_{i=1}^n K\left(\frac{x-x_i}{h}\right) (Y_i-\alpha - \beta x_i)^2
$$

 and now we have to optimize over the two linear parameters $\alpha$
and $\beta$. This method is known as *local linear regression*
[[loader2006local]](#loader2006local), [[hastie2013elements]](#hastie2013elements). Naturally, this can be
extended to higher-order polynomials. Note that these methods are not yet
implemented in Scikit-learn.

```python
fig,ax=subplots()
#fig.set_size_inches((12,4))
_=ax.plot(xi,kr.predict(xi),'k-',label='kernel',lw=3)
_=ax.plot(xin,y,'o',lw=3,color='gray',ms=12)
_=ax.plot(xi,yi,'--',color='gray',label='chirp')
_=ax.plot(xi,knr.predict(xi),'k-',label='nearest')
_=ax.axis(ymax=1.1,ymin=-1.1)
_=ax.set_aspect(1/4.)
_=ax.axis(ymax=1.05,ymin=-1.05)
_=ax.set_xlabel(r'$x$',fontsize=24)
_=ax.set_ylabel(r'$y$',fontsize=24)
_=ax.legend(loc=0)
#fig.savefig('fig-statistics/nonparametric_006.png')
```

## Curse of Dimensionality

```python
sys.displayhook= old_displayhook
```

The so-called curse of dimensionality occurs as we move into higher and higher
dimensions. The term was coined by Bellman in 1961 while he was studying
adaptive control processes. Nowadays, the term is vaguely refers to anything
that becomes more complicated as the number of dimensions increases
substantially. Nevertheless, the concept is useful for recognizing
and characterizing the practical difficulties of high-dimensional analysis and
estimation.

Consider the volume of an $n$-dimensional sphere,

$$
\begin{equation}
V_s(n,r)= \begin{cases}
   \pi^{n/2} \frac{r^n}{(n/2)!}              & \texttt{ if $n$ is even}  \\\
   2^n\pi^{(n-1)/2} \frac{r^{ (n-1) }}{(n-1)!}((n-1)/2)! & \texttt{ if $n$ is odd}
\end{cases}
\label{_auto1} \tag{1}
\end{equation}
$$

 Further, consider the sphere $V_s(n,1/2)$ enclosed by an $n$
dimensional unit cube.  The volume of the cube is always equal to one, but
$\lim_{n\rightarrow\infty} V_s(n,1/2) = 0$. What does this mean? It means that
the volume of the cube is pushed away from its center, where the embedded
hypersphere lives.  Specifically, the distance from the center of the cube to
its vertices in $n$ dimensions is $\sqrt{n}/2$, whereas the distance from the
center of the inscribing sphere is $1/2$. This diagonal distance goes to
infinity as $n$ does. For a fixed $n$, the tiny spherical region at the center
of the cube has many long spines attached to it, like a hyper-dimensional sea
urchin or porcupine.

What are the consequences of this? For methods that rely on nearest
neighbors, exploiting locality to lower bias becomes intractable. For
example, suppose we have an $n$ dimensional space and a point near the
origin we want to localize around. To estimate behavior around this
point, we need to average the unknown function about this point, but
in a high-dimensional space, the chances of finding neighbors to
average are slim. Looked at from the opposing point of view, suppose
we have a binary variable, as in the coin-flipping problem. If we have
1000 trials, then, based on our earlier work, we can be confident
about estimating the probability of heads.  Now, suppose we have 10
binary variables.  Now we have $2^{ 10 }=1024$ vertices to estimate.
If we had the same 1000 points, then at least 24 vertices would not
get any data.  To keep the same resolution, we would need 1000 samples
at each vertex for a grand total of $1000\times 1024 \approx 10^6$
data points. So, for a ten fold increase in the number of variables,
we now have about 1000 more data points to collect to maintain the
same statistical resolution. This is the curse of dimensionality.

Perhaps some code will clarify this. The following code generates samples in
two dimensions that are plotted as points in [Figure](#fig:curse_of_dimensionality_001) with the inscribed circle in two
dimensions. Note that for $d=2$ dimensions,  most of the points are contained
in the circle.

```python
import numpy as np
v=np.random.rand(1000,2)-1/2.
```

```python
from matplotlib.patches import Circle
from matplotlib.pylab import subplots
fig,ax=subplots()
fig.set_size_inches((5,5))
_=ax.set_aspect(1)
_=ax.scatter(v[:,0],v[:,1],color='gray',alpha=.3)
_=ax.add_patch(Circle((0,0),0.5,alpha=.8,lw=3.,fill=False))
#fig.savefig('fig-statistics/curse_of_dimensionality_001.pdf')
```

Two dimensional scatter of points randomly and independently uniformly distributed in the unit square. Note that most of the points are contained in the circle. Counter to intuition, this does not persist as the number of dimensions increases.

 The next code block describes the core computation in
[Figure](#fig:curse_of_dimensionality_002). For each of the dimensions, we
create a set of uniformly distributed random variates along each dimension
and then compute how close each $d$ dimensional vector is to the origin.
Those that measure one half are those contained in the hypersphere. The
histogram of each measurment is shown in the corresponding panel in the
[Figure](#fig:curse_of_dimensionality_002). The dark vertical line shows the threshold value. Values to the left
of this indicate the population that are contained in the hypersphere. Thus,
[Figure](#fig:curse_of_dimensionality_002) shows that as $d$ increases,
fewer points are contained in the inscribed hypersphere. The following
code paraphrases  the content of [Figure](#fig:curse_of_dimensionality_002)

```python
for d in [2,3,5,10,20,50]:
    v=np.random.rand(5000,d)-1/2.
    hist([np.linalg.norm(i) for i in v])
```

```python
siz = [2,3,5,10,20,50]
fig,axs=subplots(3,2,sharex=True)
fig.set_size_inches((10,6))
#fig.set_size_inches((10,8))
for ax,k in zip(axs.flatten(),siz):
    v=np.random.rand(5000,k)-1/2.
    _=ax.hist([np.linalg.norm(i) for i in v],color='gray',normed=True);
    _=ax.vlines(0.5,0,ax.axis()[-1]*1.1,lw=3)
    _=ax.set_title('$d=%d$'%k,fontsize=20)
    _=ax.tick_params(labelsize='small',top=False,right=False)
    _=ax.spines['top'].set_visible(False)
    _=ax.spines['right'].set_visible(False)
    _=ax.spines['left'].set_visible(False)
    _=ax.yaxis.set_visible(False)
    _=ax.axis(ymax=3.5)

fig.set_tight_layout(True)
#fig.savefig('fig-statistics/curse_of_dimensionality_002.pdf')
```

Each panel shows the histogram of lengths of uniformly distributed $d$ dimensional random vectors. The population to the left of the dark vertical line are those that are contained in the inscribed hypersphere. This shows that fewer points are contained in the hypersphere with increasing dimension.

```python

```