Linear Methods
Metrics
Primary purpose is to minimize some error metric:
[math]\displaystyle{ \displaystyle \textrm{RMSE(}\textbf{X}\textit{,h}) = \sqrt{\dfrac{1}{m} \sum^m_{i=1}(h(\textbf{x}^{(i)} - y^{(i)})^2} }[/math]
[math]\displaystyle{ \displaystyle \textrm{MAE(}\textbf{X}\textit{,h}) = \dfrac{1}{m}\sum^m_{i=1} | h(\textbf{x}^{(i)} - y^{(i)} }[/math]
RMSE gives the Euclidean norm, [math]\displaystyle{ \ell_2 }[/math]. MAE gives the Manhattan norm, [math]\displaystyle{ \ell_1 }[/math]. A higher norm index overweights large values relative to small ones. This makes RMSE more sensitive to outliers than MAE. In general,
[math]\displaystyle{ \|\textbf{v}\|_k = (|v_0|^k + |v_1|^k + \dots + |v_n|^k)^{1/k} }[/math]
Linear Regression
- [math]\displaystyle{ \hat{y} = \theta_0 + \theta_1x_1 + \theta_2x_2 + \cdots + \theta_nx_n = h_\theta(\textbf{x}) = \boldsymbol{\theta} \cdot \textbf{x} }[/math]
- [math]\displaystyle{ \mathbf{\theta} }[/math] is the parameter vector. The bias term is [math]\displaystyle{ \theta_0 }[/math] and the feature weights are the [math]\displaystyle{ \theta_1 \dots \theta_n }[/math]
- [math]\displaystyle{ \textbf{x} }[/math] is the feature vector of [math]\displaystyle{ x_0 \dots x_n }[/math] where [math]\displaystyle{ x_0 = 1 }[/math]
- [math]\displaystyle{ \boldsymbol{\theta}\cdot \mathbf{x} }[/math] is the dot product of feature weights and features
- Solve using the normal equation:
- [math]\displaystyle{ \boldsymbol{\hat{\theta}} = (\mathbf{X}^T\mathbf{X})^{-1} \,\mathbf{X}^T\,\mathbf{y} }[/math]
- Performance Metrics
- [math]\displaystyle{ R^2 = 1 - \dfrac{SS_\text{res}}{SS_\text{tot}} }[/math], where
- [math]\displaystyle{ SS_\text{tot} = \displaystyle\sum_i (y_i-\bar{y})^2 }[/math],
- [math]\displaystyle{ SS_\text{res} = \displaystyle\sum_i e_i^2 = \sum(y_i - \hat{y}_i)^2 }[/math], and
- [math]\displaystyle{ SS_\text{reg} = \displaystyle\sum_i (\hat{y}_i-\bar{y})^2 }[/math]
- [math]\displaystyle{ SS_\text{tot} = SS_\text{reg} + SS_\text{res} }[/math]
- [math]\displaystyle{ F = \dfrac{\left(\dfrac{\text{RSS}_1-\text{RSS}_2}{p_2-p_1}\right)}{\left(\dfrac{\text{RSS}_2}{n-p_2}\right)} }[/math]
- [math]\displaystyle{ \text{Adj} R^2 = 1 - (1-R^2)\dfrac{n-1}{n-p-1} }[/math] with p independent variables and n observations.
- Correlation & variance: [math]\displaystyle{ \qquad\rho_{X,Y} = \dfrac{\text{cov}(X,Y)}{\sigma_{X}\sigma_{Y}} }[/math]
- Derivation of normal equation
- [math]\displaystyle{ \text{RSS}(\beta) = (\mathbf{y} - \mathbf{X}\beta)^T\,(\mathbf{y} - \mathbf{X}\beta) }[/math]
which is quadratic and can be differentiated as
- [math]\displaystyle{ \dfrac{\partial\text{RSS}}{\partial\beta} = -2\mathbf{X}^T(\mathbf{y} - \mathbf{X}\beta) }[/math]
- [math]\displaystyle{ \dfrac{\partial^2\text{RSS}}{\partial\beta\,\partial\beta^T} = 2\mathbf{X}^T\mathbf{X} }[/math]
Confusingly, the literature also uses ESS (explained sum of squares) to mean [math]\displaystyle{ SS_\text{reg} }[/math] and RSS (Residual sum of squares) to mean [math]\displaystyle{ SS_\text{res} }[/math]. In addition, for the formulae above the acronyms SST (total), SSE (errors/residuals) and SSR (regression) are also in use.
| name 1 | name 2 | formula | meaning |
|---|---|---|---|
| TSS Total sum of squares |
SST Sum of squares, total |
[math]\displaystyle{ \displaystyle\sum (y_i-\bar{y})^2 }[/math] | observed vs. mean |
| ESS Explained sum of squares |
SSR Sum of squares, regression |
[math]\displaystyle{ \displaystyle\sum (\hat{y}_i-\bar{y}_i)^2 }[/math] | predicted vs. mean |
| RSS Residual sum of squares |
SSE Sum of squared errors |
[math]\displaystyle{ \displaystyle\sum (y_i-\hat{y})^2 }[/math] | observed vs. predicted |
Logistic Regression
Part of the Generalized Linear Models family
- Models made from a linear combination of features
- A link function connects the mean of the output variable with the linear combo of features
- Underlying dependent variable has an underlying distribution in the exponential family. Usually, binomial for binary variables, Poisson if it's a count variable, and Gaussian if continuous.
Logistic regression, unlike linear regression, is a classifier. It generates probabilities rather than scalar estimates. Some terminology:
- The percentage [math]\displaystyle{ p }[/math] is the number of times something happens out of a hundred: [math]\displaystyle{ \dfrac{p}{100} }[/math]
- The odds are the ratio of times an event happens to the number of times it doesn't. If event [math]\displaystyle{ x }[/math] happens [math]\displaystyle{ p=75\% }[/math] of the time, the odds are 3:1 that it happens. The odds ratio 3:1 derives from:
- [math]\displaystyle{ \dfrac{p}{(1-p)} = \dfrac{0.75}{(1-0.75) = 0.25} = 3 }[/math]
- The logit function or log-odds is the log of the odds p/(1-p):
- [math]\displaystyle{ \text{logit}(p) = \log \left(\dfrac{p}{1-p} \right) = \log(p) - \log(1-p) }[/math]
- The sigmoid function is
- [math]\displaystyle{ \sigma(x) = \dfrac{1}{1+e^{-x}} }[/math]
- The logistic function:
- [math]\displaystyle{ P(Y=1|X) = \dfrac{\exp({\beta_0+\beta_1x})}{1+\exp({\beta_0+\beta_1x})} = \dfrac{1}{1+ e^{-(\beta_0+\beta_1 x)}} }[/math]
- Three points
1. The link function [math]\displaystyle{ g(p)=\log(p/(1-p)) }[/math] links the mean response [math]\displaystyle{ p = \mathbb{E}[y] }[/math] to the linear predictor
2. The distributions ("error structure") belong to the Bernoulli family [math]\displaystyle{ B(p) }[/math] which are a special case of the binomial family [math]\displaystyle{ B(n,p) }[/math], given by
- [math]\displaystyle{ P(X=k) = \binom{n}{k}p^k(1-p)^{n-k} }[/math]
3. The variance is given by: [math]\displaystyle{ V(p)=p(1-p) }[/math]
- Unlike linear regression which uses OLS to determine coefficients, logistic regression seeks to maximize a likelihood function:
- [math]\displaystyle{ \mathcal{L}(\theta|x) = p_\theta(x) = P_\theta(X=x) }[/math]
- Of which the log likelihood is often more tractable:
- [math]\displaystyle{ \ell(\beta_0, \beta_1) = \displaystyle \prod_{i:y_i=1} p(x_i) \prod_{i':y'_i=0} (1-p(x'_i)) }[/math]
- Measures of goodness of fit include AIC, null and residual deviance, and the confusion matrix
- AIC (Akaike Information Criterion): [math]\displaystyle{ \textrm{AIC} = 2k - 2\ln(\hat{L}) }[/math]
- Null and Residual Deviance:
- Confusion Matrix. Here is some Python code that explains it compactly:
def deconfuse(tn, fp, fn, tp):
print('Precision:', tp/(tp+fp))
print('Recall (TPR):', tp/(tp+fn))
print('Specificity (TNR):', tn/(tn+fp))
print('Accuracy:', (tp+tn)/(tp+tn+fp+fn))
print('F1-score:', tp/(tp+(fn+fp)/2))
Generalized Linear Models
Beyond the scope of this page, but try here