
    AVh                        d Z ddlZddlZddl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 g dZg dZ G d dej4                        Zd Zd Zd$dZ G d dej4                        Z edg       G d d             Z  e d      Z! edg      jE                  e#d        e d      Z$ ed g      jE                  e#d        ed!g       G d" d#ee             Z%y)%z+Base classes for probability distributions.    N)context)dtypes)ops)tensor_shape)tensor_util)	array_ops)math_ops)kullback_leibler)util)deprecation)
tf_inspect)	tf_export)ReparameterizationTypeFULLY_REPARAMETERIZEDNOT_REPARAMETERIZEDDistribution)batch_shapebatch_shape_tensorcdf
covariancecross_entropyentropyevent_shapeevent_shape_tensorkl_divergencelog_cdflog_problog_survival_functionmeanmodeprobsamplestddevsurvival_functionvariancec                       e Zd ZdZy)_BaseDistributionz<Abstract base class needed for resolving subclass hierarchy.N)__name__
__module____qualname____doc__     `/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/distributions/distribution.pyr'   r'   C   s    Dr-   r'   )	metaclassc                     t        |       st        d| z        t        j                  | j                  | j
                  | j                  | j                  | j                        S )zCreate a deep copy of fn.

  Args:
    fn: a callable

  Returns:
    A `FunctionType`: a deep copy of fn.

  Raises:
    TypeError: if `fn` is not a callable.
  zfn is not callable: %s)codeglobalsnameargdefsclosure)	callable	TypeErrortypesFunctionType__code____globals__r(   __defaults____closure__)fns    r.   _copy_fnr?   H   sR     
"
,r1
22 
		;;;;nn
 r-   c                    | xs d} | j                  d      }dj                  d |j                  d      D              }t        |      D cg c](  \  }}|j                         j	                         dk(  r|* }}}|r7|d   }dj                  |d|       dz   |z   dz   dj                  ||d       z   S | dz   |z   S c c}}w )zGUpdate old_str by inserting append_str just before the "Args:" section. 
c              3   &   K   | ]	  }d |z    yw)z    %sNr,   ).0lines     r.   	<genexpr>z$_update_docstring.<locals>.<genexpr>o   s     LTDLs   zargs:Nz

)splitjoin	enumeratestriplower)old_str
append_strold_str_linesixrE   has_args_ixfinal_args_ixs          r.   _update_docstringrS   h   s    Mr'--%- yyLZ5E5Ed5KLL* #=1*R					( 	*+ * OMIImN]34!"$*+iimn567 8 Vj((*s   -B>c                     t        j                         rI|G|j                  s|j                  r/t	        j
                  | |      }|j                  j                  r|S t	        j
                  | ||      S )zDConverts to tensor avoiding an eager bug that loses float precision.r3   r3   preferred_dtype)r   executing_eagerly
is_integeris_boolr   convert_to_tensordtypeis_floating)valuer3   rW   vs       r.   _convert_to_tensorr`   ~   sd     !o&A!!_%<%<e$/Awwh			$
9 9r-   c                       e Zd Zd Zy)_DistributionMetac                 6   |st        d      |D cg c]  }|t        k(  st        |t              r| }}|d   }|t        k(  r"t        j
                  j                  | |||      S t        |t              st        d|d|j                  d      t        D ]  }d|z  }|j                  |d      }||v rt        ||d      }	|	st        d|j                  d	|d      |j                  |d      }
|
_t        j                  |
      }|swt        |	      }t        j                  |	      }|t        d
|j                  d|      t!        |j"                  d|d|      |_        |||<    t        j
                  j                  | |||      S c c}w )aq  Control the creation of subclasses of the Distribution class.

    The main purpose of this method is to properly propagate docstrings
    from private Distribution methods, like `_log_prob`, into their
    public wrappers as inherited by the Distribution base class
    (e.g. `log_prob`).

    Args:
      classname: The name of the subclass being created.
      baseclasses: A tuple of parent classes.
      attrs: A dict mapping new attributes to their values.

    Returns:
      The class object.

    Raises:
      TypeError: If `Distribution` is not a subclass of `BaseDistribution`, or
        the new class is derived via multiple inheritance and the first
        parent class is not a subclass of `BaseDistribution`.
      AttributeError:  If `Distribution` does not implement e.g. `log_prob`.
      ValueError:  If a `Distribution` public method lacks a docstring.
    zOExpected non-empty baseclass. Does Distribution not subclass _BaseDistribution?r   z First parent class declared for z  must be Distribution, but saw ''z_%sNz%Internal error: expected base class 'z' to implement method 'z/Expected base class fn to contain a docstring: .zAdditional documentation from `z`:

)r7   r'   
issubclassr   abcABCMeta__new__r(   $_DISTRIBUTION_PUBLIC_METHOD_WRAPPERSgetgetattrAttributeErrorr   getdocr?   
ValueErrorrS   r+   )mcs	classnamebaseclassesattrsbase
which_baseattrspecial_attrclass_attr_valuebase_attr_valueclass_special_attr_valueclass_special_attr_docstringclass_attr_docstrings                r.   ri   z_DistributionMeta.__new__   s   .  8 9 9 %H$$
4(F 	HJ H a=D  [[  ieDDdL)6?P Q Q4 %T\l4.	dD1o}}d$% 	% "'<!>	!	)%/%6%67O%P")!/2'..?		%}}d$% 	% "3

"
"57"9 %eDk;%> ;;sI{EBBQHs   "FN)r(   r)   r*   ri   r,   r-   r.   rb   rb      s	    BCr-   rb   z$distributions.ReparameterizationType)v1c                   R    e Zd ZdZ ej
                  ddd      d        Zd Zd Zy	)
r   a  Instances of this class represent how sampling is reparameterized.

  Two static instances exist in the distributions library, signifying
  one of two possible properties for samples from a distribution:

  `FULLY_REPARAMETERIZED`: Samples from the distribution are fully
    reparameterized, and straight-through gradients are supported.

  `NOT_REPARAMETERIZED`: Samples from the distribution are not fully
    reparameterized, and straight-through gradients are either partially
    unsupported or are not supported at all. In this case, for purposes of
    e.g. RL or variational inference, it is generally safest to wrap the
    sample results in a `stop_gradients` call and use policy
    gradients / surrogate loss instead.
  
2019-01-01The 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                     || _         y N	_rep_type)selfrep_types     r.   __init__zReparameterizationType.__init__   s     DNr-   c                      d| j                   z  S )Nz<Reparameterization Type: %s>r   r   s    r.   __repr__zReparameterizationType.__repr__   s    *T^^;;r-   c                 
    | |u S )a+  Determine if this `ReparameterizationType` is equal to another.

    Since ReparameterizationType instances are constant static global
    instances, equality checks if two instances' id() values are equal.

    Args:
      other: Object to compare against.

    Returns:
      `self is other`.
    r,   r   others     r.   __eq__zReparameterizationType.__eq__   s     5=r-   N)	r(   r)   r*   r+   r   
deprecatedr   r   r   r,   r-   r.   r   r      s?      ;'
 <r-   r   r   z#distributions.FULLY_REPARAMETERIZEDr   z!distributions.NOT_REPARAMETERIZEDzdistributions.Distributionc                      e Zd ZdZ ej
                  ddd      	 	 	 dLd       Zed        Zej                  d	        Ze
dMd
       Ze
d        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zd Zd ZdNdZd Zed        Zd ZdOdZd Zed        ZdPdZdQdZdRdZ d Z!dSd Z"d! Z#d" Z$dTd#Z%d$ Z&d% Z'dUd&Z(d' Z)d( Z*dVd)Z+d* Z,d+ Z-dWd,Z.d- Z/d. Z0dXd/Z1d0 Z2d1 Z3dYd2Z4d3 Z5dZd4Z6d5 Z7d[d6Z8d7 Z9d8 Z:d\d9Z;d: Z<d]d;Z=d< Z>d^d=Z?d> Z@d_d?ZAd@ ZBd`dAZCdB ZDdadCZEdD ZFdbdEZGdF ZHdG ZIeJj                  dcdH       ZLdI ZMdJ ZNdK ZOy)dr   a  A generic probability distribution base class.

  `Distribution` is a base class for constructing and organizing properties
  (e.g., mean, variance) of random variables (e.g, Bernoulli, Gaussian).

  #### Subclassing

  Subclasses are expected to implement a leading-underscore version of the
  same-named function. The argument signature should be identical except for
  the omission of `name="..."`. For example, to enable `log_prob(value,
  name="log_prob")` a subclass should implement `_log_prob(value)`.

  Subclasses can append to public-level docstrings by providing
  docstrings for their method specializations. For example:

  ```python
  @util.AppendDocstring("Some other details.")
  def _log_prob(self, value):
    ...
  ```

  would add the string "Some other details." to the `log_prob` function
  docstring. This is implemented as a simple decorator to avoid python
  linter complaining about missing Args/Returns/Raises sections in the
  partial docstrings.

  #### Broadcasting, batching, and shapes

  All distributions support batches of independent distributions of that type.
  The batch shape is determined by broadcasting together the parameters.

  The shape of arguments to `__init__`, `cdf`, `log_cdf`, `prob`, and
  `log_prob` reflect this broadcasting, as does the return value of `sample` and
  `sample_n`.

  `sample_n_shape = [n] + batch_shape + event_shape`, where `sample_n_shape` is
  the shape of the `Tensor` returned from `sample_n`, `n` is the number of
  samples, `batch_shape` defines how many independent distributions there are,
  and `event_shape` defines the shape of samples from each of those independent
  distributions. Samples are independent along the `batch_shape` dimensions, but
  not necessarily so along the `event_shape` dimensions (depending on the
  particulars of the underlying distribution).

  Using the `Uniform` distribution as an example:

  ```python
  minval = 3.0
  maxval = [[4.0, 6.0],
            [10.0, 12.0]]

  # Broadcasting:
  # This instance represents 4 Uniform distributions. Each has a lower bound at
  # 3.0 as the `minval` parameter was broadcasted to match `maxval`'s shape.
  u = Uniform(minval, maxval)

  # `event_shape` is `TensorShape([])`.
  event_shape = u.event_shape
  # `event_shape_t` is a `Tensor` which will evaluate to [].
  event_shape_t = u.event_shape_tensor()

  # Sampling returns a sample per distribution. `samples` has shape
  # [5, 2, 2], which is [n] + batch_shape + event_shape, where n=5,
  # batch_shape=[2, 2], and event_shape=[].
  samples = u.sample_n(5)

  # The broadcasting holds across methods. Here we use `cdf` as an example. The
  # same holds for `log_cdf` and the likelihood functions.

  # `cum_prob` has shape [2, 2] as the `value` argument was broadcasted to the
  # shape of the `Uniform` instance.
  cum_prob_broadcast = u.cdf(4.0)

  # `cum_prob`'s shape is [2, 2], one per distribution. No broadcasting
  # occurred.
  cum_prob_per_dist = u.cdf([[4.0, 5.0],
                             [6.0, 7.0]])

  # INVALID as the `value` argument is not broadcastable to the distribution's
  # shape.
  cum_prob_invalid = u.cdf([4.0, 5.0, 6.0])
  ```

  #### Shapes

  There are three important concepts associated with TensorFlow Distributions
  shapes:
  - Event shape describes the shape of a single draw from the distribution;
    it may be dependent across dimensions. For scalar distributions, the event
    shape is `[]`. For a 5-dimensional MultivariateNormal, the event shape is
    `[5]`.
  - Batch shape describes independent, not identically distributed draws, aka a
    "collection" or "bunch" of distributions.
  - Sample shape describes independent, identically distributed draws of batches
    from the distribution family.

  The event shape and the batch shape are properties of a Distribution object,
  whereas the sample shape is associated with a specific call to `sample` or
  `log_prob`.

  For detailed usage examples of TensorFlow Distributions shapes, see
  [this tutorial](
  https://github.com/tensorflow/probability/blob/master/tensorflow_probability/examples/jupyter_notebooks/Understanding_TensorFlow_Distributions_Shapes.ipynb)

  #### Parameter values leading to undefined statistics or distributions.

  Some distributions do not have well-defined statistics for all initialization
  parameter values. For example, the beta distribution is parameterized by
  positive real numbers `concentration1` and `concentration0`, and does not have
  well-defined mode if `concentration1 < 1` or `concentration0 < 1`.

  The user is given the option of raising an exception or returning `NaN`.

  ```python
  a = tf.exp(tf.matmul(logits, weights_a))
  b = tf.exp(tf.matmul(logits, weights_b))

  # Will raise exception if ANY batch member has a < 1 or b < 1.
  dist = distributions.beta(a, b, allow_nan_stats=False)
  mode = dist.mode().eval()

  # Will return NaN for batch members with either a < 1 or b < 1.
  dist = distributions.beta(a, b, allow_nan_stats=True)  # Default behavior
  mode = dist.mode().eval()
  ```

  In all cases, an exception is raised if *invalid* parameters are passed, e.g.

  ```python
  # Will raise an exception if any Op is run.
  negative_a = -1.0 * a  # beta distribution by definition has a > 0.
  dist = distributions.beta(negative_a, b, allow_nan_stats=True)
  dist.mean().eval()
  ```

  r   r   Tr   Nc                    |g n|}t        |      D ],  \  }}	|	t        j                  |	      rt        d||	fz         |r|d   dk7  r8|xs t	        |       j
                  }
t        j                  |
      5 }	 ddd       || _        || _	        || _
        || _        |xs i | _        || _        || _        y# 1 sw Y   ?xY w)a  Constructs the `Distribution`.

    **This is a private method for subclass use.**

    Args:
      dtype: The type of the event samples. `None` implies no type-enforcement.
      reparameterization_type: Instance of `ReparameterizationType`.
        If `distributions.FULLY_REPARAMETERIZED`, this
        `Distribution` can be reparameterized in terms of some standard
        distribution with a function whose Jacobian is constant for the support
        of the standard distribution. If `distributions.NOT_REPARAMETERIZED`,
        then no such reparameterization is available.
      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.
      parameters: Python `dict` of parameters used to instantiate this
        `Distribution`.
      graph_parents: Python `list` of graph prerequisites of this
        `Distribution`.
      name: Python `str` name prefixed to Ops created by this class. Default:
        subclass name.

    Raises:
      ValueError: if any member of graph_parents is `None` or not a `Tensor`.
    Nz)Graph parent item %d is not a Tensor; %s.rG   /)rJ   r   
is_tf_typero   typer(   r   
name_scope_dtype_reparameterization_type_allow_nan_stats_validate_args_parameters_graph_parents_name)r   r\   reparameterization_typevalidate_argsallow_nan_stats
parametersgraph_parentsr3   itnon_unique_names              r.   r   zDistribution.__init__  s    \ (/B]M-( O1	
+003D1vMNNO 48s?3T
 3 3o>>/* dDK$;D!+D'D!'RD'DDJ s   9B99Cc                     | j                   S r   _parameter_dictr   s    r.   r   zDistribution._parameters  s    r-   c                      d|v r|d= || _         y)a1  Intercept assignments to self._parameters to avoid reference cycles.

    Parameters are often created using locals(), so we need to clean out any
    references to `self` before assigning it to an attribute.

    Args:
      value: A dictionary of parameters to assign to the `_parameters` property.
    r   Nr   r   r^   s     r.   r   zDistribution._parameters  s     
- Dr-   c                     t        j                  ||g      5  | j                  |      cddd       S # 1 sw Y   yxY w)a,  Shapes of parameters given the desired shape of a call to `sample()`.

    This is a class method that describes what key/value arguments are required
    to instantiate the given `Distribution` so that a particular shape is
    returned for that instance's call to `sample()`.

    Subclasses should override class method `_param_shapes`.

    Args:
      sample_shape: `Tensor` or python list/tuple. Desired shape of a call to
        `sample()`.
      name: name to prepend ops with.

    Returns:
      `dict` of parameter name to `Tensor` shapes.
    valuesN)r   r   _param_shapes)clssample_shaper3   s      r.   param_shapeszDistribution.param_shapes  s7    $ 
l^	4 -|,- - -s   4=c                 Z   t        |t        j                        r+|j                         st	        d      |j                         }| j                  |      }i }|j                         D ]?  \  }}t        j                  |      }|t	        d      t        j                  |      ||<   A |S )a  param_shapes with static (i.e. `TensorShape`) shapes.

    This is a class method that describes what key/value arguments are required
    to instantiate the given `Distribution` so that a particular shape is
    returned for that instance's call to `sample()`. Assumes that the sample's
    shape is known statically.

    Subclasses should override class method `_param_shapes` to return
    constant-valued tensors when constant values are fed.

    Args:
      sample_shape: `TensorShape` or python list/tuple. Desired shape of a call
        to `sample()`.

    Returns:
      `dict` of parameter name to `TensorShape`.

    Raises:
      ValueError: if `sample_shape` is a `TensorShape` and is not fully defined.
    z.TensorShape sample_shape must be fully definedz>sample_shape must be a fully-defined TensorShape or list/tuple)

isinstancer   TensorShapeis_fully_definedro   as_listr   itemsr   constant_value)r   r   paramsstatic_paramsr3   shapestatic_shapes          r.   param_static_shapesz Distribution.param_static_shapes  s    , , 8 89**,IJJ!))+ll+FM||~ Ce //6l		LN 	N(44\BmDC r-   c                     t        d      )Nz_param_shapes not implemented)NotImplementedError)r   s    r.   r   zDistribution._param_shapes&  s    
=
>>r-   c                     | j                   S )z9Name prepended to all ops created by this `Distribution`.)r   r   s    r.   r3   zDistribution.name*  s     ::r-   c                     | j                   S )z8The `DType` of `Tensor`s handled by this `Distribution`.)r   r   s    r.   r\   zDistribution.dtype/  s     ;;r-   c                     | j                   j                         D ci c]  \  }}|j                  d      s|dk7  r||  c}}S c c}}w )zADictionary of parameters used to instantiate this `Distribution`.__r   )r   r   
startswith)r   kr_   s      r.   r   zDistribution.parameters4  sL     "--335 7TQ<<%!v+ qD 7 7 7s   #Ac                     | j                   S )a  Describes how samples from the distribution are reparameterized.

    Currently this is one of the static instances
    `distributions.FULLY_REPARAMETERIZED`
    or `distributions.NOT_REPARAMETERIZED`.

    Returns:
      An instance of `ReparameterizationType`.
    )r   r   s    r.   r   z$Distribution.reparameterization_type=  s     (((r-   c                     | j                   S )a  Python `bool` describing behavior when a stat is undefined.

    Stats return +/- infinity when it makes sense. E.g., the variance of a
    Cauchy distribution is infinity. However, sometimes the statistic is
    undefined, e.g., if a distribution's pdf does not achieve a maximum within
    the support of the distribution, the mode is undefined. If the mean is
    undefined, then by definition the variance is undefined. E.g. the mean for
    Student's T for df = 1 is undefined (no clear way to say it is either + or -
    infinity), so the variance = E[(X - mean)**2] is also undefined.

    Returns:
      allow_nan_stats: Python `bool`.
    )r   r   s    r.   r   zDistribution.allow_nan_statsJ  s        r-   c                     | j                   S )z?Python `bool` indicating possibly expensive checks are enabled.)r   r   s    r.   r   zDistribution.validate_args[  s     r-   c                 P    t        | j                  fi |} t        |       di |S )a  Creates a deep copy of the distribution.

    Note: the copy distribution may continue to depend on the original
    initialization arguments.

    Args:
      **override_parameters_kwargs: String/value dictionary of initialization
        arguments to override with new values.

    Returns:
      distribution: A new instance of `type(self)` initialized from the union
        of self.parameters and override_parameters_kwargs, i.e.,
        `dict(self.parameters, **override_parameters_kwargs)`.
    r,   )dictr   r   )r   override_parameters_kwargsr   s      r.   copyzDistribution.copy`  s,     dooD)CDJ4:#
##r-   c                 \    t        dj                  t        |       j                              )Nz)batch_shape_tensor is not implemented: {}r   formatr   r(   r   s    r.   _batch_shape_tensorz Distribution._batch_shape_tensorr  )    
3::4:;N;NOQ Qr-   c                 2   | j                  |      5  | j                  j                         rGt        j                  | j                  j                         t        j                  d      cddd       S | j                         cddd       S # 1 sw Y   yxY w)a  Shape of a single sample from a single event index as a 1-D `Tensor`.

    The batch dimensions are indexes into independent, non-identical
    parameterizations of this distribution.

    Args:
      name: name to give to the op

    Returns:
      batch_shape: `Tensor`.
    r   r\   r3   N)	_name_scoper   r   r   r[   r   r   int32r   r   r3   s     r.   r   zDistribution.batch_shape_tensorv  sz     
		$	 (				*	*	,$$T%5%5%=%=%?+1<<*79( (
 %%'( ( (   AB4BBc                 ,    t        j                  d       S r   r   r   r   s    r.   _batch_shapezDistribution._batch_shape      ##D))r-   c                 H    t        j                  | j                               S )a)  Shape of a single sample from a single event index as a `TensorShape`.

    May be partially defined or unknown.

    The batch dimensions are indexes into independent, non-identical
    parameterizations of this distribution.

    Returns:
      batch_shape: `TensorShape`, possibly unknown.
    )r   as_shaper   r   s    r.   r   zDistribution.batch_shape  s       !2!2!455r-   c                 \    t        dj                  t        |       j                              )Nz)event_shape_tensor is not implemented: {}r   r   s    r.   _event_shape_tensorz Distribution._event_shape_tensor  r   r-   c                 2   | j                  |      5  | j                  j                         rGt        j                  | j                  j                         t        j                  d      cddd       S | j                         cddd       S # 1 sw Y   yxY w)zShape of a single sample from a single batch as a 1-D int32 `Tensor`.

    Args:
      name: name to give to the op

    Returns:
      event_shape: `Tensor`.
    r   r   N)	r   r   r   r   r[   r   r   r   r   r   s     r.   r   zDistribution.event_shape_tensor  sz     
		$	 (				*	*	,$$T%5%5%=%=%?+1<<*79( (
 %%'( ( (r   c                 ,    t        j                  d       S r   r   r   s    r.   _event_shapezDistribution._event_shape  r   r-   c                 H    t        j                  | j                               S )zShape of a single sample from a single batch as a `TensorShape`.

    May be partially defined or unknown.

    Returns:
      event_shape: `TensorShape`, possibly unknown.
    )r   r   r   r   s    r.   r   zDistribution.event_shape  s       !2!2!455r-   c                     | j                  |      5  t        j                  | j                  | j                  | j
                        d      cddd       S # 1 sw Y   yxY w)zIndicates that `event_shape == []`.

    Args:
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      is_scalar_event: `bool` scalar `Tensor`.
    is_scalar_eventrU   N)r   r   r[   _is_scalar_helperr   r   r   s     r.   r   zDistribution.is_scalar_event  T     
		$	 """

 
 !1!143J3J
K "" " "   ;AA c                     | j                  |      5  t        j                  | j                  | j                  | j
                        d      cddd       S # 1 sw Y   yxY w)zIndicates that `batch_shape == []`.

    Args:
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      is_scalar_batch: `bool` scalar `Tensor`.
    is_scalar_batchrU   N)r   r   r[   r   r   r   r   s     r.   r   zDistribution.is_scalar_batch  r   r   c                 \    t        dj                  t        |       j                              )Nzsample_n is not implemented: {}r   )r   nseeds      r.   	_sample_nzDistribution._sample_n  *    
?FFT
  r-   c                    | j                  ||g      5  t        j                  |t        j                  d      }| j                  |d      \  }} | j                  ||fi |}t        j                  |      dd  }t        j                  ||gd      }t        j                  ||      }| j                  ||      }|cd d d        S # 1 sw Y   y xY w)Nr   r   r      r   )r   r   r[   r   r   _expand_sample_shape_to_vectorr   r   r   concatreshape_set_sample_static_shape)	r   r   r   r3   kwargsr   samplesbatch_event_shapefinal_shapes	            r.   _call_sample_nzDistribution._call_sample_n  s    			$~		6 
**
fllAl;;
(olAq$1&1g#//'2126$$l4E%FJk!!';7g--g|Dg
 
 
s   B)CCc                 (    | j                  |||      S )a~  Generate samples of the specified shape.

    Note that a call to `sample()` without arguments will generate a single
    sample.

    Args:
      sample_shape: 0D or 1D `int32` `Tensor`. Shape of the generated samples.
      seed: Python integer seed for RNG
      name: name to give to the op.

    Returns:
      samples: a `Tensor` with prepended dimensions `sample_shape`.
    )r   )r   r   r   r3   s       r.   r"   zDistribution.sample  s     |T488r-   c                 \    t        dj                  t        |       j                              )Nzlog_prob is not implemented: {}r   r   s     r.   	_log_probzDistribution._log_prob  r   r-   c           	      X   | j                  ||g      5  t        |d| j                        }	  | j                  |fi |cd d d        S # t        $ rI}	 t        j                   | j                  |fi |      cY d }~cd d d        S # t        $ r |w xY wd }~ww xY w# 1 sw Y   y xY wNr   r^   rV   )r   r`   r\   r   r   r	   log_probr   r^   r3   r   original_exceptions        r.   _call_log_probzDistribution._call_log_prob  s    			$w		/ 	# 
gtzz;e#t~~e.v.		# 	#
 ! #	#jdjj9&9:
:	# 	# # 	#"
"	##	# 	#@   B A	B%B	:B;B 	BBBB  B)c                 &    | j                  ||      S )a/  Log probability density/mass function.

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      log_prob: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r  r   r^   r3   s      r.   r   zDistribution.log_prob	  s     ud++r-   c                 \    t        dj                  t        |       j                              )Nzprob is not implemented: {}r   r   s     r.   r  zDistribution._prob  *    
;BBT
  r-   c           	      X   | j                  ||g      5  t        |d| j                        }	  | j                  |fi |cd d d        S # t        $ rI}	 t        j                   | j                  |fi |      cY d }~cd d d        S # t        $ r |w xY wd }~ww xY w# 1 sw Y   y xY wr   )r   r`   r\   r  r   r	   expr   r  s        r.   
_call_probzDistribution._call_prob  s    			$w		/ 	# 
gtzz;e#tzz%*6*		# 	#
 ! #	#ndnnU=f=>
>	# 	# # 	#"
"	##	# 	#r  c                 &    | j                  ||      S )a'  Probability density/mass function.

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      prob: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r  r  s      r.   r!   zDistribution.prob&  s     ??5$''r-   c                 \    t        dj                  t        |       j                              )Nzlog_cdf is not implemented: {}r   r   s     r.   _log_cdfzDistribution._log_cdf3  *    
>EET
  r-   c           	      X   | j                  ||g      5  t        |d| j                        }	  | j                  |fi |cd d d        S # t        $ rI}	 t        j                   | j                  |fi |      cY d }~cd d d        S # t        $ r |w xY wd }~ww xY w# 1 sw Y   y xY wr   )r   r`   r\   r  r   r	   r  _cdfr  s        r.   _call_log_cdfzDistribution._call_log_cdf7  s    			$w		/ 	# 
gtzz;e#t}}U-f-		# 	#
 ! #	#idii889
9	# 	# # 	#"
"	##	# 	#r  c                 &    | j                  ||      S )a_  Log cumulative distribution function.

    Given random variable `X`, the cumulative distribution function `cdf` is:

    ```none
    log_cdf(x) := Log[ P[X <= x] ]
    ```

    Often, a numerical approximation can be used for `log_cdf(x)` that yields
    a more accurate answer than simply taking the logarithm of the `cdf` when
    `x << -1`.

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      logcdf: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r  r  s      r.   r   zDistribution.log_cdfC  s    * eT**r-   c                 \    t        dj                  t        |       j                              )Nzcdf is not implemented: {}r   r   s     r.   r  zDistribution._cdfZ  s*    
:AAT
  r-   c           	      X   | j                  ||g      5  t        |d| j                        }	  | j                  |fi |cd d d        S # t        $ rI}	 t        j                   | j                  |fi |      cY d }~cd d d        S # t        $ r |w xY wd }~ww xY w# 1 sw Y   y xY wr   )r   r`   r\   r  r   r	   r  r  r  s        r.   	_call_cdfzDistribution._call_cdf^  s    			$w		/ 	# 
gtzz;e#tyy)&)		# 	#
 ! #	#mdmmE<V<=
=	# 	# # 	#"
"	##	# 	#r  c                 &    | j                  ||      S )a  Cumulative distribution function.

    Given random variable `X`, the cumulative distribution function `cdf` is:

    ```none
    cdf(x) := P[X <= x]
    ```

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      cdf: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r  r  s      r.   r   zDistribution.cdfj  s    " >>%&&r-   c                 \    t        dj                  t        |       j                              )Nz,log_survival_function is not implemented: {}r   r   s     r.   _log_survival_functionz#Distribution._log_survival_function}  s*    
6==J	!" "r-   c           	      Z   | j                  ||g      5  t        |d| j                        }	  | j                  |fi |cd d d        S # t        $ rJ}	 t        j                   | j                  |fi |       cY d }~cd d d        S # t        $ r |w xY wd }~ww xY w# 1 sw Y   y xY wr   )r   r`   r\   r  r   r	   log1pr   r  s        r.   _call_log_survival_functionz(Distribution._call_log_survival_function  s    			$w		/ 	# 
gtzz;e#*t**5;F;		# 	#
 ! #	#%!:6!: :;
;	# 	# # 	#"
"	##	# 	#s@   B!A	B&B
;B<B!
BBBB!!B*c                 &    | j                  ||      S )a  Log survival function.

    Given random variable `X`, the survival function is defined:

    ```none
    log_survival_function(x) = Log[ P[X > x] ]
                             = Log[ 1 - P[X <= x] ]
                             = Log[ 1 - cdf(x) ]
    ```

    Typically, different numerical approximations can be used for the log
    survival function, which are more accurate than `1 - cdf(x)` when `x >> 1`.

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      `Tensor` of shape `sample_shape(x) + self.batch_shape` with values of type
        `self.dtype`.
    )r  r  s      r.   r   z"Distribution.log_survival_function  s    , ++E488r-   c                 \    t        dj                  t        |       j                              )Nz(survival_function is not implemented: {}r   r   s     r.   _survival_functionzDistribution._survival_function  s*    
HOOT
  r-   c                 8   | j                  ||g      5  t        |d| j                        }	  | j                  |fi |cd d d        S # t        $ r9}	 d | j
                  |fi |z
  cY d }~cd d d        S # t        $ r |w xY wd }~ww xY w# 1 sw Y   y xY w)Nr   r^   rV   g      ?)r   r`   r\   r!  r   r   r  s        r.   _call_survival_functionz$Distribution._call_survival_function  s    			$w		/ 	# 
gtzz;e#&t&&u77		# 	#
 ! #	#hdhhu///
/	# 	# # 	#"
"	##	# 	#s@   BA	BA9*B+B9BBBBBc                 &    | j                  ||      S )a  Survival function.

    Given random variable `X`, the survival function is defined:

    ```none
    survival_function(x) = P[X > x]
                         = 1 - P[X <= x]
                         = 1 - cdf(x).
    ```

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      `Tensor` of shape `sample_shape(x) + self.batch_shape` with values of type
        `self.dtype`.
    )r#  r  s      r.   r$   zDistribution.survival_function  s    & ''t44r-   c                 \    t        dj                  t        |       j                              )Nzentropy is not implemented: {}r   r   s    r.   _entropyzDistribution._entropy  r  r-   c                 p    | j                  |      5  | j                         cddd       S # 1 sw Y   yxY w)zShannon entropy in nats.N)r   r&  r   s     r.   r   zDistribution.entropy  s.    			$	 ]]_     ,5c                 \    t        dj                  t        |       j                              )Nzmean is not implemented: {}r   r   s    r.   _meanzDistribution._mean  r
  r-   c                 p    | j                  |      5  | j                         cddd       S # 1 sw Y   yxY w)zMean.N)r   r*  r   s     r.   r   zDistribution.mean  .    			$	 ZZ\  r(  c                 \    t        dj                  t        |       j                              )Nzquantile is not implemented: {}r   r   s     r.   	_quantilezDistribution._quantile  r   r-   c                     | j                  ||g      5  t        |d| j                        } | j                  |fi |cd d d        S # 1 sw Y   y xY wr   )r   r`   r\   r.  )r   r^   r3   r   s       r.   _call_quantilezDistribution._call_quantile  sT    			$w		/ - 
gtzz;eT^^E,V,- - -s   +A

Ac                 &    | j                  ||      S )a  Quantile function. Aka "inverse cdf" or "percent point function".

    Given random variable `X` and `p in [0, 1]`, the `quantile` is:

    ```none
    quantile(p) := x such that P[X <= x] == p
    ```

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      quantile: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r0  r  s      r.   quantilezDistribution.quantile  s    " ud++r-   c                 \    t        dj                  t        |       j                              )Nzvariance is not implemented: {}r   r   s    r.   	_variancezDistribution._variance  r   r-   c                    | j                  |      5  	 | j                         cddd       S # t        $ rF}	 t        j                  | j                               cY d}~cddd       S # t        $ r |w xY wd}~ww xY w# 1 sw Y   yxY w)a  Variance.

    Variance is defined as,

    ```none
    Var = E[(X - E[X])**2]
    ```

    where `X` is the random variable associated with this distribution, `E`
    denotes expectation, and `Var.shape = batch_shape + event_shape`.

    Args:
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      variance: Floating-point `Tensor` with shape identical to
        `batch_shape + event_shape`, i.e., the same shape as `self.mean()`.
    N)r   r4  r   r	   square_stddevr   r3   r  s      r.   r%   zDistribution.variance  s    & 
		$	 ##~~# # ! #	#0
0# # # 	#"
"	### #=   A?-	A<"A(A<A?(A44A77A<<A??Bc                 \    t        dj                  t        |       j                              )Nzstddev is not implemented: {}r   r   s    r.   r7  zDistribution._stddev  s*    
=DDT
  r-   c                    | j                  |      5  	 | j                         cddd       S # t        $ rF}	 t        j                  | j                               cY d}~cddd       S # t        $ r |w xY wd}~ww xY w# 1 sw Y   yxY w)a  Standard deviation.

    Standard deviation is defined as,

    ```none
    stddev = E[(X - E[X])**2]**0.5
    ```

    where `X` is the random variable associated with this distribution, `E`
    denotes expectation, and `stddev.shape = batch_shape + event_shape`.

    Args:
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      stddev: Floating-point `Tensor` with shape identical to
        `batch_shape + event_shape`, i.e., the same shape as `self.mean()`.
    N)r   r7  r   r	   sqrtr4  r8  s      r.   r#   zDistribution.stddev  s    ( 
		$	 ##||~# # ! #	#t~~/0
0# # # 	#"
"	### #r9  c                 \    t        dj                  t        |       j                              )Nz!covariance is not implemented: {}r   r   s    r.   _covariancezDistribution._covariance;  s*    
AHHT
  r-   c                 p    | j                  |      5  | j                         cddd       S # 1 sw Y   yxY w)a  Covariance.

    Covariance is (possibly) defined only for non-scalar-event distributions.

    For example, for a length-`k`, vector-valued distribution, it is calculated
    as,

    ```none
    Cov[i, j] = Covariance(X_i, X_j) = E[(X_i - E[X_i]) (X_j - E[X_j])]
    ```

    where `Cov` is a (batch of) `k x k` matrix, `0 <= (i, j) < k`, and `E`
    denotes expectation.

    Alternatively, for non-vector, multivariate distributions (e.g.,
    matrix-valued, Wishart), `Covariance` shall return a (batch of) matrices
    under some vectorization of the events, i.e.,

    ```none
    Cov[i, j] = Covariance(Vec(X)_i, Vec(X)_j) = [as above]
    ```

    where `Cov` is a (batch of) `k' x k'` matrices,
    `0 <= (i, j) < k' = reduce_prod(event_shape)`, and `Vec` is some function
    mapping indices of this distribution's event dimensions to indices of a
    length-`k'` vector.

    Args:
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      covariance: Floating-point `Tensor` with shape `[B1, ..., Bn, k', k']`
        where the first `n` dimensions are batch coordinates and
        `k' = reduce_prod(self.event_shape)`.
    N)r   r>  r   s     r.   r   zDistribution.covariance?  s4    H 
		$	       r(  c                 \    t        dj                  t        |       j                              )Nzmode is not implemented: {}r   r   s    r.   _modezDistribution._modef  r
  r-   c                 p    | j                  |      5  | j                         cddd       S # 1 sw Y   yxY w)zMode.N)r   rA  r   s     r.   r    zDistribution.modej  r,  r(  c                 F    t        j                  | || j                        S N)r   )r
   r   r   r   s     r.   _cross_entropyzDistribution._cross_entropyo  "    ))eT%9%9; ;r-   c                 r    | j                  |      5  | j                  |      cddd       S # 1 sw Y   yxY w)a  Computes the (Shannon) cross entropy.

    Denote this distribution (`self`) by `P` and the `other` distribution by
    `Q`. Assuming `P, Q` are absolutely continuous with respect to
    one another and permit densities `p(x) dr(x)` and `q(x) dr(x)`, (Shanon)
    cross entropy is defined as:

    ```none
    H[P, Q] = E_p[-log q(X)] = -int_F p(x) log q(x) dr(x)
    ```

    where `F` denotes the support of the random variable `X ~ P`.

    Args:
      other: `tfp.distributions.Distribution` instance.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      cross_entropy: `self.dtype` `Tensor` with shape `[B1, ..., Bn]`
        representing `n` different calculations of (Shanon) cross entropy.
    N)r   rE  r   r   r3   s      r.   r   zDistribution.cross_entropys  s5    , 
		$	 (  '( ( (   -6c                 F    t        j                  | || j                        S rD  )r
   r   r   r   s     r.   _kl_divergencezDistribution._kl_divergence  rF  r-   c                 r    | j                  |      5  | j                  |      cddd       S # 1 sw Y   yxY w)an  Computes the Kullback--Leibler divergence.

    Denote this distribution (`self`) by `p` and the `other` distribution by
    `q`. Assuming `p, q` are absolutely continuous with respect to reference
    measure `r`, the KL divergence is defined as:

    ```none
    KL[p, q] = E_p[log(p(X)/q(X))]
             = -int_F p(x) log q(x) dr(x) + int_F p(x) log p(x) dr(x)
             = H[p, q] - H[p]
    ```

    where `F` denotes the support of the random variable `X ~ p`, `H[., .]`
    denotes (Shanon) cross entropy, and `H[.]` denotes (Shanon) entropy.

    Args:
      other: `tfp.distributions.Distribution` instance.
      name: Python `str` prepended to names of ops created by this function.

    Returns:
      kl_divergence: `self.dtype` `Tensor` with shape `[B1, ..., Bn]`
        representing `n` different calculations of the Kullback-Leibler
        divergence.
    N)r   rK  rH  s      r.   r   zDistribution.kl_divergence  s5    2 
		$	 (  '( ( (rI  c                 T   dj                  t        |       j                  | j                  | j                  j
                  dj                  | j                        nd| j                  j
                  dj                  | j                        nd| j                  j                        S )Nzatfp.distributions.{type_name}("{self_name}"{maybe_batch_shape}{maybe_event_shape}, dtype={dtype})z, batch_shape={}rA   z, event_shape={})	type_name	self_namemaybe_batch_shapemaybe_event_shaper\   )r   r   r(   r3   r   ndimsr   r\   r   s    r.   __str__zDistribution.__str__  s      &vt*--))&*&6&6&<&<&H $6#<#<T=M=M#N(*&*&6&6&<&<&H $6#<#<T=M=M#N(*jjoo  & 	 '	(r-   c                     dj                  t        |       j                  | j                  | j                  | j
                  | j                  j                        S )Nzo<tfp.distributions.{type_name} '{self_name}' batch_shape={batch_shape} event_shape={event_shape} dtype={dtype}>)rN  rO  r   r   r\   )r   r   r(   r3   r   r   r\   r   s    r.   r   zDistribution.__repr__  sN     %ft*--)) ,, ,,jjoo % '		(r-   c              #      K   t        j                  | j                        5  t        j                  ||g n|| j                  z         5 }| ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY ww)z(Helper function to standardize op scope.Nr   )r   r   r3   r   )r   r3   r   scopes       r.   r   zDistribution._name_scope  sq      
			" >>$2Vt/B/B
BE HM   s4    A9)A-A!A-	A9!A*	&A--A62A9c                    t        j                  |      }|t        j                  |      }n/t	        j
                  ||j                  j                               }|j                         j                  }|t        j                  |      }t        j                  t        j                  |d      t	        j                  dgt        j                         t        j"                  |            }t        j$                  ||      }||fS |dk(  rf|It'        j(                  t	        j                  |g|j                  j                               |      }||fS t        j$                  |dg      }||fS |dk7  rt+        d      ||fS )z-Helper to `sample` which ensures input is 1D.)r\   r   r   rU   z#Input is neither scalar nor vector.)r   r   r	   reduce_prodnpprodr\   as_numpy_dtype	get_shaperR  r   rankr   pick_vectorequalarrayr   r   r   r   r[   ro   )r   xr3   x_static_valrZ  rR  expanded_shapes          r.   r   z+Distribution._expand_sample_shape_to_vector  sN   --a0L!!!$dWW\)?)?)ABdKKME}nnQe''
..
"
((A3bhh
');=n 

A~
.a d7N 
!		!!!HHl^177+A+A+CD d7N	 a!% d7N 
!<==d7Nr-   c                    t        j                  t        j                  |            }|j	                         j
                  }|j
                  }| j                  j
                  }| j                  j
                  }|#|!||||z   |z   }|j                  dg|z         |H|F|j                  dg||z
  z        }|j                  |j	                         j                  |             |e|ct        j                  dg||z
  z        j                  | j                        }|j                  |j	                         j                  |             ||||	||z
  |z
  }n|
|||z
  |z
  }|u|st        j                  dg|z        j                  | j                        j                  dg|z        }|j                  |j	                         j                  |             |S )z+Helper to `sample`; sets static shape info.N)r   r   r   r   r\  rR  r   r   	set_shapeconcatenate
merge_with)r   ra  r   rR  sample_ndimsbatch_ndimsevent_ndimsr   s           r.   r   z%Distribution._set_sample_static_shape  s     ++""<02L KKME%%L""((K""((K 	 [(;6ekk4&5.! \5&&vu|/C'DEekk!++-**512 [4&&&%+%
&((3D4D4D(E kk!++-**512 		K$;,{:, \%=+l:+		!k&=(($)<=II)k4&*<= 		AKKM,,U34Hr-   c                 d   |j                   |j                   dk(  S  |       }|j                         j                   I|j                         j                  d   j                  "|j                         j	                         dgk(  S t        j                  t        j                  |      d   d      S )z;Implementation for `is_scalar_batch` and `is_scalar_event`.r   )	rR  r\  dimsr^   r   r	   r_  r   r   )r   r   dynamic_shape_fnr   s       r.   r   zDistribution._is_scalar_helper  s    %1$$E+q!''3 __&&(QC//>>)//%03Q77r-   )NNN)DistributionParamShapes)r   )r   )r   )r   r   )r,   Nr"   )r   )r!   )r   )r   )r   )r$   )r   )r   )r2  )r%   )r#   )r   )r    )r   )r   NN)Pr(   r)   r*   r+   r   r   r   propertyr   setterclassmethodr   r   staticmethodr   r3   r\   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r   r  r   r  r  r!   r  r  r   r  r  r   r  r  r   r!  r#  r$   r&  r   r*  r   r.  r0  r2  r4  r%   r7  r#   r>  r   rA  r    rE  r   rK  r   rS  r   
contextlibcontextmanagerr   r   r   r   r,   r-   r.   r   r     sz   FP ;'
  !44l     ! ! - -( $ $L ? ?     7 7 
) 
) ! !   $$Q(&* 6 6Q( * 6 6""9 
#,
#(
#+.
#'&"

#90
#5*

-,&#8#:% N
;(2;(8( 
(  :*X8r-   r   ro  )&r+   rg   rt  r8   numpyrY  tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr   r	   #tensorflow.python.ops.distributionsr
   r   tensorflow.python.utilr   r    tensorflow.python.util.tf_exportr   __all__rj   rh   r'   r?   rS   r`   rb   r   r   export_constantr(   r   r   r,   r-   r.   <module>r     s"   2 
    + . + 4 3 + * @ 4 . - 6( $.#++ 
@),	9DC DCN 567+ + 8+b //FG  	34 5 E E%' --BC  	12 3 C C#% +,-S8$0A S8 .S8r-   