o
    eY                     @   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%   PD:\Projects\ConvertPro\env\Lib\site-packages\sympy/stats/symbolic_probability.py	<genexpr>   s    z_.<locals>.<genexpr>)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 r"   )r   r   __new__
_condition)clsZprob	conditionkwargsobjr%   r%   r&   r0   >   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Zgiven_conditionr%   r&   r'   [   s    z#Probability.doit.<locals>.<genexpr>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   
ValueErrorfalseZerotruer   r   r   hasattr)selfhintsr3   r6   r7   Zcondrvr:   resultr%   r<   r&   r=   H   s`   






zProbability.doitc                 K   s   | j ||djddS )Nr3   T)r7   rB   r=   rI   argr3   r4   r%   r%   r&   _eval_rewrite_as_Integral}      z%Probability._eval_rewrite_as_Integralc                 C      |  t S r"   rewriter
   r=   rI   r%   r%   r&   evaluate_integral      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&   r'          z%Expectation.expand.<locals>.<genexpr>c                 3   ra   rb   rc   rd   rL   r%   r&   r'      rf   rL   )
r>   r1   r   r@   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   rl   c                    s:   g | ]}t |ts| jd i n| qS )r%   )r@   r   rB   r=   )r#   rO   r3   rJ   rI   r%   r&   
<listcomp>  s    
z$Expectation.doit.<locals>.<listcomp>r8   r=   r%   )r?   r1   r>   r=   r   r@   r   r   rC   r   r   Zcompute_expectationrB   r   Zis_Addr   Zis_Mulr-   r   rH   )rI   rJ   rk   r`   r6   r7   rl   rK   r%   rm   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   rD   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)rk   r7   rM   rN   r%   r%   r&   rP   ;  s   z%Expectation._eval_rewrite_as_Integralc                 C   rR   r"   rS   rU   r%   r%   r&   rV   @  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   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 fd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                 3   s    | ]
}t |  V  qd S r"   )r   r   )r#   ZxvrL   r%   r&   r'     s    z"Variance.expand.<locals>.<genexpr>c                    s   dt | d i  S )N   r3   )r    r   r/   rL   r%   r&   <lambda>  s    z!Variance.expand.<locals>.<lambda>r|   )r>   r1   r   r   rF   r@   r   r   ri   map	itertoolscombinationsr   r(   rg   r   )	rI   rJ   rO   r;   re   Z	variancesZmap_to_covarZcovariancesrj   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   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   )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   r1br2Zcoeff_rv_list2r3   r%   r&   rn     s
    (z%Covariance.expand.<locals>.<listcomp>)r>   r1   r   r   r   r   rF   r   r   r@   r   r    _expand_single_argumentr   rg   )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"   )
r@   r   r   rA   r   r>   r   ri   _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   ri   r   rg   )r2   mr;   rj   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   2  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   5  s   z$Covariance._eval_rewrite_as_Integralc                 C   rR   r"   rS   rU   r%   r%   r&   rV   :  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', 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   a  s   zMoment.__new__c                 K      |  tjdi |S Nr%   rT   r   r=   rI   rJ   r%   r%   r&   r=   k  rQ   zMoment.doitc                 K   s   t || | |S r"   r   rI   r   r   r   r3   r4   r%   r%   r&   r   n  s   z#Moment._eval_rewrite_as_Expectationc                 K   r   r"   r   r   r%   r%   r&   rz   q  r   z#Moment._eval_rewrite_as_Probabilityc                 K   r   r"   rT   r   r
   r   r%   r%   r&   rP   t  r   z Moment._eval_rewrite_as_Integral)r   N
rX   rY   rZ   r[   r0   r=   r   rz   rP   __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   )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   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   x  s    "	

r   )5r   Zsympy.concrete.summationsr   Zsympy.core.addr   Zsympy.core.exprr   Zsympy.core.functionr   rh   Zsympy.core.mulr   Zsympy.core.relationalr   Zsympy.core.singletonr   Zsympy.core.symbolr	   Zsympy.integrals.integralsr
   Zsympy.logic.boolalgr   Zsympy.core.parametersr   Zsympy.core.sortingr   Zsympy.core.sympifyr   r   r   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

` @q 	: