K i[>dZddlmZddlmZddlmZddlmZddl m Z m Z ddl m Z ddlmZdd lmZdd lmZdd lmZmZdd lmZdd lmZmZddlmZmZmZddl m!Z!m"Z"ddl#m$Z$m%Z%ddl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.ddl/m0Z0ddl1m2Z2ddl3m4Z4Gdde'Z5GddZ6GddZ7GddZ8e6e8e8e7dZ9Gdde)e(Z:Gd d!e+Z;Gd"d#e)Z"% %uu r/ct|jDcgc]}t|j|}}|j|Scc}wr()rangerGr r8r=)r4ir,s r.pdfzJointPSpace.pdfJsD05d6J6J0KL1wt{{A&LL t  #&&MsAct|j}|s*t|j|jjSt |Dcgc]}|j jc}Scc}wr()rr=r r8r2rpspacer1r4rvsrvs r.r1zJointPSpace.domainOsUT../ T->->-B-BC C#>Bryy//>??>s A,c4|jj|Sr()r2r7)r4indexs r.component_domainzJointPSpace.component_domainVsxx}}U##r/c\j}|jtr tdt |Dcgc]}t j |}}|Dcgc]}tt|}}tt||}tfd|D}|Dcgc] }||vs|g }}d} t |D]R}||vs|| jjjj|t|| || <| dz } Tjjr$t!|t#j|g|} n9jj$r#t!|t'j|g|}  j)|Scc}wcc}wcc}w)Nz_Marginal distributions cannot be computed for symbolic dimensions. It is a work under progress.c 3nK|],}tttj|.ywr()rstrr r8.0rJr4s r. z4JointPSpace.marginal_distribution..as&JQF3wt{{A678Js25rr<)rGatomsr ValueErrorrIr r8rVdictziptupleappendr=r2r7 is_Continuousrr is_Discreterxreplace) r4indicescountrJorigall_syms replace_dictr,rDrRfs ` r.marginal_distributionz!JointPSpace.marginal_distributionYs$$ ;;v XY Y16u>A Q'>>,01qF3q6N11C$/0 J'JJ (91ASL1$99u Au $$T%6%6%:%:%?%?%BC %fUm 4u        * *sI&7d&7&7&BLVLMA    * *sI&7d&7&7&BLVLMAzz,''?1:sFF$( F)2F)Nc FtfdtjD}xs|tfd|Ds|S|jz}D]~}t |t r>|j|t t|j|jdi}Qt |tsb|j||ji}jt|vrtt!dtfd|D}t#|g|S)Nc3<K|]}j|ywr()r@rWs r.rYz2JointPSpace.compute_expectation..psHqTZZ]Hsc3&K|]}|v ywr()rXrJrOs r.rYz2JointPSpace.compute_expectation..rs*18*sr<zq Expectations of expression with unindexed joint random symbols cannot be calculated yet.c3K|]`}tt|j|jdjj j|jdfbyw)r<N)r rVbaser7r=r2)rXrPr4s r.rYz2JointPSpace.compute_expectation..~sYD8: BGG RWWQZ8    ! ! & &rwwqz 24DsA&A))r^rIrGanyrKr)r rbrVror7rr8r@rNotImplementedErrorr#r)r4exprrOevaluatekwargssymsrPrDs` ` r.compute_expectationzJointPSpace.compute_expectationosHE$2F2F,GHHkT*T**KDHH} 6B"g&}}b'#bgg, *K%LMB -}}b"))_5  6 ::- -%j2)'*+ +D>BDD&v&&r/ctr(rqr4 conditions r.wherezJointPSpace.where !##r/ctr(rx)r4rrs r.compute_densityzJointPSpace.compute_densityr|r/cjt|j||jj|||iS)zo Internal sample method Returns dictionary mapping RandomSymbol to realization value. )libraryseed)rr8r=sample)r4sizerrs r.rzJointPSpace.samples< T[[$/1B1B1I1I$#$2J201 1r/ctr(rxrys r. probabilityzJointPSpace.probabilityr|r/)NFrmscipyN)__name__ __module__ __qualname____doc__r*propertyr2r8r=r@rGrKr1rSrirvr{r~rrrmr/r.r&r&$s-44''@@ $(,'&$$1$r/r&c(eZdZdZddZedZy)SampleJointScipyz7Returns the sample from scipy of the given distributionNc(|j|||Sr() _sample_scipyr+r-rrs r.r*zSampleJointScipy.__new__  tT22r/c ddl}|t|tr|jj | n| ddlm  fd fd fdd}d d d d}|j}|jj|vry||jj||}|j|||jj|zS) zSample from SciPy.rNr)statscjjt|jj t|j |S)N)meancovr random_state)multivariate_normalrOrmuflattensigmar-r rand_state scipy_statss r.z0SampleJointScipy._sample_scipy..sFA`A`AdAd!$''*224 ,4jBeBRr/cjjt|jtj |S)N)alpharr) dirichletrOrrfloatrrs r.rz0SampleJointScipy._sample_scipy..s={?T?T?X?X U3;;=DWa@Y@cr/cjjt|jt |j t j|S)N)nprr) multinomialrOintrrrrrrs r.rz0SampleJointScipy._sample_scipy..sE+:Q:Q:U:Udff+DFFE!:!B!B!D4^h;V;jr/MultivariateNormalDistributionMultivariateBetaDistributionMultinomialDistributionc\t|jjjSr(rrrshaper-s r.rz0SampleJointScipy._sample_scipy..<;P;X;X;Z;`;`r/c\t|jjjSr(rrrrrs r.rz0SampleJointScipy._sample_scipy..DJJ9O9W9W9Y9_9_r/c\t|jjjSr(rrrrrs r.rz0SampleJointScipy._sample_scipy..Jtvv4F4N4N4P4V4Vr/) numpyr)rrandom default_rngrrkeys __class__rreshape) r+r-rrr scipy_rv_map sample_shape dist_listsamplesrrs @@r.rzSampleJointScipy._sample_scipys  <:dC011t1> " ") 37,t~~667dCt&Kl4>>3J3J&KD&QQRRr/r()rrrrr* classmethodrrmr/r.rrs A3SSr/rc(eZdZdZddZedZy)SampleJointNumpyz7Returns the sample from numpy of the given distributionNc(|j|||Sr() _sample_numpyrs r.r*zSampleJointNumpy.__new__rr/c ddl}|t|tr|jj | n| fd fd fdd}dd d d}|j }|j j|vry||j j|t|}|j|||j j|zS) zSample from NumPy.rNrcjt|jtj t|j t|S)N)rrr)rrrrrrr-rrs r.rz0SampleJointNumpy._sample_numpy..sBA_A_!$''5199; U3$B`B@r/cvjt|jtj |S)N)rr)rrrrrrs r.rz0SampleJointNumpy._sample_numpy..s4z?S?S U3;;=D@T@Jr/cjt|jt|jt j |S)N)rpvalsr)rrrrrrrrs r.rz0SampleJointNumpy._sample_numpy..s<*:P:Pdff+Z%>%F%F%Ht;Q;Ur/rc\t|jjjSr(rrs r.rz0SampleJointNumpy._sample_numpy..rr/c\t|jjjSr(rrs r.rz0SampleJointNumpy._sample_numpy..rr/c\t|jjjSr(rrs r.rz0SampleJointNumpy._sample_numpy..rr/) rr)rrrrrrrr) r+r-rrr numpy_rv_maprrrrs @r.rzSampleJointNumpy._sample_numpys  <:dC011t1> " ") 37,t~~667d4jIt&Kl4>>3J3J&KD&QQRRr/r()rrrrr*rrrmr/r.rrs A3SSr/rc(eZdZdZddZedZy)SampleJointPymcz6Returns the sample from pymc of the given distributionNc(|j|||Sr() _sample_pymcrs r.r*zSampleJointPymc.__new__sdD11r/c J ddl fd fd fdd}ddd d}|j}|jj |vryddl}|jd j|j j5||jj | jt|d d |d d ddd}dddj|||jj |zS#t$rddl YwxYw#1swYNxYw)zSample from PyMC.rNcjdt|jtj t|j td|jj dfS)NXr<r)rrr)MvNormalrrrrrrr-pymcs r.rz.SampleJointPymc._sample_pymc..sT cl477E&B&J&J&L U3Atww}}Q?O;PRr/cvjdt|jtj S)Nr)a) Dirichletrrrrrs r.rz.SampleJointPymc._sample_pymc..s,sjU&C&K&K&MNr/c jdt|jt|jt j dt|jfS)Nrr<)rrr) MultinomialrrrrrrrCrs r.rz.SampleJointPymc._sample_pymc..sN  DFF TVVU+335aTVV=M!Or/rc\t|jjjSr(rrs r.rz.SampleJointPymc._sample_pymc..rr/c\t|jjjSr(rrs r.rz.SampleJointPymc._sample_pymc..rr/c\t|jjjSr(rrs r.rz.SampleJointPymc._sample_pymc..rr/pymc3r<F)drawschains progressbar random_seedreturn_inferencedatacompute_convergence_checksr)r ImportErrorrrrrlogging getLoggersetLevelERRORModelrrr) r+r-rr pymc_rv_maprrrrrs @r.rzSampleJointPymc._sample_pymcs: ! /R-O(O  /a,_'V  $$& >> " ") 3'"++GMM: ZZ\ i 0K// 0 6kkT 1%]ax}[`kabcdehiG it&Kl4>>3J3J&KD&QQRR; !  !4 i isDAD DDD"r()rrrrr*rrrmr/r.rrs @2"S"Sr/r)rrrrcNeZdZdZdZdZedZedZdZ d dZ d Z y) JointDistributionz Represented by the random variables part of the joint distribution. Contains methods for PDF, CDF, sampling, marginal densities, etc. rKcttt|}tt |D]'}t ||tst ||||<)tj|g|Sr() listmapr rIrCr)rrr*)r+r7rJs r.r*zJointDistribution.__new__saC&'s4y! 3A$q'4()$q'2Q 3}}S(4((r/c,t|jSr()rsymbolsr3s r.r1zJointDistribution.domain%sT\\**r/c4|jjdSr;)densityr7r3s r.rKzJointDistribution.pdf)s||  ##r/c t|tst|dt||j }|j j j}|jtd|jD}tt|D]l}||jr&t|||||j|||f}8||j sHt#|||||j|||f}nS)Nz should be of type dict, got c3:K|]}|jdywrNr6)rXrJs r.rYz(JointDistribution.cdf..2s>AaffQi>s)r)r\r[typerr1r2setsrKr^rrIrCr`rinfrar)r4otherrOrFrrrJrs r.cdfzJointDistribution.cdf-s%&%eUV Vjjl{{##xx>>>?s5z" $A1v##"4#a&$q'++#a&M*#$Q##dSVT!W[[#a&M%#$  $r/Ncd}||vrtdt|zt|std|zt ||||}||Std|j j d|)z, A random realization from the distribution )rrrrz&Sampling from %s is not supported yet.zFailed to import %srz Sampling for z# is not currently implemented from )rqrVr$r[_get_sample_class_jrvrr)r4rrr librariessampss r.rzJointDistribution.sample<s8 ) #%&N*-g,'78 8W%2W<= =%g.tTE  L!>>**G5 r/c |j|Sr(rr4r7s r.__call__zJointDistribution.__call__Otxxr/r) rrrr _argnamesr*rr1rKrrr rmr/r.rrsJ I)++$$ &r/rceZdZdZdZy)r?zg Representation of random symbols with joint probability distributions to allow indexing." ct|jtr[|jj|kdk(r3t d|j d|jjdz dt ||Sy)NTzIndex keys for z can only up to r<.)r)rMr&rGr[namer )r4keys r. __getitem__zJointRandomSymbol.__getitem__Wsf dkk; / ++s2t; YY ; ;a ?"ABB4% % 0r/N)rrrrrrmr/r.r?r?Rs &r/r?cTeZdZdZdZdZedZedZdZ dZ dZ 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. cZt|dk(rt|drt|d}td|Dst t dt jd|D}t|tstt|dk(r|Stj|||S)Nr<rc3HK|]}t|ttfywr()r)r rrXrPs r.rYz/MarginalDistribution.__new__..lsIr:b7L"9:Is "zMarginal distribution can be intitialised only in terms of random variables or indexed random variablesc3 K|]}|ywr(rmrs r.rYz/MarginalDistribution.__new__..ps.BR.s ) rCr"r^allr[r#rfromiterr)rrrr*)r+r-rOs r.r*zMarginalDistribution.__new__is s8q=Xc!f-A-CISIIZ) nn.#..$ 12s>$;O7PTU7UK}}S$,,r/cyr(rmr3s r.checkzMarginalDistribution.checkus r/c|jdDcgc]}t|ts|}}t|Dcgc]}|jj c}Scc}wcc}wr;)r7r)rr rMr2)r4rJrOrPs r.r2zMarginalDistribution.setxsM))A,FQ*Q *EqFFC8bBIIMM899G8sAAAcr|jd}|Dchc]}|jjc}Scc}wr;)r7rMr8rNs r.rzMarginalDistribution.symbols}s-iil+./R   ///s4c|jd|jd}}t|Dcgc] }||vs| }}t|trRt |j j}t ddtfd|D}|j|}ntd|D}t||j||Scc}w)Nrr<xT)realc36K|]}t|ywr(r )rXrJr!s r.rYz+MarginalDistribution.pdf..s61A6sc3K|]9}t|tr|jjn|jd;ywr)r)rrMr8r7rs r.rYz+MarginalDistribution.pdf..s2h^`ZL-I))rwwWXzYhs?A) r7rr)rrCr1rr^rKr compute_pdf)r4r!rrrOrJmarginalise_outrdrus ` r.rKzMarginalDistribution.pdfsIIaL$))A,c&4T&:Kasl1KK d- . (()Ec%A666D88D>DhdghhDDvdD,,T?CDaHHLs CCc|D]?}d}t|tr|jj}|j ||z|}A|Sr;)r)rrMrKr&)r4rrrOrPlpdfs r.r%z MarginalDistribution.compute_pdfsJ 7BD"l+yy}}''T 26D  7  r/cddlm}t|tr|jj }nQt|t rA|jj|jj|jd}|j||jji}|jjr$t||jjf}|S|jjrhtj tj"tj$fvr|j&|j(f}|||jj|f}|S)Nr)rr<)sympy.concrete.summationsrr)rrMr2r rorSr7rbr8r`rrarIntegersNaturals Naturals0rsup)r4rrrPrdoms r.r&z$MarginalDistribution.marginalise_outs1 b, '))--C G $''** **2771:68C}}b"))"2"234 99 " "D299#3#3S"9:D  YY " "qzz1::q{{;;ww(tbii..45D r/c |j|Sr(rr s r.r zMarginalDistribution.__call__r r/N) rrrrr*rrr2rrKr%r&r rmr/r.rr`sN - ::00 I&r/rN)=rmathrsympy.core.basicrsympy.core.functionrsympy.core.singletonrsympy.core.symbolrrsympy.core.sympifyr sympy.sets.setsr sympy.tensor.indexedr sympy.concrete.productsr r*rrsympy.core.containersrsympy.integrals.integralsrrsympy.matricesrrrsympy.stats.crvrrsympy.stats.drvrrsympy.stats.rvrrrrrrr r!sympy.utilities.iterablesr"sympy.utilities.miscr#sympy.externalr$r&rrrrrr?rrmr/r.rCs "&"-&&(+4'9DDPL===/+(n$-n$b%S%SN%S%SN)S)SZ    : n:x & &J<Jr/