B
    ™‘[’2  ã               @   sÐ   d dl Z d dlmZ d dl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 d dlmZ d dlmZmZ d dlmZmZmZ dd	d
dg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)Ú_sympify)Údefault_sort_key)
ÚExprÚAddÚMulÚSÚIntegralÚEqÚSumÚSymbolÚDummyÚBasic)Úglobal_evaluate)ÚvarianceÚ
covariance)ÚRandomSymbolÚprobabilityÚexpectationÚProbabilityÚExpectationÚVarianceÚ
Covariancec               @   s6   e Zd ZdZddd„Zddd„Zddd„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 krt | |¡}nt |ƒ}t | ||¡}||_|S )N)r   r   Ú__new__Ú
_condition)ÚclsZprobÚ	conditionÚkwargsÚobj© r   ú?lib/python3.7/site-packages/sympy/stats/symbolic_probability.pyr   '   s    zProbability.__new__c             C   s   t ||ddS )NF)Úevaluate)r   )ÚselfÚargr   r   r   r   Ú_eval_rewrite_as_Integral1   s    z%Probability._eval_rewrite_as_Integralc             C   s
   |   t¡S )N)Úrewriter   )r!   r"   r   r   r   r   Ú_eval_rewrite_as_Sum4   s    z Probability._eval_rewrite_as_Sumc             C   s   |   t¡ ¡ S )N)r$   r   Údoit)r!   r   r   r   Úevaluate_integral7   s    zProbability.evaluate_integral)N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r%   r'   r   r   r   r   r      s
   



c               @   sH   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dd„ Z	dS )r   aÔ  
    Symbolic expression for the expectation.

    Examples
    ========

    >>> from sympy.stats import Expectation, Normal, Probability
    >>> from sympy import symbols, Integral
    >>> 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))

    This class is aware of some properties of the expectation:

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

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

    >>> Expectation(X + Y).doit()
    Expectation(X) + Expectation(Y)
    >>> Expectation(a*X + Y).doit()
    a*Expectation(X) + Expectation(Y)
    >>> Expectation(a*X + Y)
    Expectation(a*X + Y)
    Nc             K   sL   t |ƒ}|d kr,| t¡s|S t | |¡}nt |ƒ}t | ||¡}||_|S )N)r   Úhasr   r   r   r   )r   Úexprr   r   r   r   r   r   r   i   s    
zExpectation.__new__c                s¨   | j d }| j‰ | t¡s|S t|tƒr@t‡ fdd„|j D ƒŽ S t|tƒr¤g }g }x8|j D ].}t|tƒsr| t¡r~| |¡ qZ| |¡ qZW t|Ž tt|Ž ˆ d S | S )Nr   c                s   g | ]}t |ˆ d  ¡ ‘qS ))r   )r   r&   )Ú.0Úa)r   r   r   ú
<listcomp>}   s    z$Expectation.doit.<locals>.<listcomp>)r   )	Úargsr   r,   r   Ú
isinstancer   r   Úappendr   )r!   Úhintsr-   ÚrvÚnonrvr/   r   )r   r   r&   u   s    



zExpectation.doitc             C   s   |  t¡}t|ƒdkrtƒ ‚t|ƒdkr,|S | ¡ }|jd krFtdƒ‚|j}|jd  	¡ rjt
|j ¡ ƒ}nt
|jd ƒ}|jjr¸t| ||¡tt||ƒ|ƒ ||jjjj|jjjjfƒS |jjrÆt‚n6t| ||¡tt||ƒ|ƒ ||jjjj|jjjfƒS d S )Né   r   zProbability space not knownZ_1)Zatomsr   ÚlenÚNotImplementedErrorÚpopZpspaceÚ
ValueErrorÚsymbolÚnameÚisupperr   ÚlowerZis_Continuousr   Úreplacer   r	   ZdomainÚsetÚinfZsupZ	is_Finiter
   )r!   r"   r   Zrvsr5   r<   r   r   r   Ú_eval_rewrite_as_ProbabilityŠ   s"    

8z(Expectation._eval_rewrite_as_Probabilityc             C   s   t ||ddS )NF)r   r    )r   )r!   r"   r   r   r   r   r#   £   s    z%Expectation._eval_rewrite_as_Integralc             C   s
   |   t¡S )N)r$   r   )r!   r"   r   r   r   r   r%   ¦   s    z Expectation._eval_rewrite_as_Sumc             C   s   |   t¡ ¡ S )N)r$   r   r&   )r!   r   r   r   r'   ©   s    zExpectation.evaluate_integral)N)N)N)N)
r(   r)   r*   r+   r   r&   rC   r#   r%   r'   r   r   r   r   r   ;   s   ,



c               @   sR   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ddd„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 ``doit()``:

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

    Nc             K   s>   t |ƒ}|d krt | |¡}nt |ƒ}t | ||¡}||_|S )N)r   r   r   r   )r   r"   r   r   r   r   r   r   r   Þ   s    zVariance.__new__c       	         s  | j d }| j‰ | t¡s tjS t|tƒr.| S t|tƒr g }x"|j D ]}| t¡rD| |¡ qDW tt	‡ fdd„|ƒŽ }‡ fdd„}tt	|t
 |d¡ƒŽ }|| S t|tƒrg }g }x2|j D ](}| t¡rÖ| |¡ q¼| |d ¡ q¼W t|ƒdkrútjS t|Ž tt|Ž ˆ ƒ S | S )Nr   c                s   t | ˆ ƒ ¡ S )N)r   r&   )Zxv)r   r   r   Ú<lambda>ö   s    zVariance.doit.<locals>.<lambda>c                s   dt | dˆ iŽ ¡  S )Né   r   )r   r&   )Úx)r   r   r   rD   ÷   s    rE   )r1   r   r,   r   r   ÚZeror2   r   r3   ÚmapÚ	itertoolsÚcombinationsr   r8   r   )	r!   r4   r"   r5   r/   Z	variancesZmap_to_covarZcovariancesr6   r   )r   r   r&   è   s4    





zVariance.doitc             C   s$   t |d |ƒ}t ||ƒd }|| S )NrE   )r   )r!   r"   r   Úe1Úe2r   r   r   Ú_eval_rewrite_as_Expectation	  s    z%Variance._eval_rewrite_as_Expectationc             C   s   |   t¡  t¡S )N)r$   r   r   )r!   r"   r   r   r   r   rC     s    z%Variance._eval_rewrite_as_Probabilityc             C   s   t | jd | jddS )Nr   F)r    )r   r1   r   )r!   r"   r   r   r   r   r#     s    z"Variance._eval_rewrite_as_Integralc             C   s
   |   t¡S )N)r$   r   )r!   r"   r   r   r   r   r%     s    zVariance._eval_rewrite_as_Sumc             C   s   |   t¡ ¡ S )N)r$   r   r&   )r!   r   r   r   r'     s    zVariance.evaluate_integral)N)N)N)N)N)r(   r)   r*   r+   r   r&   rM   rC   r#   r%   r'   r   r   r   r   r   ­   s   0

!



c               @   sj   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ddd„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 ``doit()``:

    >>> 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).doit()
    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).doit()
    Variance(X)
    >>> Covariance(a*X, b*Y).doit()
    a*b*Covariance(X, Y)
    Nc             K   sn   t |ƒ}t |ƒ}| dtd ¡r4t||gtd\}}|d krLt | ||¡}nt |ƒ}t | |||¡}||_|S )Nr    r   )Úkey)r   r:   r   Úsortedr   r   r   r   )r   Úarg1Úarg2r   r   r   r   r   r   r   H  s    zCovariance.__new__c                s¼   | j d }| j d }| j‰||kr0t|ˆƒ ¡ S | t¡s@tjS | t¡sPtjS t||gt	d\}}t
|tƒr„t
|tƒr„t||ˆƒS |  | ¡ ¡}|  | ¡ ¡‰ ‡ ‡fdd„|D ƒ}t|Ž S )Nr   r7   )rN   c          	      s@   g | ]8\}}ˆ D ]*\}}|| t t||gtd dˆiŽ ‘qqS ))rN   r   )r   rO   r   )r.   r/   Zr1ÚbZr2)Úcoeff_rv_list2r   r   r   r0   l  s   z#Covariance.doit.<locals>.<listcomp>)r1   r   r   r&   r,   r   r   rG   rO   r   r2   r   Ú_expand_single_argumentÚexpandr   )r!   r4   rP   rQ   Zcoeff_rv_list1Zaddendsr   )rS   r   r   r&   W  s"    



zCovariance.doitc             C   sž   t |tƒrtj|fgS t |tƒrng }xD|jD ]:}t |tƒrL| |  |¡¡ q,t |tƒr,| tj|f¡ q,W |S t |tƒr„|  |¡gS | 	t¡rštj|fgS d S )N)
r2   r   r   ZOner   r1   r   r3   Ú_get_mul_nonrv_rv_tupler,   )r   r-   Zoutvalr/   r   r   r   rT   p  s    





z"Covariance._expand_single_argumentc             C   sH   g }g }x.|j D ]$}| t¡r*| |¡ q| |¡ qW t|Ž t|Ž fS )N)r1   r,   r   r3   r   )r   Úmr5   r6   r/   r   r   r   rV   ƒ  s    
z"Covariance._get_mul_nonrv_rv_tuplec             C   s*   t || |ƒ}t ||ƒt ||ƒ }|| S )N)r   )r!   rP   rQ   r   rK   rL   r   r   r   rM   Ž  s    z'Covariance._eval_rewrite_as_Expectationc             C   s   |   t¡  t¡S )N)r$   r   r   )r!   rP   rQ   r   r   r   r   rC   “  s    z'Covariance._eval_rewrite_as_Probabilityc             C   s   t | jd | jd | jddS )Nr   r7   F)r    )r   r1   r   )r!   rP   rQ   r   r   r   r   r#   –  s    z$Covariance._eval_rewrite_as_Integralc             C   s
   |   t¡S )N)r$   r   )r!   rP   rQ   r   r   r   r   r%   ™  s    zCovariance._eval_rewrite_as_Sumc             C   s   |   t¡ ¡ S )N)r$   r   r&   )r!   r   r   r   r'   œ  s    zCovariance.evaluate_integral)N)N)N)N)N)r(   r)   r*   r+   r   r&   ÚclassmethodrT   rV   rM   rC   r#   r%   r'   r   r   r   r   r     s   +




)rI   Zsympy.core.sympifyr   Zsympy.core.compatibilityr   Zsympyr   r   r   r   r   r	   r
   r   r   r   Zsympy.core.evaluater   Zsympy.statsr   r   Zsympy.stats.rvr   r   r   Ú__all__r   r   r   r   r   r   r   r   Ú<module>   s   0,rn