#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") np.savetxt("GP.csv", Metro.recordGP, delimiter=",") np.savetct("ParameterRecord.csv",Metro.record,delimiter=",") Metro.showplot(0) Metro.printall(0) Metro.showplot(1) Metro.printall(1) Metro.showplot(2)
# Hyper parameters to test length_scales = np.arange(1, 50, 5)[1:] sigma_vals = [0, 0.01, .1, .3] # Noise values to test # Progressively take more samples from data set to train the model for step in range(0, sampling_steps): std_dev = [] predict = [] # Using self written library # do 5 folds cross validation to find optimal hyper parameters l and sigma l_opt, sigma_opt = GP.cross_validate(training_data, l_val=length_scales, sigma_val=sigma_vals) # training model with optimal hyper parameters gp = GP.GaussianProcess(train_data=training_data, return_std=True) predict, std_dev = gp.predict(test_data=test_data[:, :-1], l=l_opt, sigma=sigma_opt) if step == sampling_steps - 1: continue #get more training samples ps = progressive_sampling(training_data, test_data, std_dev, ti) # new training and test data training_data, test_data, std_dev = ps.train, ps.test, ps.std ps = None gp = None Ptest = test_data[:, -1:].reshape(-1, 1)