The uniform distribution, in its standard form, is evenly spread across the interval [0, 1]. By adjusting parameters loc
and scale
, this distribution can be extended to any range [loc
, loc + scale
]. As a subclass of rv_continuous
within the SciPy library, the uniform
object inherits numerous general methods, enhanced with specific functionalities tailored to this distribution.
Let’s explore the uniform distribution with practical examples.
>>> import numpy as np
>>> from scipy.stats import uniform
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(1, 1)
To begin, we can calculate key statistical moments—mean, variance, skew, and kurtosis—which provide insights into the shape and central tendency of the distribution.
>>> mean, var, skew, kurt = uniform.stats(moments='mvsk')
The probability density function (PDF) is crucial for understanding the likelihood of different values within the distribution. Below, we visualize the PDF using Matplotlib.
>>> x = np.linspace(uniform.ppf(0.01),
... uniform.ppf(0.99), 100)
>>> ax.plot(x, uniform.pdf(x),
... 'r-', lw=5, alpha=0.6, label='uniform pdf')
Alternatively, we can ‘freeze’ the distribution with fixed parameters. This creates a static distribution object, useful for repeated evaluations with the same parameters.
>>> rv = uniform()
>>> ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
To ensure the accuracy of the cumulative distribution function (CDF) and the percent point function (PPF), we can perform a check. The CDF gives the probability that a random variable from the distribution is less than or equal to a given value, while the PPF is its inverse.
>>> vals = uniform.ppf([0.001, 0.5, 0.999])
>>> np.allclose([0.001, 0.5, 0.999], uniform.cdf(vals))
True
Generating random numbers from a uniform distribution is straightforward and essential for simulations and statistical modeling.
>>> r = uniform.rvs(size=1000)
Finally, we can visually compare the generated random numbers with the theoretical distribution using a histogram, providing a visual check of our implementation.
>>> ax.hist(r, density=True, bins='auto', histtype='stepfilled', alpha=0.2)
>>> ax.set_xlim([x[0], x[-1]])
>>> ax.legend(loc='best', frameon=False)
>>> plt.show()
The uniform distribution, characterized by its constant probability density function across a defined interval, is a foundational concept in statistics and probability theory. Understanding its PDF, along with related functions like CDF and PPF, is crucial for both theoretical studies and practical applications. The methods available through SciPy, as outlined below, provide a comprehensive toolkit for working with uniform distributions in Python.
Methods | Description |
---|---|
rvs(loc=0, scale=1, size=1, random_state=None) | Generate random variates from the uniform distribution. |
pdf(x, loc=0, scale=1) | Calculate the Probability Density Function (PDF) at given points. |
logpdf(x, loc=0, scale=1) | Compute the logarithm of the PDF. |
cdf(x, loc=0, scale=1) | Determine the Cumulative Distribution Function (CDF) values. |
logcdf(x, loc=0, scale=1) | Calculate the logarithm of the CDF. |
sf(x, loc=0, scale=1) | Compute the Survival Function (1 – CDF). |
logsf(x, loc=0, scale=1) | Calculate the logarithm of the Survival Function. |
ppf(q, loc=0, scale=1) | Find the Percent Point Function (inverse of CDF). |
isf(q, loc=0, scale=1) | Determine the Inverse Survival Function (inverse of SF). |
moment(order, loc=0, scale=1) | Calculate non-central moments of the distribution. |
stats(loc=0, scale=1, moments=’mv’) | Get mean(‘m’), variance(‘v’), skew(‘s’), and/or kurtosis(‘k’). |
entropy(loc=0, scale=1) | Compute the differential entropy of the distribution. |
fit(data) | Estimate parameters from data. |
expect(func, args=(), loc=0, scale=1, lb=None, ub=None, conditional=False, **kwds) | Calculate the expected value of a function with respect to the distribution. |
median(loc=0, scale=1) | Find the median of the distribution. |
mean(loc=0, scale=1) | Calculate the mean of the distribution. |
var(loc=0, scale=1) | Determine the variance of the distribution. |
std(loc=0, scale=1) | Calculate the standard deviation. |
interval(confidence, loc=0, scale=1) | Compute the confidence interval around the median. |