Bias-variance tradeoff demo

a shiny app

Carto Wong
mathematician

What is bias-variance tradeoff?

In statistics and machine learning, the bias-variance tradeoff is a problem of trying to minimize two sources of error.

  • Bias refers to the distance between the mean prediction and the true value.
  • Variance refers to the variance of the prediction upon re-sampling of the training set.
trueValue <- 26
predictions <- c(24, 28, 17, 26, 23)
meanPredication <- mean(predictions)
(meanPredication - trueValue)^2  # bias squared
## [1] 5.76
mean((predictions - meanPredication)^2)  # variance
## [1] 13.84

\(k\)-nearest neighbors algorithm

The k-nearest neighbors (regression) algorithm is a non-parametric method which makes prediction using the average of the values of the \(k\)-nearest neighbors.

Shiny app demo of bias-variance tradeoff

This shiny app can be used to explore the bias-variance tradeoff.

Try it yourself

Fix \(n = 30\). Generate two density plots using the shiny app for \(k = 1\) and \(30\) to see what happens.