a
    Sic5                     @   s|   d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ ddlmZ dgZG dd dZdS )a(  
This module was copied from the scipy project.

In the process of copying, some methods were removed because they depended on
other parts of scipy (especially on compiled components), allowing seaborn to
have a simple and pure Python implementation. These include:

- integrate_gaussian
- integrate_box
- integrate_box_1d
- integrate_kde
- logpdf
- resample

Additionally, the numpy.linalg module was substituted for scipy.linalg,
and the examples section (with doctests) was removed from the docstring

The original scipy license is copied below:

Copyright (c) 2001-2002 Enthought, Inc.  2003-2019, SciPy Developers.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above
   copyright notice, this list of conditions and the following
   disclaimer in the documentation and/or other materials provided
   with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    N)asarray
atleast_2dreshapezerosnewaxisdotexppisqrtravelpower
atleast_1dsqueezesum	transposeonescov)linalggaussian_kdec                   @   sr   e Zd ZdZdddZdd ZeZdd Zd	d
 ZeZ	de	_dddZ
dd Zdd Zedd Zedd ZdS )r   aw  Representation of a kernel-density estimate using Gaussian kernels.

    Kernel density estimation is a way to estimate the probability density
    function (PDF) of a random variable in a non-parametric way.
    `gaussian_kde` works for both uni-variate and multi-variate data.   It
    includes automatic bandwidth determination.  The estimation works best for
    a unimodal distribution; bimodal or multi-modal distributions tend to be
    oversmoothed.

    Parameters
    ----------
    dataset : array_like
        Datapoints to estimate from. In case of univariate data this is a 1-D
        array, otherwise a 2-D array with shape (# of dims, # of data).
    bw_method : str, scalar or callable, optional
        The method used to calculate the estimator bandwidth.  This can be
        'scott', 'silverman', a scalar constant or a callable.  If a scalar,
        this will be used directly as `kde.factor`.  If a callable, it should
        take a `gaussian_kde` instance as only parameter and return a scalar.
        If None (default), 'scott' is used.  See Notes for more details.
    weights : array_like, optional
        weights of datapoints. This must be the same shape as dataset.
        If None (default), the samples are assumed to be equally weighted

    Attributes
    ----------
    dataset : ndarray
        The dataset with which `gaussian_kde` was initialized.
    d : int
        Number of dimensions.
    n : int
        Number of datapoints.
    neff : int
        Effective number of datapoints.

        .. versionadded:: 1.2.0
    factor : float
        The bandwidth factor, obtained from `kde.covariance_factor`, with which
        the covariance matrix is multiplied.
    covariance : ndarray
        The covariance matrix of `dataset`, scaled by the calculated bandwidth
        (`kde.factor`).
    inv_cov : ndarray
        The inverse of `covariance`.

    Methods
    -------
    evaluate
    __call__
    integrate_gaussian
    integrate_box_1d
    integrate_box
    integrate_kde
    pdf
    logpdf
    resample
    set_bandwidth
    covariance_factor

    Notes
    -----
    Bandwidth selection strongly influences the estimate obtained from the KDE
    (much more so than the actual shape of the kernel).  Bandwidth selection
    can be done by a "rule of thumb", by cross-validation, by "plug-in
    methods" or by other means; see [3]_, [4]_ for reviews.  `gaussian_kde`
    uses a rule of thumb, the default is Scott's Rule.

    Scott's Rule [1]_, implemented as `scotts_factor`, is::

        n**(-1./(d+4)),

    with ``n`` the number of data points and ``d`` the number of dimensions.
    In the case of unequally weighted points, `scotts_factor` becomes::

        neff**(-1./(d+4)),

    with ``neff`` the effective number of datapoints.
    Silverman's Rule [2]_, implemented as `silverman_factor`, is::

        (n * (d + 2) / 4.)**(-1. / (d + 4)).

    or in the case of unequally weighted points::

        (neff * (d + 2) / 4.)**(-1. / (d + 4)).

    Good general descriptions of kernel density estimation can be found in [1]_
    and [2]_, the mathematics for this multi-dimensional implementation can be
    found in [1]_.

    With a set of weighted samples, the effective number of datapoints ``neff``
    is defined by::

        neff = sum(weights)^2 / sum(weights^2)

    as detailed in [5]_.

    References
    ----------
    .. [1] D.W. Scott, "Multivariate Density Estimation: Theory, Practice, and
           Visualization", John Wiley & Sons, New York, Chicester, 1992.
    .. [2] B.W. Silverman, "Density Estimation for Statistics and Data
           Analysis", Vol. 26, Monographs on Statistics and Applied Probability,
           Chapman and Hall, London, 1986.
    .. [3] B.A. Turlach, "Bandwidth Selection in Kernel Density Estimation: A
           Review", CORE and Institut de Statistique, Vol. 19, pp. 1-33, 1993.
    .. [4] D.M. Bashtannyk and R.J. Hyndman, "Bandwidth selection for kernel
           conditional density estimation", Computational Statistics & Data
           Analysis, Vol. 36, pp. 279-298, 2001.
    .. [5] Gray P. G., 1969, Journal of the Royal Statistical Society.
           Series A (General), 132, 272

    Nc                 C   s   t t|| _| jjdks"td| jj\| _| _|d urt|	t
| _|  jt| j  _| jjdkrrtdt| j| jkrtddt| jd  | _| j|d d S )N   z.`dataset` input should have multiple elements.z*`weights` input should be one-dimensional.z%`weights` input should be of length n   	bw_method)r   r   datasetsize
ValueErrorshapednr   astypefloat_weightsr   weightsndimlen_neffset_bandwidth)selfr   r   r"    r(   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/seaborn/external/kde.py__init__   s    zgaussian_kde.__init__c                 C   sb  t t|}|j\}}|| jkrb|dkrH|| jkrHt|| jdf}d}nd| d| j }t|t| j|}t	|f|d}t
| j}t|| j}t||}	|| jkrt| jD ]F}
|dd|
tf |	 }t|| ddd }|| j|
 t|  7 }qnVt|D ]L}
||	dd|
tf  }t|| ddd }tt| | j dd||
< q|| j }|S )	a  Evaluate the estimated pdf on a set of points.

        Parameters
        ----------
        points : (# of dimensions, # of points)-array
            Alternatively, a (# of dimensions,) vector can be passed in and
            treated as a single point.

        Returns
        -------
        values : (# of points,)-array
            The values at each point.

        Raises
        ------
        ValueError : if the dimensionality of the input points is different than
                     the dimensionality of the KDE.

        r   zpoints have dimension z, dataset has dimension )dtypeNr   )axis       @)r   r   r   r   r   r   npcommon_type
covariancer   r   choleskyinv_covr   r   r   ranger   r   r"   r   _norm_factor)r'   pointsr   mmsgoutput_dtyperesultZ	whiteningZscaled_datasetZscaled_pointsidiffenergyr(   r(   r)   evaluate   s0    



 
zgaussian_kde.evaluatec                 C   s   t | jd| jd  S )zoCompute Scott's factor.

        Returns
        -------
        s : float
            Scott's factor.
                 r   neffr   r'   r(   r(   r)   scotts_factor  s    zgaussian_kde.scotts_factorc                 C   s$   t | j| jd  d d| jd  S )z{Compute the Silverman factor.

        Returns
        -------
        s : float
            The silverman factor.
        r-   g      @r>   r?   r@   rB   r(   r(   r)   silverman_factor  s    zgaussian_kde.silverman_factora0  Computes the coefficient (`kde.factor`) that
        multiplies the data covariance matrix to obtain the kernel covariance
        matrix. The default is `scotts_factor`.  A subclass can overwrite this
        method to provide a different method, or set it through a call to
        `kde.set_bandwidth`.c                    s    du r
nx dkrj _nf dkr.j_nTt rXt tsXd_ fdd_n*t rv _fdd_nd}t	|
  dS )	a  Compute the estimator bandwidth with given method.

        The new bandwidth calculated after a call to `set_bandwidth` is used
        for subsequent evaluations of the estimated density.

        Parameters
        ----------
        bw_method : str, scalar or callable, optional
            The method used to calculate the estimator bandwidth.  This can be
            'scott', 'silverman', a scalar constant or a callable.  If a
            scalar, this will be used directly as `kde.factor`.  If a callable,
            it should take a `gaussian_kde` instance as only parameter and
            return a scalar.  If None (default), nothing happens; the current
            `kde.covariance_factor` method is kept.

        Notes
        -----
        .. versionadded:: 0.11

        Nscott	silvermanzuse constantc                      s    S Nr(   r(   r   r(   r)   <lambda>H      z,gaussian_kde.set_bandwidth.<locals>.<lambda>c                      s
      S rG   )
_bw_methodr(   rB   r(   r)   rH   K  rI   zC`bw_method` should be 'scott', 'silverman', a scalar or a callable.)rC   covariance_factorrD   r.   isscalar
isinstancestrrJ   callabler   _compute_covariance)r'   r   r7   r(   )r   r'   r)   r&   +  s    

zgaussian_kde.set_bandwidthc                 C   s~   |   | _t| ds<tt| jdd| jd| _t	| j| _
| j| jd  | _| j
| jd  | _ttdt | j | _dS )zcComputes the covariance matrix for each Gaussian kernel using
        covariance_factor().
        _data_inv_covr   F)rowvarbiasaweightsr   N)rK   factorhasattrr   r   r   r"   Z_data_covariancer   invrQ   r0   r2   r
   detr	   r4   rB   r(   r(   r)   rP   S  s    



z gaussian_kde._compute_covariancec                 C   s
   |  |S )z
        Evaluate the estimated pdf on a provided set of points.

        Notes
        -----
        This is an alias for `gaussian_kde.evaluate`.  See the ``evaluate``
        docstring for more details.

        )r=   )r'   xr(   r(   r)   pdfc  s    
zgaussian_kde.pdfc                 C   s8   z| j W S  ty2   t| j| j | _ | j  Y S 0 d S rG   )r!   AttributeErrorr   r   rB   r(   r(   r)   r"   o  s
    zgaussian_kde.weightsc                 C   s:   z| j W S  ty4   dt| jd  | _ | j  Y S 0 d S )Nr   r   )r%   r[   r   r"   rB   r(   r(   r)   rA   w  s
    zgaussian_kde.neff)NN)N)__name__
__module____qualname____doc__r*   r=   __call__rC   rD   rK   r&   rP   rZ   propertyr"   rA   r(   r(   r(   r)   r   R   s   p
8

(
)r_   numpyr.   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   __all__r   r(   r(   r(   r)   <module>   s
   GL