import numpy as np from scipy.stats import norm import matplotlib.pyplot as plt import Metropolis3 as mp3 import generator_temp_transprob as gc2 import likelihodPhi as lk2 import coordinate as cr from scipy.stats import beta from functools import partial import GP as gp population = 50 model1 = gc2.heteregeneousModel(population, [0.4, 0.1, 0.3], True, True) model1.Animate() estimate = lk2.Estimation(model1.record, model1.geo) InitialGP = np.zeros(population * (population - 1) / 2) #zero GP and no transform GPDoc = gp.GaussianProcess(estimate.DistanceMatrix, np.array((1, np.mean(estimate.DistanceMatrix)))) InitialGP = GPDoc.SampleForGP(np.zeros(population * (population - 1) / 2)) GPDoc = gp.GaussianProcess(estimate.DistanceMatrix, np.array((0.01, np.mean(estimate.DistanceMatrix)))) Metro = mp3.multiMetropolis(3000, None, [0.4, 0.1, 0.3], None, InitialGP, GPDoc, estimate.GaussianStandardPriorGP, "Change", "c", 1000) gp.kernelFunctonPlot(model1.DistanceMatrix, Metro.recordGP, Metro.record, "gradient") gp.kernelFunctonPlotRebuild(model1.DistanceMatrix, Metro.recordGP, Metro.record[0, :], "gradient", Metro.record[0, :], InitialGP)
import generator_temp_transprob as gc2 #import likelihood as lk import likelihodPhi as lk2 import coordinate as cr from scipy.stats import beta from functools import partial import GP as gp #plt.ion() #plt.style.use('ggplot') population=50 #model1=gc2.heteregeneousModel(population,[0.4,10,0.3],True,True,"gradient","uniform",False) model1=gc2.heteregeneousModel(population,[1000,20,1.5,0.3],True,True,"powerlaw","uniform",True) model1.Animate() #estimate=lk2.Estimation(model1.record,model1.geo,method="powerlaw") estimate=lk2.Estimation(model1.record,model1.geo,method="gradient") #Metro=mp3.multiMetropolis(1000,[estimate.GammaPosteriorBeta0,estimate.GammaPosteriorGamma,estimate.GammaPosteriorPhi],[0.1,0.1,5],[0.5,0.5,0.4]) #Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2)],[0.1,0.1,5],[0.5,0.5,0.4]) #Metro=mp3.multiMetropolis(1000,[estimate.GammaPosteriorBeta0,estimate.GammaPosteriorGamma],[0.1,0.1],[0.4,0.4]) #Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2),partial(estimate.GammaPriorGeneralPosterior,i=3)],[3,0.1,0.9,1],[0.5,0.5,0.4,0.4]) #InitialGP=np.zeros(population*(population-1)/2) InitialGP=gp.InitialGP(estimate.DistanceMatrix,np.array((1,1))) GPDoc=gp.GaussianProcess(estimate.DistanceMatrix,np.array((1,np.mean(estimate.DistanceMatrix)))) ################ InitialGP=GPDoc.SampleForGP(np.zeros(population*(population-1)/2)) BetaMatrix=model1.BetaMatrix BetaMatrix3=cr.BetaMatrix(model1.DistanceMatrix,[1,1]) gp.BetaMatrixPlot(model1.DistanceMatrix,[BetaMatrix,np.exp(np.log(BetaMatrix)+gp.LowerTriangularVectorToSymmetricMatrix(InitialGP,BetaMatrix.shape[0])),BetaMatrix3],3) test=estimate.GaussianPriorGP([0.1,0.9,1],GPDoc,InitialGP) Metro=mp3.multiMetropolis(1000,[partial(estimate.GammaPriorGeneralPosterior,i=0),partial(estimate.GammaPriorGeneralPosterior,i=1),partial(estimate.GammaPriorGeneralPosterior,i=2)],[0.1,0.9,1],[0.7,0.5,0.7],InitialGP,GPDoc,estimate.GaussianPriorGP,"Change")
def negativeLikelihood(parameter,GP): return -Likelihood.Likelihood(parameter,GP) Z=np.zeros((X.size,Y.size)) for j in range(X.size): for i in range(Y.size): Z[i,j]=np.exp(-negativeLikelihood(np.array((X[i],Y[j],0.3)),NONGP)) #Z[i,j]=np.exp(Likelihood.Likelihood([X[i],Y[j],0.3],NONGP)) X, Y = np.meshgrid(X, Y) #surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,linewidth=0, antialiased=False) #ax.plot_surface(X,Y,Z, rstride=1, cstride=1, color='b') #ax.set_zlim(0.1, 0.501) #ax.zaxis.set_major_locator(LinearLocator(10)) #ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) #fig.colorbar(surf, shrink=0.5, aspect=5) plt.contour(X,Y,Z) plt.show() if __name__ == "__main__": population=50 distanceMethod="gradient" if distanceMethod=="gradient" : parameter=[0.4,0.1,0.3] elif distanceMethod=="powerlaw": parameter=[200,0.1,1.5,0.3] model1=gc.heteregeneousModel(population,parameter,True,True,distanceMethod) #model1.Animate() Likelihood=lk.Estimation(model1.record,model1.geo) mleGamma(Likelihood) MLE(Likelihood) countourLikelihood(Likelihood)