U
    d  ć                   @   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
   ś=/tmp/pip-unpacked-wheel-ua33x9lu/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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 d | j jddS )Né   r   ©Śmin)r	   r   Śclampr   r
   r
   r   Śmode&   s    z
Gamma.modec                 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$   6   s    
zGamma.expandc                 C   sD   |   |”}t| j |”| j |” }| ” jt |j	”j
d |S )Nr   )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   s`   t j|| jj| jjd}| jr(|  |” t  | j| j”t  | jd |” | j|  t  	| j” S )N)r(   Śdevicer   )
r   Z	as_tensorr   r(   r,   r%   Z_validate_sampleZxlogyr	   Ślgamma)r   r*   r
   r
   r   Ślog_probE   s    
’ž
žzGamma.log_probc                 C   s4   | j t | j” t | j ” d| j  t | j ”  S )Ng      š?)r	   r   Ślogr   r-   Zdigammar   r
   r
   r   ŚentropyM   s    ’zGamma.entropyc                 C   s   | j d | j fS ©Nr   r   r   r
   r
   r   Ś_natural_paramsQ   s    zGamma._natural_paramsc                 C   s&   t  |d ”|d t  | ”  ”  S r1   )r   r-   r/   Z
reciprocal)r   ŚxŚyr
   r
   r   Ś_log_normalizerU   s    zGamma._log_normalizer)N)N)Ś__name__Ś
__module__Ś__qualname__Ś__doc__r   ZpositiveZarg_constraintsZnonnegativeZsupportZhas_rsampleZ_mean_carrier_measureŚpropertyr   r   r   r    r$   r   r   r+   r.   r0   r2   r5   Ś__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   