
    (phb                         S SK r S SKrS SKJrJr  S SKJ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JrJr  S SKrSSKJr  SSKJrJr  S/r\ R>                  " 5       r  " S	 S5      r!S
 r"g)    N)linalgspecial)check_random_state)asarray
atleast_2dreshapezerosnewaxisexppisqrtravelpower
atleast_1dsqueezesum	transposeonescov   )_mvn)gaussian_kernel_estimategaussian_kernel_estimate_loggaussian_kdec                       \ rS rSrSrSS jrS r\rS rS r	SS jr
S	 rSS
 jrS rS r\rS\l        SS jrS r\S 5       rS rS rS r\S 5       r\S 5       rSrg)r   +   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`. The square
    of `kde.factor` multiplies the covariance matrix of the data in the kde
    estimation.
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]_.

`gaussian_kde` does not currently support data that lies in a
lower-dimensional subspace of the space in which it is expressed. For such
data, consider performing principal component analysis / dimensionality
reduction and using `gaussian_kde` with the transformed data.

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

Examples
--------
Generate some random two-dimensional data:

>>> import numpy as np
>>> from scipy import stats
>>> def measure(n):
...     "Measurement model, return two coupled measurements."
...     m1 = np.random.normal(size=n)
...     m2 = np.random.normal(scale=0.5, size=n)
...     return m1+m2, m1-m2

>>> m1, m2 = measure(2000)
>>> xmin = m1.min()
>>> xmax = m1.max()
>>> ymin = m2.min()
>>> ymax = m2.max()

Perform a kernel density estimate on the data:

>>> X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
>>> positions = np.vstack([X.ravel(), Y.ravel()])
>>> values = np.vstack([m1, m2])
>>> kernel = stats.gaussian_kde(values)
>>> Z = np.reshape(kernel(positions).T, X.shape)

Plot the results:

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,
...           extent=[xmin, xmax, ymin, ymax])
>>> ax.plot(m1, m2, 'k.', markersize=2)
>>> ax.set_xlim([xmin, xmax])
>>> ax.set_ylim([ymin, ymax])
>>> plt.show()

Nc                     [        [        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 R                  :  a  Sn[	        U5      e U R#                  US9  g ! [$        R&                   a  nSn[$        R&                  " U5      UeS nAff = f)	Nr   z.`dataset` input should have multiple elements.z*`weights` input should be one-dimensional.z%`weights` input should be of length n   a1  Number of dimensions is greater than number of samples. This results in a singular data covariance matrix, which cannot be treated using the algorithms implemented in `gaussian_kde`. Note that `gaussian_kde` interprets each *column* of `dataset` to be a point; consider transposing the input to `dataset`.	bw_methodab  The data appears to lie in a lower-dimensional subspace of the space in which it is expressed. This has resulted in a singular data covariance matrix, which cannot be treated using the algorithms implemented in `gaussian_kde`. Consider performing principal component analysis / dimensionality reduction and using `gaussian_kde` with the transformed data.)r   r   datasetsize
ValueErrorshapednr   astypefloat_weightsr   weightsndimlen_neffset_bandwidthr   LinAlgError)selfr!   r    r*   msges         C/var/www/html/venv/lib/python3.13/site-packages/scipy/stats/_kde.py__init__gaussian_kde.__init__   sA   !''"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 66DFF?-C S/!
	13!! 	1?C $$S)q0	1s   E F/FFc                    [        [        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[        U R                  U5      u  pV[        U   " U R                  R                  U R                  SS2S4   UR                  U R                  U5      nUSS2S4   $ )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   points have dimension , dataset has dimension Nr   )r   r   r$   r%   r   r#   _get_output_dtype
covariancer   r!   Tr*   cho_cov)r0   pointsr%   mr1   output_dtypespecresults           r3   evaluategaussian_kde.evaluate   s    ( GFO,||;Av!vv+ &&!5/s 3004x9 o%.tG)$/LLNNDLLD1HHdllL2 ad|    c                    [        [        U5      5      n[        U5      nUR                  U R                  4:w  a  [        SU R                   35      eUR                  U R                  U R                  4:w  a  [        SU R                   35      eUSS2[        4   nU R                  U-   n[        R                  " U5      nU R                  U-
  n[        R                  " XE5      n[        R                  " [        R                  " US   5      5      n[        S[         -  UR                  S   S-  5      U-  n[#        XV-  SS9S-  n	[#        [%        U	* 5      U R&                  -  SS9U-  n
U
$ )a  
Multiply estimated density by a multivariate Gaussian and integrate
over the whole space.

Parameters
----------
mean : aray_like
    A 1-D array, specifying the mean of the Gaussian.
cov : array_like
    A 2-D array, specifying the covariance matrix of the Gaussian.

Returns
-------
result : scalar
    The value of the integral.

Raises
------
ValueError
    If the mean or covariance of the input Gaussian differs from
    the KDE's dimensionality.

zmean does not have dimension z#covariance does not have dimension Nr   r          @axis)r   r   r   r$   r%   r#   r
   r:   r   
cho_factorr!   	cho_solvenpproddiagonalr   r   r   r   r*   )r0   meanr   sum_covsum_cov_choldifftdiffsqrt_det
norm_constenergiesrA   s              r3   integrate_gaussiangaussian_kde.integrate_gaussian  s8   0 '$-(o::$&&"<TVVHEFF99((B466(KLL AwJ//C'
 ((1||d"  4772;;|A781r67==#3c#9:XE
t|!,s2S(^DLL0q9JFrD   c                    U R                   S:w  a  [        S5      e[        [        U R                  5      5      S   n[        XR
                  -
  U-  5      n[        X R
                  -
  U-  5      n[        R                  " U R                  [        R                  " U5      [        R                  " U5      -
  -  5      nU$ )a4  
Computes the integral of a 1D pdf between two bounds.

Parameters
----------
low : scalar
    Lower bound of integration.
high : scalar
    Upper bound of integration.

Returns
-------
value : scalar
    The result of the integral.

Raises
------
ValueError
    If the KDE is over more than one dimension.

r   z'integrate_box_1d() only handles 1D pdfsr   )r%   r#   r   r   r:   r!   rK   r   r*   r   ndtr)r0   lowhighstdevnormalized_lownormalized_highvalues          r3   integrate_box_1dgaussian_kde.integrate_box_1dL  s    , 66Q;FGGd4??+,Q/ll 2e;<!4 =>t||_5^456 7 rD   c                 *   Ub  SU0nO0 n[            [        R                  " XU R                  U R                  U R
                  40 UD6u  pVSSS5        W(       a'  SU R                  S-   3n[        R                  " USS9  W$ ! , (       d  f       N>= f)a  Computes the integral of a pdf over a rectangular interval.

Parameters
----------
low_bounds : array_like
    A 1-D array containing the lower bounds of integration.
high_bounds : array_like
    A 1-D array containing the upper bounds of integration.
maxpts : int, optional
    The maximum number of points to use for integration.

Returns
-------
value : scalar
    The result of the integral.

Nmaxptsz4An integral in _mvn.mvnun requires more points than i  r   )
stacklevel)	MVN_LOCKr   mvnun_weightedr!   r*   r:   r%   warningswarn)r0   
low_boundshigh_boundsrc   
extra_kwdsr_   informr1   s           r3   integrate_boxgaussian_kde.integrate_boxo  s    $ "F+JJ //
04dll04OCMOME  HRVXCMM#!, Xs   ;B
Bc                    UR                   U R                   :w  a  [        S5      eUR                  U R                  :  a  UnU nOU nUnUR                  UR                  -   n[        R
                  " U5      nSn[        UR                  5       H  nUR                  SS2U[        4   nUR                  U-
  n	[        R                  " XY5      n
[        X-  SS9S-  nU[        [        U* 5      UR                  -  SS9UR                  U   -  -  nM     [        R                  " [        R                  " US   5      5      n[!        S["        -  UR$                  S   S-  5      U-  nXm-  nU$ )a'  
Computes the integral of the product of this  kernel density estimate
with another.

Parameters
----------
other : gaussian_kde instance
    The other kde.

Returns
-------
value : scalar
    The result of the integral.

Raises
------
ValueError
    If the KDEs have different dimensionality.

z$KDEs are not the same dimensionalityg        Nr   rG   rF   r   )r%   r#   r&   r:   r   rI   ranger!   r
   rJ   r   r   r*   rK   rL   rM   r   r   r$   )r0   othersmalllargerO   rP   rA   irN   rQ   rR   rU   rS   rT   s                 r3   integrate_kdegaussian_kde.integrate_kde  sC   * 77dffCDD 77TVVEEEE""U%5%55((1uwwA==Aw/D==4'D$$\8E4<a036Hc#xi.6Q?a@PPPF   772;;|A781r67==#3c#9:XE
rD   c                 8   Uc  [        U R                  5      n[        U5      n[        UR	                  [        U R                  4[        5      U R                  US95      nUR                  U R                  XR                  S9nU R                  SS2U4   nXd-   $ )a  Randomly sample a dataset from the estimated pdf.

Parameters
----------
size : int, optional
    The number of samples to draw.  If not provided, then the size is
    the same as the effective number of samples in the underlying
    dataset.
seed : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
    If `seed` is None (or `np.random`), the `numpy.random.RandomState`
    singleton is used.
    If `seed` is an int, a new ``RandomState`` instance is used,
    seeded with `seed`.
    If `seed` is already a ``Generator`` or ``RandomState`` instance then
    that instance is used.

Returns
-------
resample : (self.d, `size`) ndarray
    The sampled dataset.

N)r"   )r"   p)intneffr   r   multivariate_normalr	   r%   r(   r:   choicer&   r*   r!   )r0   r"   seedrandom_statenormindicesmeanss          r3   resamplegaussian_kde.resample  s    . <tyy>D)$/99466)U#T__4 : 
  %%dff4<<%HQZ(|rD   c                 N    [        U R                  SU R                  S-   -  5      $ )zGCompute Scott's factor.

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

Returns
-------
s : float
    The silverman factor.
rF   g      @r   r   r   r   s    r3   silverman_factorgaussian_kde.silverman_factor  s3     TYYs
+C/dffQh@@rD   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)a8  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

Examples
--------
>>> import numpy as np
>>> import scipy.stats as stats
>>> x1 = np.array([-7, -5, 1, 4, 5.])
>>> kde = stats.gaussian_kde(x1)
>>> xs = np.linspace(-10, 10, num=50)
>>> y1 = kde(xs)
>>> kde.set_bandwidth(bw_method='silverman')
>>> y2 = kde(xs)
>>> kde.set_bandwidth(bw_method=kde.factor / 3.)
>>> y3 = kde(xs)

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> ax.plot(x1, np.full(x1.shape, 1 / (4. * x1.size)), 'bo',
...         label='Data points (rescaled)')
>>> ax.plot(xs, y1, label='Scott (default)')
>>> ax.plot(xs, y2, label='Silverman')
>>> ax.plot(xs, y3, label='Const (1/3 * Silverman)')
>>> ax.legend()
>>> plt.show()

Nscott	silvermanzuse constantc                     > T $ N r   s   r3   <lambda>,gaussian_kde.set_bandwidth.<locals>.<lambda>5  s    YrD   c                  &   > T R                  T 5      $ r   )
_bw_methodr   s   r3   r   r   8  s    T__T-BrD   zC`bw_method` should be 'scott', 'silverman', a scalar or a callable.)r   covariance_factorr   rK   isscalar
isinstancestrr   callabler#   _compute_covariance)r0   r    r1   s   `` r3   r.   gaussian_kde.set_bandwidth  s    X '!%)%7%7D"+%%)%:%:D"[[##Jy#,F,F,DO%6D"i  'DO%BD"#CS/!  "rD   c           
      |   U R                  5       U l        [        U S5      (       dR  [        [	        U R
                  SSU R                  S95      U l        [        R                  " U R                  SS9U l
        U R                  U R                  S-  -  U l        U R                  U R                  -  R                  [        R                  5      U l        S[        R                   " [        R"                  " U R                  [        R$                  " S[&        -  5      -  5      5      R)                  5       -  U l        g)	zSComputes the covariance matrix for each Gaussian kernel using
covariance_factor().
_data_cho_covr   FrowvarbiasaweightsT)lowerr   N)r   factorhasattrr   r   r!   r*   _data_covariancer   choleskyr   r:   r'   rK   float64r<   logdiagr   r   r   log_detr   s    r3   r    gaussian_kde._compute_covariance@  s     ,,.t_--$.s4<<498<0F %GD! "(1F1F7;"=D //$++q.@**T[[8@@L*,''!B$-)8 !9 ::=#%@rD   c           	          U R                  5       U l        [        [        U R                  SSU R
                  S95      U l        [        R                  " U R                  5      U R                  S-  -  $ )Nr   Fr   r   )	r   r   r   r   r!   r*   r   r   invr   s    r3   inv_covgaussian_kde.inv_covR  s]     ,,. *3t||A05,N !Ozz$//04;;>AArD   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.

)rB   )r0   xs     r3   pdfgaussian_kde.pdf^  s     }}QrD   c                    [        U5      nUR                  u  p4X0R                  :w  aL  US:X  a)  X@R                  :X  a  [        X R                  S45      nSnOSU SU R                   3n[	        U5      e[        U R                  U5      u  pg[        U   " U R                  R                  U R                  SS2S4   UR                  U R                  U5      nUSS2S4   $ )zD
Evaluate the log of the estimated pdf on a provided set of points.
r   r7   r8   Nr   )r   r$   r%   r   r#   r9   r:   r   r!   r;   r*   r<   )	r0   r   r=   r%   r>   r1   r?   r@   rA   s	            r3   logpdfgaussian_kde.logpdfj  s     A||;Av!vv+ &&!5/s 3004x9 o%.tG-d3LLNNDLLD1HHdllL2 ad|rD   c                 f   [         R                  " U5      n[         R                  " UR                  [         R                  5      (       d  Sn[        U5      e[        U R                  5      nUR                  5       nXBUS:     -   X"S:  '   [        [         R                  " U5      5      [        U5      :w  a  Sn[        U5      eUS:  X$:  -  n[         R                  " U5      (       a  SXV    SU S3n[        U5      eU R                  U   nU R                  n[        XpR                  5       US9$ )a  Return a marginal KDE distribution

Parameters
----------
dimensions : int or 1-d array_like
    The dimensions of the multivariate distribution corresponding
    with the marginal variables, that is, the indices of the dimensions
    that are being retained. The other dimensions are marginalized out.

Returns
-------
marginal_kde : gaussian_kde
    An object representing the marginal distribution.

Notes
-----
.. versionadded:: 1.10.0

zaElements of `dimensions` must be integers - the indices of the marginal variables being retained.r   z,All elements of `dimensions` must be unique.zDimensions z# are invalid for a distribution in z dimensions.)r    r*   )rK   r   
issubdtypedtypeintegerr#   r,   r!   copyuniqueanyr*   r   r   )	r0   
dimensionsdimsr1   r&   original_dims	i_invalidr!   r*   s	            r3   marginalgaussian_kde.marginal  s   * }}Z(}}TZZ44?CS/!		$(^+AXryy3t9,ACS/!AX$),	66) !9 : ;,,-3l<CS/!,,t$,,G/E/E/G$+- 	-rD   c                      U R                   $ ! [         a6    [        U R                  5      U R                  -  U l         U R                   s $ f = fr   )r)   AttributeErrorr   r&   r   s    r3   r*   gaussian_kde.weights  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-   r   r   r*   r   s    r3   rz   gaussian_kde.neff  sC    	:: 	3t||Q//DJ::	s    6AA)r   r   r   r-   r)   r<   r:   r   r%   r!   r   r   r&   )NNr   )__name__
__module____qualname____firstlineno____doc__r4   rB   __call__rV   r`   rm   ru   r   r   r   r   r.   r   propertyr   r   r   r   r*   rz   __static_attributes__r   rD   r3   r   r   +   s    Zv$1L&P H3j!FB0d!F0A &! =#~@$ 	B 	B
 0/-b ! !  rD   c                     [         R                  " X5      n[         R                  " U5      R                  nUS:X  a  SnX$4$ US:X  a  SnX$4$ US;   a  SnX$4$ [	        U SU 35      e)z
Calculates the output dtype and the "spec" (=C type name).

This was necessary in order to deal with the fused types in the Cython
routine `gaussian_kernel_estimate`. See gh-10824 for details.
r   r(      double)      zlong doublez has unexpected item size: )rK   common_typer   itemsizer#   )r:   r=   r?   r   r@   s        r3   r9   r9     s     >>*5Lxx%..H1}  
Q  
X	 	 . ;H:F 	rD   )#	threadingrg   scipyr   r   scipy._lib._utilr   numpyr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   rK    r   _statsr   r   __all__Lockre   r   r9   r   rD   r3   <module>r      sc   *   " /       J 
>>V
 V
rrD   