\documentclass[9pt,a4paper]{article}
\usepackage[a4paper,top=7mm,bottom=6mm,left=7mm,right=7mm]{geometry}
\usepackage{fontspec}
\setmainfont{Inter}
\usepackage{xcolor,tabularx,array,enumitem,amsmath}
\usepackage[most]{tcolorbox}
\usepackage{microtype}
\usepackage{amssymb}
\usepackage{hyperref}
\hypersetup{colorlinks=true,urlcolor=blue!60!black}
\definecolor{ink}{HTML}{172B36}
\definecolor{muted}{HTML}{53636B}
\definecolor{navy}{HTML}{102A43}
\definecolor{blue}{HTML}{1261A0}
\definecolor{cyan}{HTML}{DDF3F7}
\definecolor{pale}{HTML}{F2F6F8}
\definecolor{line}{HTML}{C8D5DC}
\definecolor{orange}{HTML}{A94D00}
\color{ink}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlist[itemize]{leftmargin=2.8mm,itemsep=0pt,topsep=.5pt,parsep=0pt,partopsep=0pt,label=\textcolor{blue}{\raisebox{.2ex}{\tiny$\blacktriangleright$}}}
\newcommand{\term}[2]{\textbf{#1} #2\par}
\newcommand{\trade}[1]{\textcolor{orange}{\textbf{$\leftrightarrow$ #1}}}
\newcommand{\sect}[1]{\vspace{1.2pt}{\color{navy}\bfseries\fontsize{7.75}{8.35}\selectfont #1}\par\vspace{.4pt}\textcolor{line}{\rule{\linewidth}{.4pt}}\vspace{.35pt}}
\newcommand{\algo}[3]{\textbf{#1} — #2\\[-1.4pt]{\color{muted}\textit{Tune:} #3}\par\vspace{.45pt}}
\newcommand{\tighttable}{\renewcommand{\arraystretch}{1.08}\setlength{\tabcolsep}{1.5pt}}
\begin{document}
\fontsize{6.8}{7.65}\selectfont

\begin{tcolorbox}[colback=navy,colframe=navy,boxrule=0pt,arc=1.2mm,left=3mm,right=3mm,top=1.8mm,bottom=1.8mm]
{\color{white}\bfseries\fontsize{15.5}{16.5}\selectfont MACHINE LEARNING \hfill CHEAT SHEET}\par
\vspace{1pt}{\color{white!86}\fontsize{7.8}{9}\selectfont Foundations · evaluation · probability/statistics · classic ML · deep learning · Transformers/LLMs}
\end{tcolorbox}
\vspace{1.2pt}

\begin{minipage}[t]{0.492\textwidth}
\sect{1 · MODEL REASONING FRAME}
\begin{tcolorbox}[colback=cyan,colframe=cyan,boxrule=0pt,arc=.8mm,left=2mm,right=2mm,top=1.2mm,bottom=1.2mm]
\textbf{Task/target → data/split → baseline → metric → model/knobs → trade-off → validation.} Consider data size/types, error costs, latency and interpretability. State assumptions; prefer a simple, measurable baseline before adding complexity.
\end{tcolorbox}

\sect{2 · FOUNDATIONS \& GENERALISATION}
\term{Parameter}{learned from data (weights, split thresholds). \textbf{Hyperparameter}: chosen outside training (depth, $\lambda$, learning rate), tuned on validation/CV—not test.}
\term{Loss / objective}{per-example error / loss plus constraints or regularisation. Empirical risk minimisation minimises average training loss.}
\term{Overfit}{low train error, high validation error: model learned noise. \textbf{Underfit}: both high: insufficient capacity/features/training.}
\term{Bias–variance}{simple models: high bias/low variance; flexible models: lower bias/higher variance. More data mainly reduces variance.}
\term{Regularisation}{prefers simpler solutions by penalising complexity. Stronger $\lambda$ $\Rightarrow$ more bias, less variance; tune at minimum validation error. \trade{underfit vs overfit}}
\begin{itemize}
\item \textbf{L2 / ridge} $+\lambda\|w\|_2^2$: smoothly shrinks weights; stable with correlated features.
\item \textbf{L1 / lasso} $+\lambda\|w\|_1$: sparse/feature selection; unstable among correlated features.
\item \textbf{Trees:} prune/limit depth/leaves. DL: weight decay, dropout, augmentation, early stopping.
\end{itemize}
\term{Split correctly}{train fits; validation selects; test estimates once. Stratify labels; for time use forward splits; for users/groups use group splits. Fit preprocessing inside each fold.}
\term{Leakage}{training sees unavailable future/target/test information; yields deceptively good evaluation. Audit timestamp and unit of prediction.}
\term{Cross-validation}{$k$ fits; mean estimates performance, spread its instability. Nested CV gives honest evaluation after tuning.}
\term{Scaling}{needed for distance/gradient models (kNN, SVM, linear/NN); trees largely invariant. Impute/encode using train only.}

\sect{3 · METRICS: MATCH THE COST}
\tighttable
\begin{tabularx}{\linewidth}{>{\bfseries}p{18mm}X}
Regression & MAE robust/linear cost; RMSE punishes large errors; $R^2$ vs mean baseline.\\
Classification & Precision $=TP/(TP+FP)$; recall $=TP/(TP+FN)$; F1 balances both. Threshold controls precision $\leftrightarrow$ recall.\\
Ranking / prob. & PR-AUC for rare positives; ROC-AUC ranks broadly; log loss/Brier assess probabilities. Calibrate if probabilities drive decisions.\\
Imbalance & Stratify; class/sample weights; resample train only; report PR curve + confusion matrix at business threshold. Accuracy can mislead.\\
\end{tabularx}

\sect{4 · PROBABILITY, STATS \& A/B}
\term{$E[X]$, $Var(X)$}{centre and spread; $Var(X)=E[(X-E[X])^2]$. Covariance is scale-dependent; correlation normalises to $[-1,1]$—neither implies causation.}
\term{Conditional probability}{$P(A|B)=P(A\cap B)/P(B)$. Bayes: posterior $\propto$ likelihood $\times$ prior. Independence: $P(A,B)=P(A)P(B)$.}
\term{LLN / CLT}{sample mean converges to expectation / its sampling distribution tends normal under conditions; $SE(\bar x)=s/\sqrt n$. More $n$ narrows uncertainty.}
\term{Confidence interval}{procedure whose intervals cover the true value in e.g. 95\% of repeated samples—not 95\% probability for this fixed interval.}
\term{Hypothesis test}{$H_0$ vs $H_1$; p-value is $P(\text{data at least this extreme}\mid H_0)$, not $P(H_0\mid data)$. $\alpha$ = Type-I/false-positive risk; $\beta$ = Type-II; power $=1-\beta$.}
\term{A/B recipe}{predefine unit, primary metric, MDE, $\alpha$/power and horizon → randomise → check assignment/guardrails → estimate effect + CI → practical, not only statistical, significance. Avoid peeking/multiple tests or correct them.}
\term{Common tests}{means: t-test (Welch by default); proportions: z/$\chi^2$; paired/repeated: paired test; non-normal robust option: bootstrap/permutation. Dependence demands cluster-aware SEs.}
\term{Optimisation}{gradient points uphill; GD steps $\theta\leftarrow\theta-\eta\nabla L$. Convex: local=global; non-convex NNs rely on useful minima. Too-large $\eta$ diverges; too-small is slow.}

\sect{5 · CLASSIC ML: HOW + KNOBS}
\algo{Linear / logistic regression}{linear score; logistic maps it through sigmoid to class probability. Fast, interpretable baseline; linear boundary unless features expand it.}{$\lambda$ or \texttt{C}$=1/\lambda$, L1/L2, class weight, decision threshold.}
\algo{Decision tree}{greedy feature splits reduce impurity. Nonlinear/interactions, no scaling; unstable and overfits.}{\texttt{max\_depth}, \texttt{min\_samples\_leaf/split}, max features, pruning $\alpha$.}
\algo{Random forest}{bagged trees on bootstrap rows + random feature subsets; averaging reduces variance. Robust/parallel, less interpretable.}{number of trees (stability$\uparrow$, cost$\uparrow$), max features, depth/min leaf, bootstrap, class weight.}
\algo{Gradient boosting / XGBoost}{sequential shallow trees fit residual/negative gradients; often strongest tabular model.}{learning rate $\downarrow$ needs trees $\uparrow$; depth/leaves, subsample/columns, L1/L2, early stopping. \trade{accuracy vs time/overfit}}

\sect{9 · RAPID-FIRE DISTINCTIONS}
\tighttable
\begin{tabularx}{\linewidth}{>{\bfseries}p{23mm}X}
Generative vs discr. & Model $p(x,y)$ or generate $x$ / model $p(y|x)$ or decision boundary.\\
Bagging / boosting & Parallel averaging lowers variance / sequential correction lowers bias (and can overfit).\\
Epoch / batch / step & Full pass / examples per update / one optimiser update.\\
Calibration / ranking & Probabilities match frequencies / positives rank above negatives.\\
Data / concept drift & Input $p(x)$ changes / relationship $p(y|x)$ changes. Monitor both.\\
\end{tabularx}

\begin{tcolorbox}[colback=pale,colframe=line,boxrule=.45pt,arc=.8mm,left=2mm,right=2mm,top=1.2mm,bottom=1.2mm]
\textbf{Production checklist:} reproducible pipeline and baseline; representative split; leakage checks; metric plus business cost; calibration and threshold; slice/fairness tests; latency, memory and cost; monitoring for data/concept drift; rollback plan and retraining trigger.
\end{tcolorbox}
\end{minipage}\hfill
\begin{minipage}[t]{0.492\textwidth}
\sect{5 · CLASSIC ML, CONTINUED}
\algo{SVM}{maximum-margin separator; kernels create nonlinear similarity features. Strong medium-sized/high-dimensional data; expensive at large $n$.}{\texttt{C}: violations vs margin; kernel; RBF $\gamma$: locality/complexity. Scale inputs.}
\algo{k-nearest neighbours}{predict from nearby labelled examples. No training, local/nonlinear; slow inference, curse of dimensionality.}{$k$: small=flexible/noisy, large=smooth/biased; distance, weights. Scale features.}
\algo{Naive Bayes}{Bayes with conditionally independent features. Extremely fast; good text baseline; probabilities often poor.}{Laplace smoothing $\alpha$; Gaussian variance smoothing / distribution family.}
\algo{k-means}{alternate nearest-centroid assignment and centroid update; minimises within-cluster squares. Spherical, scaled clusters.}{$k$, initialisation (k-means++), restarts, tolerance. Select with stability/business use, not silhouette alone.}
\algo{PCA}{orthogonal directions of maximum variance via eigendecomposition/SVD. Compression/denoising; components less interpretable.}{number/variance of components; whitening. Centre (usually scale) first.}

\sect{6 · MODEL CHOICE \& DIAGNOSIS}
\tighttable
\begin{tabularx}{\linewidth}{>{\bfseries}p{22mm}X}
Start & Dummy → regularised linear/logistic → tree ensemble; choose complexity only if validation and constraints justify it.\\
Train bad; val bad & Underfit: better features, less regularisation, more capacity/training; verify optimiser/data.\\
Train good; val bad & Overfit: more/cleaner data, stronger regularisation, simpler model, augmentation/early stopping; fix leakage/split first.\\
Learning curves & Gap that persists suggests variance; both plateau badly suggests bias; more data helps variance, not fundamental bias.\\
Explain & Linear coefficients (after scale); tree permutation importance/SHAP. Correlation and importance are not causality. Check slices/drift.\\
\end{tabularx}

\sect{7 · DEEP LEARNING}
\term{Mechanism}{layers compose affine transforms + nonlinear activations; backprop uses chain rule to compute gradients; minibatch optimisation updates weights. Capacity comes from width/depth.}
\term{Architecture knobs}{layers/width (capacity ↔ compute/overfit); activation (ReLU/GELU); residual paths aid deep optimisation; batch/layer norm stabilise scale; initialisation preserves signal.}
\term{Training knobs}{learning rate is usually most important; batch size (noise/memory/throughput); epochs/early stopping; scheduler/warmup; dropout; weight decay; augmentation; gradient clipping; seed.}
\term{SGD + momentum}{cheap, noisy, can generalise well; slower/tuning-sensitive. Momentum smooths and accelerates consistent directions.}
\term{Adam}{adaptive per-parameter steps + momentum; fast/easy default, more memory; $\beta_1,\beta_2,\epsilon$.}
\term{AdamW}{decouples weight decay from Adam update; standard Transformer choice. Tune learning rate + weight decay.}
\term{RMSProp / Adagrad}{adaptive moving-square / accumulated-square gradients; useful for some recurrent/sparse settings; Adagrad learning rate can vanish.}
\term{Stable recipe}{normalise → sensible init → AdamW or momentum SGD → LR warmup + decay → monitor train/val → checkpoint/early-stop → clip exploding gradients. Vanishing gradients: residuals/norm/gated units.}

\sect{8 · TRANSFORMERS \& LLMs}
\term{Transformer, roughly}{token IDs → embeddings + position → repeated blocks: masked multi-head self-attention → residual + layer norm → position-wise MLP → residual + norm → vocabulary logits. Decoder-only LLM trains next-token cross-entropy (teacher forcing).}
\term{Attention}{$Q=XW_Q, K=XW_K, V=XW_V$; $\mathrm{softmax}(QK^\top/\sqrt{d_k})V$. Each token mixes relevant token values; heads learn different relations. Causal mask blocks future tokens. Cost is $O(n^2)$ in context length.}
\term{Model hyperparameters}{layers, hidden size, heads/head dimension, MLP size, context length, vocabulary, dropout; training: tokens, batch, LR/warmup/schedule, AdamW/weight decay, precision. \trade{quality vs compute/memory/latency}}
\term{Generation knobs}{temperature $\downarrow$ = sharper/deterministic; top-$p$/top-$k$ truncate tail; max tokens; repetition penalty; beam search favours likely sequences but can be bland. These do not change weights.}
\term{Fine-tuning ladder}{(1) define task + held-out eval; (2) clean/deduplicate prompt–response data; (3) continued pretraining for domain language or \textbf{SFT} for behaviour; (4) full FT or \textbf{LoRA} low-rank adapters / \textbf{QLoRA} quantised base; (5) preference tuning \textbf{DPO} or RLHF; (6) safety, regression, slice and human eval. Low LR; mask prompt loss when appropriate.}
\term{Distillation}{train a smaller student to match teacher soft logits/distributions (KL at temperature $T$), outputs or reasoning traces, often mixed with hard-label loss. Gains speed/cost; loses capacity and can inherit teacher errors. Distillation transfers behaviour; quantisation reduces numeric precision; pruning removes weights.}
\term{RAG vs tuning}{RAG retrieves current, attributable knowledge into context; tuning changes behaviour/style/domain patterns but is a poor database. Combine when needed.}

\end{minipage}
\end{document}
