linear regression

8
Generalized Linear Regression with Regularization Zoya Byliskii March 3, 2015 1B ASIC R EGRESSION P ROBLEM Note: In the following notes I will make explicit what is a vector and what is a scalar using v ec t or notation, to avoid confusion between variables. I will occasionally expand out the vec- tor notation to make the linear algebra operations more explicit. You are given n data points: d -dimensional feature vectors and corresponding real-valued labels { x (t ) , y (t ) }, t = {1, .., n}. Your goal is to find a linear combination of the features (feature vectors elements/coordinates) that is best able to predict each label. In other words, you want to discover the parameter vector θ that allows you to make the most accurate predictions: x (t ) · θ y (t ) , t = {1, .., n} (1.1) Note: we are ignoring offset for now, and solving a problem without constant offset θ o . In other words, we want to approximate the labels we have for our n data points by finding the best-fitting θ. Expanding out the vector notation in eq. 1.1, we want: x (1) 1 θ 1 + x (1) 2 θ 2 +···+ x (1) d θ d y (1) x (2) 1 θ 1 + x (2) 2 θ 2 +···+ x (2) d θ d y (2) . . . x (n) 1 θ 1 + x (n) 2 θ 2 +···+ x (n) d θ d y (n) 1

Upload: zoya-bylinskii

Post on 17-Feb-2017

224 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: Linear regression

Generalized Linear Regression withRegularization

Zoya Byliskii

March 3, 2015

1 BASIC REGRESSION PROBLEM

Note: In the following notes I will make explicit what is a vector and what is a scalar usingvector notation, to avoid confusion between variables. I will occasionally expand out the vec-tor notation to make the linear algebra operations more explicit.

You are given n data points: d-dimensional feature vectors and corresponding real-valuedlabels {x(t ), y (t )}, t = {1, ..,n}. Your goal is to find a linear combination of the features (featurevectors elements/coordinates) that is best able to predict each label. In other words, you wantto discover the parameter vector θ that allows you to make the most accurate predictions:

x(t ) ·θ ≈ y (t ),∀t = {1, ..,n} (1.1)

Note: we are ignoring offset for now, and solving a problem without constant offset θo .

In other words, we want to approximate the labels we have for our n data points by findingthe best-fitting θ. Expanding out the vector notation in eq. 1.1, we want:

x(1)1 θ1 +x(1)

2 θ2 +·· ·+x(1)d θd ≈ y (1)

x(2)1 θ1 +x(2)

2 θ2 +·· ·+x(2)d θd ≈ y (2)

...

x(n)1 θ1 +x(n)

2 θ2 +·· ·+x(n)d θd ≈ y (n)

1

Page 2: Linear regression

This is just a linear system of n equations in d unknowns. So, we can write this in matrix form:x(1)

x(2)

...x(n)

θ1

...θd

y (1)

y (2)

...y (n)

(1.2)

Or more simply as:

Xθ ≈ y (1.3)

Where X is our data matrix.

Note: the horizontal lines in the matrix help make explicit which way the vectors are stackedin the matrix. In this case, our feature vectors x(t ) make up the rows of our matrix, and theindividual features/coordinates are the columns.

Consider the dimensions of our system:

What happens if n = d?

X is square matrix, and as long as all the data points (rows) of X are linearly independent,then X is invertible and we can solve for θ exactly:

θ = X −1 y (1.4)

Of course a θ that exactly fits all the training data is not likely to generalize to a test set (anovel set of data: new feature vectors x(t )). Thus we might want to impose some regulariza-tion to avoid overfitting (will be discussed later in this document), or to subsample the dataor perform some cross-validation (leave out part of the training data when solving for the pa-rameter θ).

2

Page 3: Linear regression

What happens if n < d?

In this case, we have fewer data points than feature dimensions - less equations than un-knowns. Think: how many degree-3 polynomials can pass through 2 points? An infinitenumber. Similarly, in this case, we can have an infinite number of solutions. Our problemis undefined in this case. What can we do? Well, we can collect more data, we can replicateour existing data while adding some noise to it, or we can apply some methods of feature se-lection (a research area in machine learning) to select out the features (columns of our datamatrix) that carry the most weight for our problem. We have to regularize even more severelyin this case, because there’s an infinite number of ways to overfit the training data.

What happens if n > d?

Now we have more data than features, so the more data we have, the less likely we are tooverfit the training set. In this case, we will not be able to find an exact solution: a θ thatsatisfies eq. 1.1. Instead, we will look for a θ that is best in the least-squares sense. It turnsout, as we will see later in this document, that this θ can be obtained by solving the modifiedsystem of equations:

X T Xθ = X T y (1.5)

This is called the normal equations. Notice that X T X is a square d ×d , invertible matrix 1.

We can now solve for θ as follows:

θ = (X T X )−1X T y (1.6)

Where (X T X )−1X T is often denoted as X + and is called the pseudoinverse of X .

1An easy-to-follow proof is provided here: <https://www.khanacademy.org/math/linear-algebra/matrix_transformations/matrix_transpose/v/lin-alg-showing-that-a-transpose-x-a-is-invertible>

3

Page 4: Linear regression

Let us now arrive at this solution by direct optimization of the least squares objective:

J (θ) = 1

n

n∑t=1

1

2

[y (t ) −θ · x(t )

]2(1.7)

Notice that this is just a way of summarizing the constraints in eq. 1.1. We want θ·x(t ) to comeas close as possible to y (t ) for all t , so we minimize the sum of squared errors.

Note: the 12 term is only for notational convenience (for taking derivatives). It is just a constant

scaling factor of the final θ we obtain. Also, the 1n term is not important for this form of re-

gression without regularization, as it will cancel out. In the form with regularization, it justrescales the regularization parameter by the amount of data points n (we’ll see this later in thisdocument). In any case, you might see formulations of regression with or without this term,but this will not make a big difference to the general form of the problem.

We want to minimize J (θ), and so we set the gradient of this function to zero: ∇θ(J (θ)) = 0.This is equivalent to solving the following system of linear equations:

∂∂θ1

(J (θ))∂∂θ2

(J (θ))...

∂∂θd

(J (θ))

=

00...0

(1.8)

Let’s consider a single one of these equations: i.e. solve for a particular partial ∂∂θi

(J (θ)). Forclarity, consider expanding the vector notation in 1.7:

J (θ) = 1

n

n∑t=1

1

2

[y (t ) −

(x(t )

1 θ1 +x(t )2 θ2 +·· ·+x(t )

d θd

)]2(1.9)

Thus, by the chain rule:

∂θi(J (θ)) = 1

n

n∑t=1

[y (t ) −

(x(t )

1 θ1 +x(t )2 θ2 +·· ·+x(t )

d θd

)](−x(t )

i

)(1.10)

= 1

n

n∑t=1

[y (t ) −

(x(t )

)Tθ

](−x(t )

i

)(1.11)

Note: we just rewrote the dot product θ · x(t ) in equivalent matrix form:(x(t )

)Tθ because we

will be putting our whole system of equations in matrix form in the following calculations.

Since we want to set all the partials to 0, it follows that:

1

n

n∑t=1

[y (t ) −

(x(t )

)Tθ

](−x(t )

i

)= 0 (1.12)

1

n

n∑t=1

x(t )i

(x(t )

)Tθ = 1

n

n∑t=1

x(t )i y (t ) (1.13)

4

Page 5: Linear regression

So we can rewrite the system in eq. 1.14 as:

1n

∑nt=1 x(t )

1

(x(t )

)Tθ

1n

∑nt=1 x(t )

2

(x(t )

)Tθ

...1n

∑nt=1 x(t )

d

(x(t )

)Tθ

=

1n

∑nt=1 x(t )

1 y (t )

1n

∑nt=1 x(t )

2 y (t )

...1n

∑nt=1 x(t )

d y (t )

(1.14)

Which is equivalent to the following condensed form:

1

n

n∑t=1

x(t )(x(t )

)Tθ = 1

n

n∑t=1

x(t ) y (t ) (1.15)

Dropping the 1n term:

n∑t=1

x(t )(x(t )

)Tθ =

n∑t=1

x(t ) y (t ) (1.16)

In other words, this equation is what we obtain by setting ∇θ(J (θ)) = 0.Let us write this equation out explicitly to see how we can rewrite it further in matrix form.First, consider writing out the left-hand-size of eq. 1.16:

x(1)

1x(1)

2...

x(1)d

(x(1)

1 x(1)2 . . . x(1)

d

)θ+

x(2)

1x(2)

2...

x(2)d

(x(2)

1 x(2)2 . . . x(2)

d

)θ+ . . .+

x(n)

1x(n)

2...

x(n)d

(x(n)

1 x(n)2 . . . x(n)

d

)θ (1.17)

Convince yourself that this is just:

x(1) x(2) . . . x(n)

x(1)

x(2)

...x(n)

θ (1.18)

These matrices should be familiar from before. We can rewrite eq. 1.18 as X T Xθ.Next, consider writing out the right-hand-size of eq. 1.16:

x(1)

1x(1)

2...

x(1)d

y (1) +

x(2)

1x(2)

2...

x(2)d

y (2) + . . .+

x(n)

1x(n)

2...

x(n)d

y (n) (1.19)

5

Page 6: Linear regression

Convince yourself that this is just: x(1) x(2) . . . x(n)

y (1.20)

Which is nothing more than X T y .

Putting together eq. 1.18 and eq. 1.20, we get exactly eq. 1.5, and so:

θ = (X T X )−1X T y

as the least-squares solution of our regression problem (no offset, no regularization).

2 REGRESSION WITH REGULARIZATION

If we overfit our training data, our predictions may not generalize very well to novel test data.For instance, if our training data is somewhat noisy (real measurements are always somewhatnoisy!), we don’t want to fit our training data perfectly - otherwise we might generate very badpredictions for real signal. It is worse to overshoot than undershoot predictions when errorsare measured by squared error. Being more guarded in how you predict, i.e., keeping θ small,helps reduce generalization error. We can enforce the constraint to keep θ small by adding aregularization term to 1.7:

J (θ) = 1

n

n∑t=1

1

2

[y (t ) −θ · x(t )

]2+λ2‖θ‖2 (2.1)

Since λ2 ‖θ‖2 = λ

2θ21 + λ

2θ22 +·· ·+ λ

2θ2d this contributes a single additional term to eq. 1.11:

∂θi(J (θ)) = 1

n

n∑t=1

[y (t ) −

(x(t )

)Tθ

](−x(t )

i

)+λθi (2.2)

So similarly to eq. 1.13, setting the partial to zero:

1

n

n∑t=1

x(t )i

(x(t )

)Tθ+λθi = 1

n

n∑t=1

x(t )i y (t ) (2.3)

Thus, collapsing the equations for all partials:

1

n

n∑t=1

x(t )(x(t )

)Tθ+λθ = 1

n

n∑t=1

x(t ) y (t ) (2.4)

And in matrix form:

6

Page 7: Linear regression

1

nX T Xθ+λθ = 1

nX T y (2.5)[

1

nX T X+λI

]θ = 1

nX T y (2.6)

(2.7)

Which gives us the solution of our least-squares regression problem with regularization:

θ =[

1

nX T X +λI

]−1 1

nX T y

Note: You can see that the 1n term was not dropped in the regularized form of the problem, but

is equivalent to a rescaling of λ.

Aside: how do we know 1n X T X +λI is invertible? Here is a rough proof outline:

• First note that X T X is positive semidefiniteproof: uT X T X u = ||X u||2 ≥ 0 so all eigenvalues must be ≥ 0

• The eigenvalues of X T X +λI are µi +λ, where µi are eigenvalues of X T Xproof: X T X u = µu implies (X T X +λI )u = (µ+λ)u

• All eigenvalues of X T X are strictly positive, so it must be invertibleproof: λ> 0, and so µi +λ> 0 for every i

3 REGRESSION WITH REGULARIZATION AND OFFSET

The offset parameter can be thought of as adjusting to the magnitude of the data. It is asingle scalar, and we do not want to penalize its size during regularization, because a largeoffset might allow many of the other parameters to be much smaller and still provide a goodfit for the data. The calculation for the least-squares solution with regularization and offset issimilar to the calculation without offset.

J (θ) = 1

n

n∑t=1

1

2

[y (t ) −

(θ · x(t )+θo

)]2 + λ

2‖θ‖2 (3.1)

Setting ∇θ(J (θ)) = 0 as before, we have:

1

n

n∑t=1

x(t )(x(t )

)Tθ+ 1

nθo

n∑t=1

x(t ) +λθ = 1

n

n∑t=1

x(t ) y (t ) (3.2)

And in matrix form:

7

Page 8: Linear regression

1

nX T Xθ+ 1

nθo X T 1+λθ = 1

nX T y (3.3)(

1

nX T X +λ

)θ+

(1

nX T 1

)θo = 1

nX T y (3.4)

Where 1 is the vector composed of all ones (here with dimension n×1), and so X T 1 =∑nt=1 x(t ).

We separately minimize J (θ) with respect to θo (which does not appear in the regularizer):

∂θo(J (θ)) = 1

n

n∑t=1

[y (t ) −

(x(t )

)Tθ−θo

](−1) (3.5)

Setting this partial to 0 we get:

θo = 1

n

n∑t=1

[y (t ) −

(x(t )

)Tθ

](3.6)

= 1

nyT 1− 1

n

(Xθ

)T1 (3.7)

Note that we now have 2 equations (3.4 and 3.7) in 2 unknowns: θ and θo that can be solvedalgebraically.

4 ADDITIONAL RESOURCES

• Matrix cookbook: <http://www.mit.edu/~wingated/stuff_i_use/matrix_cookbook.pdf>.

• Matrix approach to linear regression with a statistical perspective: <http://www.maths.qmul.ac.uk/~bb/SM_I_2013_LecturesWeek_6.pdf>.

8