Probability theory is a fundamental concept in mathematicsand statistics that deals with the likelihood of events occurring. In the realmof data scienceand machine learning, understanding various types of probability is crucial formaking informed decisions and predictions. Python, with its extensive librarieslike NumPy, SciPy, and Stats Models, provides powerful tools for probabilitycomputation and analysis. In this article, we will delve into different typesof probability and explore how they are utilized in Python.
Classical probability, also known as a priori probability,is based on the assumption that all outcomes in a sample space are equallylikely. It is often used in situations where each outcome is equally probable,such as rolling a fair six-sided die or flipping a fair coin. In Python,classical probability can be computed using simple arithmetic operations.
```python
# Example of classical probability
total_outcomes = 6 #Total outcomes of rolling a fair six-sided die
desired_outcomes = 2 # Desired outcomes (e.g., rolling a 1 or a 6)
classical_prob = desired_outcomes / total_outcomes
print("Classical Probability:", classical_prob)
```
Empirical probability, also known as experimentalprobability, is based on observed data from real-world experiments orobservations. It involves calculating the probability of an event by conductingexperiments and recording the outcomes. In Python, empirical probability can becomputed using data collected from experiments.
```python
# Example of empirical probability
import numpy as np
# Simulating rolling a fair six-sided die 100 times
rolls = np.random.randint(1, 7, size=100)
desired_outcomes = np.sum((rolls == 1) | (rolls == 6))
empirical_prob = desired_outcomes / 100
print("Empirical Probability:", empirical_prob)
```
Conditional probability measures the likelihood of an eventoccurring given that another event has already occurred. It is expressed asP(A|B), the probability of event A occurring given that event B has alreadyoccurred. In Python, conditional probability can be computed using conditionalstatements and probability rules.
```python
# Example of conditional probability
# P(A|B) = P(A and B) / P(B)
# Suppose we have a deck of cards and we draw a card.
# What is the probability of drawing a King given that wehave drawn a face card?
from collections import Counter
deck = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J','Q', 'K', 'A']
face_cards = ['J', 'Q', 'K']
# Drawing a face card
face_card_drawn = Counter(np.random.choice(deck,size=100))['K'] # Let's simulate 100draws
# Drawing a King
king_drawn = Counter(np.random.choice(deck, size=100))['K']
conditional_prob = king_drawn / face_card_drawn
print("Conditional Probability of drawing a King givena face card:", conditional_prob)
```
Bayesian probability is a statistical method that uses priorknowledge and evidence to update beliefs about the likelihood of events. It isbased on Bayes' theorem and is widely used in machine learning for tasks likeBayesian inference and probabilistic modeling. Python libraries like PyMC3 andStan provide powerful tools for Bayesian analysis.
```python
# Example of Bayesian probability using PyMC3
import pymc3 as pm
# Simulating coin tosses
data = np.random.binomial(n=1, p=0.6, size=100)
# Prior belief about the fairness of the coin
with pm.Model() as model:
# Priordistribution
p = pm.Beta('p',alpha=2, beta=2)
# Likelihood
likelihood =pm.Bernoulli('likelihood', p=p, observed=data)
# Posteriordistribution
trace =pm.sample(1000, tune=1000)
# Plotting posterior distribution
pm.plot_posterior(trace)
```