U
    9%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+ ddddg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%   _/var/www/html/Darija-Ai-API/env/lib/python3.8/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 k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rXtj| j|j d  |djf | S |	t
rvt|j| |dS t trt|}t|dkr|d  krddlm} || |jf |ddS t fdd	|D rt| S t| S  d k	rt ttfstd
   dks.|tjkr4tjS t|ttfsPtd
| |tjkrbtjS |rvt| |dS  d k	rtt|  S t|t krt| S t||}t|dr|r| S |S d 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   doit)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}   s    z%Probability._eval_rewrite_as_Integralc                 C   s   |  t S r"   rewriter
   r=   rI   r%   r%   r&   evaluate_integral   s    zProbability.evaluate_integral)N)N)	__name__
__module____qualname____doc__r0   r=   rP   _eval_rewrite_as_SumrT   r%   r%   r%   r&   r   &   s   

5
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 krFt|s8|S t| |}nt |}t| ||}||_|S )Nr   )ExpectationMatrix)r   	is_Matrix-sympy.stats.symbolic_multivariate_probabilityrZ   r   r   r0   r1   )r2   exprr3   r4   rZ   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rlt fdd|j D S t|trg }g }|j D ]"}t|r|| q|| qt|t	t| d S | S )Nr   c                 3   s   | ]}t | d  V  qdS rL   Nr   r   r#   arL   r%   r&   r'      s   z%Expectation.expand.<locals>.<genexpr>c                 3   s   | ]}t | d  V  qdS r^   r_   r`   rL   r%   r&   r'      s   rL   )
r>   r1   r   r@   r   fromiter_expandr   appendr   )rI   rJ   r]   Zexpand_exprr;   nonrvra   r%   rL   r&   r      s,    




zExpectation.expandc                    s>   dd}j jd } dd} dd }|rF|jf }t|rXt|tr\|S |r| dd}t| ||dS |t	rt
|| S  d k	rt| jf S |jrt fd	d
|jD  S |jr|tr|S t
|t kr|S t
|j||d}t|dr6|r6|jf S |S d S )NdeepTr   r6   Fr7   evalf)r6   rg   c                    s6   g | ].}t |ts&| jf 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?   r1   r>   r=   r   r@   r   r   rC   r   r   Zcompute_expectationrB   r   Zis_Addr   Zis_Mulr-   r   rH   )rI   rJ   rf   r]   r6   r7   rg   rK   r%   rh   r&   r=      s:    



zExpectation.doitc                 K   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rt|||tt||| ||jjjj|jjjjfS |jjrtn6t|||tt||| ||jjjj|jjjfS d 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;   rl   r%   r%   r&   _eval_rewrite_as_Probability"  s"    

8z(Expectation._eval_rewrite_as_Probabilityc                 K   s   | j ||djdddS )NrL   FT)rf   r7   rM   rN   r%   r%   r&   rP   ;  s    z%Expectation._eval_rewrite_as_Integralc                 C   s   |  t S r"   rQ   rS   r%   r%   r&   rT   @  s    zExpectation.evaluate_integral)N)N)N)rU   rV   rW   rX   r0   r   r=   ru   rP   rY   rT   Zevaluate_sumr%   r%   r%   r&   r      s   E
+

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 kr:t| |}nt |}t| ||}||_|S )Nr   )VarianceMatrix)r   r[   r\   rv   r   r0   r1   )r2   rO   r3   r4   rv   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rg }|j D ]}t|r@|| q@t fdd|D  } fdd}tt	|t
|d }|| S t|trg }g }|j D ]&}t|r|| q||d  qt|dkrt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>      z!Variance.expand.<locals>.<lambda>rw   )r>   r1   r   r   rF   r@   r   r   rd   map	itertoolscombinationsr   r(   rb   r   )	rI   rJ   rO   r;   ra   Z	variancesZmap_to_covarZcovariancesre   r%   rL   r&   r     s4    




zVariance.expandc                 K   s$   t |d |}t ||d }|| S )Nrw   r   )rI   rO   r3   r4   e1e2r%   r%   r&   _eval_rewrite_as_Expectation  s    z%Variance._eval_rewrite_as_Expectationc                 K   s   |  t tS r"   rR   r   r   rN   r%   r%   r&   ru     s    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     s    z"Variance._eval_rewrite_as_Integralc                 C   s   |  t S r"   rQ   rS   r%   r%   r&   rT     s    zVariance.evaluate_integral)N)N)N)N)rU   rV   rW   rX   r0   r   r   ru   rP   rY   rT   r%   r%   r%   r&   r   E  s   0
!


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r4ddlm} ||||S |dtjrVt||gtd\}}|d krnt	
| ||}nt |}t	
| |||}||_|S )Nr   )CrossCovarianceMatrixr9   key)r   r[   r\   r   rk   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r0t| S t|s>tjS t|sLtjS t||gtd\}}t	|t
rt	|t
rt||S | | }| |   fdd|D }t|S )Nr   r!   r   c              	      s@   g | ]8\}} D ]*\}}|| t t||gtd di qqS )r   r3   )r    r   r   )r#   ra   r1br2Zcoeff_rv_list2r3   r%   r&   ri     s     z%Covariance.expand.<locals>.<listcomp>)r>   r1   r   r   r   r   rF   r   r   r@   r   r    _expand_single_argumentr   rb   )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rhg }|jD ]8}t |trJ|| | q*t	|r*|tj|f q*|S t |tr~| |gS t	|rtj|fgS d S r"   )
r@   r   r   rA   r   r>   r   rd   _get_mul_nonrv_rv_tupler   )r2   r]   Zoutvalra   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   rd   r   rb   )r2   mr;   re   ra   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   s   |  t tS r"   r   rI   r   r   r3   r4   r%   r%   r&   ru   2  s    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   s   |  t S r"   rQ   rS   r%   r%   r&   rT   :  s    zCovariance.evaluate_integral)N)N)N)N)rU   rV   rW   rX   r0   r   classmethodr   r   r   ru   rP   rY   rT   r%   r%   r%   r&   r      s   ,






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                    sR   t |}t |}t |}|d k	r<t |}t | ||||S t | |||S d S r"   r   superr0   )r2   Xncr3   r4   	__class__r%   r&   r0   a  s    zMoment.__new__c                 K   s   |  tjf |S r"   rR   r   r=   rI   rJ   r%   r%   r&   r=   k  s    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   s   |  t tS r"   r   r   r%   r%   r&   ru   q  s    z#Moment._eval_rewrite_as_Probabilityc                 K   s   |  t tS r"   rR   r   r
   r   r%   r%   r&   rP   t  s    z Moment._eval_rewrite_as_Integral)r   N)r   N)r   N)r   N
rU   rV   rW   rX   r0   r=   r   ru   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                    sF   t |}t |}|d k	r2t |}t | |||S t | ||S d S r"   r   )r2   r   r   r3   r4   r   r%   r&   r0     s    zCentralMoment.__new__c                 K   s   |  tjf |S r"   r   r   r%   r%   r&   r=     s    zCentralMoment.doitc                 K   s&   t ||f|}t||||f|t S r"   )r   r   rR   )rI   r   r   r3   r4   mur%   r%   r&   r     s    z*CentralMoment._eval_rewrite_as_Expectationc                 K   s   |  t tS r"   r   rI   r   r   r3   r4   r%   r%   r&   ru     s    z*CentralMoment._eval_rewrite_as_Probabilityc                 K   s   |  t tS r"   r   r   r%   r%   r&   rP     s    z'CentralMoment._eval_rewrite_as_Integral)N)N)N)Nr   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   rc   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 	: