B
    `iS                 @   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 ddlmZ ddlmZ ddlmZ ddlmZ dZdd Zd,ddZ dde	j!ddfddZ"dde	j!ddfddZ#e	j$dddde	j!ddfd d!Z%G d"d# d#ej&Z'd$d% Z(d&d' Z)d(d) Z*d*d+ Z+dS )-z The Binomial distribution class.    )absolute_import)division)print_functionN)v2)_numpy)distribution)exponential)assert_util)batched_rejection_sampler)distribution_util)
dtype_util)implementation_selection)prefer_static)reparameterization)samplers)tensor_util)tensorshape_utila5  
For each batch member of counts `value`, `P[value]` is the probability that
after sampling `self.total_count` draws from this Binomial distribution, the
number of successes is `value`. Since different sequences of draws can result in
the same counts, the probability includes a combinatorial coefficient.

Note: `value` must be a non-negative tensor with dtype `dtype` and whose shape
can be broadcast with `self.probs` and `self.total_count`. `value` is only legal
if it is less than or equal to `self.total_count` and its components are equal
to integer values.
c             C   sT   t ||  }t | |}t ||||  }t jj|| d d| d}t |||S )zThe binomial cumulative distribution function.

  Args:
    k: floating point `Tensor`.
    n: floating point `Tensor`.
    p: floating point `Tensor`.

  Returns:
    `sum_{j=0}^k p^j (1 - p)^(n - j)`.
     )abx)tfZ	ones_likeequalwheremathZbetainc)knpZonesZk_eq_nZsafe_dnZdk r   j/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow_probability/python/distributions/_numpy/binomial.py_bdtr5   s
    r    r   c       	         s   t t j|tgddtdd  		d	  tjt jddddkrxt tj	  dkrt tj	 	fd	d
} fdd}t 
tj|||dd S )a  Utility for rejection sampling from log-concave discrete distributions.

  This utility constructs an easy-to-sample-from upper bound for a discrete
  univariate log-concave distribution (for discrete univariate distributions, a
  necessary and sufficient condition is p_k^2 >= p_{k-1} p_{k+1} for all k).
  The method requires that the mode of the distribution is known. While a better
  method can likely be derived for any given distribution, this method is
  general and easy to implement. The expected number of iterations is bounded by
  4+m, where m is the probability of the mode. For details, see [(Devroye,
  1979)][1].

  Args:
    mode: Tensor, the mode[s] of the [batch of] distribution[s].
    prob_fn: Python callable, counts -> prob(counts).
    dtype: DType of the generated samples.
    sample_shape: 0D or 1D `int32` `Tensor`. Shape of the generated samples.
    distribution_minimum: Tensor of type `dtype`. The minimum value
      taken by the distribution. The `prob` method will only be called on values
      greater than equal to the specified minimum. The shape must broadcast with
      the batch shape of the distribution. If unspecified, the domain is treated
      as unbounded below.
    distribution_maximum: Tensor of type `dtype`. The maximum value
      taken by the distribution. See `distribution_minimum` for details.
    seed: Python integer or `Tensor` instance, for seeding PRNG.

  Returns:
    samples: a `Tensor` with prepended dimensions `sample_shape`.

  #### References

  [1] Luc Devroye. A Simple Generator for Discrete Log-Concave
      Distributions. Computing, 1987.

  [2] Dillon et al. TensorFlow Distributions. 2017.
      https://arxiv.org/abs/1711.10604
  r   )axisg      ?g       @r   )dtype)ZrateNc                s   t j| ddd\}}}}t j| d}|  }j|d}| }|  }	t j| d}
t|
}t|||	}tt	j
| d| }| }t||}||fS )z+Proposal for log-concave rejection sampler.   Z&log_concave_rejection_sampler_proposal)r   salt)seedr"   )r%   )r   Z
split_seeduniformsampleprobr   Z
less_equalr   round
tfp_randomZ
rademacher)r%   Ztop_lobe_fractions_seedZexponential_samples_seedZtop_selector_seedZrademacher_seedZtop_lobe_fractionsZtop_offsetsZexponential_samplesZexponential_heightZexponential_offsetsZtop_selectorZon_top_maskZunsigned_offsetsoffsetsZpotential_samplesZenvelope_height)r"   exponential_distributionmodemode_height
mode_shapetop_fraction	top_widthr   r   proposal   s*    

z0_log_concave_rejection_sampler.<locals>.proposalc                s0   | k|  k@ }t || d}t ||dS )Ng        )r   r   )valuesZin_range_maskZin_range_values)distribution_maximumdistribution_minimumprob_fnr   r   target   s    z._log_concave_rejection_sampler.<locals>.target)r   Zbroadcast_toconcatr   shaper   ZExponentialconstantnpinfZstop_gradientr
   )	r-   r6   r"   sample_shaper5   r4   r%   r2   r7   r   )
r4   r5   r"   r,   r-   r.   r/   r6   r0   r1   r   _log_concave_rejection_samplerK   s"    ,
!r>   c       	   	   C   s|   t |p
dd |dkr2t |dkt j|d}tt|t|}t jj	t j
| |gdd||||d}W dQ R X |S )z3Sample using *fast* `tf.random.stateless_binomial`.Zbinomial_cpuNr   )r!   )r9   r%   countsprobsoutput_dtype)r   
name_scoper   r   sigmoidr   Zbroadcast_shaper9   randomZstateless_binomialr8   )	r9   r?   r@   logitsrA   r%   nameZbatch_shapesamplesr   r   r   _random_binomial_cpu   s    	rH   c       	   
   C   sb   t |p
dJ t|||d}t| |j|j| t jd|jd||d}t ||}W dQ R X |S )z9Sample using XLA-friendly python-based rejection sampler.Zbinomial_noncpu)total_countrE   r@   r   )r"   )r6   r"   r=   r5   r4   r%   N)	r   rB   Binomialr>   r-   r(   r"   zeroscast)	r9   r?   r@   rE   rA   r%   rF   distrG   r   r   r   _random_binomial_noncpu   s    	
rN   F)Z	autographc       	   
   C   sf   t |p
dN t|}t j| t jdd} t| ||||||d}tjdt	t
d}|f |S Q R X dS )a'  Sample a binomial, CPU specialized to stateless_binomial.

  Args:
    shape: Shape of the full sample output. Trailing dims should match the
      broadcast shape of `counts` with `probs|logits`.
    counts: Batch of total_count.
    probs: Batch of p(success).
    logits: Batch of log-odds(success).
    output_dtype: DType of samples.
    seed: int or Tensor seed.
    name: Optional name for related ops.

  Returns:
    samples: Samples from binomial distributions.
    runtime_used_for_sampling: One of `implementation_selection._RUNTIME_*`.
  Zrandom_binomialr9   )Z
dtype_hintrF   )r9   r?   r@   rE   rA   r%   rF   binomial)fn_nameZ
default_fnZcpu_fnN)r   rB   r   sanitize_seedconvert_to_tensorint32dictr   Zimplementation_selectingrN   rH   )	r9   r?   r@   rE   rA   r%   rF   paramsZsampler_implr   r   r   _random_binomial   s    

rV   c                   s  e Zd ZdZd4 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eedd Zeedd Zdd Zeed5ddZd6dd Zd!d" Zed#d$d% Zd7d&d'Zd(d) Zd8d*d+Zd9d,d-Zd.d/ Zd0d1 Zd2d3 Z  Z S ):rJ   a9  Binomial distribution.

  This distribution is parameterized by `probs`, a (batch of) probabilities for
  drawing a `1`, and `total_count`, the number of trials per draw from the
  Binomial.

  #### Mathematical Details

  The Binomial is a distribution over the number of `1`'s in `total_count`
  independent trials, with each trial having the same probability of `1`, i.e.,
  `probs`.

  The probability mass function (pmf) is,

  ```none
  pmf(k; n, p) = p**k (1 - p)**(n - k) / Z
  Z = k! (n - k)! / n!
  ```

  where:
  * `total_count = n`,
  * `probs = p`,
  * `Z` is the normalizing constant, and,
  * `n!` is the factorial of `n`.

  #### Examples

  Create a single distribution, corresponding to 5 coin flips.

  ```python
  dist = Binomial(total_count=5., probs=.5)
  ```

  Create a single distribution (using logits), corresponding to 5 coin flips.

  ```python
  dist = Binomial(total_count=5., logits=0.)
  ```

  Creates 3 distributions with the third distribution most likely to have
  successes.

  ```python
  p = [.2, .3, .8]
  # n will be broadcast to [4., 4., 4.], to match p.
  dist = Binomial(total_count=4., probs=p)
  ```

  The distribution functions can be evaluated on counts.

  ```python
  # counts same shape as p.
  counts = [1., 2, 3]
  dist.prob(counts)  # Shape [3]

  # p will be broadcast to [[.2, .3, .8], [.2, .3, .8]] to match counts.
  counts = [[1., 2, 1], [2, 2, 4]]
  dist.prob(counts)  # Shape [2, 3]

  # p will be broadcast to shape [5, 7, 3] to match counts.
  counts = [[...]]  # Shape [5, 7, 3]
  dist.prob(counts)  # Shape [5, 7, 3]
  ```
  NFTc       	   	      s   t t }|dk|dkkr"tdt|p,dn}t|||gtj}tj	||dd| _
tj	||dd| _tj	||dd| _tt| j|tj||||d W dQ R X dS )	aR  Initialize a batch of Binomial distributions.

    Args:
      total_count: Non-negative floating point tensor with shape broadcastable
        to `[N1,..., Nm]` with `m >= 0` and the same dtype as `probs` or
        `logits`. Defines this as a batch of `N1 x ...  x Nm` different Binomial
        distributions. Its components should be equal to integer values.
      logits: Floating point tensor representing the log-odds of a
        positive event with shape broadcastable to `[N1,..., Nm]` `m >= 0`, and
        the same dtype as `total_count`. Each entry represents logits for the
        probability of success for independent Binomial distributions. Only one
        of `logits` or `probs` should be passed in.
      probs: Positive floating point tensor with shape broadcastable to
        `[N1,..., Nm]` `m >= 0`, `probs in [0, 1]`. Each entry represents the
        probability of success for independent Binomial distributions. Only one
        of `logits` or `probs` should be passed in.
      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.
    Nz<Construct `Binomial` with `probs` or `logits`, but not both.rJ   rI   )r"   rF   rE   r@   )r"   Zreparameterization_typevalidate_argsallow_nan_stats
parametersrF   )rT   locals
ValueErrorr   rB   r   Zcommon_dtypefloat32r   Zconvert_nonref_to_tensor_total_count_logits_probssuperrJ   __init__r   ZNOT_REPARAMETERIZED)	selfrI   rE   r@   rW   rX   rF   rY   r"   )	__class__r   r   ra   C  s&    !

zBinomial.__init__c             C   s   t ddddS )Nr   )rI   rE   r@   )rT   )clsr   r   r   _params_event_ndimsx  s    zBinomial._params_event_ndimsc             C   s   | j S )zNumber of trials.)r]   )rb   r   r   r   rI   |  s    zBinomial.total_countc             C   s   | j S )zInput argument `logits`.)r^   )rb   r   r   r   rE     s    zBinomial.logitsc             C   s   | j S )zInput argument `probs`.)r_   )rb   r   r   r   r@     s    zBinomial.probsc             C   s0   | j d kr| jn| j }tt| jt|S )N)r^   r_   r   Zbroadcast_dynamic_shaper9   r]   )rb   r   r   r   r   _batch_shape_tensor  s    zBinomial._batch_shape_tensorc             C   s(   | j d kr| jn| j }t| jj|jS )N)r^   r_   r   Zbroadcast_static_shaperI   r9   )rb   r   r   r   r   _batch_shape  s    zBinomial._batch_shapec             C   s   t jg t jdS )N)r"   )r   r:   rS   )rb   r   r   r   _event_shape_tensor  s    zBinomial._event_shape_tensorc             C   s
   t g S )N)r   ZTensorShape)rb   r   r   r   _event_shape  s    zBinomial._event_shapec             C   sF   t | j}| jd k	r&t| j||}nt| j||}t||}|| S )N)r   rR   rI   r^   _log_unnormalized_prob_logits_log_unnormalized_prob_probsr_   _log_normalization)rb   r?   rI   ZunnormZnormr   r   r   	_log_prob  s    

zBinomial._log_probc             C   s   t | |S )N)r   exprm   )rb   r?   r   r   r   _prob  s    zBinomial._probc             C   s4   t | j}| j|d}t||\}}t|||dS )N)rI   )r   r   r   )r   rR   rI   _probs_parameter_no_checks_maybe_broadcastr    )rb   r?   rI   r@   r   r   r   _cdf  s    zBinomial._cdfc             C   sd   t j|dd}tt|gt| j| jd kr2d n
t| j| jd krJd n
t| j| j|dd S )NrO   )r$   )r9   r?   r@   rE   rA   r%   r   )	r   rQ   rV   r   rR   r]   r_   r^   r"   )rb   r   r%   r   r   r   	_sample_n  s    



zBinomial._sample_nc             C   s0   |d krt | j}|d kr(| j|d}|| S )N)rI   )r   rR   r]   rp   )rb   r@   rI   r   r   r   _mean  s
    zBinomial._meanc             C   s.   t | j}| j|d}| j||dd|  S )N)rI   )r@   rI   g      ?)r   rR   r]   rp   rt   )rb   rI   r@   r   r   r   	_variance  s    zBinomial._variancezNote that when `(1 + total_count) * probs` is an integer, there are
      actually two modes. Namely, `(1 + total_count) * probs` and
      `(1 + total_count) * probs - 1` are both modes. Here we return only the
      larger of the two modes.c             C   s4   t | j}| j|d}t j|t d| | S )N)rI   g      ?)r   rR   r]   rp   r   Zminimumfloor)rb   rI   r@   r   r   r   _mode  s    zBinomial._modec          	   C   s"   |  |p
d
 |  S Q R X dS )z@Logits computed from non-`None` input arg (`probs` or `logits`).logits_parameterN)_name_and_control_scope_logits_parameter_no_checks)rb   rF   r   r   r   rx     s    zBinomial.logits_parameterc             C   s<   | j d kr0t| j}tj|tj|  S t| j S )N)r^   r   rR   r_   r   loglog1pidentity)rb   r@   r   r   r   rz     s    
z$Binomial._logits_parameter_no_checksc          	   C   s"   |  |p
d
 |  S Q R X dS )z?Probs computed from non-`None` input arg (`probs` or `logits`).probs_parameterN)ry   rp   )rb   rF   r   r   r   r~     s    zBinomial.probs_parameterc             C   sF   | j d krt| j}ntj| j }|d kr4| j}t|dk|dS )Nr   )r^   r   r}   r_   r   rC   rI   r   )rb   rI   r@   r   r   r   rp     s    
z#Binomial._probs_parameter_no_checksc             C   s   d S )Nr   )rb   r   r   r   _default_event_space_bijector  s    z&Binomial._default_event_space_bijectorc             C   s   | j s
g S g }|t| jkrRt| j}d}d}|tj||dtj	||dg7 }| j
d k	r|t| j
krt| j
}td|j}|tj|ddtj||ddg7 }|S )Nz,Argument `total_count` must be non-negative.z<Argument `total_count` cannot contain fractional components.)messageg      ?z!probs has components less than 0.z$probs has components greater than 1.)rW   r   Zis_refrI   r   rR   r	   Zassert_non_negativer   Zassert_integer_formr_   r:   r"   assert_less_equal)rb   Zis_init
assertionsrI   Zmsg1Zmsg2r@   Zoner   r   r   _parameter_control_dependencies  s(    
z(Binomial._parameter_control_dependenciesc             C   s:   g }| j s|S |t| |tj|| jdd |S )zCheck counts for proper values.zNSampled counts must be itemwise less than or equal to `total_count` parameter.)r   )rW   extendr   Zassert_nonnegative_integer_formappendr	   r   rI   )rb   r?   r   r   r   r   _sample_control_dependencies  s    
z%Binomial._sample_control_dependencies)NNFTN)N)NN)N)N)N)!__name__
__module____qualname____doc__ra   classmethodre   propertyrI   rE   r@   rf   rg   rh   ri   r   ZAppendDocstring_binomial_sample_noterm   ro   rr   rs   rt   ru   rw   rx   rz   r~   rp   r   r   r   __classcell__r   r   )rc   r   rJ     s<   @    /




rJ   c             C   s>   t | } t jt j|  | t jt j| ||  S )z)Log unnormalized probability from logits.)r   rR   r   multiply_no_nanZsoftplus)rE   r?   rI   r   r   r   rj     s    
rj   c             C   s<   t | } t jt j| |t jt j|  ||  S )z(Log unnormalized probability from probs.)r   rR   r   r   r{   r|   )r@   r?   rI   r   r   r   rk   #  s    
rk   c             C   s(   t d|  d| |  tjd|  S )Ng      ?)tfp_mathZlbetar   r   r{   )r?   rI   r   r   r   rl   +  s    rl   c             C   sL   t | jr(t |jr(t | j|jsD|t|  }| t| } | |fS )N)r   Zis_fully_definedr9   Zis_compatible_withr   Z
zeros_like)r   r   r   r   r   rq   0  s    rq   )r   NNN),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.randomr*   Z2tensorflow_probability.python.distributions._numpyr   r   Z-tensorflow_probability.python.internal._numpyr	   r
   r   r   r   r   Z&tensorflow_probability.python.internalr   r   r   r   r   r    r>   r\   rH   rN   functionrV   DistributionrJ   rj   rk   rl   rq   r   r   r   r   <module>   s`      
d
  