
    eCi5                     v    S r SSKrSSKJrJrJrJrJrJrJ	r	J
r
JrJrJrJrJrJr  SSKJr  S/r " S S5      rg)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power
atleast_1dsumonescov)linalggaussian_kdec                       \ rS rSrSrSS jrS r\rS rS r	\r
S\
l        SS	 jrS
 rS r\S 5       r\S 5       rSrg)r   Q   a  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                 f   [        [        U5      5      U l        U R                  R                  S:  d  [	        S5      eU R                  R
                  u  U l        U l        Ub  [        U5      R                  [        5      U l        U =R                  [        U R                  5      -  sl        U R                  R                  S:w  a  [	        S5      e[        U R                  5      U R                  :w  a  [	        S5      eS[        U R                  S-  5      -  U l        U R#                  US9  g )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#   s       N/home/james-whalen/.local/lib/python3.13/site-packages/seaborn/external/kde.py__init__gaussian_kde.__init__   s    !''"23||  1$MNN++&w/66u=DMMMS//M||  A% !MNN4==!TVV+ !HII3t}}a/00DJY/    c                 p   [        [        U5      5      nUR                  u  p#X R                  :w  aL  US:X  a)  X0R                  :X  a  [	        XR                  S45      nSnOSU SU R                   3n[        U5      e[        R                  " U R                  U5      n[        U4US9n[        R                  " U R                  5      n[        XpR                  5      n[        Xq5      n	X0R                  :  a[  [!        U R                  5       HA  n
USS2U
["        4   U	-
  n[%        X-  SS9S-  nX`R&                  U
   [)        U* 5      -  -  nMC     OT[!        U5       HE  n
XSS2U
["        4   -
  n[%        X-  SS9S-  n[%        [)        U* 5      U R&                  -  SS9Xj'   MG     X`R*                  -  nU$ )	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result	whiteningscaled_datasetscaled_pointsidiffenergys                r)   evaluategaussian_kde.evaluate   s   ( GFO,||;Av!vv+ &&!5.qc1I$&&R o%~~doov>t<0OODLL1	Y5I.;466]%aGm4}DT[q1C7,,q/#vg,66 # 1X%aGm(DDT[q1C7VGT\\ 9B	 
 +++r,   c                 N    [        U R                  SU R                  S-   -  5      $ )zGCompute Scott's factor.

Returns
-------
s : float
    Scott's factor.
         r   neffr   r(   s    r)   scotts_factorgaussian_kde.scotts_factor  s!     TYYTVVAX//r,   c                 t    [        U R                  U R                  S-   -  S-  SU R                  S-   -  5      $ )zSCompute the Silverman factor.

Returns
-------
s : float
    The silverman factor.
r0   g      @rF   rG   rH   rJ   s    r)   silverman_factorgaussian_kde.silverman_factor  s3     TYYs
+C/dffQh@@r,   a0  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                 v  ^ ^ Tc  OTS:X  a  T R                   T l        OTS:X  a  T R                  T l        Os[        R                  " T5      (       a(  [        T[        5      (       d  ST l        U4S jT l        O0[        T5      (       a  TT l        U 4S jT l        OSn[        U5      eT R                  5         g)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                     > T $ N r   s   r)   <lambda>,gaussian_kde.set_bandwidth.<locals>.<lambda>G  s    Yr,   c                  &   > T R                  T 5      $ rT   )
_bw_methodrJ   s   r)   rV   rW   J  s    T__T-Br,   zC`bw_method` should be 'scott', 'silverman', a scalar or a callable.)rK   covariance_factorrN   r1   isscalar
isinstancestrrY   callabler   _compute_covariance)r(   r   r:   s   `` r)   r'   gaussian_kde.set_bandwidth*  s    * '!%)%7%7D"+%%)%:%:D"[[##Jy#,F,F,DO%6D"i  'DO%BD"#CS/!  "r,   c           	         U R                  5       U l        [        U S5      (       dS  [        [	        U R
                  SSU R                  S95      U l        [        R                  " U R                  5      U l
        U R                  U R                  S-  -  U l        U R                  U R                  S-  -  U l        [        [        R                  " S[        -  U R                  -  5      5      U l        g)zSComputes the covariance matrix for each Gaussian kernel using
covariance_factor().
_data_inv_covr   F)rowvarbiasaweightsr   N)rZ   factorhasattrr   r   r   r#   _data_covariancer   invrb   r3   r5   r   detr
   r7   rJ   s    r)   r_    gaussian_kde._compute_covarianceR  s     ,,.t_--$.s4<<498<0F %GD! "(D,A,A!BD//$++q.@))DKKN: AbD,@!ABr,   c                 $    U R                  U5      $ )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.

)rC   )r(   xs     r)   pdfgaussian_kde.pdfb  s     }}Qr,   c                      U R                   $ ! [         a6    [        U R                  5      U R                  -  U l         U R                   s $ f = frT   )r"   AttributeErrorr   r   rJ   s    r)   r#   gaussian_kde.weightsn  sB    	!==  	! L/DM== 	!s    =AAc                      U R                   $ ! [         a/    S[        U R                  S-  5      -  U l         U R                   s $ f = f)Nr   r   )r&   rq   r   r#   rJ   s    r)   rI   gaussian_kde.neffv  sC    	:: 	3t||Q//DJ::	s    6AA)rY   rh   rb   r&   r7   r"   r3   rZ   r   r   rf   r5   r   )NNrT   )__name__
__module____qualname____firstlineno____doc__r*   rC   __call__rK   rN   rZ   r'   r_   rn   propertyr#   rI   __static_attributes__rU   r,   r)   r   r   Q   sr    o`0$6p H0A &! &#PC 
  ! !  r,   )ry   numpyr1   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   __all__r   rU   r,   r)   <module>r      s=   2N < < < <  
k kr,   