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 ddlmZ ddlmZ G dd dejZeeedddZdS )zThe Weibull distribution class.    )absolute_import)division)print_functionN)v2)invert)softplus)weibull_cdf)kullback_leibler)transformed_distribution)uniform)assert_util)distribution_util)
dtype_util)tensor_utilc                   s   e Zd ZdZd  f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dd Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS )!Weibulla1  The Weibull distribution with 'concentration' and `scale` parameters.

  #### Mathematical details

  The probability density function (pdf) of this distribution is,

  ```none
  pdf(x; lambda, k) =
    k / lambda * (x / lambda) ** (k - 1) * exp(-(x / lambda) ** k)
  ```

  where `concentration = k` and `scale = lambda`.

  The cumulative density function of this distribution is,

  ```cdf(x; lambda, k) = 1 - exp(-(x / lambda) ** k)```

  The Weibull distribution includes the Exponential and Rayleigh distributions
  as special cases:

  ```Exponential(rate) = Weibull(concentration=1., 1. / rate)```

  ```Rayleigh(scale) = Weibull(concentration=2., sqrt(2.) * scale)```

  #### Examples

  Example of initialization of one distribution.

  ```python
  tfd = tfp.distributions

  # Define a single scalar Weibull distribution.
  dist = tfd.Weibull(concentration=1., scale=3.)

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

  Example of initialization of a 3-batch of distributions with varying scales
  and concentrations.

    ```python
  tfd = tfp.distributions

  # Define a 3-batch of Weibull distributions.
  scale = [1., 3., 45.]
  concentration = [2.5, 22., 7.]
  dist = tfd.Weibull(concentration=concentration, scale=scale)

  # Evaluate the cdfs at 1.
  dist.cdf(1.)    # shape: [3]
  ```
  FTc       	   	      s   t t }t|}tj||gtjd}tj|d|d}tj|d|d}t	j
|||d| _t||}tt| jtjtj||dtj||d|dtj| j|d||d	 W d
Q R X d
S )aG  Construct Weibull distributions.

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

    Args:
     concentration: Positive Float-type `Tensor`, the concentration param of the
       distribution. Must contain only positive values.
     scale: Positive Float-type `Tensor`, the scale param of the distribution.
       Must contain only positive values.
     validate_args: Python `bool` indicating whether arguments should be checked
       for correctness.
     allow_nan_stats: Python `bool` indicating whether nan values should be
       allowed.
     name: Python `str` name given to ops managed by this class.
       Default value: `'Weibull'`.

    Raises:
      TypeError: if concentration and scale are different dtypes.

    )Z
dtype_hintconcentration)namedtypescale)r   r   validate_args)r   )lowhighallow_nan_stats)r   )distributionZbijector
parametersr   N)dictlocalstf
name_scoper   Zcommon_dtypefloat32r   Zconvert_nonref_to_tensorweibull_cdf_bijectorZ
WeibullCDF_weibull_bijectorr   Zget_broadcast_shapesuperr   __init__r   ZUniformzerosZonesinvert_bijectorZInvert)	selfr   r   r   r   r   r   r   Zbatch_shape)	__class__ i/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow_probability/python/distributions/_numpy/weibull.pyr#   \   s(    




zWeibull.__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Weibull._param_shapesc             C   s   t dddS )Nr   )r   r   )r   )clsr(   r(   r)   _params_event_ndims   s    zWeibull._params_event_ndimsc             C   s   | j jS )z-Distribution parameter for the concentration.)r!   r   )r&   r(   r(   r)   r      s    zWeibull.concentrationc             C   s   | j jS )z!Distribution parameter for scale.)r!   r   )r&   r(   r(   r)   r      s    zWeibull.scalec             C   s`   t jd| jd}t | j}t jtj| jd}||t j|  t j	| j
 t j	| | S )Ng      ?)r   )r   constantr   r,   r   npeuler_gammamath
reciprocallogr   )r&   oner   r3   r(   r(   r)   _entropy   s    zWeibull._entropyc             C   s   | j j|ddS )Nr   )Zevent_ndims)r!   Zforward_log_det_jacobian)r&   xr(   r(   r)   	_log_prob   s    zWeibull._log_probc          	   C   s6   t jd| jd}| jt t j|t j| j  S )Ng      ?)r   )	r   r1   r   r   expr4   lgammar5   r   )r&   r7   r(   r(   r)   _mean   s    zWeibull._meanc             C   st   t | j}t jd| jd}t jd| jd}t | jt t j	|||  t |t j	|t j
|    S )Ng      ?)r   g       @)r   r,   r   r1   r   Zsquarer   r;   r4   r<   r5   )r&   r   r7   twor(   r(   r)   	_variance   s    
zWeibull._variancec             C   sv   t | j}t jd| jd}t jd| jd}| jt jt t j	|||  t |t j	|t j
|    S )Ng      ?)r   g       @)r   r,   r   r1   r   r   r4   sqrtr;   r<   r5   )r&   r   r7   r>   r(   r(   r)   _stddev   s    
zWeibull._stddevc             C   s:   t jd| jd}t | j}|| | t j| | j S )Ng      ?)r   )r   r1   r   r,   r   r4   r5   r   )r&   r7   r   r(   r(   r)   _mode   s    
zWeibull._modec             C   s   | j |S )N)r!   _parameter_control_dependencies)r&   Zis_initr(   r(   r)   rC      s    z'Weibull._parameter_control_dependenciesc             C   s&   g }| j s|S |tj|dd |S )z Checks the validity of a sample.zSample must be non-negative.)message)r   appendr   Zassert_non_negative)r&   r9   Z
assertionsr(   r(   r)   _sample_control_dependencies   s    z$Weibull._sample_control_dependenciesc             C   s   t j| jdS )N)r   )softplus_bijectorZSoftplusr   )r&   r(   r(   r)   _default_event_space_bijector   s    z%Weibull._default_event_space_bijector)FTr   )__name__
__module____qualname____doc__r#   staticmethodr.   classmethodr0   propertyr   r   r8   r:   r=   r?   rA   rB   rC   rF   rH   __classcell__r(   r(   )r'   r)   r   %   s"   5  2	
r   c          	   C   s   t |p
d t | j}t |j}t | j}t |j}t j||t j|  t j||t j|   || t j|tj|    || | t 	t j
|| d   d S Q R X dS )a9  Calculate the batched KL divergence KL(a || b) with a and b Weibull.

  Args:
    a: instance of a Weibull distribution object.
    b: instance of a Weibull distribution object.
    name: (optional) Name to use for created operations. default is
      'kl_weibull_weibull'.

  Returns:
    Batchwise KL(a || b)
  Zkl_weibull_weibullg      ?N)r   r   r,   r   r   r4   r6   r2   r3   r;   r<   )abr   Za_concentrationZb_concentrationZa_scaleZb_scaler(   r(   r)   _kl_weibull_weibull   s    	zrS   )N)rL   
__future__r   r   r   numpyr2   Z;tensorflow_probability.python.internal.backend.numpy.compatr   r   Z.tensorflow_probability.python.bijectors._numpyr   r%   r   rG   r   r    Z2tensorflow_probability.python.distributions._numpyr	   r
   r   Z-tensorflow_probability.python.internal._numpyr   r   r   r   ZTransformedDistributionr   Z
RegisterKLrS   r(   r(   r(   r)   <module>   s&    :
