This is my first Knitr document, which lets the user combine R code and text in a single formatted document.

I wanted to have an accessible example that illustrates the bias variance tradeoff.

# An illustration of the Bias Variance Tradeoff

by Gene Leynes

http://geneorama.com/

http://www.linkedin.com/in/geneleynes

## Summary

The Bias Variance Tradeoff is an important concept in machine learning. This concept helps you evaluate which model will work the best.

When most people think of fitting a model, something like this comes to mind:

Where you basically just draw the best straight line though some points. This paradigm makes it hard to imagine what some one would mean by “model selection”.

The bais varance problem arises when you start to use non linear models that don't have to follow straight lines.

If you consider this data fit with two different smoothing parameters:

you can get a sense of the problem.

Intuitively the plot on the left *seems* to do a better job at representing the information contained in the data… However the model on the right has absolutely no error.

This is the bias variance tradeoff.

In mathematical terms the model on the right has too much *variance* because it only works for that set of points. If you gave it a different set of points generated in the same way, you would get a significantly different model. So, when your model “Chases the points” of your observed data, you have too much “variance”.

Bias, not shown here, is when the model is too inflexible for new input.

# Demo using R to illustrate the Bias Variance tradeoff

## Getting Started

I always start by clearing the workspace and loading libraries.

I'll be using ggplot to draw the graphs, and I like to use my own library geneorama. You don't need it, but you can learn more about it here:

http://geneorama.com/geneorama-package-now-available/

```
rm(list = ls())
library(ggplot2)
library(geneorama)
```

## Example Function

To start with I made an example function that represents some true, but unobservable relationship.

This plot *could* illustrate my enjoyment of a 12 hour long documentary on snail farming. I might have been somewhat excited initially, but then gotten bored, and then really excited towards the end.

```
set.seed(5)
dat = data.frame(x = runif(1000, 0, 12))
dat$x = sort(dat$x)
dat$y = with(dat, sin(x * 1.3) * 15 + 3 * (x - 4)^2)
ggplot(dat, aes(x = x, y = y)) + geom_point() + opts(plot.title = theme_text(size = 16,
face = "bold")) + opts(title = "Some Function Representing the \"Real\" Model\n")
```

## The Simulated Noisy “Real World” Data

### Distribution of the “noise”

For whatever reason, there is noise in the data. Maybe I was recording my enjoyment of the documentary through electrodes attached my scalp, and there was some measurement error. Maybe it was sun spot activitiy… in any event there was noise.

Anyway, the assumed distribution of the noise is shown below. You can see clearly that the stronger positive signals have lower variance. Maybe that's happenstance, or maybe those signals come though more clearly.

```
sigma = with(dat, (exp(x - 5)/(1 + exp(x - 5)) - exp(x - 7)/(1 + exp(x - 7)) *
2) + 1.4) * 6
ggplot(dat, aes(x = x, y = sigma)) + geom_point() + opts(plot.title = theme_text(size = 16,
face = "bold")) + opts(title = paste("The Sigma of the Errors in the Real Data\n",
"(Probably overly complicated)\n")) + opts(panel.background = theme_rect(fill = "antiquewhite3",
colour = "black"))
```

This sigma distribution is based on a function that I completly invented to have a shape that I wanted for this example:

\[ \sigma^2(x) = ((\frac{ e^{x-5} } { 1 + e^{x-5} } -

\frac{ e^{x-7} } {1+e^{x-7}}*2)+1.4)*6

\]

### Creating the observed noisy y

This is a pretty easy part, just add the noise to the distribution based on sigma. I make the noise using the built in `rnorm`

function, and set the `sd`

paramater to `sigma`

.

You can see the lower variance in the upper right part of the graph, where the observed values are closer to the true function.

```
dat$yobs = dat$y + rnorm(nrow(dat), mean = 0, sd = sigma)
```

Now, plot the result

```
p = ggplot(dat, aes(x = x, y = yobs)) + geom_point(alpha = 0.65) + opts(plot.title = theme_text(size = 16,
face = "bold"))
p + geom_line(aes(x = dat$x, y = dat$y), size = 2) + opts(title = "Observed Data\n(With the truth model as a solid line)\n")
```

### Now, let's look at some fits using

### Local Polynomial Regression Fitting (the loess function in R)

Using the loess function I can show some examples of fits where I'm chasing the points at one end of the spectrum, and another end I'm not using the data enough to represent the true curve.

This is spectrum is shown using a range of colors in the RColorBrewer package, I can show a range of fits where the lighter fits are clearly over fitting, and the darker fits are too smooth. With real data it's always a major challenge to decide how smooth to make your fits.

```
library(RColorBrewer)
if (require(geneorama)) {
plot(dat$x, dat$yobs, panel.first = bgfun("honeydew2"), pch = 16, cex = 0.5,
main = "Several fits using the loess function:")
} else {
plot(yobs ~ x, dat, pch = 16, cex = 0.5, main = "Several fits using the loess function:")
}
spans = seq(0.01, 1, length.out = 9)
colors = brewer.pal(length(spans), "YlGnBu")
for (i in 1:length(spans)) {
lines(dat$x, predict(loess(yobs ~ x, dat, span = spans[i])), lwd = 3, col = colors[i])
}
```

## Some of the fits shown separately

It's helpful to see the two extremes by themselves, and an example of something “reasonable”.

### Too much bias

This model is definitely too smooth, and doesn't capture the nuances of the curve. You would say this one has too much bias.

```
p + geom_line(aes(x = dat$x, y = predict(loess(yobs ~ x, dat, span = max(spans)))),
size = 2) + opts(title = "Too Smooth (too baised)\n")
```

### Too much variance

This model chases the points, and would vary from one set of data to another.

```
p + geom_line(aes(x = dat$x, y = predict(loess(yobs ~ x, dat, span = min(spans)))),
size = 2) + opts(title = "Too much variance\n")
```

### A tradeoff of bias and variance

This example is somewhere in between, and does a better job. But the trick is figuring out how to get to this picture automatically.

Note, in this example the code is a little more clear and you can see the manual choice of span=.2 in the loess function.

```
p + geom_line(aes(x = dat$x, y = predict(loess(yobs ~ x, dat, span = 0.2))),
size = 2) + opts(title = "Just Right?\n")
```

### Conclusion

Of course, the problem that I manually picked the “good” example. It's easy for a human to pick a resonable example, but you also want to be able to tell a machine how to pick the resonable example. Also, just shooting for resonable is one thing, how does one find the “best” model? This is the whole question about machine learning.

### Easy to train:

[1]

### Hard to train:

[2]

[1]: A Regular Joe (it's actually Joe the Plumber)

[2]: A computer

### Next post: Machine learning basics

This is my first Knitr document, which lets the user combine R code and text in a single formatted document.

I wanted to have an accessible example that illustrates the bias variance tradeoff.

# An illustration of the Bias Variance Tradeoff

by Gene Leynes

http://geneorama.com/

http://www.linkedin.com/in/geneleynes

## Summary

The Bias Variance Tradeoff is an important concept in machine learning. This concept helps you evaluate which model will work the best.

When most people think of fitting a model, something like this comes to mind:

Where you basically just draw the best straight line though some points. This paradigm makes it hard to imagine what some one would mean by “model selection”.

The bais varance problem arises when you start to use non linear models that don't have to follow straight lines.

If you consider this data fit with two different smoothing parameters:

you can get a sense of the problem.

Intuitively the plot on the left *seems* to do a better job at representing the information contained in the data… However the model on the right has absolutely no error.

This is the bias variance tradeoff.

+qyB0GBy9JkX3ue50mdTNoQGE6NHUlaXB336PGfhEB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UgBKqFUAhF6kAJRQqwAIvUgBKKFWARWEBmA5IDRgBYQGrIDQgBUQGrACQgNWQGjACggNWAGhASsgNGDF5ELfvKVgBSVUv6i7/oMkapfyaAFTCy0utXWoX8Kltm7rP0jmCFUs5eEC1ie0K6Xm5mcQun4JFUsRl8pCi7BdCD3B1qsLvfrfgNoReltC1/etcgFihh+ZGXLoOYTmn0NXL2AG2y5rz6Eh9NRl1Nz87cEfni3hAqFHFfDA5lcodP0UHRF6VAEzCH3/1tfXDl0/gKIdesT2a5ci7PbvLQFXCgErIDRgBYQGrIDQgBUQGrACQgNWQGjACggNWAGhASsgNGAFhAasgNCAFRAasAJCA1ZAaMAKCA1YAaEBKyA0YAWEBqyA0IAVEHpGRPRqhkEnNwiEnpFIaFF/hKYtAqHnQogQl4VwOs8zFuCGgNAzIcKQdsJH6MsMA+ltDAg9EyQUC5pDQ+hpgdAz4fML4cceFUg4pgdCz0TINUjKIRChpwZCzwQxGUJXBELPhR130DR2uKYOJB1TA6GXAyJXAEIDVkBowAoIDVgBoQErIDRgBYQGrIDQgBUQGrACQgNWQGjACggNWAGhASsgNGAFhAasgNCAFRAasAJCA1b8H7FC99Zr7ceWAAAAAElFTkSuQmCC" alt="plot of chunk unnamed-chunk-8"/>

## Some of the fits shown separately

It's helpful to see the two extremes by themselves, and an example of something “reasonable”.

### Too much bias

This model is definitely too smooth, and doesn't capture the nuances of the curve. You would say this one has too much bias.

```
p + geom_line(aes(x = dat$x, y = predict(loess(yobs ~ x, dat, span = max(spans)))),
size = 2) + opts(title = "Too Smooth (too baised)\n")
```

### Too much variance

This model chases the points, and would vary from one set of data to another.

```
p + geom_line(aes(x = dat$x, y = predict(loess(yobs ~ x, dat, span = min(spans)))),
size = 2) + opts(title = "Too much variance\n")
```

### A tradeoff of bias and variance

This example is somewhere in between, and does a better job. But the trick is figuring out how to get to this picture automatically.

Note, in this example the code is a little more clear and you can see the manual choice of span=.2 in the loess function.

```
p + geom_line(aes(x = dat$x, y = predict(loess(yobs ~ x, dat, span = 0.2))),
size = 2) + opts(title = "Just Right?\n")
```

### Conclusion

Of course, the problem that I manually picked the “good” example. It's easy for a human to pick a resonable example, but you also want to be able to tell a machine how to pick the resonable example. Also, just shooting for resonable is one thing, how does one find the “best” model? This is the whole question about machine learning.

### Easy to train:

[1]

### Hard to train:

[2]

[1]: A Regular Joe (it's actually Joe the Plumber)

[2]: A computer