o
    *ήcY                     @   s  d dl 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 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Z0G dd deZ1G dd deZ2G dd  d eZ3G d!d" d"eZ4dS )#    N)Sum)Add)Expr)expand)Mul)Eq)S)Symbol)Integral)Not)global_parameters)default_sort_key)_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    | ]}t |V  qd S N)r   ).0i r%   G/tmp/pip-target-vg8gfxp4/lib/python/sympy/stats/symbolic_probability.py	<genexpr>   s    z_.<locals>.<genexpr>)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 r"   )r   r   __new__
_condition)clsprob	conditionkwargsobjr%   r%   r&   r1   >   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                 3   s    | ]}t | V  qd S r"   )r   )r#   rvgiven_conditionr%   r&   r'   [   s    z#Probability.doit.<locals>.<genexpr>z4%s is not a relational or combination of relationals)r8   doitr%   )argsr2   get
isinstancer   r   Onefuncr@   hasr   r   probabilityr   r   r)   sympy.stats.frv_typesr<   r,   r   r   r   
ValueErrorfalseZerotruer   r   r   hasattr)selfhintsr5   r8   r9   condrvr<   resultr%   r>   r&   r@   H   s`   






zProbability.doitc                 K   s   | j ||djddS )Nr5   T)r9   rE   r@   rN   argr5   r6   r%   r%   r&   _eval_rewrite_as_Integral}      z%Probability._eval_rewrite_as_Integralc                 C      |  t S r"   rewriter
   r@   rN   r%   r%   r&   evaluate_integral      zProbability.evaluate_integralr"   )	__name__
__module____qualname____doc__r1   r@   rV   _eval_rewrite_as_Sumr\   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_probabilityrc   r   r   r1   r2   )r3   exprr5   r6   rc   r7   r%   r%   r&   r1      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 rR   Nr   r   r#   arR   r%   r&   r'          z%Expectation.expand.<locals>.<genexpr>c                 3   rg   rh   ri   rj   rR   r%   r&   r'      rl   rR   )
rA   r2   r   rC   r   fromiter_expandr   appendr   )rN   rO   rf   expand_exprr=   nonrvrk   r%   rR   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   r8   Fr9   evalf)r8   rs   c                    s:   g | ]}t |ts| jd i n| qS )r%   )rC   r   rE   r@   )r#   rU   r5   rO   rN   r%   r&   
<listcomp>  s    
z$Expectation.doit.<locals>.<listcomp>r:   r@   r%   )rB   r2   rA   r@   r   rC   r   r   rF   r   r   compute_expectationrE   r   is_Addr   is_Mulr.   r   rM   )rN   rO   rr   rf   r8   r9   rs   rQ   r%   rt   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 known_1)r.   r   r)   NotImplementedErrorpopr   rI   symbolnameisupperr	   loweris_Continuousr
   replacer   r   domainsetinfsup	is_Finiter   )rN   rU   r5   r6   rvsr=   r|   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 )NrR   FT)rr   r9   rS   rT   r%   r%   r&   rV   ;  s   z%Expectation._eval_rewrite_as_Integralc                 C   rX   r"   rY   r[   r%   r%   r&   r\   @  r]   zExpectation.evaluate_integralr"   )r^   r_   r`   ra   r1   r   r@   r   rV   rb   r\   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   rd   re   r   r   r1   r2   )r3   rU   r5   r6   r   r7   r%   r%   r&   r1   v  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   )xvrR   r%   r&   <lambda>  s    z!Variance.expand.<locals>.<lambda>c                    s   dt | d i  S )N   r5   )r    r   r0   rR   r%   r&   r     s    r   )rA   r2   r   r   rK   rC   r   r   ro   map	itertoolscombinationsr   r)   rm   r   )	rN   rO   rU   r=   rk   	variancesmap_to_covarcovariancesrq   r%   rR   r&   r     s6   






zVariance.expandc                 K   s$   t |d |}t ||d }|| S )Nr   r   )rN   rU   r5   r6   e1e2r%   r%   r&   _eval_rewrite_as_Expectation  s   z%Variance._eval_rewrite_as_Expectationc                 K      |  t tS r"   rZ   r   r   rT   r%   r%   r&   r        z%Variance._eval_rewrite_as_Probabilityc                 K   s   t | jd | jddS )Nr   Fr:   )r   rA   r2   rT   r%   r%   r&   rV     rW   z"Variance._eval_rewrite_as_Integralc                 C   rX   r"   rY   r[   r%   r%   r&   r\     r]   zVariance.evaluate_integralr"   )r^   r_   r`   ra   r1   r   r   r   rV   rb   r\   r%   r%   r%   r&   r   E  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   )CrossCovarianceMatrixr;   key)r   rd   re   r   r{   r   r;   sortedr   r   r1   r2   )r3   arg1arg2r5   r6   r   r7   r%   r%   r&   r1     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   r5   )r    r   r   )r#   rk   r1br2coeff_rv_list2r5   r%   r&   ru     s
    (z%Covariance.expand.<locals>.<listcomp>)rA   r2   r   r   r   r   rK   r   r   rC   r   r    _expand_single_argumentr   rm   )rN   rO   r   r   coeff_rv_list1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"   )
rC   r   r   rD   r   rA   r   ro   _get_mul_nonrv_rv_tupler   )r3   rf   outvalrk   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"   )rA   r   ro   r   rm   )r3   mr=   rq   rk   r%   r%   r&   r   "  s   
z"Covariance._get_mul_nonrv_rv_tuplec                 K   s*   t || |}t ||t || }|| S r"   r   )rN   r   r   r5   r6   r   r   r%   r%   r&   r   -  s   z'Covariance._eval_rewrite_as_Expectationc                 K   r   r"   r   rN   r   r   r5   r6   r%   r%   r&   r   2  r   z'Covariance._eval_rewrite_as_Probabilityc                 K   s   t | jd | jd | jddS )Nr   r!   Fr:   )r   rA   r2   r   r%   r%   r&   rV   5  s   z$Covariance._eval_rewrite_as_Integralc                 C   rX   r"   rY   r[   r%   r%   r&   r\   :  r]   zCovariance.evaluate_integralr"   )r^   r_   r`   ra   r1   r   classmethodr   r   r   r   rV   rb   r\   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', 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   superr1   )r3   Xncr5   r6   	__class__r%   r&   r1   a  s   zMoment.__new__c                 K      |  tjdi |S Nr%   rZ   r   r@   rN   rO   r%   r%   r&   r@   k  rW   zMoment.doitc                 K   s   t || | |S r"   r   rN   r   r   r   r5   r6   r%   r%   r&   r   n  s   z#Moment._eval_rewrite_as_Expectationc                 K   r   r"   r   r   r%   r%   r&   r   q  r   z#Moment._eval_rewrite_as_Probabilityc                 K   r   r"   rZ   r   r
   r   r%   r%   r&   rV   t  r   z Moment._eval_rewrite_as_Integral)r   N
r^   r_   r`   ra   r1   r@   r   r   rV   __classcell__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 )CentralMomenta&  
    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', 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   )r3   r   r   r5   r6   r   r%   r&   r1     s   zCentralMoment.__new__c                 K   r   r   r   r   r%   r%   r&   r@     rW   zCentralMoment.doitc                 K   s.   t ||fi |}t||||fi |t S r"   )r   r   rZ   )rN   r   r   r5   r6   mur%   r%   r&   r     s   z*CentralMoment._eval_rewrite_as_Expectationc                 K   r   r"   r   rN   r   r   r5   r6   r%   r%   r&   r     r   z*CentralMoment._eval_rewrite_as_Probabilityc                 K   r   r"   r   r   r%   r%   r&   rV     r   z'CentralMoment._eval_rewrite_as_Integralr"   r   r%   r%   r   r&   r   x  s    "	

r   )5r   sympy.concrete.summationsr   sympy.core.addr   sympy.core.exprr   sympy.core.functionr   rn   sympy.core.mulr   sympy.core.relationalr   sympy.core.singletonr   sympy.core.symbolr	   sympy.integrals.integralsr
   sympy.logic.boolalgr   sympy.core.parametersr   sympy.core.sortingr   sympy.core.sympifyr   r   r   sympy.statsr   r   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

` @q 	: