Skip to content
Ahmed Haroon
Machine Learning

Linear Models

Linear Regression

Linear regression predicts a continuous value using a linear function of the input features.

Given an input xx, we want to choose parameters θ\theta so that the prediction is as close as possible to the true value yy.

To include an intercept, we set x0(i)=1x_0^{(i)}=1. Each input x(i)Rd+1x^{(i)}\in\mathbb{R}^{d+1} then contains the intercept and dd features, while θRd+1\theta\in\mathbb{R}^{d+1} contains the corresponding parameters.

The model predicts:

hθ(x)=j=0dθjxj=θTx h_{\theta}(x) = \sum_{j=0}^{d} \theta_jx_j = \theta^Tx

For training example ii, the difference:

hθ(x(i))y(i) h_\theta(x^{(i)})-y^{(i)}

is called the residual.

We measure the total error by adding the squared residuals across the training set:

J(θ)=12i=1n(hθ(x(i))y(i))2 J(\theta) = \frac12 \sum_{i=1}^{n} \left( h_{\theta}(x^{(i)}) - y^{(i)} \right)^2

Our goal is to choose θ\theta that minimizes J(θ)J(\theta).

Linear regression fit with vertical residuals from each data point to the fitted line

The cost function measures the squared vertical residuals between each training label and the fitted line.

To minimize J(θ)J(\theta) using gradient descent, we need to know how the cost changes with each parameter θj\theta_j.

Taking the derivative gives:

θjJ(θ)=i=1n[hθ(x(i))y(i)]xj(i),j=0,,d \frac{\partial}{\partial\theta_j} J(\theta) = \sum_{i=1}^{n} \left[ h_\theta(x^{(i)}) - y^{(i)} \right] x_j^{(i)}, \qquad j=0,\ldots,d

This derivative tells us how changing θj\theta_j changes the total error.

See derivation
θjJ(θ)=θj[12i=1n(hθ(x(i))y(i))2] \frac{\partial}{\partial \theta_j} J(\theta) = \frac{\partial}{\partial \theta_j} \left[ \frac{1}{2} \sum_{i=1}^{n} \left( h_{\theta}(x^{(i)}) - y^{(i)} \right)^2 \right] =i=1n[212(hθ(x(i))y(i))θj(hθ(x(i))y(i))] = \sum_{i=1}^{n} \left[ 2\cdot\frac12 \left( h_\theta(x^{(i)})-y^{(i)} \right) \cdot \frac{\partial}{\partial\theta_j} \left( h_\theta(x^{(i)})-y^{(i)} \right) \right] =i=1n[(hθ(x(i))y(i))θj(k=0dθkxk(i)y(i))] = \sum_{i=1}^{n} \left[ \left( h_\theta(x^{(i)})-y^{(i)} \right) \cdot \frac{\partial}{\partial\theta_j} \left( \sum_{k=0}^{d}\theta_kx_k^{(i)} - y^{(i)} \right) \right]
=i=1n[(hθ(x(i))y(i))θj(θjxj(i))] = \sum_{i=1}^{n} \left[ \left( h_\theta(x^{(i)})-y^{(i)} \right) \cdot \frac{\partial}{\partial\theta_j} \left( \theta_jx_j^{(i)} \right) \right]
=i=1n[(hθ(x(i))y(i))xj(i)] = \sum_{i=1}^{n} \left[ \left( h_\theta(x^{(i)})-y^{(i)} \right) x_j^{(i)} \right]

Instead of computing the gradient over the entire dataset before every update, stochastic gradient descent uses one training example at a time.

For training example ii:

θθ+α(y(i)hθ(x(i)))x(i) \theta \leftarrow \theta + \alpha \left( y^{(i)} - h_\theta(x^{(i)}) \right) x^{(i)}

where α>0\alpha>0 is the learning rate.

Gradient descent path over contour lines of the linear regression cost function

Gradient descent updates θ\theta by moving downhill along the contours of J(θ)J(\theta) until it reaches the minimum.

Repeat until convergence { \text{Repeat until convergence \{} For i=1 to n, { \quad \text{For } i=1 \text{ to } n,\text{ \{} θθ+α(y(i)hθ(x(i)))x(i) \quad\quad \theta \leftarrow \theta + \alpha \left( y^{(i)} - h_\theta(x^{(i)}) \right) x^{(i)} } \quad \text{\}} } \text{\}}

Closed Form Solution

Gradient descent finds the parameters through repeated updates. For ordinary least squares, we can also solve for the optimal parameters directly.

Let XRn×(d+1)X\in\mathbb{R}^{n\times(d+1)} contain one training example per row, and let:

y=[y(1)y(n)] \vec y = \begin{bmatrix} y^{(1)}\\ \vdots\\ y^{(n)} \end{bmatrix}

The predictions for the entire dataset can then be written as:

y^=Xθ \hat{\vec y} = X\theta

Using the fact that zTz=izi2z^Tz=\sum_i z_i^2, we can rewrite the cost as:

J(θ)=12i=1n(hθ(x(i))y(i))2 J(\theta) = \frac12 \sum_{i=1}^{n} \left( h_{\theta}(x^{(i)}) - y^{(i)} \right)^2 =12(Xθy)T(Xθy) = \frac12 \left( X\theta-\vec y \right)^T \left( X\theta-\vec y \right)

Setting the gradient to zero gives the normal equations:

XTXθ=XTy X^TX\theta = X^T\vec y

If XX has full column rank, the minimizer is unique and is given by:

θ=(XTX)1XTy \theta^\star = \left( X^TX \right)^{-1} X^T\vec y

If XTXX^TX is singular, the inverse does not exist. The minimum-norm solution is:

θ=X+y \theta^\star = X^+\vec y

where X+X^+ is the Moore-Penrose pseudoinverse.

Orthogonal projection of the target vector onto the column space of the design matrix

The closed-form solution chooses the prediction vector y^=Xθ\hat{y} = X\theta that is closest to y\vec{y} inside the column space of XX.

See derivation
θJ(θ)=θ(12(Xθy)T(Xθy)) \nabla_{\theta}J(\theta) = \nabla_{\theta} \left( \frac12 \left( X\theta-\vec y \right)^T \left( X\theta-\vec y \right) \right) =12θ((Xθ)TXθ(Xθ)TyyT(Xθ)+yTy) = \frac12 \nabla_{\theta} \left( (X\theta)^TX\theta - (X\theta)^T\vec y - \vec y^T(X\theta) + \vec y^T\vec y \right)
=12θ(θTXTXθθTXTyyTXθ) = \frac12 \nabla_{\theta} \left( \theta^TX^TX\theta - \theta^TX^T\vec y - \vec y^TX\theta \right)
=12[2XTXθθ(θTXTy+yTXθ)] = \frac12 \left[ 2X^TX\theta - \nabla_{\theta} \left( \theta^TX^T\vec y + \vec y^TX\theta \right) \right] =12[2XTXθXTyXTy] = \frac12 \left[ 2X^TX\theta - X^T\vec y - X^T\vec y \right] =12[2XTXθ2XTy] = \frac12 \left[ 2X^TX\theta - 2X^T\vec y \right] =XTXθXTy = X^TX\theta - X^T\vec y

Probabilistic Interpretation

Least squares also has a probabilistic interpretation. We can view the observed target as the model's prediction plus random noise.

Assume independent additive noise terms:

ϵ(i)N(0,σ2) \epsilon^{(i)} \sim \mathcal N(0,\sigma^2)

so that:

y(i)=θTx(i)+ϵ(i) y^{(i)} = \theta^Tx^{(i)} + \epsilon^{(i)}

Rearranging gives:

ϵ(i)=y(i)θTx(i) \epsilon^{(i)} = y^{(i)} - \theta^Tx^{(i)}

Therefore, conditioned on x(i)x^{(i)}, the response follows a Gaussian distribution centered at the linear prediction:

Y(i)X(i)=x(i)N(θTx(i),σ2) Y^{(i)} \mid X^{(i)}=x^{(i)} \sim \mathcal N \left( \theta^Tx^{(i)}, \sigma^2 \right)

Assuming the training examples are independent, the likelihood of the observed targets is:

L(θ)=L(θ;X,Y)=i=1np(y(i)x(i);θ) L(\theta) = L(\theta;X,Y) = \prod_{i=1}^{n} p(y^{(i)}\mid x^{(i)};\theta)

We choose θ\theta to make the observed data as likely as possible.

Because log\log is an increasing function, maximizing the likelihood is equivalent to maximizing the log-likelihood:

(θ)=logi=1np(y(i)x(i);θ) \ell(\theta) = \log \prod_{i=1}^{n} p(y^{(i)}\mid x^{(i)};\theta)

For fixed σ2>0\sigma^2>0, maximizing (θ)\ell(\theta) is equivalent to minimizing:

12i=1n(y(i)θTx(i))2 \frac12 \sum_{i=1}^{n} \left( y^{(i)} - \theta^Tx^{(i)} \right)^2

This is exactly the least-squares objective we started with. Therefore, under the Gaussian noise assumption, maximum likelihood and least squares give the same estimate of θ\theta.

The positive factor 1/σ21/\sigma^2 only rescales the objective, so we do not need to know the noise variance to estimate θ\theta.

See derivation
(θ)=logi=1np(y(i)x(i);θ) \ell(\theta) = \log \prod_{i=1}^{n} p(y^{(i)}\mid x^{(i)};\theta) =i=1nlogp(y(i)x(i);θ) = \sum_{i=1}^{n} \log p(y^{(i)}\mid x^{(i)};\theta) =i=1nlog[1σ2πexp((y(i)θTx(i))22σ2)] = \sum_{i=1}^{n} \log \left[ \frac{1}{\sigma\sqrt{2\pi}} \exp \left( -\frac{ (y^{(i)}-\theta^Tx^{(i)})^2 }{ 2\sigma^2 } \right) \right] =i=1n(log[1σ2π](y(i)θTx(i))22σ2) = \sum_{i=1}^{n} \left( \log \left[ \frac{1}{\sigma\sqrt{2\pi}} \right] - \frac{ (y^{(i)}-\theta^Tx^{(i)})^2 }{ 2\sigma^2 } \right) =nlog[1σ2π]12σ2i=1n(y(i)θTx(i))2 = n \log \left[ \frac{1}{\sigma\sqrt{2\pi}} \right] - \frac{1}{2\sigma^2} \sum_{i=1}^{n} \left( y^{(i)}-\theta^Tx^{(i)} \right)^2

To find the θ\theta that maximizes (θ)\ell(\theta):

θ=argmaxθ(θ) \theta = \arg\max_{\theta} \ell(\theta) =argmaxθ[nlog(1σ2π)12σ2i=1n(y(i)θTx(i))2] = \arg\max_{\theta} \left[ n \log \left( \frac{1}{\sigma\sqrt{2\pi}} \right) - \frac{1}{2\sigma^2} \sum_{i=1}^{n} \left( y^{(i)}-\theta^Tx^{(i)} \right)^2 \right]
=argmaxθ[12σ2i=1n(y(i)θTx(i))2] = \arg\max_{\theta} \left[ -\frac{1}{2\sigma^2} \sum_{i=1}^{n} \left( y^{(i)}-\theta^Tx^{(i)} \right)^2 \right]
=argminθ[12σ2i=1n(y(i)θTx(i))2] = \arg\min_{\theta} \left[ \frac{1}{2\sigma^2} \sum_{i=1}^{n} \left( y^{(i)}-\theta^Tx^{(i)} \right)^2 \right]
=argminθ[12i=1n(y(i)θTx(i))2] = \arg\min_{\theta} \left[ \frac12 \sum_{i=1}^{n} \left( y^{(i)}-\theta^Tx^{(i)} \right)^2 \right]

Logistic Regression

Linear regression predicts a continuous value. For binary classification, we instead want to predict the probability that y=1y=1.

Logistic regression first computes the linear score:

θTx \theta^Tx

and then passes it through the sigmoid function to convert it into a value between 00 and 11:

hθ(x)=g(θTx)=11+eθTx h_{\theta}(x) = g(\theta^Tx) = \frac{1}{ 1+e^{-\theta^Tx} }
Sigmoid curve mapping real-valued scores to probabilities between 0 and 1

The sigmoid function converts the score θTx\theta^T x into a probability, with g(0)=12g(0)=\frac12.

We interpret this output as the probability of class 11:

p(y=1x;θ)=hθ(x) p(y=1\mid x;\theta) = h_{\theta}(x)

and therefore:

p(y=0x;θ)=1hθ(x) p(y=0\mid x;\theta) = 1-h_{\theta}(x)

Because y0,1y\in{0,1}, both cases can be written compactly as:

p(yx;θ)=(hθ(x))y(1hθ(x))1y p(y\mid x;\theta) = \left( h_{\theta}(x) \right)^y \left( 1-h_{\theta}(x) \right)^{1-y}
Binary classification decision boundary separating two classes

Logistic regression predicts one class on one side of the boundary θTx=0\theta^T x = 0 and the other class on the opposite side.

Maximum-Likelihood Estimation

The log-likelihood of the training data is:

(θ)=logi=1np(y(i)x(i);θ) \ell(\theta) = \log \prod_{i=1}^{n} p(y^{(i)}\mid x^{(i)};\theta) =i=1nlogp(y(i)x(i);θ) = \sum_{i=1}^{n} \log p(y^{(i)}\mid x^{(i)};\theta) =i=1nlog[(hθ(x(i)))y(i)(1hθ(x(i)))1y(i)] = \sum_{i=1}^{n} \log \left[ \left( h_{\theta}(x^{(i)}) \right)^{y^{(i)}} \left( 1-h_{\theta}(x^{(i)}) \right)^{1-y^{(i)}} \right]

Taking its derivative with respect to θj\theta_j gives:

θj(θ)=i=1n(y(i)hθ(x(i)))xj(i) \frac{\partial}{\partial\theta_j} \ell(\theta) = \sum_{i=1}^{n} \left( y^{(i)} - h_{\theta}(x^{(i)}) \right) x_j^{(i)}

This is the full-data gradient.

A per-example stochastic gradient-ascent update is:

θθ+α(y(i)hθ(x(i)))x(i) \theta \leftarrow \theta + \alpha \left( y^{(i)} - h_{\theta}(x^{(i)}) \right) x^{(i)}

where α>0\alpha>0 is the learning rate. A batch update instead uses the full sum in the gradient above.

See derivation

For a sigmoid function:

g(z)=11+ez g(z) = \frac{1}{1+e^{-z}}

its derivative is:

ddzg(z)=ddz11+ez \frac{d}{dz}g(z) = \frac{d}{dz} \frac{1}{1+e^{-z}} =ddz(1+ez)1 = \frac{d}{dz} \left( 1+e^{-z} \right)^{-1} =1(1+ez)2(ez) = -1 \left( 1+e^{-z} \right)^{-2} \left( -e^{-z} \right) =11+ezez1+ez = \frac{1}{1+e^{-z}} \frac{e^{-z}}{1+e^{-z}} =11+ez(111+ez) = \frac{1}{1+e^{-z}} \left( 1- \frac{1}{1+e^{-z}} \right) =g(z)(1g(z)) = g(z) \left( 1-g(z) \right)
θj(θ)=θji=1nlog[(hθ(x(i)))y(i)(1hθ(x(i)))1y(i)] \frac{\partial}{\partial\theta_j} \ell(\theta) = \frac{\partial}{\partial\theta_j} \sum_{i=1}^{n} \log \left[ \left( h_{\theta}(x^{(i)}) \right)^{y^{(i)}} \left( 1-h_{\theta}(x^{(i)}) \right)^{1-y^{(i)}} \right] =θji=1n(y(i)loghθ(x(i))+(1y(i))log(1hθ(x(i)))) = \frac{\partial}{\partial\theta_j} \sum_{i=1}^{n} \left( y^{(i)} \log h_{\theta}(x^{(i)}) + (1-y^{(i)}) \log \left( 1-h_{\theta}(x^{(i)}) \right) \right) =θji=1n(y(i)logg(θTx(i))+(1y(i))log(1g(θTx(i)))) = \frac{\partial}{\partial\theta_j} \sum_{i=1}^{n} \left( y^{(i)} \log g(\theta^Tx^{(i)}) + (1-y^{(i)}) \log \left( 1-g(\theta^Tx^{(i)}) \right) \right) =i=1n([y(i)g(θTx(i))1y(i)1g(θTx(i))]θj[g(θTx(i))]) = \sum_{i=1}^{n} \left( \left[ \frac{y^{(i)}}{g(\theta^Tx^{(i)})} - \frac{1-y^{(i)}}{ 1-g(\theta^Tx^{(i)}) } \right] \cdot \frac{\partial}{\partial\theta_j} \left[ g(\theta^Tx^{(i)}) \right] \right)
=i=1n([y(i)g(θTx(i))g(θTx(i))(1g(θTx(i)))]θj[g(θTx(i))]) = \sum_{i=1}^{n} \left( \left[ \frac{ y^{(i)}-g(\theta^Tx^{(i)}) }{ g(\theta^Tx^{(i)}) \left( 1-g(\theta^Tx^{(i)}) \right) } \right] \cdot \frac{\partial}{\partial\theta_j} \left[ g(\theta^Tx^{(i)}) \right] \right)
=i=1n([y(i)g(θTx(i))g(θTx(i))(1g(θTx(i)))]g(θTx(i))(1g(θTx(i)))θj[θTx(i)]) = \sum_{i=1}^{n} \left( \left[ \frac{ y^{(i)}-g(\theta^Tx^{(i)}) }{ g(\theta^Tx^{(i)}) \left( 1-g(\theta^Tx^{(i)}) \right) } \right] g(\theta^Tx^{(i)}) \left( 1-g(\theta^Tx^{(i)}) \right) \frac{\partial}{\partial\theta_j} \left[ \theta^Tx^{(i)} \right] \right)
=i=1n(y(i)g(θTx(i)))xj(i) = \sum_{i=1}^{n} \left( y^{(i)} - g(\theta^Tx^{(i)}) \right) x_j^{(i)} =i=1n(y(i)hθ(x(i)))xj(i) = \sum_{i=1}^{n} \left( y^{(i)} - h_{\theta}(x^{(i)}) \right) x_j^{(i)}

Logistic Loss

So far, we have written logistic regression as a maximum-likelihood problem.

Equivalently, we can turn it into a minimization problem by taking the negative log-likelihood.

For one example with score:

t=θTx t=\theta^Tx

the logistic loss is:

logistic(t,y)=ylog(1+et)+(1y)log(1+et) \ell_{\mathrm{logistic}}(t,y) = y \log \left( 1+e^{-t} \right) + (1-y) \log \left( 1+e^t \right)

Therefore, maximizing the likelihood over the dataset is equivalent to minimizing the sum of these losses:

argmaxθ(θ) \arg\max_{\theta} \ell(\theta) =argminθi=1nlogistic(θTx(i),y(i)) = \arg\min_{\theta} \sum_{i=1}^{n} \ell_{\mathrm{logistic}} \left( \theta^Tx^{(i)}, y^{(i)} \right)
Logistic loss as a function of the score for each label

Minimizing the logistic loss pushes the score t=θTxt=\theta^Tx toward the correct sign: a confidently wrong prediction is penalized without bound, while a confidently correct one costs almost nothing.

See derivation
logp(yx;θ) -\log p(y\mid x;\theta) =ylogg(t)(1y)log(1g(t)) = -y\log g(t) - (1-y) \log \left( 1-g(t) \right) =ylog(1+et)+(1y)log(1+et) = y \log \left( 1+e^{-t} \right) + (1-y) \log \left( 1+e^t \right) =logistic(t,y) = \ell_{\mathrm{logistic}}(t,y)

If the training data are completely separable, the unregularized likelihood has no finite maximizer: increasing the norm of a separating θ\theta keeps improving the likelihood.

Regularization or another finite-parameter constraint prevents this divergence.

Multiclass Classification

For kk mutually exclusive classes, we give each class its own parameter vector θj\theta_j.

For an input xx, class jj receives the score:

θjTx \theta_j^Tx

Softmax converts these kk scores into probabilities that sum to 11:

p(y=ix;θ)=ϕi=exp(θiTx)j=1kexp(θjTx) p(y=i\mid x;\theta) = \phi_i = \frac{ \exp(\theta_i^Tx) }{ \sum_{j=1}^{k} \exp(\theta_j^Tx) }
Softmax decision regions partitioning the feature space into k classes

Softmax assigns each point to the class with the largest score θiTx\theta_i^T x, carving the feature space into kk regions whose boundaries lie where two classes tie.

Only relative class scores matter.

If the same vector aa is added to every class parameter, each numerator and the denominator are multiplied by exp(aTx)\exp(a^Tx), so the probabilities are unchanged.

Consequently, the unconstrained softmax parameterization is not identifiable without fixing a reference class, imposing a constraint, or using regularization.

For each training example, we want the model to assign high probability to the correct class.

The corresponding cross-entropy loss, which is the negative log-likelihood, is:

ce(θ)=i=1mlog(exp(θy(i)Tx(i))j=1kexp(θjTx(i))) \ell_{ce}(\theta) = \sum_{i=1}^{m} - \log \left( \frac{ \exp \left( \theta_{y^{(i)}}^Tx^{(i)} \right) }{ \sum_{j=1}^{k} \exp \left( \theta_j^Tx^{(i)} \right) } \right)

Taking the derivative of the cross-entropy loss with respect to θj\theta_j gives:

θjce(θ)=i=1m(ϕj(i)1{y(i)=j})x(i) \frac{\partial}{\partial\theta_j} \ell_{ce}(\theta) = \sum_{i=1}^{m} \left( \phi_j^{(i)} - 1\left\{ y^{(i)}=j \right\} \right) x^{(i)}

where:

ϕj(i)=p(y(i)=jx(i);θ) \phi_j^{(i)} = p \left( y^{(i)}=j \mid x^{(i)}; \theta \right)

The gradient compares the model's predicted probability for class jj:

ϕj(i) \phi_j^{(i)}

with the target:

1{y(i)=j} 1\left\{ y^{(i)}=j \right\}

In other words, the update is driven by predicted probability minus the true class indicator.

The gradient descent update is:

θjθjαi=1m(ϕj(i)1{y(i)=j})x(i) \theta_j \leftarrow \theta_j - \alpha \sum_{i=1}^{m} \left( \phi_j^{(i)} - 1\left\{ y^{(i)}=j \right\} \right) x^{(i)}
See derivation
θlce(θ)=θli=1mlog(exp(θy(i)Tx(i))j=1kexp(θjTx(i))) \frac{\partial}{\partial\theta_l} \ell_{ce}(\theta) = \frac{\partial}{\partial\theta_l} \sum_{i=1}^{m} - \log \left( \frac{ \exp \left( \theta_{y^{(i)}}^Tx^{(i)} \right) }{ \sum_{j=1}^{k} \exp \left( \theta_j^Tx^{(i)} \right) } \right) =θli=1m[θy(i)Tx(i)+logj=1kexp(θjTx(i))] = \frac{\partial}{\partial\theta_l} \sum_{i=1}^{m} \left[ - \theta_{y^{(i)}}^Tx^{(i)} + \log \sum_{j=1}^{k} \exp \left( \theta_j^Tx^{(i)} \right) \right] =i=1m[θl(logj=1kexp(θjTx(i)))θl(θy(i)Tx(i))] = \sum_{i=1}^{m} \left[ \frac{\partial}{\partial\theta_l} \left( \log \sum_{j=1}^{k} \exp \left( \theta_j^Tx^{(i)} \right) \right) - \frac{\partial}{\partial\theta_l} \left( \theta_{y^{(i)}}^Tx^{(i)} \right) \right] =i=1m[1j=1kexp(θjTx(i))(θlj=1kexp(θjTx(i)))x(i)1{y(i)=l}] = \sum_{i=1}^{m} \left[ \frac{1}{ \sum_{j=1}^{k} \exp \left( \theta_j^Tx^{(i)} \right) } \left( \frac{\partial}{\partial\theta_l} \sum_{j=1}^{k} \exp \left( \theta_j^Tx^{(i)} \right) \right) - x^{(i)} 1 \left\{ y^{(i)}=l \right\} \right] =i=1m[exp(θlTx(i))j=1kexp(θjTx(i))x(i)x(i)1{y(i)=l}] = \sum_{i=1}^{m} \left[ \frac{ \exp \left( \theta_l^Tx^{(i)} \right) }{ \sum_{j=1}^{k} \exp \left( \theta_j^Tx^{(i)} \right) } x^{(i)} - x^{(i)} 1 \left\{ y^{(i)}=l \right\} \right] =i=1m(exp(θlTx(i))j=1kexp(θjTx(i))1{y(i)=l})x(i) = \sum_{i=1}^{m} \left( \frac{ \exp \left( \theta_l^Tx^{(i)} \right) }{ \sum_{j=1}^{k} \exp \left( \theta_j^Tx^{(i)} \right) } - 1 \left\{ y^{(i)}=l \right\} \right) x^{(i)} =i=1m(ϕl(i)1{y(i)=l})x(i) = \sum_{i=1}^{m} \left( \phi_l^{(i)} - 1 \left\{ y^{(i)}=l \right\} \right) x^{(i)}