Topics¶
-
Statistics basics π¶
- Descriptive statistics and inferential statistics
- Continuous data and discrete data
-
Visualization π¶
- Histograms, bar charts, pie charts, scatter plots.
-
Central tendency metrics and dispersion metrics π¶
- Average, median, mode.
- Variance, Standard deviation, Range, Interquartile Range
-
Probabilities π²¶
- Probability Distributions (Normal Distribution)
- Random Variables
- Probability Rules
- Conditional Probability and Bayes' Theorem
- Independence and Dependence
- Correlation and Covariance
- Central Limit Theorem
-
Statistical hypothesis testing β¶
- p-value, confidence interval
- type-1 error and type 2 error
- Z-test and t-test
- ANOVA
Introduction¶
Probability and statistics are the cornerstones of machine learning. Understanding these concepts allows you to make sense of data, build predictive models, and make informed decisions. This guide will take you through the fundamentals, applications, and advanced topics in probability and statistics, complete with Python code examples to help you become an expert.
Statistics¶
Statistics is a powerful and indispensable field of study that plays a pivotal role in helping us make sense of the world around us. It is the science of collecting, analyzing, interpreting, presenting, and organizing data. By harnessing the tools and techniques of statistics, we can uncover valuable insights, identify patterns, and make informed decisions in a wide range of disciplines β from science and business to healthcare and social sciences.
Statistics can be broadly divided into two main categories: descriptive statistics and inferential statistics, each serving a distinct but interconnected purpose.
Descriptive Statistics¶
Descriptive statistics involve methods used to summarize, simplify, and describe the key features of a dataset. This branch aims to provide a concise and meaningful overview of data through measures like:
- Central tendency (mean, median, mode)
- Dispersion (range, variance, standard deviation)
- Graphical representations such as histograms, bar charts, and scatter plots.
Descriptive statistics help us understand the fundamental characteristics of a dataset and make it more manageable for analysis.
Inferential Statistics¶
Inferential statistics go beyond description and seek to draw meaningful conclusions or make predictions based on sample data. It involves techniques such as:
- Hypothesis testing
- Confidence intervals
- Regression analysis
Inferential statistics allow us to make inferences about a larger population based on a representative sample, providing insights into patterns, relationships, and probabilities of outcomes.
Descriptive Statistics Concepts¶
Measures of Central Tendency¶
Refers to the average or βtypicalβ value of a dataset.
- Mean β The arithmetic average.
- Median β The middle value when data is sorted.
- Mode β The most frequent value.
Measures of Dispersion¶
Refers to how spread out the data values are.
- Range β Difference between the maximum and minimum values.
- Variance β Average of squared deviations from the mean.
- Standard Deviation β Square root of variance (measures spread).
- Interquartile Range (IQR) β Middle 50% of the data (Q3 - Q1).
Variance¶
Variance quantifies the average of the squared differences between each data point and the mean. It provides information about how individual data points deviate from the mean.
Population Variance¶
The population variance (ΟΒ²) is defined as:
Where:
- \( x_i \): an individual data point
- \( \mu \): the population mean
- \( N \): total number of data points in the population
Sample Variance¶
The sample variance (sΒ²) β an unbiased estimator of the population variance when calculated from a sample β is defined as:
Where:
- \( x_i \): an individual data point in the sample
- \( \overline{x} \): the sample mean
- \( N \): the number of observations in the sample
When to Use Which¶
- Use population variance when you have data for the entire population.
- Use sample variance when you only have a subset of the population.
Example Calculation¶
Given the dataset: 2, 4, 6, 8
- Mean (xΜ) = (2 + 4 + 6 + 8) / 4 = 5
- Squared deviations:
- (2β5)Β² = 9
- (4β5)Β² = 1
- (6β5)Β² = 1
- (8β5)Β² = 9
-
Sum of squared deviations = 20
-
Population variance (ΟΒ²) = 20 / 4 = 5
- Sample variance (sΒ²) = 20 / (4β1) = 6.67
Python Example¶
# Variance Calculation Examples
# Example dataset
data = [2, 4, 6, 8]
# --- Manual Calculation ---
N = len(data)
mean = sum(data) / N
# Calculate squared differences
squared_diffs = [(x - mean)**2 for x in data]
# Population variance (divide by N)
pop_variance = sum(squared_diffs) / N
# Sample variance (divide by N - 1)
sample_variance = sum(squared_diffs) / (N - 1)
print('Mean =', mean)
print('Population Variance =', pop_variance)
print('Sample Variance =', sample_variance)
# --- Using NumPy ---
import numpy as np
arr = np.array(data)
print('NumPy Population Variance =', arr.var()) # ddof=0 (default)
print('NumPy Sample Variance =', arr.var(ddof=1)) # ddof=1 for sample
Notes¶
- Dividing by Nβ1 in the sample variance formula (Besselβs correction) removes bias in estimating population variance.
- Variance units are squared; take the square root to obtain the standard deviation.