B
    `y                 @   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 Chi distribution class.    )absolute_import)division)print_functionN)v2)_numpy)invert)softplus)square)chi2)kullback_leibler)transformed_distribution)assert_util)
dtype_util)tensor_utilc                   sl   e Zd ZdZd fdd	Zedd Zedd	 ZdddZ	dd Z
dd Zdd Zdd Zdd Z  ZS )Chia  Chi distribution.

  The Chi distribution is defined over nonnegative real numbers and uses a
  degrees of freedom ('df') parameter.

  #### Mathematical Details

  The probability density function (pdf) is,

  ```none
  pdf(x; df, x >= 0) = x**(df - 1) exp(-0.5 x**2) / Z
  Z = 2**(0.5 df - 1) Gamma(0.5 df)
  ```

  where:

  * `df` denotes the degrees of freedom,
  * `Z` is the normalization constant, and,
  * `Gamma` is the [gamma function](
    https://en.wikipedia.org/wiki/Gamma_function).

  The Chi distribution is a transformation of the Chi2 distribution; it is the
  distribution of the positive square root of a variable obeying a Chi2
  distribution.

  FTc          	      s~   t t }t|`}tj|gtjd}tj|d|d| _	t
t| jtj| j	||dttj|d|||d W dQ R X dS )aB  Construct Chi distributions with parameter `df`.

    Args:
      df: Floating point tensor, the degrees of freedom of the
        distribution(s). `df` must contain only positive values.
      validate_args: Python `bool`, default `False`. When `True` distribution
        parameters are checked for validity despite possibly degrading runtime
        performance. When `False` invalid inputs may silently render incorrect
        outputs.
      allow_nan_stats: Python `bool`, default `True`. When `True`, statistics
        (e.g., mean, mode, variance) use the value `NaN` to indicate the result
        is undefined. When `False`, an exception is raised if one or more of the
        statistic's batch members are undefined.
      name: Python `str` name prefixed to Ops created by this class.
        Default value: `'Chi'`.
    )Z
dtype_hintdf)namedtype)r   validate_argsallow_nan_stats)r   )distributionZbijectorr   
parametersr   N)dictlocalstf
name_scoper   Zcommon_dtypefloat32r   Zconvert_nonref_to_tensor_dfsuperr   __init__r
   ZChi2invert_bijectorZInvertsquare_bijectorZSquare)selfr   r   r   r   r   r   )	__class__ e/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow_probability/python/distributions/_numpy/chi.pyr   @   s    

zChi.__init__c             C   s
   t ddS )Nr   )r   )r   )clsr$   r$   r%   _params_event_ndimsd   s    zChi._params_event_ndimsc             C   s   | j S )z.Distribution parameter for degrees of freedom.)r   )r"   r$   r$   r%   r   h   s    zChi.dfNc             C   s:   t |d kr| jn|}tdt tdd|   S )Ng       @g      ?)r   convert_to_tensorr   npsqrtexptfp_mathZlog_gamma_difference)r"   r   r$   r$   r%   _meanm   s    z	Chi._meanc             C   s   t | j}|| |d  S )N   )r   r(   r   r-   )r"   r   r$   r$   r%   	_variancer   s    zChi._variancec             C   sF   t | j}t jd| d|td |d t jd|     S )Ng      ?g       @g      ?)r   r(   r   mathlgammar)   logdigamma)r"   r   r$   r$   r%   _entropyv   s    zChi._entropyc             C   s   t j| jdS )N)r   )softplus_bijectorZSoftplusr   )r"   r$   r$   r%   _default_event_space_bijector|   s    z!Chi._default_event_space_bijectorc             C   s8   | j s
g S g }|t| jkr4|tj| jdd |S )NzArgument `df` must be positive.)message)r   r   Zis_refr   appendr   Zassert_positive)r"   Zis_init
assertionsr$   r$   r%   _parameter_control_dependencies   s    z#Chi._parameter_control_dependenciesc             C   s&   g }| j s|S |tj|dd |S )NzSample must be non-negative.)r7   )r   r8   r   Zassert_non_negative)r"   xr9   r$   r$   r%   _sample_control_dependencies   s    z Chi._sample_control_dependencies)FTr   )N)__name__
__module____qualname____doc__r   classmethodr'   propertyr   r-   r/   r4   r6   r:   r<   __classcell__r$   r$   )r#   r%   r   $   s      
	r   c          	   C   sn   t |p
dV t | j}t |j}dt jd|  ||  t jd|  t jd|  S Q R X dS )a%  Calculate the batched KL divergence KL(a || b) with a and b Chi.

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

  Returns:
    Batchwise KL(a || b)
  Z
kl_chi_chig      ?N)r   r   r(   r   r0   r3   r1   )abr   Za_dfZb_dfr$   r$   r%   _kl_chi_chi   s    rF   )N) r@   
__future__r   r   r   numpyr)   Z;tensorflow_probability.python.internal.backend.numpy.compatr   r   Z"tensorflow_probability.python.mathr   r,   Z.tensorflow_probability.python.bijectors._numpyr   r    r   r5   r	   r!   Z2tensorflow_probability.python.distributions._numpyr
   r   r   Z-tensorflow_probability.python.internal._numpyr   r   r   ZTransformedDistributionr   Z
RegisterKLrF   r$   r$   r$   r%   <module>   s$   m
