CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... ·...

22
CMSC858P: Statistical learning background Hector Corrada Bravo February 2011 The purpose of this class is for you to learn machine learning techniques com- monly used in data analysis. By the end of the term, you should be able to read papers that used these methods critically and analyze data using them. When using any of these tools we will be we will be asking ourselves if our findings are “statistically significant”. For example, if we make use of a partic- ular classification algorithm and find that we can predict the outcome of 7 out of our 10 cases, how can we determine if this could have happened by chance alone? To be able to answer these questions, we need to understand some ba- sic probabilistic and statistical principles. Today we will review some of these principles. Probability If I toss a coin, what is the chance it lands heads? In this class we will sometimes be using notation like this: Let X be a random variable that takes values 0 (tails) or 1 (heads) such that Pr(X = 1) = 1/2 For die, we would write: Pr(X = k)=1/6,k =1,..., 6. We will refer to these as probability distributions. More “complicated” distributions can be defined, for example, by considering the random variable Y = N X i=1 X i 1

Transcript of CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... ·...

Page 1: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

CMSC858P: Statistical learning background

Hector Corrada Bravo

February 2011

The purpose of this class is for you to learn machine learning techniques com-monly used in data analysis. By the end of the term, you should be able to readpapers that used these methods critically and analyze data using them.

When using any of these tools we will be we will be asking ourselves if ourfindings are “statistically significant”. For example, if we make use of a partic-ular classification algorithm and find that we can predict the outcome of 7 outof our 10 cases, how can we determine if this could have happened by chancealone? To be able to answer these questions, we need to understand some ba-sic probabilistic and statistical principles. Today we will review some of theseprinciples.

Probability

If I toss a coin, what is the chance it lands heads?

In this class we will sometimes be using notation like this:

Let X be a random variable that takes values 0 (tails) or 1 (heads) such that

Pr(X = 1) = 1/2

For die, we would write:

Pr(X = k) = 1/6, k = 1, . . . , 6.

We will refer to these as probability distributions.

More “complicated” distributions can be defined, for example, by consideringthe random variable

Y =N∑

i=1

Xi

1

Page 2: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

where the Xi’s, i = 1, . . . , N are independent tosses of the same die (or coin).

What are possible values of Y ? What is the distribution of Y ? What doesindependent mean?

Populations, LLN and CLT

In science, randomness usually comes from either random sampling or random-ization.

How does the above relate to populations?

The coin toss can be related to a very large population where each subject iseither, say, a democrat (heads) or a republican (tails). If half are democratsand half are republican, then if we pick a person at random, it’s just like a cointoss.

If dems are 1, and reps are 0, what is the population average x?

If I take a random sample with replacement (a poll) of N = 10 subjects, whatis the distribution of the sample average?

What happens to the difference between the sample average and the populationaverage as the sample size gets bigger?

Why is the sample average X a random variable? What about the distribu-tion? Is the population average a random variable? What does the law of largenumbers (LLN) say? What does the central limit theorem (CLT) say?

Inference

How does this all relate to scientific problems? Many times in science we canmodel the process producing data with a stochastic (probabilistic) model whereparameters (such as population averages) are unkowns. We then make infer-ences based on the data.

For example, in the dems and reps problem we may not know the percentagesof 1s and 0s. To find out, we take a random sample, and construct estimates(the sample average), confidence intervals and p-values.

We will discuss later in class how to make these types of inferences.

For continuous data, this is all pretty much the same. For example, we may wantto know if the average weight of Baltimore women is over some recommendedideal weight.

This inferential approach is used in any situation where a population averageis of interest and we can only obtain a random sample. It is also used whenrandomization is used.

2

Page 3: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

Regression

A common situation in applied sciences is that one has an independent variableor outcome Y and one or more dependent variables or covariates X1, . . . , Xp.One usually observes these variables for multiple “subjects”.

Note: We use upper case to denote a random variable. To denote actual numberswe use lower case. One way to think about it: Y has not happened yet, andwhen it does, we see Y = y.

One may be interested in various things: What effects do the covariates haveon the outcome? How well can we describe these effects? Can we predict theoutcome using the covariates?, etc. . .

Linear Regression

Linear regression is the most common approach for describing the relation be-tween predictors (or covariates) and outcome. Here we will see how regressionrelates to prediction.

Let’s start with a simple example. Let’s say we have a random sample of USmales and we record their heights (X) and weights (Y ).

Say we pick a random subject. How would you predict their weight?

What if I told you their height? Would your strategy for predicting change?

3

Page 4: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

We can show mathematically that for a particular definition of “best”, describedbelow, the average is the best predictor of a value picked from that population.However, if we have information about a related variable, then the conditionalaverage is best.

One can think of the conditional average as the average weights for all men ofa particular height.

4

Page 5: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

In the case of weight and height, the data actually look bivariate normal (footballshaped) and one can show that the best predictor (the conditional average) ofweight given height is

E[Y |X = x] = µY + ρσY

σX(x− µX)

with µX = E[X] (average height), µY = E[Y ] (average weight), and where ρ isthe correlation coefficient of height and weight.

5

Page 6: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

If we obtain a random sample of the data, then each of the above parametersis substituted by the sample estimates and we get a familiar expression:

Y (x) = Y + rSDY

SDX(x− X).

Technical note: Because in practice it is useful to describe distributions of pop-ulations with continuous distributions we will start using the word expectation

6

Page 7: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

or the phrase expected value instead of average. We use the notation E[·]. Ifyou think of integrals as sums, then you can think of expectations as averages.

Notice that equation (1.1) can be written in this, more familiar, notation:

E[Y |X = x] = β0 + β1x.

Because the conditional distribution of Y given X is normal, then we can writethe even more familiar version:

Y = β0 + β1X + ε,

with ε a mean 0, normally distributed random variable that is independent ofX. This notation is popular in many fields because β1 has a nice interpretationand its typical (least squares) estimate has nice properties.

When more than one predictor exists, it is quite common to extend this linearregression model to the multiple linear regression model:

Y = β0 + β1X1 + · · ·+ βpXp + ε

with ε as unbiased (0 mean) error independent of the Xj as before.

A drawback of these models is that they are quite restrictive. Linearity and ad-ditivity are two very strong assumptions. This may have practical consequences.For example, by assuming linearity one may never notice that a covariate hasan effect that increases and then decreases. We will see various examples of thisin class.

Linear regression is popular mainly because of the interpretability of the pa-rameters. Howerver, the interpretation only makes sense if the model is anappropriate approximation of the natural data generating process. It is likelythat the linear regression model from a randomly selected publication will do aterrible job at predicting results in data where the model was not trained on.Prediction is not really given much importance in many scientific fields, e.g. Epi-demiology and Social Sciences. In other fields, e.g. Surveillance, Finance andweb-commerce is everything. Notice that in the fields where prediction is im-portant, linear regression is not as popular.

Prediction

Methods for prediction can be divided into two general groups: continuous anddiscrete outcomes.

When the data is discrete we will refer to it as classification. Other terms arediscriminant analysis, pattern recognition.

7

Page 8: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

When the data is continuous we will refer to it as regression. Other terms aresmoothing and curve estimation.

These seem very different but they have some in common. In this class, we willtalk about the commonalities, but in general, we will discuss these two casesseparately.

The main common characteristic in both cases is that we observe predictorsX1, . . . , Xp and we want to predict the outcome Y .

Note: I will useX to denote the vector of all predictors. So, Xi are the predictorsfor the i-th subject and can include age, gender, ethnicity, etc.

Note: Given a prediction method we will use f(x) to the denote the predictionwe get if the predictors are X = x.

Q: What are examples of prediction problems?

So, what does it mean to predict well? Let’s look at the continuous data casefirst.

If I have a prediction f(X) based on predictors X, how do I define a “good pre-diction” mathematically. A common way of defining closeness is with Euclideandistance:

L{Y, f(X)} = {Y − f(X)}2.

We sometime call this the loss function.

Notice that because both Y and f(X) are random variables, so is (2.2). Mini-mizing a random variable is meaningless because it is not a number. A commonthing to do is minimize over the average loss, or the expected predictionerror:

EXEY |X [{Y − f(X)}2|X].

For a given x, the expected loss is minimized by the conditional expectation:

f(x) = E[Y |X = x],

so it all comes down to getting a good estimate of E[Y |X = x]. We usually callf(x) the regression function.

Note: For discrete problems we usually want a plausible prediction. Note f(x)is typically a continuous number and not a class. We can take an extra stepand define a prediction rule. For example, for binary outcomes, we can say: iff(x) > 0.5, I predict a 1, otherwise, predict 0. However, it is useful to changethe loss function. More on this later.

Notice that if the regression model holds, then

8

Page 9: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

f(X) = E[Y |X1 = x1, . . . , Xp = xp] = β0 +p∑

j=1

βjxj .

For Gaussian models, the solution is the same for least squares and MLE. How-ever, many times, it is hard to believe that the linear regresion model holds. Asimple example comes from AIDS research.

9

Page 10: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

Technical note: It should be noted that for some designed experiments it doesnot make sense to assume the X are random variables. In this case, we usuallyassume we have “design points” x1i, . . . , xpi, i = 1, . . . , n and non-IID observa-tions Y1, . . . , Yn for each design point. In most cases, the theory for both casesis very similar if not the same. These are called random design model and fixeddesign model respectively.

Other settings

A major focus of this class is prediction, or supervised learning. However, wewill also see a few other learning settings. For instance, suppose we only observevectors of random variables, X1, . . . , Xp but no outcome Y ? In this case we stillwant to find some informative structure (e.g. clustering). This setting is calledunsupervised learning. We can include probability density estimation under thissetting.

We will also spend a little time looking at settings where along with outcomesY we don’t observe vectors of covariates, but rather other, harder to representstructures (e.g. strings, graphs). For example, suppose you want to classifyproteins from their amminoacid sequence.

Finally, we might take a look at settings where we do not receive training data(pairs of covariate vectors X and outcomes Y ) at once, or in a batch. Onesuch setting is called online learning, where training data is obtained one pointat a time, and we want to minimize expected prediction error over theentire sequence of training data. Think of google building a model of yourweb-searching profile.

Terminology and notation

We will be mixing the terminology of statistics and computer science. Forexample, we will sometimes call Y and X the outcome/predictors, sometimesobserved/covariates, and even input/output.

We will denote predictors with X and outcomes with Y (quantitative) and G(qualitative). Notice G are not numbers, so we cannot add or multiply them.

Height and weight are quantitative measurements. These are sometimes calledcontinuous measurements.

Gender is a qualitative measurement. They are also called categorical or discrete.This is a particularly simple example because there are only two values. Withtwo values we sometimes call it binary. We will use G to denote the set ofpossible values. For gender it would be G = {Male, Female}. A special caseof qualitative variables are ordered qualitative where one can impose an order.With men/women this can’t be done, but with, say, G = {low,medium, high}it can.

10

Page 11: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

For both types of variables, it makes sense to use the inputs to predict theoutput. Statisticians call the prediction task regression when the outcome isquantitative and classification when we predict qualitative outcomes. We willsee that these have a lot in common and that they can be viewed as a task offunction approximation (as with scatter plots).

Notice that inputs also vary in measurement type.

Technical notation

We will follow the notation of Hastie, Tibshirani and Friedman. Observed valueswill be denoted in lower case. So xi means the ith observation of the randomvariable X. Matrices are represented with bold face upper case. For exampleX will represent all observed predictors. N will usually mean the number ofobservations, or length of Y . i will be used to denote which observation and jto denote which covariate or predictor. Vectors will not be bold, for examplexi may mean all predictors for subject i, unles it is the vector of a particularpredictor xj . All vectors are assumed to be column vectors, so the i-th row ofX will be x′i, i.e., the transpose of xi.

A regression problem

Recall the example data from AIDS research mentioned previously. Here weare plotting the data along with a curve from which data could have plausiblybeen generated. In fact, this curve was estimated from the data by a regressiontechnique called loess, which we will discuss in a future lecture.

11

Page 12: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

For now, let’s consider this curve as truth and simulate CD4 counts from it. Wewill use this simulated data to compare two simple but commonly used methodsto predict Y (CD4 counts) from X (Time), and discuss some of the issues thatwill arise throughout this course. In particular, what is overfitting, and what isthe bias-variance tradeoff.

12

Page 13: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

Linear regression

Probably the most used method in statistics. In this case, we predict the outputY via the model

Y = β0 + β1X.

However, we do not know what β0 or β1 are.

13

Page 14: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

We use the training data to estimate them. We can also say we train the modelon the data to get numeric coefficients. We will use the hat to denote theestimates: β0 and β1.

We will start using β to denote the vector (β0, β1)′. A statistician would callthese the parameters of the model.

The most common way to estimates βs is by least squares. In this case, wechoose the β that minimizes

RSS(β) =N∑

i=1

{yi − (β0 + β1Xi)}2.

If you know linear algebra and calculus you can show that β = (X′X)−1X ′y.

Notice we can predict Y for any X:

Y = β0 + β1X

The next Figure shows the prediction graphically. However, the data seems tosuggest we could do better by considering more flexible models.

14

Page 15: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

K-nearest neighbor

Nearest neighbor methods use the points closest in predictor space to x to obtainan estimate of Y . For the K-nearest neighbor method (KNN) we define

Y =1k

∑xk∈Nk(x)

yk.

15

Page 16: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

Here Nk(x) contains the k-nearest points to x. Notice, as for linear regression,we can predict Y for any X.

In the next Figure we see the results of KNN using the 15 nearest neighbors.This estimate looks better than the linear model.

We do better with KNN than with linear regression. However, we have to becareful about overfitting.

Roughly speaking, overfitting is when you mold an algorithm to work very well(sometimes perfect) on a particular data set forgetting that it is the outcome

16

Page 17: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

of a random process and our trained algorithm may not do as well in otherinstances.

Next, we see what happens when we use KNN with k=1. In this case we makeno mistakes in prediction, but do we really believe we can do well in generalwith this estimate?

It turns out we have been hiding a test data set. Now we can see which of thesetrained algorithms performs best on an independent test set generated by thesame stochastic process.

17

Page 18: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

We can see how good our predictions are using RSS again.

Method Train set Test setLinear 99.70 93.58K=15 67.41 75.32K=1 0.00 149.10

Notice RSS is worse in test set than in the training set for the KNN methods.Especially for KNN=1. The spikes we had in the estimate to predict the training

18

Page 19: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

data perfectly no longer helps.

So, how do we choose k? We will study various ways. First, let’s talk about thebias/variance trade-off.

Smaller k give more flexible estimates, but too much flexibility can result inover-fitting and thus estimates with more variance. Larger k will give morestable estimates but may not be flexible enough. Not being flexible is relatedto being biased.

The next figure shows the RSS in the test and training sets for KNN withvarying k. Notice that for small k we are clearly overfitting.

19

Page 20: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

An illustration of the bias-variance tradeoff

The next figure illustrates the bias/variance tradeoff. Here we make boxplotsof f(1) − f(1), where f(1) is the estimate for each method trained on 1000simulations.

20

Page 21: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

We can see that the prediction from the linear model is consistently inaccurate.That is, it is biased, but stable (little variance). For k = 1 we get the opposite,there is a lot of variability, but once in a while it is very accurate (unbiased).For k = 15 we get a decent tradeoff of the two.

In this case we have simulated data as follows: for a given x

Y = f(x) + ε

where f(x) is the “true” curve we are using and ε is normal with mean zero and

21

Page 22: CMSC858P: Statistical learning background - CBCBhcorrada/CMSC858B/lectures/lect3/StatLearn... · CMSC858P: Statistical learning background ... gender, ethnicity, etc. Note: ... In

some variance σ2.

We have been measuring how good our predictions are by using RSS. Recallthat we sometimes refer to this as a loss function. Recall also that for this lossfunction, if we want to minimize the expected prediction error for a givenx:

EY |X=x[{Y − f(X)}2|X = x],

we get the conditional expectation f(x) = E[Y |X = x]. With some algebra wesee that the RSS for this optimal selection is σ2 in our setting. That is, we can’tdo better than this, on average, with any other predictor.

Notice that KNN is an intuitive estimator of this optimal predictor. We do notknow the function E[Y |X = x] looks like so we estimate it with the y’s of nearbyx’s. The larger k is, the less precise my estimate might be since the radius ofx’s I use for is larger.

Predictions are not always perfect.

22