bayesian_modeling

## Checking, Comparison, and Decisions

Model checking asks whether the model can reproduce the data's relevant features, and posterior
predictive checking is the primary tool. Simulate replicated datasets from the fitted model,
compute a summary statistic on each and on the observed data, and compare. If the observed value
falls in the tail of the replicated distribution, the model fails to reproduce that feature —
which is informative regardless of whether any parameter appears significant.


bayesian_modeling

The choice of summary statistic determines what is checked, and it should reflect what the
model is for. A model reproducing the mean and failing on the variance, on the number of zeros,
on the extremes, or on the autocorrelation may be adequate or inadequate depending on the use.
Checking several statistics addressing different aspects, including ones the model was not fit to
match, is what makes the check informative.


bayesian_modeling

Model comparison in the Bayesian framework has several tools with different properties. Bayes
factors compare marginal likelihoods and are theoretically clean and notoriously sensitive to
prior specification, including to the priors on parameters that both models share — which means
a Bayes factor can be dominated by a prior choice that the posterior is insensitive to.
Information criteria estimate out-of-sample predictive accuracy, with leave-one-out
cross-validation approximated by importance sampling being the modern standard, and it reports
diagnostics indicating when the approximation is unreliable.


bayesian_modeling

Checking, comparison, and decisions:
  posterior predictive check  simulate and compare; the primary diagnostic
  choice of statistic   check what the model is for, including features it was not
                        fit to match
  graphical checks      overlay replicated and observed distributions; more
                        informative than a single p-value
  Bayes factor          marginal likelihood ratio; prior-sensitive, including to
                        shared priors
  WAIC and PSIS-LOO     estimate out-of-sample predictive density; report the
                        diagnostics indicating unreliability
  model averaging       weight predictions by model plausibility rather than
                        selecting one
  stacking              optimize predictive weights directly; usually better than
                        weights derived from information criteria
  decision theory       choose the action minimizing expected loss under the
                        posterior; the natural completion of the framework
  loss function         encodes the asymmetry of consequences; a symmetric loss is
                        an assumption rather than a default
  value of information  the expected improvement in decision quality from more
                        data; determines whether to collect it
  reporting             the posterior distribution, not only a point and an
                        interval; and the prior, so the analysis is reproducible


bayesian_modeling

Decision theory is the framework's natural completion and is underused. Having a posterior
distribution over an unknown quantity and a loss function describing the consequences of each
action makes the optimal action a computation: choose the action minimizing expected loss under
the posterior. This handles asymmetric consequences naturally — where over-estimating costs more
than under-estimating, the optimal point estimate is not the posterior mean — and it makes
explicit that the summary statistic to report depends on what will be done with it. The posterior
mean minimizes squared error loss, the median minimizes absolute error, and a quantile minimizes
the asymmetric loss appropriate to an inventory decision, so reporting the mean by default embeds
an unstated assumption about consequences.


bayesian_modeling

The practical workflow that has emerged in the field is worth stating as a sequence. Specify the
model and the priors; check the priors by simulating from them; fit to simulated data with known
parameters and confirm the parameters are recovered, which validates the implementation; fit to
the real data; check the computational diagnostics before looking at any result; perform
posterior predictive checks; compare against alternative models; and conduct sensitivity analysis
on the choices that were least well justified. Skipping the simulation-based validation is the
most commonly omitted step and the one most likely to leave an implementation error undetected,
because a model with a coding error frequently produces plausible-looking results on real data
where the truth is unknown.


causal_machine_learning

## Why Prediction Is Insufficient

Predictive models answer what will happen given what is observed; causal questions ask what would
happen if something were changed. These are different questions with different answers, and a
model that predicts excellently can be entirely misleading as a guide to action — which is the
central point and the one that most often needs establishing.


causal_machine_learning

The canonical illustration is a model predicting hospital mortality that learns asthmatic
patients with pneumonia have lower mortality. The prediction is accurate: asthmatic patients were
admitted directly to intensive care because they were considered high risk, and the intensive
treatment produced better outcomes. Acting on the model — deprioritizing asthmatic patients
because they are predicted to do well — would remove the treatment producing the prediction. The
model correctly captured the association in the data and the association was produced by the
policy it would be used to change.


causal_machine_learning

This is the general structure rather than an anomaly. Any model trained on observational data
learns associations shaped by the decisions that generated the data, and using the model to change
those decisions invalidates the associations it learned. Prediction is adequate when the goal is
forecasting under an unchanged policy and inadequate whenever the goal is choosing an action.


causal_machine_learning

Prediction versus causation:
  prediction           what will happen given observation
  causation            what would happen under intervention
  the asthma example   the association was produced by the policy the model would
                       change
  observational data   shaped by the decisions that generated it
  policy invariance    predictions hold only while the decision process is
                       unchanged
  when prediction suffices  forecasting under an unchanged policy
  when it does not     any question of the form "what should we do"
  correlation          symmetric; consistent with either direction or a common
                       cause
  intervention         setting a variable rather than observing it; the do
                       operator distinguishes them notationally
  counterfactual       what would have happened to this unit under a different
                       treatment; unobservable, which is the fundamental problem
  the fundamental problem  only one potential outcome is ever observed per unit
  ladder of causation  association, intervention, counterfactual; each level
                       requires assumptions the level below cannot supply


causal_machine_learning

Q: A model predicting customer churn identifies that customers who contact support are more
likely to churn. Acting on this by reducing support contact would:
A. Reduce churn
B. Likely worsen it, since support contact is a symptom of dissatisfaction rather than a cause of
   churn, and removing the support removes an intervention that may be reducing churn
C. Have no effect
D. Reduce costs without affecting churn
Reasoning: The association arises because dissatisfied customers contact support and dissatisfied
customers churn. Support contact is downstream of the cause rather than being the cause, and it
may be reducing churn relative to what would occur without it. This is the prediction-intervention
gap: the model captured a real association that does not support the intervention. Answer: B.


causal_machine_learning

## Causal Graphs and Identification

A causal graph represents assumed causal relationships as directed edges, and its value is that
it makes the assumptions explicit and mechanically determines what can be estimated. The graph is
not derived from the data — several graphs are compatible with any dataset — so it encodes domain
knowledge, and stating it is what permits the analysis to be criticized.


causal_machine_learning

Three structures determine how association flows and they must be distinguished because the
correct handling of each is different. A chain, where one variable causes a second which causes a
third, transmits association, and conditioning on the middle variable blocks it. A fork, where one
variable causes two others, transmits association between the two effects, and conditioning on
the common cause blocks it — this is confounding, and the common cause is the confounder. A
collider, where two variables both cause a third, transmits no association, and conditioning on
the common effect creates one.


causal_machine_learning

The collider case is where intuition fails most severely. Adjusting for additional variables is
widely believed to be conservative, and it is not: conditioning on a common effect of the
treatment and the outcome induces a spurious association where none existed. Selection into the
sample is a form of conditioning, so a sample selected on a variable affected by both the treatment
and the outcome produces collider bias regardless of what is adjusted for in the analysis.


causal_machine_learning

Graphs and identification:
  directed edge        an assumed causal relationship
  the graph is an assumption  several are compatible with any data; stating it
                       makes it criticizable
  chain                A to B to C; transmits association, blocked by conditioning
                       on B
  fork                 B causes A and C; confounding, blocked by conditioning on B
  collider             A and C both cause B; blocked by default, opened by
                       conditioning on B
  d-separation         the graphical criterion for conditional independence
  back-door criterion  a set blocking all back-door paths identifies the effect by
                       adjustment
  front-door criterion  identification through a mediator when unmeasured
                       confounding blocks the back door
  instrumental variable  affects treatment, and the outcome only through it
  adjusting for a mediator  removes the effect being estimated
  adjusting for a collider  creates bias where none existed
  selection bias       selection on a collider; unfixable by adjustment because
                       the conditioning already occurred
  M-bias               adjusting for a variable that is not a confounder can open
                       a path; more adjustment is not safer
  positivity           every unit must have a nonzero probability of each
                       treatment; without it the effect is not identified in that
                       region


causal_machine_learning

Positivity deserves emphasis because it is frequently violated silently. Estimating the effect of
a treatment requires that units with the relevant covariates could have received either treatment
level, and if some region of covariate space deterministically receives one treatment, no data
informs the counterfactual there. A model will nonetheless produce an estimate by extrapolating,
and the extrapolation is unconstrained by data. Checking the overlap of covariate distributions
between treatment groups is the diagnostic, and restricting the analysis to the region of common
support is the honest response.


causal_machine_learning

## Estimation

Given identification — an argument that the effect is estimable from the available data under
stated assumptions — estimation is the statistical task of computing it. Machine learning
contributes here because the adjustment frequently requires modelling high-dimensional
relationships that parametric models handle poorly.


causal_machine_learning

Naively plugging a machine learning model into a causal estimator produces bias, and the reason
is instructive. Machine learning methods are regularized to minimize prediction error, which
introduces bias to reduce variance, and that bias does not vanish at the rate required for valid
inference about a causal parameter. The resulting estimate has a bias term that dominates the
standard error, so confidence intervals have incorrect coverage.


causal_machine_learning

Double machine learning addresses this with two devices. Orthogonalization constructs an
estimating equation insensitive to first-order errors in the nuisance models, so that errors in
estimating the treatment assignment or the outcome model do not propagate directly into the
causal estimate. Cross-fitting estimates the nuisance models on one part of the data and the
causal parameter on another, which removes the overfitting bias that arises when the same
observations inform both. Together they permit flexible machine learning for the nuisance
components while retaining valid inference.


causal_machine_learning

Estimation approaches:
  regression adjustment  model the outcome given treatment and covariates
  propensity score     the probability of treatment given covariates
  matching             pair treated and control units with similar propensity or
                       covariates
  inverse propensity weighting  weight by the inverse probability of the observed
                       treatment; unstable when propensities approach zero or one
  doubly robust        consistent if either the outcome model or the propensity
                       model is correct; the practical default
  double machine learning  orthogonalization plus cross-fitting; valid inference
                       with flexible nuisance models
  orthogonalization    the estimating equation is insensitive to first-order
                       nuisance errors
  cross-fitting        nuisance and parameter estimated on different folds
  the regularization problem  machine learning bias does not vanish fast enough
                       for naive plug-in inference
  difference in differences  compare changes over time across groups; requires
                       parallel trends
  synthetic control    a weighted combination of untreated units matching the
                       pre-period; for a single treated unit
  regression discontinuity  compare units just above and below a threshold; strong
                       internal validity, local to the threshold
  instrumental variables  identification through an instrument; the exclusion
                       restriction is untestable and usually the weak point


causal_machine_learning

The doubly robust property is worth understanding because it explains why these estimators are
preferred. An estimator combining an outcome model and a propensity model is consistent if either
one is correctly specified, which is a genuinely useful guarantee — it does not require getting
both right, and it means two independent attempts at modelling provide protection that either
alone does not. The property does not survive both being wrong, and in high dimensions both are
approximations, which is why the combination with cross-fitting and orthogonalization matters
rather than the doubly robust property alone.


causal_machine_learning

## Heterogeneous Effects and Policy Learning

An average treatment effect answers whether a treatment helps on average, and it frequently
conceals that it helps some units substantially and harms others. Estimating how the effect varies
with covariates — the conditional average treatment effect — is what permits targeting, and it is
where machine learning contributes most directly to causal inference.


causal_machine_learning

The estimation problem is harder than prediction because the target is never observed. For a
predictive model, each observation has a label and the error is measurable. For an effect estimate,
each unit's individual treatment effect is the difference between two potential outcomes and only
one is observed, so there is no label to compare against and standard model selection does not
apply. This is why methods designed for the purpose exist rather than applying a regression to
the difference.


causal_machine_learning

Causal forests adapt random forests to this task by splitting to maximize heterogeneity in the
treatment effect rather than in the outcome, and by using separate samples for splitting and
estimation so that the estimates within a leaf are honest. Meta-learners construct the effect
estimate from standard predictive models in various arrangements, and which arrangement performs
better depends on the structure — whether the baseline outcome or the effect is the more complex
function.


causal_machine_learning

Heterogeneous effects and policy:
  average treatment effect  the population average; conceals heterogeneity
  conditional average effect  how the effect varies with covariates
  individual effect    never observed; the fundamental problem
  no ground truth      standard model selection does not apply
  causal forest        splits to maximize effect heterogeneity; honest estimation
                       with sample splitting
  meta-learners        S, T, X, and R learners; construct effects from predictive
                       models
  which learner        depends on whether the baseline or the effect is more
                       complex, and on the treatment balance
  policy learning      derive a treatment assignment rule from estimated effects
  policy value         the expected outcome under a policy; the quantity to
                       maximize
  uplift modelling     the marketing term for the same problem
  the four quadrants   persuadables, sure things, lost causes, and those harmed by
                       treatment; targeting only the persuadables is the point
  off-policy evaluation  estimate a new policy's value from data collected under
                       another; requires overlap
  constraints          budget, fairness, and capacity turn policy learning into a
                       constrained optimization


causal_machine_learning

The four-quadrant framing deserves emphasis because it clarifies what targeting achieves. Under
any treatment, some units would have the desired outcome regardless, some would not regardless,
some are influenced positively, and some are influenced negatively. A predictive model identifies
who will have the outcome, which mixes the first group with the third. An effect model identifies
who is influenced, which is the only group where treatment changes anything. Targeting by
predicted outcome therefore spends the budget substantially on units whose behaviour is unchanged,
and the difference between predictive and causal targeting is frequently large enough to determine
whether a campaign is profitable.


causal_machine_learning

[user] Our marketing team targets discount offers at customers with the highest predicted churn
probability. Is that right?
[assistant] tool_call causal.targeting_audit({"current_policy": "target_highest_predicted_churn",
"model": "churn_classifier", "treatment": "discount_offer",
"available": ["historical_randomized_holdout"], "budget_constrained": true})
[tool] {"issue": "targeting_by_outcome_not_by_effect",
"quadrant_analysis": {"sure_things_targeted": true, "lost_causes_targeted": true,
"sleeping_dogs_risk": true}, "recommendation": "estimate_conditional_treatment_effect",
"data_available": "randomized_holdout_permits_uplift_estimation"}
[assistant] No — you are targeting by predicted outcome and you want to target by predicted
effect, and those select substantially different customers.


causal_machine_learning

Your highest-churn-probability customers divide into two groups that the model cannot
distinguish. Some are churning for reasons a discount addresses, and treating them changes the
outcome. Others are churning for reasons a discount does not touch — they have moved, their needs
changed, a competitor offers something you do not — and treating them costs money and changes
nothing. The model ranks both identically because both have high churn probability.


causal_machine_learning

The group you are missing entirely is customers with moderate churn probability who are highly
responsive to a discount. They rank low on your list and they are where the budget produces the
most retained revenue per pound.


causal_machine_learning

There is also a group worth actively excluding. Some customers would have stayed and, on
receiving an unsolicited discount offer, are prompted to reconsider the relationship or to expect
discounts thereafter — the sleeping dogs. Targeting them is worse than doing nothing, and a
predictive model cannot identify them because their predicted churn is low and their effect is
negative.


causal_machine_learning

You have a randomized holdout, which is what makes the fix straightforward. Use it to estimate
the conditional average treatment effect — how much the discount changes churn probability as a
function of customer covariates — with a causal forest or an X-learner. Then rank by estimated
effect rather than by predicted churn and target the top of that ranking within your budget.


causal_machine_learning

Two practical points. Validate the uplift model with a Qini or uplift curve on held-out
randomized data rather than with a standard classification metric, since the target is
unobservable and ordinary metrics do not measure what you need. And run the new policy against the
old one as an experiment rather than switching outright — the uplift estimate is itself uncertain,
and the comparison is what establishes the gain.


causal_machine_learning

Expect the targeted set to overlap with your current one by substantially less than intuition
suggests, frequently under half, which is the practical measure of how different the two questions
are.


causal_machine_learning

## Sensitivity and Practice

Causal conclusions from observational data rest on untestable assumptions, and the honest
treatment is to state them and to quantify how much violation would overturn the conclusion rather
than to assert that they hold. Sensitivity analysis does this: it asks how strong an unmeasured
confounder would need to be, in its association with both treatment and outcome, to explain away
the estimated effect.


causal_machine_learning

The E-value expresses this in an interpretable form — the minimum strength of association an
unmeasured confounder would need with both the exposure and the outcome to account for the
observed effect. An E-value of 1.3 means a modest unmeasured confounder suffices and the finding is
fragile; an E-value of 5 means an implausibly strong one would be required. This converts an
untestable assumption into a quantified statement about how much would have to be wrong.


causal_machine_learning

The practical workflow that produces defensible causal analysis has a consistent shape. State the
causal question precisely, including the population, the treatment, the comparison, and the
outcome. Draw the assumed causal graph and state what is assumed. Determine identification from
the graph — which is where the analysis frequently ends, because the effect is not identified with
the available data and no estimation method rescues it. Then estimate, check the assumptions that
are checkable, and conduct sensitivity analysis on the ones that are not.


causal_machine_learning

Sensitivity and practice:
  untestable assumptions  no unmeasured confounding, positivity, the exclusion
                       restriction; state them
  sensitivity analysis  how much violation would overturn the conclusion
  E-value              the minimum confounder strength required to explain the
                       effect away
  negative controls    an outcome the treatment could not affect; an effect there
                       indicates residual confounding
  placebo tests        apply the method where no effect should exist
  pre-period tests     for difference-in-differences, check that trends were
                       parallel before treatment
  falsification        find a prediction the causal claim makes that the
                       association-only explanation does not
  triangulation        several designs with different assumptions; agreement is
                       far stronger than any single estimate
  the identification first  determine what is estimable before estimating; many
                       analyses fail here and proceed regardless
  state the population  an effect estimated on one population need not transfer
  transportability     whether an effect generalizes to another population; a
                       separate question requiring separate assumptions
  experiments where possible  randomization eliminates the assumptions that
                       observational analysis must defend


causal_machine_learning

Triangulation deserves the closing emphasis because it is the strongest available response to the
untestability problem. Any single observational design rests on assumptions that cannot be
verified, so a single estimate however carefully produced carries that exposure. Several designs
with different and largely unrelated assumptions — an instrumental variable analysis, a
difference-in-differences, a regression discontinuity, and a natural experiment — that agree
provide evidence far stronger than the sum of their individual strengths, because the assumptions
would all have to fail in a coordinated way to produce agreement. Where designs disagree, that is
informative too, and it identifies which assumption is doing the work.


classical_machine_learning

## Learning Theory and the Bias-Variance Tradeoff

Supervised learning assumes examples are drawn independently from a fixed but unknown
distribution, and the goal is to find a function minimizing expected loss on new draws
from that distribution. Everything difficult about the field follows from the fact that
you can only measure loss on the sample you have, while you care about the population
you do not.


classical_machine_learning

The bias-variance decomposition makes the tension precise. Expected squared error at a
point decomposes into squared bias — how far the average prediction over training sets
sits from the truth — plus variance — how much the prediction moves across training sets
— plus irreducible noise. A model that is too simple has high bias and cannot represent
the truth however much data it sees. A model that is too flexible has high variance and
fits the particular noise in its training set. The classical picture is a U-shaped test
error as complexity increases, with an optimum in between.


classical_machine_learning

That classical picture is incomplete, and the modern qualification matters. Very
overparameterized models — deep networks, wide random forests, high-dimensional
interpolators — exhibit double descent: test error rises to a peak near the
interpolation threshold where the model has just enough capacity to fit the training data
exactly, then falls again as capacity increases further. The explanation involves
implicit regularization: among the many parameter settings that fit the training data,
gradient descent finds ones with small norm, and those generalize. This does not repeal
the bias-variance tradeoff so much as show that "complexity" measured by parameter count
is the wrong axis.


classical_machine_learning

No free lunch theorems state that averaged over all possible problems, every learning
algorithm performs identically. The theorem is often quoted as though it counselled
agnosticism; its real content is the opposite. It says that all performance comes from
assumptions matching the structure of the actual problem, so the practical question is
never which algorithm is best but which algorithm's inductive bias matches your data.
Convolution assumes locality and translation invariance. Trees assume axis-aligned
piecewise-constant structure. Linear models assume additivity. Kernel methods assume
smoothness in a chosen metric. Matching the bias to the domain is the work.


classical_machine_learning

Core learning concepts:
  empirical risk       average loss on the training sample
  generalization gap   difference between empirical and expected risk
  overfitting          low training loss, high test loss; variance-dominated
  underfitting         high loss on both; bias-dominated
  capacity             VC dimension, Rademacher complexity; formal measures of
                       how much a model class can fit
  regularization       any modification reducing variance at some bias cost
  inductive bias       the assumptions that make generalization possible at all
  curse of dimension   volume grows exponentially, so data becomes sparse and
                       distance-based methods degrade


classical_machine_learning

The curse of dimensionality has a specific and often-missed consequence for
distance-based methods: as dimension grows, the ratio between the nearest and farthest
neighbour distances approaches one, so "nearest neighbour" ceases to be meaningfully
different from "any neighbour". This is why k-nearest-neighbours degrades sharply in
high dimensions, why distance-based clustering needs dimensionality reduction first, and
why feature selection is not merely a convenience but often a precondition for these
methods working at all.


classical_machine_learning

Q: A model achieves 99% training accuracy and 71% test accuracy. The most appropriate
first intervention is:
A. Increase model capacity
B. Reduce capacity or add regularization, and get more data if possible
C. Train for more epochs
D. Change the evaluation metric
Reasoning: The 28-point gap is the signature of variance-dominated error — the model is
fitting the training set's noise. Increasing capacity or training longer worsens it. The
remedies that reduce variance are regularization, simpler models, more data, and
ensembling. Answer: B.


classical_machine_learning

## Linear Models

Linear regression fits a hyperplane by least squares, and its enduring value is that it
is fast, exactly solvable, and interpretable: each coefficient is the expected change in
the target per unit change in that predictor, holding the others fixed. That "holding
the others fixed" clause is what makes coefficients hard to interpret when predictors are
correlated, since the data may contain no examples where one varies and the others do
not.


classical_machine_learning

Logistic regression models the log-odds of a binary outcome as a linear function,
producing calibrated probabilities rather than raw scores. It is fit by maximum
likelihood, has a convex objective so there are no local optima, and its coefficients
exponentiate to odds ratios. It remains the default in medicine, credit scoring, and any
domain where the model must be explained and defended, and it is a strong baseline that
gradient-boosted trees often beat by only a few points on tabular data.


classical_machine_learning

Regularized variants trade bias for variance. Ridge adds an L2 penalty, shrinking
coefficients smoothly and handling correlated predictors gracefully by splitting the
effect among them. Lasso adds an L1 penalty, driving coefficients exactly to zero and so
performing selection, but among correlated predictors it arbitrarily keeps one, making
the selected set unstable across resamples. Elastic net combines both and recovers
grouped selection. All require standardized features, since the penalty is scale
dependent, and none produce valid conventional p-values — post-selection inference is
genuinely hard and the naive standard errors from a lasso-selected model are
meaningless.


classical_machine_learning

Linear model reference:
  OLS solution           beta = (X'X)^{-1} X'y;  compute via QR, never by inverting
  ridge                  beta = (X'X + lambda I)^{-1} X'y;  always invertible
  lasso                  no closed form; coordinate descent or LARS
  logistic link          log(p/(1-p)) = X beta;  p = 1/(1 + exp(-X beta))
  odds ratio             exp(beta);  multiplicative change in odds per unit
  multinomial            softmax over K classes; one coefficient vector per class
  Poisson regression     log link for counts; check for overdispersion
  regularization path    fit across lambda values; choose by cross-validation
  standardization        required for penalized models, harmless for plain OLS


classical_machine_learning

Generalized linear models unify these by pairing an exponential-family distribution with
a link function. The choice of family should follow from the data-generating process:
binomial for binary outcomes, Poisson or negative binomial for counts, gamma for positive
continuous quantities with variance growing with the mean, Tweedie for the
zero-inflated positive continuous data common in insurance. Using ordinary least squares
on a count or a proportion is a frequent shortcut that produces impossible predictions
(negative counts, probabilities above one) and heteroskedastic residuals that invalidate
the standard errors.


classical_machine_learning

## Trees and Ensembles

A decision tree recursively partitions the feature space by axis-aligned splits, choosing
at each node the split that most reduces impurity — Gini or entropy for classification,
variance for regression. The result is interpretable, handles mixed data types natively,
requires no scaling, captures interactions automatically, and is invariant to monotone
transformations of the features. It is also unstable: a small change in the data can
produce an entirely different tree, because an early split changes everything below it.


classical_machine_learning

That instability is precisely what makes trees ideal ensemble members. Bagging trains
many trees on bootstrap resamples and averages them, and the variance of an average of B
correlated predictors falls with B but is floored by the correlation between them.
Random forests reduce that correlation further by restricting each split to a random
subset of features, which is why they outperform plain bagging. The out-of-bag samples —
those excluded from each bootstrap — provide a free validation estimate without a
separate holdout.


classical_machine_learning

Boosting takes the opposite approach: fit models sequentially, each correcting the
errors of the ensemble so far. Gradient boosting fits each new tree to the negative
gradient of the loss with respect to the current predictions, which for squared error is
just the residual and for other losses generalizes cleanly. Because each tree is fit to
what remains, boosting reduces bias where bagging reduces variance, and boosted ensembles
of shallow trees typically outperform random forests on tabular data — at the cost of
sensitivity to hyperparameters and a real ability to overfit that random forests largely
lack.


classical_machine_learning

Ensemble methods compared:
  bagging            parallel, independent, reduces variance; hard to overfit
  random forest      bagging plus feature subsampling; strong default, few knobs
  extra trees        random split thresholds too; faster, more variance reduction
  gradient boosting  sequential, reduces bias; best tabular accuracy, needs tuning
  XGBoost            regularized objective, second-order optimization, sparsity-aware
  LightGBM           histogram binning, leaf-wise growth; fastest on large data
  CatBoost           ordered target statistics for categoricals; best default
                     handling of high-cardinality categorical features
  stacking           train a meta-model on base model predictions; use out-of-fold
                     predictions or the meta-model learns from leakage


classical_machine_learning

The key boosting hyperparameters interact in a predictable way. Learning rate and number
of trees trade off directly: halving the rate roughly doubles the trees needed, and lower
rates with more trees generalize slightly better at proportionally more compute. Tree
depth controls interaction order — depth 3 captures three-way interactions — and shallow
trees with many rounds usually beat deep trees with few. Subsampling rows and columns per
tree adds randomness that reduces variance. Early stopping on a validation set is the
single most valuable practice, because it removes the tree count from the search entirely.


classical_machine_learning

Feature importance from tree ensembles is widely used and widely misinterpreted. The
default impurity-based importance is biased toward high-cardinality and continuous
features, which have more possible split points and therefore more chances to appear
useful even when uninformative. Permutation importance — measuring the performance drop
when a feature is randomly shuffled — is unbiased in that respect but attributes shared
credit arbitrarily among correlated features and can be misleading when permutation
creates impossible feature combinations. SHAP values provide a theoretically grounded
attribution with consistency guarantees and are the current best practice, at
substantially higher computational cost.


classical_machine_learning

[user] Random forest gives 0.84 AUC and XGBoost gives 0.86 on my 50k-row tabular dataset,
but XGBoost's performance swings a lot between runs.
[assistant] tool_call ml.compare_models({"rows": 50000, "rf_auc": 0.84,
"xgb_auc": 0.86, "xgb_cv_std": 0.031, "rf_cv_std": 0.006,
"xgb_params": {"n_estimators": 1200, "learning_rate": 0.3, "max_depth": 9,
"subsample": 1.0, "early_stopping": false}})
[tool] {"diagnosis": "overfit_and_undertuned", "flags": ["learning_rate_high",
"depth_high_for_n", "no_subsampling", "no_early_stopping"],
"suggested": {"learning_rate": 0.05, "max_depth": 5, "subsample": 0.8,
"colsample_bytree": 0.8, "early_stopping_rounds": 50}}
[assistant] The instability is a tuning problem, and fixing it will probably also raise
the score. Your current settings are close to worst-case for variance.


classical_machine_learning

Learning rate 0.3 with 1200 trees is a lot of aggressive steps — each tree moves the
ensemble far, so which trees get built depends heavily on the particular training split,
and the ensemble lands in a different place each run. Depth 9 compounds this: that
allows nine-way interactions, which 50k rows cannot support reliably, so the deep trees
are fitting noise. And with no subsampling and no early stopping, there is nothing
damping either effect.


classical_machine_learning

Change these together: learning rate 0.05, max_depth 5, subsample 0.8, colsample_bytree
0.8, and early stopping with 50 rounds patience on a validation fold. Set n_estimators
high — say 5000 — and let early stopping choose. The lower rate needs more trees, which
is expected and cheap.


classical_machine_learning

I would predict this lands around 0.87-0.88 AUC with a cross-validation standard
deviation closer to your random forest's 0.006. The rule of thumb worth carrying: when
boosting is unstable, the cause is almost always too-large steps, too-deep trees, or no
early stopping, in that order.


classical_machine_learning

Keep the random forest as a baseline. A 0.02 AUC gap for substantially more tuning
effort and instability is not always worth it, and if the model needs to be re-fit
automatically without supervision, the forest's insensitivity to hyperparameters is a
real operational advantage.


classical_machine_learning

## Kernels, Distance Methods, and Clustering

Support vector machines find the hyperplane maximizing the margin between classes, which
is a convex optimization with a unique solution. The decision boundary depends only on
the support vectors — the points at or inside the margin — so the model is sparse in the
training data. Soft margins introduce slack variables allowing violations, with a
regularization parameter C controlling the tradeoff between margin width and violations.


classical_machine_learning

The kernel trick is the idea with the widest reach. Because the SVM's optimization
depends on the data only through inner products, replacing the inner product with a
kernel function implicitly maps into a higher-dimensional space without ever computing
the coordinates there. The radial basis function kernel corresponds to an
infinite-dimensional space, so an RBF SVM can represent arbitrarily complex boundaries
while remaining a convex problem. The same trick applies to PCA, ridge regression, and
Gaussian processes, all of which have kernelized forms.


classical_machine_learning

The limitation is scale. Kernel methods require the N-by-N kernel matrix, so memory grows
quadratically and training typically between quadratic and cubic in the number of
samples. Beyond a few tens of thousands of points this becomes prohibitive, which is why
kernel methods lost ground to trees and neural networks as datasets grew, and why
approximations like random Fourier features and the Nystrom method exist.


classical_machine_learning

Clustering finds structure without labels, and the algorithms differ in what shape of
cluster they can find. K-means minimizes within-cluster variance and therefore finds
spherical clusters of similar size; it is fast, requires k in advance, and is sensitive
to initialization (k-means++ addresses this) and to feature scaling. Hierarchical
clustering builds a dendrogram permitting any number of clusters after the fact, at
quadratic or worse cost. DBSCAN groups by density, finding arbitrary shapes, identifying
outliers as noise, and requiring no k — but needing a density scale that may not be
uniform across the data. Gaussian mixture models fit soft, elliptical clusters by
expectation-maximization and give probabilistic assignments.


classical_machine_learning

Clustering guidance:
  k-means             fast, spherical, needs k, scale-sensitive; use k-means++
  hierarchical        dendrogram, any linkage; O(n^2) memory, good for small n
  DBSCAN              density-based, arbitrary shapes, finds outliers, no k needed
  HDBSCAN             hierarchical DBSCAN; handles varying density, fewer knobs
  GMM                 soft assignment, elliptical, model-based; use BIC to choose k
  spectral            uses graph Laplacian eigenvectors; finds non-convex clusters
  choosing k          elbow on inertia (crude), silhouette score, gap statistic,
                      BIC for model-based; or a downstream task metric
  validation          without labels there is no ground truth; stability across
                      resamples is the most honest internal criterion


classical_machine_learning

The uncomfortable truth about clustering is that it always returns clusters, whether or
not the data has any. Running k-means with k = 5 on uniform random noise produces five
tidy-looking clusters with a plausible silhouette score. Before interpreting clusters as
real structure, it is worth checking whether the clustering tendency exceeds what random
data of the same shape would produce (the Hopkins statistic, or a gap statistic against
a null reference), and whether the same clusters appear under resampling.


classical_machine_learning

## Dimensionality Reduction and Imbalanced Data

Principal component analysis projects onto the directions of maximum variance, which are
the eigenvectors of the covariance matrix or equivalently the right singular vectors of
the centred data. It is linear, deterministic, and invertible up to the discarded
components, and the eigenvalues give the variance explained. Two cautions dominate
practice: PCA is not scale-invariant, so unstandardized features let whichever variable
has the largest units dominate the components; and variance is not relevance, so the
components that explain the most variance may not be the ones that predict the target —
supervised alternatives like partial least squares exist for that reason.


classical_machine_learning

Nonlinear methods preserve local structure at the cost of interpretability and global
fidelity. t-SNE and UMAP produce visually compelling two-dimensional embeddings and are
heavily over-interpreted. In both, cluster sizes are meaningless, distances between
clusters are largely meaningless, and the apparent structure depends strongly on
hyperparameters — perplexity for t-SNE, n_neighbors and min_dist for UMAP. They are
excellent for visual exploration and for generating hypotheses, and they are not
appropriate as a preprocessing step feeding a downstream model, nor as evidence that
clusters are separable.


classical_machine_learning

Class imbalance is a framing problem more often than an algorithmic one. With 1%
positives, accuracy is useless, and the first response should be to choose an appropriate
metric — average precision, or expected cost under the real asymmetry between false
positives and false negatives — rather than to alter the data. Resampling techniques
distort the predicted probabilities, which matters whenever the score is used for ranking
or expected-value decisions rather than a hard classification.


classical_machine_learning

Handling imbalance:
  do nothing first     train on the natural distribution, optimize log loss, and
                       choose the threshold afterward from the cost structure.
                       This is correct more often than the literature suggests.
  class weights        reweight the loss; equivalent to resampling in expectation
                       without duplicating data
  random undersampling discards majority data; fast, loses information
  random oversampling  duplicates minority data; risks overfitting those points
  SMOTE                synthesizes minority points by interpolation; can create
                       implausible examples in high dimensions or across a
                       nonlinear class boundary
  threshold tuning     the highest-value intervention; the default 0.5 is almost
                       never the right operating point
  calibration          Platt scaling or isotonic regression after resampling, to
                       restore probability meaning


classical_machine_learning

Interpretability requirements should shape model choice from the start rather than being
retrofitted. Intrinsically interpretable models — linear models with few terms, shallow
decision trees, generalized additive models, rule lists — expose their reasoning directly
and are the right choice when a decision must be explained to a regulator or contested by
the person it affects. Post-hoc explanation methods applied to black-box models — LIME,
SHAP, partial dependence plots, counterfactuals — approximate the model's behaviour and
can be misleading: LIME's local surrogates are unstable across runs, partial dependence
plots assume feature independence and produce nonsense when features are correlated, and
SHAP values answer a specific attribution question that may not be the one being asked.
An accurate model with a plausible-sounding wrong explanation is worse than a slightly
less accurate model that is genuinely transparent.


computer_vision

## Image Formation and Representation

An image is a projection of a three-dimensional scene onto a two-dimensional sensor, and
the information lost in that projection is what makes vision hard. Depth is not directly
recoverable from a single view, scale and distance are confounded, and the appearance of a
surface depends on illumination, viewing angle, and material properties as much as on the
object's identity. Every vision system is in some sense recovering what the projection
discarded.


computer_vision

The pinhole camera model relates world coordinates to image coordinates through a
perspective projection, parameterized by intrinsics — focal length, principal point, and skew
— and extrinsics giving the camera's pose in the world. Real lenses deviate from the pinhole
model through radial and tangential distortion, which is corrected by calibration: imaging a
known pattern from many angles and solving for the parameters that best explain the observed
positions. Uncalibrated geometry is why measurements from ordinary photographs are unreliable
and why any metric vision task begins with calibration.


computer_vision

Digital images quantize both space and intensity. Spatial sampling introduces aliasing when
the scene contains detail finer than the pixel spacing, which appears as moiré patterns on
regular structures and as jagged edges elsewhere; the remedy is optical low-pass filtering
before sampling, which is why many sensors include an anti-aliasing filter and why removing
it improves sharpness at the cost of moiré. Intensity quantization to eight bits is
adequate for display and inadequate for processing, since operations that stretch contrast
expose the quantization as banding — which is why raw capture and higher bit depths matter
for any pipeline that adjusts exposure.


computer_vision

Image formation concepts:
  pinhole model         perspective projection; parallel lines meet at vanishing
                        points
  intrinsics            focal length, principal point, skew; internal to the camera
  extrinsics            rotation and translation; the camera's pose
  radial distortion     barrel or pincushion; corrected by calibration
  colour spaces         RGB for capture and display, HSV for intuitive
                        manipulation, Lab for perceptual uniformity, YCbCr for
                        compression since chroma can be subsampled
  gamma                 display encoding is nonlinear; linearize before any
                        arithmetic on light
  bit depth             8-bit for display, 12-16 for capture; processing in higher
                        depth avoids banding
  aliasing              from sampling below Nyquist; requires optical prefiltering
  demosaicing           reconstructing colour from a Bayer pattern; a source of
                        artifacts at edges
  dynamic range         the sensor's ratio of saturation to noise floor; the
                        limiting factor in high-contrast scenes


computer_vision

The gamma issue deserves the same emphasis it receives in graphics, because it silently
corrupts image processing. Standard image files are gamma-encoded, so pixel values are not
proportional to light. Averaging, blurring, resizing, and blending performed directly on
encoded values produce results that are systematically too dark, and the effect is most
visible when downsampling — a checkerboard of black and white pixels averaged in encoded
space gives a mid-grey that is far darker than the correct result. Converting to linear
before arithmetic and back afterward fixes it, and libraries that do this by default are the
exception rather than the rule.


computer_vision

Q: Downsampling an image by averaging pixel values directly in an 8-bit sRGB file produces a
result that is too dark. The cause is:
A. Insufficient bit depth
B. sRGB values are gamma-encoded and not proportional to light, so averaging them does not
   average the light
C. The wrong interpolation kernel
D. Chroma subsampling
Reasoning: sRGB applies a roughly 2.2 power curve, so a value of 0.5 corresponds to about 22
percent of the light of a value of 1.0. Averaging encoded values therefore weights bright
regions too little. Linearizing, averaging, and re-encoding gives the correct result.
Answer: B.


computer_vision

## Classical Processing and Features

Filtering is the foundation of classical image processing and remains embedded inside modern
architectures. Convolution with a kernel computes a weighted sum over a neighbourhood, and
the kernel determines the operation: a Gaussian smooths, a Laplacian detects intensity
changes in all directions, and directional derivative kernels detect edges of a particular
orientation. Separability matters for cost — a two-dimensional Gaussian can be applied as two
one-dimensional passes, reducing the work from the square of the kernel size to twice its
size.


computer_vision

Nonlinear filters solve problems linear ones cannot. A median filter removes impulse noise
while preserving edges, because the median of a neighbourhood containing a few outliers is
unaffected by them while a mean is dragged toward them. Bilateral filtering smooths while
preserving edges by weighting neighbours by both spatial and intensity distance, so pixels
across an edge contribute little. Morphological operations on binary images — erosion,
dilation, and their compositions opening and closing — clean up segmentation results by
removing small artifacts and filling small gaps.


computer_vision

Feature detection finds points that can be reliably located across images, and the criterion
is distinctiveness in all directions. A flat region has no localizable position; an edge is
localizable perpendicular to itself and not along it; a corner is localizable in both
directions, which is why corner detectors are the basis of matching. Descriptors then
characterize the local appearance around each point in a way that is invariant to the
transformations expected — rotation, scale, illumination — so that the same physical point
produces a similar descriptor in a different image.


computer_vision

Classical operations:
  Gaussian blur        smoothing; separable; the standard scale-space operator
  Sobel / Scharr       gradient; edge detection with orientation
  Laplacian            second derivative; zero crossings at edges
  Canny                gradient, non-maximum suppression, hysteresis thresholding;
                       still the standard edge detector
  median               removes impulse noise, preserves edges
  bilateral            edge-preserving smoothing
  morphology           erosion, dilation, opening, closing on binary masks
  histogram equalization  contrast enhancement; CLAHE does it locally to avoid
                       amplifying noise in flat regions
  Harris corner        distinctive in both directions
  SIFT / ORB           scale- and rotation-invariant descriptors; ORB is free of
                       patent concerns and much faster
  RANSAC               robust model fitting from data with outliers; essential for
                       geometry estimation from noisy correspondences
  homography           projective transform between planes; image stitching and
                       perspective correction


computer_vision

RANSAC deserves emphasis because it is what makes geometry from real correspondences
possible. Feature matching produces many correct correspondences and a substantial fraction of
wrong ones, and a least-squares fit to that mixture is dominated by the outliers. RANSAC
repeatedly samples the minimum number of correspondences needed to fit the model, counts how
many others agree, and keeps the hypothesis with the most support. The number of iterations
needed grows with the outlier fraction and with the minimal sample size, which is why models
requiring fewer points are strongly preferred in this setting.


computer_vision

## Convolutional Networks

Convolutional networks impose two priors that match natural images: locality, since nearby
pixels are related, and translation equivariance, since a pattern's meaning does not depend on
where it appears. Weight sharing across positions implements both, reducing parameters
enormously relative to a fully connected layer and making learned features
position-independent.


computer_vision

The receptive field — the input region influencing one output unit — grows with depth and is
a real design constraint. Stacking 3x3 convolutions grows it by two per layer, so
capturing context spanning a large fraction of the image requires either many layers,
downsampling to increase the effective field relative to the image, or dilated convolutions
that skip positions and grow the field exponentially without additional parameters.
Architectures that fail on large objects frequently have receptive fields smaller than the
objects.


computer_vision

Residual connections were the change that made very deep networks trainable. Adding the
input of a block to its output gives gradients an identity path to flow along, so the
effective depth for optimization purposes is much less than the nominal depth, and networks of
hundreds of layers train stably where plain stacks of twenty degrade. The reframing worth
carrying is that a residual block learns a correction to the identity rather than a full
transformation, which is an easier optimization problem when the desired mapping is close to
identity.


computer_vision

Convolutional architecture elements:
  convolution           local weighted sum with shared weights
  stride                downsampling by skipping positions
  padding               preserves spatial size; 'same' padding is the usual default
  pooling               downsampling by max or average; increasingly replaced by
                        strided convolution
  1x1 convolution       channel mixing and dimensionality change with no spatial
                        extent; the workhorse of efficient architectures
  depthwise separable   spatial convolution per channel followed by 1x1 mixing;
                        roughly a tenfold reduction in computation
  dilated / atrous      skips positions; exponentially growing receptive field
  residual connection   identity path; enables very deep training
  batch normalization   stabilizes training; couples examples within a batch
  global average pool   replaces flattening plus dense layers; far fewer parameters
                        and translation invariant
  receptive field       must exceed the scale of the structures to be recognized


computer_vision

Data augmentation matters more for vision than for most domains because the invariances are
known and enumerable. Random crops teach translation invariance, horizontal flips teach
mirror invariance where appropriate, colour jitter teaches illumination robustness, and
rotation teaches orientation invariance where the domain permits. The caution is that the
augmentation must reflect an invariance the deployed system actually faces: vertical flips
are appropriate for satellite imagery and inappropriate for photographs of scenes with a
gravitational up, and applying them wastes capacity teaching an invariance that will never be
needed while potentially confusing classes distinguished by orientation.


computer_vision

## Detection, Segmentation, and Beyond Classification

Classification assigns a label to an entire image and is the simplest task. Detection
additionally localizes objects with bounding boxes and must handle a variable number of
outputs, which is the structural difficulty. Two-stage detectors first propose candidate
regions and then classify and refine them, giving high accuracy at higher latency. One-stage
detectors predict boxes and classes directly over a dense grid of positions, which is faster
and historically less accurate, with the gap having largely closed. Transformer-based
detectors treat detection as set prediction with learned queries, removing the hand-designed
anchor boxes and non-maximum suppression entirely.


computer_vision

Segmentation assigns a label per pixel. Semantic segmentation labels each pixel with a class
without distinguishing instances; instance segmentation separates individual objects;
panoptic segmentation combines both, labelling every pixel with a class and, for countable
classes, an instance. Encoder-decoder architectures with skip connections dominate, because
the encoder's downsampling provides context while the skip connections restore the spatial
detail that downsampling destroyed — which is exactly why the U-Net shape recurs across
segmentation, depth estimation, and image-to-image translation.


computer_vision

The evaluation metrics have specific properties worth understanding. Intersection over union
measures overlap between predicted and ground truth regions and is the standard for both
detection and segmentation. Mean average precision integrates precision over recall at a
range of IoU thresholds, which makes it sensitive to localization quality as well as
classification. The threshold choice matters: results reported at IoU 0.5 tolerate loose
boxes, and averaging over thresholds from 0.5 to 0.95 rewards precise localization
substantially more.


computer_vision

Task and evaluation vocabulary:
  classification        one label per image
  detection             boxes plus classes; variable output count
  semantic segmentation per-pixel class, instances merged
  instance segmentation per-pixel class with instances separated
  panoptic              both, covering every pixel
  keypoint / pose       structured point prediction
  depth estimation      per-pixel distance; monocular depth is scale-ambiguous
  IoU                   intersection over union; the standard overlap measure
  mAP                   mean average precision; averaged over classes and IoU
                        thresholds
  non-maximum suppression  removes duplicate detections; a source of failure when
                        objects genuinely overlap
  anchor boxes          predefined shapes; require tuning to the dataset's object
                        aspect ratios
  class imbalance       most positions contain background; focal loss and hard
                        example mining address it


computer_vision

## Video, 3D, and Transformers

Video adds a temporal dimension and the associated redundancy and opportunity. Consecutive
frames are highly correlated, so processing every frame independently wastes computation, and
the motion between them carries information a single frame cannot. Optical flow estimates
per-pixel motion between frames and is the classical representation of that information.
Modern approaches use 3D convolutions over space and time, two-stream architectures
processing appearance and motion separately, or temporal attention.


computer_vision

Three-dimensional vision recovers geometry from images. Stereo uses two calibrated cameras
and computes disparity — the horizontal shift of corresponding points — which is inversely
proportional to depth, so precision degrades quadratically with distance and the usable range
is set by the baseline separation. Structure from motion recovers both camera poses and scene
structure from an unordered image collection. Simultaneous localization and mapping does the
same incrementally in real time, which is what robots and augmented reality devices require.
Neural radiance fields and Gaussian splatting represent a scene as a continuous or
point-based function optimized to reproduce the observed images, achieving photorealistic
novel view synthesis from a modest number of photographs.


computer_vision

Vision transformers apply the transformer architecture to images by splitting them into
patches, embedding each, and treating the sequence with self-attention. They discard the
locality and translation-equivariance priors that convolutions impose, which means they need
substantially more data to reach the same accuracy — the priors were doing real work — and
they exceed convolutional networks when that data is available. Hybrid architectures with
convolutional stems or hierarchical windowed attention recover much of the efficiency while
keeping the flexibility.


computer_vision

Video, 3D, and modern architectures:
  optical flow          per-pixel motion; classical and learned methods
  3D convolution        space-time filtering; expensive
  two-stream            separate appearance and motion pathways
  temporal attention    long-range dependencies across frames
  stereo disparity      depth from two views; precision falls as distance squared
  structure from motion poses and geometry from an image collection
  SLAM                  incremental, real-time mapping and localization
  NeRF / splatting      learned scene representation; photorealistic novel views
  vision transformer    patches plus self-attention; data-hungry, highly flexible
  hierarchical / windowed  restores locality and efficiency to attention
  self-supervised       contrastive or masked-patch pretraining; removes the label
                        bottleneck that dominated the field for a decade
  vision-language       joint image-text embedding; enables open-vocabulary
                        classification and retrieval without task-specific labels


computer_vision

[user] My defect detection model gets 97% accuracy but misses most of the actual defects.
[assistant] tool_call cv.eval_audit({"task": "binary_defect_classification",
"accuracy": 0.97, "positive_rate": 0.028, "recall": 0.19, "precision": 0.61,
"threshold": 0.5, "loss": "cross_entropy", "augmentation": ["flip", "rotate"]})
[tool] {"issue": "class_imbalance_masked_by_accuracy", "majority_baseline_accuracy":
0.972, "model_vs_baseline": -0.002, "recommendations": ["metric_change",
"threshold_tuning", "loss_reweighting", "defect_specific_augmentation"]}
[assistant] Your model is performing slightly worse than a model that predicts "no defect"
every time, which would score 97.2%. Accuracy is meaningless at a 2.8% positive rate and it is
hiding the failure.


computer_vision

Three changes, in order of how quickly they will help.


computer_vision

Change the metric first, because you cannot fix what you are not measuring. Report precision
and recall at your operating point, and average precision over the full curve. For defect
detection, also report recall at a precision level the process can tolerate — if operators
can inspect 10% of flagged parts, the meaningful number is recall at that flag rate.


computer_vision

Tune the threshold. A model trained with cross-entropy on imbalanced data outputs
well-calibrated probabilities that rarely exceed 0.5 for the rare class, so the default
threshold suppresses nearly all positive predictions. Sweeping the threshold and choosing
based on the actual cost of a missed defect versus a false alarm will likely improve recall
substantially with no retraining at all. This is often the entire fix.


computer_vision

Then address training. Focal loss down-weights easy negatives so that the gradient is
dominated by the hard and rare cases, and it was designed for exactly this regime. Class
weighting in the loss is a simpler alternative. Either is preferable to oversampling, which
distorts the probability calibration you need for threshold tuning.


computer_vision

Finally, your augmentation is generic. For defect detection, augmentation should reflect the
variation defects actually exhibit — scale, contrast against the substrate, partial occlusion,
position within the part. Flips and rotations teach invariances that may not even hold if
defects have a characteristic orientation relative to the manufacturing process.


computer_vision

One thing to verify before any of this: check whether the defects you are missing are
visually distinguishable at your input resolution. If the model downsamples a 4000-pixel
image to 224 pixels, a small defect may occupy less than one pixel after resizing, in which
case no amount of loss engineering helps and the answer is tiling the image and running
detection per tile.


data_engineering

## Storage Formats and Architecture

The choice of file format determines more about analytical performance than the query
engine does. Row-oriented formats store all fields of a record contiguously, which suits
transactional access retrieving whole records. Columnar formats store each field
contiguously across records, which suits analytical access reading a few fields across many
records — only the needed columns are read, and homogeneous values within a column compress
far better than heterogeneous values within a row.


data_engineering

Parquet is the standard columnar format for analytics. It stores data in row groups, each
containing column chunks with their own statistics — minimum, maximum, null count — which
enables predicate pushdown: a query filtering on a column can skip entire row groups whose
statistics prove no rows match. This is why the physical ordering of data matters
enormously. A table sorted by the column most queries filter on will skip most of its row
groups; the same data in random order must read everything.


data_engineering

CSV persists because it is universal and should be avoided for anything at scale. It has no
schema, no types, no compression by default, no column pruning, ambiguous handling of
embedded delimiters and newlines, and no way to skip data without parsing it. A CSV file
that is ten gigabytes is typically one to two gigabytes as Parquet and queries an order of
magnitude faster on a column subset.


data_engineering

Format characteristics:
  CSV / JSON lines    universal, no schema, no pruning, no compression; use for
                      interchange, never for analytical storage
  Parquet             columnar, compressed, statistics per row group, predicate
                      pushdown; the analytical default
  ORC                 similar to Parquet; strong in the Hive ecosystem
  Avro                row-oriented with schema evolution; good for streaming and
                      for write-heavy landing zones
  Arrow               in-memory columnar; the interchange standard between
                      processes and languages with zero-copy reads
  compression         Snappy for speed, Zstandard for a better ratio at similar
                      speed, Gzip for maximum ratio and slow decompression
  partitioning        directory structure by a low-cardinality column; enables
                      skipping whole directories
  file size           target 128 MB to 1 GB per file; many small files are the
                      most common performance pathology in data lakes


data_engineering

The small file problem deserves emphasis because it appears in nearly every data lake that
has been running for a while. Streaming ingestion or frequent micro-batch writes produce
thousands of tiny files, and every query pays per-file overhead — opening, reading the
footer, planning — that swamps the actual work. A table of a hundred thousand files of one
megabyte each performs dramatically worse than the same data in a thousand files of a
hundred megabytes. Compaction jobs that periodically rewrite small files into larger ones
are not optional maintenance; they are a requirement for any table written incrementally.


data_engineering

Table formats add a metadata layer over files, and they solve problems that plain
directories of Parquet cannot. Delta Lake, Apache Iceberg, and Apache Hudi each provide
atomic commits so that a reader never sees a partial write, snapshot isolation and time
travel so that a query can read a consistent historical version, schema evolution without
rewriting data, and efficient row-level updates and deletes — which plain immutable files
cannot support and which regulations requiring deletion of individual records demand.


data_engineering

Q: A Parquet table is queried with a filter on a column, and the query still reads the
entire dataset. The most likely reason is:
A. Parquet does not support filtering
B. The data is not sorted or partitioned by that column, so row-group statistics do not
   allow skipping
C. The file is compressed
D. The filter is on a string column
Reasoning: Predicate pushdown skips a row group only when its min/max statistics prove no
rows can match. If the filter column's values are scattered randomly, every row group spans
nearly the full range and none can be skipped. Sorting by the filter column, or partitioning
on it, is what makes skipping effective. Answer: B.


data_engineering

## Batch and Stream Processing

Batch processing operates on bounded datasets at scheduled intervals, and it remains the
right default for most analytical work. It is simpler to reason about, easier to test, and
trivially reprocessable — a bug is fixed by rerunning the job, which is not true of a
streaming system whose state has already been consumed downstream.


data_engineering

Stream processing operates on unbounded data continuously and is justified when latency
genuinely matters. The complexity it introduces is substantial and centres on time. Event
time is when something happened; processing time is when the system saw it. These differ,
sometimes by a lot, because of network delays, client buffering, mobile devices coming back
online after hours offline, and upstream retries. Windowed aggregations must be defined on
event time to be correct, and event time arrives out of order.


data_engineering

Watermarks are the mechanism for handling this. A watermark is an assertion that no events
older than a given timestamp will arrive, which allows a window to be closed and its result
emitted. The tradeoff is direct: an aggressive watermark emits results quickly and drops
late data, while a conservative one waits longer and holds more state. Late-arriving data
after the watermark must be either dropped, sent to a side output for separate handling, or
used to emit a correction that updates the previously published result — and which of these
is acceptable is a business decision rather than a technical one.


data_engineering

Streaming concepts:
  event time            when it happened; the semantically correct basis
  processing time       when the system saw it; easier and usually wrong
  watermark             assertion of completeness up to a timestamp
  tumbling window       fixed, non-overlapping intervals
  sliding window        fixed size, advancing by a smaller step; overlapping
  session window        activity separated by a gap; dynamic boundaries
  late data             arrives after the watermark; drop, side-output, or update
  exactly-once          achieved by idempotent sinks or transactional writes, not
                        by the transport
  checkpointing         periodic state snapshots for recovery
  backpressure          slow consumers must throttle producers, not accumulate
                        unbounded buffers
  state store           keyed state for aggregations and joins; its size is the
                        main operational constraint


data_engineering

The lambda architecture — running batch and streaming paths in parallel and merging their
results — was a common response to streaming systems being unreliable, and it requires
maintaining the same logic twice in two systems, which guarantees they eventually diverge.
The kappa architecture treats everything as a stream and reprocesses by replaying the log,
which requires the log to be retained long enough and the processing to be deterministic.
Modern practice increasingly runs one engine over one storage layer with both bounded and
unbounded modes, which is the same code in both paths and is the right target.


data_engineering

The honest question before building a streaming pipeline is what decision depends on the
latency. If a dashboard is read once a morning, hourly batch is sufficient and vastly
cheaper to build and operate. Streaming is justified by fraud detection, real-time
personalization, operational alerting, and similar cases where minutes matter. Building a
streaming system because it is more sophisticated is a common and expensive error.


data_engineering

## Data Modeling

Analytical data modeling differs from transactional modeling because the access patterns
differ. Transactional systems normalize to eliminate update anomalies, since updates are
frequent and correctness is paramount. Analytical systems denormalize to eliminate joins,
since data is written once and read many times and joins are the dominant cost.


data_engineering

Dimensional modeling organizes analytical data into fact tables and dimension tables. Facts
record events or measurements — a sale, a click, a sensor reading — with foreign keys to
dimensions and numeric measures. Dimensions describe the entities involved — customer,
product, date, store — and are wide, denormalized, and relatively small. The star schema
places dimensions directly around the fact table; the snowflake schema normalizes dimensions
into sub-dimensions, which saves space and adds joins, and is usually not worth it.


data_engineering

The grain of a fact table — what one row represents — is the first and most consequential
modeling decision. It must be declared explicitly and stated in the documentation, because
every subsequent decision depends on it and because mixing grains within a table produces
double counting that is very difficult to detect. One row per order line item and one row
per order are different tables, and a measure that is correct at one grain is wrong at the
other.


data_engineering

Dimensional modeling concepts:
  fact table            events with measures and dimension keys; long and narrow
  dimension table       descriptive attributes; wide and short
  grain                 what one fact row represents; declare it explicitly
  additive measure      summable across all dimensions
  semi-additive         summable across some but not time; account balances
  non-additive          ratios and percentages; store the components instead
  degenerate dimension  an identifier with no attributes, such as an order number
  conformed dimension   shared across fact tables; what allows cross-process
                        analysis
  slowly changing type 1  overwrite; no history
  slowly changing type 2  new row with validity dates; preserves history and is
                        the usual correct choice
  surrogate key         system-generated; required for type 2 dimensions
  date dimension        pre-built with fiscal periods, holidays, weekday flags;
                        always worth building


data_engineering

Slowly changing dimensions are where historical correctness is won or lost. If a customer
moves from one region to another and the dimension is overwritten, every historical sale is
retroactively attributed to the new region and last year's regional report changes when
rerun. Type 2 handling — inserting a new dimension row with validity dates and pointing new
facts at it — preserves the state as of each transaction, so history is stable. The cost is
complexity in the load and the need for surrogate keys, and the benefit is reports that do
not change beneath the people who read them.


data_engineering

[user] Our nightly pipeline takes 6 hours and it used to take 40 minutes. Same data volume
growth of maybe 3x over a year.
[assistant] tool_call data.pipeline_profile({"runtime_h": 6, "baseline_min": 40,
"volume_growth": 3.0, "stage_times": {"ingest": 0.4, "transform": 4.9, "load": 0.7},
"file_count": 384000, "avg_file_mb": 1.4, "partitions": 12, "shuffle_gb": 840})
[tool] {"bottleneck": "transform", "issues": ["small_file_overhead",
"partition_skew_12_partitions", "large_shuffle", "superlinear_scaling"],
"expected_linear_runtime_h": 2.0, "excess_h": 4.0}
[assistant] Volume tripled but runtime grew ninefold, so something is scaling
superlinearly. Three causes, and the file count is the most immediate.


data_engineering

You have 384,000 files averaging 1.4 MB. Every task pays fixed per-file overhead — open,
read footer, plan — and at that size the overhead dominates the actual reading. Compact into
files of 128 MB to 512 MB and this alone typically recovers a large fraction of the runtime.
Add a scheduled compaction job so it does not silently return; incremental writes recreate
this condition continuously.


data_engineering

Twelve partitions is too few for your data volume and is almost certainly causing skew. With
a large shuffle across few partitions, one or two tasks handle disproportionate data while
the rest finish early, so the stage runs as long as its slowest task and your cluster sits
mostly idle. Increase partition count to somewhere around two to four times your total core
count, and check whether the partition key itself is skewed — if one key holds a large share
of rows, salting that key or handling it separately is needed regardless of partition count.


data_engineering

The 840 GB shuffle is worth attacking directly since it is likely the dominant cost. Look at
whether the joins can be reordered to filter earlier, whether a broadcast join is possible
for the smaller side, and whether you are shuffling columns the downstream stages never use —
projecting to needed columns before a shuffle is a common large win that is easy to miss.


data_engineering

One diagnostic to run first: look at the stage-level timings and the task duration
distribution within the longest stage. If the maximum task duration is many times the median,
it is skew and the partition work is the priority. If tasks are uniform and simply numerous,
it is the small files. That distinction tells you which fix to do first rather than doing all
three blindly.


data_engineering

## Orchestration and Quality

Orchestration coordinates the execution of interdependent tasks, and the requirements are
dependency management, scheduling, retries, backfills, and observability. Pipelines are
directed acyclic graphs, and the orchestrator's job is to run each task once its
dependencies have succeeded, retry transient failures, and surface persistent ones.


data_engineering

Idempotency is the property that makes orchestration workable. A task that can be rerun
safely can be retried automatically, backfilled over a historical range, and recovered from
partial failure without manual intervention. Achieving it usually means writing to a
partition determined by the run's logical date and replacing that partition entirely rather
than appending, so a rerun overwrites rather than duplicating. Pipelines built on appends
without deduplication cannot be safely rerun, which means every failure requires a human to
assess what partially completed.


data_engineering

The distinction between logical date and execution time is a persistent source of confusion.
A job scheduled for a given day processes that day's data, and it may execute hours later or
be backfilled months afterward. Every task should derive its input and output locations from
the logical date passed in, and should never call a function returning the current time — a
pipeline that uses the wall clock cannot be backfilled and silently produces wrong results
when a run is delayed past midnight.


data_engineering

Orchestration and quality practices:
  idempotent tasks       rerunnable without duplication; replace partitions
  logical vs actual date derive everything from the logical date, never the clock
  backfill               rerun a historical range; requires idempotency
  retries with backoff   for transient failures; distinguish them from permanent
  SLA monitoring         alert when a pipeline has not completed by a deadline,
                         not only when it fails
  data freshness check   the most valuable single check; stale data that looks
                         current is worse than missing data
  schema validation      types, required fields, allowed values
  volume checks          row counts against expectation; catches partial loads
  distribution checks    means, null rates, category frequencies against history
  referential integrity  foreign keys resolve
  uniqueness             primary keys are unique; duplicates are the most common
                         real defect
  circuit breaker        stop the pipeline rather than propagate bad data
                         downstream


data_engineering

Failing loudly rather than propagating bad data is a decision worth making explicitly.
A pipeline that detects a quality violation and continues publishes wrong numbers that people
act on; one that halts leaves the previous day's data in place and produces an alert. The
second is almost always preferable, because stale data is visibly stale while wrong data is
invisibly wrong. This requires the downstream consumers to tolerate a missed update, which is
a design conversation to have before the incident rather than during it.


data_engineering

Data contracts formalize the interface between producer and consumer. A contract specifies
the schema, semantics, quality guarantees, and change process for a dataset, and is enforced
in the producer's continuous integration so that a breaking change fails the producer's
build rather than the consumer's pipeline at three in the morning. This shifts the cost of a
breaking change onto the party making it, which is where it belongs and where it is
cheapest to address.


data_engineering

## Distributed Computation, Cost, and Governance

Distributed processing frameworks partition data across a cluster and execute operations in
parallel. The fundamental cost distinction is between narrow transformations, where each
output partition depends on one input partition and computation proceeds locally, and wide
transformations, which require a shuffle — redistributing data across the network so that
all records with a given key land together. Shuffles are expensive, involving serialization,
network transfer, and disk spill, and minimizing them is the core of performance tuning.


data_engineering

Data skew is the most common distributed performance problem. When one key holds a
disproportionate share of records, the task processing it takes far longer than the others,
and the stage completes only when it does. Detection is by comparing maximum to median task
duration. Remedies include salting the hot key by appending a random suffix and aggregating
in two stages, broadcasting the smaller side of a join to avoid shuffling the larger, or
separating the hot keys and handling them with a different strategy.


data_engineering

Predicate and projection pushdown are the optimizations with the highest return and the
least effort. Filtering as early as possible reduces every downstream stage's data volume;
selecting only needed columns does the same. Modern query optimizers do a great deal of this
automatically, and the cases they cannot handle are usually those where a user-defined
function opaque to the optimizer sits between the source and the filter — which is a good
reason to prefer built-in expressions over custom functions where possible.


data_engineering

Distributed computation and cost:
  narrow transformation  map, filter; no data movement
  wide transformation    join, groupBy, sort; requires a shuffle
  broadcast join         send the small side to every node; avoids shuffling the
                         large side; bounded by memory
  partitioning strategy  hash for even distribution, range for sorted access
  skew handling          salting, broadcast, or separate treatment of hot keys
  predicate pushdown     filter at the source
  projection pushdown    read only needed columns
  caching                persist a reused intermediate; only when actually reused
  spot instances         large savings for fault-tolerant batch work
  storage tiering        move cold data to cheaper storage on a lifecycle policy
  cost attribution       tag resources by team and pipeline; unattributed cost is
                         unmanaged cost
  the largest saving     deleting data nobody queries, and not computing tables
                         nobody reads


data_engineering

Governance requirements shape architecture rather than sitting alongside it. Data lineage —
knowing which sources produced which derived tables — is required to assess the impact of an
upstream change and to answer where a number came from, and it is far easier to capture
automatically from the orchestrator than to document by hand. Access control must operate at
the column and row level for sensitive data rather than at the table level, since analysts
frequently need aggregate access without individual-record access. Retention policies must be
implemented as automated deletion rather than as documented intentions.


data_engineering

The right to erasure in privacy regulation deserves specific architectural attention because
it conflicts with the append-only immutable design that data lakes naturally adopt.
Deleting one person's records from thousands of Parquet files requires either rewriting those
files, which table formats with row-level delete support now make tractable, or
crypto-shredding — encrypting each subject's data with a per-subject key and deleting the key,
which renders the data unrecoverable without touching it. Choosing one of these before the
first deletion request arrives is substantially cheaper than choosing one afterward.


deep_learning

## Backpropagation and Automatic Differentiation

Backpropagation is the chain rule applied in reverse topological order over a
computation graph. The forward pass computes outputs and caches intermediate
activations; the backward pass propagates the gradient of the scalar loss with
respect to each intermediate, multiplying by local Jacobians as it goes. The
asymmetry that makes it practical is that reverse-mode differentiation costs roughly
one forward pass per *output*, while forward-mode costs one per *input*. Neural
networks have millions of inputs (parameters) and one output (the loss), so reverse
mode is cheaper by a factor of the parameter count. This is the entire reason deep
learning is computationally feasible.


deep_learning

The memory cost is the mirror image of the compute saving. Every activation needed by
a backward operation must be retained from the forward pass, so activation memory
scales with depth times batch size times width. This, not parameter memory, is usually
what makes training OOM. Gradient checkpointing trades compute for memory by
discarding activations and recomputing them during the backward pass, typically
storing only layer boundaries: memory drops from O(n) to O(sqrt(n)) in the number of
layers at the cost of roughly one extra forward pass, about 30% more compute for a
large multiple reduction in memory.


deep_learning

Vanishing and exploding gradients follow from the product structure. The gradient at
layer l is a product of Jacobians from the loss back to l; if their typical singular
values are below one, the product decays geometrically and early layers stop learning,
and if above one, it explodes into numerical overflow. Sigmoid and tanh saturate,
with derivatives bounded by 0.25 and 1 respectively, which is why deep sigmoid
networks were nearly untrainable before the modern toolkit. Three fixes each attack a
different part of the product: ReLU-family activations keep the derivative at exactly
one on the active side, residual connections add an identity path so the Jacobian is
I + something rather than something, and normalization layers keep the scale of
activations stable across depth.


deep_learning

Gradient identities worth having at hand:
  y = Wx + b            dL/dW = (dL/dy) x^T,  dL/dx = W^T (dL/dy),  dL/db = dL/dy
  y = relu(x)           dL/dx = dL/dy * [x > 0]
  y = sigmoid(x)        dL/dx = dL/dy * y(1-y)
  y = tanh(x)           dL/dx = dL/dy * (1 - y^2)
  y = softmax(x)        dL/dx = y * (dL/dy - sum(dL/dy * y))
  softmax + cross-ent   dL/dlogits = p - onehot(target)      (the fused form)
  y = x1 + x2           gradient copies to both branches
  y = x1 * x2           dL/dx1 = dL/dy * x2  (gradients swap)


deep_learning

The fused softmax-cross-entropy gradient p - y is worth committing to memory. It is
both numerically stable — computing softmax and log separately overflows for large
logits, while the log-sum-exp trick inside the fused version does not — and
conceptually clean: the gradient is simply the prediction error. Frameworks expose
this as `cross_entropy(logits, target)` taking raw logits rather than probabilities
precisely so the fusion can happen, and passing already-softmaxed values into it is a
common and quietly damaging bug.


deep_learning

Q: A network uses sigmoid activations in 20 layers and trains extremely slowly, with
early-layer gradients near 1e-9. The most direct architectural fix is:
A. Lower the learning rate
B. Use ReLU activations and residual connections
C. Increase the batch size
D. Add L2 regularization
Reasoning: The sigmoid derivative peaks at 0.25, so 20 layers multiply to at most
0.25^20, around 1e-12 — the gradient vanishes structurally, independent of the
optimizer settings. Lowering the learning rate makes it worse; batch size affects
gradient noise, not its magnitude through depth; L2 shrinks weights, further reducing
the product. ReLU keeps the derivative at 1 where active, and residual connections
give the gradient an identity path that bypasses the multiplicative chain entirely.
Answer: B.


deep_learning

## Architectures

The multilayer perceptron stacks affine transforms and pointwise nonlinearities. The
universal approximation theorem guarantees that a single sufficiently wide hidden
layer can approximate any continuous function on a compact set to arbitrary accuracy,
which sounds like it should settle the architecture question and settles nothing:
the theorem says nothing about how wide, whether gradient descent will find those
weights, or how much data generalization would need. Depth matters empirically
because it buys compositional structure — features built from features — at
exponentially lower parameter cost than width for the functions that natural data
happens to exhibit.


deep_learning

Convolutional networks impose two priors that match images: locality (a pixel's
meaning depends on its neighbours) and translation equivariance (a cat is a cat
wherever it appears). Weight sharing across positions cuts parameters enormously and
makes the learned features position-independent. The receptive field — the input
region influencing one output unit — grows linearly with depth for stacked 3x3
convolutions and exponentially with dilation, and matching the receptive field to the
scale of the relevant structure is a real design constraint. Pooling and strided
convolutions downsample, trading spatial resolution for channel depth and larger
receptive fields; the resulting pyramid of scales is what makes CNNs work on objects
of varying size.


deep_learning

Recurrent networks process sequences by carrying a hidden state, sharing weights
across time steps. The vanilla RNN suffers vanishing gradients over long sequences for
the same product-of-Jacobians reason as deep feedforward nets, compounded because the
same matrix multiplies repeatedly, so its spectral radius determines the fate of all
long-range signal. LSTM and GRU add gating with an additive cell-state path, giving
gradients a near-identity route across time and extending usable context from tens to
hundreds of steps. Their fundamental limit is sequential computation: step t cannot
begin until t-1 finishes, so training cannot parallelize across the time axis, and
that constraint rather than accuracy is what the transformer displaced.


deep_learning

The transformer replaces recurrence with attention, computing all positions in
parallel. Self-attention lets every position attend to every other in a single
operation, so the path length between any two tokens is constant rather than linear
in their distance — long-range dependencies are structurally as easy as short-range
ones. The cost is quadratic: attention is O(n^2 d) in sequence length, which is the
central scaling problem of the architecture and the target of flash attention (an
IO-aware exact reformulation that avoids materializing the n-by-n matrix), sliding
window attention, and the various linear-attention approximations.


deep_learning

Architecture selection heuristics:
  grid-structured, translation-invariant       convolutional
  sequential with strict causality, tiny       GRU/LSTM still competitive
  long-range dependencies, parallel training   transformer
  irregular relational structure               graph neural network
  set-valued input, permutation invariant      deep sets / attention pooling
  very long sequences, linear cost required    state-space models (Mamba, S4)
  small tabular data                           gradient-boosted trees, not deep nets


deep_learning

That last line is worth stating plainly because it is repeatedly relearned: on
small-to-medium tabular data with heterogeneous features, gradient-boosted trees
(XGBoost, LightGBM, CatBoost) usually beat neural networks, train in seconds, need no
normalization, handle missing values natively, and provide honest feature importances.
Deep learning wins where the input has strong structural priors it can exploit —
images, audio, text, graphs — or where the dataset is large enough that
representation learning pays for itself.


deep_learning

[user] I have 8,000 rows and 45 mixed numeric and categorical features predicting
customer churn. What architecture should I use?
[assistant] tool_call ml.dataset_profile({"rows": 8000, "features": 45,
"categorical": 19, "target": "binary", "positive_rate": 0.11})
[tool] {"n": 8000, "d": 45, "cardinality_max": 340, "missing_pct": 0.07,
"class_balance": [0.89, 0.11], "recommendation": "tree_ensemble"}
[assistant] Use gradient-boosted trees — LightGBM or CatBoost — not a neural network.


deep_learning

At 8,000 rows and 45 heterogeneous features this is squarely in the regime where tree
ensembles dominate. They handle the 19 categorical columns natively (CatBoost
especially, with ordered target statistics that avoid the leakage plain target
encoding causes), tolerate the 7% missing values without imputation, are invariant to
monotone feature scaling, and will train in under a minute so you can iterate on
features instead of on architecture.


deep_learning

A neural network here would need one-hot or learned embeddings for the categoricals,
careful normalization of the numerics, and would be fitting far more parameters than
8,000 rows can support — you would spend the project fighting overfitting to reach
parity at best.


deep_learning

Two specifics for the 11% positive rate: do not oversample or reweight by default,
since it distorts the predicted probabilities you likely want for expected-value
ranking. Optimize log loss, check a calibration curve, and pick the decision threshold
afterward from the actual cost of a false positive versus a missed churner. Use
stratified 5-fold CV and report average precision rather than accuracy.


deep_learning

## Optimization

Stochastic gradient descent updates parameters against the gradient of a minibatch
loss. The minibatch introduces noise, and that noise is not purely a nuisance — it
helps escape sharp minima and saddle points, and the ratio of learning rate to batch
size acts as an effective temperature governing how much the optimizer explores. This
is the basis of the linear scaling rule: when you multiply batch size by k, multiply
the learning rate by k to hold the noise scale roughly constant, with a warmup period
to survive the initially large steps.


deep_learning

Momentum accumulates an exponentially weighted average of past gradients, v = beta*v +
grad, and steps along v. It damps oscillation across the steep directions of an
ill-conditioned valley while accumulating speed along the shallow one, effectively
reducing the condition number the optimizer sees. Nesterov momentum evaluates the
gradient at the look-ahead position, giving a correction term that improves the
convergence rate on convex problems and helps modestly in practice.