a
    –¥»a1  ã                   @   sT   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„ Z	G dd	„ d	eƒZ
dS )
é    )ÚNumberN)Úconstraints)ÚExponentialFamily)Úbroadcast_allc                 C   s
   t  | ¡S ©N)ÚtorchÚ_standard_gamma)Úconcentration© r
   úS/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/distributions/gamma.pyr   	   s    r   c                       s˜   e Zd ZdZejejdœZejZdZdZ	e
dd„ ƒZe
dd„ ƒZd‡ fd
d„	Zd‡ fdd„	Ze ¡ fdd„Zdd„ Zdd„ Ze
dd„ ƒZdd„ Z‡  ZS )ÚGammaa	  
    Creates a Gamma distribution parameterized by shape :attr:`concentration` and :attr:`rate`.

    Example::

        >>> m = Gamma(torch.tensor([1.0]), torch.tensor([1.0]))
        >>> m.sample()  # Gamma distributed with concentration=1 and rate=1
        tensor([ 0.1046])

    Args:
        concentration (float or Tensor): shape parameter of the distribution
            (often referred to as alpha)
        rate (float or Tensor): rate = 1 / scale of the distribution
            (often referred to as beta)
    ©r	   ÚrateTr   c                 C   s   | j | j S r   r   ©Úselfr
   r
   r   Úmean"   s    z
Gamma.meanc                 C   s   | j | j d¡ S )Né   )r	   r   Úpowr   r
   r
   r   Úvariance&   s    zGamma.varianceNc                    sR   t ||ƒ\| _| _t|tƒr0t|tƒr0t ¡ }n
| j ¡ }tt	| ƒj
||d� d S )N©Úvalidate_args)r   r	   r   Ú
isinstancer   r   ÚSizeÚsizeÚsuperr   Ú__init__)r   r	   r   r   Úbatch_shape©Ú	__class__r
   r   r   *   s
    

zGamma.__init__c                    sR   |   t|¡}t |¡}| j |¡|_| j |¡|_tt|ƒj|dd� | j	|_	|S )NFr   )
Z_get_checked_instancer   r   r   r	   Úexpandr   r   r   Ú_validate_args)r   r   Z	_instanceÚnewr   r
   r   r   2   s    
zGamma.expandc                 C   sD   |   |¡}t| j |¡ƒ| j |¡ }| ¡ jt |j	¡j
d� |S )N)Úmin)Z_extended_shaper   r	   r   r   ÚdetachZclamp_r   ZfinfoÚdtypeZtiny)r   Zsample_shapeÚshapeÚvaluer
   r
   r   Úrsample;   s    
zGamma.rsamplec                 C   sd   t j|| jj| jjd�}| jr(|  |¡ | jt  | j¡ | jd t  |¡  | j|  t  	| j¡ S )N)r$   Údeviceé   )
r   Z	as_tensorr   r$   r(   r    Z_validate_sampler	   ÚlogÚlgamma)r   r&   r
   r
   r   Úlog_probA   s    
ÿþ
þzGamma.log_probc                 C   s4   | j t | j¡ t | j ¡ d| j  t | j ¡  S )Ng      ð?)r	   r   r*   r   r+   Zdigammar   r
   r
   r   ÚentropyI   s    ÿzGamma.entropyc                 C   s   | j d | j fS ©Nr)   r   r   r
   r
   r   Ú_natural_paramsM   s    zGamma._natural_paramsc                 C   s&   t  |d ¡|d t  | ¡  ¡  S r.   )r   r+   r*   Z
reciprocal)r   ÚxÚyr
   r
   r   Ú_log_normalizerQ   s    zGamma._log_normalizer)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZpositiveZarg_constraintsZsupportZhas_rsampleZ_mean_carrier_measureÚpropertyr   r   r   r   r   r   r'   r,   r-   r/   r2   Ú__classcell__r
   r
   r   r   r      s"   

	
r   )Znumbersr   r   Ztorch.distributionsr   Ztorch.distributions.exp_familyr   Ztorch.distributions.utilsr   r   r   r
   r
   r
   r   Ú<module>   s   