DS Math Notes BASIC Part1
DS Math Notes BASIC Part1
⚡ Each topic includes: Definition • Explanation • Formula • Interview Q&A • Code • Common Mistakes • Quick Revision
TOPIC 1 Descriptive Statistics Summarize & understand data at a glance
📖 DEFINITION
Descriptive statistics are mathematical tools that summarize and describe the main features of a dataset.
Instead of looking at every single number, we use a few key values to understand what the data looks like
overall — its center, spread, and shape.
💡 EXPLANATION (200-250 words)
Imagine your class got the following marks in a test: 55, 60, 72, 80, 90, 60, 72, 85. That's 8 numbers.
Descriptive statistics help you answer questions like: What is the average score? What is the most common
score? How spread out are the scores?
The three most important measures of the CENTER of data are:
• Mean (Average): Add all values and divide by the count. Most commonly used.
• Median: The middle value when data is sorted. Great when there are outliers (extreme values).
• Mode: The most frequently occurring value. Used for categories.
The two key measures of SPREAD (how far apart values are):
• Variance: Average of the squared differences from the mean. Tells you how 'scattered' data is.
• Standard Deviation (SD): Square root of variance. Easier to interpret because it's in the same unit as
the original data.
Example with marks [55, 60, 60, 72, 72, 80, 85, 90]:
• Mean = (55+60+60+72+72+80+85+90) / 8 = 574 / 8 = 71.75
• Median = (72+72)/2 = 72 (middle two values)
• Mode = 60 and 72 (both appear twice — bimodal)
• Variance ≈ 131.69, Standard Deviation ≈ 11.47
📐 MATHEMATICAL FORMULAS
Mean (μ) = (x₁ + x₂ + ... + xₙ) / n = Σxᵢ / n
Variance (σ²) = Σ(xᵢ - μ)² / n (for population)
Variance (s²) = Σ(xᵢ - x̄ )² / (n-1) (for sample — use n-1 !)
Standard Deviation (σ) = √Variance
Median = middle value of sorted data (or avg of 2 middle if even n)
🎯 REAL INTERVIEW QUESTIONS & ANSWERS
Q1: When would you use median instead of mean?
Answer: Use median when data has outliers (extreme values). For example, in salary data, a few CEOs
earning crores skew the mean upward. The median gives a better 'typical' value. Example: [10K, 12K, 11K,
13K, 500K] → Mean = 109.2K (misleading), Median = 12K (realistic).
Q2: What is the difference between population variance and sample variance?
Answer: For population variance we divide by n (total population). For sample variance we divide by (n-1) —
this is called Bessel's correction. We do this because samples tend to underestimate spread; using n-1
corrects this bias. In Data Science, we almost always work with samples, so use n-1.
Q3: If mean > median, what does that say about the distribution?
Answer: It means the data is right-skewed (positively skewed). There are some very large values pulling the
mean upward. Examples include income distributions, house prices, and response times.
⚠️ COMMON MISTAKES TO AVOID
• ❌ Using mean with outliers — always check for outliers first; use median instead.
• ❌ Dividing by n instead of (n-1) for sample variance — always use n-1 for sample data.
• ❌ Forgetting to sort data before finding the median.
• ❌ Assuming standard deviation and variance are the same — SD = √Variance.
• ❌ Ignoring mode in categorical data — mode is the only measure of center for categories.
✅ WHEN TO USE vs ❌ WHEN NOT TO USE
✅ Use MEAN when data is normally distributed ❌ Do NOT use mean for highly skewed data
✅ Use MEDIAN when outliers exist ❌ Do NOT use mode as the only summary for
numerical data
✅ Use MODE for categorical data (e.g., most ❌ Do NOT use variance alone (hard to interpret
popular product) units)
✅ Use SD to compare spread across two datasets ❌ Do NOT use SD if you haven't checked for
outliers first
# Central Tendency
mean = [Link](data) # 71.75
median = [Link](data) # 72.0
mode = [Link](data)[0] # 60 (first mode)
# Spread
variance = [Link](data, ddof=1) # ddof=1 for sample variance
std_dev = [Link](data, ddof=1) # sample standard deviation
Mean Easy to calc; uses all data Pulled by outliers; not robust
📖 DEFINITION
Probability is a number between 0 and 1 that tells us how likely an event is to happen. P = 0 means
impossible. P = 1 means certain. Everything else falls in between. It is the mathematical language of
uncertainty — the foundation of all machine learning.
💡 EXPLANATION (200-250 words)
Think of probability as answering: 'Out of all possible outcomes, how many make our event happen?' If you
roll a fair die, there are 6 possible outcomes. The chance of getting a 4 is 1 out of 6, so P(4) = 1/6 ≈ 0.167.
Key concepts in probability:
• Sample Space (S): All possible outcomes. For a coin flip: S = {Heads, Tails}.
• Event (E): A specific outcome or set of outcomes we care about.
• Complement Rule: P(not A) = 1 - P(A). If P(rain) = 0.3, then P(no rain) = 0.7.
• Addition Rule: P(A or B) = P(A) + P(B) - P(A and B). Avoids double-counting.
• Multiplication Rule: P(A and B) = P(A) × P(B|A). For independent events: P(A) × P(B).
• Conditional Probability: P(A|B) = probability of A given B has occurred.
The MOST IMPORTANT formula for DS/ML is Bayes' Theorem — used in spam filters, medical diagnosis,
and NLP:
Real Example: If P(email is spam) = 0.2, and P(word 'free' | spam) = 0.9, and P('free' | not spam) = 0.1.
Bayes tells us: given the email contains 'free', what is P(spam)?
📐 MATHEMATICAL FORMULAS
Basic Probability: P(E) = Favorable Outcomes / Total Outcomes
Complement: P(Aᶜ) = 1 - P(A)
Addition Rule: P(A ∪ B) = P(A) + P(B) - P(A ∩ B)
Multiplication Rule: P(A ∩ B) = P(A) × P(B|A)
Conditional Probability: P(A|B) = P(A ∩ B) / P(B)
Bayes' Theorem: P(A|B) = [ P(B|A) × P(A) ] / P(B)
For Independent Events: P(A ∩ B) = P(A) × P(B)
🎯 REAL INTERVIEW QUESTIONS & ANSWERS
Q1: What is Bayes' Theorem and where is it used in ML?
Answer: Bayes' Theorem updates our belief about a hypothesis after seeing new evidence. Formula: P(H|E)
= P(E|H) × P(H) / P(E). In ML it's used in Naive Bayes classifiers for spam detection, sentiment analysis, and
medical diagnosis. It allows a model to start with a prior belief and update it with data.
Q2: What is the difference between joint and conditional probability?
Answer: Joint probability P(A ∩ B) is the probability that BOTH events A and B happen at the same time.
Conditional probability P(A|B) is the probability of A happening GIVEN that B has already happened. P(A|B)
= P(A ∩ B) / P(B).
Q3: Two events are independent. P(A)=0.4, P(B)=0.3. Find P(A and B).
Answer: Since independent: P(A ∩ B) = P(A) × P(B) = 0.4 × 0.3 = 0.12 (12%). Independent means knowing
B happened tells us nothing about A.
⚠️ COMMON MISTAKES TO AVOID
• ❌ Confusing P(A|B) with P(B|A) — these are DIFFERENT! This is called the 'base rate fallacy'.
• ❌ Assuming events are independent without checking — always verify independence.
• ❌ Forgetting to subtract the intersection in the addition rule — leads to double counting.
• ❌ Using 'probability' and 'odds' interchangeably — they're different (odds = p / (1-p)).
• ❌ Thinking P = 0 means 'impossible' in continuous distributions — it just means infinitely unlikely.
✅ WHEN TO USE vs ❌ WHEN NOT TO USE
✅ Bayes' Theorem: for classification, spam filters ❌ Don't apply independence assumption without
checking
✅ Conditional probability: when events are ❌ Don't use classical probability for skewed real-
dependent world events
✅ Multiplication rule: for independent sequential ❌ Don't ignore base rates when applying Bayes
events
✅ Complement rule: when 'not happening' is easier ❌ Don't confuse theoretical vs empirical probability
to compute
Empirical Prob. Based on observed data / Flip coin 1000 times, count heads
experiments
Bayesian Prob. Prior belief updated with Spam filter learning from
evidence examples
TOPIC 3 Probability Distributions Understanding how data is spread
📖 DEFINITION
A probability distribution describes all possible values a variable can take and how likely each value is. It's
like a 'map' of probability — telling you where most values cluster and where they rarely appear.
Distributions are the backbone of statistical modeling.
💡 EXPLANATION (200-250 words)
There are two types of distributions: Discrete (countable outcomes) and Continuous (any value in a range).
The 3 most important distributions in Data Science:
• Normal Distribution (Gaussian): Bell-shaped curve. Most real-world data (heights, test scores, errors in
measurement) follows this. Defined by mean (μ) and standard deviation (σ). The '68-95-99.7 rule' tells
us: 68% of data falls within 1σ of mean, 95% within 2σ, 99.7% within 3σ.
• Binomial Distribution: Used when you have n trials, each with probability p of 'success'. Example:
'What is the probability of getting exactly 3 heads in 10 coin flips?' In ML, it models binary classification
outcomes.
• Poisson Distribution: Used to count events in a fixed time or space. Example: 'How many customers
will arrive in the next hour?' Uses one parameter λ (average rate).
Why do DS/ML engineers care? Because many ML algorithms assume your data follows the Normal
distribution. If it doesn't, you may need to transform it. Knowing the right distribution helps you choose the
right model and interpret results correctly.
📐 MATHEMATICAL FORMULAS
Normal: P(x) = (1/σ√2π) × e^(-(x-μ)²/2σ²) [Bell Curve]
Standard Normal (Z-score): Z = (x - μ) / σ
Binomial: P(X=k) = C(n,k) × pᵏ × (1-p)^(n-k)
Binomial Mean = n×p, Variance = n×p×(1-p)
Poisson: P(X=k) = (λᵏ × e⁻λ) / k!
Poisson Mean = Variance = λ
🎯 REAL INTERVIEW QUESTIONS & ANSWERS
Q1: What is the Central Limit Theorem (CLT)?
Answer: CLT states that if you take large enough samples (n ≥ 30) from ANY distribution, the distribution of
the sample MEANS will approach a Normal distribution. This is why normal distribution is so powerful in
statistics — it applies even when the underlying data isn't normal. It's the mathematical reason why we can
use t-tests and z-tests.
Q2: What is a Z-score and how do you use it?
Answer: Z-score measures how many standard deviations a value is from the mean. Z = (x - μ) / σ. Z=0
means exactly at mean. Z=2 means 2 standard deviations above. Used for: outlier detection (|Z| > 3 is
usually an outlier), feature normalization, and comparing values across different scales.
Q3: Which distribution would you use to model the number of fraud transactions per day?
Answer: Poisson distribution — because fraud transactions are rare events that occur at some average rate
(λ), they are independent of each other, and we're counting occurrences in a fixed time window. These are
exactly the conditions for a Poisson distribution.
⚠️ COMMON MISTAKES TO AVOID
• ❌ Assuming all data is normally distributed without checking — plot a histogram first!
• ❌ Confusing Binomial and Bernoulli — Bernoulli is just 1 trial; Binomial is n trials.
• ❌ Forgetting that Poisson requires events to be independent and rare.
• ❌ Using Z-score with non-normal data — first check normality with a Q-Q plot or Shapiro-Wilk test.
• ❌ Mixing up PDF (height) and CDF (area) — CDF gives probability, not PDF directly.
✅ WHEN TO USE vs ❌ WHEN NOT TO USE
✅ Normal: test scores, measurement errors, ML ❌ Normal: skewed data, counts, proportions
residuals
✅ Poisson: rare events per time/area (fraud, ❌ Poisson: when events are NOT independent or
accidents) rare
✅ Z-score: feature scaling, outlier detection ❌ Z-score: small samples (use t-distribution
instead)
print(f'P(score<80) = {p_below_80:.4f}')
print(f'P(3 heads) = {p_3_heads:.4f}')
print(f'P(2 frauds) = {p_2_fraud:.4f}')
Poisson Count of events in fixed time Calls per hour, bugs per page
📖 DEFINITION
Linear algebra is the branch of mathematics dealing with vectors (lists of numbers) and matrices (grids of
numbers). Almost every ML algorithm — from linear regression to deep neural networks — is just linear
algebra under the hood. It's the math that makes computation on large datasets fast and efficient.
💡 EXPLANATION (200-250 words)
A VECTOR is just a list of numbers like [3, 5, 2]. Think of it as a point in space or a data row with 3 features.
A MATRIX is a rectangular grid of numbers — like a spreadsheet. A 3×2 matrix has 3 rows and 2 columns.
Why does this matter for DS/ML?
• Your entire dataset IS a matrix! Each row = one data sample. Each column = one feature.
• ML model weights are stored as vectors and matrices.
• Neural network forward pass = just a series of matrix multiplications.
Key operations:
• Dot Product: Multiply matching elements and sum them. [1,2,3]·[4,5,6] = 1×4 + 2×5 + 3×6 = 32. Used
in similarity measures and linear regression.
• Matrix Multiplication: Row × Column multiplication. Input matrix × weight matrix = output. Used in every
neural network layer.
• Transpose: Flip rows and columns. If A is 3×2, then Aᵀ is 2×3.
• Eigenvalues & Eigenvectors: Special vectors that only scale (not rotate) when multiplied by a matrix.
Used in PCA (dimensionality reduction).
📐 MATHEMATICAL FORMULAS
Dot Product: a · b = Σ(aᵢ × bᵢ) = a₁b₁ + a₂b₂ + ... + aₙbₙ
Vector Magnitude: ||a|| = √(a₁² + a₂² + ... + aₙ²)
Cosine Similarity: cos(θ) = (a · b) / (||a|| × ||b||) ∈ [-1, 1]
Matrix Multiply: C[i,j] = Σₖ A[i,k] × B[k,j] (A: m×k, B: k×n → C:
m×n)
Transpose: (Aᵀ)[i,j] = A[j,i]
Eigenvalue: Av = λv (v = eigenvector, λ = eigenvalue)
🎯 REAL INTERVIEW QUESTIONS & ANSWERS
Q1: What is the dot product and why is it used in ML?
Answer: Dot product multiplies corresponding elements and sums them. It measures how 'aligned' two
vectors are. In ML: linear regression output = weight vector · input vector. In NLP: cosine similarity (dot
product normalized) measures how similar two word embeddings are. It's the most common operation in ML
— appears in every activation function computation.
Q2: What is PCA and how does it use linear algebra?
Answer: PCA (Principal Component Analysis) uses eigenvalues and eigenvectors to reduce dimensionality.
Steps: (1) Compute covariance matrix of features. (2) Find eigenvalues/eigenvectors. (3) Sort by largest
eigenvalues. (4) Project data onto top k eigenvectors. This captures maximum variance in fewer dimensions,
speeding up training and removing noise.
Q3: Why does matrix multiplication order matter?
Answer: Matrix multiplication is NOT commutative: A×B ≠ B×A. Also, for A×B to be valid, the number of
columns in A must equal the number of rows in B. For neural networks, the shape of weight matrices must
be compatible at each layer, or you get a dimension mismatch error.
⚠️ COMMON MISTAKES TO AVOID
• ❌ Treating matrix multiplication as element-wise — it is NOT the same as simple multiplication.
• ❌ Forgetting dimension compatibility: (m×n) × (n×p) = (m×p). Inner dimensions must match!
• ⁻Confusing transpose with inverse — Aᵀ flips rows/cols; A⁻¹ satisfies A × A⁻¹ = I.
• ❌ Assuming every matrix has an inverse — only square, non-singular matrices do.
• ❌ Using loops for matrix operations in Python — always use NumPy for speed!
✅ WHEN TO USE vs ❌ WHEN NOT TO USE
✅ Matrix multiplication: neural network layers ❌ Don't use loops for matrix ops — use NumPy
✅ Dot product: similarity, regression predictions ❌ Don't invert matrices numerically — use
[Link]
✅ Eigenvectors: PCA, dimensionality reduction ❌ Don't apply PCA before splitting train/test data
# Vectors
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
dot = [Link](a, b) # 32
mag_a = [Link](a) # 3.742
cos_sim = dot / ([Link](a) * [Link](b)) # 0.9746
# Matrix Operations
A = [Link]([[1, 2], [3, 4], [5, 6]]) # 3x2
B = [Link]([[7, 8, 9], [10, 11, 12]]) # 2x3
📖 DEFINITION
Calculus in ML is primarily about derivatives — measuring how fast a function changes. When a machine
learning model trains, it uses derivatives (via gradient descent) to find the settings that minimize its errors.
Without calculus, deep learning wouldn't exist.
💡 EXPLANATION (200-250 words)
Imagine you're blindfolded on a hilly landscape and want to reach the lowest valley (minimum error). You
take small steps downhill. Each step's direction is determined by the gradient — the derivative that tells you
which way is 'downhill'.
Key calculus concepts for ML:
• Derivative: Rate of change of f(x) with respect to x. Written as f'(x) or df/dx. If f(x) = x², then f'(x) = 2x.
At x=3, the slope is 6.
• Partial Derivative: Derivative of a function with multiple variables, holding others constant. Used when
your loss function has many parameters (weights in a neural network).
• Gradient: A vector of all partial derivatives. Points in the direction of steepest increase. We move in the
OPPOSITE direction to decrease the loss.
• Chain Rule: How to differentiate composite functions. If y = f(g(x)), then dy/dx = f'(g(x)) × g'(x). This is
how backpropagation works in neural networks — chaining derivatives backward through layers.
• Gradient Descent: Update rule: θ = θ - α × ∇L(θ). Here θ are parameters, α is the learning rate (step
size), and ∇L is the gradient of the loss.
📐 MATHEMATICAL FORMULAS
Derivative: f'(x) = lim[h→0] [f(x+h) - f(x)] / h
Power Rule: d/dx[xⁿ] = n × xⁿ⁻¹ e.g., d/dx[x³] = 3x²
Chain Rule: dy/dx = (dy/du) × (du/dx) for y=f(u), u=g(x)
Gradient: ∇f = [∂f/∂x₁, ∂f/∂x₂, ..., ∂f/∂xₙ]
Gradient Descent: θ_new = θ_old - α × ∂Loss/∂θ
MSE Loss: L = (1/n) Σ(yᵢ - ŷᵢ)² → ∂L/∂ŷ = -2(y-ŷ)/n
🎯 REAL INTERVIEW QUESTIONS & ANSWERS
Q1: Explain gradient descent in simple terms.
Answer: Gradient descent is an optimization algorithm that minimizes a loss function. Start with random
weights. Calculate the gradient (slope) of the loss function. Move parameters in the opposite direction of the
gradient by a small step (learning rate α). Repeat until the loss stops decreasing. Analogy: rolling a ball
downhill to find the valley (minimum error).
Q2: What happens if the learning rate is too high or too low?
Answer: Too HIGH → model overshoots the minimum, bounces around, may never converge (loss oscillates
or diverges). Too LOW → training takes forever, may get stuck in local minima or plateau. The right learning
rate (0.001–0.01 typically) is crucial. Use learning rate schedulers or Adam optimizer to adapt it
automatically.
Q3: What is the chain rule and why is it key to backpropagation?
Answer: Chain rule: d(f∘g)/dx = f'(g(x)) × g'(x). In a neural network, the loss is a composition of many
functions (layer after layer). Backpropagation applies the chain rule from the output layer backward to
compute ∂Loss/∂weight for every weight. Without the chain rule, we couldn't compute how each weight
contributed to the error.
⚠️ COMMON MISTAKES TO AVOID
• ❌ Setting learning rate too high — causes training to diverge instead of converge.
• ❌ Forgetting to apply chain rule for nested functions — will compute wrong gradient.
• ❌ Confusing gradient (vector) with derivative (scalar) — gradient is multi-dimensional.
• ❌ Not normalizing features before gradient descent — different scales cause uneven steps.
• ❌ Stopping too early (underfitting) or too late (overfitting) in gradient descent.
✅ WHEN TO USE vs ❌ WHEN NOT TO USE
✅ Gradient descent: training neural networks ❌ Don't use full batch GD on huge datasets — use
mini-batch
✅ Chain rule: backpropagation in deep learning ❌ Don't skip feature scaling before GD
✅ Partial derivatives: optimizing multi-parameter ❌ Don't use fixed high learning rate — use
models decay/scheduling
✅ Adam/RMSProp: adaptive learning rate variants ❌ Don't manually compute gradients — use
autograd (PyTorch/TF)
for i in range(n_steps):
gradient = 2 * x # derivative of x^2 = 2x
x = x - lr * gradient # gradient descent step
if i % 5 == 0:
print(f'Step {i:2d}: x={x:.4f}, f(x)={x**2:.4f}')
Stochastic GD (SGD) 1 sample each step Very fast but noisy updates
Adam Optimizer Adaptive learning rates Most popular in modern DL; often
default choice
📖 DEFINITION
Covariance measures how two variables change together — if one goes up, does the other go up too?
Correlation is a normalized version of covariance (scaled to [-1, 1]) that makes it easier to compare
relationships. These are foundational tools in EDA (Exploratory Data Analysis) and feature selection.
💡 EXPLANATION (200-250 words)
Imagine tracking two variables: hours studied (X) and exam score (Y). When X increases, does Y increase?
If yes, they have positive covariance/correlation. If Y decreases when X increases, they're negatively
correlated.
Correlation types:
• +1: Perfect positive correlation (move exactly together). Example: shoe size & height.
• -1: Perfect negative correlation (move exactly opposite). Example: temperature & hot cocoa sales.
• 0: No linear correlation. Example: shoe size & IQ score.
The problem with covariance: its value depends on the units of the variables, making comparison hard.
Correlation fixes this by dividing by the product of standard deviations, giving a unitless number in [-1,1].
IMPORTANT in DS: Correlation ≠ Causation. Ice cream sales and drowning rates are correlated (both peak
in summer), but ice cream doesn't cause drowning — summer weather is the confounding variable.
In ML: high correlation between two features = multicollinearity. This can hurt models like linear regression.
Feature selection removes redundant correlated features. A correlation heatmap is one of the first things you
make in EDA.
📐 MATHEMATICAL FORMULAS
Covariance: Cov(X,Y) = Σ[(Xᵢ-X̄ )(Yᵢ-Ȳ)] / (n-1)
Pearson Correlation: r = Cov(X,Y) / (σₓ × σᵧ) ∈ [-1, 1]
r > 0: positive correlation. r < 0: negative. r = 0: no linear
correlation
Spearman Rank Correlation: rs = 1 - [6Σdᵢ²] / [n(n²-1)] (for ranks)
🎯 REAL INTERVIEW QUESTIONS & ANSWERS
Q1: What is the difference between correlation and covariance?
Answer: Covariance measures direction and magnitude of the linear relationship between two variables, but
its value depends on units (hard to interpret). Correlation is covariance divided by product of SDs — giving a
standardized score in [-1,1] that's unit-free and easy to compare. Use covariance in math derivations; use
correlation for practical interpretation and comparison.
Q2: Two features have correlation = 0.95. What do you do?
Answer: 0.95 indicates very high multicollinearity. I would drop one of the features before training a linear
model, as keeping both adds noise without new information and can cause the model's coefficients to
become unstable. For tree-based models (Random Forest, XGBoost), multicollinearity matters less. I'd also
check VIF (Variance Inflation Factor) to confirm.
Q3: When would you use Spearman correlation over Pearson?
Answer: Spearman correlation is used when: (1) Data is not normally distributed, (2) There are outliers, (3)
The relationship is monotonic but not strictly linear, or (4) Data is ordinal (rankings). Pearson assumes
linearity and normality. Spearman is more robust and works on the ranks of data rather than raw values.
⚠️ COMMON MISTAKES TO AVOID
• ❌ Confusing correlation with causation — always the #1 statistics mistake in interviews!
• ❌ Using Pearson for non-normal or ordinal data — use Spearman instead.
• ❌ Interpreting r=0 as 'no relationship' — it means no LINEAR relationship; there could be a nonlinear
one.
• ❌ Ignoring multicollinearity in linear regression — it inflates coefficient variance.
• ❌ Not visualizing the scatter plot — always plot before relying on the correlation number.
✅ WHEN TO USE vs ❌ WHEN NOT TO USE
✅ Pearson: both variables are continuous and ❌ Don't assume causation from correlation
normal
✅ Spearman: ordinal data or non-normal ❌ Don't use Pearson with heavy outliers or skewed
distribution data
✅ Correlation heatmap: feature selection in EDA ❌ Don't remove correlated features from tree
models without testing
✅ Covariance matrix: PCA, Gaussian mixture ❌ Don't rely on correlation alone — always plot first
models
# Covariance matrix
cov_matrix = [Link](hours, scores) # 2x2 matrix
cov_xy = cov_matrix[0, 1] # Cov(X,Y)
# Pearson Correlation
r_pearson, p_val = pearsonr(hours, scores)
print(f'Pearson r = {r_pearson:.4f}, p = {p_val:.4f}')
# Spearman Correlation
r_spearman, _ = spearmanr(hours, scores)
print(f'Spearman r = {r_spearman:.4f}')
Kendall τ Concordant vs discordant pairs [-1, 1]; better for small samples
Mutual Info Any dependency (not just linear) [0, ∞]; used in feature selection
TOPIC 7 Hypothesis Testing Making data-driven decisions with confidence
📖 DEFINITION
Hypothesis testing is a statistical procedure to determine whether the observed data provides enough
evidence to reject a baseline assumption (called the null hypothesis). It answers: 'Is this result due to
chance, or is it a real pattern?' It's used to validate ML model improvements, A/B tests, and business
decisions.
💡 EXPLANATION (200-250 words)
Think of hypothesis testing like a courtroom: the null hypothesis (H₀) is 'innocent until proven guilty.' You
need strong evidence (low p-value) to reject innocence.
Step-by-step process:
1. State H₀ (null hypothesis): 'There is no effect / no difference.' e.g., 'Both versions of a website have
equal click rates.'
2. State H₁ (alternative hypothesis): 'There IS an effect.' e.g., 'Version B has a higher click rate than
Version A.'
3. Choose significance level α (usually 0.05 = 5%). This is your 'threshold of surprise.'
4. Collect data and calculate the test statistic (t-score, z-score, etc.)
5. Compute p-value: the probability of observing your data IF H₀ is true.
6. Decision: If p-value < α → Reject H₀ (result is statistically significant). If p-value ≥ α → Fail to reject
H₀.
p-value = 0.03 means: if the null hypothesis were true, there's only a 3% chance of seeing data this extreme.
Since 3% < 5% (α), we reject H₀.
Types of errors: Type I (False Positive): rejecting H₀ when it's actually true (α risk). Type II (False Negative):
failing to reject H₀ when H₁ is true (β risk).
📐 MATHEMATICAL FORMULAS
t-statistic (one sample): t = (x̄ - μ₀) / (s / √n)
t-statistic (two samples): t = (x̄ ₁ - x̄ ₂) / √(s₁²/n₁ + s₂²/n₂)
z-statistic: z = (x̄ - μ₀) / (σ / √n) [when σ known or n > 30]
Reject H₀ if: p-value < α OR |test_stat| > critical_value
Type I Error (α) = P(Reject H₀ | H₀ is true) [False Positive]
Type II Error (β) = P(Fail to reject H₀ | H₁ is true) [False
Negative]
Power of Test = 1 - β = P(correctly rejecting false H₀)
🎯 REAL INTERVIEW QUESTIONS & ANSWERS
Q1: What is a p-value? If p-value = 0.03 and α = 0.05, what do you conclude?
Answer: The p-value is the probability of observing results as extreme as the current data, assuming H₀ is
true. p=0.03 < α=0.05 → we reject H₀. The result is statistically significant. However, statistical significance
≠ practical significance. Always check effect size too.
Q2: What is the difference between Type I and Type II errors?
Answer: Type I (α): False Positive — you reject H₀ but it was actually true. Example: calling an innocent
person guilty. Type II (β): False Negative — you fail to reject H₀ but H₁ is true. Example: letting a guilty
person go free. In ML: Type I = false alarm (classifying normal as fraud), Type II = missed detection
(classifying fraud as normal). Reducing one typically increases the other.
Q3: You run an A/B test. When would you use a t-test vs a z-test?
Answer: Use z-test when: population SD is known OR n > 30 (CLT applies). Use t-test when: SD is unknown
AND n < 30 (small sample). In practice for A/B testing in tech companies, we almost always use the t-test
with Welch's correction (unequal variances) since population SD is never truly known.
⚠️ COMMON MISTAKES TO AVOID
• ❌ Interpreting p-value as the probability that H₀ is true — it is NOT. It's P(data | H₀ is true).
• ❌ p-hacking: running many tests until you get p < 0.05 — inflates false positive rate. Use Bonferroni
correction.
• ❌ Confusing statistical significance with practical significance — a tiny difference can be 'significant'
with a huge sample.
• ❌ Choosing one-tailed vs two-tailed test after seeing the data — decide before the experiment!
• ❌ Ignoring test assumptions — t-test assumes normality; check before applying.
✅ WHEN TO USE vs ❌ WHEN NOT TO USE
✅ t-test: compare means of two groups (A/B test) ❌ Don't use t-test with heavily skewed non-normal
data
✅ z-test: large samples (n>30) or known σ ❌ Don't run multiple tests without correction
(Bonferroni)
✅ Chi-squared: compare categorical frequencies ❌ Don't use p-value as the only decision metric
✅ ANOVA: compare means across 3+ groups ❌ Don't conclude causation from a significant result
print(f't-statistic: {t_stat:.4f}')
print(f'p-value: {p_value:.4f}')
📖 DEFINITION
Linear regression is a supervised ML algorithm that models the relationship between one or more input
features (X) and a continuous output (y) by fitting the best straight line (or hyperplane) through the data. It's
the simplest and most interpretable ML model — often the first one learned and frequently used as a
baseline.
💡 EXPLANATION (200-250 words)
Imagine you want to predict house prices based on size (sq ft). You have 100 data points (size, price).
Linear regression finds the line: Price = m × Size + b that comes 'closest' to all the data points.
How it finds the 'best' line:
• Calculate the residual (error) for each point: actual y minus predicted ŷ.
• Square each residual to make them positive and penalize large errors more.
• Sum all squared residuals: this is the Mean Squared Error (MSE) — the loss function.
• Ordinary Least Squares (OLS) minimizes MSE analytically using calculus (matrix solution) or gradient
descent.
Key metrics to evaluate regression:
• R² (R-squared): How much variance in y is explained by the model. R²=1 = perfect fit. R²=0 = model
explains nothing. Range: 0 to 1 (can be negative for bad models).
• MAE (Mean Absolute Error): Average absolute difference between actual and predicted.
• RMSE (Root Mean Squared Error): Square root of MSE. Same units as y. Penalizes large errors more
than MAE.
Assumptions: Linear relationship between X and y. Residuals are normally distributed. No multicollinearity.
Homoscedasticity (constant variance of residuals).
📐 MATHEMATICAL FORMULAS
Simple: ŷ = β₀ + β₁x (β₀=intercept, β₁=slope)
Multiple: ŷ = β₀ + β₁x₁ + β₂x₂ + ... + βₙxₙ = Xβ
OLS Solution: β = (XᵀX)⁻¹ Xᵀy (matrix form)
MSE: L = (1/n) Σ(yᵢ - ŷᵢ)²
MAE: (1/n) Σ|yᵢ - ŷᵢ|
RMSE: √[(1/n) Σ(yᵢ - ŷᵢ)²]
R²: 1 - [Σ(yᵢ-ŷᵢ)²] / [Σ(yᵢ-ȳ)²] = 1 - SS_res/SS_tot
🎯 REAL INTERVIEW QUESTIONS & ANSWERS
Q1: What are the assumptions of linear regression?
Answer: Four key assumptions: (1) Linearity — relationship between X and y must be linear. (2)
Independence — observations must be independent (no autocorrelation). (3) Homoscedasticity — residuals
have constant variance (no 'funnel' pattern in residual plot). (4) Normality of residuals — residuals should be
normally distributed. Violations lead to biased coefficients or incorrect standard errors.
Q2: What is the difference between R² and adjusted R²?
Answer: R² always increases when you add more features — even useless ones. Adjusted R² penalizes for
adding unnecessary features: Adj R² = 1 - [(1-R²)(n-1)/(n-k-1)] where k = number of features. Use Adjusted
R² for model selection to avoid overfitting by adding irrelevant variables.
Q3: What is the difference between Ridge and Lasso regression?
Answer: Both add a regularization penalty to linear regression to prevent overfitting. Ridge (L2) adds sum of
squared coefficients (β²) — shrinks all coefficients but keeps all features. Lasso (L1) adds sum of absolute
coefficients (|β|) — can shrink coefficients to exactly zero, performing automatic feature selection. Use
Lasso when you suspect many features are irrelevant.
⚠️ COMMON MISTAKES TO AVOID
• ❌ Using linear regression for non-linear relationships without transformation.
• ❌ Not checking residual plots for violation of assumptions.
• ❌ Using R² as the only metric — a high R² can still mean a bad model (overfitting).
• ❌ Not scaling features before applying Ridge/Lasso — regularization is scale-sensitive.
• ❌ Not splitting into train/test before evaluation — always evaluate on unseen data!
✅ WHEN TO USE vs ❌ WHEN NOT TO USE
✅ When relationship between X and y is linear ❌ When target is categorical (use logistic
regression)
✅ When interpretability matters (coefficients explain ❌ When relationship is strongly non-linear (use
impact) trees/NNs)
✅ Ridge when multicollinearity exists; Lasso for ❌ Without normalizing features for Ridge/Lasso
feature selection
# Linear Regression
model = LinearRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)
print(f'Intercept: {model.intercept_:.2f}')
print(f'Slope: {model.coef_[0]:.4f}')
print(f'R² Score: {r2_score(y_test, y_pred):.4f}')
print(f'RMSE: {[Link](mean_squared_error(y_test,y_pred)):.2f}')
Ridge (L2 Reg) Linear + penalty on β² — no When all features are relevant
zeroing out
Lasso (L1 Reg) Linear + penalty on |β| — zeroes Feature selection, sparse models
some β
Elastic Net Mix of Ridge + Lasso penalties Large feature set with correlated
features
Logistic Regression Binary target (0/1), outputs Spam detection, churn prediction
probability