Category: Easy

  • ML0095 Confidence Interval

    What is a confidence interval, and what is the correct interpretation of a 95% confidence interval, as used in sequential A/B testing and experimentation platforms?

    Answer

    A confidence interval is a range computed from sample data that is designed to contain an unknown population parameter with a specified probability under repeated sampling. The correct frequentist interpretation of a 95% confidence interval is: if you repeated the experiment many times, computing a new interval from each sample, about 95% of those intervals would contain the true parameter. It is a statement about the procedure, not about any single interval. Once data is observed, the true parameter is either inside your specific interval or not (probability 0 or 1); you cannot say “there is a 95% probability the parameter is in this interval,” because that is a Bayesian credible interval interpretation requiring a prior. Sequential A/B testing platforms use “anytime-valid” confidence sequences that maintain this coverage guarantee across continuous monitoring, and asymmetric experiment designs treat confidence interval width as a design parameter controlled through sample allocation between test and control groups.

    (1) Procedure vs Single Interval: the 95% refers to the long-run coverage rate of the interval-construction procedure across hypothetical repeated samples, not the probability that the true parameter is in any one observed interval.
    (2) Frequentist vs Bayesian: saying “95% probability the parameter is in this interval” is a Bayesian credible interval statement; a 2024 paper showed this distinction has real business impact, as frequentist estimators overstate A/B test effect sizes (winner’s curse) while Bayesian estimators with correct priors do not.
    (3) Production Use: anytime-valid confidence sequences enable canary testing so the 95% coverage holds throughout continuous monitoring, not just at a pre-specified endpoint; asymmetric experiment designs optimize CI width by nonlinearly increasing control group size when shrinking test group size (40% test reduction requires 3x control increase).

    Twenty horizontal confidence intervals computed from repeated samples, with a vertical line at the true parameter value; about 19 of 20 intervals cross the true value while one misses it, illustrating 95% coverage

    Figure 1: The frequentist interpretation: across 20 repeated experiments, about 19 of the 95% confidence intervals contain the true parameter (vertical line); the 95% is a property of the procedure, not of any single interval.

    The width of a confidence interval depends on three things: the confidence level (higher confidence means wider intervals), the sample size (larger n means narrower intervals), and the variability of the data (higher variance means wider intervals). For a mean with known variance, the 95% CI is the sample mean plus or minus 1.96 standard errors. When the variance is unknown and estimated from the sample, the t-distribution replaces the normal, giving slightly wider intervals for small samples. A critical production issue is peeking: repeatedly checking whether a confidence interval excludes zero and stopping when it does inflates the false positive rate, because each check is a separate test. Anytime-valid confidence sequences solve this by using a test statistic whose coverage guarantee holds uniformly over time, so you can monitor continuously without breaking the 95% promise. Conformal prediction (2024-2025) extends the idea to distribution-free prediction intervals for ML models, providing finite-sample coverage without normality assumptions.

    Mathematical Formulation:
    \bar{x} \pm z_{\alpha/2}\, \frac{\sigma}{\sqrt{n}}
    P\!\left(\bar{X} - z_{\alpha/2}\frac{\sigma}{\sqrt{n}} \leq \mu \leq \bar{X} + z_{\alpha/2}\frac{\sigma}{\sqrt{n}}\right) = 1 - \alpha

    Where:

    • \bar{x} is the sample mean, \sigma is the population standard deviation (replaced by sample standard deviation s with the t-distribution when unknown), and n is the sample size.
    • z_{\alpha/2} is the critical value of the standard normal at level \alpha/2; for 95% confidence, \alpha = 0.05 and z_{0.025} = 1.96.
    • The probability statement is over the random interval (which depends on \bar{X}), not over the fixed parameter \mu; this is the frequentist distinction. The interval either covers \mu or not for any single sample, but the procedure covers it 1 - \alpha of the time across repeated samples.
    ConceptFrequentist CIBayesian Credible Interval
    Interpretation95% of repeated-sample intervals contain the true value95% probability the parameter is in this interval
    Requires Prior?NoYes (prior over the parameter)
    Parameter IsFixed, unknown constantRandom variable with a distribution
    Production NoteFrequentist: winner’s curse overstates effect sizesBayesian: correct priors avoid winner’s curse

    Login to view more content
  • ML0091 Naive Bayes Types

    What are the different types of Naive Bayes classifiers (Gaussian, Multinomial, Bernoulli), and when is each appropriate for tasks like Microsoft’s spam filtering or medical diagnosis?

    Answer

    The three main variants of Naive Bayes differ in the distribution they assume for each feature given the class, and the choice is driven by the feature type. Gaussian Naive Bayes assumes each continuous feature follows a normal distribution per class, estimated by the class-conditional mean and variance; it suits real-valued features like medical lab values or sensor readings. Multinomial Naive Bayes models integer feature counts drawn from a multinomial distribution, and in practice TF-IDF weights are often substituted even though they are not counts; it is the standard for text classification, used in Microsoft’s spam filtering training module and production review-spam detection processing 85,000 reviews per day. Bernoulli Naive Bayes models binary presence/absence indicators with a Bernoulli distribution; it suits boolean features like “contains word X” or “has flag Y” and explicitly accounts for the absence of a feature, which Multinomial NB does not.

    (1) Gaussian NB (Continuous): estimates \mu_{yj} and \sigma_{yj}^2 per class per feature; best for real-valued features like blood pressure, age, or sensor readings; assumes normality, so skewed or multi-modal features need transformation or kernel density estimation.
    (2) Multinomial NB (Count Data): estimates P(w_j \mid y) = (N_{yj} + \alpha) / (N_y + \alpha d); the standard for text classification on word counts, and commonly run on TF-IDF weights as a practical approximation; Microsoft’s training module uses it for email spam filtering, and a production system classifies 85,000 reviews/day at AUC above 0.99.
    (3) Bernoulli NB (Binary Presence): estimates P(x_j = 1 \mid y) and explicitly models P(x_j = 0 \mid y); best for short documents or boolean feature sets where absence is informative; scikit-learn ships it alongside categorical and complement variants, and the R naivebayes package adds a Poisson variant for count features.

    Three panels: Gaussian NB shows bell curves per class for a continuous feature; Multinomial NB shows a bar chart of word count probabilities per class; Bernoulli NB shows a binary presence/absence table with P(x=1|y) and P(x=0|y)

    Figure 1: The three Naive Bayes variants: Gaussian models continuous features as per-class normal distributions, Multinomial models count data as a multinomial over a vocabulary, and Bernoulli models binary presence/absence with explicit absence probabilities.

    The choice between Multinomial and Bernoulli for text depends on document length and whether absence carries information. Multinomial NB uses word frequencies, so a word appearing three times contributes three times the evidence; Bernoulli NB uses only presence, so “free” appearing once or ten times contributes the same amount, but the absence of “free” also contributes evidence. For long documents where frequency matters (emails, articles), Multinomial is standard. For short documents where presence is the signal (search queries, short reviews), Bernoulli can match or beat Multinomial. A common production pattern is TF-IDF weighting with Multinomial NB, as in the Banking77 support ticket router that classifies 13,000 banking queries into 77 intents in under 1 ms per ticket. For mixed feature types (continuous plus categorical plus binary), scikit-learn does not natively combine variants, so practitioners either discretize continuous features or use the R naivebayes package which supports mixed distributions including kernel density estimation.

    Mathematical Formulation:
    P(x_j \mid y) = \frac{1}{\sqrt{2\pi\sigma_{yj}^2}} \exp\!\left(-\frac{(x_j - \mu_{yj})^2}{2\sigma_{yj}^2}\right)
    P(x_j \mid y) = \frac{N_{yj} + \alpha}{N_y + \alpha\, d}
    P(x_j \mid y) = \theta_{yj}^{x_j}(1 - \theta_{yj})^{1 - x_j}

    Where:

    • The first equation is Gaussian NB: \mu_{yj} and \sigma_{yj}^2 are the mean and variance of feature j in class y, estimated from training data.
    • The second equation is Multinomial NB: N_{yj} is the count of feature j in class y, N_y is the total count, \alpha is Laplace smoothing, and d is the vocabulary size.
    • The third equation is Bernoulli NB: \theta_{yj} = P(x_j = 1 \mid y) is the probability that feature j is present in class y; the term (1 - \theta_{yj})^{1 - x_j} explicitly models the absence of the feature.
    VariantFeature TypeDistributionBest For
    Gaussian NBContinuous (real-valued)Normal per classMedical diagnosis, sensor data, Iris dataset
    Multinomial NBCounts or TF-IDFMultinomialEmail spam, review classification, ticket routing
    Bernoulli NBBinary presence/absenceBernoulliShort text, boolean features, spam with word presence

    Login to view more content
  • ML0090 Naive Bayes Classifier

    Explain the Naive Bayes classifier and the “naive” conditional independence assumption behind it, as used in production spam detection and support ticket routing.

    Answer

    Naive Bayes is a probabilistic classifier that applies Bayes’ theorem with a “naive” assumption: given the class label, all features are conditionally independent. This lets the classifier multiply per-feature likelihoods instead of estimating the full joint distribution, which would require exponential data. Despite the assumption being almost always violated in practice, Naive Bayes trains in O(nd) time, predicts in O(d) time, and performs surprisingly well on text classification, spam filtering, and support ticket routing. A production case study processes 85,000 product reviews per day with Multinomial Naive Bayes, achieving AUC above 0.99 for spam detection while retraining in under 5 minutes on a single-core VM, and a TF-IDF plus Multinomial NB pipeline routes banking support tickets in under 1 millisecond per ticket on the Banking77 dataset.

    (1) Bayes’ Theorem Applied: the classifier computes the posterior P(y \mid x) for each class and picks the argmax; the naive assumption factors the likelihood P(x_1, \ldots, x_d \mid y) into a product of one-dimensional terms \prod P(x_j \mid y).
    (2) Why the Assumption Works Anyway: even when the independence assumption is wrong, the argmax decision is often correct because correlated features push the posterior in the same direction; the calibration is off but the ranking is not, which is why scikit-learn recommends CalibratedClassifierCV with isotonic regression for reliable probabilities.
    (3) Production Strengths: trains in milliseconds, predicts in microseconds, needs no GPU, produces interpretable per-feature contributions, and a 2024 Generalized Naive Bayes paper showed that relaxing the independence assumption via optimal structure learning improves accuracy while keeping the computational profile.

    Two panels: left shows the full joint distribution P(x1,x2,x3|y) as a large table requiring exponential data; right shows the naive factorization as three small per-feature tables P(x1|y), P(x2|y), P(x3|y) multiplied together

    Figure 1: The naive independence assumption factorizes the intractable joint likelihood P(x1, x2, x3 | y) into a product of one-dimensional per-feature likelihoods, replacing an exponential-size table with d small tables.

    The independence assumption is the single most important thing to understand about Naive Bayes, because it is both its source of speed and its source of error. When two features are highly correlated (e.g., “free” and “gift” both appearing in spam), the classifier double-counts their evidence, pushing the posterior toward 0 or 1 more aggressively than the true joint distribution would. This is why GaussianNB in scikit-learn tends to produce overconfident probabilities and why calibration is recommended. The assumption also means Naive Bayes cannot capture feature interactions; a 2024 paper on Generalized Naive Bayes (GNB) addresses this by learning an optimal dependency structure among features while preserving the efficient factorization, proving the approximation is at least as good as classical Naive Bayes. For text specifically, the classic remedies are TF-IDF weighting, document-length normalization, and complement Naive Bayes, introduced by Rennie et al. to correct the multinomial model’s mismatch with real text. In practice, the speed-accuracy trade-off favors Naive Bayes for high-volume, low-latency text tasks where a transformer would be overkill.

    Mathematical Formulation:
    \hat{y} = \arg\max_{y}\; P(y) \prod_{j=1}^{d} P(x_j \mid y)
    P(x_j \mid y) = \frac{N_{yj} + \alpha}{N_y + \alpha\, d}

    Where:

    • \hat{y} is the predicted class; P(y) is the class prior, and the product of per-feature likelihoods replaces the intractable joint P(x_1, \ldots, x_d \mid y) under the naive conditional independence assumption.
    • The second equation is the smoothed estimator for discrete (count) features: N_{yj} is the count of feature j in class y, N_y is the total count for class y, d is the vocabulary size, and \alpha is the Laplace smoothing parameter (typically 1) that prevents zero probabilities for unseen feature-class combinations. Continuous features use a density instead, such as a per-class Gaussian.
    • The argmax is computed in log space to avoid underflow: \log P(y) + \sum_j \log P(x_j \mid y), which is a sum of precomputed log-likelihoods and runs in O(d) per prediction.
    PropertyNaive BayesLogistic Regression
    Training CostO(nd) single passO(nd) per iteration, multiple iterations
    Key AssumptionConditional independence of features given classNo independence assumption; linear decision boundary
    CalibrationOverconfident; needs isotonic calibrationWell-calibrated by default
    InterpretabilityPer-feature log-likelihood contributionsPer-feature weights (coefficients)
    Best ForHigh-volume text, spam, ticket routingGeneral tabular classification with calibrated probabilities

    Login to view more content
  • ML0085 Covariate Label Concept Drift

    What is the difference between covariate shift, label shift, and concept drift?

    Answer

    All three are ways the joint distribution P(X, Y) at serving time can differ from training time, and they differ in which factor moved. Covariate shift: the input distribution P(X) changes while the labeling rule P(Y|X) stays fixed (your users got older, but age still means the same thing for risk). Label shift: the class prior P(Y) changes while the class-conditional input P(X|Y) stays fixed (fraud rate doubles in a crisis, but fraud still looks the same). Concept drift: the labeling rule P(Y|X) itself changes (the definition of spam evolves), so the model’s boundary is simply wrong now. Detection and repair differ per type, which is why the taxonomy matters operationally.

    (1) Covariate Shift: P(X) moves, P(Y|X) fixed; visible in input statistics alone, and importance reweighting (weighting training points by density ratio) is the classical correction.
    (2) Label Shift: P(Y) moves, P(X|Y) fixed; invisible in per-feature input stats if classes look alike, but visible in the model’s output distribution, and corrected by reweighting with estimated new priors.
    (3) Concept Drift: P(Y|X) moves; no input-only monitor can see it in general, so you need ground truth (delayed labels) or estimation methods, and the only real fix is retraining on the new concept.

    Three panels: covariate shift moves the input cloud while the boundary stays; label shift changes class proportions; concept drift rotates the boundary itself

    Figure 1: The three drift types: covariate shift relocates the inputs (boundary still valid), label shift reweights class frequencies, and concept drift moves the true boundary, which invalidates the model even if inputs look unchanged.

    The operational asymmetry is the interview-worthy insight. Input-side monitors (feature histograms, PSI, KS tests) reliably catch covariate shift and obvious label shift, because those live in P(X) and in the output distribution. But a pure concept drift can leave every input statistic untouched while accuracy collapses, which is why production monitoring stacks pair input-drift detectors with performance estimation or delayed ground-truth evaluation. NannyML’s CBPE documentation states exactly this split: confidence-based performance estimation stays accurate under covariate shift but cannot detect concept drift without labels.

    Mathematical Formulation:
    P(X, Y) = P(Y \mid X)\,P(X) = P(X \mid Y)\,P(Y)

    Where:

    • X is the input and Y the target; the joint can be factorized in two directions.
    • Covariate shift: P_{tr}(X) \neq P_{te}(X) while P(Y \mid X) is unchanged.
    • Label shift: P_{tr}(Y) \neq P_{te}(Y) while P(X \mid Y) is unchanged; concept drift: P(Y \mid X) itself changes (subscripts tr/te denote training vs serving).
    TypeWhat ChangesExampleDetection Signal
    Covariate ShiftP(X); boundary P(Y|X) intactUser base ages; medical device sees older patientsInput stats (PSI/KS per feature)
    Label ShiftP(Y); class appearance P(X|Y) intactFraud rate spikes during a holiday seasonOutput/prediction distribution shift
    Concept DriftP(Y|X); the rule itself movesSpammers change tactics; same features, new meaningDelayed labels; invisible to input-only monitors

    Login to view more content
  • ML0080 Decision Tree Pruning

    Why do decision trees need pruning, and how is it done?

    Answer

    An unconstrained decision tree grows until every leaf is pure, which on real data means it memorizes noise: training accuracy hits 100% while test accuracy sags. Pruning is the cure, and it comes in two forms. Pre-pruning stops growth early with rules like maximum depth, minimum samples per leaf, or minimum impurity decrease. Post-pruning grows the full tree first, then cuts back the branches whose complexity is not repaid by fit, using a validation-based criterion such as cost-complexity pruning. Post-pruning is usually stronger because a split that looks useless alone may unlock a highly valuable subtree below it; pre-pruning can never see that.

    (1) Why Trees Overfit: depth adds one axis-aligned cut per level, so a deep tree carves the training set into tiny pure cells, which is high variance by construction.
    (2) Pre- vs Post-Pruning: pre-pruning is cheap but myopic (it can miss XOR-style interactions); post-pruning explores the full tree and then removes only the provably weakest links.
    (3) Production Default: scikit-learn implements minimal cost-complexity pruning via the ccp_alpha parameter: its documented example shows an unpruned tree at 100% train / 88% test accuracy, while sweeping the pruning path finds ccp_alpha near 0.015 maximizing test accuracy.

    Train and test accuracy versus ccp_alpha: train accuracy falls monotonically, test accuracy rises to a peak near alpha 0.015 then falls, marking the bias-variance sweet spot

    Figure 1: The pruning path: as ccp_alpha grows, more of the tree is cut, training accuracy declines, and test accuracy peaks where complexity is repaid by generalization (the sklearn example peaks near 0.015).

    Cost-complexity pruning scores each subtree by its error plus a per-leaf penalty, so a branch survives only if it reduces impurity enough to pay for its leaves. Pruning proceeds weakest-link-first: at each step the node whose removal costs the least accuracy per leaf is collapsed, producing a nested sequence of subtrees indexed by alpha. You then pick alpha by cross-validation rather than by training-set performance, which is what makes the chosen tree a generalization-optimal member of the sequence.

    Two trees side by side: a deep full tree with many leaves and low purity, and its pruned version where dashed subtrees were collapsed into single leaves

    Figure 2: Post-pruning in action: the full tree (left) contains branches fitted to noise; the dashed subtrees are the weakest links, and the pruned tree (right) collapses them into leaves.

    Mathematical Formulation:
    R_{\alpha}(T) = R(T) + \alpha\,|\widetilde{T}|
    \alpha_{eff}(t) = \frac{R(t) - R(T_t)}{|T_t| - 1}

    Where:

    • R(T) is the total (sample-weighted) impurity of the leaves of tree T, and |\widetilde{T}| is its number of terminal nodes.
    • \alpha is the complexity parameter (ccp_alpha in scikit-learn); larger alpha prunes more aggressively.
    • \alpha_{eff}(t) is the effective alpha of node t: the penalty value at which keeping its branch T_t costs exactly as much as collapsing it, so the node with the smallest value is pruned first.
    AspectPre-Pruning (Early Stop)Post-Pruning (Grow Then Cut)
    Knobsmax_depth, min_samples_leaf, min_impurity_decreaseccp_alpha chosen from cost_complexity_pruning_path by CV
    Failure ModeMyopic: stops before a weak-looking split that enables a strong subtreeCosts a full grow pass plus a validation sweep
    Typical AccuracyGood, knob-sensitiveUsually better; principled weakest-link order
    When UsedMassive data, streaming, latency-critical fitsDefault for single interpretable trees

    Login to view more content
  • ML0076 Reinforcement Learning Loop

    What is reinforcement learning? Describe the agent-environment interaction loop.

    Answer

    Reinforcement learning is learning what to do from trial and error: an agent acts in an environment, receives a scalar reward, and adjusts its behavior to maximize expected cumulative reward. The loop: at step t the agent observes state s_t, picks an action a_t from its policy \pi(a \mid s), the environment transitions to a new state and returns reward r_{t+1}, and the agent updates its policy or value estimates from the experience; this repeats until the episode ends. Unlike supervised learning there are no labels: the only supervision is the reward signal, which may arrive many steps late, and the agent’s own actions decide what data it sees next, which is why exploration must be built in.

    (1) The MDP Tuple: states, actions, transition probabilities, a reward function, and a discount factor \gamma; the Markov property says the state summarizes everything relevant about the history.
    (2) Return and Value: the agent maximizes the expected discounted return, and value functions score states or state-action pairs so credit for delayed rewards can be assigned to earlier decisions.
    (3) The Same Loop Now Trains LLMs: RLHF (InstructGPT) fits a reward model from human preference rankings and optimizes the language model against it with PPO; DeepSeek-R1’s GRPO removes the value critic and baselines each prompt against its own sampled answer group, lifting AIME pass@1 from 15.6% to 71.0% with pure RL.

    Agent box and environment box with an action arrow from agent to environment and a state plus reward arrow back, annotated with the policy and the experience tuple

    Figure 1: The interaction loop: the agent’s policy turns the observed state into an action, the environment answers with the next state and a scalar reward, and the experience tuple (s_t, a_t, r_{t+1}, s_{t+1}) feeds the learning update.

    Mathematical Formulation:
    G_t = \sum_{k=0}^{\infty} \gamma^k r_{t+k+1}
    \pi^{*} = \arg\max_{\pi}\ \mathbb{E}_{\pi}\big[G_0\big]

    Where:

    • G_t is the discounted return from step t; r_{t+k+1} is the reward received k steps later.
    • \gamma \in [0, 1) is the discount factor, trading immediate reward against future reward and keeping infinite-horizon returns finite.
    • \pi is the policy and the expectation runs over trajectories induced by \pi and the environment’s transition dynamics; \pi^{*} is the optimal policy.
    FeatureReinforcement LearningSupervised Learning
    Supervision SignalScalar reward, possibly delayed many stepsA label per example
    DataSelf-generated, non-stationaryFixed, assumed i.i.d.
    ObjectiveMaximize expected discounted returnMinimize a per-example loss
    Feedback TimingCredit assignment over timeImmediate and exact
    Canonical Loopact, observe reward and next state, updateepochs over a fixed dataset
    Three boxes: the language model policy samples answers, a reward model scores them, and a PPO or GRPO update improves the policy, with a loop arrow back to the policy

    Figure 2: The loop specialized to LLM post-training: the policy is the language model, actions are token sequences, and a learned reward model supplies the scalar reward. PPO (InstructGPT) trains a value critic alongside; GRPO (DeepSeek-R1) replaces it with a group baseline over sampled answers.


    Login to view more content
  • ML0074 Linear vs Non-Linear Reduction

    What is the difference between linear and non-linear dimensionality reduction? Give examples of each.

    Answer

    Linear methods rotate and project: the low-dimensional representation is a linear function of the original features, as in PCA, LDA, or truncated SVD. Non-linear methods learn a curved mapping, so they can unroll manifolds that no flat projection can represent, as in t-SNE, UMAP, Isomap, and autoencoders. The price of curvature is weaker guarantees: a linear projection preserves global geometry as well as any linear map can and gives a reusable transform for new points, while neighbor-graph methods such as t-SNE and UMAP preserve local neighborhoods but distort global distances and produce no reusable mapping (autoencoders and parametric t-SNE are the exceptions).

    (1) Linear Methods: closed-form, fast, invertible on the kept subspace, and out-of-sample points reuse the same projection matrix. Examples: PCA, LDA, truncated SVD.
    (2) Non-Linear Methods: capture curved manifolds and cluster structure that projections cannot. Examples: t-SNE and UMAP (neighbor graphs), Isomap (geodesic distances), autoencoders (a learned parametric map).
    (3) Choose by Purpose: preprocessing for a downstream model calls for a linear method; 2D visualization of clusters calls for a non-linear one. Production pipelines often chain them: 10x Genomics’ Cell Ranger reduces single-cell gene expression with PCA first, then computes t-SNE or UMAP projections on the top components for its Loupe Browser maps.

    Three panels: a horseshoe-shaped manifold of points, its PCA projection collapsing the two ends together, and a non-linear unrolling that orders the points along the true intrinsic coordinate

    Figure 1: Why curvature matters: the horseshoe’s two ends are close in ambient space but far apart along the manifold. PCA projects along a straight axis and collapses the ends together; a non-linear method unrolls the curve and keeps the true ordering.

    Mathematical Formulation:
    z_i = W^{\top} x_i \ \ (W \in \mathbb{R}^{d \times k},\ \text{linear})
    z_i = g_{\phi}(x_i) \ \ (g\ \text{learned, non-linear})

    Where:

    • W is an orthonormal projection matrix and z_i the k-dimensional representation of x_i.
    • g_{\phi} is a learned map such as an autoencoder’s encoder; its parameters \phi are fitted by gradient descent.
    • t-SNE and UMAP are transductive: they output the coordinates z_i directly without an explicit g, so new points need a parametric variant or a refit.
    FeatureLinear (PCA, LDA, SVD)Non-Linear (t-SNE, UMAP, Autoencoder)
    MappingFixed matrix WLearned or implicit g
    Global GeometryPreserved as well as a linear map allowsDistorted: cluster sizes and distances not meaningful
    New PointsApply the same WAutoencoder: yes; t-SNE/UMAP: refit or parametric variant
    Failure ModeCurved manifolds collapseOver-read clusters; stochastic restarts differ
    Best UsePreprocessing, compression, de-correlationVisualization, cluster discovery
    Same clustered data embedded two ways: PCA projection with clusters overlapping, versus a UMAP-style embedding with clusters cleanly separated

    Figure 2: The same clustered data through both lenses. PCA (left) keeps global spread but overlaps clusters that are not linearly separable; the non-linear embedding (right) separates the clusters, at the cost of making distances between them uninterpretable.


    Login to view more content
  • ML0073 Principal Component Analysis

    How does Principal Component Analysis (PCA) work, step by step?

    Answer

    PCA finds the orthogonal directions of maximum variance in centered data and projects the data onto the top k of them. Step by step: center each feature by subtracting its mean (and scale to unit variance when units differ); compute the covariance matrix; eigendecompose it, or equivalently run SVD on the data matrix, so eigenvectors become the principal directions and eigenvalues the variance each direction carries; keep the top k eigenvectors and project every sample onto them. The result is a rotated coordinate system whose axes are uncorrelated and ordered by explanatory power, which is why the first few components often carry most of the variance.

    (1) Variance as Information: each principal component is the direction of maximum residual variance, orthogonal to all previous ones, and its eigenvalue \lambda_j is exactly the variance the data has along it.
    (2) Explained-Variance Ratio Picks k: plot the cumulative share \sum_{j \leq k} \lambda_j / \sum_j \lambda_j and keep enough components to cover, say, 95% of total variance.
    (3) Linear and Scale-Sensitive: PCA rotates but never unrolls, so curved manifolds need non-linear methods, and features with different units must be standardized first. Learned embeddings sidestep truncation differently: OpenAI’s text-embedding-3 models were trained with Matryoshka representation learning, so shortening a 3072-dimension vector to 256 still beats the older full-size ada-002, a property naive PCA truncation of a learned embedding cannot promise.

    Two-dimensional elliptical point cloud with the first principal component arrow along the major axis and the second along the minor axis

    Figure 1: The two principal components of a 2D cloud: PC1 lies along the direction of maximum spread and carries the largest eigenvalue; PC2 is orthogonal to it and mops up the residual variance.

    Mathematical Formulation:
    \Sigma = \frac{1}{N} X^{\top} X \ \ (\text{centered } X)
    \Sigma v_j = \lambda_j v_j
    z_i = V_k^{\top} x_i

    Where:

    • X is the centered data matrix with N samples and d features; \Sigma is its d \times d covariance matrix.
    • v_j, \lambda_j are the eigenvector-eigenvalue pairs of \Sigma, sorted by descending \lambda_j.
    • V_k stacks the top k eigenvectors and z_i is the k-dimensional score vector of sample x_i.
    Scree plot: bars of per-component variance falling off, with a cumulative explained variance line crossing the 95 percent mark

    Figure 2: The scree plot: bars show the variance each component carries and the curve shows the cumulative share. Keeping components up to the 95% crossing is the standard rule for choosing k.

    Projection is lossy: the discarded components carry the remaining variance, so projecting back into the original space reconstructs the data only up to the kept subspace. In 2D this means every point collapses onto the PC1 line when only one component is kept, which is the geometric picture behind “95% of variance retained”.

    Point cloud with each point connected by a thin line to its perpendicular reconstruction on the principal component axis

    Figure 3: Reconstruction from one component: every point is replaced by its perpendicular projection onto the PC1 axis. The gray connectors are the reconstruction errors, whose mean squared length is exactly the discarded variance \lambda_2 (their sum is N \lambda_2).

    FeatureEigendecomposition of CovarianceSVD of the Data Matrix
    InputThe d \times d matrix \SigmaThe centered N \times d matrix X directly
    Numerical StabilityForming X^{\top} X squares the condition numberStable: never materializes X^{\top} X
    ScalabilityFine for small dRandomized SVD scales to huge N and d
    PracticeTextbook derivationWhat libraries (scikit-learn) actually call

    Login to view more content
  • ML0071 Covariance vs Correlation

    What is the difference between covariance and correlation?

    Answer

    Both measure how two variables move together. Covariance is the raw co-movement, expressed in the product of the two variables’ units: positive when the variables tend to sit above their means together, negative when one runs high while the other runs low. Correlation (Pearson’s \rho) is covariance divided by both standard deviations, which strips the units and bounds the result to [-1, 1], so strengths become comparable across datasets. Neither proves causation, and both capture only linear association: a perfect parabola scores zero correlation despite being fully determined.

    (1) Units: covariance keeps the product of the units (dollars times kilograms); correlation is unitless and bounded, so rescaling a variable changes the covariance but leaves the correlation untouched.
    (2) Sign vs Strength: covariance tells you the direction of co-movement; only correlation tells you a comparable strength, since 0.9 vs 0.2 means the same thing for any pair of variables.
    (3) Linear Only: both are second-moment statistics of linear association. Zero correlation does not imply independence: Y = X^2 on symmetric data has \rho = 0 while X fully determines Y.

    Four scatter panels: positive correlation, negative correlation, zero correlation with independent points, and a parabola with zero correlation but full dependence

    Figure 1: Correlation reads linear co-movement: positive (top left), negative (top right), zero with independence (bottom left), and the trap case, zero correlation with complete dependence on a parabola (bottom right). The first three have matching covariance signs; the fourth shows why a second-moment statistic can miss structure.

    Mathematical Formulation:
    \mathrm{Cov}(X, Y) = \mathbb{E}\big[(X - \mu_X)(Y - \mu_Y)\big]
    \rho_{XY} = \frac{\mathrm{Cov}(X, Y)}{\sigma_X \, \sigma_Y}

    Where:

    • X, Y are random variables with means \mu_X, \mu_Y and standard deviations \sigma_X, \sigma_Y.
    • \mathbb{E}[\cdot] is expectation over the joint distribution; in practice both quantities are estimated by sample averages.
    • \rho_{XY} lies in [-1, 1] by the Cauchy-Schwarz inequality, with \pm 1 meaning an exact linear relationship.
    FeatureCovarianceCorrelation
    DefinitionMean product of centered valuesCovariance divided by both \sigma
    UnitsProduct of the variables’ unitsUnitless
    RangeUnbounded[-1, 1]
    Scale InvarianceNo: rescaling a variable rescales itYes: unchanged by a positive affine rescaling (a negative scale flips the sign)
    Typical UsePortfolio variance, covariance matrices, PCAComparing association strength, feature screening

    Login to view more content
  • ML0069 Z-Test vs T-Test

    When do you use a z-test versus a t-test?

    Answer

    Both tests compare an estimated effect with its standard error to judge whether a mean differs from a reference value. Use a z-test when the reference distribution can be treated as exactly normal: the population standard deviation \sigma is known, or the sample is large enough that the estimate of \sigma is essentially exact. Use a t-test when \sigma is estimated from a small sample: the extra estimation uncertainty fattens the tails of the test statistic, and the t-distribution with n - 1 degrees of freedom accounts for it. As n grows, the t-distribution converges to the normal, so beyond a few dozen observations the two tests give nearly identical p-values.

    (1) Same Statistic, Different Reference: both divide an effect estimate by its standard error; they differ only in whether the reference distribution is the standard normal or the heavier-tailed t.
    (2) When t Is Mandatory: small samples with unknown \sigma, where the t’s heavier tails keep the false positive rate at the promised \alpha. Use Welch’s variant when the two groups have unequal variances.
    (3) In Practice at Scale: conversion metrics in online experiments are proportions with millions of users, so the normal (z) approximation is effectively exact; Booking.com’s experimentation write-ups fall back to the two-sample t-test when comparing means on smaller or skewed samples.

    Standard normal curve overlaid with a heavy-tailed t distribution with 3 degrees of freedom, the t tails visibly higher beyond plus minus 2

    Figure 1: Why the distinction exists: the t-distribution (3 degrees of freedom shown) has visibly heavier tails than the standard normal, so its critical values sit farther out. As degrees of freedom grow, the t curve collapses onto the normal and the two tests coincide.

    Mathematical Formulation:
    z = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}
    t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}

    Where:

    • \bar{x} is the sample mean, \mu_0 the hypothesized mean, and n the sample size.
    • \sigma is the known population standard deviation used by the z-test; s is the sample-estimated standard deviation used by the t-test.
    • The t statistic follows a t-distribution with n - 1 degrees of freedom, which approaches N(0, 1) as n grows.
    FeatureZ-TestT-Test
    Spread Parameter\sigma known (or n huge)s estimated from the sample
    Reference DistributionStandard normalt with n - 1 degrees of freedom
    Tail BehaviorThinner tails, tighter critical valuesHeavier tails at small n
    Typical CaseProportion tests at scale (conversion)Small samples, heavy-tailed metrics
    Large-n BehaviorAsymptotically valid for proportions via the CLTCoincides with the z-test

    Login to view more content