Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Implementing Probability Distributions to Python | Probability & Statistics
Mathematics for Data Science

bookImplementing Probability Distributions to Python

In this lesson, we're exploring how three common probability distributions — Binomial, Uniform, and Normal — can be applied to quality control in manufacturing.
Wel'l walk through Python code that calculates probabilities and creates visualizations for each distribution. The goal is to understand both the statistical concepts and how the code works step-by-step.

Binomial Distribution

The Binomial distribution models the probability of getting exactly kk successes in nn independent trials, each with probability pp of success.

123456789101112131415161718
from scipy.stats import binom import matplotlib.pyplot as plt # number of trials n = 100 # probability of success p = 0.02 # number of successes k = 3 binom_prob = binom.pmf(k, n, p) print("Binomial probability:", binom_prob) # Vizualization x_vals = range(0, 15) y_vals = binom.pmf(x_vals, n, p) plt.bar(x_vals, y_vals, color='skyblue') plt.show()
copy
  • n = 100 - we're testing 100 rods;
  • p = 0.02 - 2% chance a rod is defective;
  • k = 3 - probability of exactly 3 defectives;
  • binom.pmf() computes the probability mass function.

Uniform Distribution

The Uniform distribution models a continuous variable where all values between $a$ and $b$ are equally likely.

12345678910111213141516
from scipy.stats import uniform import numpy as np a = 49.5 b = 50.5 low, high = 49.8, 50.2 uniform_prob = uniform.cdf(high, a, b - a) - uniform.cdf(low, a, b - a) print("Uniform probability:", uniform_prob) # Vizualization x = np.linspace(a, b, 100) pdf = uniform.pdf(x, a, b - a) plt.plot(x, pdf, color='black') plt.fill_between(x, pdf, where=(x >= low) & (x <= high), color='lightgreen', alpha=0.5) plt.show()
copy
  • a, b - total range of rod lengths;
  • low, high - interval of interest;
  • Subtracting CDF values gives probability inside interval.

Normal Distribution

The Normal distribution describes values clustering around a mean $\mu$ with spread measured by standard deviation $\sigma$.

12345678910111213141516171819
from scipy.stats import norm mu = 200 sigma = 5 lower, upper = 195, 205 norm_prob = norm.cdf(upper, mu, sigma) - norm.cdf(lower, mu, sigma) print("Normal probability:", norm_prob) z1 = (lower - mu) / sigma z2 = (upper - mu) / sigma print("Z-scores:", z1, z2) # Vizualization x = np.linspace(mu - 4*sigma, mu + 4*sigma, 200) pdf = norm.pdf(x, mu, sigma) plt.plot(x, pdf, color='black') plt.fill_between(x, pdf, where=(x >= lower) & (x <= upper), color='plum', alpha=0.5) plt.show()
copy
  • mu - mean rod weight;
  • sigma - standard deviation;
  • Probability - CDF difference;
  • Z-scores show how far bounds are from mean.

Real-World Application

  • Binomial - how likely is a certain number of defective rods?
  • Uniform - are rod lengths within tolerance?
  • Normal - are rod weights within expected variability?

By combining these, quality control targets defects, ensures precision, and maintains product consistency.


Quiz

**1.

**2.


**3.


4. If uniform range widened from (49.5,50.5) to (49,51), what happens to P(49.8≤X≤50.2)? A) Increases B) Decreases ✅ C) Stays the same D) Becomes 1


5. In the normal code, why compute Z-scores first? A) Normalize values for comparison with standard normal ✅ B) Make plot symmetric C) Determine skewness D) Adjust mean


6. If σ reduced from 5 to 2 in normal example, what happens to P(195≤X≤205)? A) Decreases B) Increases ✅ C) Stays same D) Doubles


7. In binomial visualization, if p increases from 0.02 to 0.10 (n=100)? A) Peak shifts right ✅ B) Peak stays at k=3 but taller C) Flat distribution D) Equal bars


**8.


9. To model defective rods and rod weights together, which pair fits best? A) Binomial and Normal ✅ B) Uniform and Normal C) Binomial and Uniform D) Normal and Uniform

1. In the binomial code cell, what does n = 100 represent?

2. Which function calculates the probability of exactly k defective rods?

3. In the uniform code, what does

uniform.cdf(high, a, b-a) - uniform.cdf(low, a, b-a) compute?

question mark

In the binomial code cell, what does n = 100 represent?

Select the correct answer

question mark

Which function calculates the probability of exactly k defective rods?

Select the correct answer

question mark

In the uniform code, what does uniform.cdf(high, a, b-a) - uniform.cdf(low, a, b-a) compute?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 11

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 1.89

bookImplementing Probability Distributions to Python

Swipe um das Menü anzuzeigen

In this lesson, we're exploring how three common probability distributions — Binomial, Uniform, and Normal — can be applied to quality control in manufacturing.
Wel'l walk through Python code that calculates probabilities and creates visualizations for each distribution. The goal is to understand both the statistical concepts and how the code works step-by-step.

Binomial Distribution

The Binomial distribution models the probability of getting exactly kk successes in nn independent trials, each with probability pp of success.

123456789101112131415161718
from scipy.stats import binom import matplotlib.pyplot as plt # number of trials n = 100 # probability of success p = 0.02 # number of successes k = 3 binom_prob = binom.pmf(k, n, p) print("Binomial probability:", binom_prob) # Vizualization x_vals = range(0, 15) y_vals = binom.pmf(x_vals, n, p) plt.bar(x_vals, y_vals, color='skyblue') plt.show()
copy
  • n = 100 - we're testing 100 rods;
  • p = 0.02 - 2% chance a rod is defective;
  • k = 3 - probability of exactly 3 defectives;
  • binom.pmf() computes the probability mass function.

Uniform Distribution

The Uniform distribution models a continuous variable where all values between $a$ and $b$ are equally likely.

12345678910111213141516
from scipy.stats import uniform import numpy as np a = 49.5 b = 50.5 low, high = 49.8, 50.2 uniform_prob = uniform.cdf(high, a, b - a) - uniform.cdf(low, a, b - a) print("Uniform probability:", uniform_prob) # Vizualization x = np.linspace(a, b, 100) pdf = uniform.pdf(x, a, b - a) plt.plot(x, pdf, color='black') plt.fill_between(x, pdf, where=(x >= low) & (x <= high), color='lightgreen', alpha=0.5) plt.show()
copy
  • a, b - total range of rod lengths;
  • low, high - interval of interest;
  • Subtracting CDF values gives probability inside interval.

Normal Distribution

The Normal distribution describes values clustering around a mean $\mu$ with spread measured by standard deviation $\sigma$.

12345678910111213141516171819
from scipy.stats import norm mu = 200 sigma = 5 lower, upper = 195, 205 norm_prob = norm.cdf(upper, mu, sigma) - norm.cdf(lower, mu, sigma) print("Normal probability:", norm_prob) z1 = (lower - mu) / sigma z2 = (upper - mu) / sigma print("Z-scores:", z1, z2) # Vizualization x = np.linspace(mu - 4*sigma, mu + 4*sigma, 200) pdf = norm.pdf(x, mu, sigma) plt.plot(x, pdf, color='black') plt.fill_between(x, pdf, where=(x >= lower) & (x <= upper), color='plum', alpha=0.5) plt.show()
copy
  • mu - mean rod weight;
  • sigma - standard deviation;
  • Probability - CDF difference;
  • Z-scores show how far bounds are from mean.

Real-World Application

  • Binomial - how likely is a certain number of defective rods?
  • Uniform - are rod lengths within tolerance?
  • Normal - are rod weights within expected variability?

By combining these, quality control targets defects, ensures precision, and maintains product consistency.


Quiz

**1.

**2.


**3.


4. If uniform range widened from (49.5,50.5) to (49,51), what happens to P(49.8≤X≤50.2)? A) Increases B) Decreases ✅ C) Stays the same D) Becomes 1


5. In the normal code, why compute Z-scores first? A) Normalize values for comparison with standard normal ✅ B) Make plot symmetric C) Determine skewness D) Adjust mean


6. If σ reduced from 5 to 2 in normal example, what happens to P(195≤X≤205)? A) Decreases B) Increases ✅ C) Stays same D) Doubles


7. In binomial visualization, if p increases from 0.02 to 0.10 (n=100)? A) Peak shifts right ✅ B) Peak stays at k=3 but taller C) Flat distribution D) Equal bars


**8.


9. To model defective rods and rod weights together, which pair fits best? A) Binomial and Normal ✅ B) Uniform and Normal C) Binomial and Uniform D) Normal and Uniform

1. In the binomial code cell, what does n = 100 represent?

2. Which function calculates the probability of exactly k defective rods?

3. In the uniform code, what does

uniform.cdf(high, a, b-a) - uniform.cdf(low, a, b-a) compute?

question mark

In the binomial code cell, what does n = 100 represent?

Select the correct answer

question mark

Which function calculates the probability of exactly k defective rods?

Select the correct answer

question mark

In the uniform code, what does uniform.cdf(high, a, b-a) - uniform.cdf(low, a, b-a) compute?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 5. Kapitel 11
some-alt