{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Let the diagonal elements of a diagonal covariance matrix "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "D"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " be stored in the vector "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "d"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "When all elements of "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "d"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are strictly positive, whitening of a data point "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is performed by computing "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "x \\cdot d^{-1/2}"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", where the inverse square root can be taken element-wise. "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "\\log\\det{D}"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is calculated as "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "-2 \\sum(\\log{d})"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", where the "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "\\log"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " operation is performed element-wise."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "This "
            },
            {
              "__type": "CrossRef",
              "__tag": 4002,
              "value": "Covariance",
              "reference": {
                "__type": "LocalRef",
                "__tag": 4022,
                "kind": "module",
                "path": "scipy.stats._covariance:Covariance"
              },
              "kind": "module"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " class supports singular covariance matrices. When computing "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "_log_pdet"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", non-positive elements of "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "d"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are ignored. Whitening is not well defined when the point to be whitened does not lie in the span of the columns of the covariance matrix. The convention taken here is to treat the inverse square root of non-positive elements of "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "d"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " as zeros."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Return a representation of a covariance matrix from its diagonal."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "diagonal",
              "annotation": "array_like",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The diagonal elements of a diagonal matrix."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/scipy/stats/_covariance.py",
  "item_line": 70,
  "item_type": "staticmethod",
  "aliases": [
    "scipy.stats.Covariance.from_diagonal"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "Prepare a symmetric positive definite covariance matrix ``A`` and a\ndata point ``x``.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import numpy as np\nfrom scipy import stats\nrng = np.random.default_rng()\nn = 5\nA = np.diag(rng.random(n))\nx = rng.random(size=n)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nExtract the diagonal from ``A`` and create the `Covariance` object.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "d = np.diag(A)\ncov = stats.Covariance.from_diagonal(d)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nCompare the functionality of the `Covariance` object against a\nreference implementations.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "res = cov.whiten(x)\nref = np.diag(d**-0.5) @ x\nnp.allclose(res, ref)\nres = cov.log_pdet\nref = np.linalg.slogdet(A)[-1]\nnp.allclose(res, ref)\n",
        "execution_status": "success"
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "diagonal",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "from_diagonal"
  },
  "references": null,
  "qa": "scipy.stats._covariance:Covariance.from_diagonal",
  "arbitrary": [],
  "local_refs": [
    "diagonal"
  ]
}