
    AVh%                     N   d 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 ddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddgZ edg       G d dej,                               Z G d de      Z ej2                  ee      dd       Zy)z)The Normal (Gaussian) distribution class.    N)constant_op)dtypes)ops)tensor_shape)	array_ops)	check_ops)math_ops)nn)
random_ops)distribution)kullback_leibler)special_math)deprecation)	tf_exportNormalNormalWithSoftplusScalezdistributions.Normal)v1c                        e Zd ZdZ ej
                  ddd      	 	 	 d fd	       Zed        Ze	d        Z
e	d	        Zd
 Zd Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )r   aK  The Normal distribution with location `loc` and `scale` parameters.

  #### Mathematical details

  The probability density function (pdf) is,

  ```none
  pdf(x; mu, sigma) = exp(-0.5 (x - mu)**2 / sigma**2) / Z
  Z = (2 pi sigma**2)**0.5
  ```

  where `loc = mu` is the mean, `scale = sigma` is the std. deviation, and, `Z`
  is the normalization constant.

  The Normal distribution is a member of the [location-scale family](
  https://en.wikipedia.org/wiki/Location-scale_family), i.e., it can be
  constructed as,

  ```none
  X ~ Normal(loc=0, scale=1)
  Y = loc + scale * X
  ```

  #### Examples

  Examples of initialization of one or a batch of distributions.

  ```python
  import tensorflow_probability as tfp
  tfd = tfp.distributions

  # Define a single scalar Normal distribution.
  dist = tfd.Normal(loc=0., scale=3.)

  # Evaluate the cdf at 1, returning a scalar.
  dist.cdf(1.)

  # Define a batch of two scalar valued Normals.
  # The first has mean 1 and standard deviation 11, the second 2 and 22.
  dist = tfd.Normal(loc=[1, 2.], scale=[11, 22.])

  # Evaluate the pdf of the first distribution on 0, and the second on 1.5,
  # returning a length two tensor.
  dist.prob([0, 1.5])

  # Get 3 samples, returning a 3 x 2 tensor.
  dist.sample([3])
  ```

  Arguments are broadcast when possible.

  ```python
  # Define a batch of two scalar valued Normals.
  # Both have mean 1, but different standard deviations.
  dist = tfd.Normal(loc=1., scale=[11, 22.])

  # Evaluate the pdf of both distributions on the same point, 3.0,
  # returning a length 2 tensor.
  dist.prob(3.0)
  ```

  
2019-01-01zThe TensorFlow Distributions library has moved to TensorFlow Probability (https://github.com/tensorflow/probability). You should update all references to use `tfp.distributions` instead of `tf.distributions`.T	warn_oncec           	      v   t        t                     }t        j                  |||g      5 }t        j                  |rt        j                  |      gng       5  t        j                  |d      | _	        t        j                  |d      | _
        t        j                  | j                  | j                  g       ddd       ddd       t        t        | ;  | j                  j                  t         j"                  |||| j                  | j                  g|       y# 1 sw Y   dxY w# 1 sw Y   hxY w)aD  Construct Normal distributions with mean and stddev `loc` and `scale`.

    The parameters `loc` and `scale` must be shaped in a way that supports
    broadcasting (e.g. `loc + scale` is a valid operation).

    Args:
      loc: Floating point tensor; the means of the distribution(s).
      scale: Floating point tensor; the stddevs of the distribution(s).
        Must contain only positive values.
      validate_args: Python `bool`, default `False`. When `True` distribution
        parameters are checked for validity despite possibly degrading runtime
        performance. When `False` invalid inputs may silently render incorrect
        outputs.
      allow_nan_stats: Python `bool`, default `True`. When `True`,
        statistics (e.g., mean, mode, variance) use the value "`NaN`" to
        indicate the result is undefined. When `False`, an exception is raised
        if one or more of the statistic's batch members are undefined.
      name: Python `str` name prefixed to Ops created by this class.

    Raises:
      TypeError: if `loc` and `scale` have different `dtype`.
    valueslocnamescaleN)dtypereparameterization_typevalidate_argsallow_nan_stats
parametersgraph_parentsr   )dictlocalsr   
name_scopecontrol_dependenciesr   assert_positiver   identity_loc_scaleassert_same_float_dtypesuperr   __init__r   r   FULLY_REPARAMETERIZEDselfr   r   r!   r"   r   r#   	__class__s          Z/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/distributions/normal.pyr/   zNormal.__init__j   s	   H fhJ	c5\	2 Dd##$1 &/%>%>u%E$F79; D&&s7	((W=))499dkk*BC	DD 
&$ kk , B B#'yy$++. ! D DD Ds$   .D/A$D# D/#D,	(D//D8c           	      z    t        t        dt        j                  | t        j
                        gdz              S )N)r   r   r      )r%   zipr   convert_to_tensorr   int32)sample_shapes    r4   _param_shapeszNormal._param_shapes   s=     5 5!.  /12 3 	56 6    c                     | j                   S )z$Distribution parameter for the mean.)r+   r2   s    r4   r   z
Normal.loc   s     99r=   c                     | j                   S )z.Distribution parameter for standard deviation.)r,   r?   s    r4   r   zNormal.scale   s     ;;r=   c                     t        j                  t        j                  | j                        t        j                  | j                              S N)r   broadcast_dynamic_shapeshaper   r   r?   s    r4   _batch_shape_tensorzNormal._batch_shape_tensor   s5    ,,!

#% %r=   c                     t        j                  | j                  j                         | j                  j                               S rB   )r   broadcast_static_shaper   	get_shaper   r?   s    r4   _batch_shapezNormal._batch_shape   s5    ++

   r=   c                 L    t        j                  g t        j                        S )Nr6   )r   constantr   r:   r?   s    r4   _event_shape_tensorzNormal._event_shape_tensor   s    &,,77r=   c                 ,    t        j                  g       S rB   )r   TensorShaper?   s    r4   _event_shapezNormal._event_shape   s    ##B''r=   c                     t        j                  |g| j                         gd      }t        j                  |dd| j
                  j                  |      }|| j                  z  | j
                  z   S )Nr   g        g      ?)rD   meanstddevr   seed)r   concatbatch_shape_tensorr   random_normalr   r   r   )r2   nrS   rD   sampleds        r4   	_sample_nzNormal._sample_n   s`    qc4#:#:#<=qAE&&"Rtxx~~DJGTZZ$((**r=   c                 F    | j                  |      | j                         z
  S rB   )_log_unnormalized_prob_log_normalizationr2   xs     r4   	_log_probzNormal._log_prob   s!    &&q)D,C,C,EEEr=   c                 J    t        j                  | j                  |            S rB   r   log_ndtr_zr]   s     r4   _log_cdfzNormal._log_cdf   s      ,,r=   c                 J    t        j                  | j                  |            S rB   r   ndtrrc   r]   s     r4   _cdfzNormal._cdf   s    TWWQZ((r=   c                 L    t        j                  | j                  |             S rB   ra   r]   s     r4   _log_survival_functionzNormal._log_survival_function   s      $''!*--r=   c                 L    t        j                  | j                  |             S rB   rf   r]   s     r4   _survival_functionzNormal._survival_function   s    dggaj[))r=   c                 P    dt        j                  | j                  |            z  S )Ng      )r	   squarerc   r]   s     r4   r[   zNormal._log_unnormalized_prob   s    (//$''!*---r=   c                     dt        j                  dt         j                  z        z  t        j                  | j                        z   S N      ?g       @)mathlogpir	   r   r?   s    r4   r\   zNormal._log_normalization   s0    "tww,''(,,tzz*BBBr=   c                     | j                   t        j                  | j                        z  }dt	        j
                  dt        j                  z  t        j                  z        z  t        j
                  |      z   S rp   )	r   r   	ones_liker   rr   rs   rt   er	   )r2   r   s     r4   _entropyzNormal._entropy   sR    JJ,,TXX66E"tww,/008<<3FFFr=   c                 Z    | j                   t        j                  | j                        z  S rB   )r   r   rv   r   r?   s    r4   _meanzNormal._mean   s     88i))$**555r=   c                 J    | j                  t        j                  |            S rB   )_inv_zr   ndtri)r2   ps     r4   	_quantilezNormal._quantile   s    ;;|))!,--r=   c                 Z    | j                   t        j                  | j                        z  S rB   )r   r   rv   r   r?   s    r4   _stddevzNormal._stddev   s     ::	++DHH555r=   c                 "    | j                         S rB   )rz   r?   s    r4   _modezNormal._mode   s    ::<r=   c                     t        j                  d|g      5  || j                  z
  | j                  z  cddd       S # 1 sw Y   yxY w)z'Standardize input `x` to a unit normal.standardizer   N)r   r'   r   r   r]   s     r4   rc   z	Normal._z   s;    	qc	2 )$((ldjj() ) )	   ?Ac                     t        j                  d|g      5  || j                  z  | j                  z   cddd       S # 1 sw Y   yxY w)z4Reconstruct input `x` from a its normalized version.reconstructr   N)r   r'   r   r   )r2   zs     r4   r|   zNormal._inv_z   s;    	qc	2 '^dhh&' ' 'r   )FTr   rB   ) __name__
__module____qualname____doc__r   
deprecatedr/   staticmethodr<   propertyr   r   rE   rI   rL   rO   rY   r_   rd   rh   rj   rl   r[   r\   rx   rz   r   r   r   rc   r|   __classcell__r3   s   @r4   r   r   )   s    =~ ;'
  ##**X 6 6
    %
 
8(+F-).*.CG
6.6)
'r=   c                   Z     e Zd ZdZ ej
                  ddd      	 	 	 d fd	       Z xZS )r   z(Normal with softplus applied to `scale`.r   z4Use `tfd.Normal(loc, tf.nn.softplus(scale)) instead.Tr   c                     t        t                     }t        j                  ||g      5 }t        t
        |   |t        j                  |d      |||       d d d        || _	        y # 1 sw Y   || _	        y xY w)Nr   softplus_scaler   )r   r   r!   r"   r   )
r%   r&   r   r'   r.   r   r/   r
   softplus_parametersr1   s          r4   r/   z NormalWithSoftplusScale.__init__   sv     fhJ	eW	- #T3E(89%) 4  "D "Ds   .A++A;)FTr   )r   r   r   r   r   r   r/   r   r   s   @r4   r   r      s;    0;	 ##-"
"r=   c                 r   t        j                  |d| j                  |j                  g      5  t        j                  d| j
                        }t        j                  d| j
                        }t        j                  d| j
                        }t        j                  | j                        }t        j                  |j                        }||z  }t        j                  | j                  |j                        ||z  z  |||z
  t        j                  |      z
  z  z   cddd       S # 1 sw Y   yxY w)aD  Calculate the batched KL divergence KL(n_a || n_b) with n_a and n_b Normal.

  Args:
    n_a: instance of a Normal distribution object.
    n_b: instance of a Normal distribution object.
    name: (optional) Name to use for created operations.
      default is "kl_normal_normal".

  Returns:
    Batchwise KL(n_a || n_b)
  kl_normal_normal   r6   r7   rq   N)r   r'   r   r   rK   r   r	   rn   r   squared_differencers   )	n_an_br   onetwohalfs_a_squareds_b_squaredratios	            r4   _kl_normal_normalr     s     ~~d.#''0BC :


q		
2C


q		
2C3995D//#)),K//#)),K+%E''9S;=NOeckHLL$7789: : :s   C5D--D6rB   )r   rr   tensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr   r   r	   r
   r   #tensorflow.python.ops.distributionsr   r   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   __all__Distributionr   r   
RegisterKLr    r=   r4   <module>r      s    0  3 . + 4 + + * $ , < @ < . 6  %&'H'\&& H' (H'V"f "2 VV,: -:r=   