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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d dejZdS )z The Logistic distribution class.    )absolute_import)division)print_functionN)v2)identity)distribution)assert_util)
dtype_util)prefer_static)reparameterization)samplers)tensor_utilc                   s   e Zd ZdZd3 fdd	Zedd Zedd	 Ze	d
d Z
e	dd Zd4ddZdd Zdd Zdd Zd5ddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Z  ZS )6Logistica  The Logistic distribution with location `loc` and `scale` parameters.

  #### Mathematical details

  The cumulative density function of this distribution is:

  ```none
  cdf(x; mu, sigma) = 1 / (1 + exp(-(x - mu) / sigma))
  ```

  where `loc = mu` and `scale = sigma`.

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

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

  #### Examples

  Examples of initialization of one or a batch of distributions.

  ```python
  tfd = tfp.distributions

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

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

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

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

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

  # Arguments are broadcast when possible.
  # Define a batch of two scalar valued Logistics.
  # Both have mean 1, but different scales.
  dist = tfd.Logistic(loc=1., scale=[11, 22.])

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

  FTc          	      s~   t t }t|`}tj||gtjd}tj|d|d| _	tj|d|d| _
tt| j| j
jtj||||d W dQ R X dS )a6  Construct Logistic distributions with mean and scale `loc` and `scale`.

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

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

    Raises:
      TypeError: if loc and scale are different dtypes.
    )Z
dtype_hintloc)namedtypescale)r   Zreparameterization_typevalidate_argsallow_nan_stats
parametersr   N)dictlocalstf
name_scoper	   Zcommon_dtypefloat32r   Zconvert_nonref_to_tensor_loc_scalesuperr   __init__r   r   ZFULLY_REPARAMETERIZED)selfr   r   r   r   r   r   r   )	__class__ j/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow_probability/python/distributions/_numpy/logistic.pyr   ]   s    

zLogistic.__init__c             C   s    t tdtj| tjdgd S )N)r   r   )r      )r   zipr   convert_to_tensorint32)Zsample_shaper!   r!   r"   _param_shapes   s    zLogistic._param_shapesc             C   s   t dddS )Nr   )r   r   )r   )clsr!   r!   r"   _params_event_ndims   s    zLogistic._params_event_ndimsc             C   s   | j S )z(Distribution parameter for the location.)r   )r   r!   r!   r"   r      s    zLogistic.locc             C   s   | j S )z!Distribution parameter for scale.)r   )r   r!   r!   r"   r      s    zLogistic.scaleNc             C   s4   t t |d kr| jn|t |d kr,| jn|S )N)r
   Zbroadcast_shapeshaper   r   )r   r   r   r!   r!   r"   _batch_shape_tensor   s    zLogistic._batch_shape_tensorc             C   s   t | jj| jjS )N)r   Zbroadcast_static_shaper   r*   r   )r   r!   r!   r"   _batch_shape   s    zLogistic._batch_shapec             C   s   t jg t jdS )N)r   )r   constantr&   )r   r!   r!   r"   _event_shape_tensor   s    zLogistic._event_shape_tensorc             C   s
   t g S )N)r   ZTensorShape)r   r!   r!   r"   _event_shape   s    zLogistic._event_shapec             C   s   t | j}t | j}t |g| j||dgd}tj|t	t
| jjd| j|d}t j|t j|  }|| | S )N)r   r   r   g      ?)r*   minvalmaxvalr   seed)r   r%   r   r   concatr+   r   uniformnpZfinfor	   Zas_numpy_dtyper   Ztinymathloglog1p)r   nr2   r   r   r*   r4   Zsampledr!   r!   r"   	_sample_n   s    zLogistic._sample_nc             C   sH   t | j}t | j}|| | }| dt j|   t j| S )Ng       @)r   r%   r   r   r6   softplusr7   )r   xr   r   zr!   r!   r"   	_log_prob   s    zLogistic._log_probc             C   s   t j| |  S )N)r   r6   r;   _z)r   r<   r!   r!   r"   _log_cdf   s    zLogistic._log_cdfc             C   s   t | |S )N)r   sigmoidr?   )r   r<   r!   r!   r"   _cdf   s    zLogistic._cdfc             C   s   t j| | S )N)r   r6   r;   r?   )r   r<   r!   r!   r"   _log_survival_function   s    zLogistic._log_survival_functionc             C   s   t | | S )N)r   rA   r?   )r   r<   r!   r!   r"   _survival_function   s    zLogistic._survival_functionc             C   s,   t | j}t dt j| | j|dS )Ng       @)r   )r   r%   r   broadcast_tor6   r7   r+   )r   r   r!   r!   r"   _entropy   s    zLogistic._entropyc             C   s    t | j}t || j|dS )N)r   )r   r%   r   rE   r+   )r   r   r!   r!   r"   _mean   s    zLogistic._meanc             C   s<   t | j}t |t jtjtd |jd | j	|dS )N   )r   )r   )
r   r%   r   rE   r-   r5   pisqrtr   r+   )r   r   r!   r!   r"   _stddev   s    zLogistic._stddevc             C   s   |   S )N)rG   )r   r!   r!   r"   _mode   s    zLogistic._modec          	   C   s&   t d || j | j S Q R X dS )z)Standardize input `x` to a unit logistic.ZstandardizeN)r   r   r   r   )r   r<   r!   r!   r"   r?      s    zLogistic._zc             C   s&   | j | jtj|tj|    S )N)r   r   r   r6   r7   r8   )r   r<   r!   r!   r"   	_quantile   s    zLogistic._quantilec             C   s   t j| jdS )N)r   )identity_bijectorZIdentityr   )r   r!   r!   r"   _default_event_space_bijector   s    z&Logistic._default_event_space_bijectorc             C   sN   |rt | j| jg | js g S g }|t| jkrJ|t	j
| jdd |S )Nz"Argument `scale` must be positive.)message)r	   Zassert_same_float_dtyper   r   r   r   Zis_refr   appendr   Zassert_positive)r   Zis_initZ
assertionsr!   r!   r"   _parameter_control_dependencies   s    z(Logistic._parameter_control_dependencies)FTr   )NN)N)__name__
__module____qualname____doc__r   staticmethodr'   classmethodr)   propertyr   r   r+   r,   r.   r/   r:   r>   r@   rB   rC   rD   rF   rG   rK   rL   r?   rM   rO   rR   __classcell__r!   r!   )r    r"   r   #   s4   8  &

r   )rV   
__future__r   r   r   numpyr5   Z;tensorflow_probability.python.internal.backend.numpy.compatr   r   Z.tensorflow_probability.python.bijectors._numpyr   rN   Z2tensorflow_probability.python.distributions._numpyr   Z-tensorflow_probability.python.internal._numpyr   r	   r
   Z&tensorflow_probability.python.internalr   r   r   Distributionr   r!   r!   r!   r"   <module>   s   