
    (^i-                       d dl mZ d dlZd dlZd dlZd dlZd dlmZ  ej                  dej                  z        Z ej                  e      Z ej                  d      ej                  z  Z ej                  d      ZddZddZ ej$                  d      dd	       Zdd
Z ej$                  d      dd       ZddZddZddZddZddZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZy)    )annotationsNerf      c                .    t        j                  | |      S N)np	logaddexplog_plog_qs     `/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/samplers/_tpe/_truncnorm.py_log_sumr   3   s    <<u%%    c                `    | t        j                  t        j                  || z
               z   S r	   )r
   log1pexpr   s     r   	_log_diffr   7   s&    288RVVEEM22333r   i  c                    | dz  }|dk  rdt        j                  |       z  }|S |dk  rddt        j                  |      z  z   }|S ddt        j                  |      z  z
  }|S )N;f?g;f      ?g;f?      ?)matherfcr   )axys      r   _ndtr_singler   ;   sr    	F
A;$))QB- H 
Z### H #		!$$Hr   c                *    ddt        | dz        z  z   S )Nr   r   r   r   s    r   _ndtrr"   I   s    s1v:&&&r   c                F   | dkD  rt        |         S | dkD  rt        j                  t        |             S d| dz  z  t        j                  |        z
  dt        j                  dt        j                  z        z  z
  }d}d}d}d}d| dz  z  }d}d	}t	        ||z
        t
        j                  j                  kD  rO|dz  }|}| }||z  }|d|z  dz
  z  }|||z  |z  z  }t	        ||z
        t
        j                  j                  kD  rO|t        j                  |      z   S )
N   i      r   r   g        r      r   )r   r   logpiabssys
float_infoepsilon)	r   log_LHS
last_totalright_hand_side	numeratordenom_factor
denom_conssignis	            r   _log_ndtr_singler5   N   s*   1uaR   3wxxQ((QTkDHHaRL(3!dgg+1F+FFGJOILQTJD	A
j?*
+cnn.D.D
D	Q$
u
"QUQY	4)+l:: j?*
+cnn.D.D
D TXXo...r   c                j     t        j                  t        dd      |       j                  t              S )Nr&   )r
   
frompyfuncr5   astypefloatr!   s    r   	_log_ndtrr:   i   s(    02==)1a03::5AAr   c                "    | dz   dz  t         z
  S )Nr   g       @)_norm_pdf_logC)r   s    r   _norm_logpdfr=   m   s    T7S=>))r   c                   |dk  }| dkD  }||z   }dddfd}dd}t        j                  | t         j                  t         j                        }| |   x}j                  r |||         ||<   | |   x}	j                  r ||	||         ||<   | |   x}
j                  r ||
||         ||<   t        j
                  |      S )z3Log of Gaussian probability mass within an intervalr   c                >    t        t        |      t        |             S r	   )r   r:   r   bs     r   mass_case_leftz'_log_gauss_mass.<locals>.mass_case_leftz   s    1y|44r   c                     | |        S r	    )r   rA   rB   s     r   mass_case_rightz(_log_gauss_mass.<locals>.mass_case_right}   s    qb1"%%r   c                Z    t        j                  t        |        t        |       z
        S r	   )r
   r   r"   r@   s     r   mass_case_centralz*_log_gauss_mass.<locals>.mass_case_central   s$     xxq	E1"I-..r   )
fill_valuedtyper   
np.ndarrayrA   rK   returnrK   )r
   	full_likenan
complex128sizereal)r   rA   	case_left
case_rightcase_centralrE   rG   outa_lefta_right	a_centralrB   s              @r   _log_gauss_massrY   q   s    
 QIQJ+,L5&/ ,,qRVV2==
ACI,$$')=IZ= &&)'1Z=AJ|_$	**-i<IL773<r   c                H   | dkD  }| j                         }t        j                  t        j                  | |                ||<   t        j                  |       }t        j
                  |dk        x}d   j                  r&t        j                  d||   t        z   z         ||<   t        j
                  |dk\        x}d   j                  r7t         t        j                  t        j                  ||                z  ||<   t        d      D ]  }t        |      }d|dz  z  t        z
  }||z
  t        j                  ||z
        z  }	||	z  }t        j                  t        j                  |	      dt        j                  |      z  k        s n ||xx   d	z  cc<   |S )
a\	  
    Use the Newton method to efficiently find the root.

    `ndtri_exp(y)` returns `x` such that `y = log_ndtr(x)`, meaning that the Newton method is
    supposed to find the root of `f(x) := log_ndtr(x) - y = 0`.

    Since `df/dx = d log_ndtr(x)/dx = (ndtr(x))'/ndtr(x) = norm_pdf(x)/ndtr(x)`, the Newton update
    is x[n + 1] := x[n] - (log_ndtr(x) - y) * ndtr(x) / norm_pdf(x).

    As an initial guess, we use the Gaussian tail asymptotic approximation:
        1 - ndtr(x) \simeq norm_pdf(x) / x
        --> log(norm_pdf(x) / x) = -1/2 * x**2 - 1/2 * log(2pi) - log(x)

    First recall that y is a non-positive value and y = log_ndtr(inf) = 0 and
    y = log_ndtr(-inf) = -inf.

    If abs(y) is very small, we first derive -x such that z = log_ndtr(-x) and then flip the sign.
    Please note that the following holds:
        z = log_ndtr(-x) --> z = log(1 - ndtr(x)) = log(1 - exp(y)) = log(-expm1(y)).
    Recall that as long as ndtr(x) = exp(y) > 0.5 --> y > -log(2) = -0.693..., x becomes positive.

    ndtr(x) = exp(y) \simeq 1 + y --> -y \simeq 1 - ndtr(x). From this, we can calculate:
        log(1 - ndtr(x)) \simeq log(-y) \simeq -1/2 * x**2 - 1/2 * log(2pi) - log(x).
    Because x**2 >> log(x), we can ignore the second and third terms, leading to:
        log(-y) \simeq -1/2 * x**2 --> x \simeq sqrt(-2 log(-y)).
    where we take the positive `x` as abs(y) becomes very small only if x >> 0.
    The second order approximation version is sqrt(-2 log(-y) - log(-2 log(-y))).

    If abs(y) is very large, we use log_ndtr(x) \simeq -1/2 * x**2 - 1/2 * log(2pi) - log(x).
    To solve this equation analytically, we ignore the log term, yielding:
        log_ndtr(x) = y \simeq -1/2 * x**2 - 1/2 * log(2pi)
        --> y + 1/2 * log(2pi) = -1/2 * x**2 --> x**2 = -2 * (y + 1/2 * log(2pi))
        --> x = sqrt(-2 * (y + 1/2 * log(2pi))

    For the moderate y, we use Eq. (13), i.e., standard logistic CDF, in the following paper:

    - `Approximating the Cumulative Distribution Function of the Normal Distribution
      <https://jsr.isrt.ac.bd/wp-content/uploads/41n1_5.pdf>`__

    The standard logistic CDF approximates ndtr(x) with:
        exp(y) = ndtr(x) \simeq 1 / (1 + exp(-pi * x / sqrt(3)))
        --> exp(-y) \simeq 1 + exp(-pi * x / sqrt(3))
        --> log(exp(-y) - 1) \simeq -pi * x / sqrt(3)
        --> x \simeq -sqrt(3) / pi * log(exp(-y) - 1).
    g{Gzr   g       d   r%   r   g:0yE>)copyr
   r'   expm1
empty_likenonzerorP   sqrtr<   _ndtri_exp_approx_Cranger:   r   allr)   )
r   flippedzr   
small_indsmoderate_inds_
log_ndtr_xlog_norm_pdf_xdxs
             r   
_ndtri_exprn      sm   ^ %iG	A!G*--.AgJ
aAjjR((
!,11:)G!HII*AG,,a055//"&&1]CSBS9T2UU-3Z 	q\
1~5 1nzN'B CC	R66"&&*tbffQi//0	 gJ"J Hr   c                   t        j                  | ||      \  } }}t        j                  | ||      \  } }}|dk  }| }t        ||      }dd}dd}t        j                  |       }| |   x}	j
                  r ||	||   ||   ||         ||<   | |   x}
j
                  r ||
||   ||   ||         ||<   t        j                  ||k(  | dk(  | dk(  gt        j                  ||g|      S )a=  
    Compute the percent point function (inverse of cdf) at q of the given truncated Gaussian.

    Namely, this function returns the value `c` such that:
        q = \int_{a}^{c} f(x) dx

    where `f(x)` is the probability density function of the truncated normal distribution with
    the lower limit `a` and the upper limit `b`.

    More precisely, this function returns `c` such that:
        ndtr(c) = ndtr(a) + q * (ndtr(b) - ndtr(a))
    for the case where `a < 0`, i.e., `case_left`. For `case_right`, we flip the sign for the
    better numerical stability.
    r   c                n    t        t        |      t        j                  |       |z         }t	        |      S r	   )r   r:   r
   r'   rn   qr   rA   log_mass	log_Phi_xs        r   ppf_leftzppf.<locals>.ppf_left   s*    Yq\266!9x+?@	)$$r   c                t    t        t        |       t        j                  |        |z         }t	        |       S r	   )r   r:   r
   r   rn   rq   s        r   	ppf_rightzppf.<locals>.ppf_right   s3     Yr]BHHaRL8,CD	9%%%r   r&   default)
rr   rK   r   rK   rA   rK   rs   rK   rL   rK   )	r
   
atleast_1dbroadcast_arraysrY   r`   rP   selectr   rN   )rr   r   rA   rR   rS   rs   ru   rw   rU   q_leftq_rights              r   ppfr      s    mmAq!$GAq!!!!Q*GAq!AIJq!$H%& --
CI,$$!&!I,)hyFYZIZ= &&#GQz]AjM8T^K_`J99a1fa1fa1f-!Q/?MMr   c                    |xs t         j                  j                         }t        j                  | |||      j                  }|j                  dd|      }t        || |      |z  |z   S )z
    This function generates random variates from a truncated normal distribution defined between
    `a` and `b` with the mean of `loc` and the standard deviation of `scale`.
    r   r&   )lowhighrP   )r
   randomRandomState	broadcastshapeuniformr   )r   rA   locscalerandom_staterP   	quantiless          r   rvsr   	  se      :299#8#8#:L<<1c5)//D$$$>Iy!Q%'#--r   c                d   | |z
  |z  } t        j                  | ||      \  } }}t        |       t        ||      z
  t        j                  |      z
  }t        j
                  | ||      \  } }}t        j                  ||k(  | |k  | |kD  z  gt         j                  t         j                   g|      S )Nrx   )	r
   rz   r=   rY   r'   r{   r|   rN   inf)r   r   rA   r   r   rU   s         r   logpdfr     s     
SEAmmAq!$GAq!
q/OAq1
1BFF5M
AC!!!Q*GAq!99a1fq1uQ/0266BFF72CSQQr   )r   rK   r   rK   rL   rK   )r   r9   rL   r9   )r   rK   rL   rK   )r   rK   rL   rK   rJ   )r   rK   rL   rK   )rr   rK   r   np.ndarray | floatrA   r   rL   rK   )r   r&   N)r   rK   rA   rK   r   r   r   r   r   znp.random.RandomState | NonerL   rK   )r   r&   )r   rK   r   r   rA   r   r   r   r   r   rL   rK   )
__future__r   	functoolsr   r*   numpyr
   optuna.samplers._tpe._erfr   rb   r(   _norm_pdf_Cr'   r<   rc   _log_2r   r   	lru_cacher   r"   r5   r:   r=   rY   rn   r   r   r   rD   r   r   <module>r      sp  D #   
  ) diiDGG$+&diilTWW, 	!&4 T
 
'
 T/ /4B*$NEP&NX   !15... 
. 	.
 /. .*   !RRR R 
	R
 R Rr   