
    wg[>                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZmZmZ ddl m!Z!m"Z" ddl#m$Z$m%Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4  G d de'      Z5 G d d      Z6 G d d      Z7 G d d      Z8e6e8e8e7dZ9 G d de)e(      Z: G d  d!e+      Z; G d" d#e)      Z<y$)%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                       e Zd ZdZd Zed        Zed        Zed        Zed        Z	ed        Z
ed        Zed	        Zd
 Zd ZddZd Zd ZddZd Zy)JointPSpacezt
    Represents a joint probability space. Represented using symbols for
    each component and a distribution.
    c                     t        |t              rt        ||      S t        |t              rt	        ||      S t        |      }t        j                  | ||      S N)
isinstancer   r   r   r   r!   r   __new__)clssymdists      Y/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/stats/joint_rv.pyr*   zJointPSpace.__new__)   sO    d89)#t44d67'T22$}}S#t,,    c                 .    | j                   j                  S r(   )domainsetselfs    r.   r2   zJointPSpace.set1   s    {{r/   c                      | j                   d   S )Nr   argsr3   s    r.   symbolzJointPSpace.symbol5       yy|r/   c                      | j                   d   S N   r6   r3   s    r.   distributionzJointPSpace.distribution9   r9   r/   c                 .    t        | j                  |       S r(   )JointRandomSymbolr8   r3   s    r.   valuezJointPSpace.value=   s     d33r/   c                     | j                   j                  }t        |t              rt	        t        |j                              S t        |t              r|j                  d   d   S t        j                  S )Nr   )
r=   r2   r)   r
   r   lenr7   r   limitsOne)r4   _sets     r.   component_countzJointPSpace.component_countA   sV      $$dJ'S^$$g&;;q>"%%uur/   c                     t        | j                        D cg c]  }t        | j                  |       }} | j                  | S c c}w r(   )rangerG   r   r8   r=   )r4   ir,   s      r.   pdfzJointPSpace.pdfJ   sD    05d6J6J0KL1wt{{A&LL t  #&& Ms   Ac                     t        | j                        }|s*t        | j                  | j                  j                        S t        |D cg c]  }|j                  j                   c} S c c}w r(   )r   r=   r    r8   r2   r   pspacer1   r4   rvsrvs      r.   r1   zJointPSpace.domainO   sU    T../T->->-B-BCC#>Bryy//>??>s   A,c                 4    | j                   j                  |   S r(   )r2   r7   )r4   indexs     r.   component_domainzJointPSpace.component_domainV   s    xx}}U##r/   c                 \     j                   }|j                  t              rt        d      t	        |      D cg c]  }t         j                  |       }}|D cg c]  }t        t        |             }}t        t        ||            }t         fd|D              }|D cg c]
  }||vs|g }}d}	t	        |      D ]R  }||vs||	   j                   j                  j                  j                  |          t        ||	         ||	<   |	dz  }	T  j                  j                  r$t!        |t#          j                  | g|       }
n9 j                  j$                  r#t!        |t'          j                  | g|       }

j)                  |      S c c}w c c}w c c}w )Nz_Marginal distributions cannot be computed for symbolic dimensions. It is a work under progress.c           	   3   n   K   | ],  }t        t        t        j                  |                   . y wr(   )r   strr   r8   .0rJ   r4   s     r.   	<genexpr>z4JointPSpace.marginal_distribution.<locals>.<genexpr>a   s&     JQF3wt{{A678Js   25r   r<   )rG   atomsr   
ValueErrorrI   r   r8   rV   dictziptupleappendr=   r2   r7   is_Continuousr   r   is_Discreter   xreplace)r4   indicescountrJ   origall_symsreplace_dictr,   rD   rR   fs   `          r.   marginal_distributionz!JointPSpace.marginal_distributionY   s   $$;;v X Y Y16u>AQ'>>,01qF3q6N11C$/0J'JJ (91ASL1$99u 	Au$$T%6%6%:%:%?%?%BC %fUm 4u
		
 **sI&7d&7&7&BLVLMA**sI&7d&7&7&BLVLMAzz,'' ?1 :s   FF$(	F)2F)Nc           	      F    t         fdt         j                        D              }xs |t        fd|D              s|S | j                  z  }D ]~  }t        |t              r>|j                  |t        t        |j                        |j                  d         i      }Qt        |t              sb|j                  ||j                  i      }  j                  t        |      v rt        t!        d            t         fd|D              }t#        |g| S )Nc              3   <   K   | ]  }j                   |     y wr(   )r@   rW   s     r.   rY   z2JointPSpace.compute_expectation.<locals>.<genexpr>p   s     HqTZZ]Hs   c              3   &   K   | ]  }|v  
 y wr(    )rX   rJ   rO   s     r.   rY   z2JointPSpace.compute_expectation.<locals>.<genexpr>r   s     *18*s   r<   zq
            Expectations of expression with unindexed joint random symbols
            cannot be calculated yet.c              3      K   | ]`  }t        t        |j                        |j                  d          j                  j
                  j                  |j                  d       f b yw)r<   N)r   rV   baser7   r=   r2   )rX   rP   r4   s     r.   rY   z2JointPSpace.compute_expectation.<locals>.<genexpr>~   sY      D8:  BGGRWWQZ8!!&&rwwqz24 Ds   A&A))r^   rI   rG   anyrK   r)   r   rb   rV   ro   r7   r   r8   r@   r   NotImplementedErrorr#   r   )r4   exprrO   evaluatekwargssymsrP   rD   s   ` `     r.   compute_expectationzJointPSpace.compute_expectationo   s   HE$2F2F,GHHkT*T**KDHH} 	6B"g&}}b'#bgg,
*K%LMB-}}b"))_5		6
 ::--%j 2) '* + +  D>BD D&v&&r/   c                     t               r(   rq   r4   	conditions     r.   wherezJointPSpace.where       !##r/   c                     t               r(   rx   )r4   rr   s     r.   compute_densityzJointPSpace.compute_density   r|   r/   c                 j    t        | j                  |       | j                  j                  |||      iS )zo
        Internal sample method

        Returns dictionary mapping RandomSymbol to realization value.
        )libraryseed)r   r8   r=   sample)r4   sizer   r   s       r.   r   zJointPSpace.sample   s<     T[[$/1B1B1I1I$#$ 2J 20 1 	1r/   c                     t               r(   rx   ry   s     r.   probabilityzJointPSpace.probability   r|   r/   )NFrm   scipyN)__name__
__module____qualname____doc__r*   propertyr2   r8   r=   r@   rG   rK   r1   rS   ri   rv   r{   r~   r   r   rm   r/   r.   r&   r&   $   s    -       4 4   ' ' @ @$(,'&$$1$r/   r&   c                   (    e Zd ZdZddZed        Zy)SampleJointScipyz7Returns the sample from scipy of the given distributionNc                 (    | j                  |||      S r(   )_sample_scipyr+   r-   r   r   s       r.   r*   zSampleJointScipy.__new__         tT22r/   c                   	
 ddl }|t        |t              r|j                  j	                  |      	n|	ddlm
 	
fd	
fd	
fdd}d	 d
 d d}|j                         }|j                  j                  |vry ||j                  j                     ||      }|j                  | ||j                  j                     |      z         S )zSample from SciPy.r   Nr   )statsc                     j                   j                  t        | j                        j	                         t        | j
                        |      S )N)meancovr   random_state)multivariate_normalrO   r   muflattensigmar-   r   
rand_statescipy_statss     r.   <lambda>z0SampleJointScipy._sample_scipy.<locals>.<lambda>   sF    A`A`AdAd!$''*224 ,4j Be BR r/   c                     j                   j                  t        | j                  t              j                         |      S )N)alphar   r   )	dirichletrO   r   r   floatr   r   s     r.   r   z0SampleJointScipy._sample_scipy.<locals>.<lambda>   s=    {?T?T?X?X U3;;=DWa @Y @c r/   c                     j                   j                  t        | j                        t	        | j
                  t              j                         |      S )N)npr   r   )multinomialrO   intr   r   r   r   r   r   s     r.   r   z0SampleJointScipy._sample_scipy.<locals>.<lambda>   sE    +:Q:Q:U:Udff+DFFE!:!B!B!D4^h ;V ;j r/   MultivariateNormalDistributionMultivariateBetaDistributionMultinomialDistributionc                 \    t        | j                        j                         j                  S r(   r   r   r   shaper-   s    r.   r   z0SampleJointScipy._sample_scipy.<locals>.<lambda>       <;P;X;X;Z;`;` r/   c                 \    t        | j                        j                         j                  S r(   r   r   r   r   r   s    r.   r   z0SampleJointScipy._sample_scipy.<locals>.<lambda>       DJJ9O9W9W9Y9_9_ r/   c                 \    t        | j                        j                         j                  S r(   r   r   r   r   r   s    r.   r   z0SampleJointScipy._sample_scipy.<locals>.<lambda>       Jtvv4F4N4N4P4V4V r/   )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   zSampleJointScipy._sample_scipy   s     	<:dC011t1<JJ./R-c(j
 /a,_'V
 !%%'	>>"")37,t~~667dCt&Kl4>>3J3J&KD&QQRRr/   r(   )r   r   r   r   r*   classmethodr   rm   r/   r.   r   r      s     A3 S Sr/   r   c                   (    e Zd ZdZddZed        Zy)SampleJointNumpyz7Returns the sample from numpy of the given distributionNc                 (    | j                  |||      S r(   )_sample_numpyr   s       r.   r*   zSampleJointNumpy.__new__   r   r/   c                   	 ddl }|t        |t              r|j                  j	                  |      	n|		fd	fd	fdd}d d	 d
 d}|j                         }|j                  j                  |vry ||j                  j                     |t        |            }|j                  | ||j                  j                     |      z         S )zSample from NumPy.r   Nr   c                     j                  t        | j                  t              j	                         t        | j
                  t              |      S )N)r   r   r   )r   r   r   r   r   r   r-   r   r   s     r.   r   z0SampleJointNumpy._sample_numpy.<locals>.<lambda>   sB    A_A_!$''5199; U3$ B` B@ r/   c                 v    j                  t        | j                  t              j	                         |      S )N)r   r   )r   r   r   r   r   r   s     r.   r   z0SampleJointNumpy._sample_numpy.<locals>.<lambda>   s4    z?S?S U3;;=D @T @J r/   c                     j                  t        | j                        t        | j                  t
              j                         |      S )N)r   pvalsr   )r   r   r   r   r   r   r   r   s     r.   r   z0SampleJointNumpy._sample_numpy.<locals>.<lambda>   s<    *:P:Pdff+Z%>%F%F%Ht ;Q ;U r/   r   c                 \    t        | j                        j                         j                  S r(   r   r   s    r.   r   z0SampleJointNumpy._sample_numpy.<locals>.<lambda>   r   r/   c                 \    t        | j                        j                         j                  S r(   r   r   s    r.   r   z0SampleJointNumpy._sample_numpy.<locals>.<lambda>   r   r/   c                 \    t        | j                        j                         j                  S r(   r   r   s    r.   r   z0SampleJointNumpy._sample_numpy.<locals>.<lambda>   r   r/   )
r   r)   r   r   r   r   r   r   r   r   )
r+   r-   r   r   r   numpy_rv_mapr   r   r   r   s
            @r.   r   zSampleJointNumpy._sample_numpy   s     	<:dC011t1<JJ/@-J(U
 /a,_'V
 !%%'	>>"")37,t~~667d4jIt&Kl4>>3J3J&KD&QQRRr/   r(   )r   r   r   r   r*   r   r   rm   r/   r.   r   r      s     A3 S Sr/   r   c                   (    e Zd ZdZddZed        Zy)SampleJointPymcz6Returns the sample from pymc of the given distributionNc                 (    | j                  |||      S r(   )_sample_pymcr   s       r.   r*   zSampleJointPymc.__new__   s    dD11r/   c           	      J  	 	 ddl 		fd	fd	fdd}d d d	 d}|j                         }|j                  j
                  |vryddl}|j                  d
      j                  |j                         	j                         5   ||j                  j
                     |       	j                  t        |      dd|dd      dd d   }ddd       j                  | ||j                  j
                     |      z         S # t        $ r ddl	Y w xY w# 1 sw Y   NxY w)zSample from PyMC.r   Nc                     j                  dt        | j                  t              j	                         t        | j
                  t              d| j                  j                  d   f      S )NXr<   r   )r   r   r   )MvNormalr   r   r   r   r   r   r-   pymcs    r.   r   z.SampleJointPymc._sample_pymc.<locals>.<lambda>   sT    cl477E&B&J&J&L U3Atww}}Q?O;P  R r/   c                 v    j                  dt        | j                  t              j	                               S )Nr   )a)	Dirichletr   r   r   r   r   s    r.   r   z.SampleJointPymc._sample_pymc.<locals>.<lambda>   s,    sjU&C&K&K&MN r/   c           	          j                  dt        | j                        t        | j                  t
              j                         dt        | j                        f      S )Nr   r<   )r   r   r   )Multinomialr   r   r   r   r   r   rC   r   s    r.   r   z.SampleJointPymc._sample_pymc.<locals>.<lambda>   sN      DFFTVVU+335aTVV=M ! O r/   r   c                 \    t        | j                        j                         j                  S r(   r   r   s    r.   r   z.SampleJointPymc._sample_pymc.<locals>.<lambda>   r   r/   c                 \    t        | j                        j                         j                  S r(   r   r   s    r.   r   z.SampleJointPymc._sample_pymc.<locals>.<lambda>   r   r/   c                 \    t        | j                        j                         j                  S r(   r   r   s    r.   r   z.SampleJointPymc._sample_pymc.<locals>.<lambda>   r   r/   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   zSampleJointPymc._sample_pymc   s:   	!/R-O(O	
 /a,_'V
  $$&	>>"")3'"++GMM:ZZ\ 	i0K//06kkT
1%]ax}  [`k  a  bc  d  eh  iG	i t&Kl4>>3J3J&KD&QQRR;  	! 	!4	i 	is   D ADDDD"r(   )r   r   r   r   r*   r   r   rm   r/   r.   r   r      s     @2 "S "Sr/   r   )r   r   r   r   c                   N    e Zd ZdZdZd Zed        Zed        Zd Z	d
dZ
d	 Zy)JointDistributionz
    Represented by the random variables part of the joint distribution.
    Contains methods for PDF, CDF, sampling, marginal densities, etc.
    rK   c                     t        t        t        |            }t        t	        |            D ]'  }t        ||   t               st        ||         ||<   ) t        j                  | g| S r(   )	listmapr	   rI   rC   r)   r   r   r*   )r+   r7   rJ   s      r.   r*   zJointDistribution.__new__  sa    C&'s4y! 	3A$q'4()$q'2Q	3 }}S(4((r/   c                 ,    t        | j                        S r(   )r   symbolsr3   s    r.   r1   zJointDistribution.domain%  s    T\\**r/   c                 4    | j                   j                  d   S r;   )densityr7   r3   s    r.   rK   zJointDistribution.pdf)  s    ||  ##r/   c           	         t        |t              st        |dt        |            |j	                         }| j
                  j                  j                  }| j                  t        d | j                  D                    }t        t        |            D ]l  }||   j                  r&t        |||   ||   j                  |||      f      }8||   j                   sHt#        |||   ||   j                  |||      f      }n S )Nz should be of type dict, got c              3   :   K   | ]  }|j                   d      ywr   Nr6   )rX   rJ   s     r.   rY   z(JointDistribution.cdf.<locals>.<genexpr>2  s     >AaffQi>s   )r)   r\   r[   typer   r1   r2   setsrK   r^   r   rI   rC   r`   r   infra   r   )r4   otherrO   rF   rr   rJ   r   s          r.   cdfzJointDistribution.cdf-  s    %&%eUVVjjl{{##xx>>>?s5z" 	$A1v##"4#a&$q'++#a&M*# $Q##dSVT!W[[#a&M%# $	$ r/   Nc                     d}||vrt        dt        |      z        t        |      st        d|z        t	        |   | ||      }||S t        d| j
                  j                  d|      )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 )rq   rV   r$   r[   _get_sample_class_jrvr   r   )r4   r   r   r   	librariessampss         r.   r   zJointDistribution.sample<  s     8	)#%&N*-g,'7 8 8W%2W<==%g.tTEL!>>**G5 	r/   c                       | j                   | S r(   r   r4   r7   s     r.   __call__zJointDistribution.__call__O      txxr/   r   )r   r   r   r   	_argnamesr*   r   r1   rK   r  r   r  rm   r/   r.   r   r     sJ    
 I) + + $ $&r/   r   c                       e Zd ZdZd Zy)r?   zg
    Representation of random symbols with joint probability distributions
    to allow indexing."
    c                     t        | j                  t              r[| j                  j                  |k  dk(  r3t	        d| j
                  d| j                  j                  dz
  d      t        | |      S y )NTzIndex keys for z can only up to r<   .)r)   rM   r&   rG   r[   namer   )r4   keys     r.   __getitem__zJointRandomSymbol.__getitem__W  sf    dkk;/++s2t; YY ; ;a ?"A B B4%%	 0r/   N)r   r   r   r   r  rm   r/   r.   r?   r?   R  s    &r/   r?   c                   T    e Zd ZdZd Zd Zed        Zed        Zd Z	d Z
d Zd	 Zy
)MarginalDistributionz
    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                 Z   t        |      dk(  rt        |d         rt        |d         }t        d |D              st	        t        d            t        j                  d |D              }t        |t              st        t        |            dk(  r|S t        j                  | ||      S )Nr<   r   c              3   H   K   | ]  }t        |t        t        f        y wr(   )r)   r   r   rX   rP   s     r.   rY   z/MarginalDistribution.__new__.<locals>.<genexpr>l  s     Ir:b7L"9:Is    "zMarginal distribution can be
             intitialised only in terms of random variables or indexed random
             variablesc              3       K   | ]  }|  y wr(   rm   r  s     r.   rY   z/MarginalDistribution.__new__.<locals>.<genexpr>p  s     .BR.s   )rC   r"   r^   allr[   r#   r   fromiterr)   r   r   r   r*   )r+   r-   rO   s      r.   r*   zMarginalDistribution.__new__i  s    s8q=Xc!f-A-CISIIZ )    nn.#..$ 12s>$;O7PTU7UK}}S$,,r/   c                      y r(   rm   r3   s    r.   checkzMarginalDistribution.checku  s    r/   c                     | j                   d   D cg c]  }t        |t              s| }}t        |D cg c]  }|j                  j
                   c} S c c}w c c}w r;   )r7   r)   r   r
   rM   r2   )r4   rJ   rO   rP   s       r.   r2   zMarginalDistribution.setx  sM    ))A,FQ*Q*EqFFC8bBIIMM899 G8s   AAAc                 r    | j                   d   }|D ch c]  }|j                  j                   c}S c c}w r;   )r7   rM   r8   rN   s      r.   r   zMarginalDistribution.symbols}  s-    iil+./R		  ///s   4c                    | j                   d   | j                   d   }}t        |      D cg c]	  }||vs| }}t        |t              rRt	        |j
                  j                         }t        dd      t        fd|D              }|j                  |      }nt        d |D              } t        || j                  ||             S c c}w )Nr   r<   xT)realc              3   6   K   | ]  }t        |        y wr(   r   )rX   rJ   r!  s     r.   rY   z+MarginalDistribution.pdf.<locals>.<genexpr>  s     61A6s   c              3      K   | ]9  }t        |t              r|j                  j                  n|j                  d     ; ywr   )r)   r   rM   r8   r7   r  s     r.   rY   z+MarginalDistribution.pdf.<locals>.<genexpr>  s2     h^`ZL-I))rwwWXzYhs   ?A)r7   r   r)   r   rC   r1   r   r^   rK   r   compute_pdf)r4   r!  rr   rO   rJ   marginalise_outrd   ru   s    `      r.   rK   zMarginalDistribution.pdf  s    IIaL$))A,c&4T&:Kasl1KKd-.(()Ec%A666D88D>DhdghhDDvdD,,T?CDaHH Ls
   	CCc                     |D ]?  }d}t        |t              r|j                  j                  }| j	                  ||z  |      }A |S r;   )r)   r   rM   rK   r&  )r4   rr   rO   rP   lpdfs        r.   r%  z MarginalDistribution.compute_pdf  sJ     	7BD"l+yy}}''T	26D		7
 r/   c                    ddl m} t        |t              r|j                  j
                  }nQt        |t              rA|j                  j                  |j                  j                  |j                  d               }|j                  ||j                  j                  i      }|j                  j                  r$t        ||j                  j                  f      }|S |j                  j                  rht        j                   t        j"                  t        j$                  fv r|j&                  |j(                  f} |||j                  j                  |f      }|S )Nr   )r   r<   )sympy.concrete.summationsr   r)   r   rM   r2   r   ro   rS   r7   rb   r8   r`   r   ra   r   IntegersNaturals	Naturals0r  sup)r4   rr   rP   r   doms        r.   r&  z$MarginalDistribution.marginalise_out  s    1b,'))--CG$''**		**2771:68C}}b"))"2"23499"" D299#3#3S"9:D  YY""qzz1::q{{;;ww(tbii..45Dr/   c                       | j                   | S r(   r   r
  s     r.   r  zMarginalDistribution.__call__  r  r/   N)r   r   r   r   r*   r  r   r2   r   rK   r%  r&  r  rm   r/   r.   r  r  `  sN    
- : : 0 0
I&r/   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   r*  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   r?   r  rm   r/   r.   <module>rC     s   	  " & " - & & ( + 4 ' 9 D D P L= = = / + (n$- n$b%S %SN%S %SN)S )SZ 	 :n :x
& 
&J< Jr/   