o
    }oi&                     @  s   d dl mZ d dlmZ d dlmZ d dlZd dlmZ G dd deZ	G dd	 d	eZ
G d
d deZG dd deZG dd deZee	e
eeef ZdddZdddZG dd deZdS )    )annotations)
NamedTuple)UnionN)
_truncnormc                   @  s   e Zd ZU ded< dS ) _BatchedCategoricalDistributions
np.ndarrayweightsN__name__
__module____qualname____annotations__ r   r   b/home/ubuntu/.local/lib/python3.10/site-packages/optuna/samplers/_tpe/probability_distributions.pyr      s   
 r   c                   @  .   e Zd ZU ded< ded< ded< ded< dS )_BatchedTruncNormDistributionsr   musigmafloatlowhighNr	   r   r   r   r   r      
   
 r   c                   @  r   )!_BatchedTruncLogNormDistributionsr   r   r   r   r   r   Nr	   r   r   r   r   r      r   r   c                   @  6   e Zd ZU ded< ded< ded< ded< ded< dS )	&_BatchedDiscreteTruncNormDistributionsr   r   r   r   r   r   stepNr	   r   r   r   r   r         
 r   c                   @  r   )	)_BatchedDiscreteTruncLogNormDistributionsr   r   r   r   r   r   r   Nr	   r   r   r   r   r   %   r   r   ar   breturn)tuple[np.ndarray, np.ndarray, np.ndarray]c                 C  s   | j |j krt| j dksJ t|}|tj| | dd }| | }|| }tj| td}d|d< |dd |dd k|dd |dd kB |dd< tj|jtd}t	|d ||< || || |fS )	z
    This function is a quicker version of:
        np.unique(np.concatenate([a[:, None], b[:, None]], axis=-1), return_inverse=True).
       stable)kinddtypeTr   N)
shapelennpargsort
empty_likeboolemptysizeintcumsum)r   r   ordera_orderb_orderis_first_occurrenceinvr   r   r   _unique_inverse_2d6   s   
8r7   c                 C  s0   t |  | \}}}t||| | jS )z
    This function reduces the log Gaussian probability mass computation by avoiding the
    duplicated evaluations using the np.unique_inverse(...) equivalent operation.
    )r7   ravelr   _log_gauss_massreshaper(   )r   r   a_uniqb_uniqr6   r   r   r   _log_gauss_mass_uniqueI   s   r=   c                   @  s2   e Zd ZU ded< ded< dd
dZdddZdS )_MixtureOfProductDistributionr   r   zlist[_BatchedDistributions]distributionsrngnp.random.RandomState
batch_sizer0   r    c                   s  |j t| j| j|d tj|t| jftd}g g g }}}g }g g }}	t| jD ]\}
}t|t	ry|j d d f }|
|}tj|dd}t|d d df d sZJ d|d d df< tj||d d tjf k dd|d d |
f< q,t|tr|| ||
 ||j |	|j q,t|tr|| ||
 ||
 |t|j |	t|j q,t|tr||
 || ||
 ||j|jd   |	|j|jd   q,t|tr||
 || ||
 ||
 |t|j|jd   |	t|j|jd   q,J t|rt fdd	|D }t fd
d	|D }tdd	 |D }tdd	 |D }tdd	 |D }tjt|d d tjf | | t|	d d tjf | | |||dj|d d |f< t|d d |f |d d |f< t|dkd }|| || || }}}t |t!|d d |f | | |  |||d d |f< |S )N)pr/   r%   r'   axisr"      Fc                      g | ]}|j   qS r   r   .0dactive_indicesr   r   
<listcomp>       z8_MixtureOfProductDistribution.sample.<locals>.<listcomp>c                   rG   r   r   rI   rL   r   r   rN      rO   c                 S     g | ]}|j qS r   )r   rI   r   r   r   rN          c                 S  rQ   r   )r   rI   r   r   r   rN      rR   c                 S  s   g | ]}t |d dqS )r           )getattrrI   r   r   r   rN      s    )r   r   locscalerandom_staterS   r   )"choicer)   r   r*   r.   r?   r   	enumerate
isinstancer   randr1   iscloseallsumnewaxisr   appendr   r   r   logr   r   r   asarrayarrayr   rvsTexpnonzeroclipround)selfr@   rB   ret	disc_indsnumerical_indslog_indsnumerical_distslows_numerichighs_numericirK   active_weightsrnd_quantile	cum_probs
active_musactive_sigmaslowshighsstepssteps_not_0low_dstep_dhigh_dr   rL   r   sampleV   sz   


 .















"(z$_MixtureOfProductDistribution.samplexc              
   C  s  t jt|t| jft jd}g }g }g }g }t| jD ]]\}}t|trJ|d d |t j	t j	f 
t j}	|t t j|jt j	 |	ddd 7 }qt|trl|| ||d d |f  ||j ||j qt|tr|| |t |d d |f  |t |j |t |j qt|trt j|d d |f dd\}
}t|j|j\}}}|t|
|jd  d d t j	f | | |
|jd  d d t j	f | | t || 7 }|t|j|jd  | | |j|jd  | | | 8 }qt|tryt j|d d |f dd\}
}t|j|j\}}}|tt |
|jd  d d t j	f | | t |
|jd  d d t j	f | | t || 7 }|tt |j|jd  | | t |j|jd  | | | 8 }qJ t|rt d	d
 |D j}t dd
 |D j}|tj t |jd d t j	d d f t || | t || | ||dj!dd7 }|t | jt j	 7 }|j"dd}d|t #|< t j$dd t t %||d d d f  j!dd| W  d    S 1 sw   Y  d S )Nr%   r'   rD   ).r   T)return_inverserF   Fc                 S  rQ   r   rH   rI   r   r   r   rN      rR   z9_MixtureOfProductDistribution.log_pdf.<locals>.<listcomp>c                 S  rQ   r   rP   rI   r   r   r   rN      rR   )r   r   rU   rV   r"   r   ignore)divide)&r*   zerosr)   r   float64rY   r?   rZ   r   r_   astypeint64ra   take_along_axisr   r`   r   r   r   r   uniquer7   r   r   r=   r   ix_r   r9   r   rb   re   logpdfr^   maxisneginferrstaterf   )rj   r   weighted_log_pdf
cont_distsx_cont	lows_cont
highs_contrr   rK   xixi_uniqxi_invmu_uniq
sigma_uniqmu_sigma_invmus_contsigmas_contmax_r   r   r   log_pdf   s   
 



""
((

*&z%_MixtureOfProductDistribution.log_pdfN)r@   rA   rB   r0   r    r   )r   r   r    r   )r
   r   r   r   r   r   r   r   r   r   r>   R   s
   
 
Dr>   )r   r   r   r   r    r!   )r   r   r   r   r    r   )
__future__r   typingr   r   numpyr*   optuna.samplers._tper   r   r   r   r   r   _BatchedDistributionsr7   r=   r>   r   r   r   r   <module>   s*    
	
	