### Linear regression

We transpose $w$ because by convention we express all vectors as column vectors here.

### Mean square error (MSE)

which $x^i$ and $y^i$ is the input features and the true value for the $i th$ datapoints in the training dataset.

MSE is popular because it is easy to compute and it has a smooth differentiable To optimize $w$, we take its differentiate.

Mean square error (MSE) is actually the L2-norm:

Since sometimes it is so common, we often drop its subfix.

Transform x to:

### Optimize MSE cost

Here we calculate a generic formula to optimize $J$.

Setting $\nabla_w J = 0$ to optimize $J$:

In the following equation, we can solve $x$ using linear algebra:

We will adopt the following notation saying $x$ is computed by linear algebra using $A$ and $b$:

Recall

$w$ can therefore be solved by:

Notice that the solution for $w$ are not unique.

MSE is also vulnerable to outlier. With an outlier, our model is shifted from the blue line to the red line which the blue line can model the training dataset better if the outlier is not there.

### L2 regularization with mean square error (MSE)

To avoid overfitting, we use L2-norm as a regularization to the cost function $J$. (With the MSE computed in the previous section)

Optimize $J$:

With L2 normalization and MSE, $w$ is:

Let’s visualize the solution. In the diagram below, $W^*_a$ is where regularization cost is 0. i.e. all $w_i = 0$. $W^*_b$ is where MSE is minimum. The optimal solution for $J$ is where the concentric circle meet with the eclipse $W^*$.

This is also called ridge regression.

### Regression tree

Linear regression models a linear relationship between input and output. We can combine decision tree with linear regression to create a non-linear model.

### Change of basis

Non-linearity can be produced by a change of basis also. For example, to model the following quadratic relation:

We can transform $x$:

To:

and then apply:

To apply a quadratic functions with 2 features:

We transfrom $x$ to $z$ with:

Note: We are not restricted to polynomial functions: any functions including exponentials, logarithms, trigonometric functions can be applied to the basis.

### L1-norm as cost functions (Robust regression) or regularization

We are going to explore more cost functions besides MSE. MSE has issues with outliners. It exaggerate the cost by squaring the error. MSE tries harder to fit the outlier into the model. A L1-norm cost function has the same incentive to reduce error from the outlier or a normal data and hence less vulnerable to outlier. Nevertheless, the differential at 0 is not smooth which sometimes impact the effectiveness of the gradient descent in those area.

Besides using L-1 as the error cost, L1-norm can be added to the cost function as a regularization, the optimal solution $w^*$ for the L1 regularization usually push some of the $w_i$ to be exactly 0.

L1-norm or L1 regularization promotes sparsity for $w$ which can be desirable to avoid overfitting in particular when we have too many input features. Alternatively, L1-norm can be used for feature selection by eliminate features with $w_i=0$.

#### Huber loss

Huber loss smoothes the cost function when $w$ approach 0 so it is differentiable.

The blue curve is the MSE, the red is L1-norm and the green is the Huber loss which tries to smooth out at $w=0$.

### L0-norm as regularization

We can add a L0 norm to the regularization. L0-norm even favor more sparsity in $w$ than L1-norm.

For example, if $w=(1, 2, 0, 0, 1)$, the L0-norm regularization is 3.

Since training data is not unlimited, even a true model may have $w_i=0$, the MSE cost $\sum^N_{i=1} \| w^Tx -y \|^2$ is likely not bottom at $w_i=0$. When we add L0 regularization, we add a constant $\lambda$ to the MSE cost except at $w_i=0$ which has 0 regularization cost. The total cost shift upward to the green line with the exception at the green dot which remain unchanged.

Without regularization, the optimized $w$ is at the orange dot. With L2 regularization, we may shift the orange dot slightly to the left. However, with a L0 regularization, we can move the optimal point to the green dot subject to the value of $\lambda$.

### Marginal loss (Hinge loss)

Let’s consider a problem to create a boundary to separate the green and red dots below. With a linear regression with MSE cost function, we create a red line to divide the green dots from the red dots. If $wx_i - y_i > 0$, it is classified as green. As shown 3 green dots will be mis-classified.

MSE cannot provide an optimal solution (the blue line) because it mis-calculates the cost value. At point $x_i$, it should be already classified correctly but yet it contributes $d^2$ to its cost function. Hence, for classification problem, we need a cost function that does not penalize when a good decision is make.

For example, for the green dots, if $wx_i \geq 1$, the penalty should be set to 0 and we start counting penalty when $% $.

The mathematical equation for Hunge loss is formated as:

### Support vector machine (SVM)

L2-regularization with hinge loss is SVM.

The datapoint closest to the gray lines are called support vectors. The optimal solution for SVM maximizes the margins between the support vectors. i.e. SVM creates the largest margin to separate both classes.

We add penalty to the cost function for points where $% $. i.e. points falls within the max magin.

The cost function is defined as the amount of constraint violation.

To have the lowest cost, we want $w \cdot x > 1$ with the smallest $\| w\|$. (Assume $y=1$, $y= -1$ behave the same way.) The cost is reduced to:

The dot product of $w$ and $x$ is the projection of $w$ on $x$. i.e. the green line.

For $w \cdot x$ to have the maximum value (longest green line), the optimal $w^{*}$ and $x$ should be the same. If your draw 2 parallel boundaries passing the support vectors, like the one above on the right, the one with the maximum margin is the radius i.e. $w^{*}$. Hence the optimal $w^{*}$ provides the maximum margin for the support vectors. Therefore SVM indeed maximizes the margin.

With regularization, we can prevent overfitting for outlier:

Having different value of regularization level controlled by $\lambda$, we can have very different optimal solution:

### Maximum likelihood estimation

Given a model parameterized by $w$ and a datapoint $x_i$, the probability that the model predicts the true value $y_i$ is:

The probability to match the predictions for all training datapoints become:

In our training, we want to find $w$ that have the highest $P(Y \vert X, w)$. i.e. we want $w$ to have the maximum likelihood given our observed training data.

#### Negative log likelihood (NNL)

Logarithm is monotonic. Taking the log of a probability function changes the sign and the direction of the function. Hence, to maximize a probability function is the same as minimize the negative log of the function.

In later section, NNL is very handy as a cost function because the log cancels the exponential function used in the classifier.

### Linear regression with gaussian distribution

We want to optimize $\theta$ for a linear regression model with the assumption that $y$ is gaussian distributed with $\mu = x\theta$:

Likelihood:

Optimize $\theta$ using the log likelihood $l$:

The inverse of $x$ may not be well conditioned. We can add a $\delta$ to improve the solution:

Solution:

In machine learning, we often use Mean Square Error (MSE) with L2-regularization as our cost function. In fact, the L2-regularization is not a random choice. Here, we proof that solving the equation below lead us to the same solution $\theta^*$ above.

As a side note, we can rewrite the regularization as an optimization constraint which the $\| \theta \|$ needs to smaller than $t(\delta)$.

### Bayesian linear regression

To optimize a linear regression, we can also use Bayesian inference. Based on a prior belief on how $\theta$ may be distributed ($\mathcal{N}(\theta \vert \theta_0, V_0)$), we compute the posterior with the likelihood $\mathcal{N}(y \vert x\theta, \sigma^2 I )$ using Bayes’ theorem:

Comparison between MLE linear regression and Bayesian linear regression

Source Nando de Freitas, UBC machine learning class.

### Logistic regression

#### Logistic function (sigmoid function)

A logistic function is defined as:

#### Logistic loss

In a logistic regression, we compute the probability of being a class $Y$ as:

Here we use the negative log likeliness (NNL) as our cost function:

Logistic loss is sometime viewed as the smooth version of the Hinge loss.

Logistic loss with L2 regularization:

### Maximum a posteriori (MAP)

We use Maximum likelihood estimation as our cost function to find the optimized $w*$.

Alternatively, we can use Maximum a posteriori (MAP) to find the optimized $w*$.

In this section, we go through the process of how some cost functions are defined using MAP.

Using Baye’s theorem:

Take the negative log

The total cost for all training data is

Compute the prior with the assumption that it has a Gaussian distribution of $\mu=0, \sigma^2 = \frac{1}{\lambda}$:

If the likelihood is also gaussian distributed:

So for a Gaussian distribution prior and likelihood, the cost function is

which is the same as the MSE with L2-regularization.

If the likeliness is computed from a logistic function, the corresponding cost function is:

#### Softmax classifier

For many classification problems, we categorize an input to one of the many classes. For example, we can classify an image to one of the 100 possible object classes. We use a softmax classifier to compute K probabilities, one per class for an input image (the combined probabilities remains 1).

The network computes K scores per image. The probability that an image belongs to the class $i$ will be.

To avoid the numerical stability problem caused by adding large exponential values, we subtract the inputs by its maximum. Adding or subtract a number from the input produces the same probabilities in softmax.

Softmax is the most common classifier among others.

Softmax cost function defined as the NLL:

### Non-linear decision boundary

In the classification problem discussed before, classes are separable by a line or a plane. How can we handle non-linear decision boundary?

We are changing our basis to a quadric function:

After the change, we plot it again. It is easy to realize that the $(x_1)^2$ and $(x_2)^2$ plane represent the distance from the center of the cluster and therefore can be divided by a plane.

Other polynomial basis:

Recall the optimized $w$ for a linear regression using MSE + L2 regularization is:

To make a prediction after the change of basis:

which the Gram matrix ‘K’ is defined as

which $K$ contains the inner products between all training examples.

$\hat{K}$contains the inner products between test example and training.

Consider a degree 2 basis:

We change the basis to a quadratic equation:

And K can be computed directly from $xi$

Hence, we can compute $\hat{y}$ from $x$ directly.

### Parametric model vs non-parametric model

A parametric model captures a model with equations and parameters. For example, a linear regression model is represented by a linear equation parameterized by $w$.

After a model is built, we make prediction directly from $w$ and we do not need to keep the training data. If the model is too simple, our predictions will suffer regardless of how big is the training data.

A non-parametric model uses similarity between training data and the testing data to make prediction. The training data becomes the parameters of the model. The K-nearest neighbors classifier is a typical non-parametric model. We locate K nearest neighbors in the training dataset and make predictions based on them. There is little assumption on the model and the predictions improved with the size of the training dataset. A non-parametric model however can be difficult to optimize if the training dataset is too large.

### Kernel

Kernel is a non-parametric model which use similarity between training data as the input features $f_i$ to make predictions.

which

If $x$ is very similar to $x^i$, $k=1$. If $x$ is very different from $x^i$, $k=0$.

We can use any model to make prediction based on the new input features. For example, a linear model

For example, if we have 3 training datapoints with $y^1 = 1, y^2 = -1, y^3 = 1$, then

In classification problem

#### SVM with kernels

Using kernels, we convert each feature $x_i$ into m features measuring its similarity with all the training datapoints.

and a Gaussian kernel with $\sigma$:

We use a linear regression model:

and train our model using the SVM cost function:

Choice of $\lambda$ & $\sigma$

• Larger $\lambda$ means high regularization with higher bias and lower variance.
• Larger $\sigma$ means we take more influence from the neighbors. i.e. higher bias and lower variance.

When to use a linear model (Logistic regression or SVM with linear kernel $y = w x$ ) or a SVM Gaussian kernel?

Note: N is the number of feature for $x$ and m is the number of training data

 N (large) & m (small) Many input features and vulnerable to overfit with small amount of data Linear model N (large) & m (large) Training data is too large to use kernels Linear model N (small) & m (intermediate) Use non-linear kernel to have a more complex mode Non-linear kernel N (small) & m (large) Training data is too large to use kernels. Add/Create new features. Linear model

Radial basis functions (RBF) is a non-parametric model that takes account of all training datapoints instead of just K-nearest neighbors. However, the influence decreases as the distance increase.

In the first step, we build a matrix using all training datapoints ($x^1, x^2, \cdots x^n$):

which $k$ is a kernel function to measure similarity based on a Gaussian distribution:

and $w$ is calculated with $z$ and true value $y$:

To make prediction for new datapoints $\hat{x}$:

We compute $\hat{z}$:

The output $\hat{y}$ is calculated with:

If we have only one testing datapoint $x'$, the equation is simplified to:

For example, we have 3 training datapoints $x_1, x_2, x_3$, each creating a bell curve $w_1 e^{(- \frac{ \| x' - x_1 \|^2}{2 \sigma^2})}$ indicating much it contributes to the final output (blue dots) at $x_i$.

With more datapoints, we can build a complex functions using this gaussian functions.