
    -ji)                        S SK Jr  S SKJr  S SKJr  S SKrS SKJ	r	  S SKJ
r
  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  Sr " S S\5      r " S S5      rg)    )annotations)Callable)
NamedTupleN)BaseDistribution)CategoricalChoiceType)CategoricalDistribution)FloatDistribution)IntDistribution) _BatchedCategoricalDistributions))_BatchedDiscreteTruncLogNormDistributions)&_BatchedDiscreteTruncNormDistributions)_BatchedDistributions)!_BatchedTruncLogNormDistributions)_BatchedTruncNormDistributions)_MixtureOfProductDistributiong-q=c                  R    \ rS rSr% S\S'   S\S'   S\S'   S\S'   S\S	'   S
\S'   Srg)_ParzenEstimatorParameters   floatprior_weightboolconsider_magic_clipconsider_endpointsCallable[[int], np.ndarray]weightsmultivariatezJdict[str, Callable[[CategoricalChoiceType, CategoricalChoiceType], float]]categorical_distance_func N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r       _/home/james-whalen/.local/lib/python3.13/site-packages/optuna/samplers/_tpe/parzen_estimator.pyr   r      s*    ((  r%   r   c                      \ rS rSr S         SS jjrSS jrSS jr\SS j5       rSS jr	SS jr
          SS	 jr          SS
 jr        SS jrSrg)_ParzenEstimator&   Nc                b   UR                   S:  a  [        SUR                    S35      eX l        U R                  U5      nUb  [	        U5      [	        U5      :X  d   eUb  UO$U R                  UR                  [	        U5      5      n[	        U5      S:X  a  [        R                  " S/5      nO![        R                  " XcR                   /5      nXfR                  5       -  n[        U[        U5       VVs/ s H"  u  pxU R                  US S 2U4   XU   U5      PM$     snnS9U l        g s  snnf )Nr   zAA non-negative value must be specified for prior_weight, but got .      ?)r   distributions)r   
ValueError_search_space
_transformlen_call_weights_funcr   nparrayappendsumr   	enumerate_calculate_distributions_mixture_distribution)	selfobservationssearch_space
parameterspredetermined_weightstransformed_observationsr   iparams	            r&   __init___ParzenEstimator.__init__'   sG    ""Q&&334A7 
 *#'??<#@ $,4L0MQT!R
 1
 	
 

 %0 "((););SAY=Z[ 	 '(A-hhuoGii*A*A)BCG;;= %B
 !*, 7	 !8HA --,QT2E;NPZ !8	&
"s   6)D+c                Z    U R                   R                  X5      nU R                  U5      $ N)r9   sample_untransform)r:   rngsizesampleds       r&   rF   _ParzenEstimator.sampleP   s)    ,,33C>  ))r%   c                Z    U R                  U5      nU R                  R                  U5      $ rE   )r0   r9   log_pdf)r:   samples_dicttransformed_sampless      r&   rM   _ParzenEstimator.log_pdfT   s)    "ool;))112EFFr%   c                   [         R                  " U " U5      5      S U n[         R                  " US:  5      (       a  [        SU S3SU S3-   5      e[	        U5      S:  a0  [         R
                  " U5      S::  a  [        SU S3SU S3-   5      e[         R                  " [         R                  " U5      5      (       d  [        SU S	U S3-   5      eU$ )
Nr   z@The `weights` function is not allowed to return negative values z. z*The argument of the `weights` function is r+   z?The `weight` function is not allowed to return all-zero values z+ The argument of the `weights` function is zFThe `weights`function is not allowed to return infinite or NaN values z,. The argument of the `weights` function is )r3   r4   anyr.   r1   r6   allisfinite)weights_funcnws      r&   r2   #_ParzenEstimator._call_weights_funcX   s    HH\!_%bq)66!a%==RSTRUUWX>qcCD  q6A:"&&)q.QRSQTTUV?s!DE  vvbkk!n%%XCCA3aHI  r%   c                    [         R                  " U R                   Vs/ s H  o!U   PM	     sn5      R                  $ s  snf rE   )r3   r4   r/   T)r:   rN   rA   s      r&   r0   _ParzenEstimator._transformo   s4    xx$:L:LM:Le,:LMNPPPMs   >c                p    [        U R                  5       VVs0 s H  u  p#X1S S 2U4   _M     snn$ s  snnf rE   )r7   r/   )r:   samples_arrayr@   rA   s       r&   rG   _ParzenEstimator._untransformr   s5    ;DTEWEW;XY;XxqQT**;XYYYs   2c                    [        U[        5      (       a  U R                  XX45      $ [        U[        [        45      (       d   eU R                  XU5      $ rE   )
isinstancer   $_calculate_categorical_distributionsr	   r
   "_calculate_numerical_distributions)r:   r;   
param_namer<   r=   s        r&   r8   )_ParzenEstimator._calculate_distributionsu   sY     l$;<<<<,  l->,PQQQQ::<Wabbr%   c                >   UR                   n[        U5      n[        U5      S:X  a!  [        [        R                  " SU4SU-  S9S9$ [        U5      S-   n[        R                  " Xv4UR
                  U-  S9nUR                  [        5      n	X$R                  ;   Ga  [        R                  " U	SS9u  pUR                  U   n[        R                  " U
 VVs/ s H  o Vs/ s H  o" X]   U5      PM     snPM     snn5      n[        R                  " XtR
                  -  5      [        R                  " U5      -  [        R                  " S	5      -  n[        R                  " U[        R                  " USS
9S S 2[        R                  4   -  S-  * U-  5      nUU   US [        U	5      & O,U[        R                  " [        U	5      5      U	4==   S-  ss'   UR!                  SSS9nU[        R"                  " US:H  SU5      -  n[        U5      $ s  snf s  snnf )Nr      r,   )
fill_value)r   shaperg   T)return_inverse   )axis   )rl   keepdims)choicesr1   r   r3   fullr   astypeintr   uniquer4   logexpmaxnewaxisaranger6   where)r:   r;   rc   r<   r=   ro   	n_choices	n_kernelsr   observed_indicesused_indicesrev_indices	dist_funcr@   cdistscoefcat_weightsrow_sumss                      r&   ra   5_ParzenEstimator._calculate_categorical_distributions   s    &&L	|!3I3?K  %)	''(!..:
 (..s3=== )+		2BSW(X%L"<<ZHIHHS_`S_aIAyQ7IS_`aE66)&=&==>	ARRUWU[U[\]U^^D&&EBFF5q,A!RZZ-,P$PUV#V!WZ^!^_K/:;/GG+c*+,BIIc"2346FFG1LG;;A;5288HM1h77/88 J`s   	H
"H6H
H
c                v  ^ ^^^^^ UR                   mUR                  mUR                  b$  TUR                  S-  -  mTUR                  S-  -  mUR                  (       aB  [        R                  " T5      m[        R                  " T5      m[        R                  " T5      mTmSUUUUUU 4S jjnU" 5       n[        R
                  " TSTT-   -  /5      m[        R
                  " UTT-
  /5      nUR                  cS  UR                  (       d!  [        TXRR                   UR                  5      $ [        TXRR                   UR                  5      $ UR                  (       d,  [        TXRR                   UR                  UR                  5      $ [        TXRR                   UR                  UR                  5      $ )Nrm   c                   > TR                   (       a\  Sn U [        [        T5      S5      S[        TR                  5      S-   -  -  -  TT-
  -  n[        R
                  " [        T5      4US9nOSTT-   -  n[        R                  " TU5      n[        R                  " U5      nXE   n[        R                  " [        U5      S-   [        S9nTUS	'   XgSS
& TUS
'   [        R                  " USS
 US	S -
  USS  USS
 -
  5      nTR                  (       d/  UR                  S	   S:  a  US   US   -
  US	'   US   US   -
  US
'   U[        R                  " U5         S [        T5       nTT-
  n	TR                  (       a$  [        T5      S-   n
TT-
  [        SSU
-   5      -  nO[        n[        R                   " [        R"                  " X+U	5      5      $ )Ng?rf   g         rh         ?rm   )dtyper   g      Y@r,   )r   rv   r1   r/   r3   rp   r5   argsortemptyr   maximumr   ri   r   minEPSasarrayclip)SIGMA0_MAGNITUDEsigmasigmasprior_mumus_with_priorsorted_indices
sorted_mussorted_mus_with_endpointssorted_sigmasmaxsigmar{   minsigmahighlowmusr;   r=   r:   s               r&   compute_sigmasK_ParzenEstimator._calculate_numerical_distributions.<locals>.compute_sigmas   s   &&#& $#l+Q/DC@R@R<SVW<W4XYZcz# 
 L(9';N #*-!#3!9!#N!;+;
,.HHS5H15LTY,Z)/2)!,2<!B/04)"- "

-a36OPQRT6UU-ab14MaPR4SS!
 "449R9X9XYZ9[_`9`'@'CF_`aFb'bM!$1"58QRT8UU ""% 'rzz.'ABCVSEVW czH---1	 3J#ecIo*GG::bggfABBr%   r   )return
np.ndarray)
r   r   steprt   r3   r5   r   r   r   r   )	r:   r;   r<   r=   r   r   r   r   r   s	   `` `  @@@r&   rb   3_ParzenEstimator._calculate_numerical_distributions   s      (<$$q((CL%%))D66,/L&&+C66$<D*	C *	CX  !iicS4Z0126D3J<0$##5!1!1<3D3D  9!1!1<3D3D   ##=!1!1<3D3DlFWFW  A!1!1<3D3DlFWFW r%   )r9   r/   rE   )
r;   dict[str, np.ndarray]r<   zdict[str, BaseDistribution]r=   r   r>   znp.ndarray | Noner   None)rH   znp.random.RandomStaterI   rr   r   r   )rN   r   r   r   )rU   r   rV   rr   r   r   )r]   r   r   r   )
r;   r   rc   strr<   r   r=   r   r   r   )
r;   r   rc   r   r<   r   r=   r   r   r   )r;   r   r<   z#FloatDistribution | IntDistributionr=   r   r   r   )r   r    r!   r"   rB   rF   rM   staticmethodr2   r0   rG   r8   ra   rb   r$   r   r%   r&   r(   r(   &   s	    48'
+'
 2'
 /	'

  1'
 
'
R*G  ,QZc c c '	c
 /c 
c"9 "9 "9 .	"9
 /"9 
"9HS S :S /	S
 
Sr%   r(   )
__future__r   collections.abcr   typingr   numpyr3   optuna.distributionsr   r   r   r	   r
   .optuna.samplers._tpe.probability_distributionsr   r   r   r   r   r   r   r   r   r(   r   r%   r&   <module>r      sY    " $   1 6 8 2 0 [ b P \ Y X  U Ur%   