o
    8VaY                     @   sD  d dl Z d dlmZmZmZmZmZmZmZm	Z	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mZ d d	lmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ g d
Z%e!&edd Z'e!&edd Z'G dd deZ(G dd deZ)G dd deZ*G dd deZ+G dd deZ,G dd deZ-dS )    N)
ExprAddMulSIntegralEqSumSymbolexpandNot)default_sort_key)global_parameters)_sympify)
Relational)Boolean)variance
covariance)
RandomSymbolpspace	dependentgiven
sampling_ERandomIndexedSymbol	is_randomPSpace
sampling_Prandom_symbols)ProbabilityExpectationVariance
Covariancec                 C   s8   | j }t|dkrtt|| krdS tdd |D S )N   Fc                 S   s   g | ]}t |qS  )r   ).0ir"   r"   B/usr/lib/python3/dist-packages/sympy/stats/symbolic_probability.py
<listcomp>   s    z_.<locals>.<listcomp>)Zfree_symbolslennextiterany)xatomsr"   r"   r%   _   s   r-   c                 C   s   dS )NTr"   r+   r"   r"   r%   r-      s   c                   @   s8   e Zd ZdZdddZdd ZdddZeZd	d
 ZdS )r   a  
    Symbolic expression for the probability.

    Examples
    ========

    >>> from sympy.stats import Probability, Normal
    >>> from sympy import Integral
    >>> X = Normal("X", 0, 1)
    >>> prob = Probability(X > 1)
    >>> prob
    Probability(X > 1)

    Integral representation:

    >>> prob.rewrite(Integral)
    Integral(sqrt(2)*exp(-_z**2/2)/(2*sqrt(pi)), (_z, 1, oo))

    Evaluation of the integral:

    >>> prob.evaluate_integral()
    sqrt(2)*(-sqrt(2)*sqrt(pi)*erf(sqrt(2)/2) + sqrt(2)*sqrt(pi))/(4*sqrt(pi))
    Nc                 K   s>   t |}|d u rt| |}nt |}t| ||}||_|S N)r   r   __new__
_condition)clsZprob	conditionkwargsobjr"   r"   r%   r0   6   s   zProbability.__new__c                    s  | j d }| j |dd}|dd }t|tr.tj| j|j d  |djdi | S |	t
r=t|j| |dS t tr}t|}t|dkrg|d  krgddlm} || |jdi |ddS t fdd	|D rwt| S t| S  d urt ttfstd
   dks|tju rtjS t|ttfstd
| |tju rtjS |rt| |dS  d urtt|  S t|t krt| S t||}t|dr|r| S |S )Nr   
numsamplesFfor_rewriteevaluater!   )BernoulliDistributionc                    s   g | ]}t | qS r"   )r   )r#   rvZgiven_conditionr"   r%   r&   S       z$Probability.doit.<locals>.<listcomp>z4%s is not a relational or combination of relationals)r6   doitr"   )argsr1   get
isinstancer   r   Onefuncr>   hasr   r   Zprobabilityr   r   r'   Zsympy.stats.frv_typesr:   r*   r   r   r   
ValueErrorZfalseZerotruer   r   r   hasattr)selfhintsr3   r6   r7   Zcondrvr:   resultr"   r<   r%   r>   @   s`   






zProbability.doitc                 K   s   | j ||djddS )Nr3   T)r7   rC   r>   rI   argr3   r4   r"   r"   r%   _eval_rewrite_as_Integralu      z%Probability._eval_rewrite_as_Integralc                 C      |  t S r/   rewriter   r>   rI   r"   r"   r%   evaluate_integralz      zProbability.evaluate_integralr/   )	__name__
__module____qualname____doc__r0   r>   rP   _eval_rewrite_as_SumrV   r"   r"   r"   r%   r      s    


5r   c                   @   sN   e Zd ZdZdddZdd Zdd Zdd	d
ZdddZeZ	dd Z
e
ZdS )r   a(  
    Symbolic expression for the expectation.

    Examples
    ========

    >>> from sympy.stats import Expectation, Normal, Probability, Poisson
    >>> from sympy import symbols, Integral, Sum
    >>> mu = symbols("mu")
    >>> sigma = symbols("sigma", positive=True)
    >>> X = Normal("X", mu, sigma)
    >>> Expectation(X)
    Expectation(X)
    >>> Expectation(X).evaluate_integral().simplify()
    mu

    To get the integral expression of the expectation:

    >>> Expectation(X).rewrite(Integral)
    Integral(sqrt(2)*X*exp(-(X - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma), (X, -oo, oo))

    The same integral expression, in more abstract terms:

    >>> Expectation(X).rewrite(Probability)
    Integral(x*Probability(Eq(X, x)), (x, -oo, oo))

    To get the Summation expression of the expectation for discrete random variables:

    >>> lamda = symbols('lamda', positive=True)
    >>> Z = Poisson('Z', lamda)
    >>> Expectation(Z).rewrite(Sum)
    Sum(Z*lamda**Z*exp(-lamda)/factorial(Z), (Z, 0, oo))

    This class is aware of some properties of the expectation:

    >>> from sympy.abc import a
    >>> Expectation(a*X)
    Expectation(a*X)
    >>> Y = Normal("Y", 1, 2)
    >>> Expectation(X + Y)
    Expectation(X + Y)

    To expand the ``Expectation`` into its expression, use ``expand()``:

    >>> Expectation(X + Y).expand()
    Expectation(X) + Expectation(Y)
    >>> Expectation(a*X + Y).expand()
    a*Expectation(X) + Expectation(Y)
    >>> Expectation(a*X + Y)
    Expectation(a*X + Y)
    >>> Expectation((X + Y)*(X - Y)).expand()
    Expectation(X**2) - Expectation(Y**2)

    To evaluate the ``Expectation``, use ``doit()``:

    >>> Expectation(X + Y).doit()
    mu + 1
    >>> Expectation(X + Expectation(Y + Expectation(2*X))).doit()
    3*mu + 1

    To prevent evaluating nested ``Expectation``, use ``doit(deep=False)``

    >>> Expectation(X + Expectation(Y)).doit(deep=False)
    mu + Expectation(Expectation(Y))
    >>> Expectation(X + Expectation(Y + Expectation(2*X))).doit(deep=False)
    mu + Expectation(Expectation(Y + Expectation(2*X)))

    Nc                 K   sf   t |}|jrddlm} |||S |d u r#t|s|S t| |}nt |}t| ||}||_|S )Nr   )ExpectationMatrix)r   	is_Matrix-sympy.stats.symbolic_multivariate_probabilityr]   r   r   r0   r1   )r2   exprr3   r4   r]   r5   r"   r"   r%   r0      s   
zExpectation.__new__c                    s   | j d }| j t|s|S t|tr t fdd|j D S t|}t|tr6t fdd|j D S t|trbg }g }|j D ]}t|rN|| qB|| qBt|t	t| d S | S )Nr   c                 3        | ]}t | d  V  qdS rL   Nr   r
   r#   arL   r"   r%   	<genexpr>       z%Expectation.expand.<locals>.<genexpr>c                 3   ra   rb   rc   rd   rL   r"   r%   rf      rg   rL   )
r?   r1   r   rA   r   fromiter_expandr   appendr   )rI   rJ   r`   Zexpand_exprr;   nonrvre   r"   rL   r%   r
      s,   




zExpectation.expandc                    s@   dd}j jd } dd} dd }|r%|jdi }t|r.t|tr0|S |r@ dd}t| ||dS |t	rMt
|| S  d ur_t| jdi S |jrpt fd	d
|jD  S |jrz|trz|S t
|t kr|S t
|j||d}t|dr|r|jdi S |S )NdeepTr   r6   Fr7   evalf)r6   rm   c                    s:   g | ]}t |ts| jd i n| qS )r"   )rA   r   rC   r>   )r#   rO   r3   rJ   rI   r"   r%   r&   	  s    
z$Expectation.doit.<locals>.<listcomp>r8   r>   r"   )r@   r1   r?   r>   r   rA   r   r   rD   r   r   Zcompute_expectationrC   r   Zis_Addr   Zis_Mulr,   r   rH   )rI   rJ   rl   r`   r6   r7   rm   rK   r"   rn   r%   r>      s:   



zExpectation.doitc                 K   s   | t}t|dkrt t|dkr|S | }|jd u r#td|j}|jd 	 r5t
|j }nt
|jd }|jjr\t|||tt||| ||jjjj|jjjjfS |jjrbtt|||tt||| ||jjjj|jjjfS )Nr!   r   zProbability space not knownZ_1)r,   r   r'   NotImplementedErrorpopr   rE   symbolnameisupperr	   lowerZis_Continuousr   replacer   r   domainsetinfsupZ	is_Finiter   )rI   rO   r3   r4   Zrvsr;   rq   r"   r"   r%   _eval_rewrite_as_Probability  s"   

86z(Expectation._eval_rewrite_as_Probabilityc                 K   s   | j ||djdddS )NrL   FT)rl   r7   rM   rN   r"   r"   r%   rP   3  s   z%Expectation._eval_rewrite_as_Integralc                 C   rR   r/   rS   rU   r"   r"   r%   rV   8  rW   zExpectation.evaluate_integralr/   )rX   rY   rZ   r[   r0   r
   r>   rz   rP   r\   rV   Zevaluate_sumr"   r"   r"   r%   r   ~   s    
E
+
r   c                   @   sL   e Zd ZdZdddZdd ZdddZdd	d
ZdddZeZ	dd Z
dS )r   a  
    Symbolic expression for the variance.

    Examples
    ========

    >>> from sympy import symbols, Integral
    >>> from sympy.stats import Normal, Expectation, Variance, Probability
    >>> mu = symbols("mu", positive=True)
    >>> sigma = symbols("sigma", positive=True)
    >>> X = Normal("X", mu, sigma)
    >>> Variance(X)
    Variance(X)
    >>> Variance(X).evaluate_integral()
    sigma**2

    Integral representation of the underlying calculations:

    >>> Variance(X).rewrite(Integral)
    Integral(sqrt(2)*(X - Integral(sqrt(2)*X*exp(-(X - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma), (X, -oo, oo)))**2*exp(-(X - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma), (X, -oo, oo))

    Integral representation, without expanding the PDF:

    >>> Variance(X).rewrite(Probability)
    -Integral(x*Probability(Eq(X, x)), (x, -oo, oo))**2 + Integral(x**2*Probability(Eq(X, x)), (x, -oo, oo))

    Rewrite the variance in terms of the expectation

    >>> Variance(X).rewrite(Expectation)
    -Expectation(X)**2 + Expectation(X**2)

    Some transformations based on the properties of the variance may happen:

    >>> from sympy.abc import a
    >>> Y = Normal("Y", 0, 1)
    >>> Variance(a*X)
    Variance(a*X)

    To expand the variance in its expression, use ``expand()``:

    >>> Variance(a*X).expand()
    a**2*Variance(X)
    >>> Variance(X + Y)
    Variance(X + Y)
    >>> Variance(X + Y).expand()
    2*Covariance(X, Y) + Variance(X) + Variance(Y)

    Nc                 K   sZ   t |}|jrddlm} |||S |d u rt| |}nt |}t| ||}||_|S )Nr   )VarianceMatrix)r   r^   r_   r{   r   r0   r1   )r2   rO   r3   r4   r{   r5   r"   r"   r%   r0   n  s   
zVariance.__new__c           	         s  | j d }| j t|stjS t|tr| S t|trLg }|j D ]}t|r+|| q tt	 fdd| } fdd}tt	|t
|d }|| S t|trg }g }|j D ]}t|rd|| qX||d  qXt|dkrutjS t|tt|  S | S )Nr   c                    s   t |   S r/   )r   r
   )ZxvrL   r"   r%   <lambda>  s    z!Variance.expand.<locals>.<lambda>c                    s   dt | d i  S )N   r3   )r    r
   r.   rL   r"   r%   r|     r=   r}   )r?   r1   r   r   rF   rA   r   r   rj   map	itertoolscombinationsr   r'   rh   r   )	rI   rJ   rO   r;   re   Z	variancesZmap_to_covarZcovariancesrk   r"   rL   r%   r
   |  s6   






zVariance.expandc                 K   s$   t |d |}t ||d }|| S )Nr}   r   )rI   rO   r3   r4   e1e2r"   r"   r%   _eval_rewrite_as_Expectation  s   z%Variance._eval_rewrite_as_Expectationc                 K      |  t tS r/   rT   r   r   rN   r"   r"   r%   rz        z%Variance._eval_rewrite_as_Probabilityc                 K   s   t | jd | jddS )Nr   Fr8   )r   r?   r1   rN   r"   r"   r%   rP     rQ   z"Variance._eval_rewrite_as_Integralc                 C   rR   r/   rS   rU   r"   r"   r%   rV     rW   zVariance.evaluate_integralr/   )rX   rY   rZ   r[   r0   r
   r   rz   rP   r\   rV   r"   r"   r"   r%   r   =  s    
0
!

r   c                   @   sd   e Zd ZdZdddZdd Zedd Zed	d
 ZdddZ	dddZ
dddZeZdd ZdS )r    a  
    Symbolic expression for the covariance.

    Examples
    ========

    >>> from sympy.stats import Covariance
    >>> from sympy.stats import Normal
    >>> X = Normal("X", 3, 2)
    >>> Y = Normal("Y", 0, 1)
    >>> Z = Normal("Z", 0, 1)
    >>> W = Normal("W", 0, 1)
    >>> cexpr = Covariance(X, Y)
    >>> cexpr
    Covariance(X, Y)

    Evaluate the covariance, `X` and `Y` are independent,
    therefore zero is the result:

    >>> cexpr.evaluate_integral()
    0

    Rewrite the covariance expression in terms of expectations:

    >>> from sympy.stats import Expectation
    >>> cexpr.rewrite(Expectation)
    Expectation(X*Y) - Expectation(X)*Expectation(Y)

    In order to expand the argument, use ``expand()``:

    >>> from sympy.abc import a, b, c, d
    >>> Covariance(a*X + b*Y, c*Z + d*W)
    Covariance(a*X + b*Y, c*Z + d*W)
    >>> Covariance(a*X + b*Y, c*Z + d*W).expand()
    a*c*Covariance(X, Z) + a*d*Covariance(W, X) + b*c*Covariance(Y, Z) + b*d*Covariance(W, Y)

    This class is aware of some properties of the covariance:

    >>> Covariance(X, X).expand()
    Variance(X)
    >>> Covariance(a*X, b*Y).expand()
    a*b*Covariance(X, Y)
    Nc                 K   s   t |}t |}|js|jrddlm} ||||S |dtjr+t||gtd\}}|d u r7t	
| ||}nt |}t	
| |||}||_|S )Nr   )CrossCovarianceMatrixr9   key)r   r^   r_   r   rp   r   r9   sortedr   r   r0   r1   )r2   arg1arg2r3   r4   r   r5   r"   r"   r%   r0     s   zCovariance.__new__c                    s   | j d }| j d }| j||krt| S t|stjS t|s&tjS t||gtd\}}t	|t
r@t	|t
r@t||S | | }| |   fdd|D }t|S )Nr   r!   r   c              	      s@   g | ]\}} D ]\}}|| t t||gtd di qqS )r   r3   )r    r   r   )r#   re   Zr1bZr2Zcoeff_rv_list2r3   r"   r%   r&     s
    (z%Covariance.expand.<locals>.<listcomp>)r?   r1   r   r
   r   r   rF   r   r   rA   r   r    _expand_single_argumentr   rh   )rI   rJ   r   r   Zcoeff_rv_list1Zaddendsr"   r   r%   r
     s$   


zCovariance.expandc                 C   s   t |trtj|fgS t |tr4g }|jD ]}t |tr%|| | qt	|r1|tj|f q|S t |tr?| |gS t	|rItj|fgS d S r/   )
rA   r   r   rB   r   r?   r   rj   _get_mul_nonrv_rv_tupler   )r2   r`   Zoutvalre   r"   r"   r%   r     s    




z"Covariance._expand_single_argumentc                 C   sF   g }g }|j D ]}t|r|| q|| qt|t|fS r/   )r?   r   rj   r   rh   )r2   mr;   rk   re   r"   r"   r%   r     s   
z"Covariance._get_mul_nonrv_rv_tuplec                 K   s*   t || |}t ||t || }|| S r/   r   )rI   r   r   r3   r4   r   r   r"   r"   r%   r   %  s   z'Covariance._eval_rewrite_as_Expectationc                 K   r   r/   r   rI   r   r   r3   r4   r"   r"   r%   rz   *  r   z'Covariance._eval_rewrite_as_Probabilityc                 K   s   t | jd | jd | jddS )Nr   r!   Fr8   )r   r?   r1   r   r"   r"   r%   rP   -  s   z$Covariance._eval_rewrite_as_Integralc                 C   rR   r/   rS   rU   r"   r"   r%   rV   2  rW   zCovariance.evaluate_integralr/   )rX   rY   rZ   r[   r0   r
   classmethodr   r   r   rz   rP   r\   rV   r"   r"   r"   r%   r      s    
,





r    c                       sH   e Zd ZdZd fdd	Zdd Zddd	Zdd
dZdddZ  Z	S )Momenta  
    Symbolic class for Moment

    Examples
    ========

    >>> from sympy import Symbol, Integral
    >>> from sympy.stats import Normal, Expectation, Probability, Moment
    >>> mu = Symbol('mu', real=True)
    >>> sigma = Symbol('sigma', real=True, positive=True)
    >>> X = Normal('X', mu, sigma)
    >>> M = Moment(X, 3, 1)

    To evaluate the result of Moment use `doit`:

    >>> M.doit()
    mu**3 - 3*mu**2 + 3*mu*sigma**2 + 3*mu - 3*sigma**2 - 1

    Rewrite the Moment expression in terms of Expectation:

    >>> M.rewrite(Expectation)
    Expectation((X - 1)**3)

    Rewrite the Moment expression in terms of Probability:

    >>> M.rewrite(Probability)
    Integral((x - 1)**3*Probability(Eq(X, x)), (x, -oo, oo))

    Rewrite the Moment expression in terms of Integral:

    >>> M.rewrite(Integral)
    Integral(sqrt(2)*(X - 1)**3*exp(-(X - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma), (X, -oo, oo))

    r   Nc                    sN   t |}t |}t |}|d urt |}t | ||||S t | |||S r/   r   superr0   )r2   Xncr3   r4   	__class__r"   r%   r0   Y  s   zMoment.__new__c                 K      |  tjdi |S Nr"   rT   r   r>   rI   rJ   r"   r"   r%   r>   c  rQ   zMoment.doitc                 K   s   t || | |S r/   r   rI   r   r   r   r3   r4   r"   r"   r%   r   f  s   z#Moment._eval_rewrite_as_Expectationc                 K   r   r/   r   r   r"   r"   r%   rz   i  r   z#Moment._eval_rewrite_as_Probabilityc                 K   r   r/   rT   r   r   r   r"   r"   r%   rP   l  r   z Moment._eval_rewrite_as_Integral)r   N
rX   rY   rZ   r[   r0   r>   r   rz   rP   __classcell__r"   r"   r   r%   r   6  s    "


r   c                       sH   e Zd ZdZd fdd	Zdd ZdddZdd	d
ZdddZ  Z	S )CentralMomenta1  
    Symbolic class Central Moment

    Examples
    ========

    >>> from sympy import Symbol, Integral
    >>> from sympy.stats import Normal, Expectation, Probability, CentralMoment
    >>> mu = Symbol('mu', real=True)
    >>> sigma = Symbol('sigma', real=True, positive=True)
    >>> X = Normal('X', mu, sigma)
    >>> CM = CentralMoment(X, 4)

    To evaluate the result of CentralMoment use `doit`:

    >>> CM.doit().simplify()
    3*sigma**4

    Rewrite the CentralMoment expression in terms of Expectation:

    >>> CM.rewrite(Expectation)
    Expectation((X - Expectation(X))**4)

    Rewrite the CentralMoment expression in terms of Probability:

    >>> CM.rewrite(Probability)
    Integral((x - Integral(x*Probability(True), (x, -oo, oo)))**4*Probability(Eq(X, x)), (x, -oo, oo))

    Rewrite the CentralMoment expression in terms of Integral:

    >>> CM.rewrite(Integral)
    Integral(sqrt(2)*(X - Integral(sqrt(2)*X*exp(-(X - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma), (X, -oo, oo)))**4*exp(-(X - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma), (X, -oo, oo))

    Nc                    sB   t |}t |}|d urt |}t | |||S t | ||S r/   r   )r2   r   r   r3   r4   r   r"   r%   r0     s   zCentralMoment.__new__c                 K   r   r   r   r   r"   r"   r%   r>     rQ   zCentralMoment.doitc                 K   s.   t ||fi |}t||||fi |t S r/   )r   r   rT   )rI   r   r   r3   r4   Zmur"   r"   r%   r     s   z*CentralMoment._eval_rewrite_as_Expectationc                 K   r   r/   r   rI   r   r   r3   r4   r"   r"   r%   rz     r   z*CentralMoment._eval_rewrite_as_Probabilityc                 K   r   r/   r   r   r"   r"   r%   rP     r   z'CentralMoment._eval_rewrite_as_Integralr/   r   r"   r"   r   r%   r   p  s    "	

r   ).r   Zsympyr   r   r   r   r   r   r   r	   r
   ri   r   Zsympy.core.compatibilityr   Zsympy.core.parametersr   Zsympy.core.sympifyr   Zsympy.core.relationalr   Zsympy.logic.boolalgr   Zsympy.statsr   r   Zsympy.stats.rvr   r   r   r   r   r   r   r   r   r   __all__registerr-   r   r   r   r    r   r   r"   r"   r"   r%   <module>   s,    00

` @q 	: