K iUddlmZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z mZddlmZmZmZmZmZmZmZmZmZdd lmZmZmZmZmZmZm Z dd l!m"Z"Gd d eZ#d Z$GddZ%GddZ&GddZ'e%e'e'e&dZ(Gdde eZ)Gdde)Z*dZ+Gdde)Z,dZ-Gdde)Z.dZ/Gd d!e)Z0d"Z1y#)$)prod)Basic)pi)S)exp) multigamma)sympify_sympify) ImmutableMatrixInverseTrace Determinant MatrixSymbol MatrixBase Transpose MatrixSet matrix2numpy) _value_checkRandomMatrixSymbolNamedArgsMixinPSpace_symbol_converter MatrixDomain Distribution) import_modulecxeZdZdZdZedZedZedZedZ edZ dZ d d Z y ) MatrixPSpacezD Represents probability space for Matrix Distributions. ct|}t|t|}}|jr |js tdt j |||||S)NzDimensions should be integers)rr is_integer ValueErrorr__new__)clssym distributiondim_ndim_ms f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/stats/matrix_distributions.pyr!zMatrixPSpace.__new__sQ$u  U%5%5<= =}}S#|UEBBc |jdS)Nargsselfs r'zMatrixPSpace. s1r(c |jdSNrr+r-s r'r/zMatrixPSpace.!s499Q<r(cVt|j|jjSN)rsymbolr$setr-s r'domainzMatrixPSpace.domain#sDKK):):)>)>??r(cft|j|jd|jd|S)N)rr4r,r-s r'valuezMatrixPSpace.value's'!$++tyy|TYYq\4PPr(c|jhSr3)r:r-s r'valueszMatrixPSpace.values+s |r(c|jt}t|dkDst|ts t d|j j |S)Nr*ztCurrently, no algorithm has been implemented to handle general expressions containing multiple matrix distributions.)atomsrlen isinstanceNotImplementedErrorr$pdf)r.exprr,rmss r'compute_densityzMatrixPSpace.compute_density/sQjj+, s8a< 41C D%'56 6  $$T**r(NcV|j|jj|||iS)zu Internal sample method Returns dictionary mapping RandomMatrixSymbol to realization value. )libraryseed)r:r$sample)r.sizerGrHs r'rIzMatrixPSpace.sample7s,  D--44T7QU4VWWr(scipyN) __name__ __module__ __qualname____doc__r!propertyr$r4r6r:r<rErIrLr(r'rrsnC56L / 0F @@QQ+Xr(rcttt|}||}|j||j}t |||d|d}|j S)Nrr*)listmapr check dimensionrr:)r4r"r,distdimpspaces r'rvr[@sU GT" #D :DDJJ ..C &$AA 7F <<r(c(eZdZdZddZedZy)SampleMatrixScipyz7Returns the sample from scipy of the given distributionNc(|j|||Sr3) _sample_scipyr"rXrJrHs r'r!zSampleMatrixScipy.__new__K  tT22r(c ddlm ddl} fd fdd}ddd}|j}|jj |vry|t |tr|jj| }n|}||jj |t||} | j|||jj |zS) zSample from SciPy.r)statsNcjjt|jt |j t |S)N)dfscalerJ)wishartrvsintnr scale_matrixfloatrXrJ rand_state scipy_statss r'r/z1SampleMatrixScipy._sample_scipy..Us>+BUBUBYBYtvv;l43D3De&LSWCZCYr(cjjt|jtt|j tt|j t||S)N)meanrowcovcolcovrJ random_state) matrix_normalrhrlocation_matrixrlscale_matrix_1scale_matrix_2rms r'r/z1SampleMatrixScipy._sample_scipy..WsV{G`G`GdGd!$"6"6>#D$7$7?#D$7$7?dYcHeHer(WishartDistributionMatrixNormalDistributionc.|jjSr3rkshaperXs r'r/z1SampleMatrixScipy._sample_scipy..^0A0A0G0Gr(c.|jjSr3rvr~rs r'r/z1SampleMatrixScipy._sample_scipy.._d6J6J6P6Pr(rH) rMrcnumpykeys __class__rNr@rirandom default_rngrreshape) r"rXrJrHr scipy_rv_map sample_shape dist_listrnsampros @r'r_zSampleMatrixScipy._sample_scipyNs /$Y)e $H)P !%%' >> " ") 3 <:dC011t1> " ") 3 <:dC011t1.sYT5F5Fs 4 4e<#D$7$7?#D$7$7?**00 6G62r(cjdt|jt|jt S)Nr)nur)WishartBartlettrirjrrkrlrs r'r/z/SampleMatrixPymc._sample_pymc..s50D0DStvv;,t/@/@%"H1E1Jr()r{rzc.|jjSr3r}rs r'r/z/SampleMatrixPymc._sample_pymc..rr(c.|jjSr3rrs r'r/z/SampleMatrixPymc._sample_pymc..rr(ryrr*F)drawschains progressbar random_seedreturn_inferencedatacompute_convergence_checksr)r ImportErrorpymc3rrrNlogging getLoggersetLevelERRORModelrIrr) r"rXrJrH pymc_rv_maprrrsampsrs @r'rzSampleMatrixPymc._sample_pymcs# ! )2 $J  $H)P  $$& >> " ") 3&!**7==9 ZZ\ d 0K// 0 6KKd4j[_v{Y^K_`cdE d}}T$IL1H1H$I$$OOPP5 !  !. d dsC<AD< D  D Dr3)rNrOrPrQr!rrrLr(r'rrs @2QQr(r)rMrrrc4eZdZdZdZedZdZddZy)MatrixDistributionz1 Abstract class for Matrix Distribution. c|Dcgc](}t|tr t|n t|*}}t j |g|Scc}wr3)r@rTr r rr!)r"r,args r'r!zMatrixDistribution.__new__sR.24'*)33(=$c]#44}}S(4((4s-A cyr3rLr+s r'rVzMatrixDistribution.checks r(cZt|tr t|}|j|Sr3)r@rTr rB)r.rCs r'__call__zMatrixDistribution.__call__s$ dD !"4(Dxx~r(Ncgd}||vrtdt|zt|std|zt ||||}||Std|j j d|)zo Internal sample method Returns dictionary mapping RandomSymbol to realization value. )rMrrrz&Sampling from %s is not supported yet.zFailed to import %sz Sampling for z# is not currently implemented from )rAstrrr _get_sample_class_matrixrvrrN)r.rJrGrH librariesrs r'rIzMatrixDistribution.samples8 ) #%&N*-g,'78 8W%2W<= =*73D$E  L!>>**G5 r(rK) rNrOrPrQr! staticmethodrVrrIrLr(r'rrs*)    r(rcFeZdZdZedZedZedZdZ y)MatrixGammaDistributionalphabetarkct|tst|jdt|jdt|j dt|j dy)N+The shape matrix must be positive definite.Should be square matrix#Shape parameter should be positive.z#Scale parameter should be positive.r@rris_positive_definite is_square is_positivers r'rVzMatrixGammaDistribution.checksX, 5 ::=4 5\++. U&&(MNT%%'LMr(cj|jjd}t||tjSr1rkr~rrRealsr.ks r'r5zMatrixGammaDistribution.set+    # #A &Aqww''r(c.|jjSr3r}r-s r'rWz!MatrixGammaDistribution.dimension  &&&r(c|j|j|j}}}|jd}t |t r t |}t |ttfstdt|zt| |z|z }tt||||zzt||zz }t|| z}t||t!|dzdz z z} ||z| zS)Nr4%s should be an isinstance of Matrix or MatrixSymbolr*r8)rrrkr~r@rTr rrr rr rr rrr) r.xrrrkp sigma_inv_xterm1term2term3s r'rBzMatrixGammaDistribution.pdfs$(JJ 4;L;L\t   q ! a "A!j,78&(+A/0 0 --a/$6 E+&'$5/Zq=Q)QR\*uf5Q51QU8A:#56u}u$$r(N rNrOrP _argnamesrrVrRr5rWrBrLr(r'rrsH1INN(('' %r(rc`t|tr t|}t|t|||fS)a  Creates a random variable with Matrix Gamma Distribution. The density of the said distribution can be found at [1]. Parameters ========== alpha: Positive Real number Shape Parameter beta: Positive Real number Scale Parameter scale_matrix: Positive definite real square matrix Scale Matrix Returns ======= RandomSymbol Examples ======== >>> from sympy.stats import density, MatrixGamma >>> from sympy import MatrixSymbol, symbols >>> a, b = symbols('a b', positive=True) >>> M = MatrixGamma('M', a, b, [[2, 1], [1, 2]]) >>> X = MatrixSymbol('X', 2, 2) >>> density(M)(X).doit() exp(Trace(Matrix([ [-2/3, 1/3], [ 1/3, -2/3]])*X)/b)*Determinant(X)**(a - 3/2)/(3**a*sqrt(pi)*b**(2*a)*gamma(a)*gamma(a - 1/2)) >>> density(M)([[1, 0], [0, 1]]).doit() exp(-4/(3*b))/(3**a*sqrt(pi)*b**(2*a)*gamma(a)*gamma(a - 1/2)) References ========== .. [1] https://en.wikipedia.org/wiki/Matrix_gamma_distribution )r@rTr r[r)r4rrrks r' MatrixGammars0V,%&|4 f-t\/J KKr(cFeZdZdZedZedZedZdZ y)rzrjrkct|tst|jdt|jdt|j dy)Nrrrrrs r'rVzWishartDistribution.checkLsE, 5 ::=4 5\++. Q]]$IJr(cj|jjd}t||tjSr1rrs r'r5zWishartDistribution.setUrr(c.|jjSr3r}r-s r'rWzWishartDistribution.dimensionZrr(c |j|j}}|jd}t|tr t |}t|t tfstdt|zt| |ztdz }tt|d||ztdz zt|tdz |zz }t|| tdz z}t|t||z dz dz z}||z|zS)Nrrr8r*)rjrkr~r@rTr rrr rr rrr rr) r.rrjrkrrrrrs r'rBzWishartDistribution.pdf^s&&$"3"3<   q ! a "A!j,78&(+A/0 0 --a/!A$6 E+&'!ac!A$h-:a!fa;P)PQ\*qb1g6Q1QUQY<>2u}u$$r(NrrLr(r'rzrzHsH%IKK(('' %r(rzc^t|tr t|}t|t||fS)a Creates a random variable with Wishart Distribution. The density of the said distribution can be found at [1]. Parameters ========== n: Positive Real number Represents degrees of freedom scale_matrix: Positive definite real square matrix Scale Matrix Returns ======= RandomSymbol Examples ======== >>> from sympy.stats import density, Wishart >>> from sympy import MatrixSymbol, symbols >>> n = symbols('n', positive=True) >>> W = Wishart('W', n, [[2, 1], [1, 2]]) >>> X = MatrixSymbol('X', 2, 2) >>> density(W)(X).doit() exp(Trace(Matrix([ [-1/3, 1/6], [ 1/6, -1/3]])*X))*Determinant(X)**(n/2 - 3/2)/(2**n*3**(n/2)*sqrt(pi)*gamma(n/2)*gamma(n/2 - 1/2)) >>> density(W)([[1, 0], [0, 1]]).doit() exp(-2/3)/(2**n*3**(n/2)*sqrt(pi)*gamma(n/2)*gamma(n/2 - 1/2)) References ========== .. [1] https://en.wikipedia.org/wiki/Wishart_distribution )r@rTr r[rz)r4rjrks r'Wishartrls.P,%&|4 f)A|+< ==r(cFeZdZdZedZedZedZdZ y)r{)rvrwrxc t|tst|jdt|tst|jdt|jdt|jd|j d}|j d}t|j d|k(dt |dt |t|j d|k(dt |dt |y) Nr)Scale matrix 1 should be be square matrix)Scale matrix 2 should be be square matrixrr*"Scale matrix 1 should be of shape  x "Scale matrix 2 should be of shape )r@rrrrr~r)rvrwrxrjrs r'rVzMatrixNormalDistribution.checks.,7 <<?4 5.,7 <<?4 5^--0 ^--0   ! !! $  ! !! $^))!,1!!fc!f4. /^))!,1!!fc!f4. /r(cj|jj\}}t||tjSr3rvr~rrrr.rjrs r'r5zMatrixNormalDistribution.set*##))1Aqww''r(c.|jjSr3rr-s r'rWz"MatrixNormalDistribution.dimension##)))r(c>|j|j|j}}}|j\}}t |t r t |}t |ttfstdt|zt|t||z zt|z||z z}tt| tdz }dt zt||zdz zt#|t|dz zzt#|t|dz zz} || z S)Nrr8)rvrwrxr~r@rTr rrr rr rrr rrr) r.rMUVrjrrnumdens r'rBzMatrixNormalDistribution.pdfs&&(;(;T=P=Pa1ww1 a "A!j,78&(+A/0 0 9QU++GAJ6A>5<-!$%tq1vax ;q>AaDF#;;k!nqQRtTUv>VV3wr(NrrLr(r'r{r{sFGI//$((** r(r{ct|tr t|}t|tr t|}t|tr t|}|||f}t|t|S)a Creates a random variable with Matrix Normal Distribution. The density of the said distribution can be found at [1]. Parameters ========== location_matrix: Real ``n x p`` matrix Represents degrees of freedom scale_matrix_1: Positive definite matrix Scale Matrix of shape ``n x n`` scale_matrix_2: Positive definite matrix Scale Matrix of shape ``p x p`` Returns ======= RandomSymbol Examples ======== >>> from sympy import MatrixSymbol >>> from sympy.stats import density, MatrixNormal >>> M = MatrixNormal('M', [[1, 2]], [1], [[1, 0], [0, 1]]) >>> X = MatrixSymbol('X', 1, 2) >>> density(M)(X).doit() exp(-Trace((Matrix([ [-1], [-2]]) + X.T)*(Matrix([[-1, -2]]) + X))/2)/(2*pi) >>> density(M)([[3, 4]]).doit() exp(-4)/(2*pi) References ========== .. [1] https://en.wikipedia.org/wiki/Matrix_normal_distribution )r@rTr r[r{)r4rvrwrxr,s r'rrs]R/4()/:.$'(8.$'(8 ^^ KAQ)?)NyYZ]^Y^O_)_ _`FQJM"1$&& &r(NrrLr(r'rrsHMIUU&((** &r(rct|tr t|}t|tr t|}t|tr t|}||||f}t|t|S)a Creates a random variable with Matrix Gamma Distribution. The density of the said distribution can be found at [1]. Parameters ========== nu: Positive Real number degrees of freedom location_matrix: Positive definite real square matrix Location Matrix of shape ``n x p`` scale_matrix_1: Positive definite real square matrix Scale Matrix of shape ``p x p`` scale_matrix_2: Positive definite real square matrix Scale Matrix of shape ``n x n`` Returns ======= RandomSymbol Examples ======== >>> from sympy import MatrixSymbol,symbols >>> from sympy.stats import density, MatrixStudentT >>> v = symbols('v',positive=True) >>> M = MatrixStudentT('M', v, [[1, 2]], [[1, 0], [0, 1]], [1]) >>> X = MatrixSymbol('X', 1, 2) >>> density(M)(X) gamma(v/2 + 1)*Determinant((Matrix([[-1, -2]]) + X)*(Matrix([ [-1], [-2]]) + X.T) + Matrix([[1]]))**(-v/2 - 1)/(pi**1.0*gamma(v/2)*Determinant(Matrix([[1]]))**1.0*Determinant(Matrix([ [1, 0], [0, 1]]))**0.5) References ========== .. [1] https://en.wikipedia.org/wiki/Matrix_t-distribution )r@rTr r[r)r4rrvrwrxr,s r'MatrixStudentTr /s_X/4()/:.$'(8.$'(8  @D f0$ 77r(N)2mathrsympy.core.basicrsympy.core.numbersrsympy.core.singletonr&sympy.functions.elementary.exponentialr'sympy.functions.special.gamma_functionsrsympy.core.sympifyr r sympy.matricesr r r rrrrrrsympy.stats.rvrrrrrrrsympy.externalrrr[r]rrrrrrrzrr{rrr rLr(r'rs"!"6>0***KKK( )X6)XX#P#PLPP>&Q&QR    '~'`#%0#%J-Ld"%,"%H*>^+1+Z06j.&!3.&d38r(