From 68a2219c6830f8af7026853854ff6338d3ffbdbf Mon Sep 17 00:00:00 2001 From: Rithish S Date: Mon, 20 May 2024 21:51:47 +0530 Subject: [PATCH 1/3] binomial distribution added --- .../machine-learning/binomial_distribution.md | 123 ++++++++++++++++++ contrib/machine-learning/index.md | 1 + 2 files changed, 124 insertions(+) create mode 100644 contrib/machine-learning/binomial_distribution.md diff --git a/contrib/machine-learning/binomial_distribution.md b/contrib/machine-learning/binomial_distribution.md new file mode 100644 index 0000000..2fddc31 --- /dev/null +++ b/contrib/machine-learning/binomial_distribution.md @@ -0,0 +1,123 @@ +# Binomial Distribution + +## Introduction + +The binomial distribution is a discrete probability distribution that describes the number of successes in a fixed number of independent Bernoulli trials, each with the same probability of success. It is commonly used in statistics and probability theory. + +### Key Characteristics + +- **Number of trials (n):** The number of independent experiments or trials. +- **Probability of success (p):** The probability of success on an individual trial. +- **Number of successes (k):** The number of successful outcomes in n trials. + +The binomial distribution is defined by the probability mass function (PMF): + +\[ P(X = k) = \binom{n}{k} p^k (1 - p)^{n - k} \] + +where: +- \(\binom{n}{k}\) is the binomial coefficient, calculated as \(\frac{n!}{k!(n-k)!}\). + +## Properties of Binomial Distribution + +- **Mean:** \( \mu = np \) +- **Variance:** \( \sigma^2 = np(1 - p) \) +- **Standard Deviation:** \( \sigma = \sqrt{np(1 - p)} \) + +## Python Implementation + +Let's implement the binomial distribution using Python. We'll use the `scipy.stats` library to compute the binomial PMF and CDF, and `matplotlib` to visualize it. + +### Step-by-Step Implementation + +1. **Import necessary libraries:** + + ```python + import numpy as np + import matplotlib.pyplot as plt + from scipy.stats import binom + ``` + +2. **Define parameters:** + + ```python + # Number of trials + n = 10 + # Probability of success + p = 0.5 + # Number of successes + k = np.arange(0, n + 1) + ``` + +3. **Compute the PMF:** + + ```python + pmf = binom.pmf(k, n, p) + ``` + +4. **Plot the PMF:** + + ```python + plt.bar(k, pmf, color='blue') + plt.xlabel('Number of Successes') + plt.ylabel('Probability') + plt.title('Binomial Distribution PMF') + plt.show() + ``` + +5. **Compute the CDF:** + + ```python + cdf = binom.cdf(k, n, p) + ``` + +6. **Plot the CDF:** + + ```python + plt.plot(k, cdf, marker='o', linestyle='--', color='blue') + plt.xlabel('Number of Successes') + plt.ylabel('Cumulative Probability') + plt.title('Binomial Distribution CDF') + plt.grid(True) + plt.show() + ``` + +### Complete Code + +Here is the complete code for the binomial distribution implementation: + +```python +import numpy as np +import matplotlib.pyplot as plt +from scipy.stats import binom + +# Parameters +n = 10 # Number of trials +p = 0.5 # Probability of success + +# Number of successes +k = np.arange(0, n + 1) + +# Compute PMF +pmf = binom.pmf(k, n, p) + +# Plot PMF +plt.figure(figsize=(12, 6)) +plt.subplot(1, 2, 1) +plt.bar(k, pmf, color='blue') +plt.xlabel('Number of Successes') +plt.ylabel('Probability') +plt.title('Binomial Distribution PMF') + +# Compute CDF +cdf = binom.cdf(k, n, p) + +# Plot CDF +plt.subplot(1, 2, 2) +plt.plot(k, cdf, marker='o', linestyle='--', color='blue') +plt.xlabel('Number of Successes') +plt.ylabel('Cumulative Probability') +plt.title('Binomial Distribution CDF') +plt.grid(True) + +plt.tight_layout() +plt.show() diff --git a/contrib/machine-learning/index.md b/contrib/machine-learning/index.md index 82596a2..ec4dfc3 100644 --- a/contrib/machine-learning/index.md +++ b/contrib/machine-learning/index.md @@ -1,3 +1,4 @@ # List of sections - [Section title](filename.md) +- [Binomial Distribution](binomial_distribution.md) From 3f731e7f211facfc725a08d455c90dc6c27b8df6 Mon Sep 17 00:00:00 2001 From: Rithish S <123345316+Rithish5513U@users.noreply.github.com> Date: Mon, 20 May 2024 21:53:31 +0530 Subject: [PATCH 2/3] Update binomial_distribution.md --- contrib/machine-learning/binomial_distribution.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contrib/machine-learning/binomial_distribution.md b/contrib/machine-learning/binomial_distribution.md index 2fddc31..94e4c41 100644 --- a/contrib/machine-learning/binomial_distribution.md +++ b/contrib/machine-learning/binomial_distribution.md @@ -1,3 +1,7 @@ + + # Binomial Distribution ## Introduction From 7d800d598046394c7409eab176750f0468198e5f Mon Sep 17 00:00:00 2001 From: Rithish S <123345316+Rithish5513U@users.noreply.github.com> Date: Mon, 20 May 2024 21:57:32 +0530 Subject: [PATCH 3/3] Update binomial_distribution.md --- contrib/machine-learning/binomial_distribution.md | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/contrib/machine-learning/binomial_distribution.md b/contrib/machine-learning/binomial_distribution.md index 94e4c41..0d1d328 100644 --- a/contrib/machine-learning/binomial_distribution.md +++ b/contrib/machine-learning/binomial_distribution.md @@ -1,7 +1,3 @@ - - # Binomial Distribution ## Introduction @@ -16,16 +12,16 @@ The binomial distribution is a discrete probability distribution that describes The binomial distribution is defined by the probability mass function (PMF): -\[ P(X = k) = \binom{n}{k} p^k (1 - p)^{n - k} \] +P(X = k) = (n choose k) p^k (1 - p)^(n - k) where: -- \(\binom{n}{k}\) is the binomial coefficient, calculated as \(\frac{n!}{k!(n-k)!}\). +- (n choose k) is the binomial coefficient, calculated as n! / (k!(n-k)!). ## Properties of Binomial Distribution -- **Mean:** \( \mu = np \) -- **Variance:** \( \sigma^2 = np(1 - p) \) -- **Standard Deviation:** \( \sigma = \sqrt{np(1 - p)} \) +- **Mean:** μ = np +- **Variance:** σ² = np(1 - p) +- **Standard Deviation:** σ = √(np(1 - p)) ## Python Implementation