# Thinking probabilistically - Continuous variables
<b>

1. Probability density functions
2. Introduction to the Normal distribution
    1. The Normal PDF
    2. The Normal CDF
3. The Normal distribution: Properties and warnings
4. Are the Belmont Stakes results Normally distributed?
5. What are the chances of a horse matching or beating Secretariat's record?
6. The Exponential distribution
7. If you have a story, you can simulate it!
8. Distribution of no-hitters and cycles

</b>

## Probability density functions
- Continuous variables
    - Quantities that can take any value, not just discrete values
- Probability Density function (PDF)
    - Continuous analog to the PMF
    - Mathematical description of the relative likelihood of observing a value of a continuous variable


## Introduction to the Normal distribution

* Normal distribution: Describes a continuous variable whose PDF has a single symmetric peak.

$$ \begin{align} \text{mean of a Normal distribution} & \neq \text{mean computed from data} \\ \text{st. dev of a Normal distribution} & \neq \text{st. dev computed from data} \end{align} $$

### The Normal PDF: 

In this exercise, you will explore the Normal PDF and also learn a way to plot a PDF of a known distribution using hacker statistics. Specifically, you will plot a Normal PDF for various values of the variance.


```
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
```


```
sns.set()
```


```
# with stds of interest: samples_std1, samples_std3, stamples_std10
samples_std1 = np.random.normal(20, 1, 100000)
samples_std3 = np.random.normal(20, 3, 100000)
samples_std10 = np.random.normal(20, 10, 100000)
```


```
# make histogram
plt.figure(figsize=(30,15))
_ = plt.hist(samples_std1,histtype='step',density=True,bins=100)
_ = plt.hist(samples_std3,histtype='step',density=True,bins=100)
_ = plt.hist(samples_std10,histtype='step',density=True,bins=100)

# Make a legends and set limits
_ = plt.legend(('std = 1', 'std = 3', 'std = 10'))
plt.ylim(-0.01, 0.42)
```

### The Normal CDF

Now that you have a feel for how the Normal PDF looks, let's consider its CDF. Using the samples you generated in the last exercise (in your namespace as samples_std1, samples_std3, and samples_std10), generate and plot the CDFs.


```
def ecdf(data):
    """Compute ECDF for a one-dimensional array of measurements."""
    # Number of data points: n
    n = len(data)

    # x-data for Ecdf 
    x = np.sort(data)

    # y-data for the ecdf-y
    y =np.arange(1,n+1)/n

    return x,y
```


```
x_std1, y_std1 = ecdf(samples_std1)
x_std3, y_std3 = ecdf(samples_std3)
x_std10, y_std10 = ecdf(samples_std10)
```


```
# Plot CDF
_ = plt.plot(x_std1,y_std1,marker='.',linestyle=None)
_ = plt.plot(x_std3,y_std3,marker='*',linestyle=None)
_ = plt.plot(x_std10,y_std10,marker='o',linestyle=None)
# Make a legend
_ = plt.legend(('std = 1', 'std = 3', 'std = 10'), loc='lower right')
```



### The Normal distribution: Properties and warnings
**Are the Belmont Stakes results Normally distributed?**

Since 1926, the Belmont Stakes is a 1.5 mile-long race of 3-year old thoroughbred horses. [Secretariat](https://en.wikipedia.org/wiki/Secretariat_(horse) ) ran the fastest Belmont Stakes in history in 1973. While that was the fastest year, 1970 was the slowest because of unusually wet and sloppy conditions. With these two outliers removed from the data set, compute the mean and standard deviation of the Belmont winners' times. Sample out of a Normal distribution with this mean and standard deviation using the ```np.random.normal()``` function and plot a CDF. Overlay the ECDF from the winning Belmont times. Are these close to Normally distributed?

Note: Justin scraped the data concerning the Belmont Stakes from the [Belmont Wikipedia page](https://en.wikipedia.org/wiki/Belmont_Stakes).


```
url = "https://raw.githubusercontent.com/reddyprasade/Data-Analysis-with-Python/main/Statistics/Data/belmont.csv"
```


```
df = pd.read_csv(url)
df
```


```
df.head()
```


```
df.tail()
```


```
df.info()
```


```
df.describe()
```


```
belmont_no_outliers = np.array([148.51, 146.65, 148.52, 150.7 , 150.42, 150.88, 151.57, 147.54,
       149.65, 148.74, 147.86, 148.75, 147.5 , 148.26, 149.71, 146.56,
       151.19, 147.88, 149.16, 148.82, 148.96, 152.02, 146.82, 149.97,
       146.13, 148.1 , 147.2 , 146.  , 146.4 , 148.2 , 149.8 , 147.  ,
       147.2 , 147.8 , 148.2 , 149.  , 149.8 , 148.6 , 146.8 , 149.6 ,
       149.  , 148.2 , 149.2 , 148.  , 150.4 , 148.8 , 147.2 , 148.8 ,
       149.6 , 148.4 , 148.4 , 150.2 , 148.8 , 149.2 , 149.2 , 148.4 ,
       150.2 , 146.6 , 149.8 , 149.  , 150.8 , 148.6 , 150.2 , 149.  ,
       148.6 , 150.2 , 148.2 , 149.4 , 150.8 , 150.2 , 152.2 , 148.2 ,
       149.2 , 151.  , 149.6 , 149.6 , 149.4 , 148.6 , 150.  , 150.6 ,
       149.2 , 152.6 , 152.8 , 149.6 , 151.6 , 152.8 , 153.2 , 152.4 ,
       152.2 ])
```


```
# Compute mean and standard deviation: mu, sigma
mu = np.mean(belmont_no_outliers)
sigma = np.std(belmont_no_outliers)
```


```
# Sample out of a normal distribution with this mu and sigma: samples
samples = np.random.normal(mu, sigma, size=10000)
```


```
# Get the CDF of the samples and of the data
x_theor, y_theor = ecdf(belmont_no_outliers) # orginal data

x, y = ecdf(samples) # normal distribution with this mu and sigma: samples
```


```
# Plot the CDFs
plt.figure(figsize=(30,15))
_ = plt.plot(x_theor, y_theor) # Orginal data
_ = plt.plot(x, y, marker='.', linestyle='none') # Samples
_ = plt.xlabel('Belmont winning time (sec.)')
_ = plt.ylabel('CDF')
#plt.legend()
```

What are the chances of a horse matching or beating Secretariat's record?

Assume that the Belmont winners' times are Normally distributed (with the 1970 and 1973 years removed), what is the probability that the winner of a given Belmont Stakes will run it as fast or faster than Secretariat?


```
# Take a million samples out of the Normal distribution: samples
samples = np.random.normal(mu,sigma,size=1000000)

# Compute the fraction that are faster than 144 seconds: prob
prob = np.sum(samples/144)/len(samples)
# Print the result
print('Probability of besting Secretariat:', prob)
```

## The Exponential distribution
- The **waiting time** between **arrivals** of a Poisson process is Exponentially distributed


**If you have a story, you can simulate it!**

- Sometimes, the story describing our probability distribution does not have a named distribution to go along with it. In these cases, fear not! You can always simulate it. We'll do that in this and the next exercise.

- In earlier exercises, we looked at the rare event of no-hitters in Major League Baseball. Hitting the cycle is another rare baseball event. When a batter hits the cycle, he gets all four kinds of hits, a single, double, triple, and home run, in a single game. Like no-hitters, this can be modeled as a Poisson process, so the time between hits of the cycle are also Exponentially distributed.

- How long must we wait to see both a no-hitter and then a batter hit the cycle? The idea is that we have to wait some time for the no-hitter, and then after the no-hitter, we have to wait for hitting the cycle. Stated another way, what is the total waiting time for the arrival of two different Poisson processes? The total waiting time is the time waited for the no-hitter, plus the time waited for the hitting the cycle.

Now, you will write a function to sample out of the distribution described by this story.


```
def successive_poisson(tau1,tau2,size=1):
    """
    We ahave Compute time for arrival Successive Poisson.
    """
    # Draw samples out of first exponential distribution: t1
    t1 = np.random.exponential(tau1,size)

    # Draw samples out of Second exponential distribution: t2

    t2 = np.random.exponential(tau2,size)

    return t1,t2
```

### Distribution of no-hitters and cycles
Now, you'll use your sampling function to compute the waiting time to observe a no-hitter and hitting of the cycle. The mean waiting time for a no-hitter is 764 games, and the mean waiting time for hitting the cycle is 715 games.


```
# Draw samples of waiting times: waiting_times
waiting_times = successive_poisson(764, 715, size=100000)

# Make the histogram
plt.figure(figsize=(30,15))
_ = plt.hist(waiting_times,bins=100,histtype='step',density=True)


# Label axes
_ = plt.xlabel('waiting times')
_ = plt.ylabel('PDF')
```

79aefe469e405a90528aea0cbf364bd4e6f3412e
ipynb
Jupyter Notebook
Statistics/STAST-EN-2021-106 Thinking probabilistically - Continuous variables.ipynb
reddyprasade/Data-Analysis-with-Python-
2440e23486856eea5556c8262467b3a618032bc2
MIT
2021-06-29T23:15:05.000Z
2021-06-29T23:15:05.000Z
Statistics/STAST-EN-2021-106 Thinking probabilistically - Continuous variables.ipynb
reddyprasade/Data-Analysis-with-Python-
2440e23486856eea5556c8262467b3a618032bc2
MIT
Statistics/STAST-EN-2021-106 Thinking probabilistically - Continuous variables.ipynb
reddyprasade/Data-Analysis-with-Python-
2440e23486856eea5556c8262467b3a618032bc2
MIT
2021-12-20T10:04:53.000Z
2021-12-20T10:04:53.000Z
Qwen/Qwen-72B
1. YES
2. YES