
    ukiy              	           d dl Zd dlmZ d dlm Z d dlmZ d dlm	Z	m
Z
mZ d dlmZmZmZmZ d dlmZmZ dded	ed
edefdZdded	ed
edefdZdded	ed
edefdZdd	ed
edefdZd	edefdZd	edefdZd	edefdZy)    N)lax)numpy)_const)promote_args_inexactpromote_dtypes_inexactensure_arraylike)xlogyentrgammaln	gammaincc)Array	ArrayLikekmulocreturnc           
         t        d| ||      \  } }}t        | d      }t        j                  | |      }t	        ||      t        |dz         z
  |z
  }t        j                  t        j                  t        j                  ||      t        j                  t        j                  |       |             t        j                   |      S )a7  Poisson log probability mass function.

  JAX implementation of :obj:`scipy.stats.poisson` ``logpmf``.

  The Poisson probability mass function is given by

  .. math::

     f(k) = e^{-\mu}\frac{\mu^k}{k!}

  and is defined for :math:`k \ge 0` and :math:`\mu \ge 0`.

  Args:
    k: arraylike, value at which to evaluate the PMF
    mu: arraylike, distribution shape parameter
    loc: arraylike, distribution offset parameter

  Returns:
    array of logpmf values.

  See Also:
    - :func:`jax.scipy.stats.poisson.cdf`
    - :func:`jax.scipy.stats.poisson.pmf`
  poisson.logpmfr      )r   
_lax_constr   subr	   r   jnpwhere
logical_orltneroundnpinf)r   r   r   zerox	log_probss         W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/scipy/stats/poisson.pylogpmfr$      s    2 $$4aSA*!R	Aq	$	ggao!ArlWQU^+b0)	3>>#&&D/"%&&1q"9;=?VVGY
P P    c                 B    t        j                  t        | ||            S )a0  Poisson probability mass function.

  JAX implementation of :obj:`scipy.stats.poisson` ``pmf``.

  The Poisson probability mass function is given by

  .. math::

     f(k) = e^{-\mu}\frac{\mu^k}{k!}

  and is defined for :math:`k \ge 0` and :math:`\mu \ge 0`.

  Args:
    k: arraylike, value at which to evaluate the PMF
    mu: arraylike, distribution shape parameter
    loc: arraylike, distribution offset parameter

  Returns:
    array of pmf values.

  See Also:
    - :func:`jax.scipy.stats.poisson.cdf`
    - :func:`jax.scipy.stats.poisson.logpmf`
  )r   expr$   )r   r   r   s      r#   pmfr(   :   s    2 
2s#	$$r%   c                    t        d| ||      \  } }}t        | d      }t        j                  | |      }t	        t        j                  d|z         |      }t        j                  t        j                  ||      ||      S )an  Poisson cumulative distribution function.

  JAX implementation of :obj:`scipy.stats.poisson` ``cdf``.

  The cumulative distribution function is defined as:

  .. math::

     f_{cdf}(k, p) = \sum_{i=0}^k f_{pmf}(k, p)

  where :math:`f_{pmf}(k, p)` is the probability mass function
  :func:`jax.scipy.stats.poisson.pmf`.

  Args:
    k: arraylike, value at which to evaluate the CDF
    mu: arraylike, distribution shape parameter
    loc: arraylike, distribution offset parameter

  Returns:
    array of cdf values.

  See Also:
    - :func:`jax.scipy.stats.poisson.pmf`
    - :func:`jax.scipy.stats.poisson.logpmf`
  r   r   r   )	r   r   r   r   r   r   floorr   r   )r   r   r   r    r!   ps         r#   cdfr,   V   sl    4 $$4aSA*!R	Aq	$	ggao!		!a% "%!	366!T?D!	,,r%   c                    t        d| |      \  } }t        | |      \  }}t        j                  |j                  |j                        }t        j
                  |      }t        j                  |      }t        j                  |dk(  |t        j                  |dk  t        |      t        j                  |dk  t        |      t        |                        }t        j                  ||j                        }t        j                  ||      S )am  Shannon entropy of the Poisson distribution.

  JAX implementation of :obj:`scipy.stats.poisson` ``entropy``.

  The entropy :math:`H(X)` of a Poisson random variable
  :math:`X \sim \text{Poisson}(\mu)` is defined as:

  .. math::

   H(X) = -\sum_{k=0}^\infty p(k) \log p(k)

  where :math:`p(k) = e^{-\mu} \mu^k / k!` for
  :math:`k \geq \max(0, \lfloor \text{loc} \rfloor)`.

  This implementation uses **regime switching** for numerical stability
  and performance:

  - **Small** :math:`\mu < 10`: Direct summation over PMF with adaptive
    upper bound :math:`k \leq \mu + 20`
  - **Medium** :math:`10 \leq \mu < 100`: Summation with bound
    :math:`k \leq \mu + 10\sqrt{\mu} + 20`
  - **Large** :math:`\mu \geq 100`: Asymptotic Stirling approximation:
    :math:`H(\mu) \approx \frac{1}{2} \log(2\pi e \mu) - \frac{1}{12\mu}`

  Matches SciPy to relative error :math:`< 10^{-5}` across all regimes.

  Args:
    mu: arraylike, mean parameter of the Poisson distribution.
      Must be ``> 0``.
    loc: arraylike, optional location parameter (default: 0).
      Accepted for API compatibility with scipy but does not
      affect the entropy

  Returns:
    Array of entropy values with shape broadcast from ``mu`` and ``loc``.
    Returns ``NaN`` for ``mu <= 0``.

  Examples:
    >>> from jax.scipy.stats import poisson
    >>> poisson.entropy(5.0)
    Array(2.204394, dtype=float32)
    >>> poisson.entropy(jax.numpy.array([1, 10, 100]))
    Array([1.3048419, 2.5614073, 3.7206903], dtype=float32)

  See Also:
    - :func:`jax.scipy.stats.poisson.pmf`
    - :func:`jax.scipy.stats.poisson.logpmf`
    - :obj:`scipy.stats.poisson`
  zpoisson.entropyr   
   d   )r   r   r   broadcast_shapesshaperavel
zeros_liker   _entropy_small_mu_entropy_medium_mu_entropy_large_mureshapebroadcast_to)	r   r   promoted_mupromoted_locresult_shapemu_flatzero_resultresultresult_mu_shapes	            r#   entropyr@   v   s    d .C8'"c4R=+| %%,
 IIk"'w'+ 99qLIIl 	ii#7#'"& KK(9(9:/ 
		/<	88r%   c                 X   d}t        j                  || j                        dddf   }t        || d      }t        j                  | dz         j                  |j                        }||dddf   k  }t        j                  ||d      }t        j                  t        |      d      S )u   Entropy via direct PMF summation for small μ (< 10).
  Uses adaptive upper bound k ≤ μ + 20 to capture >99.999% of mass.
  #   dtypeNr              axis)	r   arangerD   r(   ceilastyper   sumr
   r   max_kr   probsupper_boundsmaskprobs_maskeds          r#   r4   r4      s     %	jjbhh'40!
aQ-% "r'"))!''2,	
\$'"	"$4,,	l#!	,,r%   c                    d}t        j                  || j                        dddf   }t        || d      }t        j                  | dt        j
                  |       z  z   dz         j                  |j                        }||dddf   k  }t        j                  ||d      }t        j                  t        |      d      S )	u   Entropy for medium mu (10-100): Adaptive bounds based on std dev.

  Bounds: k ≤ μ + 10√μ + 20. Caps at k=250 for JIT compatibility.
     rC   Nr   r.   rE   rF   rG   )
r   rI   rD   r(   rJ   sqrtrK   r   rL   r
   rM   s          r#   r5   r5      s    
 %	jjbhh'40!
aQ-%"rCHHRL00256==aggF,	
\$'"	"$4,,	l#!	,,r%   c                     dt        j                  dt        j                  z  t        j                  z  | z        z  dd| z  z  z
  S )uw   Entropy for large mu (>= 100): Asymptotic approximation.

  Formula: H(λ) ≈ 0.5*log(2πeλ) - 1/(12λ) + O(λ^-2)
  g      ?   g      ?   )r   logr   pie)r   s    r#   r6   r6      s;    
 
swwq255y244'",-	-rBw	??r%   )r   )r   r   jax._srcr   r   jax._src.lax.laxr   r   jax._src.numpy.utilr   r   r   jax._src.scipy.specialr	   r
   r   r   jax._src.typingr   r   r$   r(   r,   r@   r4   r5   r6    r%   r#   <module>rb      s      ! 1 ^ ^ B B ,Pi PY PY Pu PB%9 %) %) %E %8-9 -) -) -E -@R9	 R9	 R9% R9h-% -E - -5 -U - @% @E @r%   