This is an article from the series about statistics (and probability theory). The motivation behind this series is to retain knowledge while taking courses, reading books and articles. I write articles about something that I missed before, found interesting or possibly useful to me in the future. These articles do not constitute a course, they are just a collection of my personal study notes or summaries on different topics related to statistics.
Outcomes and events
In statistics, observational units are people, things, systems, etc., for which data is observed. Variables are characteristics or quantities that represent attributes of observational units. For example, if a hive of bees is an observational unit, then variables can include the type of bees, the age of the hive, the number of hive frames, the defensiveness of the bees, etc.
In probability theory, an outcome is the result of some random situation, experiment or phenomenon. It can be a coin flip, a soccer match, a new neighbour, or the weather tomorrow. The sample space is the collection of all possible outcomes of a random phenomenon. But in practice, it is often infeasible or even impossible to list all possible outcomes, so instead, we work with events. An event is a specific combination of outcomes or a subset of the sample space that we are interested in. For example, “Team A wins the match with team B”, “The temperature is exactly 20°C”, “My new neighbour is taller than me, has any number of kids and one cat” or “Getting 5 tails out of 7 coin flips and the first flip results in tail”.
Because events are sets, we can use basic set operations: unions (or), intersections (and) and complements (not). Because the probability of the union of all outcomes in sample space is always equal to 1, if we know the probability of some event, we can easily find its complementary event without calculating the probabilities of all remaining outcomes.
In probability theory, outcomes are somewhat similar to observational units in statistics in that sense that typically they both aren’t defined strictly. More often we work with events instead of outcomes and variables instead of observational units.
Random variables
Random variable, also known as stochastic variable, describes the numerical outcome of a random experiment or phenomenon. It encompasses all possible outcomes in some form. Random variables can be divided into two categories:
discrete random variable - can take on a finite number of distinct values. Example: the number of kids in a family.
continuous random variable - can take on an infinite number of possible values (from an interval). Example: the weight of a potato.
Probability distribution
Probability distribution is a mathematical function that describes the likelihood of different outcomes or events. There are several such functions:
PDF: Probability density function - describes the probability distribution of a continuous random variable.
PMF: Probability mass function - describes the probability distribution of a discrete random variable.
CDF: Cumulative distribution function - gives the cumulative probability distribution for a random variable (discrete or continuous). the CDF provides the probability that a random variable is less than or equal to a specific value. It is defined for both discrete and continuous random variables.
Discrete distributions
A discrete distribution models discrete random variables, i.e. variables with a countable number of possible outcomes.
Binomial. The binomial distribution describes the number of successes in a fixed number of trials with two possible outcomes. For example, the number of tails in 100 coin tosses.
Bernoulli. Models a single trial with two possible outcomes: success or failure. It is often used to represent situations where there is a binary event, and we want to calculate the probability of observing a specific outcome. For example, the probability of drawing a red card from a deck.
Geometric. Models the number of trials needed to achieve the first success in a sequence of independent Bernoulli trials, where each trial has two possible outcomes (success or failure) and the probability of success remains the same for each trial. The geometric distribution is commonly used in various applications, such as modelling the number of attempts until an event occurs, analyzing waiting times, and estimating probabilities in scenarios involving repeated trials with a fixed probability of success.
Poisson. Models the number of events occurring in a fixed interval of time or space, given the average rate of occurrence. It is used when events happen randomly and independently over a continuous interval. Unlike binomial distribution, we don’t have any successes or failures, we only model how many events occurred. The Poisson distribution is often used in situations where events occur with a low probability but at a high rate, such as modelling the number of phone calls received at a call centre in a given time period, the number of customers arriving at a store per hour, or the number of accidents on a particular stretch of road in a day.
Code
import numpy as npimport seaborn as snsfrom scipy.stats import binom, geom, bernoulli, poissonimport matplotlib.pyplot as pltfig, axs = plt.subplots(4, 2, figsize=(11, 12), num='discrete', gridspec_kw={'width_ratios': [2, 1], 'wspace':0.1, 'hspace':0.4})fig.suptitle('Discrete distributions examples, \nprobability mass functions (PMFs) and \ cumulative distribution functions (CDFs)', fontsize=14)fig.subplots_adjust(top=0.9)# Binomial#ax = axs[0,0]p =0.7# the probability of successn =100# the number of trialsx = np.arange(binom.ppf(0.001, n, p), binom.ppf(0.999, n, p)).astype(int)y = binom.pmf(x, n, p)y2 = binom.cdf(x, n, p)sns.barplot(ax=axs[0,0], x=x, y=y).set_title('Binomial distribution PMF')sns.lineplot(ax=axs[0,1], x=x, y=y2).set_title('Binomial distribution CDF')ax.set_xticks(axs[0,0].get_xticks()[::3])# Bernoulliax = axs[1,0]p =0.3# the probability of success (e.g. obserivng a specific outcome)x = [0., 1.] # 0 is failure and 1 is successx_plot = ['failure', 'success'] # just to hve a nicer ploty = bernoulli.pmf(x, p)y2 = bernoulli.cdf(x, p)ax.plot(x, y, 'bo', ms=8, label='bernoulli pmf')ax.vlines(x_plot, 0, y, colors='b', linestyles='-', lw=2, label='frozen pmf')ax.set_title('Bernoulli distribution PMF')ax.set_xlabel('Outcome')sns.lineplot(ax=axs[1,1], x=x, y=y2).set_title('Bernoulli distribution CDF')# Geometricx = np.arange(geom.ppf(0.001, p), geom.ppf(0.999, p)).astype(int)y = geom.pmf(x, p)y2 = geom.cdf(x, p)sns.barplot(ax=axs[2,0], x=x, y=y).set_title('Geometric distribution PMF')sns.lineplot(ax=axs[2,1], x=x, y=y2).set_title('Geometric distribution CDF')# Poissonax = axs[3,0]mu =30# the average rate of events occurring in the given intervalx = np.arange(poisson.ppf(0.001, mu), poisson.ppf(0.999, mu)).astype(int)y = poisson.pmf(x, mu)y2 = poisson.cdf(x, mu)sns.barplot(ax=ax, x=x, y=y).set_title('Poisson distribution PMF')ax.set_xticks(ax.get_xticks()[::3])sns.lineplot(ax=axs[3,1], x=x, y=y2).set_title('Poisson distribution CDF')plt.show('discrete')fig.clear()plt.close(fig)
Continuous distributions
Most popular examples of continuous distributions are:
Uniform. All values within a specified range have equal probability. It is often used when there is no preference or bias towards any particular value in the range.
Normal or Gaussian. Characterized by a bell-shaped curve. It is used to model many natural phenomena, such as heights, weights, and measurement errors.
Exponential. This distribution models the time between events in a Poisson process, where events occur randomly and independently at a constant average rate.
Gamma. The gamma distribution is a versatile distribution that is often used to model waiting times, durations, and survival times. It is a continuous analogue of the negative binomial distribution.
Note that the exponential distribution is a specific case of the gamma distribution with a shape parameter of 1, the gamma distribution offers more flexibility in modelling various scenarios by allowing different shapes and scales. The choice between these distributions depends on the specific characteristics and requirements of the data being analyzed.
Code
from scipy.stats import uniform, norm, expon, gammafig, axs = plt.subplots(4, 2, figsize=(11, 12), num='continuous', gridspec_kw={'width_ratios': [2, 1], 'wspace':0.1, 'hspace':0.4})fig.suptitle('Continuous distributions examples, \nprobability density functions (PDFs) and \ cumulative distribution functions (CDFs)', fontsize=14)fig.subplots_adjust(top=0.9)x = np.linspace(-3, 3, 100)# Uniformy = uniform.pdf(x, loc=-1, scale=3) # uniform probability on [loc, loc+scale]y2 = uniform.cdf(x, loc=-1, scale=3)sns.lineplot(ax=axs[0,0], x=x, y=y, legend=True).set_title('Uniform distribution PDF')sns.lineplot(ax=axs[0,1], x=x, y=y2, legend=True).set_title('Uniform distribution CDF')# Normaly = norm.pdf(x)y2 = norm.cdf(x)sns.lineplot(ax=axs[1,0], x=x, y=y, legend=True).set_title('Normal distribution PDF')sns.lineplot(ax=axs[1,1], x=x, y=y2, legend=True).set_title('Normal distribution CDF')x = np.linspace(-1, 8, 100)# Gammafor a in (0.8, 1.4, 2.1, 5.): y = gamma.pdf(x, a=a) y2 = gamma.cdf(x, a=a) sns.lineplot(ax=axs[2,0], x=x, y=y, label=f'a={a}').set_title('Gamma distribution PDF') sns.lineplot(ax=axs[2,1], x=x, y=y2, label=f'a={a}').set_title('Gamma distribution CDF')# Exponentialy = expon.pdf(x)y2 = expon.cdf(x)sns.lineplot(ax=axs[3,0], x=x, y=y, legend=True).set_title('Exponential distribution PDF \n= Gamma(a=1.)')sns.lineplot(ax=axs[3,1], x=x, y=y2, legend=True).set_title('Exponential distribution CDF \n= Gamma(a=1.)')plt.show('continuous')fig.clear()plt.close(fig)
Practical notes: KDE vs PDF
Kernel Density Estimation is a non-parametric statistical technique used to estimate the probability density function (PDF) of a continuous random variable based on a set of observed data points. The goal of KDE is to create a smooth estimate of the underlying distribution of the data.
In KDE, a kernel function is placed at each data point, and these kernels are then combined to create a smooth curve that approximates the PDF. The kernel function is usually a smooth and symmetric function, such as the Gaussian (normal) distribution.
Below is an example from (sci-learn tutorial)[https://scikit-learn.org/stable/modules/density.html#kernel-density-estimation]