
    EdY                        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(j-        e          d             Z. e(j-        e"          d             Z. G d de          Z/ G d de          Z0 G d de          Z1 G d de          Z2 G d de          Z3 G 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                     | j         }t          |          dk    r"t          t          |                    | k    rdS t	          d |D                       S )N   Fc              3   4   K   | ]}t          |          V  d S N)r   ).0is     @lib/python3.11/site-packages/sympy/stats/symbolic_probability.py	<genexpr>z_.<locals>.<genexpr>   s(      ++y||++++++    )free_symbolslennextiterany)xatomss     r(   _r2      sW    NE
5zzQ 4U,,1 u++U++++++r*   c                     dS )NT )r0   s    r(   r2   r2   !   s    4r*   c                   2    e Zd ZdZddZd ZddZeZ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                     t          |          }|t          j        | |          }n%t          |          }t          j        | ||          }||_        |S r%   )r   r   __new__
_condition)clsprob	conditionkwargsobjs        r(   r7   zProbability.__new__>   sT    ~~ 	5,sD))CC ++I,sD)44C"
r*   c                    | j         d         }| j        |                    dd          }|                    dd           }t          |t                    r;t
          j         |                     |j         d         |          j        di |z
  S |	                    t                    r%t          |                              ||          S t          t                    rt          |          }t          |          dk    r=|d         k    r1ddlm}  | |                     |          j        di |dd          S t%          fd|D                       rt'          |          S t'          |                                          S .t          t(          t*          f          st-          d	z            dk    s|t
          j        u rt
          j        S t          |t(          t*          f          st-          d	|z            |t
          j        u rt
          j        S |rt5          ||
          S /t'          t7          |                                                    S t          |          t9                      k    rt'          |          S t          |                              |          }t;          |d          r|r|                                S |S )Nr   
numsamplesFfor_rewriteevaluater#   )BernoulliDistributionc              3   8   K   | ]}t          |          V  d S r%   )r   )r&   rvgiven_conditions     r(   r)   z#Probability.doit.<locals>.<genexpr>[   s-      CCb9R11CCCCCCr*   z4%s is not a relational or combination of relationals)r?   doitr4   )argsr8   get
isinstancer   r	   OnefuncrG   hasr   r   probabilityr   r   r,   sympy.stats.frv_typesrC   r/   r   r   r   
ValueErrorfalseZerotruer   r   r   hasattr)	selfhintsr;   r?   r@   condrvrC   resultrF   s	           @r(   rG   zProbability.doitH   s4   IaL	/YY|U33
))M5999i%% 	H5 ?499Y^A%6-8 % : ::>H HAFH H H H ==,-- 	C)$$00O6A 1 C C C o|44 	5#I..F6{{a WF1I$@ WGGGGGG,,-FTYYy-A-A-F-O-O-O-OQRTUVVVCCCCFCCCCC 5"9o>>>"9--22444 	)W0EFF	)S&( ) ) ) e# 	yAG'; 	6M)j'%:;; 	#S " # # # 	5L 	QiZPPPP 	IuY@@AAFFHHH )( 	;y/:::	""..y9966"" 	{ 	;;== Mr*   c                 X    |                      ||                              d          S )Nr;   T)r@   rL   rG   rU   argr;   r<   s       r(   _eval_rewrite_as_Integralz%Probability._eval_rewrite_as_Integral}   s)    yy	y2277D7IIIr*   c                 Z    |                      t                                                    S r%   rewriter   rG   rU   s    r(   evaluate_integralzProbability.evaluate_integral        ||H%%**,,,r*   r%   )	__name__
__module____qualname____doc__r7   rG   r^   _eval_rewrite_as_Sumrc   r4   r*   r(   r   r   &   sq         .   3 3 3jJ J J J 5- - - - -r*   r   c                   D    e Zd ZdZd	dZd Zd Zd	dZd	dZeZ	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                     t          |          }|j        rddlm}  |||          S |'t	          |          s|S t          j        | |          }n%t          |          }t          j        | ||          }||_        |S )Nr   )ExpectationMatrix)r   	is_Matrix-sympy.stats.symbolic_multivariate_probabilityrl   r   r   r7   r8   )r9   exprr;   r<   rl   r=   s         r(   r7   zExpectation.__new__   s    ~~> 	6WWWWWW$$T9555 	5T?? ,sD))CC ++I,sD)44C"
r*   c                    | j         d         }| j        t          |          s|S t          |t                    r%t	          j        fd|j         D                       S t          |          }t          |t                    r%t	          j        fd|j         D                       S t          |t                    rg }g }|j         D ]<}t          |          r|                    |           '|                    |           =t          j        |          t          t          j        |                    z  S | S )Nr   c              3   ^   K   | ]'}t          |                                           V  (dS rZ   Nr   r   r&   ar;   s     r(   r)   z%Expectation.expand.<locals>.<genexpr>   sP        (  ( !,A C C C J J L L  (  (  (  (  (  (r*   c              3   ^   K   | ]'}t          |                                           V  (dS rr   rs   rt   s     r(   r)   z%Expectation.expand.<locals>.<genexpr>   sP        /  / !,A C C C J J L L  /  /  /  /  /  /r*   rZ   )
rH   r8   r   rJ   r   fromiter_expandr   appendr   )rU   rV   ro   expand_exprrE   nonrvru   r;   s          @r(   r   zExpectation.expand   sd   y|O	 	KdC   	(<  (  (  (  (!Y (  (  ( ( ( ( dmmk3'' 	Z<  /  /  /  /(- /  /  / / / / c"" 	ZBEY $ $Q<< $IIaLLLLLLOOOO<&&{3<3C3Cy'Y'Y'YYYr*   c                                          dd          } j         j        d         }                     dd          }                     dd           }|r |j        di }t	          |          rt          |t                    r|S |r)                     dd          }t          |||          S |                    t                    r#t          |                              |          S .                      t          |                    j        di S |j        rt           fd	|j        D              S |j        r|                    t                    r|S t          |          t%                      k    r                     |          S t          |                              ||
          }t'          |d          r|r |j        di S |S )NdeepTr   r?   Fr@   evalf)r?   r~   c                     g | ]N}t          |t                    s!                     |          j        d i n                    |          OS )r4   )rJ   r   rL   rG   )r&   r]   r;   rV   rU   s     r(   
<listcomp>z$Expectation.doit.<locals>.<listcomp>  sq     / / /  &c;77W73	227@@%@@@=AYYsI=V=V/ / /r*   rA   rG   r4   )rI   r8   rH   rG   r   rJ   r   r   rM   r   r   compute_expectationrL   r   is_Addr   is_Mulr1   r   rT   )	rU   rV   r}   ro   r?   r@   r~   rX   r;   s	   ``      @r(   rG   zExpectation.doit   s    yy&&O	y|YY|U33
))M5999 	&49%%u%%D 	*T;"?"? 	K 	SIIgt,,EdI*ERRRR88'(( 	E$<<33D)DDD  	C9499U433449BBEBBB ; 	0 / / / / / /$(I/ / / 0 0 ; 	zz+&& $<<688# 	#99T??"11$1MM66"" 	{ 	6;'''''Mr*   c           	         |                     t                    }t          |          dk    rt                      t          |          dk    r|S |                                }|j        t          d          |j        }|j        d         	                                r't          |j                                                  }nt          |j        dz             }|j        j        rnt          |                    ||          t          t!          ||          |          z  ||j        j        j        j        |j        j        j        j        f          S |j        j        rt          t-          |                    ||          t          t!          ||          |          z  ||j        j        j        j        |j        j        j        f          S )Nr#   r   zProbability space not known_1)r1   r   r,   NotImplementedErrorpopr   rP   symbolnameisupperr
   loweris_Continuousr   replacer   r   domainsetinfsup	is_Finiter   )rU   r]   r;   r<   rvsrE   r   s          r(   _eval_rewrite_as_Probabilityz(Expectation._eval_rewrite_as_Probability"  s   ii%%s88a< 	(%'''s88q= 	JWWYY9 	<:;;;;q>!!## 	0FK--//00FFFK$.//F9" 	RCKKF33K2vPY4Z4ZZ]cegeneueye}  @B  @I  @P  @T  @X  ]Y  Z  Z  Zy" R))3;;r622;r"f~~y3Y3YY\bdfdmdtdxd|  A  H  L  P  \Q  R  R  Rr*   c                 Z    |                      ||                              dd          S )NrZ   FT)r}   r@   r[   r\   s       r(   r^   z%Expectation._eval_rewrite_as_Integral;  s,    yy	y2277UPT7UUUr*   c                 Z    |                      t                                                    S r%   r`   rb   s    r(   rc   zExpectation.evaluate_integral@  rd   r*   r%   )re   rf   rg   rh   r7   r   rG   r   r^   ri   rc   evaluate_sumr4   r*   r(   r   r      s        C CJ     8( ( (VR R R R2V V V V 5- - - %LLLr*   r   c                   B    e Zd ZdZd	dZd Zd	dZd	dZd	dZeZ	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                     t          |          }|j        rddlm}  |||          S |t	          j        | |          }n%t          |          }t	          j        | ||          }||_        |S )Nr   )VarianceMatrix)r   rm   rn   r   r   r7   r8   )r9   r]   r;   r<   r   r=   s         r(   r7   zVariance.__new__v  s    smm= 	2TTTTTT!>#y111 	4,sC((CC ++I,sC33C"
r*   c           	      &  	 | j         d         }| j        	t          |          st          j        S t          |t                    r| S t          |t                    r~g }|j         D ]&}t          |          r|                    |           't          t          	fd|           }	fd}t          t          |t          j        |d                     }||z   S t          |t                    rg }g }|j         D ]?}t          |          r|                    |           '|                    |dz             @t          |          dk    rt          j        S t          j        |          t          t          j        |          	          z  S | S )Nr   c                 H    t          |                                           S r%   )r    r   )xvr;   s    r(   <lambda>z!Variance.expand.<locals>.<lambda>  s    HR,C,C,J,J,L,L r*   c                 F    dt          | di                                z  S )N   r;   )r!   r   )r0   r;   s    r(   r   z!Variance.expand.<locals>.<lambda>  s%    Qz1'J	'J'J'Q'Q'S'S%S r*   r   )rH   r8   r   r	   rR   rJ   r   r   ry   map	itertoolscombinationsr   r,   rw   r    )
rU   rV   r]   rE   ru   	variancesmap_to_covarcovariancesr{   r;   s
            @r(   r   zVariance.expand  s   ilO	~~ 	6Mc<(( 	MKS!! 	MBX ! !Q<< !IIaLLLS!L!L!L!LbQQRISSSSLs<1GA1N1NOOPK{**S!! 
	MEBX ' 'Q<< 'IIaLLLLLLA&&&&2ww!| v<&&xR0@0@)'L'LLL r*   c                 X    t          |dz  |          }t          ||          dz  }||z
  S )Nr   r   )rU   r]   r;   r<   e1e2s         r(   _eval_rewrite_as_Expectationz%Variance._eval_rewrite_as_Expectation  s2    S!VY//BS),,a/B7Nr*   c                 f    |                      t                                         t                    S r%   ra   r   r   r\   s       r(   r   z%Variance._eval_rewrite_as_Probability  "    ||K((00===r*   c                 F    t          | j        d         | j        d          S )Nr   FrA   )r   rH   r8   r\   s       r(   r^   z"Variance._eval_rewrite_as_Integral  s    	!doFFFFr*   c                 Z    |                      t                                                    S r%   r`   rb   s    r(   rc   zVariance.evaluate_integral  rd   r*   r%   )re   rf   rg   rh   r7   r   r   r   r^   ri   rc   r4   r*   r(   r    r    E  s        / /`     B   
> > > >G G G G 5- - - - -r*   r    c                   n    e Zd ZdZddZd Zed             Zed             ZddZ	ddZ
dd	ZeZ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                    t          |          }t          |          }|j        s|j        rddlm}  ||||          S |                    dt
          j                  rt          ||gt                    \  }}|t          j
        | ||          }n&t          |          }t          j
        | |||          }||_        |S )Nr   )CrossCovarianceMatrixrB   key)r   rm   rn   r   r   r   rB   sortedr   r   r7   r8   )r9   arg1arg2r;   r<   r   r=   s          r(   r7   zCovariance.__new__  s    ~~~~> 	@T^ 	@[[[[[[((tY???::j"3"<== 	Dt2BCCCJD$ 	;,sD$//CC ++I,sD$	::C"
r*   c                    | j         d         }| j         d         }| j        ||k    r"t          |                                          S t	          |          st
          j        S t	          |          st
          j        S t          ||gt                    \  }}t          |t                    r&t          |t                    rt          ||          S |                     |                                          }|                     |                                          fd|D             }t          j        |          S )Nr   r#   r   c           
      v    g | ]5\  }}D ]-\  }}||z  t          t          ||gt                     diz  .6S )r   r;   )r!   r   r   )r&   ru   r1br2coeff_rv_list2r;   s        r(   r   z%Covariance.expand.<locals>.<listcomp>  sw     P P P2P P5<a Q3z62r(8H#I#I#I_U^___ P P P Pr*   )rH   r8   r    r   r   r	   rR   r   r   rJ   r   r!   _expand_single_argumentr   rw   )rU   rV   r   r   coeff_rv_list1addendsr   r;   s         @@r(   r   zCovariance.expand  s>   y|y|O	4< 	6D),,33555 	6M 	6MT4L.>???
ddL)) 	5j|.L.L 	5dD)44455dkkmmDD55dkkmmDDP P P P P"0P P P|G$$$r*   c                    t          |t                    rt          j        |fgS t          |t                    r|g }|j        D ]p}t          |t                    r)|                    |                     |                     @t          |          r!|                    t          j        |f           q|S t          |t                    r|                     |          gS t          |          rt          j        |fgS d S r%   )
rJ   r   r	   rK   r   rH   r   ry   _get_mul_nonrv_rv_tupler   )r9   ro   outvalru   s       r(   r   z"Covariance._expand_single_argument  s     dL)) 	#UDM?"c"" 	#FY . .a%% .MM#"="=a"@"@AAAAq\\ .MM15!*---Mc"" 	#//5566t__ 	#UDM?"	# 	#r*   c                     g }g }|j         D ]<}t          |          r|                    |           '|                    |           =t          j        |          t          j        |          fS r%   )rH   r   ry   r   rw   )r9   mrE   r{   ru   s        r(   r   z"Covariance._get_mul_nonrv_rv_tuple"  sl     	  	 A||  		!QU##S\"%5%566r*   c                 t    t          ||z  |          }t          ||          t          ||          z  }||z
  S r%   r   )rU   r   r   r;   r<   r   r   s          r(   r   z'Covariance._eval_rewrite_as_Expectation-  s<    dI..y))+dI*F*FFBwr*   c                 f    |                      t                                         t                    S r%   r   rU   r   r   r;   r<   s        r(   r   z'Covariance._eval_rewrite_as_Probability2  r   r*   c                 ^    t          | j        d         | j        d         | j        d          S )Nr   r#   FrA   )r   rH   r8   r   s        r(   r^   z$Covariance._eval_rewrite_as_Integral5  s(    $)A,	!doPUVVVVr*   c                 Z    |                      t                                                    S r%   r`   rb   s    r(   rc   zCovariance.evaluate_integral:  rd   r*   r%   )re   rf   rg   rh   r7   r   classmethodr   r   r   r   r^   ri   rc   r4   r*   r(   r!   r!     s        * *X   &% % %2 # # [#$ 7 7 [7   
> > > >W W W W 5- - - - -r*   r!   c                   B     e Zd ZdZd	 fd	Zd Zd	dZd	dZd	dZ x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                    t          |          }t          |          }t          |          }|4t          |          }t                                          | ||||          S t                                          | |||          S r%   r   superr7   )r9   Xncr;   r<   	__class__s         r(   r7   zMoment.__new__a  ss    QKKQKKQKK 	1 ++I77??31a;;;77??31a000r*   c                 L     |                      t                    j        di |S Nr4   ra   r   rG   rU   rV   s     r(   rG   zMoment.doitk  '    -t||K((-66666r*   c                 .    t          ||z
  |z  |          S r%   r   rU   r   r   r   r;   r<   s         r(   r   z#Moment._eval_rewrite_as_Expectationn  s    AEA:y111r*   c                 f    |                      t                                         t                    S r%   r   r   s         r(   r   z#Moment._eval_rewrite_as_Probabilityq  r   r*   c                 f    |                      t                                         t                    S r%   ra   r   r   r   s         r(   r^   z Moment._eval_rewrite_as_Integralt  "    ||K((00:::r*   )r   N
re   rf   rg   rh   r7   rG   r   r   r^   __classcell__r   s   @r(   r   r   >  s        ! !D1 1 1 1 1 17 7 72 2 2 2> > > >; ; ; ; ; ; ; ;r*   r   c                   B     e Zd ZdZd fd	Zd ZddZddZddZ x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                     t          |          }t          |          }|3t          |          }t                                          | |||          S t                                          | ||          S r%   r   )r9   r   r   r;   r<   r   s        r(   r7   zCentralMoment.__new__  sd    QKKQKK 	. ++I77??31i88877??31---r*   c                 L     |                      t                    j        di |S r   r   r   s     r(   rG   zCentralMoment.doit  r   r*   c                 n    t          ||fi |}t          ||||fi |                    t                     S r%   )r   r   ra   )rU   r   r   r;   r<   mus         r(   r   z*CentralMoment._eval_rewrite_as_Expectation  sC    I0000aB	44V44<<[IIIr*   c                 f    |                      t                                         t                    S r%   r   rU   r   r   r;   r<   s        r(   r   z*CentralMoment._eval_rewrite_as_Probability  r   r*   c                 f    |                      t                                         t                    S r%   r   r   s        r(   r^   z'CentralMoment._eval_rewrite_as_Integral  r   r*   r%   r   r   s   @r(   r   r   x  s        ! !D. . . . . .7 7 7J J J J> > > >; ; ; ; ; ; ; ;r*   r   )5r   sympy.concrete.summationsr   sympy.core.addr   sympy.core.exprr   sympy.core.functionr   rx   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__registerr2   r   r   r    r!   r   r   r4   r*   r(   <module>r      sR       ) ) ) ) ) )                   1 1 1 1 1 1       $ $ $ $ $ $ " " " " " " $ $ $ $ $ $ . . . . . . # # # # # # 3 3 3 3 3 3 / / / / / / ' ' ' ' ' ' , , , , , , ' ' ' ' ' ' , , , , , , , ,@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ C
B
B D, , , L!!  "!]- ]- ]- ]- ]-$ ]- ]- ]-@}% }% }% }% }%$ }% }% }%~n- n- n- n- n-t n- n- n-bE- E- E- E- E- E- E- E-P7; 7; 7; 7; 7;T 7; 7; 7;t7; 7; 7; 7; 7;D 7; 7; 7; 7; 7;r*   