
    \h3                    t   S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  SSKJrJr  SSKJr  SSKJrJr  SSKJr  SSK J!r!  SSK"J#r#  SSK$J%r%J&r&  SSK'J(r(  SSK)J*r*  SSK+J,r,  SSKJ-r-  SSKJ.r.  SSK/J0r0J1r1J2r2  SSK3J4r4  SSK5J6r6  SSK7J8r8  SSK9J:r:  SSK;J<r<  SS/0r=\" S 5      r>\S! 5       r?\?R                  \
5      S" 5       rA " S# S$\
5      rB " S% S&\B5      rC " S' S(\B5      rD " S) S*\B5      rE " S+ S,\
5      rF " S- S.\F5      rG " S/ S0\5      rH " S1 S2\H5      rI " S3 S4\H\(5      rJ " S5 S6\F5      rK " S7 S8\K5      rL " S9 S:\B5      rMS; rNS< rOS= rPS> rQS`S@ jrRSaSA jrS  SaSB jrT " SC SD\
5      rUSbSE jrVScSF jrWScSG jrXScSH jrYS`SI jrZ\8" SJSK9  SdSM j5       r[SeSN jr\S?SLS\R                  S?4SO jr^S?SL\R                  S?4SP jr_S?SL\R                  S?4SQ jr`  SfSR jra  SfSS jrb  SgST jrcSU rdSV reSW rfS`SX jrg " SY SZ5      rh " S[ S\\
5      riS] rjS^ rkS_ rlg?)ha  
Main Random Variables Module

Defines abstract random variable type.
Contains interfaces for probability space object (PSpace) as well as standard
operators, P, E, sample, density, where, quantile

See Also
========

sympy.stats.crv
sympy.stats.frv
sympy.stats.rv_interface
    )annotations)singledispatch)prod)Add)Basic)Tuple)Expr)FunctionLambda)	fuzzy_and)Mul)EqNe)S)DummySymbol)sympify)
DiracDelta)KroneckerDelta)AndOr)MatrixSymbol)Indexed)lambdify)
Relational)_sympify)	FiniteSet
ProductSetIntersection)solveset)import_module)doctest_depends_on)sympy_deprecation_warning)iterable)samplescipyxc                    gNF )r'   s    F/var/www/auris/envauris/lib/python3.13/site-packages/sympy/stats/rv.py	is_randomr,   5   s        c                >    U R                   n[        S U 5       5      $ )Nc              3  8   #    U  H  n[        U5      v   M     g 7fN)r,   ).0is     r+   	<genexpr>_.<locals>.<genexpr>=   s     +Uy||U   )free_symbolsany)r'   atomss     r+   _r9   :   s    NNE+U+++r-   c                  Z    \ rS rSrSrSrSrSrSrS r	\
S 5       r\
S 5       rS rS rS	rg
)RandomDomain@   z
Represents a set of variables and the values which they can take.

See Also
========

sympy.stats.crv.ContinuousDomain
sympy.stats.frv.FiniteDomain
Fc                @    [        U6 n[        R                  " X/UQ76 $ r0   )r   r   __new__)clssymbolsargss      r+   r>   RandomDomain.__new__P   s     W%}}S1D11r-   c                     U R                   S   $ Nr   rA   selfs    r+   r@   RandomDomain.symbolsT       yy|r-   c                     U R                   S   $ N   rE   rF   s    r+   setRandomDomain.setX   rI   r-   c                    [        5       er0   NotImplementedError)rG   others     r+   __contains__RandomDomain.__contains__\       !##r-   c                    [        5       er0   rP   rG   exprs     r+   compute_expectation RandomDomain.compute_expectation_   rU   r-   r*   N)__name__
__module____qualname____firstlineno____doc__is_ProductDomain	is_Finiteis_Continuousis_Discreter>   propertyr@   rM   rS   rY   __static_attributes__r*   r-   r+   r;   r;   @   sU     IMK2    $$r-   r;   c                  D    \ rS rSrSrS r\S 5       r\S 5       rS r	Sr
g)	SingleDomainc   z
A single variable and its domain.

See Also
========

sympy.stats.crv.SingleContinuousDomain
sympy.stats.frv.SingleFiniteDomain
c                V    UR                   (       d   e[        R                  " XU5      $ r0   )	is_Symbolr   r>   r?   symbolrM   s      r+   r>   SingleDomain.__new__m   s"    }}S#..r-   c                     U R                   S   $ rD   rE   rF   s    r+   rl   SingleDomain.symbolq   rI   r-   c                ,    [        U R                  5      $ r0   r   rl   rF   s    r+   r@   SingleDomain.symbolsu       %%r-   c                    [        U5      S:w  a  g[        U5      S   u  p#U R                  U:H  =(       a    X0R                  ;   $ )NrL   Fr   )lentuplerl   rM   )rG   rR   symvals       r+   rS   SingleDomain.__contains__y   s9    u:?<?{{c!5cXXo5r-   r*   N)r[   r\   r]   r^   r_   r>   rd   rl   r@   rS   re   r*   r-   r+   rg   rg   c   s9    /   & &6r-   rg   c                  >    \ rS rSrSrS r\S 5       r\S 5       rSr	g)MatrixDomain   z+
A Random Matrix variable and its domain.

c                Z    [        U5      [        U5      p![        R                  " XU5      $ r0   )_symbol_converterr   r   r>   rk   s      r+   r>   MatrixDomain.__new__   s#    '/#}}S#..r-   c                     U R                   S   $ rD   rE   rF   s    r+   rl   MatrixDomain.symbol   rI   r-   c                ,    [        U R                  5      $ r0   rq   rF   s    r+   r@   MatrixDomain.symbols   rs   r-   r*   N)
r[   r\   r]   r^   r_   r>   rd   rl   r@   re   r*   r-   r+   r{   r{      s4    /   & &r-   r{   c                  d    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
S rS	rg
)ConditionalDomain   z
A RandomDomain with an attached condition.

See Also
========

sympy.stats.crv.ConditionalContinuousDomain
sympy.stats.frv.ConditionalFiniteDomain
c                    UR                  [        U5       Vs0 s H  nX3R                  _M     sn5      n[        R                  " XU5      $ s  snf r0   )xreplacerandom_symbolsrl   r   r>   )r?   
fulldomain	conditionrss       r+   r>   ConditionalDomain.__new__   sN    &&$Y/(1/ )+II/(1 2	}}Si88(1s   Ac                .    U R                   R                  $ r0   )r   r@   rF   s    r+   r@   ConditionalDomain.symbols   s    &&&r-   c                     U R                   S   $ rD   rE   rF   s    r+   r   ConditionalDomain.fulldomain   rI   r-   c                     U R                   S   $ rK   rE   rF   s    r+   r   ConditionalDomain.condition   rI   r-   c                    [        S5      e)Nz)Set of Conditional Domain not ImplementedrP   rF   s    r+   rM   ConditionalDomain.set   s    !"MNNr-   c                ^    [        U R                  R                  5       U R                  5      $ r0   )r   r   
as_booleanr   rF   s    r+   r   ConditionalDomain.as_boolean   s     4??--/@@r-   r*   N)r[   r\   r]   r^   r_   r>   rd   r@   r   r   rM   r   re   r*   r-   r+   r   r      sd    9
 ' '     O OAr-   r   c                      \ rS rSr% SrSrS\S'   SrS\S'   SrS\S'   S\S'   \	S	 5       r
\	S
 5       r\	S 5       r\	S 5       rS rS rSS jrS rS rSrg)PSpace   aQ  
A Probability Space.

Explanation
===========

Probability Spaces encode processes that equal different values
probabilistically. These underly Random Symbols which occur in SymPy
expressions and contain the mechanics to evaluate statistical statements.

See Also
========

sympy.stats.crv.ContinuousPSpace
sympy.stats.frv.FinitePSpace
Nzbool | Nonera   rb   rc   is_realc                     U R                   S   $ rD   rE   rF   s    r+   domainPSpace.domain   rI   r-   c                     U R                   S   $ rK   rE   rF   s    r+   densityPSpace.density   rI   r-   c                B   ^  [        U 4S jT R                   5       5      $ )Nc              3  <   >#    U  H  n[        UT5      v   M     g 7fr0   )RandomSymbol)r1   rw   rG   s     r+   r3    PSpace.values.<locals>.<genexpr>   s     ILSc400Ls   )	frozensetr@   rF   s   `r+   valuesPSpace.values   s    IDLLIIIr-   c                .    U R                   R                  $ r0   )r   r@   rF   s    r+   r@   PSpace.symbols   s    {{"""r-   c                    [        5       er0   rP   rG   r   s     r+   wherePSpace.where   rU   r-   c                    [        5       er0   rP   rW   s     r+   compute_densityPSpace.compute_density   rU   r-   r*   c                    [        5       er0   rP   )rG   sizelibraryseeds       r+   r%   PSpace.sample   rU   r-   c                    [        5       er0   rP   r   s     r+   probabilityPSpace.probability   rU   r-   c                    [        5       er0   rP   rW   s     r+   rY   PSpace.compute_expectation   rU   r-   r*   r&   N)r[   r\   r]   r^   r_   ra   __annotations__rb   rc   rd   r   r   r   r@   r   r   r%   r   rY   re   r*   r-   r+   r   r      s    " "I{!!%M;%#K#    J J # #$$$$$r-   r   c                  ^    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
Srg	)
SinglePSpace   zl
Represents the probabilities of a set of random events that can be
attributed to a single variable/symbol.
c                F    [        U5      n[        R                  " XU5      $ r0   )r~   r   r>   )r?   sdistributions      r+   r>   SinglePSpace.__new__   s    a }}S\22r-   c                .    [        U R                  U 5      $ r0   )r   rl   rF   s    r+   valueSinglePSpace.value   s    DKK..r-   c                     U R                   S   $ rD   rE   rF   s    r+   rl   SinglePSpace.symbol   rI   r-   c                     U R                   S   $ rK   rE   rF   s    r+   r   SinglePSpace.distribution   rI   r-   c                L    U R                   R                  U R                  5      $ r0   )r   pdfrl   rF   s    r+   r   SinglePSpace.pdf  s      $$T[[11r-   r*   N)r[   r\   r]   r^   r_   r>   rd   r   rl   r   r   re   r*   r-   r+   r   r      s\    3 / /     2 2r-   r   c                      \ rS rSrSrSS jrSrSrSrSr	\
" S 5      r\
" S 5      r\
" S 5      rS	 rS
 rS r\
S 5       r\
S 5       rSrg)r   i  a,  
Random Symbols represent ProbabilitySpaces in SymPy Expressions.
In principle they can take on any value that their symbol can take on
within the associated PSpace with probability determined by the PSpace
Density.

Explanation
===========

Random Symbols contain pspace and symbol properties.
The pspace property points to the represented Probability Space
The symbol is a standard SymPy Symbol that is used in that probability space
for example in defining a density.

You can form normal SymPy expressions using RandomSymbols and operate on
those expressions with the Functions

E - Expectation of a random expression
P - Probability of a condition
density - Probability Density of an expression
given - A new random expression (with new random symbols) given a condition

An object of the RandomSymbol type should almost never be created by the
user. They tend to be created instead by the PSpace class's value method.
Traditionally a user does not even do this but instead calls one of the
convenience functions Normal, Exponential, Coin, Die, FiniteRV, etc....
Nc                    SSK Jn  Uc
  [        5       n[        U5      n[	        U[        5      (       d  [        S5      eX:X  a  [	        U[        5      (       a  [        n [        R                  " XU5      $ )Nr   JointRandomSymbolz(pspace variable should be of type PSpace)
sympy.stats.joint_rvr   r   r~   
isinstance	TypeErrorr   r   r   r>   )r?   rl   pspacer   s       r+   r>   RandomSymbol.__new__#  s\    :>XF"6*&&))FGG#
6<(H(HC}}S&11r-   Tc                     U R                   S   $ rK   rE   rF   s    r+   <lambda>RandomSymbol.<lambda>5      499Q<r-   c                     U R                   S   $ rD   rE   rF   s    r+   r   r   6  r   r-   c                .    U R                   R                  $ r0   rl   namerF   s    r+   r   r   7  s    4;;#3#3r-   c                .    U R                   R                  $ r0   )rl   is_positiverF   s    r+   _eval_is_positiveRandomSymbol._eval_is_positive9  s    {{&&&r-   c                .    U R                   R                  $ r0   )rl   
is_integerrF   s    r+   _eval_is_integerRandomSymbol._eval_is_integer<  s    {{%%%r-   c                h    U R                   R                  =(       d    U R                  R                  $ r0   )rl   r   r   rF   s    r+   _eval_is_realRandomSymbol._eval_is_real?  s!    {{""9dkk&9&99r-   c                .    U R                   R                  $ r0   )rl   is_commutativerF   s    r+   r   RandomSymbol.is_commutativeB  s    {{)))r-   c                    U 1$ r0   r*   rF   s    r+   r6   RandomSymbol.free_symbolsF  s	    vr-   r*   r0   )r[   r\   r]   r^   r_   r>   	is_finite	is_symbolis_Atom	_diff_wrtrd   r   rl   r   r   r   r   r   r6   re   r*   r-   r+   r   r     sz    8
2 IIGI/0F/0F34D'&: * *  r-   r   c                  r    \ rS rSrS
S jr\" S 5      r\" S 5      r\S 5       r\S 5       r	\S 5       r
S	rg)RandomIndexedSymboliJ  Nc                    Uc
  [        5       n[        U[        [        45      (       d  [	        SU-  5      e[
        R                  " XU5      $ )Nz0An Function or Indexed object is expected not %s)r   r   r   r
   r   r   r>   )r?   idx_objr   s      r+   r>   RandomIndexedSymbol.__new__L  sC    >XF'GX#677NPWXYY}}S622r-   c                     U R                   S   $ rD   rE   rF   s    r+   r   RandomIndexedSymbol.<lambda>T  r   r-   c                2    [        U R                  S   5      $ rD   )strrA   rF   s    r+   r   r   U  s    TYYq\!2r-   c                    [        U R                  [        5      (       a  U R                  R                  S   $ [        U R                  [        5      (       a  U R                  R                  S   $ g )NrL   r   )r   rl   r   rA   r
   rF   s    r+   keyRandomIndexedSymbol.keyW  sS    dkk7++;;##A&&X..;;##A&& /r-   c                    U R                   R                  (       a)  U R                   R                  nUR                  U 5        U$ U 1$ r0   )r   r6   add)rG   	free_symss     r+   r6    RandomIndexedSymbol.free_symbols^  s7    88  --IMM$vr-   c                     U R                   S   $ rK   rE   rF   s    r+   r   RandomIndexedSymbol.pspacef  rI   r-   r*   r0   )r[   r\   r]   r^   r>   rd   rl   r   r   r6   r   re   r*   r-   r+   r   r   J  sY    3 /0F23D' '    r-   r   c                  B    \ rS rSrSS jr\" S 5      r\" S 5      rSrg)RandomMatrixSymbolij  Nc                    [        U5      [        U5      p2[        U5      nUc
  [        5       n[        R                  " XX#U5      $ r0   )r   r~   r   r   r>   )r?   rl   nmr   s        r+   r>   RandomMatrixSymbol.__new__k  s:    {HQK1"6*>XF}}S!77r-   c                     U R                   S   $ rD   rE   rF   s    r+   r   RandomMatrixSymbol.<lambda>s  r   r-   c                     U R                   S   $ )N   rE   rF   s    r+   r   r  t  r   r-   r*   r0   )	r[   r\   r]   r^   r>   rd   rl   r   re   r*   r-   r+   r  r  j  s    8 /0F/0Fr-   r  c                      \ rS rSrSrSrg)ProductPSpaceiw  z
Abstract class for representing probability spaces with multiple random
variables.

See Also
========

sympy.stats.rv.IndependentProductPSpace
sympy.stats.joint_rv.JointPSpace
r*   N)r[   r\   r]   r^   r_   re   r*   r-   r+   r  r  w  s    	 	r-   r  c                      \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       rSS
 jr\S 5       r\S 5       rSS jrS rS rS rSS jrSrg	)IndependentProductPSpacei  z
A probability space resulting from the merger of two independent probability
spaces.

Often created using the function, pspace.
c                  ^	^
 0 nU H  nUR                    H  nX2U'   M	     M     [        UR                  5        Vs/ s H  oUR                  PM     sn6 nSSKJm
  SSKJm	  [        U5      [        U	U
4S jU 5       5      :  a  [        S5      e[        S U 5       5      (       a  SSKJn  Un [        R                  " U /[        U6 Q76 nU$ s  snf )Nr   )MarginalDistribution)CompoundDistributionc              3     >#    U  H9  n[        UR                  TT45      (       a  M"  [        UR                  5      v   M;     g 7fr0   )r   r   ru   r@   )r1   spacer  r  s     r+   r3   3IndependentProductPSpace.__new__.<locals>.<genexpr>  s?      :fU	E&& "6)8 
9 1c%--00fs
   !AAzOverlapping Random Variablesc              3  8   #    U  H  oR                   v   M     g 7fr0   ra   r1   r  s     r+   r3   r    s     3F5Fr5   )ProductFinitePSpace)r   r   keysrl   r   r  sympy.stats.compound_rvr  ru   sum
ValueErrorallsympy.stats.frvr  r   r>   )r?   spacesrs_space_dictr  r   rx   r@   r  objr  r  s            @@r+   r>    IndependentProductPSpace.__new__  s    E',e$ &  M4F4F4HI4HSjj4HIJ 	>@w<# :f : : : ;<<3F333;%CmmC5)V"45
! Js   Cc                    [        U R                   Vs/ s H  oR                  PM     sn6 nUR                  U R                   Vs0 s H  o3UR
                  _M     sn5      $ s  snf s  snf r0   )r   r%  r   subsr   rl   )rG   r  prvs       r+   r   IndependentProductPSpace.pdf  sQ    5))56vvt{{;{299}{;<< 6;s   A#A(c                ^    0 nU R                    H  nUR                   H  nX!U'   M	     M     U$ r0   )r%  r   )rG   dr  r   s       r+   r&  &IndependentProductPSpace.rs_space_dict  s1    [[E % & ! r-   c                    [        U R                  R                  5        Vs/ s H  oR                  PM     sn6 $ s  snf r0   )r   r&  r  rl   )rG   rx   s     r+   r@    IndependentProductPSpace.symbols  s2    1C1C1H1H1JK1J#::1JKLLKs   ;c                &    [        U R                  6 $ r0   )r   rA   rF   s    r+   r%  IndependentProductPSpace.spaces  s    $))$$r-   c                :    [        S U R                   5       5      $ )Nc              3  8   #    U  H  oR                   v   M     g 7fr0   )r   r  s     r+   r3   2IndependentProductPSpace.values.<locals>.<genexpr>  s     =||r5   )sumsetsr%  rF   s    r+   r   IndependentProductPSpace.values  s    ====r-   Nc                   U=(       d    U R                   n[        U5      nU R                   H%  nUR                  " XUR                   -  4SS0UD6nM'     U(       a#  [	        US5      (       a  UR
                  " S0 UD6$ U$ )NevaluateFdoitr*   )r   r   r%  rY   hasattrr<  )rG   rX   rvsr;  kwargsr  s         r+   rY   ,IndependentProductPSpace.compute_expectation  sr     T[[n[[E,,T3E`PU`Y_`D !f--99&v&&r-   c                d    [        U R                   Vs/ s H  oR                  PM     sn6 $ s  snf r0   )ProductDomainr%  r   )rG   r  s     r+   r   IndependentProductPSpace.domain  s%    E||EFFEs   -c                    [        S5      e)Nz'Density not available for ProductSpacesrP   rF   s    r+   r    IndependentProductPSpace.density  s    !"KLLr-   r*   c                    U R                    VVVs0 s H-  nUR                  XUS9R                  5         H  u  pVXV_M	     M/     snnn$ s  snnnf )Nr   r   r   )r%  r%   items)rG   r   r   r   r  kvs          r+   r%   IndependentProductPSpace.sample  sT    "&++ U+$dKQQS S + U 	U Us   4A
c                   Sn[        U[        5      (       a)  [        UR                  S   UR                  S   5      nSnO[        U[        5      (       a1  [        UR                   Vs/ s H  o@R                  U5      PM     sn6 $ [        U[        5      (       a1  [        UR                   Vs/ s H  o@R                  U5      PM     sn6 $ UR                  UR                  -
  n[        U5      nU R                  U5      n[        S U 5       5      (       a  SSKJn  SSKJn	  XPR$                  ;   a{  ['        [)        U R$                  5      [+        U/5      -
  5      n
['        S U
 5       5      nU R,                  R.                  " U R0                  U40 UD6n[3        UR4                  U5      $ U	" U5      n[7        S	SS
9nU" X5      nUR                  UR9                  UR:                  S5      5      nOQSSKJn  SSK J!n  U" U5      n[7        S	SS9nU" X5      nUR                  UR9                  UR:                  S5      5      nU(       d  U$ [D        RF                  U-
  $ s  snf s  snf )NFr   rL   Tc              3  L   #    U  H  n[        U5      R                  v   M     g 7fr0   r   rb   r1   r,  s     r+   r3   7IndependentProductPSpace.probability.<locals>.<genexpr>       6#Bvbz''#   "$)SingleContinuousPSpace)ContinuousDistributionHandmadec              3  8   #    U  H  oR                   v   M     g 7fr0   rl   )r1   r   s     r+   r3   rP    s     BMb		Mr5   zreal)SingleDiscretePSpace)DiscreteDistributionHandmadeinteger)$r   r   r   rA   r   r   r   r   r   lhsrhsr   r   r7   sympy.stats.crvrS  sympy.stats.crv_typesrT  r   rv   rM   r   r   	integrater   r   rl   r   	__class__r   sympy.stats.drvrZ  sympy.stats.drv_typesr[  r   One)rG   r   r?  cond_invargrX   r>  densrS  rT  randomsymbolsr@   r   rW  r  resultrZ  r[  s                     r+   r   $IndependentProductPSpace.probability  s   i$$9>>!,innQ.?@IH	3'')..I.3))#..IJJ	2&&)..I.3))#..IJJ}}y}},T"##D)6#666>L{{" %c$++&6D69J&J KBMBBkk++DHHgHHdkk3//1$7Dc%A*13E&&y':':5;;'JKF<J/5Dc4(A(1E&&y':':5;;'JKF%v91556>95 JIs   )I"/I'c                    [        U5      n[        S U 5       5      (       a)  [        SSS9nU R                  " [	        X-
  5      40 UD6nO&[        SSS9nU R                  " [        X5      40 UD6n[        XA5      $ )Nc              3  L   #    U  H  n[        U5      R                  v   M     g 7fr0   rN  rO  s     r+   r3   ;IndependentProductPSpace.compute_density.<locals>.<genexpr>  rQ  rR  rW  TrX  r\  )r   r7   r   rY   r   r   r   )rG   rX   r?  r>  rW  s        r+   r   (IndependentProductPSpace.compute_density  s    T"6#666c%A++Jtx,@ D c4(A++N4,C Dar-   c                    [        S5      e)Nz0CDF not well defined on multivariate expressions)r"  )rG   rX   r?  s      r+   compute_cdf$IndependentProductPSpace.compute_cdf  s    KLLr-   c           	     8   [        U5      nUR                  U R                   Vs0 s H  oUUR                  _M     sn5      nU Vs/ s H  n[	        U5      PM     nn[        S U 5       5      (       a  SSKJnJn  Un	U" U R                  U5      n
Oc[        S U 5       5      (       a  SSK
JnJn  Un	U" U R                  U5      n
O.[        S U 5       5      (       a  SSKJn  UR!                  X5      $ U(       a  U R"                   Vs0 s H  oU[%        ['        U5      5      _M     nnW
R(                  " U R*                  40 UD6nU R*                  UR                  U5      -  n[-        [/        U
R"                  5      U5      nW	" W
W5      $ s  snf s  snf s  snf )Nc              3  8   #    U  H  oR                   v   M     g 7fr0   rb   r1   pss     r+   r3   =IndependentProductPSpace.conditional_space.<locals>.<genexpr>  s     2'B'r5   r   )ConditionalContinuousDomainContinuousPSpacec              3  8   #    U  H  oR                   v   M     g 7fr0   rc   rw  s     r+   r3   ry    s     2'B'r5   )ConditionalDiscreteDomainDiscretePSpacec              3  8   #    U  H  oR                   v   M     g 7fr0   r  rw  s     r+   r3   ry    s     0"r5   )FinitePSpace)r   r   r   rl   r   r7   r`  rz  r{  r   rd  r~  r  r#  r$  r  conditional_spacer@   r   r   rY   r   r   rv   )rG   r   	normalizer?  r>  r,  pspacesrz  r{  r  r   r~  r  r  replacementnormr   r   s                     r+   r  *IndependentProductPSpace.conditional_space  sQ   Y'&&'L"BII'LM	(+,"6":,2'222"$E0iHF2'222 "E.t{{IFF0000411$BB9=F2c"g.KF--dhhA&AD((T]];77C U6>>2C8GVW%%/ (M, Gs   FF
Fr)   r   T)r[   r\   r]   r^   r_   r>   rd   r   r&  r@   r%  r   rY   r   r   r%   r   r   rr  r  re   r*   r-   r+   r  r    s    0 = =   M M % % > > G G M MU
 :D
M&r-   r  c                  n    \ rS rSrSrSrS r\S 5       r\S 5       r	\S 5       r
\S 5       rS	 rS
 rSrg)rB  i  z
A domain resulting from the merger of two independent domains.

See Also
========
sympy.stats.crv.ProductContinuousDomain
sympy.stats.frv.ProductFiniteDomain
Tc                   / nU HB  nUR                   (       d  UR                  U5        M'  UR                  UR                  5        MD     [	        U6 n[        S U 5       5      (       a  SSKJn  Un [        S U 5       5      (       a  SSKJ	n  Un [        S U 5       5      (       a  SSK
Jn  Un [        R                  " U /UQ76 $ )Nc              3  8   #    U  H  oR                   v   M     g 7fr0   r  r1   r   s     r+   r3   (ProductDomain.__new__.<locals>.<genexpr>4  s     7hFhr5   r   )ProductFiniteDomainc              3  8   #    U  H  oR                   v   M     g 7fr0   rv  r  s     r+   r3   r  7  s     ;(##(r5   )ProductContinuousDomainc              3  8   #    U  H  oR                   v   M     g 7fr0   r}  r  s     r+   r3   r  :  s     9f!!r5   )ProductDiscreteDomain)r`   appendextenddomainsr   r#  r$  r  r`  r  rd  r  r   r>   )r?   r  domains2r   r  r  r  s          r+   r>   ProductDomain.__new__*  s    F**'/	 
 h'7h777;%C;(;;;?)C9999='C}}S,8,,r-   c                t    U R                    VVs0 s H  nUR                    H  nX!_M     M     snn$ s  snnf r0   )r  r@   )rG   r   rl   s      r+   sym_domain_dictProductDomain.sym_domain_dict@  s@    -1\\ C\639>> 3A \ C 	C Cs    4c                    [        U R                   VVs/ s H  nUR                    H  nUPM     M     snn6 $ s  snnf r0   )r   r  r@   )rG   r   rw   s      r+   r@   ProductDomain.symbolsE  sA    T\\ =\6-3^^3 -; \ = > 	> =s    :
c                    U R                   $ r0   rE   rF   s    r+   r  ProductDomain.domainsJ  s    yyr-   c                4    [        S U R                   5       6 $ )Nc              3  8   #    U  H  oR                   v   M     g 7fr0   )rM   r  s     r+   r3   $ProductDomain.set.<locals>.<genexpr>P  s     B\6JJ\r5   )r   r  rF   s    r+   rM   ProductDomain.setN  s    BT\\BCCr-   c                    U R                    H`  n[        U Vs/ s H?  n[        UR                  R	                  US   5      5      [
        R                  L d  M=  UPMA     sn5      nXB;  d  M`    g   gs  snf )Nr   FT)r  r   r   r@   containsr   true)rG   rR   r   itemelems        r+   rS   ProductDomain.__contains__R  sn    llFu )ut!()@)@a)I!J!""( #u ) *D ! # )s   <A2
A2
c                l    [        U R                   Vs/ s H  oR                  5       PM     sn6 $ s  snf r0   )r   r  r   )rG   r   s     r+   r   ProductDomain.as_boolean_  s*    t||D|V&&(|DEEDs   1r*   N)r[   r\   r]   r^   r_   r`   r>   rd   r  r@   r  rM   rS   r   re   r*   r-   r+   rB  rB    ss     -, C C > >   D DFr-   rB  c                h    [        U SS5      nUb!  S n[        U" [        5      5      n[        X2S9$ / $ )z6
Returns all RandomSymbols within a SymPy Expression.
r8   Nc                .    U R                   R                  $ r0   r   )r,  s    r+   r    random_symbols.<locals>.<lambda>i  s    "))..r-   )r   )getattrlistr   sorted)rX   r8   compls       r+   r   r   c  s<     D'4(E(|$%a""	r-   c                b  ^ [        U 5      n [        U [        5      (       a  U R                  b  U R                  $ U R	                  [
        5      (       a-  [        U R                  [
        5      5      S   nUR                  $ [        U 5      mT(       d  [        SU -  5      e[        U4S jT 5       5      (       a  TS   R                  $ SSKJn  SSKJn  T H-  n[        UR                  X#45      (       d  M!  UR                  s  $    [        T Vs/ s H  oDR                  PM     sn6 $ s  snf )z
Returns the underlying Probability Space of a random expression.

For internal use.

Examples
========

>>> from sympy.stats import pspace, Normal
>>> X = Normal('X', 0, 1)
>>> pspace(2*X + 1) == X.pspace
True
r   z6Expression containing Random Variable expected, not %sc              3  Z   >#    U  H   oR                   TS    R                   :H  v   M"     g7f)r   N)r   )r1   r,  r>  s     r+   r3   pspace.<locals>.<genexpr>  s      
4"99A%s   (+CompoundPSpace)StochasticPSpace)r   r   r   r   hasr  r  r8   r   r"  r#  r   r  sympy.stats.stochastic_processr  r  )rX   rmr  r  r,  r>  s        @r+   r   r   p  s     4=D$%%$++*A{{xx"##$**/01!4yy

CQUYZ[[

4
4441v}}6?bii.!CDD99  $#%>#Bii#%>??%>s   D,c                0    [        5       R                  " U 6 $ )z
Union of sets
)r   union)setss    r+   r8  r8    s     ;d##r-   c                    0 nU  H6  nU Vs/ s H   oCR                   UR                   :X  d  M  UPM"     snS   X#'   M8     U$ s  snf )aY  
Build a dictionary to swap RandomSymbols based on their underlying symbol.

i.e.
if    ``X = ('x', pspace1)``
and   ``Y = ('x', pspace2)``
then ``X`` and ``Y`` match and the key, value pair
``{X:Y}`` will appear in the result

Inputs: collections a and b of random variables which share common symbols
Output: dict mapping RVs in a to RVs in b
r   rV  )abr/  rsarsbs        r+   rs_swapr    sF     	A!"?#jjCJJ&>#?B H @s
   AANc                   [        U5      (       a  [        X5      (       a  U $ [        U[        5      (       a  [	        XR
                  5      n[        U5      n[        U[        5      (       Ga  [        U5      S:X  Ga  [        [        U 5      R                  [        5      (       d  [        U5      S   n[        X5      n[        U[        5      (       a6  [        R                  UR                   ;   a  [#        UR                   S   5      nSnU He  nU R%                  XG5      nUS:X  a    gUS:w  d  M$  US:X  a(  [        U [&        5      (       a  U R%                  XG5      nMR  X`R%                  XG5      -  nMg     US:X  a  gU$ [        [)        X5      5      n	U	R*                  " U40 UD6n
[-        U	R.                  U
R.                  5      nU R1                  U5      n U $ )a-  Conditional Random Expression.

Explanation
===========

From a random expression and a condition on that expression creates a new
probability space from the condition and returns the same expression on that
conditional probability space.

Examples
========

>>> from sympy.stats import given, density, Die
>>> X = Die('X', 6)
>>> Y = given(X, X > 3)
>>> density(Y).dict
{4: 1/3, 5: 1/3, 6: 1/3}

Following convention, if the condition is a random symbol then that symbol
is considered fixed.

>>> from sympy.stats import Normal
>>> from sympy import pprint
>>> from sympy.abc import z

>>> X = Normal('X', 0, 1)
>>> Y = Normal('Y', 0, 1)
>>> pprint(density(X + Y, Y)(z), use_unicode=False)
                2
       -(-Y + z)
       -----------
  ___       2
\/ 2 *e
------------------
         ____
     2*\/ pi
rL   r   TF)r,   pspace_independentr   r   r   rl   r   ru   r   r   r   rv   r    r   r   RealsrA   r  r*  r   r   r  r  r   r   )rX   r   r?  condsymbolsr,  resultssumsrestemp	fullspacer  swapdicts               r+   givenr    s   N Y#5d#F#F)\**y"2"23	 +K9b!!c+&6!&;vd|**,=>>;"9)g|,,GLL1H7<<?+GC99R%Dt|u} 19D*!=!=99R-DIIb..D  19 uT-.I''	<V<E y''6H=="DKr-   c                    [        U 5      (       d  U $ X$S'   SSKJn  U(       a  U" X5      R                  " S0 UD6$ U" X5      $ )a  
Returns the expected value of a random expression.

Parameters
==========

expr : Expr containing RandomSymbols
    The expression of which you want to compute the expectation value
given : Expr containing RandomSymbols
    A conditional expression. E(X, X>0) is expectation of X given X > 0
numsamples : int
    Enables sampling and approximates the expectation with this many samples
evalf : Bool (defaults to True)
    If sampling return a number rather than a complex expression
evaluate : Bool (defaults to True)
    In case of continuous systems return unevaluated integral

Examples
========

>>> from sympy.stats import E, Die
>>> X = Die('X', 6)
>>> E(X)
7/2
>>> E(2*X + 1)
8

>>> E(X, X > 3) # Expectation of X given that it is above 3
5

numsamplesr   )Expectationr*   )r,    sympy.stats.symbolic_probabilityr  r<  )rX   r   r  r;  r?  r  s         r+   expectationr    sE    @ T??%<<4+00:6::t''r-   c                d    X$S'   SSK Jn  U(       a  U" X5      R                  " S0 UD6$ U" X5      $ )a  
Probability that a condition is true, optionally given a second condition.

Parameters
==========

condition : Combination of Relationals containing RandomSymbols
    The condition of which you want to compute the probability
given_condition : Combination of Relationals containing RandomSymbols
    A conditional expression. P(X > 1, X > 0) is expectation of X > 1
    given X > 0
numsamples : int
    Enables sampling and approximates the probability with this many samples
evaluate : Bool (defaults to True)
    In case of continuous systems return unevaluated integral

Examples
========

>>> from sympy.stats import P, Die
>>> from sympy import Eq
>>> X, Y = Die('X', 6), Die('Y', 6)
>>> P(X > 3)
1/2
>>> P(Eq(X, 5), X > 2) # Probability that X == 5 given that X > 2
1/4
>>> P(X > Y)
5/12
r  r   )Probabilityr*   )r  r  r<  )r   given_conditionr  r;  r?  r  s         r+   r   r   ,  s7    @ &<<96;;EfEEy22r-   c                  J    \ rS rSr\" S 5      rSS jr\S 5       rS	S jrSr	g)
DensityiS  c                     U R                   S   $ rD   rE   rF   s    r+   r   Density.<lambda>T  s    1r-   Nc                    [        U5      nUc  [        R                  " X5      nU$ [        U5      n[        R                  " XU5      nU$ r0   )r   r   r>   )r?   rX   r   r'  s       r+   r>   Density.__new__V  sG    ~--*C 
 !+I--95C
r-   c                T    [        U R                  5      S:  a  U R                  S   $ g rK   )ru   rA   rF   s    r+   r   Density.condition_  s#    tyy>A99Q<r-   c                   SSK Jn  SSKJn  SSKJn  SSKJn  SSKJ	n  U R                  U R                  p[        X5      (       a  UR                  $ U	b  [        X40 UD6n[        U5      (       d   [!        ["        [%        ["        U-
  5      5      $ [        U[&        5      (       a  [        UR(                  [*        XE45      (       a1  [-        UR(                  S5      (       a  UR(                  R.                  $ [        UR(                  U5      (       a  UR(                  R0                  $ [        [)        U5      U5      (       a  XS'   [)        U5      R2                  " U40 UD6n
U(       a!  [-        U
S	5      (       a  U
R5                  5       $ U
$ )
Nr   )RandomMatrixPSpace)JointPSpace)MatrixPSpacer  )SingleFiniteDistributionr   compound_evaluater<  )sympy.stats.random_matrixr  r   r   sympy.stats.matrix_distributionsr  r   r  r$  r  rX   r   r   dictr  r   r   r'   r   r   r   r   r=  r   modelr   r<  )rG   r;  r?  r  r  r  r  r  rX   r   rk  s              r+   r<  Density.doitf  s$   @4A:<))T^^id5599 3F3Dd##!ZD122dL))$++k'PQQ^44{{///DKK);<<{{(((fTlN33*2&'--d=f=//;;= Mr-   r*   r0   r  )
r[   r\   r]   r^   rd   rX   r>   r   r<  re   r*   r-   r+   r  r  S  s+    -.D  r-   r  c                f    U(       a  [        X4SU0UD6$ [        X5      R                  " SSU0UD6$ )a  
Probability density of a random expression, optionally given a second
condition.

Explanation
===========

This density will take on different forms for different types of
probability spaces. Discrete variables produce Dicts. Continuous
variables produce Lambdas.

Parameters
==========

expr : Expr containing RandomSymbols
    The expression of which you want to compute the density value
condition : Relational containing RandomSymbols
    A conditional expression. density(X > 1, X > 0) is density of X > 1
    given X > 0
numsamples : int
    Enables sampling and approximates the density with this many samples

Examples
========

>>> from sympy.stats import density, Die, Normal
>>> from sympy import Symbol

>>> x = Symbol('x')
>>> D = Die('D', 6)
>>> X = Normal(x, 0, 1)

>>> density(D).dict
{1: 1/6, 2: 1/6, 3: 1/6, 4: 1/6, 5: 1/6, 6: 1/6}
>>> density(2*D).dict
{2: 1/6, 4: 1/6, 6: 1/6, 8: 1/6, 10: 1/6, 12: 1/6}
>>> density(X)(x)
sqrt(2)*exp(-x**2/2)/(2*sqrt(pi))
r  r;  r*   )sampling_densityr  r<  )rX   r   r;  r  r?  s        r+   r   r     sF    R  J  	 4#((E(EfEEr-   c                    Ub  [        [        X40 UD640 UD6$ [        U 5      R                  " U 40 UD6nU(       a!  [	        US5      (       a  UR                  5       $ U$ )ax  
Cumulative Distribution Function of a random expression.

optionally given a second condition.

Explanation
===========

This density will take on different forms for different types of
probability spaces.
Discrete variables produce Dicts.
Continuous variables produce Lambdas.

Examples
========

>>> from sympy.stats import density, Die, Normal, cdf

>>> D = Die('D', 6)
>>> X = Normal('X', 0, 1)

>>> density(D).dict
{1: 1/6, 2: 1/6, 3: 1/6, 4: 1/6, 5: 1/6, 6: 1/6}
>>> cdf(D)
{1: 1/6, 2: 1/3, 3: 1/2, 4: 2/3, 5: 5/6, 6: 1}
>>> cdf(3*D, D > 2)
{9: 1/4, 12: 1/2, 15: 3/4, 18: 1}

>>> cdf(X)
Lambda(_z, erf(sqrt(2)*_z/2)/2 + 1/2)
r<  )cdfr  r   rr  r=  r<  rX   r   r;  r?  rk  s        r+   r  r    s`    @ 53F3>v>> D\%%d5f5FGFF++{{}r-   c                    Ub  [        [        X40 UD640 UD6$ [        U 5      R                  " U 40 UD6nU(       a!  [	        US5      (       a  UR                  5       $ U$ )a   
Characteristic function of a random expression, optionally given a second condition.

Returns a Lambda.

Examples
========

>>> from sympy.stats import Normal, DiscreteUniform, Poisson, characteristic_function

>>> X = Normal('X', 0, 1)
>>> characteristic_function(X)
Lambda(_t, exp(-_t**2/2))

>>> Y = DiscreteUniform('Y', [1, 2, 7])
>>> characteristic_function(Y)
Lambda(_t, exp(7*_t*I)/3 + exp(2*_t*I)/3 + exp(_t*I)/3)

>>> Z = Poisson('Z', 2)
>>> characteristic_function(Z)
Lambda(_t, exp(2*exp(_t*I) - 2))
r<  )characteristic_functionr  r   compute_characteristic_functionr=  r<  r  s        r+   r  r    s]    . &uT'G'GR6RRD\99$I&IFGFF++{{}r-   c                    Ub  [        [        X40 UD640 UD6$ [        U 5      R                  " U 40 UD6nU(       a!  [	        US5      (       a  UR                  5       $ U$ )Nr<  )moment_generating_functionr  r   "compute_moment_generating_functionr=  r<  r  s        r+   r  r    s[    )%*J6*JUfUUD\<<TLVLFGFF++{{}r-   c                l    Ub  [        [        X40 UD640 UD6$ [        U 5      R                   " U 40 UD6$ )a  
Returns the domain where a condition is True.

Examples
========

>>> from sympy.stats import where, Die, Normal
>>> from sympy import And

>>> D1, D2 = Die('a', 6), Die('b', 6)
>>> a, b = D1.symbol, D2.symbol
>>> X = Normal('x', 0, 1)

>>> where(X**2<1)
Domain: (-1 < x) & (x < 1)

>>> where(X**2<1).set
Interval.open(-1, 1)

>>> where(And(D1<=D2, D2<3))
Domain: (Eq(a, 1) & Eq(b, 1)) | (Eq(a, 1) & Eq(b, 2)) | (Eq(a, 2) & Eq(b, 2))
)r   r  r   )r   r  r?  s      r+   r   r     sA    . "U9@@KFKK )""9777r-   )r&   )modulesr*   c           	         [        XX#XES9nUS:w  a;  [        SU SU4U-    S3SSS9  [        U5       Vs/ s H  n[        U5      PM     sn$ [        U5      $ s  snf )	a  
A realization of the random expression.

Parameters
==========

expr : Expression of random variables
    Expression from which sample is extracted
condition : Expr containing RandomSymbols
    A conditional expression
size : int, tuple
    Represents size of each sample in numsamples
library : str
    - 'scipy' : Sample using scipy
    - 'numpy' : Sample using numpy
    - 'pymc'  : Sample using PyMC

    Choose any of the available options to sample from as string,
    by default is 'scipy'
numsamples : int
    Number of samples, each with size as ``size``.

    .. deprecated:: 1.9

    The ``numsamples`` parameter is deprecated and is only provided for
    compatibility with v1.8. Use a list comprehension or an additional
    dimension in ``size`` instead. See
    :ref:`deprecated-sympy-stats-numsamples` for details.

seed :
    An object to be used as seed by the given external library for sampling `expr`.
    Following is the list of possible types of object for the supported libraries,

    - 'scipy': int, numpy.random.RandomState, numpy.random.Generator
    - 'numpy': int, numpy.random.RandomState, numpy.random.Generator
    - 'pymc': int

    Optional, by default None, in which case seed settings
    related to the given library will be used.
    No modifications to environment's global seed settings
    are done by this argument.

Returns
=======

sample: float/list/numpy.ndarray
    one sample or a collection of samples of the random expression.

    - sample(X) returns float/numpy.float64/numpy.int64 object.
    - sample(X, size=int/tuple) returns numpy.ndarray object.

Examples
========

>>> from sympy.stats import Die, sample, Normal, Geometric
>>> X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6) # Finite Random Variable
>>> die_roll = sample(X + Y + Z)
>>> die_roll # doctest: +SKIP
3
>>> N = Normal('N', 3, 4) # Continuous Random Variable
>>> samp = sample(N)
>>> samp in N.pspace.domain.set
True
>>> samp = sample(N, N>0)
>>> samp > 0
True
>>> samp_list = sample(N, size=4)
>>> [sam in N.pspace.domain.set for sam in samp_list]
[True, True, True, True]
>>> sample(N, size = (2,3)) # doctest: +SKIP
array([[5.42519758, 6.40207856, 4.94991743],
   [1.85819627, 6.83403519, 1.9412172 ]])
>>> G = Geometric('G', 0.5) # Discrete Random Variable
>>> samp_list = sample(G, size=3)
>>> samp_list # doctest: +SKIP
[1, 3, 2]
>>> [sam in G.pspace.domain.set for sam in samp_list]
[True, True, True]
>>> MN = Normal("MN", [3, 4], [[2, 1], [1, 2]]) # Joint Random Variable
>>> samp_list = sample(MN, size=4)
>>> samp_list # doctest: +SKIP
[array([2.85768055, 3.38954165]),
 array([4.11163337, 4.3176591 ]),
 array([0.79115232, 1.63232916]),
 array([4.01747268, 3.96716083])]
>>> [tuple(sam) in MN.pspace.domain.set for sam in samp_list]
[True, True, True, True]

.. versionchanged:: 1.7.0
    sample used to return an iterator containing the samples instead of value.

.. versionchanged:: 1.9.0
    sample returns values or array of values instead of an iterator and numsamples is deprecated.

)r   r   r  r   rL   z
            The numsamples parameter to sympy.stats.sample() is deprecated.
            Either use a list comprehension, like

            [sample(...) for i in range(zO)]

            or add a dimension to size, like

            sample(..., size=z)
            z1.9z!deprecated-sympy-stats-numsamples)deprecated_since_versionactive_deprecations_target)sample_iterr#   rangenext)	rX   r   r   r   r  r   r?  iteratorr2   s	            r+   r%   r%   -  s    F 4CMZH Q!) *4 5  *md23 4	 &+'J	
 ).j(9:(91X(9::> ;s   Ac                    [        U 5      R                  " U 40 UD6nU(       a!  [        US5      (       a  UR                  5       $ U$ )a
  
Return the :math:`p^{th}` order quantile of a probability distribution.

Explanation
===========

Quantile is defined as the value at which the probability of the random
variable is less than or equal to the given probability.

.. math::
    Q(p) = \inf\{x \in (-\infty, \infty) : p \le F(x)\}

Examples
========

>>> from sympy.stats import quantile, Die, Exponential
>>> from sympy import Symbol, pprint
>>> p = Symbol("p")

>>> l = Symbol("lambda", positive=True)
>>> X = Exponential("x", l)
>>> quantile(X)(p)
-log(1 - p)/lambda

>>> D = Die("d", 6)
>>> pprint(quantile(D)(p), use_unicode=False)
/nan  for Or(p > 1, p < 0)
|
| 1       for p <= 1/6
|
| 2       for p <= 1/3
|
< 3       for p <= 1/2
|
| 4       for p <= 2/3
|
| 5       for p <= 5/6
|
\ 6        for p <= 1

r<  )r   compute_quantiler=  r<  )rX   r;  r?  rk  s       r+   quantiler    s>    T D\**4:6:FGFF++{{}r-   c                  ^ ^^^^^^^^^^^ SSK Jn  [        T5      (       d  [        ST-  5      eTb  [	        [        T T5      5      mO[	        T 5      m[        TR                  5      m[        T U5      (       a2  T R                  T [        T R                  T R                  5      05      m OZ0 nT R                   H7  n	[        X5      (       d  M  [        U	R                  U	R                  5      X'   M9     T R                  U5      m U U4S jmUU4S jmTS;   a  [        TT 40 UD6mO[        TT 4ST0UD6mTb  [        TT40 UD6mUUUUUUUUUU4
S jn
UUUUUUUUUUU4S	 jnT[        R                  L a  U
" 5       $ U" 5       $ )
a  
Returns an iterator of realizations from the expression given a condition.

Parameters
==========

expr: Expr
    Random expression to be realized
condition: Expr, optional
    A conditional expression
size : int, tuple
    Represents size of each sample in numsamples
numsamples: integer, optional
    Length of the iterator (defaults to infinity)
seed :
    An object to be used as seed by the given external library for sampling `expr`.
    Following is the list of possible types of object for the supported libraries,

    - 'scipy': int, numpy.random.RandomState, numpy.random.Generator
    - 'numpy': int, numpy.random.RandomState, numpy.random.Generator
    - 'pymc': int

    Optional, by default None, in which case seed settings
    related to the given library will be used.
    No modifications to environment's global seed settings
    are done by this argument.

Examples
========

>>> from sympy.stats import Normal, sample_iter
>>> X = Normal('X', 0, 1)
>>> expr = X*X + 3
>>> iterator = sample_iter(expr, numsamples=3) # doctest: +SKIP
>>> list(iterator) # doctest: +SKIP
[12, 4, 7]

Returns
=======

sample_iter: iterator object
    iterator object containing the sample/samples of given expr

See Also
========

sample
sampling_P
sampling_E

r   r   Failed to import %sc                 L   > TR                  [        [        TU 5      5      5      $ r0   r*  r  zip)rA   rX   r>  s    r+   fn_subssample_iter.<locals>.fn_subs!  s    yyc#tn-..r-   c                 T   > Tb$  TR                  [        [        TU 5      5      5      $ gr)   r  )rA   r   r>  s    r+   given_fn_subs"sample_iter.<locals>.given_fn_subs$  s&     >>$s3~"677r-   pymcpymc3r  c               3  d  >
#    Sn S[        T[        5      (       a  T4OT-   nU T:  af  TR                  UT
TS9nT Vs/ s H
  o2U   S   PM     nnTb&   T" U6 nUS:w  a  US:w  a  [        S5      eU(       d  MW  T" U6 v   U S-  n U T:  a  Me  g g s  snf ! [        [        4 a    T	" U6 n NRf = f7f)Nr   )rL   rG  TF(Conditions must not contain free symbolsrL   )r   intr%   	NameErrorr   r"  )count_sizer/  r,  rA   gdr   fngiven_fnr
  r   r  rx  r>  r   r   s         r+   return_generator_infinite.sample_iter.<locals>.return_generator_infinite4  s     D#!6!6tgDAj 		ugD	AA'*+sbE!HsD+$.!4B :"+$BD Dd)OQJE% j + "9- .&-B.s:   ;B0BB0B 2B0B0B-*B0,B--B0c               3  F  >#    Sn U (       a  TR                  T4[        T[        5      (       a  T4OT-   TTS9nSn SnUT:  a[  U (       dT  T Vs/ s H
  o1U   U   PM     nnTb&   T	" U6 nUS:w  a  US:w  a  [        S5      eU(       d  Sn US-  nUT:  a	  U (       d  MT  U (       a  M  SnUT:  a.  T Vs/ s H  nWU   U   PM     nn T" U6 v   US-  nUT:  a  M-  g g s  snf ! [        [        4 a    T
" U6 n Nf = fs  snf ! [        [        4 a
    T" U6 v    NMf = f7f)NTrG  Fr   r  rL   )r%   r   r  r  r   r"  )faultyr/  r  r,  rA   r  r   r  r  r  r
  r   r  rx  r>  r   r   s         r+   return_generator_finite,sample_iter.<locals>.return_generator_finiteK  sa    		
}:dC;P;PVZ[")  6A FE*$V/23s"es3(2%t_ TzbEk(FH H!%
 *$VV f. j +./3RAbE%L3D/%$i QJE j # 4 &y1 2*D12 0
 y) %tn$%sx   AD!C D!&C$ +2D!D!(D!4C?D!	D D!D!$C<9D!;C<<D!DD!DD!)r   r   r!   r"  r   r   r  r   r   r*  r   rl   rA   r   r   Infinity)rX   r   r   r   r  r   r?  r   subrh  r  r  r  r  r  r
  rx  r>  s   ``````      @@@@@@r+   r  r    sR   l 7!!.899E$	*+D\
ryy/C$)**yy$T[[$++ FGH99C#11'

CJJ?  yy~/
 ## c4*6*c4;;F; C5f5 ." "H QZZ(**"$$r-   c                     [        U 4XX4S.UD6$ N)r   r   r  r   r  rX   r   r   r  r   r?  s         r+   sample_iter_lambdifyr$  t  )     t Cy",C;AC Cr-   c                     [        U 4XX4S.UD6$ r!  r"  r#  s         r+   sample_iter_subsr'  z  r%  r-   c                    SnSn[        X4UX5S.UD6n	U	 H  n
U
(       a  US-  nM  US-  nM     [        U5      U-  nU(       a  UR                  5       $ U$ )zK
Sampling version of P.

See Also
========

P
sampling_E
sampling_density

r   r   r  r   rL   )r  r   evalf)r   r  r   r  r*  r   r?  
count_truecount_falsesamplesr%   rk  s               r+   
sampling_Pr.    su     JK) Fg%/F>DFG !OJ1K	  z]Z'F||~r-   c           	     z    [        [        X4UX5S.UD65      n[        U6 U-  nU(       a  UR                  5       $ U$ )zJ
Sampling version of E.

See Also
========

P
sampling_P
sampling_density
r)  )r  r  r   r*  )	rX   r  r   r  r*  r   r?  r-  rk  s	            r+   
sampling_Er0    sO     ;t Fg%/F>DF GG']Z'F||~r-   c                d    0 n[        X4UX4S.UD6 H  nUR                  US5      S-   Xg'   M     U$ )zO
Sampling version of density.

See Also
========
density
sampling_P
sampling_E
r)  r   rL   )r  get)rX   r  r   r  r   r?  r  rk  s           r+   r  r    sN     Gd JW)3JBHJ!++fa014J Nr-   c                    [        X5      (       a  g[        SSS9n[        U [        X5      5      [        U 5      :g  =(       d!    [        U[        X5      5      [        U5      :g  $ )a  
Dependence of two random expressions.

Two expressions are independent if knowledge of one does not change
computations on the other.

Examples
========

>>> from sympy.stats import Normal, dependent, given
>>> from sympy import Tuple, Eq

>>> X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
>>> dependent(X, Y)
False
>>> dependent(2*X + Y, -Y)
True
>>> X, Y = given(Tuple(X, Y), Eq(X + Y, 3))
>>> dependent(X, Y)
True

See Also
========

independent
FrW  TrX  )r  r   r   r   )r  r  rW  s      r+   	dependentr4    sX    6 !sA Ar!x GAJ. /Ar!x GAJ.0r-   c                "    [        X5      (       + $ )a  
Independence of two random expressions.

Two expressions are independent if knowledge of one does not change
computations on the other.

Examples
========

>>> from sympy.stats import Normal, independent, given
>>> from sympy import Tuple, Eq

>>> X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
>>> independent(X, Y)
True
>>> independent(2*X + Y, -Y)
False
>>> X, Y = given(Tuple(X, Y), Eq(X + Y, 3))
>>> independent(X, Y)
False

See Also
========

dependent
)r4  )r  r  s     r+   independentr6    s    6 r-   c                .   [        [        U5      R                  5      n[        [        U 5      R                  5      n[        [        [	        U 5      5      R                  [	        U5      5      5      S:w  a  g[        UR                  U5      5      S:X  a  gg)aC  
Tests for independence between a and b by checking if their PSpaces have
overlapping symbols. This is a sufficient but not necessary condition for
independence and is intended to be used internally.

Notes
=====

pspace_independent(a, b) implies independent(a, b)
independent(a, b) does not imply pspace_independent(a, b)
r   FTN)rM   r   r@   ru   r   intersection)r  r  	a_symbols	b_symbolss       r+   r  r    st     F1I%%&IF1I%%&I
3~a !..~a/@ABaG
9!!),-2r-   c                    Uc  [        U 5      nU(       d  U $ U Vs0 s H  o"UR                  _M     nnU R                  U5      $ s  snf )z
Given a random expression replace all random variables with their symbols.

If symbols keyword is given restrict the swap to only the symbols listed.
)r   rl   r*  )rX   r@   r,  r  s       r+   rv_subsr<  $  sI      &(/0"BIIH099X 1s   Ac                  *    \ rS rSr% SrS\S'   S rSrg)NamedArgsMixini2  r*   ztuple[str, ...]	_argnamesc                     U R                   U R                  R                  U5         $ ! [         a(    [	        S[        U 5      R                  < SU< S35      ef = f)N'z' object has no attribute ')rA   r?  indexr"  AttributeErrortyper[   )rG   attrs     r+   __getattr__NamedArgsMixin.__getattr__5  sU    	,99T^^11$788 	, T
##T"+ , ,	,s	   '* 2AN)r[   r\   r]   r^   r?  r   rF  re   r*   r-   r+   r>  r>  2  s    !#I#,r-   r>  c                      \ rS rSrSS jrSrg)Distributioni=  r*   Nc           
     n   [        U5      nUS;   a  Uc  [        SU-  5      eUS:X  aI  SSKJn  SSKnUb  [        U[        5      (       a  UR                  R                  US9nOUnU" XU5      nGOUS:X  aR  SS	K	J
n	  SSKnUb  [        U[        5      (       a  UR                  R                  US9nOUnUS
:X  a  SOUn
U	" X
U5      nOUS;   a  SSKJn  SSKnUR                  S5      R                  UR                   5         SSKnUR)                  5          U" U 5      b5  UR+                  [-        U5      SSSUSS9SS S   nUR/                  U5      nOSnSSS5        O[1        S[3        U5      -  5      eWb  U$ [1        SU < SU< 35      e! [$         a    SSKn Nf = f! , (       d  f       N:= f)z+A random realization from the distribution >   r  numpyr  r&   Nr  r&   r   )do_sample_scipy)r   rK  )do_sample_numpyr*   r  )do_sample_pymcr  rL   F)drawschainscompute_convergence_checksprogressbarrandom_seedreturn_inferencedataXz&Sampling from %s is not supported yet.zSampling for z# is not currently implemented from )r!   r"  !sympy.stats.sampling.sample_scipyrL  rK  r   r  randomdefault_rng!sympy.stats.sampling.sample_numpyrM   sympy.stats.sampling.sample_pymcrN  logging	getLoggersetLevelERRORr  ImportErrorr  Modelr%   r   reshaperQ   r   )rG   r   r   r   modulerL  rK  
rand_statesampsrM  r  rN  r[  r  s                 r+   r%   Distribution.sample?  s    w'99fn2W<==g J|z$44"\\5545@
!
#D
;EI|z$44"\\5545@
!
 BJDDE#D<E))Gf%..w}}=% !$'3 KKd4j_d(-4V[ ( ]]^``ceE!MM$/E E  &&N(+G'5 6 6 L!W 	#  %$% s   F AF&F#"F#&
F4r   )r[   r\   r]   r^   r%   re   r*   r-   r+   rI  rI  =  s    :r-   rI  c                j    [        U 5      (       d  U /n [        U 5      nUS:X  a  [        U5      eUS:H  $ )a  
Raise a ValueError with message if condition is False, else
return True if all conditions were True, else False.

Examples
========

>>> from sympy.stats.rv import _value_check
>>> from sympy.abc import a, b, c
>>> from sympy import And, Dummy

>>> _value_check(2 < 3, '')
True

Here, the condition is not False, but it does not evaluate to True
so False is returned (but no error is raised). So checking if the
return value is True or False will tell you if all conditions were
evaluated.

>>> _value_check(a < b, '')
False

In this case the condition is False so an error is raised:

>>> r = Dummy(real=True)
>>> _value_check(r < r - 1, 'condition is not true')
Traceback (most recent call last):
...
ValueError: condition is not true

If no condition of many conditions must be False, they can be
checked by passing them as an iterable:

>>> _value_check((a < 0, b < 0, c < 0), '')
False

The iterable can be a generator, too:

>>> _value_check((i < 0 for i in (a, b, c)), '')
False

The following are equivalent to the above but do not pass
an iterable:

>>> all(_value_check(i < 0, '') for i in (a, b, c))
False
>>> _value_check(And(a < 0, b < 0, c < 0), '')
False
FT)r$   r   r"  )r   messagetruths      r+   _value_checkri  |  s=    d IK	i E~!!D=r-   c                    [        U [        5      (       a  [        U 5      n [        U [        5      (       d  [        SU -  5      eU $ )aw  
Casts the parameter to Symbol if it is 'str'
otherwise no operation is performed on it.

Parameters
==========

sym
    The parameter to be converted.

Returns
=======

Symbol
    the parameter converted to Symbol.

Raises
======

TypeError
    If the parameter is not an instance of both str and
    Symbol.

Examples
========

>>> from sympy import Symbol
>>> from sympy.stats.rv import _symbol_converter
>>> s = _symbol_converter('s')
>>> isinstance(s, Symbol)
True
>>> _symbol_converter(1)
Traceback (most recent call last):
...
TypeError: 1 is neither a Symbol nor a string
>>> r = Symbol('r')
>>> isinstance(r, Symbol)
True
z#%s is neither a Symbol nor a string)r   r   r   r   )rw   s    r+   r~   r~     s>    P #sSkc6""=sCDDJr-   c                d    SSK Jn  [        X5      (       d  [        S5      eU R	                  5       $ )a  
This function is used to sample from stochastic process.

Parameters
==========

process: StochasticProcess
    Process used to extract the samples. It must be an instance of
    StochasticProcess

Examples
========

>>> from sympy.stats import sample_stochastic_process, DiscreteMarkovChain
>>> from sympy import Matrix
>>> T = Matrix([[0.5, 0.2, 0.3],[0.2, 0.5, 0.3],[0.2, 0.3, 0.5]])
>>> Y = DiscreteMarkovChain("Y", [0, 1, 2], T)
>>> next(sample_stochastic_process(Y)) in Y.state_space
True
>>> next(sample_stochastic_process(Y))  # doctest: +SKIP
0
>>> next(sample_stochastic_process(Y)) # doctest: +SKIP
2

Returns
=======

sample: iterator object
    iterator object containing the sample of given process

r   )StochasticProcessz1Process must be an instance of Stochastic Process)$sympy.stats.stochastic_process_typesrl  r   r"  r%   )processrl  s     r+   sample_stochastic_processro    s-    @ Gg11LMM>>r-   r0   )NNT)NTN)NT)Nr*   r&   rL   Nr  )Nr&   rL   TN)Nr&   rL   N)mr_   
__future__r   	functoolsr   mathr   sympy.core.addr   sympy.core.basicr   sympy.core.containersr   sympy.core.exprr	   sympy.core.functionr
   r   sympy.core.logicr   sympy.core.mulr   sympy.core.relationalr   r   sympy.core.singletonr   sympy.core.symbolr   r   sympy.core.sympifyr   'sympy.functions.special.delta_functionsr   (sympy.functions.special.tensor_functionsr   sympy.logic.boolalgr   r   "sympy.matrices.expressions.matexprr   sympy.tensor.indexedr   sympy.utilities.lambdifyr   r   r   sympy.sets.setsr   r   r   sympy.solvers.solvesetr    sympy.externalr!   sympy.utilities.decoratorr"   sympy.utilities.exceptionsr#   sympy.utilities.iterablesr$   __doctest_requires__r'   r,   registerr9   r;   rg   r{   r   r   r   r   r   r  r  r  rB  r   r   r8  r  r  r  r   r  r   r  r  r  r   r%   r  r  r  r$  r'  r.  r0  r  r4  r6  r  r<  r>  rI  ri  r~   ro  r*   r-   r+   <module>r     s   # $   " '   2 &  * " - & > C ) ; ( - , ' ? ? + ( 8 @ . $gY/  3K   E, ,
 $5  $F6< 6:&< &$ A  AF4$U 4$n26 24B4 BH, @
1| 
1	F 	Y&} Y&vAFL AFH
!@H$&Rj&(R =A$3N/e /d-F`*ZB	8> J'29"v (vr/b !%2w zzZ%x *.B$%JJTC &* !

C MN $@ HI $* :A'+("0J<., ,<5 <~7r,\#r-   