UNIT 05 Machine Learning & Applied AI Track

Continuous Modeling: Regression Algorithms & Regularization

A mathematical and architectural derivation of Simple, Multiple, and Polynomial Linear Regression, Ordinary Least Squares (OLS) Normal Equations, Gradient Descent dynamics, Ridge (L_2), Lasso (L_1), Elastic Net penalty topologies, and rigorous metric evaluation.

01

Linear Regression: Formulations & Normal Equations

Linear Regression models the relationship between a continuous scalar dependent variable y in R and one or more independent predictor features x in R^d assuming a linear parametric structure with additive stochastic error.

1. Simple Linear Regression (Univariate)

For a single predictor x, the regression model is parameterized by slope w_1 and intercept w_0:

Target y_i = (Slope w₁ × Feature x_i) + Intercept w₀ + Noise ε_i, where noise follows a Normal Distribution(Mean=0, Variance=σ²)

Minimizing the sum of squared residuals yields the closed-form Ordinary Least Squares (OLS) solutions:

Slope w₁ = Covariance(X, Y) / Variance(X), Intercept w₀ = Mean of Y − (Slope × Mean of X)

2. Multiple Linear Regression & Matrix Formulation

When extended to d input features, each observation x_i = [xi1, xi2, dots, xid]^T is augmented with a dummy bias feature xi0 = 1:

Prediction ŷ = Intercept w₀ + (w₁×x₁ + w₂×x₂ + ... + w_d×x_d)  →  Matrix Form: y = Xw + ε

Where X in RN × (d+1) is the design matrix, w in R(d+1) is the parameter vector, and y in R^N is the target response vector.

3. The Normal Equation Derivation

The objective is to minimize the Residual Sum of Squares (RSS):

Formulation: J(w) = (1 / 2) Xw - y₂² = (1 / 2) (Xw - y)^T (Xw - y)
Expanding the quadratic form and computing the gradient with respect to parameter vector w:
Formulation: ∇w J(w) = X^T (Xw - y) = X^TXw - X^Ty
Setting the gradient strictly to zero (∇w J(w) = 0) establishes the Normal Equation:

OLS Normal Equation: Optimal Weights w* = (Xᵀ × X)⁻¹ × Xᵀ × y
Geometric Interpretation of Ordinary Least Squares (OLS)
Target Vector y ∈ ℝᴺ
Lives in N-dimensional sample space. Generally does not lie in the column space of X.
⇒
Orthogonal Projection ŷ = Xw*
The closest point in subspace col(X) to y. Residual vector e = y - y_predicted is perpendicular to col(X) (X^T e = 0).
⇒
Hat Matrix H = X(XᵀX)⁻¹Xᵀ
Orthogonal projection operator mapping y → y_predicted = Hy. Symmetric (H=H^T) and idempotent (H^2 = H).
02

Polynomial Regression: Non-Linear Basis Expansions

Real-world relationships frequently exhibit non-linear curvature (e.g., accelerating aerodynamic drag, diminishing marginal returns). Polynomial Regression captures non-linear trends by projecting input features into a higher-dimensional polynomial feature space:

Polynomial Expansion: Feature Vector = [1, x, x², x³, ..., xᵖ]  →  Prediction ŷ = wᵀ × Feature Vector = w_0 + w_1 x + w_2 x^2 + ... + w_p x^p
📐 Why is Polynomial Regression still considered a "Linear" Model?

In statistical machine learning, linearity is defined with respect to the unknown parameters w, NOT the input variables x. Because the prediction equation y_predicted = Sum j=0^p w_j φ_j(x) is a linear combination of weights w, the cost function remains strictly quadratic and convex, allowing exact optimization via standard linear OLS and gradient descent.

⚠️ Runge’s Phenomenon & The Curse of Dimensionality

As polynomial degree p increases, feature count explodes combinatorially across interactions (binom{d+p}{p}). High-degree polynomials oscillate wildly near interval boundaries (Runge's phenomenon), causing severe overfitting and necessitating regularization.

03

Cost Functions & Gradient Descent Dynamics

While the analytical Normal Equation w^* = (X^TX)-1X^Ty is exact, inverting the (d+1) × (d+1) matrix has a computational complexity of O(d^3). When features exceed d > 10,000 (e.g., text representations, genomics), matrix inversion becomes computationally intractable. We employ Gradient Descent.

J(w) = (1 / 2N)Sum_{i=1}^N ( w^Tx_i - y_i )^2 → ∇_{w} J(w) = (1 / N)X^T (Xw - y)
w_{t+1} = w_t - α ∇_{w} J(w_t) = w_t - (α / N)X^T (Xw_t - y)
Gradient Descent Variant Batch Size Per Step Computational Efficiency Convergence Properties
Batch Gradient Descent All N training samples Slow on large N; memory intensive Smooth, monotonic decrease directly to global minimum.
Stochastic Gradient Descent (SGD) Single random sample (N=1) Extremely fast; minimal memory footprint Noisy, erratic updates; can jump out of shallow local minima.
Mini-Batch Gradient Descent Mini-batch (e.g., B in [32, 256]) Optimal GPU vectorized SIMD parallelism Standard industry compromise: stable, fast, robust convergence.
04

Regularized Regression: Ridge, Lasso & Elastic Net

When features are correlated (multicollinearity) or exceed the sample size (d gg N), the matrix X^TX becomes singular or ill-conditioned (det(X^TX) ≈ 0). Inverting an ill-conditioned matrix causes weight estimates to explode to astronomical positive and negative magnitudes, yielding high variance. Regularization penalizes large weight magnitudes.

Ridge Regression (L_2 Penalty)

Formulation: JRidge(w) = MSE(w) + λ w₂² = MSE + λ Sum j=1^d w_j^2
Shrinks weights continuously towards zero without setting them exactly to zero. Closed-form solution:
Formulation: wRidge = (X^TX + λ I)-1X^Ty
Adding λ I guarantees invertibility by forcing all eigenvalues to be strictly positive.

Lasso Regression (L_1 Penalty)

Formulation: JLasso(w) = MSE(w) + λ w₁ = MSE + λ Sum j=1^d w_j
Enforces sparsity by driving coefficients of non-informative features strictly to zero (w_j = 0). Acts as an automatic embedded feature selector.

Elastic Net (Convex Hybrid)

Formulation: JEN(w) = MSE + λ [ ρ w₁ + (1 - ρ / 2) w₂² right]
Combines the sparsity of Lasso with the grouping effect of Ridge. When multiple features are strongly correlated, Lasso arbitrarily selects one; Elastic Net retains and shrinks the entire correlated group together.

The Geometry of Sparsity: Why Lasso Sets Weights Exactly to Zero
L₂ Norm: Ridge Constraint (Sphere / Circle)
Constraint surface w_1^2 + w_2^2 ≤ C is smooth and round.
Elliptical MSE contours intersect along curves where both w_1 ≠ 0 and w_2 ≠ 0.
Coefficients shrink smoothly, never hitting zero.
↔
L₁ Norm: Lasso Constraint (Diamond / Polytope)
Constraint surface |w_1| + |w_2| ≤ C has sharp corners on coordinate axes.
Expanding MSE error ellipses typically touch the sharp corners first.
At any axis corner, orthogonal weights are strictly zero (w_j = 0).
05

Regression Evaluation Metrics & Adjusted R²

Evaluation Metric Mathematical Formulation Optimal Value Critical Trade-Off & Architectural Use
Mean Absolute Error (MAE) (1 / N)Sum i=1^N |y_i - y_predicted_i| 0.0 Linear penalty. Robust to outliers; reflects median absolute error in natural target units.
Mean Squared Error (MSE) (1 / N)Sum i=1^N (y_i - y_predicted_i)^2 0.0 Differentiable loss function; heavily penalizes rare catastrophic errors quadratically.
Root Mean Squared Error (RMSE) √(MSE) 0.0 Standard industry benchmark; expresses quadratic error in identical physical units as y.
R-Squared (R^2) 1 - (Sum (y_i - y_predicted_i)^2 / Sum (y_i - Mean_y)^2) 1.0 Proportion of variance explained. Flawed: Monotonically increases when adding useless features.
Adjusted R-Squared (R^2adj) 1 - [ ((1 - R^2)(N - 1) / N - d - 1) right] 1.0 Penalizes addition of non-informative features d. Increases only if new feature improves model beyond chance.
🚨 The Fundamental Mathematical Fallacy of Raw R²

In Ordinary Least Squares, adding any feature to design matrix X—even pure Gaussian random white noise—will never decrease raw R^2. Every additional column expands the subspace col(X), allowing OLS to project y slightly closer to the training points. Adjusted R^2 corrects for degrees of freedom (N - d - 1). If a feature does not reduce unexplained variance by more than (1 / N - d - 1), Adjusted R^2 declines.

06

Verified Python Implementation: The Complete Regression Suite

The following standalone script demonstrates the Normal Equation vs. Scikit-learn OLS, Polynomial Regression, Ridge, Lasso sparsity, Elastic Net, and comprehensive metric calculations:

regression_algorithms_suite.py
# =====================================================================
# DevAscent Academy: Verified Comprehensive Regression Algorithms Suite
# Demonstrating: Analytical Normal Equation, Ridge, Lasso, Elastic Net,
#                and Complete Metric Calculations (MSE, RMSE, MAE, R2, Adj R2)
# =====================================================================

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

def compute_regression_metrics(y_true, y_pred, n_features):
    """Calculates all essential regression performance metrics."""
    n_samples = len(y_true)
    mae = mean_absolute_error(y_true, y_pred)
    mse = mean_squared_error(y_true, y_pred)
    rmse = np.sqrt(mse)
    r2 = r2_score(y_true, y_pred)
    
    # Adjusted R2 Formula: 1 - [(1 - R2) * (N - 1) / (N - d - 1)]
    adj_r2 = 1.0 - ((1.0 - r2) * (n_samples - 1.0) / (n_samples - n_features - 1.0))
    
    return {
        "MAE": mae,
        "MSE": mse,
        "RMSE": rmse,
        "R2": r2,
        "Adj_R2": adj_r2
    }

def run_analytical_vs_sklearn_ols():
    print("=== 1. ANALYTICAL NORMAL EQUATION VS SCIKIT-LEARN OLS ===")
    np.random.seed(42)
    N, d = 150, 3
    X = np.random.randn(N, d)
    true_weights = np.array([3.5, -2.0, 1.8])
    true_bias = 4.2
    y = X @ true_weights + true_bias + np.random.normal(0, 0.5, size=N)

    # 1. Analytical Normal Equation: w* = (X_aug^T @ X_aug)^(-1) @ X_aug^T @ y
    X_aug = np.hstack([np.ones((N, 1)), X])
    w_analytical = np.linalg.inv(X_aug.T @ X_aug) @ X_aug.T @ y
    
    # 2. Scikit-Learn LinearRegression
    lr = LinearRegression(fit_intercept=True)
    lr.fit(X, y)
    
    print(f"Analytical Weights: Intercept={w_analytical[0]:.4f}, Slopes={w_analytical[1:].round(4)}")
    print(f"Scikit-Learn OLS:   Intercept={lr.intercept_:.4f}, Slopes={lr.coef_.round(4)}")
    print(f"Verification Match: {np.allclose(w_analytical, np.insert(lr.coef_, 0, lr.intercept_))}\n")

def run_regularization_comparison():
    print("=== 2. REGULARIZATION SUITE: OLS VS RIDGE VS LASSO VS ELASTIC NET ===")
    np.random.seed(42)
    # Generate dataset with 10 features, but only 3 are truly informative
    N, d = 200, 10
    X = np.random.randn(N, d)
    true_w = np.array([5.0, -3.5, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
    y = X @ true_w + 2.5 + np.random.normal(0, 1.2, size=N)

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

    # Scale features (mandatory for regularized models)
    scaler = StandardScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)

    # Initialize models
    models = {
        "OLS (Unregularized)": LinearRegression(),
        "Ridge (L2, alpha=2.0)": Ridge(alpha=2.0),
        "Lasso (L1, alpha=0.35)": Lasso(alpha=0.35),
        "ElasticNet (L1+L2, alpha=0.4, l1_ratio=0.5)": ElasticNet(alpha=0.4, l1_ratio=0.5)
    }

    results = []
    coefficients = {}

    for name, model in models.items():
        model.fit(X_train_scaled, y_train)
        y_pred = model.predict(X_test_scaled)
        metrics = compute_regression_metrics(y_test, y_pred, n_features=d)
        metrics["Model"] = name
        results.append(metrics)
        coefficients[name] = model.coef_

    results_df = pd.DataFrame(results)[["Model", "MAE", "RMSE", "R2", "Adj_R2"]]
    print(results_df.to_string(index=False))

    print("\n=== Learned Feature Coefficients (Checking Sparsity) ===")
    coef_df = pd.DataFrame(coefficients, index=[f"Feat_{i+1}" for i in range(d)])
    print(coef_df.round(4))
    print("\nInsight: Notice how Lasso drives non-informative features 4-10 strictly to 0.0000!")

if __name__ == "__main__":
    run_analytical_vs_sklearn_ols()
    run_regularization_comparison()
07

Viva Voce & Machine Learning Interview Questions

Q1 • OLS Theory State the Gauss-Markov Theorem and explain what BLUE stands for in Linear Regression.
The Gauss-Markov Theorem states that in a linear regression model where the errors have expectation zero (E[ε] = 0), are uncorrelated (Cov(ε_i, ε_j) = 0), and have constant homoscedastic variance (Var(ε_i) = σ^2), the Ordinary Least Squares estimator w^* = (X^TX)-1X^Ty is BLUE: Best Linear Unbiased Estimator. "Best" means that among all possible linear unbiased estimators, OLS achieves the strictly minimum sampling variance (smallest possible parameter covariance matrix). Notably, this theorem holds without requiring the errors to follow a normal distribution.
Q2 • Matrix Mechanics Why does the Ordinary Least Squares Normal Equation fail when two features exhibit perfect multicollinearity?
When two features are collinear (e.g., x_2 = 2.5 x_1), the columns of the design matrix X are linearly dependent. Consequently, the Gram matrix X^TX in R(d+1) × (d+1) is rank-deficient (rank(X^TX) < d+1) and its determinant is strictly zero (det(X^TX) = 0). A matrix with zero determinant is singular and has no inverse (X^TX)-1, rendering the analytical Normal Equation undefined. Geometrically, infinitely many weight vectors achieve the exact same minimum RSS. Ridge Regression resolves this by adding λ I, which shifts all eigenvalues by +λ, guaranteeing non-zero determinant and invertibility.
Q3 • Optimization Why is Feature Scaling strictly mandatory prior to training Ridge, Lasso, and Elastic Net regression models?
In regularized regression, the penalty term λ Sum w_j^2 or λ Sum |w_j| applies an identical, uniform penalty multiplier across all model weights. If Feature A has a tiny numerical scale (e.g., salary in Millions: 0.05 to 0.5) and Feature B has a massive scale (e.g., credit score: 300 to 850), Feature A requires an enormous weight coefficient to produce a meaningful prediction, while Feature B requires a tiny weight. The regularizer will aggressively penalize Feature A simply because of arbitrary physical units, shrinking its coefficient toward zero regardless of true predictive importance. Standardization (μ=0, σ=1) puts all features on equal footing so regularization reflects true predictive contribution.
Q4 • Regularization Mathematically explain why Lasso (L_1) produces sparse solutions (weights =0) while Ridge (L_2) only shrinks weights toward zero.
In Lagrangian form, optimization is equivalent to minimizing MSE subject to a norm budget constraint. The L_2 budget region is a smooth hypersphere (Sum w_j^2 ≤ C). The gradient of the L_2 penalty is 2λ w_j, which diminishes proportionally as w_j → 0 (decaying to zero as the weight shrinks). In contrast, the L_1 budget region is a hyper-rhombus (polytope) with sharp corners positioned exactly on the coordinate axes where orthogonal weights are zero (w_j = 0). Furthermore, the derivative of |w_j| is the sign function sign(w_j), which exerts a constant penalty force ± λ regardless of how small w_j becomes, pulling the parameter all the way to zero and locking it there.
Q5 • Metrics Why can Adjusted R^2 decrease when adding a new feature, whereas raw R^2 can only increase or stay constant?
Raw R^2 = 1 - (SSres / SStot). In OLS, adding any feature column expands the column space of X; because projection onto a larger subspace can never increase distance to target y, SSres can never increase, so raw R^2 artificially creeps upward even for random noise. Adjusted R^2 penalizes degrees of freedom:
Formulation: R^2adj = 1 - [ ((1 - R^2)(N - 1) / N - d - 1) right]
When a new feature is added (d → d + 1), the denominator (N - d - 1) decreases, which scales up the penalty term. Unless the reduction in (1 - R^2) outweighs this penalty scaling factor, Adjusted R^2 will decline, alerting engineers that the new feature adds more model complexity than predictive power.
Q6 • Elastic Net In what practical scenario does Elastic Net vastly outperform both Ridge and Lasso?
Elastic Net outperforms both models in two critical enterprise scenarios:
  1. High-Dimensional Regimes (d > N): Lasso can select at most N features before becoming saturated, whereas Elastic Net can select all relevant features.
  2. Highly Correlated Feature Clusters: When a dataset contains clusters of collinear features (e.g., genes in the same biological pathway, or multiple macroeconomic price indexes), Lasso arbitrarily selects one feature from the cluster and zeros out the rest, producing unstable, non-reproducible feature sets. Elastic Net's L_2 component provides a grouping effect, shrinking the correlated group together while its L_1 component removes entirely unrelated features.
Q7 • Loss Functions When should an engineer evaluate a model using Mean Absolute Error (MAE) instead of Root Mean Squared Error (RMSE)?
MAE should be prioritized when the empirical dataset contains unavoidable measurement anomalies or heavy-tailed outliers that should not unduly dictate the model's global parameters. Because RMSE squares each error term before averaging ((y - y_predicted)^2), a single extreme prediction outlier (e.g., an error of 100 vs typical error of 2) contributes 10,000 to the sum, dominating the objective function and distorting the regression line away from typical inliers. MAE penalizes all errors linearly (|y - y_predicted|), providing a median-centric, robust measure of expected error in true problem domain units.
Q8 • Calculus Derive the partial derivative of the OLS Mean Squared Error with respect to parameter vector w.
Express MSE in matrix notation: J(w) = (1 / 2N) (Xw - y)^T (Xw - y). Expanding:
Formulation: J(w) = (1 / 2N) Big( w^TX^TXw - w^TX^Ty - y^TXw + y^Ty Big)
Since w^TX^Ty is a scalar, its transpose equals itself: w^TX^Ty = y^TXw. Thus:
Formulation: J(w) = (1 / 2N) Big( w^T(X^TX)w - 2y^TXw + y^Ty Big)
Applying matrix calculus identities ∇w(w^TAw) = 2Aw (for symmetric A = X^TX) and ∇w(b^Tw) = b:
Formulation: ∇w J(w) = (1 / 2N) Big( 2X^TXw - 2X^Ty Big) = (1 / N) X^T(Xw - y)