
    \h[>                        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
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Jr  SSKJrJrJr  SSK J!r!J"r"  SSK#J$r$J%r%  SSK&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.  SSK/J0r0  SSK1J2r2  SSK3J4r4   " S S\'5      r5 " S S5      r6 " S S5      r7 " S S5      r8\6\8\8\7S.r9 " S S\)\(5      r: " S  S!\+5      r; " S" S#\)5      r<g$)%zq
Joint Random Variables Module

See Also
========
sympy.stats.rv
sympy.stats.frv
sympy.stats.crv
sympy.stats.drv
    )prod)Basic)Lambda)S)DummySymbol)sympify)
ProductSetIndexed)Product)Sum	summation)Tuple)Integral	integrate)ImmutableMatrixmatrix2numpy
list2numpy)SingleContinuousDistributionSingleContinuousPSpace)SingleDiscreteDistributionSingleDiscretePSpace)ProductPSpaceNamedArgsMixinDistributionProductDomainRandomSymbolrandom_symbolsSingleDomain_symbol_converter)iterable)
filldedent)import_modulec                       \ 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	 5       r\S
 5       rS rS rSS jrS rS rSS jrS rSrg)JointPSpace$   zh
Represents a joint probability space. Represented using symbols for
each component and a distribution.
c                     [        U[        5      (       a  [        X5      $ [        U[        5      (       a  [	        X5      $ [        U5      n[        R                  " XU5      $ N)
isinstancer   r   r   r   r!   r   __new__)clssymdists      L/var/www/auris/envauris/lib/python3.13/site-packages/sympy/stats/joint_rv.pyr+   JointPSpace.__new__)   sO    d899)#44d677'22$}}St,,    c                 .    U R                   R                  $ r)   )domainsetselfs    r/   r4   JointPSpace.set1   s    {{r1   c                      U R                   S   $ )Nr   argsr5   s    r/   symbolJointPSpace.symbol5       yy|r1   c                      U R                   S   $ N   r9   r5   s    r/   distributionJointPSpace.distribution9   r=   r1   c                 .    [        U R                  U 5      $ r)   )JointRandomSymbolr;   r5   s    r/   valueJointPSpace.value=   s     d33r1   c                    U R                   R                  n[        U[        5      (       a  [	        [        UR                  5      5      $ [        U[        5      (       a  UR                  S   S   $ [        R                  $ )Nr   )
rA   r4   r*   r
   r   lenr:   r   limitsOne)r6   _sets     r/   component_countJointPSpace.component_countA   s\      $$dJ''S^$$g&&;;q>"%%uur1   c                     [        U R                  5       Vs/ s H  n[        U R                  U5      PM     nnU R                  " U6 $ s  snf r)   )rangerM   r   r;   rA   )r6   ir-   s      r/   pdfJointPSpace.pdfJ   sE    05d6J6J0KL0K1wt{{A&0KL  #&& Ms    A	c                     [        U R                  5      nU(       d*  [        U R                  U R                  R                  5      $ [        U Vs/ s H  o"R                  R                  PM     sn6 $ s  snf r)   )r   rA   r    r;   r4   r   pspacer3   r6   rvsrvs      r/   r3   JointPSpace.domainO   sW    T../T->->-B-BCC#>#Byy//#>??>s   A3c                 4    U R                   R                  U   $ r)   )r4   r:   )r6   indexs     r/   component_domainJointPSpace.component_domainV   s    xx}}U##r1   c                   ^  T R                   nUR                  [        5      (       a  [        S5      e[	        U5       Vs/ s H  n[        T R                  U5      PM     nnU Vs/ s H  n[        [        U5      5      PM     nn[        [        XT5      5      n[        U 4S jU 5       5      nU Vs/ s H  o3U;  d  M
  U/PM     nnSn	[	        U5       HR  nX1;  d  M
  X   R                  T R                  R                  R                  U   5        [        X   5      X'   U	S-  n	MT     T R                  R                  (       a$  [!        U[#        T R                  " U6 /UQ76 5      n
O>T R                  R$                  (       a#  [!        U['        T R                  " U6 /UQ76 5      n
W
R)                  U5      $ s  snf s  snf s  snf )Nz_Marginal distributions cannot be computed for symbolic dimensions. It is a work under progress.c           	   3   t   >#    U  H-  n[        [        [        TR                  U5      5      5      v   M/     g 7fr)   )r   strr   r;   .0rQ   r6   s     r/   	<genexpr>4JointPSpace.marginal_distribution.<locals>.<genexpr>a   s)     J'QF3wt{{A6788's   58r   r@   )rM   atomsr   
ValueErrorrP   r   r;   r`   dictziptupleappendrA   r4   r:   is_Continuousr   r   is_Discreter   xreplace)r6   indicescountrQ   origall_symsreplace_dictr-   rJ   r[   fs   `          r/   marginal_distribution!JointPSpace.marginal_distributionY   s   $$;;v X Y Y16u>AQ'>,01DqF3q6ND1C/0J'JJ (91SL$1$9uA$$T%6%6%:%:%?%?%BC %fm 4
	 
 **sId&7&7&BLVLMA**sId&7&7&BLVLMAzz,'' ?1 :s     F:&F?3	G GNc           	      ~  ^ ^ [        U 4S j[        T R                  5       5       5      nT=(       d    Um[        U4S jU 5       5      (       d  U$ UT R                  -  nT H  n[        U[        5      (       a?  UR                  U[        [        UR                  5      UR                  S   5      05      nMW  [        U[        5      (       d  Mn  UR                  XfR                  05      nM     T R                  [        U5      ;   a  [        [!        S5      5      e[        U 4S jU 5       5      n[#        U/UQ76 $ )Nc              3   B   >#    U  H  nTR                   U   v   M     g 7fr)   )rE   ra   s     r/   rc   2JointPSpace.compute_expectation.<locals>.<genexpr>p   s     H,GqTZZ],Gs   c              3   ,   >#    U  H	  oT;   v   M     g 7fr)    )rb   rQ   rW   s     r/   rc   rx   r   s     *T8Ts   r@   zq
            Expectations of expression with unindexed joint random symbols
            cannot be calculated yet.c              3      >#    U  Ha  n[        [        UR                  5      UR                  S    5      TR                  R
                  R                  UR                  S       4v   Mc     g7f)r@   N)r   r`   baser:   rA   r4   )rb   rX   r6   s     r/   rc   rx   ~   sW      D>B  BGGRWWQZ8!!&&rwwqz24>Bs   A)A,)ri   rP   rM   anyrR   r*   r   rm   r`   r|   r:   r   r;   rE   r   NotImplementedErrorr#   r   )r6   exprrW   evaluatekwargssymsrX   rJ   s   ` `     r/   compute_expectationJointPSpace.compute_expectationo   s   HE$2F2F,GHHkT*T***KDHH}B"g&&}}b'#bgg,
*K%LMB--}}b))_5	 
 ::--%j 2) '* + +  D>BD D&v&&r1   c                     [        5       er)   r~   r6   	conditions     r/   whereJointPSpace.where       !##r1   c                     [        5       er)   r   )r6   r   s     r/   compute_densityJointPSpace.compute_density   r   r1   rz   c                 b    [        U R                  U 5      U R                  R                  UX#S90$ )zW
Internal sample method

Returns dictionary mapping RandomSymbol to realization value.
)libraryseed)r   r;   rA   sample)r6   sizer   r   s       r/   r   JointPSpace.sample   s:     T[[$/1B1B1I1I$# 2J 20 1 	1r1   c                     [        5       er)   r   r   s     r/   probabilityJointPSpace.probability   r   r1   )NFrz   scipyN)__name__
__module____qualname____firstlineno____doc__r+   propertyr4   r;   rA   rE   rM   rR   r3   r\   rt   r   r   r   r   r   __static_attributes__rz   r1   r/   r&   r&   $   s    -       4 4   ' ' @ @$(,'&$$1$r1   r&   c                   2    \ rS rSrSrSS jr\S 5       rSrg)SampleJointScipy   z7Returns the sample from scipy of the given distributionNc                 &    U R                  XU5      $ r)   )_sample_scipyr,   r.   r   r   s       r/   r+   SampleJointScipy.__new__         T22r1   c                   ^	^
 SSK nUb  [        U[        5      (       a  UR                  R	                  US9m	OUm	SSKJm
  U	U
4S jU	U
4S jU	U
4S jS.nS	 S
 S S.nUR                  5       nUR                  R                  U;  a  gXQR                  R                     " X5      nUR                  X&UR                  R                     " U5      -   5      $ )zSample from SciPy.r   Nr   )statsc                    > TR                   R                  [        U R                  5      R	                  5       [        U R
                  5      UTS9$ )N)meancovr   random_state)multivariate_normalrW   r   muflattensigmar.   r   
rand_statescipy_statss     r/   <lambda>0SampleJointScipy._sample_scipy.<locals>.<lambda>   sD    A`A`AdAd!$''*224 ,4j Be BRr1   c                    > TR                   R                  [        U R                  [        5      R                  5       UTS9$ )N)alphar   r   )	dirichletrW   r   r   floatr   r   s     r/   r   r      s;    {?T?T?X?X U3;;=DWa @Y @cr1   c                    > TR                   R                  [        U R                  5      [	        U R
                  [        5      R                  5       UTS9$ )N)npr   r   )multinomialrW   intr   r   r   r   r   r   s     r/   r   r      sC    +:Q:Q:U:Udff+DFFE!:!B!B!D4^h ;V ;jr1   MultivariateNormalDistributionMultivariateBetaDistributionMultinomialDistributionc                 \    [        U R                  5      R                  5       R                  $ r)   r   r   r   shaper.   s    r/   r   r          <;P;X;X;Z;`;`r1   c                 \    [        U R                  5      R                  5       R                  $ r)   r   r   r   r   r   s    r/   r   r          DJJ9O9W9W9Y9_9_r1   c                 \    [        U R                  5      R                  5       R                  $ r)   r   r   r   r   r   s    r/   r   r          Jtvv4F4N4N4P4V4Vr1   )numpyr*   r   randomdefault_rngr   r   keys	__class__r   reshape)r,   r.   r   r   r   scipy_rv_mapsample_shape	dist_listsamplesr   r   s            @@r/   r   SampleJointScipy._sample_scipy   s     	<:dC0011t1<JJ./R-c(j
 /a,_'V
 !%%'	>>"")3~~667Ct4>>3J3J&KD&QQRRr1   rz   r)   )	r   r   r   r   r   r+   classmethodr   r   rz   r1   r/   r   r      s     A3 S Sr1   r   c                   2    \ rS rSrSrSS jr\S 5       rSrg)SampleJointNumpy   z7Returns the sample from numpy of the given distributionNc                 &    U R                  XU5      $ r)   )_sample_numpyr   s       r/   r+   SampleJointNumpy.__new__   r   r1   c                   ^	 SSK nUb  [        U[        5      (       a  UR                  R	                  US9m	OUm	U	4S jU	4S jU	4S jS.nS S	 S
 S.nUR                  5       nUR                  R                  U;  a  gXQR                  R                     " U[        U5      5      nUR                  X&UR                  R                     " U5      -   5      $ )zSample from NumPy.r   Nr   c                    > TR                  [        U R                  [        5      R	                  5       [        U R
                  [        5      US9$ )N)r   r   r   )r   r   r   r   r   r   r.   r   r   s     r/   r   0SampleJointNumpy._sample_numpy.<locals>.<lambda>   s@    A_A_!$''5199; U3$ B` B@r1   c                 p   > TR                  [        U R                  [        5      R	                  5       US9$ )N)r   r   )r   r   r   r   r   r   s     r/   r   r      s2    z?S?S U3;;=D @T @Jr1   c                    > TR                  [        U R                  5      [        U R                  [
        5      R                  5       US9$ )N)r   pvalsr   )r   r   r   r   r   r   r   r   s     r/   r   r      s:    *:P:Pdff+Z%>%F%F%Ht ;Q ;Ur1   r   c                 \    [        U R                  5      R                  5       R                  $ r)   r   r   s    r/   r   r      r   r1   c                 \    [        U R                  5      R                  5       R                  $ r)   r   r   s    r/   r   r      r   r1   c                 \    [        U R                  5      R                  5       R                  $ r)   r   r   s    r/   r   r      r   r1   )
r   r*   r   r   r   r   r   r   r   r   )
r,   r.   r   r   r   numpy_rv_mapr   r   r   r   s
            @r/   r   SampleJointNumpy._sample_numpy   s     	<:dC0011t1<JJ/@-J(U
 /a,_'V
 !%%'	>>"")3~~667d4jIt4>>3J3J&KD&QQRRr1   rz   r)   )	r   r   r   r   r   r+   r   r   r   rz   r1   r/   r   r      s     A3 S Sr1   r   c                   2    \ rS rSrSrSS jr\S 5       rSrg)SampleJointPymc   z6Returns the sample from pymc of the given distributionNc                 &    U R                  XU5      $ r)   )_sample_pymcr   s       r/   r+   SampleJointPymc.__new__   s    D11r1   c           
      V  ^	  SSK m	U	4S jU	4S jU	4S jS.nS S S	 S.nUR                  5       nUR                  R
                  U;  a  gSSKnUR                  S
5      R                  UR                  5        T	R                  5          XAR                  R
                     " U5        T	R                  [        U5      SSUSSS9SS S   nSSS5        WR                  X%UR                  R
                     " U5      -   5      $ ! [         a    SSKm	 GNf = f! , (       d  f       NS= f)zSample from PyMC.r   Nc                    > TR                  S[        U R                  [        5      R	                  5       [        U R
                  [        5      SU R                  R                  S   4S9$ )NXr@   r   )r   r   r   )MvNormalr   r   r   r   r   r   r.   pymcs    r/   r   .SampleJointPymc._sample_pymc.<locals>.<lambda>   sO    cl477E&B&J&J&L U3Atww}}Q?O;P  Rr1   c                 p   > TR                  S[        U R                  [        5      R	                  5       S9$ )Nr   )a)	Dirichletr   r   r   r   r   s    r/   r   r      s'    sjU&C&K&K&MNr1   c           	         > TR                  S[        U R                  5      [        U R                  [
        5      R                  5       S[        U R                  5      4S9$ )Nr   r@   )r   r   r   )Multinomialr   r   r   r   r   r   rI   r   s    r/   r   r      sI      DFFTVVU+335aTVV=M ! Or1   r   c                 \    [        U R                  5      R                  5       R                  $ r)   r   r   s    r/   r   r      r   r1   c                 \    [        U R                  5      R                  5       R                  $ r)   r   r   s    r/   r   r      r   r1   c                 \    [        U R                  5      R                  5       R                  $ r)   r   r   s    r/   r   r      r   r1   pymc3r@   F)drawschainsprogressbarrandom_seedreturn_inferencedatacompute_convergence_checksr   )r   ImportErrorr  r   r   r   logging	getLoggersetLevelERRORModelr   r   r   )
r,   r.   r   r   pymc_rv_mapr   r   r  r   r   s
            @r/   r   SampleJointPymc._sample_pymc   s%   	!/R-O(O	
 /a,_'V
  $$&	>>"")3'"++GMM:ZZ\//06kkT
1%]ax}  [`k  a  bc  d  eh  iG  t4>>3J3J&KD&QQRR;  	! 	!4 \s   D ADDD
D(rz   r)   )	r   r   r   r   r   r+   r   r   r   rz   r1   r/   r   r      s     @2 "S "Sr1   r   )r   r  r   r   c                   X    \ rS rSrSrSrS r\S 5       r\S 5       r	S r
SS
 jrS rSrg	)JointDistributioni  z
Represented by the random variables part of the joint distribution.
Contains methods for PDF, CDF, sampling, marginal densities, etc.
rR   c                     [        [        [        U5      5      n[        [	        U5      5       H+  n[        X   [         5      (       d  M  [        X   5      X'   M-     [        R                  " U /UQ76 $ r)   )	listmapr	   rP   rI   r*   r   r   r+   )r,   r:   rQ   s      r/   r+   JointDistribution.__new__  sY    C&'s4y!A$'4(()$'2 " }}S(4((r1   c                 ,    [        U R                  5      $ r)   )r   symbolsr5   s    r/   r3   JointDistribution.domain%  s    T\\**r1   c                 4    U R                   R                  S   $ r?   )densityr:   r5   s    r/   rR   JointDistribution.pdf)  s    ||  ##r1   c           	      .   [        U[        5      (       d  [        U< S[        U5      < 35      eUR	                  5       nU R
                  R                  R                  nU R                  [        S U R                   5       5      5      n[        [        U5      5       Hq  nX%   R                  (       a$  [        XBU   X5   R                  XU      45      nM:  X%   R                   (       d  MO  [#        XBU   X5   R                  XU      45      nMs     W$ )Nz should be of type dict, got c              3   >   #    U  H  oR                   S    v   M     g7fr   Nr9   )rb   rQ   s     r/   rc   (JointDistribution.cdf.<locals>.<genexpr>2  s     >AffQis   )r*   rg   rf   typer   r3   r4   setsrR   ri   r  rP   rI   rk   r   infrl   r   )r6   otherrW   rL   r   rQ   r  s          r/   cdfJointDistribution.cdf-  s    %&&%eUVVjjl{{##xx>>>?s5z"Av##"4a&$'++a&M*# $###dVTW[[a&M%# $ # r1   rz   Nc                     SnX$;  a  [        S[        U5      -  5      e[        U5      (       d  [        SU-  5      e[        U   " XUS9nUb  U$ [        SU R
                  R                  < SU< 35      e)z+A random realization from the distribution )r   r   r  r   z&Sampling from %s is not supported yet.zFailed to import %sr   zSampling for z# is not currently implemented from )r~   r`   r$   rf   _get_sample_class_jrvr   r   )r6   r   r   r   	librariessampss         r/   r   JointDistribution.sample<  s     8	#%&N*-g,'7 8 8W%%2W<==%g.tEL!>>**G5 	r1   c                      U R                   " U6 $ r)   r  r6   r:   s     r/   __call__JointDistribution.__call__O      xxr1   r   )r   r   r   r   r   	_argnamesr+   r   r3   rR   r(  r   r1  r   rz   r1   r/   r  r    sJ    
 I) + + $ $&r1   r  c                       \ rS rSrSrS rSrg)rD   iR  z[
Representation of random symbols with joint probability distributions
to allow indexing."
c                     [        U R                  [        5      (       a]  U R                  R                  U:*  S:X  a5  [	        SU R
                  < SU R                  R                  S-
  < S35      e[        X5      $ g )NTzIndex keys for z can only up to r@   .)r*   rU   r&   rM   rf   namer   )r6   keys     r/   __getitem__JointRandomSymbol.__getitem__W  sg    dkk;//++s2t; YY ; ;a ?"A B B4%%	 0r1   rz   N)r   r   r   r   r   r:  r   rz   r1   r/   rD   rD   R  s    &r1   rD   c                   \    \ rS rSrSrS rS r\S 5       r\S 5       r	S r
S rS	 rS
 rSrg)MarginalDistributioni`  z
Represents the marginal distribution of a joint probability space.

Initialised using a probability distribution and random variables(or
their indexed components) which should be a part of the resultant
distribution.
c                 ~   [        U5      S:X  a!  [        US   5      (       a  [        US   5      n[        S U 5       5      (       d  [	        [        S5      5      e[        R                  " S U 5       5      n[        U[        5      (       d  [        [        U5      5      S:X  a  U$ [        R                  " XU5      $ )Nr@   r   c              3   N   #    U  H  n[        U[        [        45      v   M     g 7fr)   )r*   r   r   rb   rX   s     r/   rc   /MarginalDistribution.__new__.<locals>.<genexpr>l  s     ISr:b7L"9::Ss   #%zMarginal distribution can be
             intitialised only in terms of random variables or indexed random
             variablesc              3   $   #    U  H  ov   M     g 7fr)   rz   r@  s     r/   rc   rA  p  s     .#BR#s   )rI   r"   ri   allrf   r#   r   fromiterr*   r  r   r   r+   )r,   r.   rW   s      r/   r+   MarginalDistribution.__new__i  s    s8q=Xc!f--A-CISIIIZ )    nn.#..$ 122s>$;O7PTU7UK}}S,,r1   c                     g r)   rz   r5   s    r/   checkMarginalDistribution.checku  s    r1   c                     U R                   S    Vs/ s H  n[        U[        5      (       d  M  UPM     nn[        U Vs/ s H  o3R                  R
                  PM     sn6 $ s  snf s  snf r?   )r:   r*   r   r
   rU   r4   )r6   rQ   rW   rX   s       r/   r4   MarginalDistribution.setx  sP    ))A,F,Q*Q*Eq,FC8CbIIMMC899 G8s   A#A# A(c                 v    U R                   S   nU Vs1 s H  o"R                  R                  iM     sn$ s  snf r?   )r:   rU   r;   rV   s      r/   r  MarginalDistribution.symbols}  s/    iil+./3R		  3///s   6c                   ^ U R                   S   U R                   S   p2[        U5       Vs/ s H  oDU;  d  M
  UPM     nn[        U[        5      (       aP  [	        UR
                  R                   5      n[        SSS9m[        U4S jU 5       5      nUR                  U5      nO[        S U 5       5      n[        XpR                  X%5      5      " T6 $ s  snf )Nr   r@   xT)realc              3   <   >#    U  H  n[        TU5      v   M     g 7fr)   r   )rb   rQ   rN  s     r/   rc   +MarginalDistribution.pdf.<locals>.<genexpr>  s     61As   c              3      #    U  H?  n[        U[        5      (       a  UR                  R                  OUR                  S    v   MA     g7fr"  )r*   r   rU   r;   r:   r@  s     r/   rc   rQ    s7     hdg^`ZL-I-I))rwwWXzYdgs   AA	)r:   r   r*   r  rI   r3   r   ri   rR   r   compute_pdf)r6   rN  r   rW   rQ   marginalise_outro   r   s    `      r/   rR   MarginalDistribution.pdf  s    IIaL$))A,c&4T&:K&:sl1&:Kd-..(()Ec%A666D88D>DhdghhDd,,TCDaHH Ls
   	CCc                     U HD  nSn[        U[        5      (       a  UR                  R                  nU R	                  X-  U5      nMF     U$ r?   )r*   r   rU   rR   rT  )r6   r   rW   rX   lpdfs        r/   rS   MarginalDistribution.compute_pdf  sF    BD"l++yy}}''	26D	 
 r1   c                    SSK Jn  [        U[        5      (       a  UR                  R
                  nOV[        U[        5      (       aA  UR                  R                  UR                  R                  UR                  S   5      5      nUR                  X"R                  R                  05      nUR                  R                  (       a#  [        XR                  R                  W45      nU$ UR                  R                  (       ai  W[        R                   [        R"                  [        R$                  4;   a  UR&                  UR(                  4nU" XR                  R                  U45      nU$ )Nr   )r   r@   )sympy.concrete.summationsr   r*   r   rU   r4   r   r|   r\   r:   rm   r;   rk   r   rl   r   IntegersNaturals	Naturals0r&  sup)r6   r   rX   r   doms        r/   rT  $MarginalDistribution.marginalise_out  s    1b,''))--CG$$''**		**2771:68C}}b))"2"23499"" D99#3#3S"9:D  YY""qzz1::q{{;;ww(tii..45Dr1   c                      U R                   " U6 $ r)   r  r0  s     r/   r1  MarginalDistribution.__call__  r3  r1   rz   N)r   r   r   r   r   r+   rG  r   r4   r  rR   rS  rT  r1  r   rz   r1   r/   r=  r=  `  sN    
- : : 0 0
I&r1   r=  N)=r   mathr   sympy.core.basicr   sympy.core.functionr   sympy.core.singletonr   sympy.core.symbolr   r   sympy.core.sympifyr	   sympy.sets.setsr
   sympy.tensor.indexedr   sympy.concrete.productsr   rZ  r   r   sympy.core.containersr   sympy.integrals.integralsr   r   sympy.matricesr   r   r   sympy.stats.crvr   r   sympy.stats.drvr   r   sympy.stats.rvr   r   r   r   r   r   r    r!   sympy.utilities.iterablesr"   sympy.utilities.miscr#   sympy.externalr$   r&   r   r   r   r+  r  rD   r=  rz   r1   r/   <module>ru     s   	  " & " - & & ( + 4 ' 9 D D P L= = = / + (n$- n$b%S %SN%S %SN)S )SZ 	 :n :x
& 
&J< Jr1   