B
    `                 @   s  d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	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gZ!ddddddddddddddd d!d"d#d$gZ"ddgZ#e$ Z%d%Z&e'ej(G d&d' d'ej)Z*d(d) Z+d*d+ Z,d=d,d-Z-d.d/ Z.d0d1 Z/G d2d3 d3ej(Z0e'e0G d4d de*Z1G d5d6 d6e2Z3d7d8 Z4d9d: Z5d;d< Z6dS )>z+Base classes for probability distributions.    )absolute_import)division)print_functionN)v2)kullback_leibler)slicing)assert_util)distribution_util)
dtype_util)	name_util)tensorshape_util)log1mexp)nest)
tf_inspect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varianceFc               @   s   e Zd ZdZdS )_BaseDistributionz<Abstract base class needed for resolving subclass hierarchy.N)__name__
__module____qualname____doc__ r)   r)   n/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow_probability/python/distributions/_numpy/distribution.pyr$   R   s   r$   c             C   s4   t | std| tj| j| j| j| j| j	d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: {})codeglobalsnameZargdefsZclosure)
callable	TypeErrorformattypesFunctionType__code____globals__r%   __defaults____closure__)fnr)   r)   r*   _copy_fnX   s    r8   c             C   s   | pd} |  d}ddd | dD }dd t|D }|rz|d }d|d| d	 | d	 d||d  S | d	 | S dS )
zGUpdate old_str by inserting append_str just before the 'Args:' section. 
c             s   s   | ]}d | V  qdS )z    %sNr)   ).0liner)   r)   r*   	<genexpr>   s    z$_update_docstring.<locals>.<genexpr>c             S   s$   g | ]\}}|   d kr|qS )zargs:)striplower)r;   ixr<   r)   r)   r*   
<listcomp>   s    z%_update_docstring.<locals>.<listcomp>Nz

)splitjoin	enumerate)Zold_strZ
append_strZold_str_linesZhas_args_ixZfinal_args_ixr)   r)   r*   _update_docstringx   s    
0rF   c          	      s   t j|st j|r|dkr@ fdd}tj||| |ddS |dkrh fdd}tj||| |ddS  fdd}tj||| ||ddd	S t j| || d
S )a  Converts the given `value` to a (structure of) `Tensor`.

  This function converts Python objects of various types to a (structure of)
  `Tensor` objects. It accepts `Tensor` objects, numpy arrays, Python lists, and
  Python scalars. For example:

  Args:
    value: An object whose structure matches that of `dtype ` and/or
      `dtype_hint` and for which each leaf has a registered `Tensor` conversion
      function.
    dtype: Optional (structure of) element type for the returned tensor. If
      missing, the type is inferred from the type of `value`.
    dtype_hint: Optional (structure of) element type for the returned tensor,
      used when dtype is None. In some cases, a caller may not have a dtype in
      mind when converting to a tensor, so dtype_hint can be used as a soft
      preference.  If the conversion to `dtype_hint` is not possible, this
      argument has no effect.
    name: Optional name to use if a new `Tensor` is created.

  Returns:
    tensor: A (structure of) `Tensor` based on `value`.

  Raises:
    TypeError: If no conversion function is registered for `value` to `dtype`.
    RuntimeError: If a registered conversion function returns an invalid value.
    ValueError: If the `value` is a tensor not of given `dtype` in graph mode.
  Nc                s   t j| | dS )N)
dtype_hintr-   )tfconvert_to_tensor)vdh)r-   r)   r*   <lambda>       z$_convert_to_tensor.<locals>.<lambda>F)check_typesc                s   t j| | dS )N)dtyper-   )rH   rI   )rJ   d)r-   r)   r*   rL      rM   c                s   t j| || dS )N)rO   rG   r-   )rH   rI   )rJ   rP   rK   )r-   r)   r*   rL      s   T)rN   Zexpand_composites)rO   rG   r-   )rH   r   	is_nestedmap_structure_up_torI   )valuerO   rG   r-   r7   r)   )r-   r*   _convert_to_tensor   s    
rT   c                s    fdd|   D S )z4Removes `dict` keys which have have `self` as value.c                s   i | ]\}}| k	r||qS r)   r)   )r;   krJ   )valr)   r*   
<dictcomp>   s    z0_remove_dict_keys_with_value.<locals>.<dictcomp>)items)Zdict_rV   r)   )rV   r*   _remove_dict_keys_with_value   s    rY   c       	      C   s|  t t |}t| j}t|}t|}t|}|dkrx|dk	rx|dk	rx|dk	rx|| | }t| dg|  |dk	r|dk	rt|dg||  }t| | |dk	r|dk	rt dg||  |}t| | |dk	rx|dk	r6|dkr|dk	r|| | }n |dkr6|dk	r6|| | }|dk	rx|dk	rxt dg| |dg| }t| | | S )zEHelper to `_set_sample_static_shape`; sets shape info for a `Tensor`.N)rH   TensorShapeget_static_valuer   rankshapeZ	set_shapeZconcatenate)	xr   r   sample_shapeZndimsZsample_ndimsZbatch_ndimsZevent_ndimsr]   r)   r)   r*   #_set_sample_static_shape_for_tensor   s<    




r`   c                   s    e Zd ZdZ fddZ  ZS )_DistributionMetaz&Helper metaclass for tfp.Distribution.c                s  |st ddd |D }|r&|d nd}|dks:|tkrPtt| | |||S t|tslt d||jxt	D ]}||krqrd|}|
|d}t||d}	|	std|j||
|d}
|
dkrdnt|
}|s|tkrt|	}|||< |sqrt|	}|dkr"td	|j|t|jd
|||_qrW |
dd  dkrhtt| | |||S tj fdd}| |d< tt| | |||S )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?c             S   s"   g | ]}|t kst|tr|qS r)   )r$   
issubclassr   )r;   baser)   r)   r*   rA     s    z-_DistributionMeta.__new__.<locals>.<listcomp>r   NzEFirst parent class declared for {} must be Distribution, but saw "{}"z_{}zAInternal error: expected base class "{}" to implement method "{}"z4Expected base class fn to contain a docstring: {}.{}z'Additional documentation from `{}`:

{}__init__c                sh   ~ d|_ |f  |j dkrDt | fdd|_ n t|j drd|t|j ||_ dS )z-A 'master `__init__`' which is always called.Nc                  s   t tjf S )N)rY   inspectgetcallargsr)   )argsdefault_init
dummy_selfkwargsr)   r*   rL   V  s    zA_DistributionMeta.__new__.<locals>.wrapped_init.<locals>.<lambda>pop)_parameterstuple_no_dependencyhasattrrY   )wrappedZself_rg   rj   )rh   )rg   ri   rj   r*   wrapped_initE  s    
z/_DistributionMeta.__new__.<locals>.wrapped_init)r/   r$   superra   __new__rb   r   r0   r%   $_DISTRIBUTION_PUBLIC_METHOD_WRAPPERSgetgetattrAttributeErrorr   getdoc#_ALWAYS_COPY_PUBLIC_METHOD_WRAPPERSr8   
ValueErrorrF   r(   	decorator)Zmcs	classnameZbaseclassesattrsZ
which_baserc   attrZspecial_attrZclass_attr_valueZbase_attr_valueZclass_special_attr_valueZclass_special_attr_docstringZclass_attr_docstringrq   )	__class__)rh   r*   rs      s\    









z_DistributionMeta.__new__)r%   r&   r'   r(   rs   __classcell__r)   r)   )r   r*   ra      s   ra   c                   s  e Zd ZdZd fdd	ZedddZedd	 Zed
d Z	e
dd Ze
dd Ze
dd Zedd Zdd Zdd Ze
dd Ze
dd Ze
dd Zdd Zd d! Zdd#d$Zd%d& Ze
d'd( Zd)d* Zdd,d-Zd.d/ Ze
d0d1 Zdd3d4Zdd6d7Zdd8d9Zd:d; Z dd>d?Z!d@dA Z"ddCdDZ#dEdF Z$ddHdIZ%dJdK Z&ddMdNZ'dOdP Z(ddRdSZ)dTdU Z*dVdW Z+ddYdZZ,d[d\ Z-d]d^ Z.dd`daZ/dbdc Z0ddedfZ1dgdh Z2ddjdkZ3dldm Z4dndo Z5ddqdrZ6dsdt Z7ddvdwZ8dxdy Z9dd{d|Z:d}d~ Z;dddZ<dd Z=dddZ>dd Z?dddZ@dd ZAdddZBdd ZCdd ZDdd ZEdd ZFeGjHdeIdfddZJdd ZKdd ZLdd ZMdd ZNdd ZO  ZPS )r   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
  @distribution_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`.

  `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(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()
  ```

  Nc       
         s   |st | j}t|}t|}t|}tt| j|d || _	|dkrPg n|}x6t
|D ]*\}}	|	dksxt|	s^td||	f q^W || _|| _|| _|| _| || _d| _|| _tdd | jddD | _| jrtj| j f| _dS )	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 `tfd.FULLY_REPARAMETERIZED`, then samples from the distribution are
        fully reparameterized, and straight-through gradients are supported.
        If `tfd.NOT_REPARAMETERIZED`, then samples from the distribution are not
        fully reparameterized, and straight-through gradients are either
        partially unsupported or are not supported at all.
      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`.
    )r-   Nz)Graph parent item %d is not a Tensor; %s.Fc             s   s   | ]}|d k	r|V  qd S )Nr)   )r;   rP   r)   r)   r*   r=   (  s    z(Distribution.__init__.<locals>.<genexpr>T)is_init)typer%   r   Zcamel_to_lower_snakeZget_name_scope_nameZstrip_invalid_charsrr   r   rd   _namerE   rH   Z	is_tensorrz   _dtype_reparameterization_type_allow_nan_stats_validate_argsrn   rl   _parameters_sanitizedZ_graph_parentsrm   _parameter_control_dependencies'_initial_parameter_control_dependenciesgroup)
selfrO   reparameterization_typevalidate_argsallow_nan_stats
parametersZgraph_parentsr-   it)r   r)   r*   rd     s,    &



zDistribution.__init__DistributionParamShapesc          	   C   s    t | | |S Q R X dS )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.
    N)rH   
name_scope_param_shapes)clsr_   r-   r)   r)   r*   param_shapes.  s    zDistribution.param_shapesc             C   sx   t |tjr(t|stdt|}| |}i }x<| D ]0\}}t	|}|dkrbtdt|||< q@W |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 definedNz>sample_shape must be a fully-defined TensorShape or list/tuple)

isinstancerH   rZ   r   is_fully_definedrz   as_listr   rX   r[   )r   r_   paramsZstatic_paramsr-   r]   static_shaper)   r)   r*   param_static_shapesC  s    



z Distribution.param_static_shapesc             C   s   t dd S )Nz_param_shapes not implemented)NotImplementedError)r_   r)   r)   r*   r   j  s    zDistribution._param_shapesc             C   s   t | dr| jS dS )z9Name prepended to all ops created by this `Distribution`.r   N)ro   r   )r   r)   r)   r*   r-   n  s    zDistribution.namec             C   s   t | dr| jS dS )z8The `DType` of `Tensor`s handled by this `Distribution`.r   N)ro   r   )r   r)   r)   r*   rO   s  s    zDistribution.dtypec                sV   t  dr jsLt jr"  n j}  fdd| D  _d _t jS )zADictionary of parameters used to instantiate this `Distribution`.r   c                s(   i | ] \}}| d s| k	r||qS )__)
startswith)r;   rU   rJ   )r   r)   r*   rW     s   z+Distribution.parameters.<locals>.<dictcomp>T)ro   r   r.   rl   rn   rX   dict)r   pr)   )r   r*   r   x  s    
zDistribution.parametersc             C   s   t d| dS )a  Returns a dict mapping constructor argument names to per-event rank.

    Distributions may implement this method to provide support for slicing
    (`__getitem__`) on the batch axes.

    Examples: Normal has scalar parameters, so would return
    `{'loc': 0, 'scale': 0}`. On the other hand, MultivariateNormalTriL has
    vector loc and matrix scale, so returns `{'loc': 1, 'scale_tril': 2}`. When
    a distribution accepts multiple parameterizations, either all possible
    parameters may be specified by the dict, e.g. Bernoulli returns
    `{'logits': 0, 'probs': 0}`, or if convenient only the parameters relevant
    to this instance may be specified.

    Parameter dtypes are inferred from Tensor attributes on the distribution
    where available, e.g. `bernoulli.probs`, 'mvn.scale_tril', falling back with
    a warning to the dtype of the distribution.

    Returns:
      params_event_ndims: Per-event parameter ranks, a `str->int dict`.
    zF{} does not support batch slicing; must implement _params_event_ndims.N)r   r0   )r   r)   r)   r*   _params_event_ndims  s    z Distribution._params_event_ndimsc             C   s   t | |  i |S )a*  Slices the batch axes of this distribution, returning a new instance.

    ```python
    b = tfd.Bernoulli(logits=tf.zeros([3, 5, 7, 9]))
    b.batch_shape  # => [3, 5, 7, 9]
    b2 = b[:, tf.newaxis, ..., -2:, 1::2]
    b2.batch_shape  # => [3, 1, 5, 2, 4]

    x = tf.random.stateless_normal([5, 3, 2, 2])
    cov = tf.matmul(x, x, transpose_b=True)
    chol = tf.cholesky(cov)
    loc = tf.random.stateless_normal([4, 1, 3, 1])
    mvn = tfd.MultivariateNormalTriL(loc, chol)
    mvn.batch_shape  # => [4, 5, 3]
    mvn.event_shape  # => [2]
    mvn2 = mvn[:, 3:, ..., ::-1, tf.newaxis]
    mvn2.batch_shape  # => [4, 2, 3, 1]
    mvn2.event_shape  # => [2]
    ```

    Args:
      slices: slices from the [] operator

    Returns:
      dist: A new `tfd.Distribution` instance with sliced parameters.
    )r   batch_slicer   )r   Zslicesr)   r)   r*   __getitem__  s    zDistribution.__getitem__c             C   s   t dt| jd S )Nz{!r} object is not iterable)r/   r0   r   r%   )r   r)   r)   r*   __iter__  s    zDistribution.__iter__c             C   s   | j S )zDescribes how samples from the distribution are reparameterized.

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

    Returns:
      An instance of `ReparameterizationType`.
    )r   )r   r)   r)   r*   r     s    
z$Distribution.reparameterization_typec             C   s   | 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   r)   r)   r*   r     s    zDistribution.allow_nan_statsc             C   s   | j S )z?Python `bool` indicating possibly expensive checks are enabled.)r   )r   r)   r)   r*   r     s    zDistribution.validate_argsc             K   sV   yt | |  |tS  tk
rP   t| jf|}t| f |}||_d|_	|S X d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)`.
    TN)
r   r   r   Ellipsisr   r   r   r   rl   r   )r   Zoverride_parameters_kwargsr   rP   r)   r)   r*   copy  s    zDistribution.copyc             C   s   t dt| jd S )Nz)batch_shape_tensor is not implemented: {})r   r0   r   r%   )r   r)   r)   r*   _batch_shape_tensor   s    z Distribution._batch_shape_tensorr   c          	   C   s   |  |p t| jtjrdn| j}tdd tj|| jddD r\tj	|t
j| jdd}n|  }tj	|dd |ddS Q R X dS )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`.
    Nc             S   s   g | ]}t |qS r)   )r   r   )r;   sr)   r)   r*   rA     s   z3Distribution.batch_shape_tensor.<locals>.<listcomp>F)rN   c             S   s   t jt j| t jdddS )N)rO   r   )r-   )rH   identityrI   int32)r   r)   r)   r*   rL   #  s   z1Distribution.batch_shape_tensor.<locals>.<lambda>)_name_and_control_scoper   r   rH   rZ   rO   allr   Zflatten_up_torR   r   r   r   )r   r-   Zshallow_structurer   r)   r)   r*   r     s    zDistribution.batch_shape_tensorc             C   s   d S )Nr)   )r   r)   r)   r*   _batch_shape'  s    zDistribution._batch_shapec             C   sL   |   }t|tjs,tdd t|D r6t|S tj| jtj|dd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.
    c             s   s,   | ]$\}}t |d ko"t|tj V  qdS )   N)lenr   rH   rZ   )r;   pathr   r)   r)   r*   r=   ;  s   z+Distribution.batch_shape.<locals>.<genexpr>F)rN   )	r   r   rH   rZ   r   r   Zflatten_with_tuple_pathsrR   rO   )r   r   r)   r)   r*   r   *  s    
zDistribution.batch_shapec             C   s   t dt| jd S )Nz)event_shape_tensor is not implemented: {})r   r0   r   r%   )r   r)   r)   r*   _event_shape_tensorA  s    z Distribution._event_shape_tensorr   c          	   C   sj   |  |V tdd t| jD r@tj| jtj| jdd}n| 	 }tj| jdd |ddS Q R X dS )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`.
    c             S   s   g | ]}t |qS r)   )r   r   )r;   r   r)   r)   r*   rA   O  s   z3Distribution.event_shape_tensor.<locals>.<listcomp>F)rN   c             S   s   t jt j| t jdddS )N)rO   r   )r-   )rH   r   rI   r   )r   r)   r)   r*   rL   Y  s   z1Distribution.event_shape_tensor.<locals>.<lambda>N)
r   r   r   flattenr   rR   rO   r   r   r   )r   r-   r   r)   r)   r*   r   E  s    	zDistribution.event_shape_tensorc             C   s   d S )Nr)   )r   r)   r)   r*   _event_shape]  s    zDistribution._event_shapec             C   s   t j| jtj|  dd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.
    F)rN   )r   rR   rO   rH   rZ   r   )r   r)   r)   r*   r   `  s    	zDistribution.event_shapeis_scalar_eventc          	   C   s0   |  | tj| | j| jddS Q R X dS )zIndicates that `event_shape == []`.

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

    Returns:
      is_scalar_event: `bool` scalar `Tensor`.
    r   )r-   N)r   rH   rI   _is_scalar_helperr   r   )r   r-   r)   r)   r*   r   l  s    	zDistribution.is_scalar_eventis_scalar_batchc          	   C   s0   |  | tj| | j| jddS Q R X dS )zIndicates that `batch_shape == []`.

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

    Returns:
      is_scalar_batch: `bool` scalar `Tensor`.
    r   )r-   N)r   rH   rI   r   r   r   )r   r-   r)   r)   r*   r   z  s    	zDistribution.is_scalar_batchc             K   s   t dt| jd S )Nzsample_n is not implemented: {})r   r0   r   r%   )r   nseedrj   r)   r)   r*   	_sample_n  s    zDistribution._sample_nc       	   	   K   s   |  | tr |dkr tdtj|tjdd}| |d\}}| j|fdt|rZ| n|i|}t	|dd }t
||gd}t||}| ||}|S Q R X dS )zWrapper around _sample_n.Nz1Must provide JAX PRNGKey as `dist.sample(seed=.)`r_   )r-   r   r   r   )r   JAX_MODErz   rH   castr   _expand_sample_shape_to_vectorr   r.   r]   concatZreshape_set_sample_static_shape)	r   r_   r   r-   rj   r   ZsamplesZbatch_event_shapeZfinal_shaper)   r)   r*   _call_sample_n  s     zDistribution._call_sample_nr)   r    c             K   s   | j |||f|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 or `tfp.util.SeedStream` instance, for seeding PRNG.
      name: name to give to the op.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      samples: a `Tensor` with prepended dimensions `sample_shape`.
    )r   )r   r_   r   r-   rj   r)   r)   r*   r      s    zDistribution.samplec          	   K   sz   t |d| jd}| |||R t| dr8| j|f|S t| drXtj| j|f|S t	d
t| jW dQ R X dS )zWrapper around _log_prob.rS   )r-   rG   	_log_prob_probzlog_prob is not implemented: {}N)rT   rO   r   ro   r   rH   mathlogr   r   r0   r   r%   )r   rS   r-   rj   r)   r)   r*   _call_log_prob  s    

zDistribution._call_log_probr   c             K   s   | j ||f|S )au  Log probability density/mass function.

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      log_prob: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r   )r   rS   r-   rj   r)   r)   r*   r     s    zDistribution.log_probc          	   K   sx   t |d| jd}| |||P t| dr8| j|f|S t| drVt| j|f|S td	t
| jW dQ R X dS )zWrapper around _prob.rS   )r-   rG   r   r   zprob is not implemented: {}N)rT   rO   r   ro   r   rH   expr   r   r0   r   r%   )r   rS   r-   rj   r)   r)   r*   
_call_prob  s    

zDistribution._call_probr   c             K   s   | j ||f|S )am  Probability density/mass function.

    Args:
      value: `float` or `double` `Tensor`.
      name: Python `str` prepended to names of ops created by this function.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      prob: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r   )r   rS   r-   rj   r)   r)   r*   r     s    zDistribution.probc          	   K   sz   t |d| jd}| |||R t| dr8| j|f|S t| drXtj| j|f|S t	d
t| jW dQ R X dS )zWrapper around _log_cdf.rS   )r-   rG   _log_cdf_cdfzlog_cdf is not implemented: {}N)rT   rO   r   ro   r   rH   r   r   r   r   r0   r   r%   )r   rS   r-   rj   r)   r)   r*   _call_log_cdf  s    

zDistribution._call_log_cdfr   c             K   s   | j ||f|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.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      logcdf: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r   )r   rS   r-   rj   r)   r)   r*   r     s    zDistribution.log_cdfc          	   K   sx   t |d| jd}| |||P t| dr8| j|f|S t| drVt| j|f|S td	t
| jW dQ R X dS )zWrapper around _cdf.rS   )r-   rG   r   r   zcdf is not implemented: {}N)rT   rO   r   ro   r   rH   r   r   r   r0   r   r%   )r   rS   r-   rj   r)   r)   r*   	_call_cdf  s    

zDistribution._call_cdfr   c             K   s   | j ||f|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.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      cdf: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r   )r   rS   r-   rj   r)   r)   r*   r     s    zDistribution.cdfc             K   s   t dt| jd S )Nz,log_survival_function is not implemented: {})r   r0   r   r%   )r   rS   rj   r)   r)   r*   _log_survival_function!  s    z#Distribution._log_survival_functionc             K   s   t |d| jd}| |||z y| j|f|S  tk
r } zFt| dr\t| j|f|S t| dr~tj	
| j|f| S |W dd}~X Y nX W dQ R X dS )z&Wrapper around _log_survival_function.rS   )r-   rG   r   r   N)rT   rO   r   r   r   ro   r   r   rH   r   log1pr   )r   rS   r-   rj   original_exceptionr)   r)   r*   _call_log_survival_function&  s    

z(Distribution._call_log_survival_functionr   c             K   s   | j ||f|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.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      `Tensor` of shape `sample_shape(x) + self.batch_shape` with values of type
        `self.dtype`.
    )r   )r   rS   r-   rj   r)   r)   r*   r   3  s    z"Distribution.log_survival_functionc             K   s   t dt| jd S )Nz(survival_function is not implemented: {})r   r0   r   r%   )r   rS   rj   r)   r)   r*   _survival_functionM  s    zDistribution._survival_functionc             K   s   t |d| jd}| |||z y| j|f|S  tk
r } zFt| drbtj| j	|f| S t| dr~d| j
|f| S |W dd}~X Y nX W dQ R X dS )z"Wrapper around _survival_function.rS   )r-   rG   r   r   g      ?N)rT   rO   r   r   r   ro   rH   r   expm1r   r   )r   rS   r-   rj   r   r)   r)   r*   _call_survival_functionQ  s    

z$Distribution._call_survival_functionr"   c             K   s   | j ||f|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.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      `Tensor` of shape `sample_shape(x) + self.batch_shape` with values of type
        `self.dtype`.
    )r   )r   rS   r-   rj   r)   r)   r*   r"   ^  s    zDistribution.survival_functionc             K   s   t dt| jd S )Nzentropy is not implemented: {})r   r0   r   r%   )r   rj   r)   r)   r*   _entropyt  s    zDistribution._entropyr   c          	   K   s"   |  | | jf |S Q R X dS )zShannon entropy in nats.N)r   r   )r   r-   rj   r)   r)   r*   r   x  s    zDistribution.entropyc             K   s   t dt| jd S )Nzmean is not implemented: {})r   r0   r   r%   )r   rj   r)   r)   r*   _mean}  s    zDistribution._meanr   c          	   K   s"   |  | | jf |S Q R X dS )zMean.N)r   r   )r   r-   rj   r)   r)   r*   r     s    zDistribution.meanc             K   s   t dt| jd S )Nzquantile is not implemented: {})r   r0   r   r%   )r   rS   rj   r)   r)   r*   	_quantile  s    zDistribution._quantilec          
   K   s   |  || tj| jr tjn| j}tj|d|d}| jrxt	t
j|td|jddt
j|td|jddg|}| j|f|S Q R X d S )NrS   )r-   rG   r   z`value` must be <= 1)messager   z`value` must be >= 0)r   rH   r   rQ   rO   float32rI   r   r	   Zwith_dependenciesr   Zassert_less_equalr   Zassert_greater_equalr   )r   rS   r-   rj   rO   r)   r)   r*   _call_quantile  s    zDistribution._call_quantilequantilec             K   s   | j ||f|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.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      quantile: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with
        values of type `self.dtype`.
    )r   )r   rS   r-   rj   r)   r)   r*   r     s    zDistribution.quantilec             K   s   t dt| jd S )Nzvariance is not implemented: {})r   r0   r   r%   )r   rj   r)   r)   r*   	_variance  s    zDistribution._variancer#   c             K   sv   |  |b y| jf |S  tk
rf } z0yt| jf |S  tk
rT   |Y nX W dd}~X Y nX W dQ R X dS )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.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      variance: Floating-point `Tensor` with shape identical to
        `batch_shape + event_shape`, i.e., the same shape as `self.mean()`.
    N)r   r   r   rH   Zsquare_stddev)r   r-   rj   r   r)   r)   r*   r#     s    zDistribution.variancec             K   s   t dt| jd S )Nzstddev is not implemented: {})r   r0   r   r%   )r   rj   r)   r)   r*   r     s    zDistribution._stddevr!   c             K   sv   |  |b y| jf |S  tk
rf } z0yt| jf |S  tk
rT   |Y nX W dd}~X Y nX W dQ R X dS )aC  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.
      **kwargs: Named arguments forwarded to subclass implementation.

    Returns:
      stddev: Floating-point `Tensor` with shape identical to
        `batch_shape + event_shape`, i.e., the same shape as `self.mean()`.
    N)r   r   r   rH   sqrtr   )r   r-   rj   r   r)   r)   r*   r!     s    zDistribution.stddevc             K   s   t dt| jd S )Nz!covariance is not implemented: {})r   r0   r   r%   )r   rj   r)   r)   r*   _covariance  s    zDistribution._covariancer   c          	   K   s"   |  | | jf |S Q R X dS )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.
      **kwargs: Named arguments forwarded to subclass implementation.

    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   r-   rj   r)   r)   r*   r     s    %zDistribution.covariancec             K   s   t dt| jd S )Nzmode is not implemented: {})r   r0   r   r%   )r   rj   r)   r)   r*   _mode  s    zDistribution._moder   c          	   K   s"   |  | | jf |S Q R X dS )zMode.N)r   r   )r   r-   rj   r)   r)   r*   r     s    zDistribution.modec             C   s   t j| || jdS )N)r   )r   r   r   )r   otherr)   r)   r*   _cross_entropy#  s    zDistribution._cross_entropyr   c          	   C   s    |  | | |S Q R X dS )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)`, (Shannon)
    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 (Shannon) cross entropy.
    N)r   r   )r   r   r-   r)   r)   r*   r   '  s    zDistribution.cross_entropyc             C   s   t j| || jdS )N)r   )r   r   r   )r   r   r)   r)   r*   _kl_divergence@  s    zDistribution._kl_divergencer   c             C   s
   |  |S )ap  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 (Shannon) cross entropy, and `H[.]` denotes (Shannon) 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.
    )r   )r   r   r-   r)   r)   r*   r   D  s    zDistribution.kl_divergencec             C   s   t dt| jd S )Nz5_default_event_space_bijector` is not implemented: {})r   r0   r   r%   )r   r)   r)   r*   _default_event_space_bijectore  s    z*Distribution._default_event_space_bijectorc             C   s   |   S )a  Bijector mapping the reals (R**n) to the event space of the distribution.

    Returns:
      event_space_bijector: `Bijector` instance or `None`.

    Distributions with continuous support have a
    `_default_event_space_bijector`, a subclass of `tfp.bijectors.Bijector`
    that maps R**n to the distribution's event space. For example, the
    `_default_event_space_bijector` of the `Beta` distribution is
    `tfb.Sigmoid()`, which maps the real line to `[0, 1]`, the support of the
    `Beta` distribution. The purpose of `_default_event_space_bijector` is
    to enable gradient descent in an unconstrained space for Variational
    Inference and Hamiltonian Monte Carlo methods. An effort has been made to
    choose bijectors such that the tails of the distribution in the
    unconstrained space are between Gaussian and Exponential. For distributions
    with discrete event space, `_default_event_space_bijector` returns `None`.
    )r   )r   r)   r)   r*   *_experimental_default_event_space_bijectorj  s    z7Distribution._experimental_default_event_space_bijectorc             C   sr   | j rdt| j  }nd}| jr0dt| j }nd}| jd k	rNdt| j }nd}djt| j| jpfd|||dS )Nz, batch_shape=r9   z, event_shape=z, dtype=z_tfp.distributions.{type_name}("{self_name}"{maybe_batch_shape}{maybe_event_shape}{maybe_dtype})z	<unknown>)	type_name	self_namemaybe_batch_shapemaybe_event_shapemaybe_dtype)	r   _str_tensorshaper   rO   
_str_dtyper0   r   r%   r-   )r   r   r   r   r)   r)   r*   __str__~  s    
zDistribution.__str__c             C   s2   dj t| j| jpdt| jt| jt| jdS )Nzo<tfp.distributions.{type_name} '{self_name}' batch_shape={batch_shape} event_shape={event_shape} dtype={dtype}>z	<unknown>)r   r   r   r   rO   )	r0   r   r%   r-   r   r   r   r   rO   )r   r)   r)   r*   __repr__  s    zDistribution.__repr__c             c   s   t | j t |z}g }|| j || jdd |tk	rd|| j|f|dkr\i n| |sr|V  dS t |}|V  W dQ R X W dQ R X W dQ R X dS )z(Helper function to standardize op scope.F)r   N)	rH   r   r-   extendr   r   UNSET_VALUE_sample_control_dependenciesZcontrol_dependencies)r   r-   rS   rj   r   depsZ
deps_scoper)   r)   r*   r     s    z$Distribution._name_and_control_scopec             C   sJ   t |}|dkrt |}ntj|t|jd}tj	||d}||fS )z-Helper to `sample` which ensures input is 1D.N)rO   )Ztensor_name)
rH   r[   Zreduce_prodnpprodr
   Zas_numpy_dtyperO   r	   Zexpand_to_vector)r   r^   r-   Zx_static_valr   r)   r)   r*   r     s    
z+Distribution._expand_sample_shape_to_vectorc                sV   | j  tj| jr8tj s8tj fdd| j tjtjt|d|| j	 S )z+Helper to `sample`; sets static shape info.c                s    S )Nr)   )_)r   r)   r*   rL     rM   z7Distribution._set_sample_static_shape.<locals>.<lambda>)r_   )
r   rH   r   rQ   rO   map_structure	functoolspartialr`   r   )r   r^   r_   r)   )r   r*   r     s    z%Distribution._set_sample_static_shapec             C   s`   t |dk	rt |dkS | }tj|jd dk	rJt |jdgkS tt|d dS )z;Implementation for `is_scalar_batch` and `is_scalar_event`.Nr   )r   r\   rH   compatZdimension_valuer]   r   equal)r   r   Zdynamic_shape_fnr]   r)   r)   r*   r     s    zDistribution._is_scalar_helperc             C   s   dS )a  Returns a list of ops to be executed in members with graph deps.

    Typically subclasses override this function to return parameter specific
    assertions (eg, positivity of `scale`, etc.).

    Args:
      is_init: Python `bool` indicating that the call site is `__init__`.

    Returns:
      dependencies: `list`-like of ops to be executed in member functions with
        graph dependencies.
    r)   r)   )r   r   r)   r)   r*   r     s    z,Distribution._parameter_control_dependenciesc             K   s   dS )a/  Returns a list of ops to be executed to validate distribution samples.

    The ops are executed in methods that take distribution samples as an
    argument (e.g. `log_prob` and `cdf`). They validate that `value` is
    within the support of the distribution. Typically subclasses override this
    function to return assertions specific to the distribution (e.g. samples
    from `Beta` must be between `0` and `1`). By convention, finite bounds of
    the support are considered valid samples, since `sample` may output values
    that are numerically equivalent to the bounds.

    Args:
      value: `float` or `double` `Tensor`.
      **kwargs: Additional keyword args.

    Returns:
      assertions: `list`-like of ops to be executed in member functions that
        take distribution samples as input.
    r)   r)   )r   rS   rj   r)   r)   r*   r     s    z)Distribution._sample_control_dependencies)NNN)r   )r   )r   )r   )r   )N)r)   Nr    )r   )r   )r   )r   )r   )r"   )r   )r   )r   )r#   )r!   )r   )r   )r   )r   )Qr%   r&   r'   r(   rd   classmethodr   r   staticmethodr   propertyr-   rO   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   r   r#   r   r!   r   r   r   r   r   r   r   r   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r   r   r)   r)   )r   r*   r   d  s    	  9'
#
















(


!c               @   s    e Zd ZdZdd Zdd ZdS )_PrettyDictz!`dict` with stable `repr`, `str`.c             C   s(   dd t |  D }dd| d S )Nc             s   s(   | ] \}}d  t|t|gV  qdS )z: N)rD   str)r;   rU   rJ   r)   r)   r*   r=     s    z&_PrettyDict.__str__.<locals>.<genexpr>{z, })sortedrX   rD   )r   pairsr)   r)   r*   r     s    z_PrettyDict.__str__c             C   s(   dd t |  D }dd| d S )Nc             s   s(   | ] \}}d  t|t|gV  qdS )z: N)rD   repr)r;   rU   rJ   r)   r)   r*   r=     s    z'_PrettyDict.__repr__.<locals>.<genexpr>r  z, r  )r  rX   rD   )r   r  r)   r)   r*   r     s    z_PrettyDict.__repr__N)r%   r&   r'   r(   r   r   r)   r)   r)   r*   r     s   r   c             C   s   t | tr tdd |  D S t | tjrt | tjsdd | D }t | tobt	| dobt	| d}|rtt
| | S t
| |S t | tjrt
| f dd |  D S | S )z/Recursively replace `dict`s with `_PrettyDict`.c             S   s   i | ]\}}t ||qS r)   ))_recursively_replace_dict_for_pretty_dict)r;   rU   rJ   r)   r)   r*   rW     s   z=_recursively_replace_dict_for_pretty_dict.<locals>.<dictcomp>c             s   s   | ]}t |V  qd S )N)r  )r;   Zx_r)   r)   r*   r=     s    z<_recursively_replace_dict_for_pretty_dict.<locals>.<genexpr>_asdict_fieldsc             S   s   i | ]\}}t ||qS r)   )r  )r;   rU   rJ   r)   r)   r*   rW     s   )r   r   r   rX   collectionsSequencesixstring_typesrm   ro   r   Mapping)r^   rg   Zis_named_tupler)   r)   r*   r  
  s    



r  c             C   s*   dd }t | } ttj|| ddS )Nc             S   s(   t | d krdS tt | ddS )N?None)r   r\   r   r   replace)r   r)   r)   r*   _str!  s    z_str_tensorshape.<locals>._str'r9   )r  r   rH   r   r   r  )r^   r  r)   r)   r*   r      s    r   c             C   s*   dd }t | } ttj|| ddS )Nc             S   s   | d krdS t | S )Nr  )r
   r-   )r   r)   r)   r*   r  ,  s    z_str_dtype.<locals>._strr  r9   )r  r   rH   r   r   r  )r^   r  r)   r)   r*   r   +  s    r   )NNN)7r(   
__future__r   r   r   abcr	  r   r   re   r1   r{   numpyr   r  Z;tensorflow_probability.python.internal.backend.numpy.compatr   rH   Z2tensorflow_probability.python.distributions._numpyr   Z;tensorflow_probability.python.distributions.internal._numpyr   Z-tensorflow_probability.python.internal._numpyr   r	   r
   r   r   Z1tensorflow_probability.python.math._numpy.genericr   Z4tensorflow_probability.python.internal.backend.numpyr   r   __all__rt   ry   objectr   r   add_metaclassABCMetaModuler$   r8   rF   rT   rY   r`   ra   r   r   r   r  r   r   r)   r)   r)   r*   <module>   s   
 
0.s         "