Statement of Completion#e8fa87ee
Basic concepts of Probability
medium
Normal distribution
Resolution
Activities
Project.ipynb
Section 1¶
In [ ]:
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
%matplotlib inline
Normal Distribution¶
Let's simulate an example:
In [ ]:
# A simplistic way to do it
x = np.linspace(-10,10,100)
fig, ax = plt.subplots()
nd = norm.pdf(x) # get the norm.pdf for x interval
ax.plot(x,nd,label='stdv=0.05' )
ax.set_xlabel('x')
ax.set_ylabel('pdf(x)')
ax.set_title('Normal Distribution')
ax.legend(loc='best', frameon=True)
ax.grid(True)
Let’s plot the probability distribution functions of a normal distribution where the mean has different standard deviations.
In [ ]:
fig, ax = plt.subplots()
x = np.linspace(-10,10,100)
stdvs = [1.0, 2.0, 3.0, 4.0]
for s in stdvs:
ax.plot(x, norm.pdf(x,scale=s), label='stdv=%.1f' % s)
ax.set_xlabel('x')
ax.set_ylabel('pdf(x)')
ax.set_title('Normal Distribution')
ax.legend(loc='best', frameon=True)
ax.set_ylim(0,0.45)
ax.grid(True)
If $X$ is a normal random variable with parameters $\mu$ = 8 and $\sigma$ = 3.
We illustrate the normal distribution and the required area in the Figure below. The area under the curve for the interval
$(\mu-\sigma,\mu+\sigma )$ is shaded.
In [ ]:
x_min = 0.0
x_max = 16.0
mean = 8.0
std = 3.0
x = np.linspace(x_min, x_max, 100)
y = norm.pdf(x,mean,std)
plt.plot(x,y, color='black')
#----------------------------------------------------------------------------------------#
# fill area 1
pt1 = mean + std
plt.plot([pt1 ,pt1 ],[0.0,norm.pdf(pt1 ,mean, std)], color='black')
pt2 = mean - std
plt.plot([pt2 ,pt2 ],[0.0,norm.pdf(pt2 ,mean, std)], color='black')
ptx = np.linspace(pt1, pt2, 10)
pty = norm.pdf(ptx,mean,std)
plt.fill_between(ptx, pty, color='#0b559f', alpha=0.5)
Activity 1: Let equal the IQ of a randomly selected Datawars student. Assume $X$ ~ $N(100, 16^2)$ What is the probability that a randomly selected student has an IQ below 90?¶
In [2]:
from scipy.stats import norm
# Mean (mu) and standard deviation (sigma)
mu = 100
sigma = 16
# Calculate the probability of having an IQ less than 90
probability = norm.cdf(x=90, loc=mu, scale=sigma)
print(f"The probability that a randomly selected student has an IQ below 90 is {probability:.4f}")
The probability that a randomly selected student has an IQ below 90 is 0.2660
Activity 2: Assume that Z has a standard normal distribution. Determine $P(Z > 1.26)$¶
In [4]:
P_b= 1-norm.cdf(-0.86)
P_b
Out[4]:
0.8051054787481916
Activity 3: Assume that Z has a standard normal distribution. Determine $P(Z > -0.86)$.¶
In [ ]:
Let's calculate the Z-SCORE for an example.¶
In [ ]:
mu, sigma = 10, 0.05
x = np.random.normal(mu, sigma, 100000)
# Calculate stats using single sample
sample_mean = np.mean(x)
sample_stdev = np.var(x)**0.5
# Calculate standard error
std_error = sample_stdev/(x.shape[0])**0.5
# Infer distribution using single sample
z = (x-sample_mean) / std_error
# Plot histogram of inferred distribution
group_labels = ['The Standard Normal Distribution']
colors = ['magenta']
fig, ax = plt.subplots()
# get the norm.pdf for x interval
ax.hist(z)
ax.set_xlabel('z')
ax.set_ylabel('Frequency')
ax.set_title('Standard Normal Distribution')
ax.legend(loc='best', frameon=True)
ax.grid(True)
Activity 4: If X is a normal random variable with parameters $\mu$ = 3 and $\sigma^2$ = 9, find $P(2 <X < 5)$¶
In [6]:
P= P_b-P_a
P
Out[6]:
0.43566413856642794
In [ ]: