3
O4W^/  ã               @   sX   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
 G dd„ deƒZdS )	é    )ÚNumberN)Úconstraints)Ú	Dirichlet)ÚExponentialFamily)Úbroadcast_allc                   s¨   e Zd ZdZejejdœZejZdZ	d‡ fdd„	Z
d‡ fdd„	Zed	d
„ ƒZedd„ ƒZf fdd„Zdd„ Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZdd„ Z‡  ZS )ÚBetaa8  
    Beta distribution parameterized by :attr:`concentration1` and :attr:`concentration0`.

    Example::

        >>> m = Beta(torch.tensor([0.5]), torch.tensor([0.5]))
        >>> m.sample()  # Beta distributed with concentration concentration1 and concentration0
        tensor([ 0.1046])

    Args:
        concentration1 (float or Tensor): 1st concentration parameter of the distribution
            (often referred to as alpha)
        concentration0 (float or Tensor): 2nd concentration parameter of the distribution
            (often referred to as beta)
    )Úconcentration1Úconcentration0TNc                sp   t |tƒr,t |tƒr,tjt|ƒt|ƒgƒ}nt||ƒ\}}tj||gdƒ}t|ƒ| _t	t
| ƒj| jj|d� d S )Né   )Úvalidate_argséÿÿÿÿ)Ú
isinstancer   ÚtorchÚtensorÚfloatr   Ústackr   Ú
_dirichletÚsuperr   Ú__init__Z_batch_shape)Úselfr   r	   r   Zconcentration1_concentration0)Ú	__class__© ú9/tmp/pip-build-e66j97b2/torch/torch/distributions/beta.pyr      s    
zBeta.__init__c                sD   | j t|ƒ}tj|ƒ}| jj|ƒ|_tt|ƒj|dd� | j|_|S )NF)r   )	Z_get_checked_instancer   r   ÚSizer   Úexpandr   r   Ú_validate_args)r   Zbatch_shapeZ	_instanceÚnew)r   r   r   r   '   s    
zBeta.expandc             C   s   | j | j | j  S )N)r   r	   )r   r   r   r   Úmean/   s    z	Beta.meanc             C   s*   | j | j }| j | j |jdƒ|d   S )Né   r
   )r   r	   Úpow)r   Útotalr   r   r   Úvariance3   s    
zBeta.variancec             C   s   | j j|ƒjddƒS )Nr
   r   r   )r   ÚrsampleÚselect)r   Zsample_shaper   r   r   r"   9   s    zBeta.rsamplec             C   s0   | j r| j|ƒ tj|d| gdƒ}| jj|ƒS )Ng      ð?r
   r   )r   Z_validate_sampler   r   r   Úlog_prob)r   ÚvalueZheads_tailsr   r   r   r$   <   s    
zBeta.log_probc             C   s
   | j jƒ S )N)r   Úentropy)r   r   r   r   r&   B   s    zBeta.entropyc             C   s*   | j jd }t|tƒr"tj|gƒS |S d S )N.r   ).r   )r   Úconcentrationr   r   r   r   )r   Úresultr   r   r   r   E   s    
zBeta.concentration1c             C   s*   | j jd }t|tƒr"tj|gƒS |S d S )N.r
   ).r
   )r   r'   r   r   r   r   )r   r(   r   r   r   r	   M   s    
zBeta.concentration0c             C   s   | j | jfS )N)r   r	   )r   r   r   r   Ú_natural_paramsU   s    zBeta._natural_paramsc             C   s"   t j|ƒt j|ƒ t j|| ƒ S )N)r   Úlgamma)r   ÚxÚyr   r   r   Ú_log_normalizerY   s    zBeta._log_normalizer)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ZpositiveZarg_constraintsZunit_intervalZsupportZhas_rsampler   r   Úpropertyr   r!   r"   r$   r&   r   r	   r)   r-   Ú__classcell__r   r   )r   r   r   
   s   	r   )Znumbersr   r   Ztorch.distributionsr   Ztorch.distributions.dirichletr   Ztorch.distributions.exp_familyr   Ztorch.distributions.utilsr   r   r   r   r   r   Ú<module>   s   