o
    ohZ                     @   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%   t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/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                   @   s<   e Zd 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))
    TNc                 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rf|d  krfddlm} || |jdi |ddS t fd	d
|D rvt| 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evaluateTr9   r!   )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?   J   s`   






zProbability.doitc                 K   s   | j ||djddS )Nr5   Fr:   rD   r?   rM   argr5   r6   r%   r%   r&   _eval_rewrite_as_Integral      z%Probability._eval_rewrite_as_Integralc                 C      |  t S r"   rewriter
   r?   rM   r%   r%   r&   evaluate_integral      zProbability.evaluate_integralr"   )
__name__
__module____qualname____doc__is_commutativer1   r?   rU   _eval_rewrite_as_Sumr[   r%   r%   r%   r&   r   %   s    


5r   c                   @   sV   e Zd ZdZdddZ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(Expectation(2*X) + Y))

    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                 C      | j d jS Nr   r@   ra   rZ   r%   r%   r&   _eval_is_commutative      z Expectation._eval_is_commutativec                    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 rQ   Nr   r   r#   arQ   r%   r&   r'          z%Expectation.expand.<locals>.<genexpr>c                 3   rl   rm   rn   ro   rQ   r%   r&   r'      rq   rQ   )
r@   r2   r   rB   r   fromiter_expandr   appendr   )rM   rN   rf   expand_exprr<   nonrvrp   r%   rQ   r&   r      s,   




zExpectation.expandc                    s>   dd}j jd } dd} dd}|r$|jdi }t|r-t|tr/|S |r? dd}t| ||dS |t	rLt
|| S  d ur^t| jdi S |jrot fd	d
|jD  S |jry|try|S t
|t kr|S t
|j||d}t|dr|r|jdi S |S )NdeepTr   r8   Fr9   evalf)r8   rx   c                    s:   g | ]}t |ts| jd i n| qS )r%   )rB   r   rD   r?   )r#   rT   r5   rN   rM   r%   r&   
<listcomp>  s    
z$Expectation.doit.<locals>.<listcomp>r:   r?   r%   )rA   r2   r@   r?   r   rB   r   r   rE   r   r   compute_expectationrD   r   is_Addr   is_Mulr.   r   rL   )rM   rN   rw   rf   r8   r9   rx   rP   r%   ry   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   rH   symbolnameisupperr	   loweris_Continuousr
   replacer   r   domainsetinfsup	is_Finiter   )rM   rT   r5   r6   rvsr<   r   r%   r%   r&   _eval_rewrite_as_Probability'  s"   

86z(Expectation._eval_rewrite_as_ProbabilityFc                 K   s   | j ||djd|dS )NrQ   F)rw   r9   rR   )rM   rT   r5   r9   r6   r%   r%   r&   rU   @  s   z%Expectation._eval_rewrite_as_Integralc                 C   rW   r"   rX   rZ   r%   r%   r&   r[   E  r\   zExpectation.evaluate_integralr"   )NF)r]   r^   r_   r`   r1   rj   r   r?   r   rU   rb   r[   evaluate_sumr%   r%   r%   r&   r      s    
E
+
r   c                   @   sT   e Zd ZdZdddZ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   rT   r5   r6   r   r7   r%   r%   r&   r1   {  s   
zVariance.__new__c                 C   rg   rh   ri   rZ   r%   r%   r&   rj     rk   zVariance._eval_is_commutativec           	         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#   xvrQ   r%   r&   r'     s    z"Variance.expand.<locals>.<genexpr>c                    s   dt | d i  S )N   r5   )r    r   r0   rQ   r%   r&   <lambda>  s    z!Variance.expand.<locals>.<lambda>r   )r@   r2   r   r   rJ   rB   r   r   rt   map	itertoolscombinationsr   r)   rr   r   )	rM   rN   rT   r<   rp   	variancesmap_to_covarcovariancesrv   r%   rQ   r&   r     s6   






zVariance.expandc                 K   s$   t |d |}t ||d }|| S )Nr   r   )rM   rT   r5   r6   e1e2r%   r%   r&   _eval_rewrite_as_Expectation  s   z%Variance._eval_rewrite_as_Expectationc                 K      |  t tS r"   rY   r   r   rS   r%   r%   r&   r        z%Variance._eval_rewrite_as_Probabilityc                 K   s   t | jd | jddS )Nr   Fr:   )r   r@   r2   rS   r%   r%   r&   rU     rV   z"Variance._eval_rewrite_as_Integralc                 C   rW   r"   rX   rZ   r%   r%   r&   r[     r\   zVariance.evaluate_integralr"   )r]   r^   r_   r`   r1   rj   r   r   r   rU   rb   r[   r%   r%   r%   r&   r   J  s    
0
!

r   c                   @   sl   e Zd ZdZdddZ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   rd   re   r   r   r   r9   sortedr   r   r1   r2   )r3   arg1arg2r5   r6   r   r7   r%   r%   r&   r1     s   zCovariance.__new__c                 C   rg   rh   ri   rZ   r%   r%   r&   rj     rk   zCovariance._eval_is_commutativec                    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#   rp   r1br2coeff_rv_list2r5   r%   r&   rz     s
    (z%Covariance.expand.<locals>.<listcomp>)r@   r2   r   r   r   r   rJ   r   r   rB   r   r    _expand_single_argumentr   rr   )rM   rN   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"   )
rB   r   r   rC   r   r@   r   rt   _get_mul_nonrv_rv_tupler   )r3   rf   outvalrp   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   rt   r   rr   )r3   mr<   rv   rp   r%   r%   r&   r   -  s   
z"Covariance._get_mul_nonrv_rv_tuplec                 K   s*   t || |}t ||t || }|| S r"   r   )rM   r   r   r5   r6   r   r   r%   r%   r&   r   8  s   z'Covariance._eval_rewrite_as_Expectationc                 K   r   r"   r   rM   r   r   r5   r6   r%   r%   r&   r   =  r   z'Covariance._eval_rewrite_as_Probabilityc                 K   s   t | jd | jd | jddS )Nr   r!   Fr:   )r   r@   r2   r   r%   r%   r&   rU   @  s   z$Covariance._eval_rewrite_as_Integralc                 C   rW   r"   rX   rZ   r%   r%   r&   r[   E  r\   zCovariance.evaluate_integralr"   )r]   r^   r_   r`   r1   rj   r   classmethodr   r   r   r   rU   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   l  s   zMoment.__new__c                 K      |  tjdi |S Nr%   rY   r   r?   rM   rN   r%   r%   r&   r?   v  rV   zMoment.doitc                 K   s   t || | |S r"   r   rM   r   r   r   r5   r6   r%   r%   r&   r   y  s   z#Moment._eval_rewrite_as_Expectationc                 K   r   r"   r   r   r%   r%   r&   r   |  r   z#Moment._eval_rewrite_as_Probabilityc                 K   r   r"   rY   r   r
   r   r%   r%   r&   rU     r   z Moment._eval_rewrite_as_Integral)r   N
r]   r^   r_   r`   r1   r?   r   r   rU   __classcell__r%   r%   r   r&   r   I  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((-Expectation(X) + 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?     rV   zCentralMoment.doitc                 K   s.   t ||fi |}t||||fi |t S r"   )r   r   rY   )rM   r   r   r5   r6   mur%   r%   r&   r     s   z*CentralMoment._eval_rewrite_as_Expectationc                 K   r   r"   r   rM   r   r   r5   r6   r%   r%   r&   r     r   z*CentralMoment._eval_rewrite_as_Probabilityc                 K   r   r"   r   r   r%   r%   r&   rU     r   z'CentralMoment._eval_rewrite_as_Integralr"   r   r%   r%   r   r&   r     s    "	

r   )5r   sympy.concrete.summationsr   sympy.core.addr   sympy.core.exprr   sympy.core.functionr   rs   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

c Ct :