Linear Regression
This is a page for closed-form and approximation methods to the Linear Regression problem.
The derivation will take the form of assuming a normal distribution on the conditional expectation \[\mathcal{E}[Y|X=x]\].
Closed form of OLE
Taking the derivative of \[\hat{\beta} = \arg \min_{\beta} \frac{1}{n}\|y-X \beta\|^2_2\] and equating to 0: \[\begin{align*}\hat{\beta} &= \arg \min_{\beta} \frac{1}{n}\|y-X \beta\|^2_2\\ &= (X^TX)^{-1}X^T y\end{align*} \]
Example Pred
import numpy as np
# synthetic data for the rest of the linear models:
np.random.seed(5)
n = 100 # samples
p = 5 # features
sigma = 0.2 # std
X = np.random.normal(0, 1, size=(n,p))
beta_true = np.random.randint(-4, 2, p)
noise = np.random.normal(0, sigma, size=(n))
y = X @ beta_true + noise
betahat = np.linalg.inv(X.T @ X) @ X.T @ y
print("betahat: ", betahat)
print("beta true:", beta_true)
Iterative Approach
An idea that permeates throughout all of Machine Learning is that
sometimes, you cannot explicitly solve the `argmin` formulation of the parameters in closed form
and other times, it may be possible, but just computationally infeasible
Gradient Descent
Stochastic Gradient Descent:
\[\hat{\beta}^{(k+1)} = \hat{\beta}^{(k)} - \eta\nabla_\beta L (\hat{\beta}^{(k)})\]
in which we iterate only over 1 sample at a time, and,
Batch Gradient Descent:
\[TODO\]
in which we iterate over the entire dataset.
Note, in practice you will choose mini-batch Gradient Descent, which is a mediation of both these approaches: \[TODO\]
A notebook
Backlinks (6)
1. Logistic Regression /wiki/ml/supervised/regression/logistic/
logistic regression is the method that seems only ever to be used for classification yet insists on calling itself regression. the resolution: it is regression — of the log-odds of a bernoulli success probability onto a linear predictor. 𐃏 this page develops it the honest way, as a generalised linear model: bernoulli response, canonical logit link, likelihood fitted by fisher scoring, inference through the deviance. the machine-learning reading (cross-entropy loss, linear decision boundaries) falls out at the end as a corollary.
2. Locally Weighted Regression /wiki/ml/supervised/regression/locally-weighted/
a straight line is too rigid for a wiggly world, and a global degree-9 polynomial is a hostage negotiation. 𐃏 locally weighted regression (LWR — and its robust cousin LOWESS) takes the diplomatic route: fit the simplest possible model, but fit it freshly at every query point, paying attention only to the training points nearby.
motivation
- linear regression commits to one \(\theta\) for the whole input space. if the true \(f\) bends, the residue of that commitment is bias everywhere.
- the fix need not be a fancier global family. any smooth function is locally linear — taylor says so — so a linear fit weighted toward a neighbourhood of \(x_0\) can track an arbitrary smooth \(f\).
- the price: there is no longer a “trained model”. LWR is memory-based and non-parametric — like k-nearest neighbours, it keeps the entire training set and does all of its work at prediction time. training is \(O(1)\); every query costs a fresh weighted least-squares solve. 𐃏
the estimator
weighted least squares at a query point
fix a query \(x_0\). assign each training point a weight \(w_i(x_0) \ge 0\) that decays with distance from \(x_0\), then solve the weighted least-squares problem
3. California Housing /tags/california-housing/
Median house values for 20,640 California census block groups from the 1990 census. The modern default for “show me a real regression problem”: big enough to be non-trivial, small enough to fit anywhere, and full of instructive pathologies — capped targets, aggregate features, and spatial structure.
Provenance
Constructed by R. Kelley Pace and Ronald Barry for Sparse Spatial Autoregressions, Statistics and Probability Letters 33(3), 1997 — the point of the paper was spatial statistics, not machine learning. The data derive from the 1990 US census at the block group level (the smallest census unit, typically 600 to 3,000 people). It was long distributed via CMU’s StatLib archive; sklearn’s fetch_california_housing mirrors that original. A cosmetically extended variant (with an ocean_proximity categorical) is the running example in Geron’s Hands-On Machine Learning, chapter 2 — numbers from the two variants are not interchangeable.
4. Regularised Regression /wiki/ml/supervised/regression/regularised/
this page collects the closed-form solutions to regularised regression (where they exist) and the iterative approximations we fall back on (where they don’t). 𐃏 along the way we will see that regularisation is not an ad-hoc hack but a perfectly sensible artefact of estimation: it drops straight out of MAP (maximum a posteriori) inference once you put a prior on the coefficients.
5. Wiki /wiki/
Knowledge is a paradox. The more one understand, the more one realises the vastness of his ignorance.
6. Regression /wiki/ml/supervised/regression/
There are many flavours of regression, each with their own assumptions, loss functions and strengths. This directory contains depth studies of each of the following flavours, including derivations and approximate / closed-form implementations.
“By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and in effect increases the mental power of the race.”—Alfred North Whitehead
“Mathematics is the art of giving the same name to different things.”—Henri Poincaré