3
O4W^ã
  ã               @   sd   d dl Z d dlmZmZ d dlmZ d dlZd dlmZ d dl	m
Z
 d dlmZ G dd„ de
ƒZdS )	é    N)ÚinfÚnan)ÚNumber)Úconstraints)ÚDistribution)Ú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ejƒ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚCauchya	  
    Samples from a Cauchy (Lorentz) distribution. The distribution of the ratio of
    independent normally distributed random variables with means `0` follows a
    Cauchy distribution.

    Example::

        >>> m = Cauchy(torch.tensor([0.0]), torch.tensor([1.0]))
        >>> m.sample()  # sample from a Cauchy distribution with loc=0 and scale=1
        tensor([ 2.3214])

    Args:
        loc (float or Tensor): mode or median of the distribution.
        scale (float or Tensor): half width at half maximum.
    )ÚlocÚscaleTNc                sR   t ||ƒ\| _| _t|tƒr0t|tƒr0tjƒ }n
| jjƒ }tt	| ƒj
||d� d S )N)Úvalidate_args)r   r	   r
   Ú
isinstancer   ÚtorchÚSizeÚsizeÚsuperr   Ú__init__)Úselfr	   r
   r   Úbatch_shape)Ú	__class__© ú;/tmp/pip-build-e66j97b2/torch/torch/distributions/cauchy.pyr      s
    

zCauchy.__init__c                sR   | j t|ƒ}tj|ƒ}| jj|ƒ|_| jj|ƒ|_tt|ƒj|dd� | j	|_	|S )NF)r   )
Z_get_checked_instancer   r   r   r	   Úexpandr
   r   r   Ú_validate_args)r   r   Z	_instanceÚnew)r   r   r   r   '   s    
zCauchy.expandc             C   s   t j| jƒ t| jj| jjd�S )N)ÚdtypeÚdevice)r   ÚfullÚ_extended_shaper   r	   r   r   )r   r   r   r   Úmean0   s    zCauchy.meanc             C   s   t j| jƒ t| jj| jjd�S )N)r   r   )r   r   r   r   r	   r   r   )r   r   r   r   Úvariance4   s    zCauchy.variancec             C   s*   | j |ƒ}| jj|ƒjƒ }| j|| j  S )N)r   r	   r   Zcauchy_r
   )r   Zsample_shapeÚshapeZepsr   r   r   Úrsample8   s    
zCauchy.rsamplec             C   sD   | j r| j|ƒ tjtjƒ | jjƒ  d|| j | j d  jƒ  S )Né   é   )r   Ú_validate_sampleÚmathÚlogÚpir
   r	   )r   Úvaluer   r   r   Úlog_prob=   s    
zCauchy.log_probc             C   s0   | j r| j|ƒ tj|| j | j ƒtj d S )Ng      à?)r   r$   r   Úatanr	   r
   r%   r'   )r   r(   r   r   r   ÚcdfB   s    
z
Cauchy.cdfc             C   s0   | j r| j|ƒ tjtj|d  ƒ| j | j S )Ng      à?)r   r$   r   Útanr%   r'   r
   r	   )r   r(   r   r   r   ÚicdfG   s    
zCauchy.icdfc             C   s   t jdt j ƒ| jjƒ  S )Né   )r%   r&   r'   r
   )r   r   r   r   ÚentropyL   s    zCauchy.entropy)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚrealZpositiveZarg_constraintsZsupportZhas_rsampler   r   Úpropertyr   r   r   r   r!   r)   r+   r-   r/   Ú__classcell__r   r   )r   r   r      s   	r   )r%   Z
torch._sixr   r   Znumbersr   r   Ztorch.distributionsr   Z torch.distributions.distributionr   Ztorch.distributions.utilsr   r   r   r   r   r   Ú<module>   s   