Lesson Overview and Goal Setting

Welcome to our vivid exploration of probability distributions! In this lesson, we're going to delve into different types of probability distributions, specifically the Uniform, Normal and Binominal distributions. We will leverage R's libraries to create visualizations of these distributions.

Understanding the Basics of Probability

Probability quantifies the likelihood of the occurrence of an event among all potential outcomes. For instance, if we toss a coin, the likelihood of obtaining a head is 50%, or 0.5. Essentially, probability distributions map out each outcome of a random variable along with its corresponding probability.

To visualize the distributions under study, we will employ the power of the ggplot2 library in R. At this stage, you can regard ggplot2 as an exceptional tool that aids us in our learning. Remember, our primary focus lies in exploring statistical distributions. There is no need to understand precisely how to use this library, but you will be provided with a fully-working code for this lesson and the following practices. We will cover the details of data visualization in R in one of the following courses.

Exploring Uniform Distribution

Imagine a situation where all outcomes are equally likely to occur. This scenario can be depicted by a Uniform Distribution. For instance, if we pick a suit from a deck of cards, the probabilities of getting a heart, club, diamond, or spade are equal. Let's generate and plot a Uniform Distribution using runif() and ggplot2.

library(ggplot2)
set.seed(123)

# Generate random numbers uniformly distributed between -1 and 1
uniform_data <- runif(1000, min = -1, max = 1)

# Plot a Histogram of the distribution
plot <- ggplot() +
 geom_histogram(aes(uniform_data), bins = 20, fill = 'dodgerblue3', alpha = 0.7) +
 labs(title = "Uniform Distribution")

Here, runif(1000, min = -1, max = 1) generates 1000 random numbers uniformly distributed between -1 and 1. The geom_histogram function constructs a histogram of the distribution.

Exploring Normal Distribution

Let's shift our focus to the Normal Distribution, which is a statistical function characterized by a bell-shaped curve and used extensively in statistical analysis. A key feature of the Normal Distribution lies in its definition via just two parameters: the mean (average) and the standard deviation (spread). Let's simulate and plot a Normal Distribution:

# Generate Normal Distribution data
normal_data <- rnorm(1000, mean = 0, sd = 1)

# Plot a Histogram of the distribution
plot <- ggplot() +
 geom_histogram(aes(normal_data), bins = 20, fill = 'dodgerblue3', alpha = 0.7) +
 labs(title = "Normal Distribution")

The function rnorm(1000, mean = 0, sd = 1) generates 1000 data points conforming to a Normal Distribution with a mean of 0 and a standard deviation of 1.

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