# Statistics Fundamentals
Statistics is primarily about analyzing data samples, and that starts with understanding the distribution of data in a sample.
## Analyzing Data Distribution
A great deal of statistical analysis is based on the way that data values are distributed within the dataset. In this section, we'll explore some statistics that you can use to tell you about the values in a dataset.

### Measures of Central Tendency
The term *measures of central tendency* sounds a bit grand, but really it's just a fancy way of saying that we're interested in knowing where the middle value in our data is. For example, suppose decide to conduct a study into the comparative salaries of people who graduated from the same school. You might record the results like this:

| Name     | Salary      |
|----------|-------------|
| Dan      | 50,000      |
| Joann    | 54,000      |
| Pedro    | 50,000      |
| Rosie    | 189,000     |
| Ethan    | 55,000      |
| Vicky    | 40,000      |
| Frederic | 59,000      |

Now, some of the former-students may earn a lot, and others may earn less; but what's the salary in the middle of the range of all salaries?

#### Mean
A common way to define the central value is to use the *mean*, often called the *average*. This is calculated as the sum of the values in the dataset, divided by the number of observations in the dataset. When the dataset consists of the full population, the mean is represented by the Greek symbol ***μ*** (*mu*), and the formula is written like this:

\begin{equation}\mu = \frac{\displaystyle\sum_{i=1}^{N}X_{i}}{N}\end{equation}

More commonly, when working with a sample, the mean is represented by ***x̄*** (*x-bar*), and the formula is written like this (note the lower case letters used to indicate values from a sample):

\begin{equation}\bar{x} = \frac{\displaystyle\sum_{i=1}^{n}x_{i}}{n}\end{equation}

In the case of our list of salaries, this can be calculated as:

\begin{equation}\bar{x} = \frac{50000+54000+50000+189000+55000+40000+59000}{7}\end{equation}

Which is **71,000**.

>In technical terminology, ***x̄*** is a *statistic* (an estimate based on a sample of data) and ***μ*** is a *parameter* (a true value based on the entire population). A lot of the time, the parameters for the full population will be impossible (or at the very least, impractical) to measure; so we use statistics obtained from a representative sample to approximate them. In this case, we can use the sample mean of salary for our selection of surveyed students to try to estimate the actual average salary of all students who graduate from our school.

In Python, when working with data in a *pandas.dataframe*, you can use the ***mean*** function, like this:

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000]})

print (df['Salary'].mean())
```

    71000.0

So, is **71,000** really the central value? Or put another way, would it be reasonable for a graduate of this school to expect to earn $71,000? After all, that's the average salary of a graduate from this school.

If you look closely at the salaries, you can see that out of the seven former students, six earn less than the mean salary. The data is *skewed* by the fact that Rosie has clearly managed to find a much higher-paid job than her classmates.

#### Median
OK, let's see if we can find another definition for the central value that more closely reflects the expected earning potential of students attending our school. Another measure of central tendancy we can use is the *median*. To calculate the median, we need to sort the values into ascending order and then find the middle-most value. When there are an odd number of observations, you can find the position of the median value using this formula (where *n* is the number of observations):

\begin{equation}\frac{n+1}{2}\end{equation}

Remember that this formula returns the *position* of the median value in the sorted list; not the value itself.

If the number of observations is even, then things are a little (but not much) more complicated. In this case you calculate the median as the average of the two middle-most values, which are found like this:

\begin{equation}\frac{n}{2} \;\;\;\;and \;\;\;\; \frac{n}{2} + 1\end{equation}

So, for our graduate salaries; first lets sort the dataset:

| Salary      |
|-------------|
| 40,000      |
| 50,000      |
| 50,000      |
| 54,000      |
| 55,000      |
| 59,000      |
| 189,000     |

There's an odd number of observation (7), so the median value is at position (7 + 1) ÷ 2; in other words, position 4:

| Salary      |
|-------------|
| 40,000      |
| 50,000      |
| 50,000      |
|***>54,000*** |
| 55,000      |
| 59,000      |
| 189,000     |

So the median salary is **54,000**.

The *pandas.dataframe* class in Python has a ***median*** function to find the median:

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000]})

print (df['Salary'].median())
```

    54000.0

#### Mode
Another related statistic is the *mode*, which indicates the most frequently occurring value. If you think about it, this is potentially a good indicator of how much a student might expect to earn when they graduate from the school; out of all the salaries that are being earned by former students, the mode is earned by more than any other.

Looking at our list of salaries, there are two instances of former students earning **50,000**, but only one instance each for all other salaries:

| Salary      |
|-------------|
| 40,000      |
|***>50,000***|
|***>50,000***|
| 54,000      |
| 55,000      |
| 59,000      |
| 189,000     |

The mode is therefore **50,000**.

As you might expect, the *pandas.dataframe* class has a ***mode*** function to return the mode:

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000]})

print (df['Salary'].mode())
```

    0    50000
    dtype: int64

##### Multimodal Data
It's not uncommon for a set of data to have more than one value as the mode. For example, suppose Ethan receives a raise that takes his salary to **59,000**:

| Salary      |
|-------------|
| 40,000      |
|***>50,000***|
|***>50,000***|
| 54,000      |
|***>59,000***|
|***>59,000***|
| 189,000     |

Now there are two values with the highest frequency. This dataset is *bimodal*. More generally, when there is more than one mode value, the data is considered *multimodal*.

The *pandas.dataframe.**mode*** function returns all of the modes:

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,59000,40000,59000]})

print (df['Salary'].mode())
```

    0    50000
    1    59000
    dtype: int64

### Distribution and Density
Now we know something about finding the center, we can start to explore how the data is distributed around it. What we're interested in here is understanding the general "shape" of the data distribution so that we can begin to get a feel for what a 'typical' value might be expected to be.

We can start by finding the extremes - the minimum and maximum. In the case of our salary data, the lowest paid graduate from our school is Vicky, with a salary of **40,000**; and the highest-paid graduate is Rosie, with **189,000**.

The *pandas.dataframe* class has ***min*** and ***max*** functions to return these values.

Run the following code to compare the minimum and maximum salaries to the central measures we calculated previously:

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000]})

print ('Min: ' + str(df['Salary'].min()))
print ('Mode: ' + str(df['Salary'].mode()[0]))
print ('Median: ' + str(df['Salary'].median()))
print ('Mean: ' + str(df['Salary'].mean()))
print ('Max: ' + str(df['Salary'].max()))
```

    Min: 40000
    Mode: 50000
    Median: 54000.0
    Mean: 71000.0
    Max: 189000

We can examine these values, and get a sense for how the data is distributed - for example, we can see that the *mean* is closer to the max than the *median*, and that both are closer to the *min* than to the *max*.

However, it's generally easier to get a sense of the distribution by visualizing the data. Let's start by creating a histogram of the salaries, highlighting the *mean* and *median* salaries (the *min*, *max* are fairly self-evident, and the *mode* is wherever the highest bar is):

```python
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000]})

salary = df['Salary']
salary.plot.hist(title='Salary Distribution', color='lightblue', bins=25)
plt.axvline(salary.mean(), color='magenta', linestyle='dashed', linewidth=2)
plt.axvline(salary.median(), color='green', linestyle='dashed', linewidth=2)
plt.show()
```

The ***mean*** and ***median*** are shown as dashed lines. Note the following:
- *Salary* is a continuous data value - graduates could potentially earn any value along the scale, even down to a fraction of cent.
- The number of bins in the histogram determines the size of each salary band for which we're counting frequencies. Fewer bins means merging more individual salaries together to be counted as a group.
- The majority of the data is on the left side of the histogram, reflecting the fact that most graduates earn between 40,000 and 55,000
- The mean is a higher value than the median and mode.
- There are gaps in the histogram for salary bands that nobody earns.

The histogram shows the relative frequency of each salary band, based on the number of bins. It also gives us a sense of the *density* of the data for each point on the salary scale. With enough data points, and small enough bins, we could view this density as a line that shows the shape of the data distribution.

Run the following cell to show the density of the salary data as a line on top of the histogram:

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

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000]})

salary = df['Salary']
density = stats.gaussian_kde(salary)
n, x, _ = plt.hist(salary, histtype='step', density=True, bins=25)
plt.plot(x, density(x)*5)
plt.axvline(salary.mean(), color='magenta', linestyle='dashed', linewidth=2)
plt.axvline(salary.median(), color='green', linestyle='dashed', linewidth=2)
plt.show()

```

Note that the density line takes the form of an asymmetric curve that has a "peak" on the left and a long tail on the right. We describe this sort of data distribution as being *skewed*; that is, the data is not distributed symmetrically but "bunched together" on one side. In this case, the data is bunched together on the left, creating a long tail on the right; and is described as being *right-skewed* because some infrequently occurring high values are pulling the *mean* to the right.

Let's take a look at another set of data. We know how much money our graduates make, but how many hours per week do they need to work to earn their salaries? Here's the data:

| Name     | Hours |
|----------|-------|
| Dan      | 41    |
| Joann    | 40    |
| Pedro    | 36    |
| Rosie    | 30    |
| Ethan    | 35    |
| Vicky    | 39    |
| Frederic | 40    |

Run the following code to show the distribution of the hours worked:

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

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Hours':[41,40,36,30,35,39,40]})

hours = df['Hours']
density = stats.gaussian_kde(hours)
n, x, _ = plt.hist(hours, histtype='step', density=True, bins=25)
plt.plot(x, density(x)*7)
plt.axvline(hours.mean(), color='magenta', linestyle='dashed', linewidth=2)
plt.axvline(hours.median(), color='green', linestyle='dashed', linewidth=2)
plt.show()
```

Once again, the distribution is skewed, but this time it's **left-skewed**. Note that the curve is asymmetric with the ***mean*** to the left of the ***median*** and the *mode*; and the average weekly working hours skewed to the lower end.

Once again, Rosie seems to be getting the better of the deal. She earns more than her former classmates for working fewer hours. Maybe a look at the test scores the students achieved on their final grade at school might help explain her success:

| Name     | Grade |
|----------|-------|
| Dan      | 50    |
| Joann    | 50    |
| Pedro    | 46    |
| Rosie    | 95    |
| Ethan    | 50    |
| Vicky    | 5     |
| Frederic | 57    |

Let's take a look at the distribution of these grades:

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

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Grade':[50,50,46,95,50,5,57]})

grade = df['Grade']
density = stats.gaussian_kde(grade)
n, x, _ = plt.hist(grade, histtype='step', density=True, bins=25)
plt.plot(x, density(x)*7)
plt.axvline(grade.mean(), color='yellow', linestyle='dashed', linewidth=3)
plt.axvline(grade.median(), color='blue', linestyle='dashed', linewidth=2)
plt.axvline(grade.mode()[0], color='red', linestyle='dashed', linewidth=1)
plt.show()
```

This time, the distribution is symmetric, forming a "bell-shaped" curve. The ***mean***, ***median***, and mode are at the same location, and the data tails off evenly on both sides from a central peak.

Statisticians call this a *normal* distribution (or sometimes a *Gaussian* distribution), and it occurs quite commonly in many scenarios due to something called the *Central Limit Theorem*, which reflects the way continuous probability works - more about that later.

#### Skewness and Kurtosis
You can measure *skewness* (in which direction the data is skewed and to what degree) and kurtosis (how "peaked" the data is) to get an idea of the shape of the data distribution. In Python, you can use the ***skew*** and ***kurt*** functions to find this:

```python
%matplotlib inline
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import scipy.stats as stats

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,30,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

numcols = ['Salary', 'Hours', 'Grade']
for col in numcols:
    print(df[col].name + ' skewness: ' + str(df[col].skew()))
    print(df[col].name + ' kurtosis: ' + str(df[col].kurt()))
    density = stats.gaussian_kde(df[col])
    n, x, _ = plt.hist(df[col], histtype='step', normed=True, bins=25)
    plt.plot(x, density(x)*6)
    plt.show()
    print('\n')
```

Now let's look at the distribution of a real dataset - let's see how the heights of the father's measured in Galton's study of parent and child heights are distributed:

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

import statsmodels.api as sm

df = sm.datasets.get_rdataset('GaltonFamilies', package='HistData').data

fathers = df['father']
density1 = stats.gaussian_kde(fathers)
n, x, _ = plt.hist(fathers, histtype='step', density=True, bins=50)
plt.plot(x, density(x)*2.5)
plt.axvline(fathers.mean(), color='magenta', linestyle='dashed', linewidth=2)
plt.axvline(fathers.median(), color='green', linestyle='dashed', linewidth=2)
plt.show()

```

As you can see, the father's height measurements are approximately normally distributed - in other words, they form a more or less *normal* distribution that is symmetric around the mean.

### Measures of Variance
We can see from the distribution plots of our data that the values in our dataset can vary quite widely. We can use various measures to quantify this variance.

#### Range
A simple way to quantify the variance in a dataset is to identify the difference between the lowest and highest values. This is called the *range*, and is calculated by subtracting the minimim value from the maximum value.

The following Python code creates a single Pandas dataframe for our school graduate data, and calculates the *range* for each of the numeric features:

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,30,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

numcols = ['Salary', 'Hours', 'Grade']
for col in numcols:
    print(df[col].name + ' range: ' + str(df[col].max() - df[col].min()))
```

    Salary range: 149000
    Hours range: 11
    Grade range: 90

#### Percentiles and Quartiles
The range is easy to calculate, but it's not a particularly useful statistic. For example, a range of 149,000 between the lowest and highest salary does not tell us which value within that range a graduate is most likely to earn -  it doesn't tell us nothing about how the salaries are distributed around the mean within that range.  The range tells us very little about the comparative position of an individual value within the distribution - for example, Frederic scored 57 in his final grade at school; which is a pretty good score (it's more than all but one of his classmates); but this isn't immediately apparent from a score of 57 and range of 90.

##### Percentiles
A percentile tells us where a given value is ranked in the overall distribution. For example, 25% of the data in a distribution has a value lower than the 25th percentile; 75% of the data has a value lower than the 75th percentile, and so on. Note that half of the data has a value lower than the 50th percentile - so the 50th percentile is also the median!

Let's examine Frederic's grade using this approach. We know he scored 57, but how does he rank compared to his fellow students?

Well, there are seven students in total, and five of them scored less than Frederic; so we can calculate the percentile for Frederic's grade like this:

\begin{equation}\frac{5}{7} \times 100 \approx 71.4\end{equation}

So Frederic's score puts him at the 71.4th percentile in his class.

In Python, you can use the ***percentileofscore*** function in the *scipy.stats* package to calculate the percentile for a given value in a set of values:

```python
import pandas as pd
from scipy import stats

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,30,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

print(stats.percentileofscore(df['Grade'], 57, 'strict'))
```

    71.42857142857143

We've used the strict definition of percentile; but sometimes it's calculated as being the percentage of values that are less than *or equal to* the value you're comparing. In this case, the calculation for Frederic's percentile would include his own score:

\begin{equation}\frac{6}{7} \times 100 \approx 85.7\end{equation}

You can calculate this way in Python by using the ***weak*** mode of the ***percentileofscore*** function:

```python
import pandas as pd
from scipy import stats

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,30,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

print(stats.percentileofscore(df['Grade'], 57, 'weak'))
```

    85.71428571428571

We've considered the percentile of Frederic's grade, and used it to rank him compared to his fellow students. So what about Dan, Joann, and Ethan? How do they compare to the rest of the class? They scored the same grade (50), so in a sense they share a percentile.

To deal with this *grouped* scenario, we can average the percentage rankings for the matching scores. We treat half of the scores matching the one we're ranking as if they are below it, and half as if they are above it. In this case, there were three matching scores of 50, and for each of these we calculate the percentile as if 1 was below and 1 was above. So the calculation for a percentile for Joann based on scores being less than or equal to 50 is:

\begin{equation}(\frac{4}{7}) \times 100 \approx 57.14\end{equation}

The value of **4** consists of the two scores that are below Joann's score of 50, Joann's own score, and half of the scores that are the same as Joann's (of which there are two, so we count one).

In Python, the ***percentileofscore*** function has a ***rank*** function that calculates grouped percentiles like this:

```python
import pandas as pd
from scipy import stats

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,30,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

print(stats.percentileofscore(df['Grade'], 50, 'rank'))
```

    57.142857142857146

##### Quartiles
Rather than using individual percentiles to compare data, we can consider the overall spread of the data by dividing those percentiles into four *quartiles*. The first quartile contains the values from the minimum to the 25th percentile, the second from the 25th percentile to the 50th percentile (which is the median), the third from the 50th percentile to the 75th percentile, and the fourth from the 75th percentile to the maximum.

In Python, you can use the ***quantile*** function of the *pandas.dataframe* class to find the threshold values at the 25th, 50th, and 75th percentiles (*quantile* is a generic term for a ranked position, such as a percentile or quartile).

Run the following code to find the quartile thresholds for the weekly hours worked by our former students:

```python
# Quartiles
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,17,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})
print(df['Hours'].quantile([0.25, 0.5, 0.75]))
```

    0.25    35.5
    0.50    39.0
    0.75    40.0
    Name: Hours, dtype: float64

Its usually easier to understand how data is distributed across the quartiles by visualizing it. You can use a histogram, but many data scientists use a kind of visualization called a *box plot* (or a *box and whiskers* plot).

Let's create a box plot for the weekly hours:

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

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,30,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

# Plot a box-whisker chart
df['Hours'].plot(kind='box', title='Weekly Hours Distribution', figsize=(6,4))
plt.show()
```

The box plot consists of:
- A rectangular *box* that shows where the data between the 25th and 75th percentile (the second and third quartile) lie. This part of the distribution is often referred to as the *interquartile range* - it contains the middle 50 data values.
- *Whiskers* that extend from the box to the bottom of the first quartile and the top of the fourth quartile to show the full range of the data.
- A line in the box that shows that location of the median (the 50th percentile, which is also the threshold between the second and third quartile)

In this case, you can see that the interquartile range is between 35 and 40, with the median nearer the top of that range. The range of the first quartile is from around 30 to 35, and the fourth quartile is from 40 to 41.

#### Outliers
Let's take a look at another box plot - this time showing the distribution of the salaries earned by our former classmates:

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

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,30,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

# Plot a box-whisker chart
df['Salary'].plot(kind='box', title='Salary Distribution', figsize=(10,8))
plt.show()
```

So what's going on here?

Well, as we've already noticed, Rosie earns significantly more than her former classmates. So much more in fact, that her salary has been identifed as an *outlier*. An outlier is a value that is so far from the center of the distribution compared to other values that it skews the distribution by affecting the mean. There are all sorts of reasons that you might have outliers in your data, including data entry errors, failures in sensors or data-generating equipment, or genuinely anomalous values.

So what should we do about it?

This really depends on the data, and what you're trying to use it for. In this case, let's assume we're trying to figure out what's a reasonable expectation of salary for a graduate of our school to earn. Ignoring for the moment that we have an extremly small dataset on which to base our judgement, it looks as if Rosie's salary could be either an error (maybe she mis-typed it in the form used to collect data) or a genuine anomaly (maybe she became a professional athelete or some other extremely highly paid job). Either way, it doesn't seem to represent a salary that a typical graduate might earn.

Let's see what the distribution of the data looks like without the outlier:

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

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,17,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

# Plot a box-whisker chart
df['Salary'].plot(kind='box', title='Salary Distribution', figsize=(10,8), showfliers=False)
plt.show()
```

Now it looks like there's a more even distribution of salaries. It's still not quite symmetrical, but there's much less overall variance. There's potentially some cause here to disregard Rosie's salary data when we compare the salaries, as it is tending to skew the analysis.

So is that OK? Can we really just ignore a data value we don't like?

Again, it depends on what you're analyzing. Let's take a look at the distribution of final grades:

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

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,17,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

# Plot a box-whisker chart
df['Grade'].plot(kind='box', title='Grade Distribution', figsize=(10,8))
plt.show()
```

Once again there are outliers, this time at both ends of the distribution. However, think about what this data represents. If we assume that the grade for the final test is based on a score out of 100, it seems reasonable to expect that some students will score very low (maybe even 0) and some will score very well (maybe even 100); but most will get a score somewhere in the middle.  The reason that the low and high scores here look like outliers might just be because we have so few data points. Let's see what happens if we include a few more students in our data:

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

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic', 'Jimmie', 'Rhonda', 'Giovanni', 'Francesca', 'Rajab', 'Naiyana', 'Kian', 'Jenny'],
                   'Grade':[50,50,46,95,50,5,57,42,26,72,78,60,40,17,85]})

# Plot a box-whisker chart
df['Grade'].plot(kind='box', title='Grade Distribution', figsize=(10,8))
plt.show()
```

With more data, there are some more high and low scores; so we no longer consider the isolated cases to be outliers.

The key point to take away here is that you need to really understand the data and what you're trying to do with it, and you need to ensure that you have a reasonable sample size, before determining what to do with outlier values.

#### Variance and Standard Deviation
We've seen how to understand the *spread* of our data distribution using the range, percentiles, and quartiles; and we've seen the effect of outliers on the distribution. Now it's time to look at how to measure the amount of variance in the data.

##### Variance
Variance is measured as the average of the squared difference from the mean. For a full population, it's indicated by a squared Greek letter *sigma* (***σ2***) and calculated like this:

\begin{equation}\sigma^{2} = \frac{\displaystyle\sum_{i=1}^{N} (X_{i} -\mu)^{2}}{N}\end{equation}

For a sample, it's indicated as ***s2*** calculated like this:

\begin{equation}s^{2} = \frac{\displaystyle\sum_{i=1}^{n} (x_{i} -\bar{x})^{2}}{n-1}\end{equation}

In both cases, we sum the difference between the individual data values and the mean and square the result. Then, for a full population we just divide by the number of data items to get the average. When using a sample, we divide by the total number of items **minus 1** to correct for sample bias.

Let's work this out for our student grades (assuming our data is a sample from the larger student population).

First, we need to calculate the mean grade:

\begin{equation}\bar{x} = \frac{50+50+46+95+50+5+57}{7}\approx 50.43\end{equation}

Then we can plug that into our formula for the variance:

\begin{equation}s^{2} = \frac{(50-50.43)^{2}+(50-50.43)^{2}+(46-50.43)^{2}+(95-50.43)^{2}+(50-50.43)^{2}+(5-50.43)^{2}+(57-50.43)^{2}}{7-1}\end{equation}

So:

\begin{equation}s^{2} = \frac{0.185+0.185+19.625+1986.485+0.185+2063.885+43.165}{6}\end{equation}

Which simplifies to:

\begin{equation}s^{2} = \frac{4113.715}{6}\end{equation}

Giving the result:

\begin{equation}s^{2} \approx 685.619\end{equation}

The higher the variance, the more spread your data is around the mean.

In Python, you can use the ***var*** function of the *pandas.dataframe* class to calculate the variance of a column in a dataframe:

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,17,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

print(df['Grade'].var())
```

    685.6190476190476

##### Standard Deviation
To calculate the variance, we squared the difference of each value from the mean. If we hadn't done this, the numerator of our fraction would always end up being zero (because the mean is at the center of our values). However, this means that the variance is not in the same unit of measurement as our data - in our case, since we're calculating the variance for grade points, it's in grade points squared; which is not very helpful.

To get the measure of variance back into the same unit of measurement, we need to find its square root:

\begin{equation}s = \sqrt{685.619} \approx 26.184\end{equation}

So what does this value represent?

It's the *standard deviation* for our grades data. More formally, it's calculated like this for a full population:

\begin{equation}\sigma = \sqrt{\frac{\displaystyle\sum_{i=1}^{N} (X_{i} -\mu)^{2}}{N}}\end{equation}

Or like this for a sample:

\begin{equation}s = \sqrt{\frac{\displaystyle\sum_{i=1}^{n} (x_{i} -\bar{x})^{2}}{n-1}}\end{equation}

Note that in both cases, it's just the square root of the corresponding variance forumla!

In Python, you can calculate it using the ***std*** function:

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,17,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})

print(df['Grade'].std())
```

    26.184328282754315

#### Standard Deviation in a Normal Distribution

In statistics and data science, we spend a lot of time considering *normal* distributions; because they occur so frequently. The standard deviation has an important relationship to play in a normal distribution.

Run the following cell to show a histogram of a *standard normal* distribution (which is a distribution with a mean of 0 and a standard deviation of 1):
http://onlinestatbook.com/stat_sim/sampling_dist/index.html

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

# Create a random standard normal distribution
df = pd.DataFrame(np.random.randn(100000, 1), columns=['Grade'])

# Plot the distribution as a histogram with a density curve
grade = df['Grade']
density = stats.gaussian_kde(grade)
n, x, _ = plt.hist(grade, color='lightgrey', normed=True, bins=100)
plt.plot(x, density(x))

# Get the mean and standard deviation
s = df['Grade'].std()
m = df['Grade'].mean()

# Annotate 1 stdev
x1 = [m-s, m+s]
y1 = [0.25, 0.25]
plt.plot(x1,y1, color='magenta')
plt.annotate('1s (68.26%)', (x1[1],y1[1]))

# Annotate 2 stdevs
x2 = [m-(s*2), m+(s*2)]
y2 = [0.05, 0.05]
plt.plot(x2,y2, color='green')
plt.annotate('2s (95.45%)', (x2[1],y2[1]))

# Annotate 3 stdevs
x3 = [m-(s*3), m+(s*3)]
y3 = [0.005, 0.005]
plt.plot(x3,y3, color='orange')
plt.annotate('3s (99.73%)', (x3[1],y3[1]))

# Show the location of the mean
plt.axvline(grade.mean(), color='grey', linestyle='dashed', linewidth=1)

plt.show()
```

The horizontal colored lines show the percentage of data within 1, 2, and 3 standard deviations of the mean (plus or minus).

In any normal distribution:
- Approximately 68.26% of values fall within one standard deviation from the mean.
- Approximately 95.45% of values fall within two standard deviations from the mean.
- Approximately 99.73% of values fall within three standard deviations from the mean.

#### Z Score
So in a normal (or close to normal) distribution, standard deviation provides a way to evaluate how far from a mean a given range of values falls, allowing us to compare where a particular value lies within the distribution. For example, suppose Rosie tells you she was the highest scoring student among her friends - that doesn't really help us assess how well she scored. She may have scored only a fraction of a point above the second-highest scoring student. Even if we know she was in the top quartile; if we don't know how the rest of the grades are distributed it's still not clear how well she performed compared to her friends.

However, if she tells you how many standard deviations higher than the mean her score was, this will help you compare her score to that of her classmates.

So how do we know how many standard deviations above or below the mean a particular value is? We call this a *Z Score*, and it's calculated like this for a full population:

\begin{equation}Z = \frac{x - \mu}{\sigma}\end{equation}

or like this for a sample:

\begin{equation}Z = \frac{x - \bar{x}}{s}\end{equation}

So, let's examine Rosie's grade of 95. Now that we know the *mean* grade is 50.43 and the *standard deviation* is 26.184, we can calculate the Z Score for this grade like this:

\begin{equation}Z = \frac{95 - 50.43}{26.184} = 1.702\end{equation}.

So Rosie's grade is 1.702 standard deviations above the mean.

### Summarizing Data Distribution in Python
We've seen how to obtain individual statistics in Python, but you can also use the ***describe*** function to retrieve summary statistics for all numeric columns in a dataframe. These summary statistics include many of the statistics we've examined so far (though it's worth noting that the *median* is not included):

```python
import pandas as pd

df = pd.DataFrame({'Name': ['Dan', 'Joann', 'Pedro', 'Rosie', 'Ethan', 'Vicky', 'Frederic'],
                   'Salary':[50000,54000,50000,189000,55000,40000,59000],
                   'Hours':[41,40,36,17,35,39,40],
                   'Grade':[50,50,46,95,50,5,57]})
print(df.describe())
```

                  Salary      Hours      Grade
    count       7.000000   7.000000   7.000000
    mean    71000.000000  35.428571  50.428571
    std     52370.475143   8.423324  26.184328
    min     40000.000000  17.000000   5.000000
    25%     50000.000000  35.500000  48.000000
    50%     54000.000000  39.000000  50.000000
    75%     57000.000000  40.000000  53.500000
    max    189000.000000  41.000000  95.000000

```python

```

MathsToML/Module04-Statistics and Probability/04-02-Statistics Fundamentals.ipynb


# Introducción a Sympy

Ademais das variables numéricas existen as variables simbólicas que permiten calcular
límites, derivadas, integrais etc., como se fai habitualmente nas clases de matemáticas.
Para poder facer estas operacións, habituais nun curso de Cálculo, é preciso ter instalada a libraría **Sympy**.

Ao contrario que o módulo **Math** ou o módulo **Numpy** que acabamos de revisar na práctica anterior, o módulo **Sympy** non traballa cunha estrutura de datos baseado en números (xa sexan de tipo enteiro ou dobre) senón que traballa con obxectos que posúen atributos e métodos que tratan de reproducir o comportamento matemático de variables, funcións, rexións, ecuacións, etc. coas que se traballa habitualmente nas disciplinas da álxebra e o cálculo diferencial e integral.

Para empregar directamente este guión de prácticas dende unha instalación de Python con *Anaconda*, basta con facer clic na aplicación 'Jupyter notebook' que xa está instalada por defecto (para máis detalles: https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html).

### Obxectivos:

- Uso de variables simbólicas
- Suposicións e requerimentos das variables
- Manipulación de expresións sinxelas en varias variables

## Instalación e carga do módulo
Para facer que estea dispoñible o módulo **Sympy**, hai que instalalo usando a ferramente `pip` (ou `conda` se estades a usar entornos de traballo diferenciados). No caso do uso de *Microsoft Azute Notebooks* (https://notebooks.azure.com/), empregaríase a seguinte instalación:

```python
!pip -q install sympy
```

    You are using pip version 19.3.1, however version 20.0.2 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.

Para dispoñer do módulo **Sympy** e importalo para o resto do guión de prácticas, usaremos:

```python
import sympy as sp
```

## Variables simbólicas
Para traballar en modo simbólico é necesario definir variables simbólicas e para facer
isto usaremos o función `sp.Symbol`. Vexamos algúns exemplos do seu uso:

```python
x = sp.Symbol('x') # define a variable simbólica x
y = sp.Symbol('y') # define a variable simbólica y
f = 3*x + 5*y # agora temos definida a expresion simbólica f
print(f)

a, b, c = sp.symbols('a:c') # define como simbólicas as variables a, b, c.
expresion = a**3 + b**2 + c
print(expresion)
```

    3*x + 5*y
    a**3 + b**2 + c

Por claridade na implementación e nos cálculos, será habitual que o nome da variable simbólica e o nome do obxecto **Sympy** no que se alamacena coincidan, pero isto non ter porque ser así:

```python
a = sp.Symbol('x')
print(a)
a.name
```

    x

    'x'

Debemos ter claso que agora as variables `x` ou `y` definidas antes non son números, nin tampouco pertencen aos obxectos definidos co módulo **Numpy** revisado na práctica anterior. Todas as variables simbólicas son obxectos da clase `sp.Symbol` e os seus atributos e métodos son completamente diferentes aos que aparecían ás variables numéricas e vectores de **Numpy**:

```python
print(type(x))
dir(x)
```

    <class 'sympy.core.symbol.Symbol'>

    ['_Symbol__xnew_cached_',
     '__abs__',
     '__add__',
     '__and__',
     '__call__',
     '__class__',
     '__complex__',
     '__delattr__',
     '__div__',
     '__doc__',
     '__eq__',
     '__float__',
     '__format__',
     '__ge__',
     '__getattribute__',
     '__getnewargs__',
     '__getstate__',
     '__gt__',
     '__hash__',
     '__init__',
     '__int__',
     '__invert__',
     '__le__',
     '__long__',
     '__lshift__',
     '__lt__',
     '__mod__',
     '__module__',
     '__mul__',
     '__ne__',
     '__neg__',
     '__new__',
     '__new_stage2__',
     '__or__',
     '__pos__',
     '__pow__',
     '__radd__',
     '__rand__',
     '__rdiv__',
     '__reduce__',
     '__reduce_ex__',
     '__repr__',
     '__rlshift__',
     '__rmod__',
     '__rmul__',
     '__ror__',
     '__rpow__',
     '__rrshift__',
     '__rshift__',
     '__rsub__',
     '__rtruediv__',
     '__rxor__',
     '__setattr__',
     '__setstate__',
     '__sizeof__',
     '__slots__',
     '__str__',
     '__sub__',
     '__subclasshook__',
     '__truediv__',
     '__xnew__',
     '__xor__',
     '_args',
     '_assumptions',
     '_compare_pretty',
     '_diff_wrt',
     '_eval_adjoint',
     '_eval_as_leading_term',
     '_eval_conjugate',
     '_eval_derivative',
     '_eval_evalf',
     '_eval_expand_complex',
     '_eval_interval',
     '_eval_is_algebraic_expr',
     '_eval_is_negative',
     '_eval_is_polynomial',
     '_eval_is_positive',
     '_eval_is_rational_function',
     '_eval_lseries',
     '_eval_nseries',
     '_eval_power',
     '_eval_rewrite',
     '_eval_simplify',
     '_eval_subs',
     '_eval_transpose',
     '_evalf',
     '_expand_hint',
     '_explicit_class_assumptions',
     '_from_mpmath',
     '_has',
     '_has_matcher',
     '_hashable_content',
     '_mhash',
     '_op_priority',
     '_parse_order',
     '_prop_handler',
     '_random',
     '_recursive_call',
     '_sage_',
     '_sanitize',
     '_sorted_args',
     '_subs',
     '_to_mpmath',
     'adjoint',
     'apart',
     'args',
     'args_cnc',
     'as_base_exp',
     'as_coeff_Add',
     'as_coeff_Mul',
     'as_coeff_add',
     'as_coeff_exponent',
     'as_coeff_mul',
     'as_coefficient',
     'as_coefficients_dict',
     'as_content_primitive',
     'as_dummy',
     'as_expr',
     'as_independent',
     'as_leading_term',
     'as_numer_denom',
     'as_ordered_factors',
     'as_ordered_terms',
     'as_poly',
     'as_powers_dict',
     'as_real_imag',
     'as_terms',
     'assumptions0',
     'atoms',
     'cancel',
     'canonical_variables',
     'class_key',
     'coeff',
     'collect',
     'combsimp',
     'compare',
     'compute_leading_term',
     'conjugate',
     'copy',
     'could_extract_minus_sign',
     'count',
     'count_ops',
     'default_assumptions',
     'diff',
     'doit',
     'dummy_eq',
     'equals',
     'evalf',
     'expand',
     'extract_additively',
     'extract_branch_factor',
     'extract_multiplicatively',
     'factor',
     'find',
     'free_symbols',
     'fromiter',
     'func',
     'getO',
     'getn',
     'has',
     'integrate',
     'invert',
     'is_Add',
     'is_AlgebraicNumber',
     'is_Atom',
     'is_Boolean',
     'is_Derivative',
     'is_Dummy',
     'is_Equality',
     'is_Float',
     'is_Function',
     'is_Integer',
     'is_Matrix',
     'is_Mul',
     'is_Not',
     'is_Number',
     'is_NumberSymbol',
     'is_Order',
     'is_Piecewise',
     'is_Poly',
     'is_Pow',
     'is_Rational',
     'is_Relational',
     'is_Symbol',
     'is_Vector',
     'is_Wild',
     'is_algebraic',
     'is_algebraic_expr',
     'is_antihermitian',
     'is_bounded',
     'is_commutative',
     'is_comparable',
     'is_complex',
     'is_composite',
     'is_constant',
     'is_even',
     'is_finite',
     'is_hermitian',
     'is_hypergeometric',
     'is_imaginary',
     'is_infinite',
     'is_infinitesimal',
     'is_integer',
     'is_irrational',
     'is_negative',
     'is_noninteger',
     'is_nonnegative',
     'is_nonpositive',
     'is_nonzero',
     'is_number',
     'is_odd',
     'is_polar',
     'is_polynomial',
     'is_positive',
     'is_prime',
     'is_rational',
     'is_rational_function',
     'is_real',
     'is_transcendental',
     'is_unbounded',
     'is_zero',
     'iter_basic_args',
     'leadterm',
     'limit',
     'lseries',
     'match',
     'matches',
     'n',
     'name',
     'normal',
     'nseries',
     'nsimplify',
     'powsimp',
     'primitive',
     'radsimp',
     'ratsimp',
     'rcall',
     'refine',
     'removeO',
     'replace',
     'rewrite',
     'round',
     'separate',
     'series',
     'simplify',
     'sort_key',
     'subs',
     'taylor_term',
     'together',
     'transpose',
     'trigsimp',
     'xreplace']

Con **Sympy** pódense definir constantes enteiras ou números racioanais (todas de forma simbólica) de xeito doado usando o comando `sp.Integer` ou `sp.Rational`. Por exemplo, podemos definir a constante simbólica $1/3$. Se fixeramos o mesmo con números representados por defecto en Python, obteríamos resultados moi diferentes. Observa tamén a diferenza que existe entre o tipo
de dato asignado no espazo de traballo

```python
a = sp.Rational('1/3')
b = sp.Integer('6')/sp.Integer('3')
c = 1/3
d = 1.0/3.0
print(a)
print(b)
print(c)
print(d)
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(a)
print(b)
```

    1/3
    2
    0
    0.333333333333
    <class 'sympy.core.numbers.Rational'>
    <class 'sympy.core.numbers.Integer'>
    <type 'int'>
    <type 'float'>
    1/3
    2

Outra forma sinxela de manexar valores constante mediante obxectos do módulo **Sympy** é usar a función `sp.S`. Unha vez feitos todos os cálculos simbólicos, se precisamos obter o valor numérico, empregaríase a función `sp.N` ou ben directamente `float`:

```python
a = sp.S(2)
b = sp.S(6)
c = a/b
d = sp.N(c)
e = float(c)
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(c)
print(d)
print('{0:.15f}'.format(e))
```

    <class 'sympy.core.numbers.Integer'>
    <class 'sympy.core.numbers.Integer'>
    <class 'sympy.core.numbers.Rational'>
    <class 'sympy.core.numbers.Float'>
    <type 'float'>
    1/3
    0.333333333333333
    0.333333333333333

Ao longo do curso usaremos asiduamente dous números reais que podes definir como constantes simbólicas: $\pi$ e o numéro $e$. Do mesmo xeito, para operar con variables ou constantes simbólicas, debemos empregar funcións que sexan capaces de manipular este tipo de obxectos, todas elas implementadas no módulo **Sympy** (por exemplo, `sp.sin`, `sp.cos`, `sp.log`, etc)

```python
import numpy as np
print(np.pi)
print(type(np.pi))

p=sp.pi # definición da constante pi
print(sp.cos(p))

e = sp.E # definición do número e
print(sp.log(e))

print(sp.N(sp.pi,1000))
print(type(sp.N(sp.pi,100)))
```

    3.14159265359
    <type 'float'>
    -1
    1
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198
    <class 'sympy.core.numbers.Float'>

## Suposicións sobre as variables

Cando se define unha variable simbólica se lle pode asignar certa información adicional sobre o tipo de valores que pode acadar, ou as suposicións que se lle van a aplicar. Por exemplo, podemos decidir antes de facer calquera cálculo se a variable toma valores enteiros ou reais, se é positiva ou negativa, maior que un certo número, etc. Este tipo de información engádese no momento da definición da variable simbólica como un argumento opcional.

```python
x = sp.Symbol('x', nonnegative = True) # A raíz cadrada dun número non negativo é real
y = sp.sqrt(x)
print(y.is_real)

x = sp.Symbol('x', integer = True) # A potencia dun número enteiro é enteira
y = x**sp.S(2)
print(y.is_integer)

a = sp.Symbol('a')
b = sp.sqrt(a)
print(b.is_real)

a = sp.Symbol('a')
b = a**sp.S(2)
print(b.is_integer)
```

    True
    True
    None
    None

Posto que os cálculos simbólicos son consistentes en **Sympy**, se poden tamén facer comprobacións sobre se algunhas desigualdades son certas ou non, sempre e cando se teña coidado nas suposicións que se fagan ao definir as variables simbólicas

```python
x = sp.Symbol('x', real = True)
p = sp.Symbol('p', positive = True)
q = sp.Symbol('q', real = True)
y = sp.Abs(x) + p # O valor absoluto
z = sp.Abs(x) + q
print(y > 0)
print(z > 0)
```

    True
    q + Abs(x) > 0

## Manipulación de expresións simbólicas

Do mesmo xeito que o módulo **Sympy** nos permite definir variables simbólicas, tamén podemos definir expresións matemáticas a partir destas e manipulalas, factorizándoas, expandíndoas, simplificalas, ou mesmo imprimilas dun xeito similar a como o faríamos con lápiz e papel

```python
x,y = sp.symbols('x,y', real=True)
expr = (x-3)*(x-3)**2*(y-2)
expr_long = sp.expand(expr) # Expandir expresión

print(expr_long) # Imprimir de forma estándar
sp.pprint(expr_long) # Imprimir de forma semellante a con lápiz e papel

expr_short = sp.factor(expr)
print(expr_short) # Factorizar expresión

expr = -3+(x**2-6*x+9)/(x-3)
expr_simple = sp.simplify(expr) # Simplificar expresión
sp.pprint(expr)
print(expr_simple)
```

    x**3*y - 2*x**3 - 9*x**2*y + 18*x**2 + 27*x*y - 54*x - 27*y + 54
     3        3      2         2
    x ⋅y - 2⋅x  - 9⋅x ⋅y + 18⋅x  + 27⋅x⋅y - 54⋅x - 27⋅y + 54
    (x - 3)**3*(y - 2)
          2
         x  - 6⋅x + 9
    -3 + ────────────
            x - 3
    x - 6

Dada unha expresión en **Sympy** tamén se pode manipulala, substituindo unhas variables simbólica por outras ou mesmo reemprazando as variables simbólicas por constantes. Para facer este tipo de substitucións emprégase a función `subs` e os valores a utilizar na substitución veñen definidos por un diccionario de Python:

```python
x,y = sp.symbols('x,y', real=True)
expr = x*x + x*y + y*x + y*y
res = expr.subs({x:1, y:2}) # Substitutición das variables simbólicas por constantes
print(res)

expr_sub = expr.subs({x:1-y}) # Subsitución de variable simbólica por unha expresión
sp.pprint(expr_sub)
print(sp.simplify(expr_sub))
```

    9
     2                          2
    y  + 2⋅y⋅(-y + 1) + (-y + 1)
    1

### **Exercicio 2.1**
Define a expresión dada pola suma dos termos seguintes:
$$
a+a^2+a^3+\ldots+a^N,
$$
onde $a$ é unha variable real arbitraria e $N$ e un valor enteiro positivo.

```python
# O TEU CÓDIGO AQUÍ
```

### **Exercicio 2.2**
Cal é o valor exacto da anterior expresión cando $N=15$ e $a=5/6$? Cal é valor numérico en coma flotante?

```python
# O TEU CÓDIGO AQUÍ
```


Taylor Series
=============

Suppose you have some function that may be expensive or difficult to evaluate and so you’d like to find an easy approximation for that function in some limited domain. One particularly nice way to handle that is with a polynomial approximation since they are easy to compute. The question is: How can we find such a polynomial? One easy answer is called the "Taylor Series" set up like so:

$$f(x) = A + B(x-x_0) + C(x-x_0)^2 + D(x-x_0)^3 + \cdots$$

So.. $x_0$ is the point around which we are interested in finding an approximation, but what are the values coefficients $A$, $B$, $C$, etc. of the binomials $(x-x_0)^n$ ? There are many ways to answer that question but *Taylor* *Series* answer is that the value of the function, and all of its derivatives *must* *match* the value of the *series* and all of *its* derivatives at the point x0. Let’s try it and see what happens: Just substitute $x \rightarrow x_0$ in the expression.

What happens? Well.. $x_0 - x_0$ is nothing but $0$ so all but the first term go to zeor and we just get:

$$f(x_0) = A$$

We have our first coefficient, $A=f(x_0)$, so $A$ is simply equal to the *value* of the function at the point of interest.

What about $B$, $C$, and all the rest? Let’s demand that the first derivative of $f(x)$ match the first derivative of the series and see what we discover. What is the first derivative of the series?

$$f'(x) = 0 + B + 2C(x-x_0) + 3D(x-x_0)^2 + \cdots $$

Now, put $x \rightarrow x_0$ and see what we get:

$$f'(x_0) = 0 + B + 0 + \cdots $$

so $B = f'(x_0)$, see how easy this is!

Now try the second derivative:

$$f''(x) = 0 + 0 + 2C + 6D(x-x_0) + \cdots $$

putting $x \rightarrow x_0$ and we see:

$$f''(x_0) = 2C$$

so $C=f''(x_0)/2$. If you carry this on for a bit you can see you get:

$$f(x) \approx f(x_0) + f'(x_0)(x-x_0) + \frac{1}{2}f''(x_0)(x-x_0)^2 + \cdots$$

or more generally:

$$f(x) = \sum_{n=0}^{n=\infty} \frac{f^{(n)}(x_0)}{n!}(x-x_0)^n$$

Morse Potential
===============

Let's use this technique to analyze the motion of nitrogen atoms in a nitrogen molecule. The atoms live in an attractive potential of the form:

$$U(x) = U_m \left( \left(1-e^{-\alpha(x-x_0)}\right)^2 - 1\right)$$

Let's load up sympy and define all the symbols we need: $U_m$, $x$, $x_0$ and $\alpha$:

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

```python
Um,x,x0,alpha=symbols('Um x x_0 alpha', real=True)
```

Next let's define the Morse Potential function $U(x)$:

```python
U = Um*((1-exp(-alpha*(x-x0)))**2-1)
U
```

Next, Let's take the derivatite of $U(x)$ with respect to $x$: $U'(x)$.

Remember the force is $-U'(x)$ so the point in space where $U'(x)=0$ is the equilibrium position.

```python
Up=U.diff(x)
print("Python expression for Force:")
print("Force is:",-Up)
print("Latex:", latex(Up))
print("Pretty math")
Up
```

Next we can solve for the value of x where $\frac{dU}{dx} = 0$.

```python
solve(Up,x)
```

Aha! So the function has a minimum at $x=x_0$. Now take the *second* derivative of $U(x)$ with respect to $x$: $U''(x)$

```python
Upp=Up.diff(x)
Upp
```

OK, we can use the `subs` method to evalute $U''(x)$ where $x=x_0$:

```python
Upp.subs(x,x0)
```

So, we see now that:

$$\frac{d^2U}{dx^2}\bigg|_{x_0} = 2 U_m \alpha^2$$

so we can connect the parameters of the Morse Potential to the Taylor Series coefficients near the minimum of the Potential.

Project 3
===========

See the podcast! But basically the project is to use the experimental data, the analytical approximation (Taylor Series) to the Morse Potential and the Heun Method to:

* Verify/show that the experimental data is consistent with the Morse Potential model.

* Use the Heun Method to compute the period of small oscillations about the equilibrium position and compare that to the experimental data *and* the Taylor Series result.

$U_m$ = 7.37 eV

$x_0$ = 1.2 A

$\alpha = 2.287\ A^{-1}$

$f$ = $5.19\times 10^{13}$ Hz

IMPORTANT NOTES
---------------

1) Without careful attention to keeping the namespaces separate it's not really practical to use sympy and pylab at the same time in the same notebook. If you're interested in this, I have a notebook that illustrates how this can be done, but I don't want overload you with too many new concepts in this one lesson. If you're already confused just take my advice and don't mix them. Doing this is easy: Create *one* notebook for sympy. Take the *result* of that notebook and use it to produce the python expression you need for the numerial algorithm. Then create a *second* notebook and simply *paste* python expression produced by the first notebook in the evaluation of the force for the second notebook. Simple.

2) This project is meant to add two tools to your toolbox 1: sympy, 2: Talor Series expansion. Both of these are really just analytical tools that work with algebra and calculus. We're going to use these tools to understand and approximate the low amplitude behavior system interacting under the Morse Potential. Then we're going to *check* our understanding and approximation by calculating the (arbitrary amplitude) behavior of a system interacting under the Morse Potential numericaly and evaluating the frequency at various amplitudes. So:

    a) Use sympy/Taylor series to estimate the low-amplitude frequency of the system
    b) Use sympy to get a python expression for the Force of interaction
    c) Use a numerical method (e.g., Heun Algorithm) to compute the time evolution (frequency) of the system
    d) Compare the results of (c) and (a)
    e) Once you've validated the code in step "d" experiment with increasing the amplitude of motion.
    f) Describe how the period and trajectory of the particle changes.

Please ask if you have questions!

```python

```


# Matrices and Matrix Operations

This function introduces various ways to create
matrices and how to use them in TensorFlow

```python
import numpy as np
import tensorflow as tf
# from tensorflow.python.framework import ops
# ops.reset_default_graph()  # 既存のグラフをリセット
tf.reset_default_graph()  # 上と同じこと
```

Start a graph session

```python
sess = tf.Session()
```

### Declaring matrices

Identity Matrix:

```python
identity_matrix = tf.diag([1.0,1.0,1.0])
print(sess.run(identity_matrix))
```

    [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]

2x3 random norm matrix:

```python
A = tf.truncated_normal([2,3])
print(sess.run(A))
```

    [[ 1.5801364  1.1259041 -1.3550861]
     [ 0.86778   -1.3771477  1.9069912]]

2x3 constant matrix:

```python
B = tf.fill([2,3], 5.0)
print(sess.run(B))
```

    [[5. 5. 5.]
     [5. 5. 5.]]

3x2 random uniform matrix:

```python
C = tf.random_uniform([3,2])
print(sess.run(C))
```

    [[0.53089714 0.627354  ]
     [0.07326257 0.23314726]
     [0.34148204 0.77786624]]

Create matrix from np array:

```python
D = tf.convert_to_tensor(np.array([[1., 2., 3.], [-3., -7., -1.], [0., 5., -2.]]))
print(sess.run(D))
```

    [[ 1.  2.  3.]
     [-3. -7. -1.]
     [ 0.  5. -2.]]

### Matrix Operations

Matrix addition/subtraction:

```python
print(sess.run(A+B))
print(sess.run(B-B))
```

    [[5.1397653 5.7964816 3.9101331]
     [4.082012  6.481913  5.5925517]]
    [[0. 0. 0.]
     [0. 0. 0.]]

Matrix Multiplication:

```python
print(sess.run(tf.matmul(B, identity_matrix)))
```

    [[5. 5. 5.]
     [5. 5. 5.]]

tf.matmul()の引数には各行列を転置するかどうかも指定できる

https://www.tensorflow.org/api_docs/python/tf/matmul

Matrix Transpose:

```python
print(sess.run(tf.transpose(C)))
```

    [[0.49928522 0.22033381 0.87936306]
     [0.32459974 0.53317904 0.41480982]]

Matrix Determinant:

```python
print(sess.run(tf.matrix_determinant(D)))
```

    -37.99999999999999

Matrix Inverse:

```python
print(sess.run(tf.matrix_inverse(D)))
```

    [[-0.5        -0.5        -0.5       ]
     [ 0.15789474  0.05263158  0.21052632]
     [ 0.39473684  0.13157895  0.02631579]]

Cholesky Decomposition:

```python
print(sess.run(tf.cholesky(identity_matrix)))
```

    [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]

Eigenvalues and Eigenvectors:  We use `tf.self_adjoint_eig()` function, which returns two objects, first one is an array of eigenvalues, the second is a matrix of the eigenvectors.

```python
eigenvalues, eigenvectors = sess.run(tf.self_adjoint_eig(D))
print(eigenvalues)
print(eigenvectors)
```

    [-10.65907521  -0.22750691   2.88658212]
    [[ 0.21749542  0.63250104 -0.74339638]
     [ 0.84526515  0.2587998   0.46749277]
     [-0.4880805   0.73004459  0.47834331]]

self-adjoint matrices とは自己随伴行列の英単語

つまりエルミート行列のことをさす

```python
eigenvectors[:, 0]  # eigenvalues[0]に対する固有ベクトル
```

    array([ 0.21749542,  0.84526515, -0.4880805 ])

つまり，`tf.self_adjoint_eig()`関数は

\begin{equation}\label{eq:}
    D \cdot \text{eigenvectors[:, i]} =  \text{eigenvalues[i]} \cdot \text{eigenvectors[:, i]}
\end{equation}

のような固有値分解をしている(ただし, i = 0 ~ D.shape[1])


```python
%pylab inline
import sympy
sympy.init_printing()
```

    Populating the interactive namespace from numpy and matplotlib

```python
t, sigma, omega = sympy.symbols('t, sigma, omega')
s = sympy.symbols('s', complex=True)
```

```python
def my_laplace_transform(f):
    return sympy.Integral(f*sympy.exp(-s*t), (t, 0, sympy.oo))

my_laplace_transform(t)
```

```python
def my_laplace_transform_double_sided(f):
    return sympy.Integral(f*sympy.exp(-s*t), (t, -sympy.oo, sympy.oo))

my_laplace_transform_double_sided(1).doit()
```

```python
sympy.integrate(sympy.exp(-s*t), t)
```

```python
my_laplace_transform(sympy.sin(2*t)).doit().simplify()
```

```python
my_laplace_transform(t).doit()
```

```python
sympy.laplace_transform(sympy.sin(t), t, s)
```

```python
omega = sympy.symbols('omega')
my_laplace_transform(sympy.sin(omega*t)).doit().simplify()
```

# Unit Step

```python
sympy.laplace_transform(1, t, s)
```

```python
y = sympy.exp(-2*t)
sympy.laplace_transform(y, s, t)
```

```python
sympy.plot(y, (t, 0, 5))
```

```python
Y = sympy.laplace_transform(y, t, s)[0]
Y
```