Foundations of Probability in Python

Probability deals with the study of random events and their outcomes. It is an essential concept in various fields like finance, physics, engineering and data science. It is defined as the likelihood of an event occurring no event can be predicted with 100% certainty. In this article, we are going to explore the foundations of probability in Python using builtin libraries for statistical computations and random number generation.

The basic concepts and keywords of probability that are needed before we get started with Python are ?

  • Sample space A set of all possible outcomes of a random experiment.

  • Event A subset of the sample space representing a particular outcome or set of outcomes for a random experiment.

  • Probability A number between 0 and 1 that represents the likelihood of an event occurring. A probability of 0 indicates that an event is impossible, whereas a probability of 1 indicates that it is certain to occur.

Generating Random Numbers

Python's random module provides several functions like randint() and random() which help us generate different kinds of random values ?

import random

# Generate a random integer between 0 and 100
random_integer = random.randint(0, 100)
print("Random integer:", random_integer)

# Generate a random float between 0 and 1
random_float = random.random()
print("Random float:", random_float)
Random integer: 89
Random float: 0.16460963462567613

Defining Sample Space and Calculating Event Probability

The sample space is defined as a list of all possible outcomes, and an event is a subset of the sample space. The probability is calculated as the number of favorable outcomes divided by the total number of possible outcomes ?

# Define a sample space (rolling a 5-sided die)
sample_space = [1, 2, 3, 4, 5]

# Define an event (rolling 1, 2, or 3)
event = [1, 2, 3]

# Calculate the probability of the event
probability = len(event) / len(sample_space)
print("Probability of the event occurring:", probability)
Probability of the event occurring: 0.6

Computing Conditional Probability

Conditional probability calculates the probability of event A occurring, given that event B has already occurred. The formula is P(A|B) = P(A ? B) / P(B) ?

# Define a sample space
sample_space = [1, 2, 3, 4, 5]

# Define events A and B
event_A = [1, 2, 3]
event_B = [2, 3, 4]

# Calculate the intersection of A and B
intersection = [x for x in event_A if x in event_B]
print("Intersection of A and B:", intersection)

# Calculate probabilities
prob_intersection = len(intersection) / len(sample_space)
prob_B = len(event_B) / len(sample_space)

# Calculate conditional probability P(A|B)
conditional_probability = prob_intersection / prob_B
print("P(A|B):", conditional_probability)
Intersection of A and B: [2, 3]
P(A|B): 0.6666666666666666

Calculating Expected Value

Expected value represents the average outcome of a random variable over many trials. It's calculated as the sum of each outcome multiplied by its probability ?

# Define a probability distribution
probabilities = [0.2, 0.3, 0.5]
outcomes = [10, 20, 30]

# Calculate the expected value
expected_value = sum([p * x for p, x in zip(probabilities, outcomes)])
print("Expected value:", expected_value)

# Alternative calculation showing the formula
print("Calculation: 0.2×10 + 0.3×20 + 0.5×30 =", 0.2*10 + 0.3*20 + 0.5*30)
Expected value: 23.0
Calculation: 0.2×10 + 0.3×20 + 0.5×30 = 23.0

Simulating Die Roll Probability

Let's simulate rolling a fair sixsided die to estimate the probability of rolling a 6 ?

import random

def roll_die():
    return random.randint(1, 6)

num_trials = 10000
num_sixes = 0

for i in range(num_trials):
    result = roll_die()
    if result == 6:
        num_sixes += 1

prob_six = num_sixes / num_trials
theoretical_prob = 1/6

print(f"Simulated probability of rolling 6: {prob_six:.4f}")
print(f"Theoretical probability: {theoretical_prob:.4f}")
Simulated probability of rolling 6: 0.1667
Theoretical probability: 0.1667

Conditional Probability with Two Dice

Let's calculate the conditional probability of rolling a 6 on the first die, given that the sum of two dice equals 7 ?

import random

def roll_two_dice():
    die1 = random.randint(1, 6)
    die2 = random.randint(1, 6)
    return (die1, die2)

num_trials = 50000
num_six_given_seven = 0
num_seven = 0

for i in range(num_trials):
    result = roll_two_dice()
    if sum(result) == 7:
        num_seven += 1
        if result[0] == 6:
            num_six_given_seven += 1

if num_seven > 0:
    prob_six_given_seven = num_six_given_seven / num_seven
    print(f"Simulated P(first die = 6 | sum = 7): {prob_six_given_seven:.4f}")
    print(f"Theoretical probability: {1/6:.4f}")
else:
    print("No trials with sum = 7 occurred")
Simulated P(first die = 6 | sum = 7): 0.1667
Theoretical probability: 0.1667

Comparison of Probability Methods

Method Use Case Example
Basic Probability Single events Rolling a specific number
Conditional Probability Dependent events Weather forecasting
Expected Value Average outcomes Investment returns
Simulation Complex scenarios Monte Carlo methods

Conclusion

Python provides powerful tools for working with probability through the random module and mathematical calculations. These fundamentals form the basis for more advanced statistical analysis and machine learning applications. Understanding probability helps in making datadriven decisions and modeling uncertainty in realworld scenarios.

Updated on: 2026-03-27T14:48:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements