Probability Distributions

Lesson Introduction

Hello! Today, we'll explore Probability Distributions, a key concept in statistics and machine learning. By the end of this lesson, you'll know what probability distributions are, why they're essential, and how to work with them in Python.

Probability distributions help us understand how data behaves and the likelihood of different outcomes. We use them in everyday tasks like predicting weather, recommending movies, and much more. Let's dive in and see how they work!

Understanding Probability Distributions

A Probability Distribution describes how values of a random variable are distributed. It tells us the chances of different outcomes. Imagine rolling a six-sided die. Each number (1 to 6) has an equal chance of appearing. That’s an example of a probability distribution!

Probability distributions are crucial because:

  • They help us understand data behavior.
  • They allow us to make predictions and decisions.
  • They are used in many fields like finance, medicine, and machine learning.

Normal Distribution: part 1

The Normal Distribution (or Gaussian Distribution) is one of the most important probability distributions. Many natural phenomena follow this distribution, like heights, IQ scores, and measurement errors. The Normal Distribution is a bell-shaped curve symmetrical around its mean (average) value.

The Normal Distribution is defined by two parameters:

  • Mean (μ\mu): The average of all values in the distribution.
  • Standard Deviation (σ\sigma): Measures how spread out the values are from the mean.

For example, let's say we measure the heights of adult men in a town. The mean height is 70 inches, and the standard deviation is 3 inches. Most men will be around 70 inches tall, with fewer being much shorter or taller.

Generating a Normal Distribution Sample in Python

We can generate a sample of data that follows a normal distribution in Python using the numpy library. Let's create a sample with 1,000 data points, where the mean (μ\mu) is 0 and the standard deviation (σ\sigma) is 1.

import numpy as np
import matplotlib.pyplot as plt

mu = 0  # mean
sigma = 1  # standard deviation
sample = np.random.normal(mu, sigma, 1000)  # generate a sample of 1000 data points

# Plot the sample
plt.hist(sample, bins=30, density=True, alpha=0.6, color='g')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram of Normal Distribution Sample')
plt.show()

In this example:

  1. We set the mean to 0 and the standard deviation to 1.
  2. We generated a sample of 1,000 data points.
  3. We plotted a histogram to visualize the sample.

After we run this code, we will see the following picture:

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal