DEV Community

Cover image for Top 15 Statistical Methods in Data Science: A Complete Guide with Examples
Amit Chandra
Amit Chandra

Posted on

Top 15 Statistical Methods in Data Science: A Complete Guide with Examples

Introduction:

In the rapidly evolving field of data science, statistical methods form the backbone of analysis, prediction, and decision-making. From simple measures of central tendency to complex hypothesis testing, these techniques allow data scientists to extract insights, model relationships, and make data-driven decisions. In this article, we will explore 15 essential statistical methods commonly used in data science, explaining each method with an example for practical understanding.


1. Descriptive Statistics

Explanation:

Descriptive statistics summarize and describe the main features of a dataset. This includes measures of central tendency (mean, median, mode) and measures of variability (standard deviation, variance).

Example:

Given a dataset of employee salaries, descriptive statistics help you find the average salary (mean), the most common salary (mode), and how dispersed the salaries are (standard deviation).

import numpy as np
import pandas as pd

# Example dataset of employee salaries
salaries = np.array([55000, 48000, 60000, 75000, 62000, 59000])

# Mean, Median, Mode, Standard Deviation
mean_salary = np.mean(salaries)
median_salary = np.median(salaries)
std_salary = np.std(salaries)

print(f"Mean Salary: {mean_salary}")
print(f"Median Salary: {median_salary}")
print(f"Standard Deviation: {std_salary}")
Enter fullscreen mode Exit fullscreen mode

2. Probability Distributions

Explanation:

Probability distributions describe how the values of a random variable are distributed. Common distributions include normal, binomial, and Poisson distributions.

Example:

In quality control, the binomial distribution can model the number of defective products in a batch, while the normal distribution models continuous data like human heights.

from scipy.stats import binom, norm
import matplotlib.pyplot as plt

# Binomial Distribution (Example: 10 trials, probability of success 0.5)
n, p = 10, 0.5
binom_dist = binom.pmf(k=np.arange(0, 11), n=n, p=p)

# Normal Distribution (Example: mean=0, std=1)
x = np.linspace(-3, 3, 100)
normal_dist = norm.pdf(x)

plt.plot(np.arange(0, 11), binom_dist, 'bo-', label="Binomial Distribution")
plt.plot(x, normal_dist, 'r-', label="Normal Distribution")
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

3. Hypothesis Testing

Explanation:

Hypothesis testing is used to determine whether there is enough evidence to reject a null hypothesis in favor of an alternative hypothesis. Common tests include t-tests, chi-square tests, and ANOVA.

Example:

A company claims their product increases productivity by 10%. Using a t-test, you can test whether the observed data supports this claim by comparing the mean productivity of two groups (with and without the product).

from scipy.stats import ttest_ind

# Two groups: productivity with and without product
productivity_with = [102, 110, 98, 105, 120]
productivity_without = [95, 85, 90, 92, 88]

# Perform t-test
t_stat, p_val = ttest_ind(productivity_with, productivity_without)
print(f"T-statistic: {t_stat}, P-value: {p_val}")
Enter fullscreen mode Exit fullscreen mode

4. p-Value and Significance

Explanation:

The p-value helps in hypothesis testing by quantifying the evidence against the null hypothesis. A low p-value (typically < 0.05) indicates strong evidence to reject the null hypothesis.

Example:

In an A/B test to compare two marketing strategies, a p-value of 0.03 suggests that the new strategy performs significantly better than the old one.

# Reusing the t-test example
if p_val < 0.05:
    print("Reject the null hypothesis, there is a significant difference.")
else:
    print("Fail to reject the null hypothesis, no significant difference.")
Enter fullscreen mode Exit fullscreen mode

5. Regression Analysis

Explanation:

Regression is used to model the relationship between a dependent variable and one or more independent variables. Linear regression is the simplest form, but logistic and polynomial regression are also popular.

Example:

In a real estate dataset, linear regression can be used to predict house prices based on features like square footage, number of bedrooms, and location.

from sklearn.linear_model import LinearRegression
import numpy as np

# Example data (Square footage, price)
X = np.array([[1500], [2000], [2500], [3000], [3500]])  # Square footage
y = np.array([300000, 400000, 500000, 600000, 700000])  # Price

# Create and fit model
model = LinearRegression()
model.fit(X, y)

# Predict price of a 3200 sqft house
predicted_price = model.predict([[3200]])
print(f"Predicted Price: {predicted_price[0]}")
Enter fullscreen mode Exit fullscreen mode

6. Correlation and Covariance

Explanation:

Correlation measures the strength of the relationship between two variables, while covariance indicates the direction of the relationship. A positive correlation means both variables move in the same direction.

Example:

In stock market analysis, you can calculate the correlation between two stocks to determine if their prices move together.

data = {'Stock_A': [10, 12, 14, 16, 18], 'Stock_B': [22, 24, 28, 26, 32]}
df = pd.DataFrame(data)

# Correlation and Covariance
correlation = df['Stock_A'].corr(df['Stock_B'])
covariance = df['Stock_A'].cov(df['Stock_B'])

print(f"Correlation: {correlation}")
print(f"Covariance: {covariance}")
Enter fullscreen mode Exit fullscreen mode

7. Central Limit Theorem

Explanation:

The Central Limit Theorem (CLT) states that the sampling distribution of the sample mean will approximate a normal distribution as the sample size becomes larger, regardless of the population's distribution.

Example:

When conducting repeated surveys of customer satisfaction, the average of the sample means will tend to a normal distribution, even if the original satisfaction scores are skewed.

import numpy as np
import matplotlib.pyplot as plt

# Simulate rolling a die
die_rolls = np.random.randint(1, 7, size=(10000, 100))
sample_means = np.mean(die_rolls, axis=1)

# Plot the distribution of sample means
plt.hist(sample_means, bins=30, density=True)
plt.title('Distribution of Sample Means (Central Limit Theorem)')
plt.show()
Enter fullscreen mode Exit fullscreen mode

8. Bayesian Statistics

Explanation:

Bayesian statistics involves updating the probability of a hypothesis as more evidence or data becomes available. It relies on Bayes’ Theorem.

Example:

In spam filtering, Bayesian models are used to update the probability that an email is spam based on new data (such as the presence of certain words).

from scipy.stats import beta

# Example: Prior beliefs (alpha=2, beta=2)
a, b = 2, 2
x = np.linspace(0, 1, 100)
y = beta.pdf(x, a, b)

plt.plot(x, y, label='Prior Belief')
plt.title('Bayesian Prior Distribution')
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

9. Analysis of Variance (ANOVA)

Explanation:

ANOVA is used to compare the means of three or more groups to see if they are statistically different from each other.

Example:

You can use ANOVA to determine if the average performance differs between students from three different schools.

from scipy.stats import f_oneway

# Three groups of students' scores from different schools
group1 = [85, 90, 88, 92]
group2 = [78, 85, 80, 82]
group3 = [91, 93, 89, 90]

# Perform ANOVA
f_stat, p_value = f_oneway(group1, group2, group3)
print(f"F-statistic: {f_stat}, P-value: {p_value}")
Enter fullscreen mode Exit fullscreen mode

10. Time Series Analysis

Explanation:

Time series analysis involves analyzing data points collected over time to identify trends, seasonal patterns, or cyclic behavior.

Example:

Time series analysis can forecast future sales based on historical data, identifying patterns in seasonal demand.

import pandas as pd
import matplotlib.pyplot as plt

# Example time series data (monthly sales)
date_range = pd.date_range(start='2023-01-01', periods=12, freq='M')
sales = pd.Series([200, 220, 230, 210, 250, 270, 300, 320, 330, 310, 290, 350], index=date_range)

# Plot time series
sales.plot(title='Monthly Sales', marker='o')
plt.show()
Enter fullscreen mode Exit fullscreen mode

11. Principal Component Analysis (PCA)

Explanation:

PCA is a dimensionality reduction technique used to reduce the number of variables in a dataset while preserving as much information as possible.

Example:

In image processing, PCA is used to reduce the complexity of images while maintaining their essential features for recognition.

from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

# Load iris dataset
iris = load_iris()
X = iris.data

# Apply PCA to reduce dimensions to 2
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)

# Scatter plot of reduced data
plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=iris.target)
plt.title('PCA of Iris Dataset')
plt.show()
Enter fullscreen mode Exit fullscreen mode

12. Chi-Square Test

Explanation:

The chi-square test is used to determine if there is an association between categorical variables in a contingency table.

Example:

A chi-square test can evaluate whether the distribution of customer preferences for three different products is statistically significant.

from scipy.stats import chi2_contingency

# Example contingency table (product preferences)
data = [[20, 30, 50], [40, 60, 80]]

# Perform chi-square test
chi2, p, dof, expected = chi2_contingency(data)
print(f"Chi-Square Statistic: {chi2}, P-value: {p}")
Enter fullscreen mode Exit fullscreen mode

13. K-Means Clustering

Explanation:

K-Means is a clustering algorithm that partitions data into K distinct clusters based on their features. It is an unsupervised learning method.

Example:

In market segmentation, K-means can group customers into segments based on purchasing behavior and demographics.

from sklearn.cluster import KMeans
import numpy as np

# Example customer data (age, income)
X = np.array([[25, 50000], [30, 60000], [35, 70000], [40, 80000]])

# Apply K-Means clustering
kmeans = KMeans(n_clusters=2)
kmeans.fit(X)
print("Cluster Centers:", kmeans.cluster_centers_)
Enter fullscreen mode Exit fullscreen mode

14. Markov Chains

Explanation:

Markov Chains are used to model systems where the next state depends only on the current state and not on the previous states (memoryless).

Example:

In website analytics, Markov Chains can model user navigation behavior, predicting the probability of moving from one page to another.

import numpy as np

# Example transition matrix
transition_matrix = np.array([[0.7, 0.3], [0.4, 0.6]])

# Initial state
state = np.array([1, 0])  # Starts in state 0

# Predict next state
next_state = state.dot(transition_matrix)
print(f"Next State: {next_state}")
Enter fullscreen mode Exit fullscreen mode

15. Monte Carlo Simulation

Explanation:

Monte Carlo simulation is a method of solving problems using random sampling to obtain numerical results. It is often used for risk assessment and decision-making.

Example:

Monte Carlo simulations are used in financial modeling to predict the probability of different investment outcomes based on random inputs.

import numpy as np

# Simulate 10,000 random outcomes of investment returns (mean=5%, std=10%)
simulated_returns = np.random.normal(0.05, 0.10, 10000)

# Calculate probability of losing money (return < 0)
probability_of_loss = np.mean(simulated_returns < 0)
print(f"Probability of Loss: {probability_of_loss * 100:.2f}%")
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Understanding and mastering these statistical methods is essential for any data scientist. Each method has its own unique application and helps in drawing meaningful insights from data. Whether you are analyzing trends, testing hypotheses, or building predictive models, these techniques will help you make informed decisions and extract value from data.


This article will help beginners as well as seasoned professionals refresh and deepen their understanding of these critical statistical tools.


DataScience MachineLearning, Statistics, Python, AI, DataAnalysis, BigData, Analytics, StatisticalMethods, PythonCode, DataScienceTools, DataVisualization, MachineLearningAlgorithms, TechBlog, DataScientist, AIinFinance, DataScienceCommunity, ArtificialIntelligence, TimeSeriesAnalysis

Top comments (0)