def getModel(): D = pm.Dirichlet('1-Dirichlet', theta=[3,2,4]); #@UndefinedVariable C1 = pm.Categorical('2-Cat', D); #@UndefinedVariable C2 = pm.Categorical('10-Cat', D); #@UndefinedVariable C3 = pm.Categorical('11-Cat', D); #@UndefinedVariable W0_0 = pm.WishartCov('4-Wishart0_1', n=5, C=np.eye(2)); #@UndefinedVariable N0_1 = pm.MvNormalCov('5-Norm0_1', mu=[-20,-20], C=np.eye(2)); #@UndefinedVariable N0_2 = pm.MvNormalCov('6-Norm0_2', mu=[0,0], C=np.eye(2)); #@UndefinedVariable N0_3 = pm.MvNormalCov('7-Norm0_3', mu=[20,20], C=np.eye(2)); #@UndefinedVariable aMu = [N0_1.value, N0_2.value, N0_3.value]; fL1 = lambda n=C1: np.select([n==0, n==1, n==2], aMu); fL2 = lambda n=C2: np.select([n==0, n==1, n==2], aMu); fL3 = lambda n=C3: np.select([n==0, n==1, n==2], aMu); p_N1 = pm.Lambda('p_Norm1', fL1, doc='Pr[Norm|Cat]'); p_N2 = pm.Lambda('p_Norm2', fL2, doc='Pr[Norm|Cat]'); p_N3 = pm.Lambda('p_Norm3', fL3, doc='Pr[Norm|Cat]'); N = pm.MvNormalCov('3-Norm', mu=p_N1, C=W0_0); #@UndefinedVariable obsN1 = pm.MvNormalCov('8-Norm', mu=p_N2, C=W0_0, observed=True, value=[-20,-20]); #@UndefinedVariable @UnusedVariable obsN2 = pm.MvNormalCov('9-Norm', mu=p_N3, C=W0_0, observed=True, value=[20,20]); #@UndefinedVariable @UnusedVariable return pm.Model([D,C1,C2,C3,N,W0_0,N0_1,N0_2,N0_3,N,obsN1,obsN2]); if __name__ == '__main__': warnings.filterwarnings("ignore"); mcmc = pst.sample_MCMC(getModel(), 5000); aBins = [100]*11; aBins[1] = 3; aBins[9] = 3; aBins[10] = 3; aHidden = [4,5,6,7,10,11]; pst.plot_Samples(mcmc, aBins=aBins, aKDE=[], aRowCols=[3,1], aHidden=aHidden);
# @UndefinedVariable aC = [pm.Categorical('Cat' + str(i), Dir) for i in range(nPts)] # @UndefinedVariable aL = [ pm.Lambda('p_Norm' + str(i), lambda k=aC[i], aNcl=aNc: aNcl[int(k)]) for i in range(nPts) ] # @UndefinedVariable #Points aN = [ pm.Normal('NormX' + str(i), mu=aL[i], tau=1, observed=True, value=aD[i]) for i in range(nPts - 1) ] # @UndefinedVariable Nz = pm.Normal('NormZ', mu=aL[-1], tau=1) # @UndefinedVariable return np.concatenate([[Nz, Dir], aUh, aNc, aC, aN]) if __name__ == '__main__': warnings.filterwarnings("ignore") aNodes = run_Categorical_Normal() model = pst.get_Model(aNodes) mcmc = pst.sample_MCMC(model, 1000) pst.plot_Samples(mcmc, nBins=500, nCols=3) # graph = pm.graph.graph(model); # graph.write_pdf("./graph7.pdf");
# Bet = pm.Beta('Beta', alpha=1, beta=1); # @UndefinedVariable # B = pm.Bernoulli('Bern', Bet); # @UndefinedVariable # B1 = pm.Bernoulli('Bern1', Bet); # @UndefinedVariable # B2 = pm.Bernoulli('Bern2', Bet); # @UndefinedVariable # N = pm.Normal('Norm', mu=U, tau=1)#, observed=True, value=10); # @UndefinedVariable # N1 = pm.Normal('Norm1', mu=U, tau=1)#, observed=True, value=-10); # @UndefinedVariable # p_N = pm.Lambda('p_Norm', lambda k=int(B): [N, N1][k]); # p_N1 = pm.Lambda('p_Norm1', lambda k=int(B1): [N, N1][k]); # p_N2 = pm.Lambda('p_Norm2', lambda k=int(B2): [N, N1][k]); # N2 = pm.Normal('Norm2', mu=p_N, tau=1, observed=True, value=2); # @UndefinedVariable # N3 = pm.Normal('Norm3', mu=p_N1, tau=1, observed=True, value=-2); # @UndefinedVariable # N4 = pm.Normal('Norm4', mu=p_N2, tau=1); # @UndefinedVariable # return [N4,U,Bet,B,B1,B2,N2,N3,N,N1]#,B1,B2]; def run_Categorical_Normal(): C = pm.Categorical('Cat', [0.2, 0.4, 0.1, 0.3]); # @UndefinedVariable p_N = pm.Lambda('p_Norm', lambda node=C: [-5, 0, 5, 10][node]); N = pm.Normal('Norm', mu=p_N, tau=1); # @UndefinedVariable return [C,N]; if __name__ == '__main__': warnings.filterwarnings("ignore"); # aNodes = run_Categorical_Normal(); aNodes = run_Bernoulli_Normal(); model = pst.get_Model(aNodes); mcmc = pst.sample_MCMC(model, 50000); pst.plot_Samples(mcmc, nBins=500); # graph = pm.graph.graph(model); # graph.write_pdf("./graph4.pdf");