a
    MSicR                     @   sd   d dl Z d dl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
mZ G dd deZdS )	    N)Real)Number)constraints)ExponentialFamily)_standard_normalbroadcast_allc                       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edd Zd" fdd	Zd# fdd	Ze fddZe fddZdd Zdd Zdd Zdd Zedd Zd d! Z  ZS )$Normala  
    Creates a normal (also called Gaussian) distribution parameterized by
    :attr:`loc` and :attr:`scale`.

    Example::

        >>> m = Normal(torch.tensor([0.0]), torch.tensor([1.0]))
        >>> m.sample()  # normally distributed with loc=0 and scale=1
        tensor([ 0.1046])

    Args:
        loc (float or Tensor): mean of the distribution (often referred to as mu)
        scale (float or Tensor): standard deviation of the distribution
            (often referred to as sigma)
    )locscaleTr   c                 C   s   | j S Nr	   self r   V/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/distributions/normal.pymean    s    zNormal.meanc                 C   s   | j S r   r   r   r   r   r   mode$   s    zNormal.modec                 C   s   | j S r   )r
   r   r   r   r   stddev(   s    zNormal.stddevc                 C   s   | j dS N   )r   powr   r   r   r   variance,   s    zNormal.varianceNc                    sR   t ||\| _| _t|tr0t|tr0t }n
| j }tt	| j
||d d S )Nvalidate_args)r   r	   r
   
isinstancer   torchSizesizesuperr   __init__)r   r	   r
   r   batch_shape	__class__r   r   r   0   s
    

zNormal.__init__c                    sR   |  t|}t|}| j||_| j||_tt|j|dd | j	|_	|S )NFr   )
_get_checked_instancer   r   r   r	   expandr
   r   r   _validate_args)r   r    	_instancenewr!   r   r   r$   8   s    
zNormal.expandc                 C   sR   |  |}t , t| j|| j|W  d    S 1 sD0    Y  d S r   )_extended_shaper   no_gradnormalr	   r$   r
   )r   sample_shapeshaper   r   r   sampleA   s    

zNormal.samplec                 C   s0   |  |}t|| jj| jjd}| j|| j  S )N)dtypedevice)r(   r   r	   r.   r/   r
   )r   r+   r,   epsr   r   r   rsampleF   s    
zNormal.rsamplec                 C   sn   | j r| | | jd }t| jtr2t| jn| j }|| j d  d|  | ttdtj	  S r   )
r%   _validate_sampler
   r   r   mathlogr	   sqrtpi)r   valuevarZ	log_scaler   r   r   log_probK   s
    

"zNormal.log_probc                 C   s<   | j r| | ddt|| j | j  td   S )N      ?   r   )	r%   r2   r   erfr	   r
   
reciprocalr3   r5   r   r7   r   r   r   cdfS   s    
z
Normal.cdfc                 C   s(   | j | jtd| d  td  S )Nr   r;   )r	   r
   r   erfinvr3   r5   r>   r   r   r   icdfX   s    zNormal.icdfc                 C   s$   ddt dt j   t| j S )Nr:   r   )r3   r4   r6   r   r
   r   r   r   r   entropy[   s    zNormal.entropyc                 C   s&   | j | jd d| jd  fS )Nr   g      )r	   r
   r   r=   r   r   r   r   _natural_params^   s    zNormal._natural_paramsc                 C   s(   d| d | dttj |   S )Ng      пr   r:   )r   r   r4   r3   r6   )r   xyr   r   r   _log_normalizerb   s    zNormal._log_normalizer)N)N)__name__
__module____qualname____doc__r   realpositivearg_constraintssupporthas_rsample_mean_carrier_measurepropertyr   r   r   r   r   r$   r   r   r-   r1   r9   r?   rA   rB   rC   rF   __classcell__r   r   r!   r   r      s0   



	
r   )r3   numbersr   r   r   torch.distributionsr   torch.distributions.exp_familyr   torch.distributions.utilsr   r   r   r   r   r   r   <module>   s   