o
    *ήcTK                     @   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mZ ddlmZ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$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+ g dZ,dd Z-G dd de'Z.ej/fddZ0G dd de'Z1dd Z2G d d! d!e'Z3d"d# Z4G d$d% d%e'Z5d&d' Z6G d(d) d)e'Z7d*d+ Z8G d,d- d-e'Z9d.d/ Z:G d0d1 d1e'Z;d2d3 Z<G d4d5 d5e'Z=d6d7 Z>G d8d9 d9e'Z?d:d; Z@G d<d= d=e'ZAd>d? ZBd@S )Azn

Contains
========
FlorySchulz
Geometric
Hermite
Logarithmic
NegativeBinomial
Poisson
Skellam
YuleSimon
Zeta
    )Sum)Basic)Lambda)I)Eq)S)Dummy)sympify)binomial	factorial)explog)floor)sqrt)	Piecewise)besseli)beta)hyper)polylogzeta)SingleDiscreteDistributionSingleDiscretePSpace)_value_check	is_random)	FlorySchulz	GeometricHermiteLogarithmicNegativeBinomialPoissonSkellam	YuleSimonZetac                 O   sl   t tt|}|| }|ddr|j|  t| |}tdd |D r3ddlm}m	} || ||}|j
S )NcheckTc                 s   s    | ]}t |V  qd S N)r   ).0arg r'   </tmp/pip-target-vg8gfxp4/lib/python/sympy/stats/drv_types.py	<genexpr>9   s    zrv.<locals>.<genexpr>r   )CompoundPSpaceCompoundDistribution)listmapr	   popr#   r   anysympy.stats.compound_rvr*   r+   value)symbolclsargskwargsdistpspacer*   r+   r'   r'   r(   rv3   s   

r8   c                   @   s6   e Zd ZdZejfddZedd Ze	dd Z
dS )	DiscreteDistributionHandmade)pdfc                 C   s   t | ||S r$   )r   __new__)r3   r:   setr'   r'   r(   r;   B   s   z$DiscreteDistributionHandmade.__new__c                 C   s
   | j d S N   )r4   )selfr'   r'   r(   r<   E   s   
z DiscreteDistributionHandmade.setc                 C   s>   t d}t| |||j|jf }tt|dtjkd d S )Nxr>   z&The pdf is incorrect on the given set.)	r   r   _inf_supdoitr   r   r   false)r:   r<   r@   valr'   r'   r(   r#   I   s   z"DiscreteDistributionHandmade.checkN)__name__
__module____qualname__	_argnamesr   Integersr;   propertyr<   staticmethodr#   r'   r'   r'   r(   r9   ?   s    
r9   c                 K   sN   t |}t||| fd}t| |}|dd|d< t| jt||fi |S )af  
    Create a Discrete Random Variable given the following:

    Parameters
    ==========

    symbol : Symbol
        Represents name of the random variable.
    density : Expression containing symbol
        Represents probability density function.
    set : set
        Represents the region where the pdf is valid, by default is real line.
    check : bool
        If True, it will check whether the given density
        integrates to 1 over the given set. If False, it
        will not perform this check. Default is False.

    Examples
    ========

    >>> from sympy.stats import DiscreteRV, P, E
    >>> from sympy import Rational, Symbol
    >>> x = Symbol('x')
    >>> n = 10
    >>> density = Rational(1, 10)
    >>> X = DiscreteRV(x, density, set=set(range(n)))
    >>> E(X)
    9/2
    >>> P(X>3)
    3/5

    Returns
    =======

    RandomSymbol

    r   Tr#   F)r	   r   as_relationalr   r.   r8   namer9   )r2   densityr<   r5   r:   r'   r'   r(   
DiscreteRVQ   s
   &
rQ   c                   @   :   e Zd ZdZejZedd Zdd Z	dd Z
dd	 Zd
S )FlorySchulzDistributionac                 C   s   t d| k | dk fd d S )Nr   r>   za must be between 0 and 1r   rT   r'   r'   r(   r#         zFlorySchulzDistribution.checkc                 C   s"   | j }|d | d| |d   S N   r>   rT   )r?   krU   r'   r'   r(   r:         zFlorySchulzDistribution.pdfc                 C   s6   | j }|d tt|  d|d tt|   d  S rX   )rU   r   r   r?   trU   r'   r'   r(   _characteristic_function   s   0z0FlorySchulzDistribution._characteristic_functionc                 C   s.   | j }|d t| d|d t|  d  S rX   )rU   r   r\   r'   r'   r(   _moment_generating_function      (z3FlorySchulzDistribution._moment_generating_functionNrF   rG   rH   rI   r   Naturalsr<   rL   r#   r:   r^   r_   r'   r'   r'   r(   rS      s    
rS   c                 C      t | t|S )a  
    Create a discrete random variable with a FlorySchulz distribution.

    The density of the FlorySchulz distribution is given by

    .. math::
        f(k) := (a^2) k (1 - a)^{k-1}

    Parameters
    ==========

    a : A real number between 0 and 1

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import density, E, variance, FlorySchulz
    >>> from sympy import Symbol, S

    >>> a = S.One / 5
    >>> z = Symbol("z")

    >>> X = FlorySchulz("x", a)

    >>> density(X)(z)
    (4/5)**(z - 1)*z/25

    >>> E(X)
    9

    >>> variance(X)
    40

    References
    ==========

    https://en.wikipedia.org/wiki/Flory%E2%80%93Schulz_distribution
    )r8   rS   )rO   rU   r'   r'   r(   r      s   ,r   c                   @   rR   )GeometricDistributionpc                 C   s   t d| k | dkfd d S )Nr   r>   zp must be between 0 and 1rV   re   r'   r'   r(   r#      rW   zGeometricDistribution.checkc                 C   s   d| j  |d  | j  S r=   re   r?   rZ   r'   r'   r(   r:      s   zGeometricDistribution.pdfc                 C   s.   | j }|tt|  dd| tt|    S r=   )rf   r   r   r?   r]   rf   r'   r'   r(   r^      r`   z.GeometricDistribution._characteristic_functionc                 C   s&   | j }|t| dd| t|   S r=   )rf   r   rh   r'   r'   r(   r_          z1GeometricDistribution._moment_generating_functionNra   r'   r'   r'   r(   rd      s    
rd   c                 C   rc   )a  
    Create a discrete random variable with a Geometric distribution.

    Explanation
    ===========

    The density of the Geometric distribution is given by

    .. math::
        f(k) := p (1 - p)^{k - 1}

    Parameters
    ==========

    p : A probability between 0 and 1

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import Geometric, density, E, variance
    >>> from sympy import Symbol, S

    >>> p = S.One / 5
    >>> z = Symbol("z")

    >>> X = Geometric("x", p)

    >>> density(X)(z)
    (4/5)**(z - 1)/5

    >>> E(X)
    5

    >>> variance(X)
    20

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Geometric_distribution
    .. [2] http://mathworld.wolfram.com/GeometricDistribution.html

    )r8   rd   rO   rf   r'   r'   r(   r         1r   c                   @   rR   )HermiteDistributiona1a2c                 C   s   t | jd t |jd d S )NzParameter a1 must be >= 0.zParameter a2 must be >= 0.)r   is_nonnegativerm   r'   r'   r(   r#     s   zHermiteDistribution.checkc                 C   sx   | j | j}}t||  }tddd}||d|   ||  }t|d|  t| }|t|| |d|d f  S )NjT)integerrY   r   )rn   ro   r   r   r   r   rC   )r?   rZ   rn   ro   term1rq   numdenr'   r'   r(   r:     s    zHermiteDistribution.pdfc                 C   s>   | j | j}}|t|d  }|td| d  }t|| S Nr>   rY   )rn   ro   r   r?   r]   rn   ro   rs   term2r'   r'   r(   r_   &  s   z/HermiteDistribution._moment_generating_functionc                 C   sF   | j | j}}|tt| d  }|tdt | d  }t|| S rv   )rn   ro   r   r   rw   r'   r'   r(   r^   ,  s   z,HermiteDistribution._characteristic_functionN)rF   rG   rH   rI   r   	Naturals0r<   rL   r#   r:   r_   r^   r'   r'   r'   r(   rl     s    
rl   c                 C      t | t||S )a  
    Create a discrete random variable with a Hermite distribution.

    Explanation
    ===========

    The density of the Hermite distribution is given by

    .. math::
        f(x):= e^{-a_1 -a_2}\sum_{j=0}^{\left \lfloor x/2 \right \rfloor}
                    \frac{a_{1}^{x-2j}a_{2}^{j}}{(x-2j)!j!}

    Parameters
    ==========

    a1 : A Positive number greater than equal to 0.
    a2 : A Positive number greater than equal to 0.

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import Hermite, density, E, variance
    >>> from sympy import Symbol

    >>> a1 = Symbol("a1", positive=True)
    >>> a2 = Symbol("a2", positive=True)
    >>> x = Symbol("x")

    >>> H = Hermite("H", a1=5, a2=4)

    >>> density(H)(2)
    33*exp(-9)/2

    >>> E(H)
    13

    >>> variance(H)
    21

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hermite_distribution

    )r8   rl   )rO   rn   ro   r'   r'   r(   r   2  s   4r   c                   @   rR   )LogarithmicDistributionre   c                 C   s   t | dk| dk fd d S )Nr   r>   p should be between 0 and 1rV   re   r'   r'   r(   r#   q  rW   zLogarithmicDistribution.checkc                 C   s"   | j }d||  |td|   S )Nr>   )rf   r   )r?   rZ   rf   r'   r'   r(   r:   u  r[   zLogarithmicDistribution.pdfc                 C   s*   | j }td|tt|   td|  S r=   )rf   r   r   r   rh   r'   r'   r(   r^   y  s   $z0LogarithmicDistribution._characteristic_functionc                 C   s&   | j }td|t|  td|  S r=   )rf   r   r   rh   r'   r'   r(   r_   }  ri   z3LogarithmicDistribution._moment_generating_functionNra   r'   r'   r'   r(   r{   l  s    
r{   c                 C   rc   )a  
    Create a discrete random variable with a Logarithmic distribution.

    Explanation
    ===========

    The density of the Logarithmic distribution is given by

    .. math::
        f(k) := \frac{-p^k}{k \ln{(1 - p)}}

    Parameters
    ==========

    p : A value between 0 and 1

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import Logarithmic, density, E, variance
    >>> from sympy import Symbol, S

    >>> p = S.One / 5
    >>> z = Symbol("z")

    >>> X = Logarithmic("x", p)

    >>> density(X)(z)
    -1/(5**z*z*log(4/5))

    >>> E(X)
    -1/(-4*log(5) + 8*log(2))

    >>> variance(X)
    -1/((-4*log(5) + 8*log(2))*(-2*log(5) + 4*log(2))) + 1/(-64*log(2)*log(5) + 64*log(2)**2 + 16*log(5)**2) - 10/(-32*log(5) + 64*log(2))

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Logarithmic_distribution
    .. [2] http://mathworld.wolfram.com/LogarithmicDistribution.html

    )r8   r{   rj   r'   r'   r(   r     rk   r   c                   @   rR   )NegativeBinomialDistributionrrf   c                 C   s(   t | dkd t |dk|dk fd d S )Nr   zr should be positiver>   r|   rV   r   r'   r'   r(   r#     s   z"NegativeBinomialDistribution.checkc                 C   s2   | j }| j}t|| d |d| |  ||  S r=   )r   rf   r
   )r?   rZ   r   rf   r'   r'   r(   r:     s   &z NegativeBinomialDistribution.pdfc                 C   s,   | j }| j}d| d|tt|    | S r=   )r   rf   r   r   r?   r]   r   rf   r'   r'   r(   r^     s    z5NegativeBinomialDistribution._characteristic_functionc                 C   s(   | j }| j}d| d|t|   | S r=   )r   rf   r   r   r'   r'   r(   r_     s   z8NegativeBinomialDistribution._moment_generating_functionNrF   rG   rH   rI   r   ry   r<   rL   r#   r:   r^   r_   r'   r'   r'   r(   r~     s    
r~   c                 C   rz   )a  
    Create a discrete random variable with a Negative Binomial distribution.

    Explanation
    ===========

    The density of the Negative Binomial distribution is given by

    .. math::
        f(k) := \binom{k + r - 1}{k} (1 - p)^r p^k

    Parameters
    ==========

    r : A positive value
    p : A value between 0 and 1

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import NegativeBinomial, density, E, variance
    >>> from sympy import Symbol, S

    >>> r = 5
    >>> p = S.One / 5
    >>> z = Symbol("z")

    >>> X = NegativeBinomial("x", r, p)

    >>> density(X)(z)
    1024*binomial(z + 4, z)/(3125*5**z)

    >>> E(X)
    5/4

    >>> variance(X)
    25/16

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Negative_binomial_distribution
    .. [2] http://mathworld.wolfram.com/NegativeBinomialDistribution.html

    )r8   r~   )rO   r   rf   r'   r'   r(   r     s   3r   c                   @   rR   )PoissonDistributionlamdac                 C      t | dkd d S )Nr   zLambda must be positiverV   r   r'   r'   r(   r#        zPoissonDistribution.checkc                 C   s   | j | t| t| j   S r$   )r   r   r   rg   r'   r'   r(   r:        zPoissonDistribution.pdfc                 C   s   t | jt t| d  S r=   )r   r   r   r?   r]   r'   r'   r(   r^        z,PoissonDistribution._characteristic_functionc                 C   s   t | jt |d  S r=   )r   r   r   r'   r'   r(   r_     s   z/PoissonDistribution._moment_generating_functionNr   r'   r'   r'   r(   r     s    
r   c                 C   rc   )ad  
    Create a discrete random variable with a Poisson distribution.

    Explanation
    ===========

    The density of the Poisson distribution is given by

    .. math::
        f(k) := \frac{\lambda^{k} e^{- \lambda}}{k!}

    Parameters
    ==========

    lamda : Positive number, a rate

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import Poisson, density, E, variance
    >>> from sympy import Symbol, simplify

    >>> rate = Symbol("lambda", positive=True)
    >>> z = Symbol("z")

    >>> X = Poisson("x", rate)

    >>> density(X)(z)
    lambda**z*exp(-lambda)/factorial(z)

    >>> E(X)
    lambda

    >>> simplify(variance(X))
    lambda

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Poisson_distribution
    .. [2] http://mathworld.wolfram.com/PoissonDistribution.html

    )r8   r   )rO   r   r'   r'   r(   r      rk   r   c                   @   B   e Zd ZdZejZedd Zdd Z	dd Z
dd	 Zd
d ZdS )SkellamDistributionmu1mu2c                 C   s    t | dkd t |dkd d S )Nr   zParameter mu1 must be >= 0zParameter mu2 must be >= 0rV   r   r'   r'   r(   r#   \  s   zSkellamDistribution.checkc                 C   sJ   | j | j}}t||  || |d   }t|dt||  }|| S )NrY   )r   r   r   r   r   )r?   rZ   r   r   rs   rx   r'   r'   r(   r:   a  s   zSkellamDistribution.pdfc                 C   s   t d)Nz-Skellam doesn't have closed form for the CDF.)NotImplementedErrorr?   r@   r'   r'   r(   _cdfg  s   zSkellamDistribution._cdfc                 C   s>   | j | j}}t||  |tt|   |tt |   S r$   )r   r   r   r   r?   r]   r   r   r'   r'   r(   r^   k  s   0z,SkellamDistribution._characteristic_functionc                 C   s6   | j | j}}t||  |t|  |t|   S r$   )r   r   r   r   r'   r'   r(   r_   o  s   (z/SkellamDistribution._moment_generating_functionN)rF   rG   rH   rI   r   rJ   r<   rL   r#   r:   r   r^   r_   r'   r'   r'   r(   r   X  s    
r   c                 C   rz   )a  
    Create a discrete random variable with a Skellam distribution.

    Explanation
    ===========

    The Skellam is the distribution of the difference N1 - N2
    of two statistically independent random variables N1 and N2
    each Poisson-distributed with respective expected values mu1 and mu2.

    The density of the Skellam distribution is given by

    .. math::
        f(k) := e^{-(\mu_1+\mu_2)}(\frac{\mu_1}{\mu_2})^{k/2}I_k(2\sqrt{\mu_1\mu_2})

    Parameters
    ==========

    mu1 : A non-negative value
    mu2 : A non-negative value

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import Skellam, density, E, variance
    >>> from sympy import Symbol, pprint

    >>> z = Symbol("z", integer=True)
    >>> mu1 = Symbol("mu1", positive=True)
    >>> mu2 = Symbol("mu2", positive=True)
    >>> X = Skellam("x", mu1, mu2)

    >>> pprint(density(X)(z), use_unicode=False)
         z
         -
         2
    /mu1\   -mu1 - mu2        /       _____   _____\
    |---| *e          *besseli\z, 2*\/ mu1 *\/ mu2 /
    \mu2/
    >>> E(X)
    mu1 - mu2
    >>> variance(X).expand()
    mu1 + mu2

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Skellam_distribution

    )r8   r   )rO   r   r   r'   r'   r(   r    t  s   8r    c                   @   r   )YuleSimonDistributionrhoc                 C   r   )Nr   zrho should be positiverV   r   r'   r'   r(   r#     r   zYuleSimonDistribution.checkc                 C   s   | j }|t||d  S r=   )r   r   )r?   rZ   r   r'   r'   r(   r:     s   zYuleSimonDistribution.pdfc                 C   s.   t dt|tt|| jd   |dkfdS )Nr>   rM   )r   r   r   r   r   r'   r'   r(   r     s   .zYuleSimonDistribution._cdfc                 C   s8   | j }|td|d ftt|  tt|  |d  S N)r>   r>   rY   r>   )r   r   r   r   r?   r]   r   r'   r'   r(   r^     s   2z.YuleSimonDistribution._characteristic_functionc                 C   s0   | j }|td|d ft| t| |d  S r   )r   r   r   r   r'   r'   r(   r_     s   *z1YuleSimonDistribution._moment_generating_functionN)rF   rG   rH   rI   r   rb   r<   rL   r#   r:   r   r^   r_   r'   r'   r'   r(   r     s    
r   c                 C   rc   )a  
    Create a discrete random variable with a Yule-Simon distribution.

    Explanation
    ===========

    The density of the Yule-Simon distribution is given by

    .. math::
        f(k) := \rho B(k, \rho + 1)

    Parameters
    ==========

    rho : A positive value

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import YuleSimon, density, E, variance
    >>> from sympy import Symbol, simplify

    >>> p = 5
    >>> z = Symbol("z")

    >>> X = YuleSimon("x", p)

    >>> density(X)(z)
    5*beta(z, 6)

    >>> simplify(E(X))
    5/4

    >>> simplify(variance(X))
    25/48

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Yule%E2%80%93Simon_distribution

    )r8   r   )rO   r   r'   r'   r(   r!        0r!   c                   @   rR   )ZetaDistributionsc                 C   r   )Nr>   zs should be greater than 1rV   r   r'   r'   r(   r#     r   zZetaDistribution.checkc                 C   s   | j }d|| t|  S r=   )r   r   )r?   rZ   r   r'   r'   r(   r:     s   zZetaDistribution.pdfc                 C   s   t | jtt| t| j S r$   )r   r   r   r   r   r   r'   r'   r(   r^     r   z)ZetaDistribution._characteristic_functionc                 C   s   t | jt|t| j S r$   )r   r   r   r   r   r'   r'   r(   r_     r   z,ZetaDistribution._moment_generating_functionNra   r'   r'   r'   r(   r      s    
r   c                 C   rc   )a  
    Create a discrete random variable with a Zeta distribution.

    Explanation
    ===========

    The density of the Zeta distribution is given by

    .. math::
        f(k) := \frac{1}{k^s \zeta{(s)}}

    Parameters
    ==========

    s : A value greater than 1

    Returns
    =======

    RandomSymbol

    Examples
    ========

    >>> from sympy.stats import Zeta, density, E, variance
    >>> from sympy import Symbol

    >>> s = 5
    >>> z = Symbol("z")

    >>> X = Zeta("x", s)

    >>> density(X)(z)
    1/(z**5*zeta(5))

    >>> E(X)
    pi**4/(90*zeta(5))

    >>> variance(X)
    -pi**8/(8100*zeta(5)**2) + zeta(3)/zeta(5)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Zeta_distribution

    )r8   r   )rO   r   r'   r'   r(   r"     r   r"   N)C__doc__sympy.concrete.summationsr   sympy.core.basicr   sympy.core.functionr   sympy.core.numbersr   sympy.core.relationalr   sympy.core.singletonr   sympy.core.symbolr   sympy.core.sympifyr	   (sympy.functions.combinatorial.factorialsr
   r   &sympy.functions.elementary.exponentialr   r   #sympy.functions.elementary.integersr   (sympy.functions.elementary.miscellaneousr   $sympy.functions.elementary.piecewiser   sympy.functions.special.besselr   &sympy.functions.special.beta_functionsr   sympy.functions.special.hyperr   &sympy.functions.special.zeta_functionsr   r   sympy.stats.drvr   r   sympy.stats.rvr   r   __all__r8   r9   rJ   rQ   rS   r   rd   r   rl   r   r{   r   r~   r   r   r   r   r    r   r!   r   r"   r'   r'   r'   r(   <module>   sT    128:798>6