Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Show Uniform Discrete Distribution in Statistics using Python
In the field of statistics, there is a major role played by probability distributions in modeling and analyzing various random phenomena. Uniform Discrete Distribution is one of them. It is particularly used when dealing with discrete random variables which have equally likely outcomes. Ahead in this article, we will explore Uniform Discrete Distribution in the light of Python programming, using the scipy.stats.randint() function.
SciPy is a powerful Python library for scientific analysis and calculations. The stats module provides tools for statistical analysis including probability distributions.
The randint() function in the scipy.stats module represents the uniform discrete variable, which inherits its functionalities from generic methods as an instance of the rv_discrete class. It allows handling of random discrete variables efficiently.
Parameters of randint() Function
The randint function takes in numerous parameters that control the behavior of the distribution. These parameters are
low (loc) It is the lower bound of the distribution, representing the smallest possible outcome. It is inclusive, i.e. it can be part of the distribution.
high (loc+scale) It is almost the opposite of low, the upper bound of the distribution representing the largest possible outcome. However it is exclusive, i.e. it is not a part of the distribution.
size (x) It is the no. of random variables to generate. Hence, determining the size of the output.
moments (mvsk) It is composed of 'm' = mean, 'v' = variance, 's' = Fisher's skew and 'k' = Fisher's kurtosis. Taking a default of mv it defines the statistical moments to calculate.
Creating a Uniform Discrete Random Variable
It is a simple implementation similar to other library modules. We need to import the scipy library first. Let us create a random variable that represents the six faces of a dice ?
Example
import scipy.stats as stats
uniformDiscreteVariable = stats.randint(low=1, high=7)
# Created a uniform discrete variable that resembles six outcomes of a dice
print("UDR: \n", uniformDiscreteVariable)
UDR: <scipy.stats._distn_infrastructure.rv_discrete_frozen object at 0x0000023787094040>
So we have created a uniform discrete integer variable with a lower bound of 1 (inclusive) and upper bound of 7 (exclusive), hence the range of possible outcomes is from 1 to 6.
Generating Uniform Discrete Values and Probability Distribution
Already we have defined our uniform discrete variables, now let us see how we can generate random values or variates from the same. Also let us generate the probability distribution.
The rvs(low, high, loc=0, size=1, random_state=None) method helps us generate random variates from our defined uniform discrete variable. For the probability distribution we will use the pmf(k, low, high, loc=0) method which gives the probability mass function, i.e. probability of each outcome of a random event. Below we have tried to print the generated uniform discrete variates and probability distribution ?
Example
import scipy.stats as stats
uniformDiscreteVariable = stats.randint(low=1, high=7)
# Created a uniform discrete variable that resembles six outcomes of a dice
res = uniformDiscreteVariable.rvs(size=5)
# Generating 5 random outputs i.e. dice rolls
probabDistrib = uniformDiscreteVariable.pmf(res)
print("UDR variates: ", res)
print("Probability Distribution: ", probabDistrib)
UDR variates: [2 4 2 4 5] Probability Distribution: [0.16666667 0.16666667 0.16666667 0.16666667 0.16666667]
Here, we defined a uniform discrete random variable that fairly represents a dice of six outcomes. Using the rvs function we have generated 5 random outputs from that same variable, i.e. the representation of outcome in 5 dice rolls. The probability function shows each number in dice has equal probability (1/6).
Visualizing Uniform Discrete Distribution
Now we have our random variates as well as the probability distribution, we can easily plot them in a graphical representation for a better visualization and understanding. In Python, matplotlib.pyplot helps us in graphical representation of the data ?
Example
import scipy.stats as stats
import matplotlib.pyplot as plt
import numpy as np
# Create uniform discrete variable for a dice
uniformDiscreteVariable = stats.randint(low=1, high=7)
# All possible outcomes (1 to 6)
outcomes = np.arange(1, 7)
# Calculate probability for each outcome
probabilities = uniformDiscreteVariable.pmf(outcomes)
print("Outcomes: ", outcomes)
print("Probabilities: ", probabilities)
# Create bar plot for uniform discrete distribution
plt.figure(figsize=(8, 6))
plt.bar(outcomes, probabilities, width=0.8, alpha=0.7)
plt.xlabel('Dice Outcomes')
plt.ylabel('Probability')
plt.title('Uniform Discrete Distribution - Dice Roll')
plt.xticks(outcomes)
plt.ylim(0, 0.2)
plt.grid(axis='y', alpha=0.3)
plt.show()
Outcomes: [1 2 3 4 5 6] Probabilities: [0.16666667 0.16666667 0.16666667 0.16666667 0.16666667 0.16666667]
Comparing Different Uniform Distributions
We can easily adjust the low and high parameters to modify the uniform discrete distribution to fit various scenarios. Let us compare distributions for single dice and two dice rolls ?
Example
import scipy.stats as stats
import matplotlib.pyplot as plt
import numpy as np
# Single dice (1-6)
single_dice = stats.randint(low=1, high=7)
single_outcomes = np.arange(1, 7)
single_prob = single_dice.pmf(single_outcomes)
# Two dice sum (2-12)
two_dice = stats.randint(low=2, high=13)
two_outcomes = np.arange(2, 13)
two_prob = two_dice.pmf(two_outcomes)
print("Single Dice Outcomes: ", single_outcomes)
print("Single Dice Probabilities: ", single_prob)
print("Two Dice Sum Outcomes: ", two_outcomes)
print("Two Dice Sum Probabilities: ", two_prob)
# Create comparison plot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Single dice plot
ax1.bar(single_outcomes, single_prob, width=0.8, alpha=0.7, color='blue')
ax1.set_xlabel('Single Dice Outcomes')
ax1.set_ylabel('Probability')
ax1.set_title('Single Dice Distribution')
ax1.set_xticks(single_outcomes)
ax1.set_ylim(0, 0.2)
# Two dice plot
ax2.bar(two_outcomes, two_prob, width=0.8, alpha=0.7, color='red')
ax2.set_xlabel('Two Dice Sum Outcomes')
ax2.set_ylabel('Probability')
ax2.set_title('Two Dice Sum Distribution')
ax2.set_xticks(two_outcomes)
ax2.set_ylim(0, 0.12)
plt.tight_layout()
plt.show()
Single Dice Outcomes: [1 2 3 4 5 6] Single Dice Probabilities: [0.16666667 0.16666667 0.16666667 0.16666667 0.16666667 0.16666667] Two Dice Sum Outcomes: [ 2 3 4 5 6 7 8 9 10 11 12] Two Dice Sum Probabilities: [0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909 0.09090909]
Statistical Properties
The uniform discrete distribution has simple statistical properties that can be calculated using SciPy methods ?
Example
import scipy.stats as stats
# Create uniform discrete variable for dice
dice = stats.randint(low=1, high=7)
# Calculate statistical properties
mean = dice.mean()
variance = dice.var()
std_dev = dice.std()
print(f"Mean: {mean}")
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_dev}")
# Generate sample and calculate empirical statistics
sample = dice.rvs(size=1000, random_state=42)
print(f"Sample Mean: {sample.mean():.4f}")
print(f"Sample Std Dev: {sample.std():.4f}")
Mean: 3.5 Variance: 2.9166666666666665 Standard Deviation: 1.707825127659933 Sample Mean: 3.4970 Sample Std Dev: 1.7016
Conclusion
The uniform discrete distribution is a fundamental concept in statistics for modeling equally likely outcomes. Using Python's scipy.stats.randint() function, we can easily create, analyze, and visualize uniform discrete distributions for various applications like dice rolls, card draws, or any scenario with equal probability outcomes.
