Esempio n. 1
0
def kernelFunctonPlotRebuild(DistanceMatrix,recordGP,meanParameter,Simulationmethod,simulationParameter,InitialGP=None):
    plt.clf()
    iterNa=recordGP.shape[0]
    vectorDistance=LowerTriangularToVector(DistanceMatrix)
    indices=np.argsort(vectorDistance)
    BetaMatrix=cr.BetaMatrix(DistanceMatrix,meanParameter,"gradient")
    BetaVectorBaseline=LowerTriangularToVector(BetaMatrix)

    BetaMatrixSimulation=cr.BetaMatrix(DistanceMatrix,np.delete(simulationParameter,-1),Simulationmethod)
    BetaVectorSimulation=LowerTriangularToVector(BetaMatrixSimulation)
    plt.plot(vectorDistance[indices],BetaVectorSimulation[indices],'k',color="green")
    plt.plot(vectorDistance[indices],BetaVectorBaseline[indices],'k',color="purple")
    '''
    maxGP=recordGP.max(0)
    BetaVector=np.exp(np.log(BetaVectorBaseline)+maxGP)
    plt.plot(vectorDistance[indices],BetaVector[indices],'k')
    minGP=recordGP.min(0)
    BetaVector=np.exp(np.log(BetaVectorBaseline)+minGP)
    plt.plot(vectorDistance[indices],BetaVector[indices],'k')
    '''
    medianGP=np.median(recordGP,0)
    BetaVector=np.exp(np.log(BetaVectorBaseline)+medianGP)
    plt.plot(vectorDistance[indices],BetaVector[indices],'k')
    upper95GP=np.percentile(recordGP,95,0)
    BetaVector=np.exp(np.log(BetaVectorBaseline)+upper95GP)
    plt.plot(vectorDistance[indices],BetaVector[indices],'k',color="red")
    lower95GP=np.percentile(recordGP,5,0)
    BetaVector=np.exp(np.log(BetaVectorBaseline)+lower95GP)
    plt.plot(vectorDistance[indices],BetaVector[indices],'k',color="blue")
    if InitialGP!=None:
        BetaVector=np.exp(np.log(BetaVectorBaseline)+InitialGP)
        plt.plot(vectorDistance[indices],BetaVector[indices],'k',color="brown")
    plt.show()
Esempio n. 2
0
 def __init__(self,
              num_people,
              parameter,
              history=False,
              processHistory=False,
              Distance="gradient",
              method="uniform",
              testProbabilityMode=False):
     """
       Initialize the representation of the state vector as a list of integers, 
      [1, 0, 0, ...]
      And simulate the locations data for each object as np array([num_people,2])
     """
     self.state = np.zeros((num_people))
     self.state[0] = 1
     self.num_people = num_people
     self.parameter = parameter
     #self.beta0=parameter[0] #baseline beta
     #self.phi=parameter[1]     #scale parameter
     self.gamma = parameter[-1]  #remove rate
     self.Distance = Distance
     self.method = method
     #change start with here
     #I need add the geo-information into state
     self.geo = cr.geodata(num_people,
                           method,
                           xbound=100.0,
                           ybound=100.0,
                           history=history)
     #cr.plotcoor(self.geo)
     self.DistanceMatrix = cr.DistanceMatrix(self.geo)
     self.BetaMatrix = cr.BetaMatrix(self.DistanceMatrix,
                                     np.delete(parameter, -1), Distance)
     #gp.BetaMatrixPlot(self.DistanceMatrix,self.BetaMatrix)
     self.mainProcess(processHistory, testProbabilityMode)
Esempio n. 3
0
 def NonParametricGPLikelihood(self,GP,gamma):
     '''
     Likelihood for non-parametric model
     '''
     BetaMatrix=cr.BetaMatrix(self.DistanceMatrix,None,GP,"GaussianProcess")
     probabilityMatrix=self.ProbabilityMatrix(BetaMatrix,gamma)
     change=self.change
     probabilityMatrix[change==0]=1-probabilityMatrix[change==0]
     loglikelihoodMatrix=np.log(probabilityMatrix)
     logLikelihood=loglikelihoodMatrix.sum(1).sum()
     return logLikelihood
Esempio n. 4
0
 def Likelihood(self,parameter,GP):
     gamma=parameter[-1]
     BetaMatrix=cr.BetaMatrix(self.DistanceMatrix,np.delete(parameter,-1),self.method)
     ####Add random  effect GP
     BetaMatrix=np.exp(np.log(BetaMatrix)+gp.LowerTriangularVectorToSymmetricMatrix(GP,BetaMatrix.shape[0]))
     probabilityMatrix=self.ProbabilityMatrix(BetaMatrix,gamma)
     change=self.change
     probabilityMatrix[change==0]=1-probabilityMatrix[change==0]
     loglikelihoodMatrix=np.log(probabilityMatrix)
     logLikelihood=loglikelihoodMatrix.sum(1).sum()
     return logLikelihood 
Esempio n. 5
0
def kernelFunctonPlot2(DistanceMatrix,recordGP,record,method):
    plt.clf()
    iterNa=recordGP.shape[0]
    vectorDistance=LowerTriangularToVector(DistanceMatrix)
    indices=np.argsort(vectorDistance)
    f  = open('GPPlotX.csv', 'w')
    f2= open('GPPlotY.csv', 'w')
    writer=csv.writer(f,delimiter=',')
    writer2=csv.writer(f2,delimiter=',')
    BetaMatrix=cr.BetaMatrix(DistanceMatrix,record,method)
    BetaVectorBaseline=LowerTriangularToVector(BetaMatrix)
    BetaVector=np.exp(np.log(BetaVectorBaseline)+recordGP[0])
    plt.plot(vectorDistance[indices],BetaVectorBaseline[indices],'k',color="green")
    for j in range(iterNa):
        BetaMatrix=cr.BetaMatrix(DistanceMatrix,record,method)
        BetaVector=np.exp(np.log(BetaVectorBaseline)+recordGP[j])
        plt.plot(vectorDistance[indices],BetaVector[indices],'k')
        writer.writerow(vectorDistance[indices])
        writer2.writerow(BetaVector[indices])
    f.close()
    f2.close()
    plt.show()
Esempio n. 6
0
def kernelFunctonPlot(DistanceMatrix,recordGP,record,method):
    plt.clf()
    iterNa=recordGP.shape[0]
    vectorDistance=LowerTriangularToVector(DistanceMatrix)
    indices=np.argsort(vectorDistance)
   
    for j in range(iterNa):
        BetaMatrix=cr.BetaMatrix(DistanceMatrix,record[j,:],method)
        BetaVectorBaseline=LowerTriangularToVector(BetaMatrix)
        BetaVector=np.exp(np.log(BetaVectorBaseline)+recordGP[j])
        plt.plot(vectorDistance[indices],BetaVectorBaseline[indices],'k',color="green")
        plt.plot(vectorDistance[indices],BetaVector[indices],'k')
    plt.show()
Esempio n. 7
0
#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)
Metro.printall(2)
Metro.plotcountour(0,1)
Metro.plotcountour(1,2)
Metro.plotcountour(0,2)
Esempio n. 8
0
#plt.style.use('ggplot')
population = 100
model1 = gc2.heteregeneousModel(population, [0.4, 10, 0.3], False, False,
                                "gradient", "uniform", False)
#model1=gc2.heteregeneousModel(50,[5,0.2,1,0.3],True,True,"powerlaw","uniform",False)
#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)
BetaMatrix = model1.BetaMatrix
BetaMatrix3 = cr.BetaMatrix(model1.DistanceMatrix, [0.2, 7])
gp.BetaMatrixPlot(model1.DistanceMatrix, [
    BetaMatrix,
    np.exp(
        np.log(BetaMatrix) + gp.LowerTriangularVectorToSymmetricMatrix(
            InitialGP, BetaMatrix.shape[0])), BetaMatrix3
], 3)
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)
Metro.showplot(0)
Metro.printall(0)
Metro.showplot(1)
Metro.printall(1)