{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The objective of machine learning\n",
"\n",
"Basically, ML tasks tend to fall into sets of tasks:\n",
"- Prediction accuracy (e.g. of the label or the group detection) \n",
"- Feature selection (which X variables and non-linearities should be in the model)\n",
"\n",
"And for both of those, the idea is that what the model learns will work **out-of-sample**. _In the framework of our [machine learning workflow](03_ML), what this means is that after we pick our model in step 5, we only get one chance to apply it on test data and then will move to production models. We want our model to perform as well at step 6 and in production as it does while we train it!_\n",
"\n",
"```{admonition} Key takeaway #1\n",
":class: tip\n",
"**The key to understanding most of the choices you make in a ML project is to remember:** The focus of ML is to learn something that generalizes _**outside of the data we have already!**_ \n",
"\n",
"_Econometrically,_ the goal is to estimate a model on a sample (the data we have) that works on the population (all of the data that can and will be generated in the real world). \n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Model Risk \n",
"\n",
"A model will create predictions, and those predictions will be wrong to some degree when we generalize outside our initial data.\n",
"\n",
"It turns out we can decompose the _expected error_ of a model like this[^biasform]\n",
"\n",
"$$\n",
"E[\\text{model error risk}] = \\text{model bias}^2+\\text{model variance}+\\text{noise}\n",
"$$\n",
"\n",
"[^biasform]: _(If you want to see the derivation of this, you can go to the [wiki page](https://en.wikipedia.org/wiki/Bias%E2%80%93variance_tradeoff) or [DS100](https://www.textbook.ds100.org/ch/21/bias_modeling.html). The former's notation is a little simpler but the latter is more helpful with intuition)_\n",
"\n",
"Let's define those terms:\n",
"\n",
"**\"Model bias\"**\n",
"- Def: Is errors stemming from the model's assumptions in how it predicts the outcome variable. (It is the opposite of model accuracy.)\n",
"- Complexity helps: Adding more variables or polynomial transformations of existing variables **will usually reduce bias** \n",
"- Adding more data to the training dataset can (but might not) reduce bias\n",
"\n",
"**\"Model variance\"**\n",
"- Def: Is extent to which estimated model varies from sample to sample\n",
"- Complexity hurts: Adding more variables or polynomial transformations of existing variables **will usually increase model variance** \n",
"- Adding more data to the training dataset will reduce variance\n",
"\n",
"**Noise**\n",
"- Def: Randomness in the data generating process beyond our understanding\n",
"- To reduce the noise term, you need more data, better data collection, and more accurate measurements\n",
"\n",
"## The bias-variance tradeoff \n",
"\n",
"```{admonition} Key takeaway #2\n",
":class: tip\n",
"**Changing the complexity of a model changes the model's bias and variance, and there is an optimal amount of complexity.**\n",
"\n",
"**THE FUNDAMENTAL TRADEOFF: Increasing model complexity increases its variance but reduces its bias** \n",
"- Models that are too simple have high bias but low variance \n",
"- Models that are too complex have the opposite problem\n",
"- Collecting a TON of data can allow you to use complex models with less variance \n",
"\n",
"This is the essence of the bias-variance tradeoff, a fundamental issue that we face in choosing models for prediction.\"[^tradeoff]\n",
"```\n",
"\n",
"[^tradeoff]: _(This is adapted from [DS100](https://www.textbook.ds100.org/ch/15/bias_modeling.html))_\n",
"\n",
"Let's work through these ideas visually in the next three tabs:\n",
"\n",
"````{tabbed} The classic bias-variance tradeoff\n",
"\n",
"We want to minimize model risk. In the graph below, that is called \"Test Error\". \n",
"\n",
"- Models that are too simple are said to be **\"underfit\"** (take steps to reduce bias)\n",
" - In the graph below, an underfit model is on the left side of the picture\n",
"- Models that are too complicated are said to be **\"overfit\"** (take steps to reduce variance)\n",
" - In the graph below, an overfit model is on the right side of the picture\n",
" \n",
"```{image} img/bias_modeling_bias_var_plot.png\n",
":alt: bias_var\n",
":width: 500px\n",
"```\n",
"````\n",
"\n",
"````{tabbed} Optimizing model performance via complexity \n",
"\n",
"In the chart below, the blue line depicts how well your model does on the \"training sample\", meaning: The data the model is trained on. The red line shows how well your model does on data it has never seen before, a so-called \"validation sample\".\n",
"\n",
"- Models that are too simple perform poorly (low scores, high bias)\n",
"- Models that are too complex perform well in training but poorly outside that sample (high variance, the gap between the lines)\n",
"\n",
"```{image} img/validation-curve.png\n",
":alt: validation\n",
":width: 500px\n",
"```\n",
"\n",
"[Source](https://jakevdp.github.io/PythonDataScienceHandbook/figures/05.03-validation-curve.png)\n",
"\n",
"````\n",
"\n",
"````{tabbed} A great video explainer (watch this!)\n",
"\n",
"\n",
"\n",
"````"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Minimizing model risk \n",
"\n",
"Our tools to minimize model risk are\n",
"1. More data! (Often helps, but not always.)\n",
"4. Proper model evaluation procedures ([via cross validation (CV)](03c_ModelEval) or [out-of-sample (OOS) forecasting](03c1_OOS)) can help gauge whether a model is overfit or underfit. \n",
"2. [Feature engineering (adding, cleaning, and selecting features; dimensionality reduction)](https://jakevdp.github.io/PythonDataScienceHandbook/05.04-feature-engineering.html) \n",
"3. [Model selection](https://jakevdp.github.io/PythonDataScienceHandbook/05.03-hyperparameters-and-model-validation.html) - picking the right model for your setting \n",
"\n",
"I added some good external resources in the links above on feature engineering and model selection. The next pages here will dig into model evaluation because it gets at the flow of testing a model."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}