
    eCi!                        S SK Jr  S SKJr  S SKJrJr  S SKrS SKJ	r	  S SK
rS SK
Jr   S SKJr  SrS S
KJr  S SKJr  S SKJr  \ " S S\5      5       rg! \ a    S SKJr  S	r N3f = f)    )annotations)	dataclass)AnyCallableN)ndarray)	DataFrame)gaussian_kdeFT)GroupBy)Scale)Statc                      \ rS rSr% SrSrS\S'   SrS\S'   S	rS
\S'   S	r	S
\S'   Sr
S\S'   SrS\S'   SrS\S'   S rSS jrSS jrS S jr        S!S jr        S"S jr          S#S jrSrg)$KDE   aj  
Compute a univariate kernel density estimate.

Parameters
----------
bw_adjust : float
    Factor that multiplicatively scales the value chosen using
    `bw_method`. Increasing will make the curve smoother. See Notes.
bw_method : string, scalar, or callable
    Method for determining the smoothing bandwidth to use. Passed directly
    to :class:`scipy.stats.gaussian_kde`; see there for options.
common_norm : bool or list of variables
    If `True`, normalize so that the areas of all curves sums to 1.
    If `False`, normalize each curve independently. If a list, defines
    variable(s) to group by and normalize within.
common_grid : bool or list of variables
    If `True`, all curves will share the same evaluation grid.
    If `False`, each evaluation grid is independent. If a list, defines
    variable(s) to group by and share a grid within.
gridsize : int or None
    Number of points in the evaluation grid. If None, the density is
    evaluated at the original datapoints.
cut : float
    Factor, multiplied by the kernel bandwidth, that determines how far
    the evaluation grid extends past the extreme datapoints. When set to 0,
    the curve is truncated at the data limits.
cumulative : bool
    If True, estimate a cumulative distribution function. Requires scipy.

Notes
-----
The *bandwidth*, or standard deviation of the smoothing kernel, is an
important parameter. Much like histogram bin width, using the wrong
bandwidth can produce a distorted representation. Over-smoothing can erase
true features, while under-smoothing can create false ones. The default
uses a rule-of-thumb that works best for distributions that are roughly
bell-shaped. It is a good idea to check the default by varying `bw_adjust`.

Because the smoothing is performed with a Gaussian kernel, the estimated
density curve can extend to values that may not make sense. For example, the
curve may be drawn over negative values when data that are naturally
positive. The `cut` parameter can be used to control the evaluation range,
but datasets that have many observations close to a natural boundary may be
better served by a different method.

Similar distortions may arise when a dataset is naturally discrete or "spiky"
(containing many repeated observations of the same value). KDEs will always
produce a smooth curve, which could be misleading.

The units on the density axis are a common source of confusion. While kernel
density estimation produces a probability distribution, the height of the curve
at each point gives a density, not a probability. A probability can be obtained
only by integrating the density across a range. The curve is normalized so
that the integral over all possible values is 1, meaning that the scale of
the density axis depends on the data values.

If scipy is installed, its cython-accelerated implementation will be used.

Examples
--------
.. include:: ../docstrings/objects.KDE.rst

   float	bw_adjustscottz-str | float | Callable[[gaussian_kde], float]	bw_methodTzbool | list[str]common_normcommon_grid   z
int | Nonegridsize   cutFbool
cumulativec                T    U R                   (       a  [        (       a  [        S5      eg g )Nz(Cumulative KDE evaluation requires scipy)r   	_no_scipyRuntimeError)selfs    P/home/james-whalen/.local/lib/python3.13/site-packages/seaborn/_stats/density.py__post_init__KDE.__post_init__^   s     ??yyIJJ  )?    c                   [        X5      n[        U[        5      (       dU  [        U[        5      (       a  [	        S U 5       5      (       d)  U R
                  R                   SU 3n[        U S35      eU R                  XSS9  g)z'Do input checks on grouping parameters.c              3  B   #    U  H  n[        U[        5      v   M     g 7f)N)
isinstancestr).0vs     r!   	<genexpr>1KDE._check_var_list_or_boolean.<locals>.<genexpr>h   s     /REq
1c0B0BEs   .z& must be a boolean or list of strings.r   )
stacklevelN)	getattrr'   r   listall	__class____name__	TypeError_check_grouping_vars)r    paramgrouping_varsvalue
param_names        r!   _check_var_list_or_booleanKDE._check_var_list_or_booleanc   sw    $ud##5$''C/RE/R,R,R NN334AeW=Jzl*PQRR!!%1!Er$   c                    SU R                   0nSU;   a  US   US'   [        X   40 UD6nUR                  UR                  U R                  -  5        U$ )zFit and return a KDE object.r   weightweights)r   r	   set_bandwidthfactorr   )r    dataorientfit_kwskdes        r!   _fitKDE._fitn   sY     $/"?t!%hGI4<373#**t~~56
r$   c                   U R                   c  X   R                  5       $ U R                  X5      n[        R                  " UR
                  R                  5       5      nX   R                  5       X@R                  -  -
  nX   R                  5       X@R                  -  -   n[        R                  " XVU R                   5      $ )z2Define the grid that the KDE will be evaluated on.)r   to_numpyrE   npsqrt
covariancesqueezeminr   maxlinspace)r    rA   rB   rD   bwgridmingridmaxs          r!   _get_supportKDE._get_supportz   s    == <((**ii%WWS^^++-.,""$rHH}4,""$rHH}4{{7T]];;r$   c           	        [         R                  " USS/[        S9n[        U5      S:  a  U$  U R	                  X5      nU R                  (       a;  US   n[
        R                  " U Vs/ s H  ouR                  Xg5      PM     sn5      nOU" U5      nUS   R                  5       n	[         R                  " X#SU	SU05      $ ! [
        R                  R                   a    Us $ f = fs  snf )zITransform single group by fitting a KDE and evaluating on a support grid.r=   densitycolumnsdtype   r   )pdr   r   lenrE   rI   linalgLinAlgErrorr   arrayintegrate_box_1dsum)
r    rA   rB   supportemptyrD   s_0s_irV   r=   s
             r!   _fit_and_evaluateKDE._fit_and_evaluate   s     fh	%B%Pt9q=L	))D)C ??!*Chh'R'3 4 4S >'RSG'lGh##%||Vh	7STT yy$$ 	L	
  Ss   C *C( C%$C%c                   [         R                  " / UR                  QSP[        S9n[	        U5      S:  a  U$  U R                  X5      nU Vs/ s H  oaU   R                  5       S:  d  M  UPM     nnU(       d  U R                  XU5      $ [        U5      nUR                  XR                  X%5      $ ! [        R                  R                   a    Us $ f = fs  snf )z9Transform multiple groups by fitting KDEs and evaluating.rV   rW   rZ   r   )r[   r   rX   r   r\   rS   rI   r]   r^   nuniquerf   r
   apply)r    rA   rB   r7   rc   rb   xgroupbys           r!   
_transformKDE._transform   s     %?t||%?Y%?uMt9q=L	''5G %2KMq!W__5F5JMK))$@@-(}}T#9#96KK yy$$ 	L	 Ls   B5 C-C5 CCc                   SU;  a  UR                  SS9nUR                  US/S9nU Vs/ s H  oUUR                  ;   d  M  [        U5      PM!     nnU(       a  U R                  SL a  U R                  XU5      nOlU R                  SL a  UnO5U R                  SU5        U R                   Vs/ s H  oUU;   d  M
  UPM     nn[        U5      R                  XR
                  X65      nU(       a  U R                  SL a!  UR                  US   R                  5       S9nOU R                  SL a  Un	O5U R                  S	U5        U R                   Vs/ s H  oUU;   d  M
  UPM     n	nUR                  UR                  U	5      S   R                  5       R                  S
5      U	S9nUS==   UR                  S5      -  ss'   SSS.U   n
US   Xz'   UR                  SS
/SS9$ s  snf s  snf s  snf )Nr=   r   )r=   )subsetTFr   )group_weightr   rq   )onrV   zweight / group_weightyrk   )rk   rs   )axis)assigndropnaorderr(   r   rm   r:   r
   rj   r   ra   joinrl   renameevaldrop)r    rA   rl   rB   scalesr*   r7   res	grid_vars	norm_varsr8   s              r!   __call__KDE.__call__   s    4;;a;(D{{68"4{5 *.DAgmm1CQD 0 0D 8//$>C5()	//}M(,(8(8O(81<NQ(8	O 	"t__fD   0 0D 8**$x.*<*<*>*?C5()	//}M(,(8(8O(81<NQ(8	O((Y'1557>>~N  C
 	I#((#:;;$V,^
xx>2x;;C E P Ps#   G"G".	G';G'	G,G, N)r6   r(   r7   r   returnNone)rA   r   rB   r(   r   r	   )rA   r   rB   r(   r   r   )rA   r   rB   r(   rb   r   r   r   )rA   r   rB   r(   r7   z	list[str]r   r   )
rA   r   rl   r
   rB   r(   r|   zdict[str, Scale]r   r   )r3   
__module____qualname____firstlineno____doc__r   __annotations__r   r   r   r   r   r   r"   r:   rE   rS   rf   rm   r   __static_attributes__r   r$   r!   r   r      s    >~ Iu?FI<F$(K!($(K!(HjCNJK
	F
	<UU'*U5<U	U*LL'*L;DL	L$*<*<(/*<9<*<FV*<	*<r$   r   )
__future__r   dataclassesr   typingr   r   numpyrI   r   pandasr[   r   scipy.statsr	   r   ImportErrorseaborn.external.kdeseaborn._core.groupbyr
   seaborn._core.scalesr   seaborn._stats.baser   r   r   r$   r!   <module>r      si    " !      (I
 * & $ @<$ @< @<  1Is   A A&%A&