o
    i eY5                     @   s   d 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gZdZedgdG dd dejZdS )z,The DirichletMultinomial distribution class.    )dtypes)ops)	array_ops)	check_ops)control_flow_ops)math_ops)
random_ops)special_math_ops)distribution)util)deprecation)	tf_exportDirichletMultinomiala  For each batch of counts,
`value = [n_0, ..., n_{K-1}]`, `P[value]` is the probability that after
sampling `self.total_count` draws from this Dirichlet-Multinomial distribution,
the number of draws falling in class `j` is `n_j`. Since this definition is
[exchangeable](https://en.wikipedia.org/wiki/Exchangeable_random_variables);
different sequences have the same counts so the probability includes a
combinatorial coefficient.

Note: `value` must be a non-negative tensor with dtype `self.dtype`, have no
fractional components, and such that
`tf.reduce_sum(value, -1) = self.total_count`. Its shape must be broadcastable
with `self.concentration` and `self.total_count`.z"distributions.DirichletMultinomial)v1c                       s   e Zd ZdZejdddd			 d+ f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dZeedd Zeedd Zdd Zed d!d" Zd#d$ Zd%d& Zd'd( Zd)d* Z  ZS )-r   aX  Dirichlet-Multinomial compound distribution.

  The Dirichlet-Multinomial distribution is parameterized by a (batch of)
  length-`K` `concentration` vectors (`K > 1`) and a `total_count` number of
  trials, i.e., the number of trials per draw from the DirichletMultinomial. It
  is defined over a (batch of) length-`K` vector `counts` such that
  `tf.reduce_sum(counts, -1) = total_count`. The Dirichlet-Multinomial is
  identically the Beta-Binomial distribution when `K = 2`.

  #### Mathematical Details

  The Dirichlet-Multinomial is a distribution over `K`-class counts, i.e., a
  length-`K` vector of non-negative integer `counts = n = [n_0, ..., n_{K-1}]`.

  The probability mass function (pmf) is,

  ```none
  pmf(n; alpha, N) = Beta(alpha + n) / (prod_j n_j!) / Z
  Z = Beta(alpha) / N!
  ```

  where:

  * `concentration = alpha = [alpha_0, ..., alpha_{K-1}]`, `alpha_j > 0`,
  * `total_count = N`, `N` a positive integer,
  * `N!` is `N` factorial, and,
  * `Beta(x) = prod_j Gamma(x_j) / Gamma(sum_j x_j)` is the
    [multivariate beta function](
    https://en.wikipedia.org/wiki/Beta_function#Multivariate_beta_function),
    and,
  * `Gamma` is the [gamma function](
    https://en.wikipedia.org/wiki/Gamma_function).

  Dirichlet-Multinomial is a [compound distribution](
  https://en.wikipedia.org/wiki/Compound_probability_distribution), i.e., its
  samples are generated as follows.

    1. Choose class probabilities:
       `probs = [p_0,...,p_{K-1}] ~ Dir(concentration)`
    2. Draw integers:
       `counts = [n_0,...,n_{K-1}] ~ Multinomial(total_count, probs)`

  The last `concentration` dimension parametrizes a single Dirichlet-Multinomial
  distribution. When calling distribution functions (e.g., `dist.prob(counts)`),
  `concentration`, `total_count` and `counts` are broadcast to the same shape.
  The last dimension of `counts` corresponds single Dirichlet-Multinomial
  distributions.

  Distribution parameters are automatically broadcast in all functions; see
  examples for details.

  #### Pitfalls

  The number of classes, `K`, must not exceed:
  - the largest integer representable by `self.dtype`, i.e.,
    `2**(mantissa_bits+1)` (IEE754),
  - the maximum `Tensor` index, i.e., `2**31-1`.

  In other words,

  ```python
  K <= min(2**31-1, {
    tf.float16: 2**11,
    tf.float32: 2**24,
    tf.float64: 2**53 }[param.dtype])
  ```

  Note: This condition is validated only when `self.validate_args = True`.

  #### Examples

  ```python
  alpha = [1., 2., 3.]
  n = 2.
  dist = DirichletMultinomial(n, alpha)
  ```

  Creates a 3-class distribution, with the 3rd class is most likely to be
  drawn.
  The distribution functions can be evaluated on counts.

  ```python
  # counts same shape as alpha.
  counts = [0., 0., 2.]
  dist.prob(counts)  # Shape []

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

  # alpha will be broadcast to shape [5, 7, 3] to match counts.
  counts = [[...]]  # Shape [5, 7, 3]
  dist.prob(counts)  # Shape [5, 7]
  ```

  Creates a 2-batch of 3-class distributions.

  ```python
  alpha = [[1., 2., 3.], [4., 5., 6.]]  # Shape [2, 3]
  n = [3., 3.]
  dist = DirichletMultinomial(n, alpha)

  # counts will be broadcast to [[2., 1., 0.], [2., 1., 0.]] to match alpha.
  counts = [2., 1., 0.]
  dist.prob(counts)  # Shape [2]
  ```

  z
2019-01-01zThe TensorFlow Distributions library has moved to TensorFlow Probability (https://github.com/tensorflow/probability). You should update all references to use `tfp.distributions` instead of `tf.distributions`.T)	warn_onceFc              	      s   t t }tj|||gd-}tj|dd| _|r t| j| _| tj|dd|| _	t
| j	d| _W d   n1 s>w   Y  tt| j| j	j||tj|| j| j	g|d dS )a  Initialize a batch of DirichletMultinomial distributions.

    Args:
      total_count:  Non-negative floating point tensor, whose dtype is the same
        as `concentration`. The shape is broadcastable to `[N1,..., Nm]` with
        `m >= 0`. Defines this as a batch of `N1 x ... x Nm` different
        Dirichlet multinomial distributions. Its components should be equal to
        integer values.
      concentration: Positive floating point tensor, whose dtype is the
        same as `n` with shape broadcastable to `[N1,..., Nm, K]` `m >= 0`.
        Defines this as a batch of `N1 x ... x Nm` different `K` class Dirichlet
        multinomial distributions.
      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.
    )valuestotal_count)nameconcentrationN)dtypevalidate_argsallow_nan_statsreparameterization_type
parametersgraph_parentsr   )dictlocalsr   
name_scopeconvert_to_tensor_total_countdistribution_util$embed_check_nonnegative_integer_form!_maybe_assert_valid_concentration_concentrationr   
reduce_sum_total_concentrationsuperr   __init__r   r
   NOT_REPARAMETERIZED)selfr   r   r   r   r   r   	__class__ h/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/ops/distributions/dirichlet_multinomial.pyr(      s6   
$	

zDirichletMultinomial.__init__c                 C      | j S )z,Number of trials used to construct a sample.)r    r*   r-   r-   r.   r         z DirichletMultinomial.total_countc                 C   r/   )zCConcentration parameter; expected prior counts for that coordinate.)r$   r0   r-   r-   r.   r      r1   z"DirichletMultinomial.concentrationc                 C   r/   )z+Sum of last dim of concentration parameter.)r&   r0   r-   r-   r.   total_concentration   r1   z(DirichletMultinomial.total_concentrationc                 C   s   t | jS N)r   shaper2   r0   r-   r-   r.   _batch_shape_tensor   s   z(DirichletMultinomial._batch_shape_tensorc                 C   s
   | j  S r3   )r2   	get_shaper0   r-   r-   r.   _batch_shape   s   
z!DirichletMultinomial._batch_shapec                 C   s   t | jdd  S )Nr   )r   r4   r   r0   r-   r-   r.   _event_shape_tensor   s   z(DirichletMultinomial._event_shape_tensorc                 C   s   | j  ddd  S )N   r   )r   r6   with_rank_at_leastr0   r-   r-   r.   _event_shape   s   z!DirichletMultinomial._event_shapeNc           	   	   C   s   t j| jtjd}|  d }tjt t	j
|g| j| j|dd|gd}t	j||tj|ddd}t tj||d	d
}t|g|  |ggd}t||}t || jS )N)r   r   )r4   alphar   seedr   )r4   dirichlet_multinomial)salt)logitsnum_samplesr=   )depth)r   castr   r   int32event_shape_tensorr   reshapelogr   random_gammar   r   multinomialr!   gen_new_seedr%   one_hotconcatbatch_shape_tensor)	r*   nr=   n_drawskunnormalized_logitsdrawsxfinal_shaper-   r-   r.   	_sample_n  s(   zDirichletMultinomial._sample_nc                 C   s8   |  |}t| j| t| j }|t| j| S r3   )_maybe_assert_valid_sampler	   lbetar   r!   log_combinationsr   )r*   countsordered_probr-   r-   r.   	_log_prob  s   

zDirichletMultinomial._log_probc                 C   s   t | |S r3   )r   expr\   r*   rZ   r-   r-   r.   _prob  s   zDirichletMultinomial._probc                 C   s   | j | j| jdtjf   S N.)r   r   r2   r   newaxisr0   r-   r-   r.   _mean!  s   zDirichletMultinomial._meana  The covariance for each batch member is defined as the following:

      ```none
      Var(X_j) = n * alpha_j / alpha_0 * (1 - alpha_j / alpha_0) *
      (n + alpha_0) / (1 + alpha_0)
      ```

      where `concentration = alpha` and
      `total_concentration = alpha_0 = sum_j alpha_j`.

      The covariance between elements in a batch is defined as:

      ```none
      Cov(X_i, X_j) = -n * alpha_i * alpha_j / alpha_0 ** 2 *
      (n + alpha_0) / (1 + alpha_0)
      ```
      c              
   C   sD   |   |   }tt|dtjf |dtjd d f  |  S r`   )_variance_scale_termrb   r   matrix_set_diagr   matmulra   	_variance)r*   rT   r-   r-   r.   _covariance%  s   z DirichletMultinomial._covariancec                 C   s&   |   }||   }|| j| |  S r3   )rc   rb   r   )r*   scalerT   r-   r-   r.   rf   @  s   zDirichletMultinomial._variancec                 C   s,   | j dtjf }td|| j  d|  S )zFHelper to `_covariance` and `_variance` which computes a shared scale..g      ?)r2   r   ra   r   sqrtr   )r*   c0r-   r-   r.   rc   E  s   z)DirichletMultinomial._variance_scale_termc                 C   s*   |s|S t |}ttj|ddg|S )z3Checks the validity of the concentration parameter.z)Concentration parameter must be positive.message)r!   #embed_check_categorical_event_shaper   with_dependenciesr   assert_positive)r*   r   r   r-   r-   r.   r#   L  s   z6DirichletMultinomial._maybe_assert_valid_concentrationc                 C   s8   | j s|S t|}ttj| jt	|dddg|S )zBCheck counts for proper shape, values, then return tensor version.r   z4counts last-dimension must sum to `self.total_count`rk   )
r   r!   r"   r   rn   r   assert_equalr   r   r%   r^   r-   r-   r.   rW   X  s   
z/DirichletMultinomial._maybe_assert_valid_sample)FTr   r3   )__name__
__module____qualname____doc__r   
deprecatedr(   propertyr   r   r2   r5   r7   r8   r;   rV   r!   AppendDocstring"_dirichlet_multinomial_sample_noter\   r_   rb   rg   rf   rc   r#   rW   __classcell__r-   r-   r+   r.   r   2   sF    o:






	N)rt   tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r   r   r   r	   #tensorflow.python.ops.distributionsr
   r   r!   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   __all__rx   Distributionr   r-   r-   r-   r.   <module>   s$   
