{
  "aliases": null,
  "backlinks": null,
  "body": "\n## Definition\n\nLet $(x_1, y_1), \\dots, (x_n, y_n)$ be data, and let a fitting rule map any subset $S$ of the data to a prediction function $\\hat f_S$.\nLet $\\ell(y, \\hat y) \\ge 0$ be a [[loss function|3BTG6VVCGZ]].\nLet $K$ be an integer with $2 \\le K \\le n$, and let $F_1, \\dots, F_K$ be a partition of $\\set{1, \\dots, n}$ into nonempty folds.\nFor each $k$, let $\\hat f^{(-k)}$ be the prediction function fitted on the data with indices outside $F_k$.\nThe *$K$-fold cross-validation estimate* of the prediction error is\n$$\\mathrm{CV} = \\frac{1}{n} \\sum_{k=1}^{K} \\sum_{i \\in F_k} \\ell\\big(y_i, \\hat f^{(-k)}(x_i)\\big).$$\n\n## Remarks\n\nThe case $K = n$, with one observation per fold, is *leave-one-out* cross-validation.\nTo choose a tuning parameter, one computes $\\mathrm{CV}$ for each candidate value and keeps a value with the smallest $\\mathrm{CV}$.\nExample: for least squares with $\\ell(y, \\hat y) = (y - \\hat y)^2$, leave-one-out has a closed form.\nIt equals $\\frac{1}{n} \\sum_i \\big((y_i - \\hat y_i)/(1 - h_{ii})\\big)^2$, where $\\hat y_i$ is the full fit and $h_{ii}$ the $i$-th diagonal entry of the hat matrix.\nNon-example: the training error $\\frac{1}{n} \\sum_i \\ell(y_i, \\hat f(x_i))$ with $\\hat f$ fitted on all data is not cross-validation.\nEach $y_i$ is predicted by a function that used it, so the error is biased downwards.\n",
  "commit": "ff8a40749dc16545e3d2fc637f6b0196e81d0bfa",
  "front": {
    "defines": "HA1KJ3B9DX",
    "kind": "definition",
    "msc": [
      "62G09",
      "62J05"
    ],
    "name": "Cross-validation",
    "sources": [
      {
        "url": "https://en.wikipedia.org/wiki/Cross-validation_(statistics)"
      }
    ],
    "tag": "SZ7QZBDPYJ",
    "uses": [
      "3BTG6VVCGZ",
      "0TTNRP5PFQ"
    ]
  },
  "kind": "definition",
  "link": "https://tamnd.github.io/math2/node/SZ7QZBDPYJ/",
  "links": [
    {
      "field": "defines",
      "target": "HA1KJ3B9DX"
    },
    {
      "field": "uses",
      "target": "3BTG6VVCGZ"
    },
    {
      "field": "uses",
      "target": "0TTNRP5PFQ"
    }
  ],
  "markdown": "https://tamnd.github.io/math2/node/SZ7QZBDPYJ.md",
  "msc": [
    "62G09",
    "62J05"
  ],
  "name": "Cross-validation",
  "prov": null,
  "role": "",
  "source": "https://github.com/tamnd/math2/blob/main/nodes/SZ7/SZ7QZBDPYJ.md",
  "status": null,
  "tag": "SZ7QZBDPYJ",
  "uri": "m2://SZ7QZBDPYJ",
  "url": "https://tamnd.github.io/math2/wiki/cross-validation-definition/",
  "writing": {
    "tag": "SZ7QZBDPYJ",
    "kind": "definition",
    "level": "W0",
    "next": [
      {
        "what": "an inventory row",
        "note": "the row of the inventory that the node accounts for",
        "have": 0,
        "want": 1
      },
      {
        "what": "a source with a locator",
        "have": 0,
        "want": 1
      }
    ]
  }
}
