
    Di)                        d dl mZ d dlmZ d dlmZ d dlZd dlm	Z	 d dlm
Z
 d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ dZ G d de      Z G d d      Zy)    )annotations)Callable)
NamedTupleN)BaseDistribution)CategoricalChoiceType)CategoricalDistribution)FloatDistribution)IntDistribution) _BatchedCategoricalDistributions))_BatchedDiscreteTruncLogNormDistributions)&_BatchedDiscreteTruncNormDistributions)_BatchedDistributions)!_BatchedTruncLogNormDistributions)_BatchedTruncNormDistributions)_MixtureOfProductDistributiong-q=c                  J    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	ed
<   y)_ParzenEstimatorParametersfloatprior_weightboolconsider_magic_clipconsider_endpointsCallable[[int], np.ndarray]weightsmultivariatezJdict[str, Callable[[CategoricalChoiceType, CategoricalChoiceType], float]]categorical_distance_funcN)__name__
__module____qualname____annotations__     k/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/optuna/samplers/_tpe/parzen_estimator.pyr   r      s*    ((  r"   r   c                      e Zd Z	 d	 	 	 	 	 	 	 	 	 ddZddZddZedd       ZddZddZ		 	 	 	 	 	 	 	 	 	 ddZ
	 	 	 	 	 	 	 	 	 	 dd	Z	 	 	 	 	 	 	 	 dd
Zy)_ParzenEstimatorNc                ^   |j                   dk  rt        d|j                    d      || _        | j                  |      }|t	        |      t	        |      k(  sJ ||n$| j                  |j                  t	        |            }t	        |      dk(  rt        j                  dg      }n!t        j                  ||j                   g      }||j                         z  }t        |t        |      D cg c]#  \  }}| j                  |d d |f   |||   |      % c}}      | _        y c c}}w )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__z_ParzenEstimator.__init__'   sJ    ""Q&&334A7 
 *#'??<#@ $,4L0MQT!R
 1
 	
 

 %0 "((););SAY=Z[ 	 '(A-hhuoGii**A*A)BCG7;;= %B
 !*, 7	 Au --,QT2E<;NPZ&
"s   2(D)c                \    | j                   j                  ||      }| j                  |      S N)r5   sample_untransform)r6   rngsizesampleds       r#   rA   z_ParzenEstimator.sampleP   s+    ,,33C>  ))r"   c                Z    | j                  |      }| j                  j                  |      S r@   )r,   r5   log_pdf)r6   samples_dicttransformed_sampless      r#   rG   z_ParzenEstimator.log_pdfT   s)    "ool;))112EFFr"   c                   t        j                   | |            d | }t        j                  |dk        rt        d| dd| dz         t	        |      dkD  r.t        j
                  |      dk  rt        d| dd| dz         t        j                  t        j                  |            st        d| d	| dz         |S )
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 )r/   r0   anyr*   r-   r2   allisfinite)weights_funcnws      r#   r.   z#_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                    t        j                  | j                  D cg c]  }||   	 c}      j                  S c c}w r@   )r/   r0   r+   T)r6   rH   r=   s      r#   r,   z_ParzenEstimator._transformo   s0    xx$:L:LMe,MNPPPMs   ;c                n    t        | j                        D ci c]  \  }}||d d |f    c}}S c c}}w r@   )r3   r+   )r6   samples_arrayr<   r=   s       r#   rB   z_ParzenEstimator._untransformr   s3    ;DTEWEW;XYxq%}QT**YYYs   1c                    t        |t              r| j                  ||||      S t        |t        t        f      sJ | j                  |||      S r@   )
isinstancer   $_calculate_categorical_distributionsr	   r
   "_calculate_numerical_distributions)r6   r7   
param_namer8   r9   s        r#   r4   z)_ParzenEstimator._calculate_distributionsu   sY     l$;<<<j,
  l->,PQQQ::<Wabbr"   c                H   |j                   }t        |      }t        |      dk(  r&t        t        j                  d|fd|z              S t        |      dz   }t        j                  ||f|j
                  |z        }|j                  t              }	||j                  v rt        j                  |	d      \  }
}|j                  |   }t        j                  |
D cg c]  }|D cg c]  } |||   |       c} c}}      }t        j                  ||j
                  z        t        j                  |      z  t        j                  d	      z  }t        j                  |t        j                  |d
      d d t        j                  f   z  dz   |z        }||   |d t        |	       n+|t        j                  t        |	            |	fxx   dz  cc<   |j!                  dd      }|t        j"                  |dk(  d|      z  }t        |      S c c}w c c}}w )Nr      r(   )
fill_value)r   shaper\   T)return_inverse   )axis   )ra   keepdims)choicesr-   r   r/   fullr   astypeintr   uniquer0   logexpmaxnewaxisaranger2   where)r6   r7   rY   r8   r9   rd   	n_choices	n_kernelsr   observed_indicesused_indicesrev_indices	dist_funcr<   cdistscoefcat_weightsrow_sumss                      r#   rW   z5_ParzenEstimator._calculate_categorical_distributions   s    &&L	|!3I3?K  %)	''i(!..:
 (..s3=== )+		2BSW(X%L+"<<ZHIHHS_`aIAyQ7I`aE66)j&=&==>	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=H
H
c                N    |j                   |j                  |j                  $|j                  dz  z  |j                  dz  z  |j                  r?t	        j                        t	        j                        t	        j                        d fd} |       }t	        j
                  dz   z  g      t	        j
                  |z
  g      }|j                  P|j                  s"t        ||j                   |j                        S t        ||j                   |j                        S |j                  s-t        ||j                   |j                  |j                        S t        ||j                   |j                  |j                        S )Nrb   c                    j                   r^d} | t        t              d      dt        j                        dz   z  z  z  z
  z  }t	        j
                  t              f|      }ndz   z  }t	        j                  |      }t	        j                  |      }||   }t	        j                  t        |      dz   t              }|d	<   ||dd
 |d
<   t	        j                  |dd
 |d	d z
  |dd  |dd
 z
        }j                  s.|j                  d	   dk\  r|d   |d   z
  |d	<   |d   |d   z
  |d
<   |t	        j                  |         d t               }z
  }	j                  r$t              dz   }
z
  t        dd|
z         z  }nt        }t	        j                   t	        j"                  |||	            S )Ng?r[   g         r]         ?rb   )dtyper   g      Y@r(   )r   rk   r-   r+   r/   re   r1   argsortemptyr   maximumr   r^   r   minEPSasarrayclip)SIGMA0_MAGNITUDEsigmasigmasprior_mumus_with_priorsorted_indices
sorted_mussorted_mus_with_endpointssorted_sigmasmaxsigmarp   minsigmahighlowmusr7   r9   r6   s               r#   compute_sigmaszK_ParzenEstimator._calculate_numerical_distributions.<locals>.compute_sigmas   s   &&#& $#l+Q/DC@R@R<SVW<W4XYZcz# 
 L(9';N #*-!#3!9!#N!;+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::bggfhABBr"   r}   )return
np.ndarray)
r   r   stepri   r/   r1   r   r   r   r   )	r6   r7   r8   r9   r   r   r   r   r   s	   `` `  @@@r#   rX   z3_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"   r@   )
r7   dict[str, np.ndarray]r8   zdict[str, BaseDistribution]r9   r   r:   znp.ndarray | Noner   None)rC   znp.random.RandomStaterD   rg   r   r   )rH   r   r   r   )rN   r   rO   rg   r   r   )rT   r   r   r   )
r7   r   rY   strr8   r   r9   r   r   r   )
r7   r   rY   r   r8   r   r9   r   r   r   )r7   r   r8   z#FloatDistribution | IntDistributionr9   r   r   r   )r   r   r   r>   rA   rG   staticmethodr.   r,   rB   r4   rW   rX   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   numpyr/   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"   