← Back
Editing: _covariance.cpython-311.pyc
� d�c�W � �� � d dl mZ d dlZd dlmZ d dlmZ dgZ G d� d� � Z G d� de � � Z d � Z G d � de � � Z G d� d e � � Z G d� de � � Z G d� de � � ZdS )� )�cached_propertyN)�linalg)� _multivariate� Covariancec �� � e Zd ZdZd� Zed� � � Zedd�� � Zed� � � Zed� � � Z d� Z d � Zed � � � Z ed� � � Zed� � � Zed � � � Zd� Zd� ZdS )r aL Representation of a covariance matrix Calculations involving covariance matrices (e.g. data whitening, multivariate normal function evaluation) are often performed more efficiently using a decomposition of the covariance matrix instead of the covariance metrix itself. This class allows the user to construct an object representing a covariance matrix using any of several decompositions and perform calculations using a common interface. .. note:: The `Covariance` class cannot be instantiated directly. Instead, use one of the factory methods (e.g. `Covariance.from_diagonal`). Examples -------- The `Covariance` class is is used by calling one of its factory methods to create a `Covariance` object, then pass that representation of the `Covariance` matrix as a shape parameter of a multivariate distribution. For instance, the multivariate normal distribution can accept an array representing a covariance matrix: >>> from scipy import stats >>> import numpy as np >>> d = [1, 2, 3] >>> A = np.diag(d) # a diagonal covariance matrix >>> x = [4, -2, 5] # a point of interest >>> dist = stats.multivariate_normal(mean=[0, 0, 0], cov=A) >>> dist.pdf(x) 4.9595685102808205e-08 but the calculations are performed in a very generic way that does not take advantage of any special properties of the covariance matrix. Because our covariance matrix is diagonal, we can use ``Covariance.from_diagonal`` to create an object representing the covariance matrix, and `multivariate_normal` can use this to compute the probability density function more efficiently. >>> cov = stats.Covariance.from_diagonal(d) >>> dist = stats.multivariate_normal(mean=[0, 0, 0], cov=cov) >>> dist.pdf(x) 4.9595685102808205e-08 c �$ � d}t |� � �)Nz�The `Covariance` class cannot be instantiated directly. Please use one of the factory methods (e.g. `Covariance.from_diagonal`).)�NotImplementedError)�self�messages �9/usr/lib/python3/dist-packages/scipy/stats/_covariance.py�__init__zCovariance.__init__; s � �8�� "�'�*�*�*� c � � t | � � S )a� Return a representation of a covariance matrix from its diagonal. Parameters ---------- diagonal : array_like The diagonal elements of a diagonal matrix. Notes ----- Let the diagonal elements of a diagonal covariance matrix :math:`D` be stored in the vector :math:`d`. When all elements of :math:`d` are strictly positive, whitening of a data point :math:`x` is performed by computing :math:`x \cdot d^{-1/2}`, where the inverse square root can be taken element-wise. :math:`\log\det{D}` is calculated as :math:`-2 \sum(\log{d})`, where the :math:`\log` operation is performed element-wise. This `Covariance` class supports singular covariance matrices. When computing ``_log_pdet``, non-positive elements of :math:`d` 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 :math:`d` as zeros. Examples -------- Prepare a symmetric positive definite covariance matrix ``A`` and a data point ``x``. >>> import numpy as np >>> from scipy import stats >>> rng = np.random.default_rng() >>> n = 5 >>> A = np.diag(rng.random(n)) >>> x = rng.random(size=n) Extract the diagonal from ``A`` and create the `Covariance` object. >>> d = np.diag(A) >>> cov = stats.Covariance.from_diagonal(d) Compare the functionality of the `Covariance` object against a reference implementations. >>> res = cov.whiten(x) >>> ref = np.diag(d**-0.5) @ x >>> np.allclose(res, ref) True >>> res = cov.log_pdet >>> ref = np.linalg.slogdet(A)[-1] >>> np.allclose(res, ref) True )�CovViaDiagonal)�diagonals r � from_diagonalzCovariance.from_diagonalA s � �v �h�'�'�'r Nc �"