The uniform distribution#
In Example: Uncertainties in steel ball manufacturing, we had a random variable for which all values between a given interval were equally probable. This is a very common situation covered by the so-called uniform distribution.
Let’s start with the simplest case: a random variable taking values between 0 and 1 with constant probability density. We write:
and we read
follows a uniform distribution taking values in .
The probability density of the uniform is constant in
What should the constant
So, the PDF is:
To find the CDF, we can use PDF Property 3:
$
Using this result, we can find the probability that
Instantiating the uniform using scipy.stats
#
Let me know show you how you can make a uniform random variable using scipy:
Show code cell source
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set(rc={"figure.dpi":100, 'savefig.dpi':300})
sns.set_context('notebook')
sns.set_style("ticks")
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina', 'svg')
import numpy as np
import scipy.stats as st
X = st.uniform()
Here is how you can get some samples:
X.rvs(size=10)
array([0.33842053, 0.57010738, 0.1323871 , 0.58357996, 0.68875828,
0.67923023, 0.84656861, 0.75593012, 0.25791332, 0.13214875])
You can evalute the PDF at any point like this:
X.pdf(0.5)
1.0
X.pdf(-0.1)
0.0
X.pdf(1.5)
0.0
Let’s plot the PDF:
fig, ax = plt.subplots()
xs = np.linspace(-0.1, 1.1, 200)
ax.plot(xs, X.pdf(xs))
ax.set_xlabel('$x$')
ax.set_ylabel('$p(x)$');
You can evaluate the CDF like this:
X.cdf(-0.5)
0.0
X.cdf(0.5)
0.5
X.cdf(1.2)
1.0
Let’s plot the CDF:
fig, ax = plt.subplots()
ax.plot(xs, X.cdf(xs))
ax.set_xlabel('$x$')
ax.set_ylabel('$F(x)$');
Finally, let’s find the probability that scipy
functionality:
a = -1.0
b = 0.3
prob_X_is_in_ab = X.cdf(b) - X.cdf(a)
print('p({0:1.2f} <= X <= {1:1.2f}) = {2:1.2f}'.format(a, b, prob_X_is_in_ab))
p(-1.00 <= X <= 0.30) = 0.30
The uniform distribution over an arbitrary interval #
The uniform distribution can also be defined over an arbitrary interval
follows a uniform distribution on .
The PDF of this random variable is:
where
This gives:
From this we get:
and we can now write:
From the PDF, we can now find the CDF for
Instantiating the generic uniform using scipy.stats
:#
Let’s instantiate using scipy.stats
:
a = -2.0
b = 5.0
X = st.uniform(loc=a, scale=(b-a))
Some samples:
X.rvs(size=10)
array([-1.63839392, -1.08615066, -1.9249188 , -0.86574048, -0.01377033,
0.91441 , 2.94486672, -0.90602192, -0.44926847, 4.28172985])
The PDF:
fig, ax = plt.subplots()
xs = np.linspace(a - 0.2, b + 0.2, 200)
ax.plot(xs, X.pdf(xs))
ax.set_xlabel('$x$')
ax.set_ylabel('$p(x)$');
The CDF:
fig, ax = plt.subplots()
ax.plot(xs, X.cdf(xs))
ax.set_xlabel('$x$')
ax.set_ylabel('$F(x)$');
Questions#
Repeat the code above so that the random variable is
.