0% found this document useful (0 votes)
8 views

Foundations of Probability in Python - Part 3

The document discusses normal and Poisson distributions. It covers key concepts like probability density function, cumulative distribution function, standard deviation, sampling, and examples of calculating probabilities and intervals using the scipy.stats distributions in Python.

Uploaded by

Mohamed Gaber
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Foundations of Probability in Python - Part 3

The document discusses normal and Poisson distributions. It covers key concepts like probability density function, cumulative distribution function, standard deviation, sampling, and examples of calculating probabilities and intervals using the scipy.stats distributions in Python.

Uploaded by

Mohamed Gaber
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Normal distributions

F O U N D AT I O N S O F P R O B A B I L I T Y I N P Y T H O N

Alexander A. Ramírez M.
CEO @ Synergy Vision
Modeling for measures

FOUNDATIONS OF PROBABILITY IN PYTHON


Adults' heights example

FOUNDATIONS OF PROBABILITY IN PYTHON


Probability density

FOUNDATIONS OF PROBABILITY IN PYTHON


Probability density examples

FOUNDATIONS OF PROBABILITY IN PYTHON


Probability density and probability

FOUNDATIONS OF PROBABILITY IN PYTHON


Symmetry

FOUNDATIONS OF PROBABILITY IN PYTHON


Mean

FOUNDATIONS OF PROBABILITY IN PYTHON


Mean (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


Mean (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


Standard deviation

FOUNDATIONS OF PROBABILITY IN PYTHON


Standard deviation (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


Standard deviation (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


One standard deviation

FOUNDATIONS OF PROBABILITY IN PYTHON


Two standard deviations

FOUNDATIONS OF PROBABILITY IN PYTHON


Three standard deviations

FOUNDATIONS OF PROBABILITY IN PYTHON


Normal sampling
# Import norm, matplotlib.pyplot, and seaborn
from scipy.stats import norm
import matplotlib.pyplot as plt
import seaborn as sns

# Create the sample using norm.rvs()


sample = norm.rvs(loc=0, scale=1, size=10000, random_state=13)

# Plot the sample


sns.distplot(sample)
plt.show()

FOUNDATIONS OF PROBABILITY IN PYTHON


Normal sampling (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


Let's do some
exercises with
normal distributions
F O U N D AT I O N S O F P R O B A B I L I T Y I N P Y T H O N
Normal probabilities
F O U N D AT I O N S O F P R O B A B I L I T Y I N P Y T H O N

Alexander A. Ramírez M.
CEO @ Synergy Vision
Probability density
In Python this can be done in a couple of
lines:

# Import norm
from scipy.stats import norm

# Calculate the probability density


# with pdf
norm.pdf(-1, loc=0, scale=1)

0.24197072451914337

loc parameter speci es the mean and


scale parameter speci es the standard
deviation.

FOUNDATIONS OF PROBABILITY IN PYTHON


pdf() vs. cdf()

FOUNDATIONS OF PROBABILITY IN PYTHON


pdf() vs. cdf() (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


pdf() vs. cdf() (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


Cumulative distribution function examples

# Calculate cdf of -1 # Calculate cdf of 0.5


norm.cdf(-1) norm.cdf(0.5)

0.15865525393145707 0.6914624612740131

FOUNDATIONS OF PROBABILITY IN PYTHON


The percent point function (ppf)

# Calculate ppf of 0.2 # Calculate ppf of 55%


norm.ppf(0.2) norm.ppf(0.55)

-0.8416212335729142 0.12566134685507416

FOUNDATIONS OF PROBABILITY IN PYTHON


ppf() is the inverse of cdf()

# Calculate cdf of value 0 # Calculate ppf of probability 50%


norm.cdf(0) norm.ppf(0.5)

0.5 0

FOUNDATIONS OF PROBABILITY IN PYTHON


Probability between two values

# Create our variables


a = -1
b = 1
# Calculate the probability between
# two values, subtracting
norm.cdf(b) - norm.cdf(a)

0.6826894921370859

FOUNDATIONS OF PROBABILITY IN PYTHON


Tail probability

# Create our variable


a = 1

# Calculate the complement


# of cdf() using sf()
norm.sf(a)

0.15865525393145707

FOUNDATIONS OF PROBABILITY IN PYTHON


Tails

# Create our variables


a = -2
b = 2
# Calculate tail probability
# by adding each tail
norm.cdf(a) + norm.sf(b)

0.04550026389635839

FOUNDATIONS OF PROBABILITY IN PYTHON


Tails (Cont.)

# Create our variables


a = -2
b = 2

# Calculate tail probability


# by adding each tail
norm.cdf(a) + norm.sf(b)

0.04550026389635839

FOUNDATIONS OF PROBABILITY IN PYTHON


Intervals

# Create our variable


alpha = 0.95

# Calculate the interval


norm.interval(alpha)

(-1.959963984540054, 1.959963984540054)

FOUNDATIONS OF PROBABILITY IN PYTHON


On to some
practice!
F O U N D AT I O N S O F P R O B A B I L I T Y I N P Y T H O N
Poisson distributions
F O U N D AT I O N S O F P R O B A B I L I T Y I N P Y T H O N

Alexander A. Ramírez M.
CEO @ Synergy Vision
Poisson modeling

FOUNDATIONS OF PROBABILITY IN PYTHON


Poisson distribution properties

FOUNDATIONS OF PROBABILITY IN PYTHON


Probability mass function (pmf)

Imagine you have 2.2 calls per minute.

FOUNDATIONS OF PROBABILITY IN PYTHON


Probability mass function (pmf) (Cont.)
In Python we do the following:

# Import poisson
from scipy.stats import poisson

# Calculate the probability mass


# with pmf
poisson.pmf(k=3, mu=2.2)

0.19663867170702193

mu parameter speci es the mean of


successful events.

FOUNDATIONS OF PROBABILITY IN PYTHON


pmf examples

# Calculate pmf of 0 # Calculate pmf of 6


poisson.pmf(k=0, mu=2.2) poisson.pmf(k=6, mu=2.2)

0.11080315836233387 0.01744840480280308

FOUNDATIONS OF PROBABILITY IN PYTHON


Different means

FOUNDATIONS OF PROBABILITY IN PYTHON


Cumulative distribution function (cdf)

# Calculate cdf of 2 # Calculate cdf of 5


poisson.cdf(k=2, mu=2.2) poisson.cdf(k=5, mu=2.2)

0.6227137499963162 0.9750902496952996

FOUNDATIONS OF PROBABILITY IN PYTHON


Survival function and percent point function (ppf)

# Calculate sf of 2 # Calculate ppf of 0.5


poisson.sf(k=2, mu=2.2) poisson.ppf(q=0.5, mu=2.2)

0.3772862500036838 2.0

FOUNDATIONS OF PROBABILITY IN PYTHON


Sample generation (rvs)
# Import poisson, matplotlib.pyplot, and seaborn
from scipy.stats import poisson
import matplotlib.pyplot as plt
import seaborn as sns

# Create the sample using poisson.rvs()


sample = poisson.rvs(mu=2.2, size=10000, random_state=13)

# Plot the sample


sns.distplot(sample, kde=False)
plt.show()

FOUNDATIONS OF PROBABILITY IN PYTHON


Sample generation (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


Let's practice with
Poisson
F O U N D AT I O N S O F P R O B A B I L I T Y I N P Y T H O N
Geometric
distributions
F O U N D AT I O N S O F P R O B A B I L I T Y I N P Y T H O N

Alexander A. Ramírez M.
CEO @ Synergy Vision
Geometric modeling

FOUNDATIONS OF PROBABILITY IN PYTHON


Geometric parameter

Model for a basketball player with probability We can model a grizzly bear that has a 0.033
0.3 of scoring. probability of catching a salmon.

FOUNDATIONS OF PROBABILITY IN PYTHON


Probability mass function (pmf)
In Python we code this as follows:

# Import geom
from scipy.stats import geom

# Calculate the probability mass


# with pmf
geom.pmf(k=30, p=0.0333)

0.02455102908739612

p parameter speci es probability of success.

FOUNDATIONS OF PROBABILITY IN PYTHON


Cumulative distribution function (cdf)

# Calculate cdf of 4
geom.cdf(k=4, p=0.3)

0.7598999999999999

FOUNDATIONS OF PROBABILITY IN PYTHON


Survival function (sf)

# Calculate sf of 2
geom.sf(k=2, p=0.3)

0.49000000000000005

FOUNDATIONS OF PROBABILITY IN PYTHON


Percent point function (ppf)

# Calculate ppf of 0.6


geom.ppf(q=0.6, p=0.3)

3.0

FOUNDATIONS OF PROBABILITY IN PYTHON


Sample generation (rvs)
# Import poisson, matplotlib.pyplot, and seaborn
from scipy.stats import geom
import matplotlib.pyplot as plt
import seaborn as sns

# Create the sample using geom.rvs()


sample = geom.rvs(p=0.3, size=10000, random_state=13)

# Plot the sample


sns.distplot(sample, bins = np.linspace(0,20,21), kde=False)
plt.show()

FOUNDATIONS OF PROBABILITY IN PYTHON


Sample generation (rvs) (Cont.)

FOUNDATIONS OF PROBABILITY IN PYTHON


Let's go try until we
succeed!
F O U N D AT I O N S O F P R O B A B I L I T Y I N P Y T H O N

You might also like