
    AVhY5                         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       G d dej*                               Zy)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                   X    e Zd ZdZ ej
                  ddd      	 	 	 d fd	       Zed        Zed        Z	ed	        Z
d
 Zd Zd Zd ZddZ ej"                  e      d        Z ej"                  e      d        Zd Z ej"                  d      d        Zd Zd Zd Zd Z x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_oncec           	      R   t        t                     }t        j                  |||g      5 }t        j                  |d      | _        |r$t        j                  | j
                        | _        | j                  t        j                  |d      |      | _	        t        j                  | j                  d      | _        ddd       t        t        | ?  | j                  j                   ||t"        j$                  || j
                  | j                  g|       y# 1 sw Y   \xY w)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__s          i/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/distributions/dirichlet_multinomial.pyr+   zDirichletMultinomial.__init__   s   H fhJ	k=%A	B Od //-Pd	BB!!# 	 !BB


%46
d #+"5"5d6I6I2"Nd%O& 

.!!''#' , @ @((**, / 'O Os   BDD&c                     | j                   S )z,Number of trials used to construct a sample.)r#   r-   s    r/   r   z DirichletMultinomial.total_count   s         c                     | j                   S )zCConcentration parameter; expected prior counts for that coordinate.)r'   r1   s    r/   r   z"DirichletMultinomial.concentration   s     r2   c                     | j                   S )z+Sum of last dim of concentration parameter.)r)   r1   s    r/   total_concentrationz(DirichletMultinomial.total_concentration   s     $$$r2   c                 @    t        j                  | j                        S N)r   shaper5   r1   s    r/   _batch_shape_tensorz(DirichletMultinomial._batch_shape_tensor   s    ??43344r2   c                 6    | j                   j                         S r7   )r5   	get_shaper1   s    r/   _batch_shapez!DirichletMultinomial._batch_shape   s    ##--//r2   c                 F    t        j                  | j                        dd  S )Nr   )r   r8   r   r1   s    r/   _event_shape_tensorz(DirichletMultinomial._event_shape_tensor   s    ??4--.rs33r2   c                 Z    | j                   j                         j                  d      dd  S )N   r   )r   r;   with_rank_at_leastr1   s    r/   _event_shapez!DirichletMultinomial._event_shape   s)    '')<<Q?DDr2   c           
         t        j                  | j                  t        j                        }| j                         d   }t        j                  t        j                  t        j                  |g| j                  | j                  |            d|g      }t        j                  ||t        j                  |d            }t        j                   t        j"                  ||	      d
      }t        j$                  |g| j'                         |ggd      }t        j                  ||      }t        j                  || j                        S )N)r   r   )r8   alphar   seedr   )r8   dirichlet_multinomial)salt)logitsnum_samplesrE   )depth)r   castr   r   int32event_shape_tensorr   reshapelogr	   random_gammar   r   multinomialr$   gen_new_seedr(   one_hotconcatbatch_shape_tensor)	r-   nrE   n_drawskunnormalized_logitsdrawsxfinal_shapes	            r/   	_sample_nzDirichletMultinomial._sample_n  s   mmD,,FLLAG!!$A#++Z,,#$$**	 	
 1g """++D7NOQE 	I--e1=rBA""QC)@)@)BQC#H!LK![)A==DJJ''r2   c                     | j                  |      }t        j                  | j                  |z         t        j                  | j                        z
  }|t	        j
                  | j                  |      z   S r7   )_maybe_assert_valid_sampler
   lbetar   r$   log_combinationsr   )r-   countsordered_probs      r/   	_log_probzDirichletMultinomial._log_prob  sq    ,,V4Ft11F:;

 
 !3!3
4	5  +<<&" " "r2   c                 J    t        j                  | j                  |            S r7   )r   expre   r-   rc   s     r/   _probzDirichletMultinomial._prob  s    <<v.//r2   c                 t    | j                   | j                  | j                  dt        j                  f   z  z  S N.)r   r   r5   r   newaxisr1   s    r/   _meanzDirichletMultinomial._mean!  s>    t11#77Y=N=N8NO P Q Qr2   a  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           
         | j                         | j                         z  }t        j                  t	        j
                  |dt        j                  f   |dt        j                  d d f          | j                               S rk   )_variance_scale_termrm   r   matrix_set_diagr   matmulrl   	_variance)r-   r\   s     r/   _covariancez DirichletMultinomial._covariance%  su    & 	!!#djjl2A$$	c9$$$%c9$$a'(
* 	* 		 r2   c                 r    | j                         }|| j                         z  }|| j                  |z  |z
  z  S r7   )ro   rm   r   )r-   scaler\   s      r/   rr   zDirichletMultinomial._variance@  s;    %%'E

A  5(1,--r2   c                     | j                   dt        j                  f   }t        j                  d|| j
                  z  z   d|z   z        S )zFHelper to `_covariance` and `_variance` which computes a shared scale..g      ?)r5   r   rl   r   sqrtr   )r-   c0s     r/   ro   z)DirichletMultinomial._variance_scale_termE  sG     
	!	!#y'8'8"8	9B=="rD$4$444bABBr2   c                     |s|S t        j                  |      }t        j                  t	        j
                  |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   s      r/   r&   z6DirichletMultinomial._maybe_assert_valid_concentrationL  sP    %IIM--!!?	A/ 	 r2   c           	          | j                   s|S t        j                  |      }t        j                  t        j                  | j                  t        j                  |d      d      g|      S )zBCheck counts for proper shape, values, then return tensor version.r   z4counts last-dimension must sum to `self.total_count`rz   )
r   r$   r%   r   r}   r   assert_equalr   r   r(   rh   s     r/   r`   z/DirichletMultinomial._maybe_assert_valid_sampleX  sg    mCCFKF--h11&"=J	L/ 	 r2   )FTr   r7   )__name__
__module____qualname____doc__r   
deprecatedr+   propertyr   r   r5   r9   r<   r>   rB   r^   r$   AppendDocstring"_dirichlet_multinomial_sample_notere   ri   rm   rs   rr   ro   r&   r`   __classcell__)r.   s   @r/   r   r   2   s$   k^ ;'
  ##*88t     % %504E(& %$$%GH" I" %$$%GH0 I0Q %$$
$%$.
C
	r2   N)r   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__r   Distributionr    r2   r/   <module>r      so    3 . + + + 2 * , 2 < I . 6 
&5 " 345n<44 n 6nr2   