コード例 #1
0
    def do_abc_rej(self, model, nbeta, nparticles, eps):

        nbatch = 100
        model.create_model_instance(nbeta, self.timePoints)
        self.nSpecies, self.nDynPars = model.get_model_info()
        #print "do_abc_rej : Species/Dynamical parameters in this model:", self.nSpecies, self.nDynPars

        done = False
        ntotsim = 0
        naccepted = 0
        acceptedDynParams = zeros([nparticles, self.nDynPars])
        acceptedInits = zeros([nparticles, self.nSpecies])
        acceptedIntMus = zeros([nparticles, self.nFP])
        acceptedIntSgs = zeros([nparticles, self.nFP])
        acceptedDists = zeros([nparticles])
        while done == False:
            print "\tRunning batch, nsims/nacc:", ntotsim, naccepted

            #Parameters
            dynParameters = self.sample_dyn_pars(nbatch)
            #Initial conditions
            inits = self.sample_inits(nbatch)
            #Intensity parameters
            intMus, intSgs = self.sample_int_pars(nbatch)
            print "\tDone sampling"
            sims = model.simulate(nbatch, dynParameters, inits, self.fps,
                                  intMus, intSgs)
            print "\tDone simulation"
            dists = self.compare_to_data(nbatch, nbeta, sims, self.nFP,
                                         self.fps)
            print 'dists: ', dists
            print "\tDone distance calculation"
            accMask = self.check_distance(nbatch, dists, eps)

            ntotsim += nbatch

            for i in range(nbatch):
                if accMask[i] == 1 and naccepted < nparticles:
                    acceptedDynParams[naccepted, :] = dynParameters[i, :]
                    acceptedInits[naccepted, :] = inits[i, :]
                    acceptedIntMus[naccepted, :] = intMus[i, ]
                    acceptedIntSgs[naccepted, :] = intSgs[i, ]
                    acceptedDists[naccepted] = dists[i]
                    naccepted += 1
                if naccepted == nparticles:
                    done = True

        print "do_abc_rej : Completed"
        print "           : Final acceptance rate = ", naccepted / float(
            ntotsim)
        return [
            acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs,
            acceptedDists
        ]
コード例 #2
0
ファイル: run-abc-flow.py プロジェクト: ucl-cssb/ABC-Flow
    def do_abc_rej(self, model, nbeta, nparticles, eps):
        
        nbatch = 100
        model.create_model_instance(nbeta, self.timePoints)
        self.nSpecies, self.nDynPars = model.get_model_info()
        #print "do_abc_rej : Species/Dynamical parameters in this model:", self.nSpecies, self.nDynPars

        done = False
        ntotsim = 0
        naccepted = 0
        acceptedDynParams = zeros([nparticles,self.nDynPars])
        acceptedInits = zeros([nparticles,self.nSpecies])
        acceptedIntMus = zeros([nparticles,self.nFP])
        acceptedIntSgs = zeros([nparticles,self.nFP])
        acceptedDists = zeros([nparticles])
        while done == False:
            print "\tRunning batch, nsims/nacc:", ntotsim, naccepted

            #Parameters
            dynParameters = self.sample_dyn_pars(nbatch)
            #Initial conditions
            inits = self.sample_inits(nbatch)
            #Intensity parameters
            intMus, intSgs = self.sample_int_pars(nbatch) 
            print "\tDone sampling"
            sims = model.simulate(nbatch, dynParameters, inits, self.fps, intMus, intSgs)
            print "\tDone simulation"
            dists = self.compare_to_data(nbatch, nbeta, sims, self.nFP )
            print 'dists: ', dists
            print "\tDone distance calculation"
            accMask = self.check_distance(nbatch, dists, eps)

            ntotsim += nbatch
            
            for i in range(nbatch):
                if accMask[i] == 1 and naccepted < nparticles:
                    acceptedDynParams[naccepted, :] = dynParameters[i, :]
                    acceptedInits[naccepted, :] = inits[i, :]
                    acceptedIntMus[naccepted, :] = intMus[i, ]
                    acceptedIntSgs[naccepted, :] = intSgs[i, ]
                    acceptedDists[naccepted] = dists[i]
                    naccepted += 1
                if naccepted == nparticles:
                    done = True

        print "do_abc_rej : Completed"
        print "           : Final acceptance rate = ", naccepted/float(ntotsim)
        return [acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs, acceptedDists]
コード例 #3
0
    def do_abc_smc(self, model, nbeta, nparticles, epsilons):

        npop = len(epsilons)
        nbatch = 100
        model.create_model_instance(nbeta, self.timePoints)
        self.nSpecies, self.nDynPars = model.get_model_info()

        # do the first population sampling from the prior
        print "do_abc_smc : Population 0"
        acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs = self.do_abc_rej(
            model, nbeta, nparticles, epsilons[0])
        acceptedWeights = ones([nparticles]) / float(nparticles)

        for pop in range(1, npop):
            currDynParams = zeros([nparticles, self.nDynPars])
            currInits = zeros([nparticles, self.nSpecies])
            currIntMus = zeros([nparticles, self.nFP])
            currIntSgs = zeros([nparticles, self.nFP])

            # calculate the scales for this population
            #(max-min)/2
            scales_dyn = self.calculate_scales(nparticles, acceptedDynParams)
            scales_inits = self.calculate_scales(nparticles, acceptedInits)
            scales_Mus = self.calculate_scales(nparticles, acceptedIntMus)
            scales_Sgs = self.calculate_scales(nparticles, acceptedIntSgs)

            # Rejection stage
            doneRej = False
            ntotsim = 0
            naccepted = 0
            while doneRej == False:
                print "\tRunning batch, nsims/nacc:", ntotsim, naccepted
                #Generates a random sample from a given 1-D array
                ids = numpy.random.choice(nparticles,
                                          size=nbatch,
                                          replace=True,
                                          p=acceptedWeights)
                print "sampled ids:", ids
                dynParameters = self.sample_perturb_pars(nbatch,
                                                         self.dynPriors,
                                                         acceptedDynParams,
                                                         ids,
                                                         scales=scales_dyn)
                inits = self.sample_perturb_pars(nbatch,
                                                 self.initPriors,
                                                 acceptedInits,
                                                 ids,
                                                 scales=scales_inits)
                intMus = self.sample_perturb_pars(nbatch,
                                                  self.intMeanPriors,
                                                  acceptedIntMus,
                                                  ids,
                                                  scales=scales_Mus)
                intSgs = self.sample_perturb_pars(nbatch,
                                                  self.intSigmaPriors,
                                                  acceptedIntSgs,
                                                  ids,
                                                  scales=scales_Sgs)
                print "\tDone sampling"

                sims = model.simulate(nbatch, dynParameters, inits, self.fps,
                                      intMus, intSgs)
                dists = self.compare_to_data(nbatch, nbeta, sims, self.nFP)
                accMask = self.check_distance(nbatch, dists, epsilons[pop])

                ntotsim += nbatch

                for i in range(nbatch):
                    if accMask[i] == 1 and naccepted < nparticles:
                        currDynParams[naccepted, :] = dynParameters[i, :]
                        currInits[naccepted, :] = inits[i, :]
                        currIntMus[naccepted, :] = intMus[i, ]
                        currIntSgs[naccepted, :] = intSgs[i, ]

                        naccepted += 1
                    if naccepted == nparticles:
                        doneRej = True

            print "do_abc_smc : Population", pop, "\tacceptance rate = ", naccepted / float(
                ntotsim)

            # update weights
            #column_stack = Stack 1-D arrays as columns into a 2-D array.
            currPar = column_stack(
                (currDynParams, currInits, currIntMus, currIntSgs))
            prevPar = column_stack((acceptedDynParams, acceptedInits,
                                    acceptedIntMus, acceptedIntSgs))
            all_scales = concatenate(
                (scales_dyn, scales_inits, scales_Mus, scales_Sgs))
            # print shape(currDynParams), shape(currInits), shape(currIntMus), shape(currIntSgs)
            # print shape(currPar), shape(prevPar)
            acceptedWeights = self.compute_weights(nparticles,
                                                   acceptedWeights,
                                                   currPar,
                                                   prevPar,
                                                   scales=all_scales)
            print "acceptedWeights:"
            print acceptedWeights

            # update best estimates
            acceptedDynParams = deepcopy(currDynParams)
            acceptedInits = deepcopy(currInits)
            acceptedIntMus = deepcopy(currIntMus)
            acceptedIntSgs = deepcopy(currIntSgs)

        print "do_abc_smc : Completed successfully"
        return [
            acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs,
            acceptedWeights
        ]
コード例 #4
0
    def do_abc_smc(self, model, nbeta, nparticles, alpha, epsilon_final,
                   outHan, nfp, fps, results_path):

        pop = 0
        finishTotal = False
        nbatch = 100

        model.create_model_instance(nbeta, self.timePoints)
        self.nSpecies, self.nDynPars = model.get_model_info()

        print "do_abc_smc : estimating starting epsilon"
        tDynParams = self.sample_dyn_pars(nparticles)
        tInits = self.sample_inits(nparticles)
        tIntMus, tIntSgs = self.sample_int_pars(nparticles)
        sims = model.simulate(nparticles, tDynParams, tInits, self.fps,
                              tIntMus, tIntSgs)
        tDists = self.compare_to_data(nparticles, nbeta, sims, self.nFP,
                                      self.fps)

        epsilon = self.set_epsilon(tDists, alpha)
        print "Epsilon pop: ", pop, epsilon

        print "do_abc_smc : Population 0"
        acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs, currDists = self.do_abc_rej(
            model, nbeta, nparticles, epsilon)
        acceptedWeights = ones([nparticles]) / float(nparticles)

        outfolder = results_path + "/pop" + repr(0)
        os.makedirs(outfolder)
        self.write_outputs(outHan, nfp, fps, outfolder, model, nbeta,
                           acceptedDynParams, acceptedInits, acceptedIntMus,
                           acceptedIntSgs, acceptedWeights)

        while finishTotal == False:
            #for pop in range(1, npop):
            pop += 1
            epsilon = self.set_epsilon(currDists, alpha)
            print "Epsilon pop: ", pop, epsilon

            currDynParams = zeros([nparticles, self.nDynPars])
            currInits = zeros([nparticles, self.nSpecies])
            currIntMus = zeros([nparticles, self.nFP])
            currIntSgs = zeros([nparticles, self.nFP])
            currDists = zeros(nparticles)

            # calculate the scales for this population
            #(max-min)/2
            scales_dyn = self.calculate_scales(nparticles, acceptedDynParams)
            scales_inits = self.calculate_scales(nparticles, acceptedInits)
            scales_Mus = self.calculate_scales(nparticles, acceptedIntMus)
            scales_Sgs = self.calculate_scales(nparticles, acceptedIntSgs)

            # Rejection stage
            doneRej = False
            ntotsim = 0
            naccepted = 0
            while doneRej == False:
                print "\tRunning batch, nsims/nacc:", ntotsim, naccepted
                #Generates a random sample from a given 1-D array
                ids = numpy.random.choice(nparticles,
                                          size=nbatch,
                                          replace=True,
                                          p=acceptedWeights)
                print "sampled ids:", ids
                dynParameters = self.sample_perturb_pars(nbatch,
                                                         self.dynPriors,
                                                         acceptedDynParams,
                                                         ids,
                                                         scales=scales_dyn)
                inits = self.sample_perturb_pars(nbatch,
                                                 self.initPriors,
                                                 acceptedInits,
                                                 ids,
                                                 scales=scales_inits)
                intMus = self.sample_perturb_pars(nbatch,
                                                  self.intMeanPriors,
                                                  acceptedIntMus,
                                                  ids,
                                                  scales=scales_Mus)
                intSgs = self.sample_perturb_pars(nbatch,
                                                  self.intSigmaPriors,
                                                  acceptedIntSgs,
                                                  ids,
                                                  scales=scales_Sgs)
                print "\tDone sampling"

                sims = model.simulate(nbatch, dynParameters, inits, self.fps,
                                      intMus, intSgs)
                dists = self.compare_to_data(nbatch, nbeta, sims, self.nFP,
                                             self.fps)
                accMask = self.check_distance(nbatch, dists, epsilon)
                ntotsim += nbatch

                for i in range(nbatch):
                    if accMask[i] == 1 and naccepted < nparticles:
                        currDynParams[naccepted, :] = dynParameters[i, :]
                        currInits[naccepted, :] = inits[i, :]
                        currIntMus[naccepted, :] = intMus[i, ]
                        currIntSgs[naccepted, :] = intSgs[i, ]
                        currDists[naccepted] = dists[i]
                        naccepted += 1
                    if naccepted == nparticles:
                        doneRej = True

            print "do_abc_smc : Population", pop, "\tacceptance rate = ", naccepted / float(
                ntotsim)
            #pop_fold_res_path = 'Population_' + str(pop)
            #os.makedirs('Population_' + str(pop))
            print "Accepted parameters : ", currDynParams

            # update weights
            #column_stack = Stack 1-D arrays as columns into a 2-D array.
            currPar = column_stack(
                (currDynParams, currInits, currIntMus, currIntSgs))
            prevPar = column_stack((acceptedDynParams, acceptedInits,
                                    acceptedIntMus, acceptedIntSgs))
            all_scales = concatenate(
                (scales_dyn, scales_inits, scales_Mus, scales_Sgs))
            # print shape(currDynParams), shape(currInits), shape(currIntMus), shape(currIntSgs)
            # print shape(currPar), shape(prevPar)
            acceptedWeights = self.compute_weights(nparticles,
                                                   acceptedWeights,
                                                   currPar,
                                                   prevPar,
                                                   scales=all_scales)
            print "acceptedWeights:"
            print acceptedWeights

            # update best estimates
            acceptedDynParams = deepcopy(currDynParams)
            acceptedInits = deepcopy(currInits)
            acceptedIntMus = deepcopy(currIntMus)
            acceptedIntSgs = deepcopy(currIntSgs)

            # write progress out
            outfolder = results_path + "/pop" + repr(pop)
            os.makedirs(outfolder)
            self.write_outputs(outHan, nfp, fps, outfolder, model, nbeta,
                               acceptedDynParams, acceptedInits,
                               acceptedIntMus, acceptedIntSgs, acceptedWeights)

            if epsilon <= epsilon_final:
                finishTotal = True
                print "do_abc_smc : Completed successfully"

        return [
            acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs,
            acceptedWeights
        ]
コード例 #5
0
ファイル: run-abc-flow.py プロジェクト: ucl-cssb/ABC-Flow
    def do_abc_smc(self, model, nbeta, nparticles, alpha, epsilon_final,
                   outHan, nfp, fps, results_path):

        pop = 0
        finishTotal = False
        #npop = len(epsilons)
        nbatch = 100
        model.create_model_instance(nbeta, self.timePoints)
        self.nSpecies, self.nDynPars = model.get_model_info()
        # do the first population sampling from the prior
        print "do_abc_smc : Population 0"

        acceptedDynParams = self.sample_dyn_pars(nbatch)
        # Initial conditions
        acceptedInits = self.sample_inits(nbatch)
        # Intensity parameters
        acceptedIntMus, acceptedIntSgs = self.sample_int_pars(nbatch)
        print "\tDone sampling"
        sims = model.simulate(nbatch, acceptedDynParams, acceptedInits, self.fps, acceptedIntMus, acceptedIntSgs)
        print "\tDone simulation"
        currDists = self.compare_to_data(nbatch, nbeta, sims, self.nFP)
        #acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs, acceptedDists = self.do_abc_rej(model, nbeta, nparticles, epsilons[0])
        acceptedWeights = ones([nparticles])/float(nparticles)

        while finishTotal == False:
        #for pop in range(1, npop):
            pop += 1
            epsilon = self.set_epsilon(currDists, alpha)
            print "Epsilon current:", epsilon

            currDynParams = zeros([nparticles, self.nDynPars])
            currInits = zeros([nparticles, self.nSpecies])
            currIntMus = zeros([nparticles, self.nFP])
            currIntSgs = zeros([nparticles, self.nFP])
            currDists = zeros(nparticles)

            # calculate the scales for this population
            #(max-min)/2
            scales_dyn = self.calculate_scales(nparticles, acceptedDynParams)
            scales_inits = self.calculate_scales(nparticles, acceptedInits)
            scales_Mus = self.calculate_scales(nparticles, acceptedIntMus)
            scales_Sgs = self.calculate_scales(nparticles, acceptedIntSgs)

            # Rejection stage
            doneRej = False
            ntotsim = 0
            naccepted = 0
            while doneRej == False:
                print "\tRunning batch, nsims/nacc:", ntotsim, naccepted
                #Generates a random sample from a given 1-D array
                ids = numpy.random.choice(nparticles, size=nbatch, replace=True, p=acceptedWeights)
                print "sampled ids:", ids
                dynParameters = self.sample_perturb_pars(nbatch, self.dynPriors, acceptedDynParams, ids, scales=scales_dyn )
                inits         = self.sample_perturb_pars(nbatch, self.initPriors, acceptedInits, ids, scales=scales_inits)
                intMus        = self.sample_perturb_pars(nbatch, self.intMeanPriors, acceptedIntMus, ids, scales=scales_Mus)
                intSgs        = self.sample_perturb_pars(nbatch, self.intSigmaPriors, acceptedIntSgs, ids, scales=scales_Sgs)
                print "\tDone sampling"

                sims = model.simulate(nbatch, dynParameters, inits, self.fps, intMus, intSgs)
                dists = self.compare_to_data(nbatch, nbeta, sims, self.nFP)
                accMask = self.check_distance(nbatch, dists, epsilon)
                ntotsim += nbatch

                for i in range(nbatch):
                    if accMask[i] == 1 and naccepted < nparticles:
                        currDynParams[naccepted, :] = dynParameters[i, :]
                        currInits[naccepted, :] = inits[i, :]
                        currIntMus[naccepted, :] = intMus[i, ]
                        currIntSgs[naccepted, :] = intSgs[i, ]
                        currDists[naccepted] = dists[i]
                        naccepted += 1
                    if naccepted == nparticles:
                        doneRej = True

            print "do_abc_smc : Population", pop, "\tacceptance rate = ", naccepted/float(ntotsim)
            #pop_fold_res_path = 'Population_' + str(pop)
            #os.makedirs('Population_' + str(pop))
            print "Accepted parameters : ", currDynParams

            # update weights
            #column_stack = Stack 1-D arrays as columns into a 2-D array.
            currPar = column_stack((currDynParams, currInits, currIntMus, currIntSgs))
            prevPar = column_stack((acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs))
            all_scales = concatenate((scales_dyn, scales_inits, scales_Mus, scales_Sgs))
            # print shape(currDynParams), shape(currInits), shape(currIntMus), shape(currIntSgs)
            # print shape(currPar), shape(prevPar)
            acceptedWeights = self.compute_weights(nparticles, acceptedWeights, currPar, prevPar, scales=all_scales)
            print "acceptedWeights:"
            print acceptedWeights
            
            # update best estimates
            acceptedDynParams = deepcopy(currDynParams)
            acceptedInits = deepcopy(currInits)
            acceptedIntMus = deepcopy(currIntMus)
            acceptedIntSgs = deepcopy(currIntSgs)

            # write progress out
            outfolder = results_path + "/pop"+repr(pop)
            os.makedirs(outfolder)
            self.write_outputs(outHan, nfp, fps, outfolder, model, nbeta, acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs, acceptedWeights )

            if epsilon <= epsilon_final:
                finishTotal = True
                print "do_abc_smc : Completed successfully"

        return [acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs, acceptedWeights]
コード例 #6
0
ファイル: run-abc-flow.py プロジェクト: Mirelio/ABC-Flow
    def do_abc_smc(self, model, nbeta, nparticles, epsilons):

        npop = len(epsilons)
        nbatch = 100
        model.create_model_instance(nbeta, self.timePoints)
        self.nSpecies, self.nDynPars = model.get_model_info()

        # do the first population sampling from the prior
        print "do_abc_smc : Population 0"
        acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs = self.do_abc_rej(model, nbeta, nparticles, epsilons[0])
        acceptedWeights = ones([nparticles])/float(nparticles)

        for pop in range(1, npop):
            currDynParams = zeros([nparticles, self.nDynPars])
            currInits = zeros([nparticles, self.nSpecies])
            currIntMus = zeros([nparticles, self.nFP])
            currIntSgs = zeros([nparticles, self.nFP])

            # calculate the scales for this population
            #(max-min)/2
            scales_dyn = self.calculate_scales(nparticles, acceptedDynParams)
            scales_inits = self.calculate_scales(nparticles, acceptedInits)
            scales_Mus = self.calculate_scales(nparticles, acceptedIntMus)
            scales_Sgs = self.calculate_scales(nparticles, acceptedIntSgs)

            # Rejection stage
            doneRej = False
            ntotsim = 0
            naccepted = 0
            while doneRej == False:
                print "\tRunning batch, nsims/nacc:", ntotsim, naccepted
                #Generates a random sample from a given 1-D array
                ids = numpy.random.choice(nparticles, size=nbatch, replace=True, p=acceptedWeights)
                print "sampled ids:", ids
                dynParameters = self.sample_perturb_pars(nbatch, self.dynPriors, acceptedDynParams, ids, scales=scales_dyn )
                inits         = self.sample_perturb_pars(nbatch, self.initPriors, acceptedInits, ids, scales=scales_inits)
                intMus        = self.sample_perturb_pars(nbatch, self.intMeanPriors, acceptedIntMus, ids, scales=scales_Mus)
                intSgs        = self.sample_perturb_pars(nbatch, self.intSigmaPriors, acceptedIntSgs, ids, scales=scales_Sgs)
                print "\tDone sampling"

                sims = model.simulate(nbatch, dynParameters, inits, self.fps, intMus, intSgs)
                dists = self.compare_to_data(nbatch, nbeta, sims, self.nFP)
                accMask = self.check_distance(nbatch, dists, epsilons[pop])

                ntotsim += nbatch

                for i in range(nbatch):
                    if accMask[i] == 1 and naccepted < nparticles:
                        currDynParams[naccepted, :] = dynParameters[i, :]
                        currInits[naccepted, :] = inits[i, :]
                        currIntMus[naccepted, :] = intMus[i, ]
                        currIntSgs[naccepted, :] = intSgs[i, ]

                        naccepted += 1
                    if naccepted == nparticles:
                        doneRej = True

            print "do_abc_smc : Population", pop, "\tacceptance rate = ", naccepted/float(ntotsim)

            # update weights
            #column_stack = Stack 1-D arrays as columns into a 2-D array.
            currPar = column_stack( (currDynParams, currInits, currIntMus, currIntSgs) )
            prevPar = column_stack( (acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs) )
            all_scales = concatenate( (scales_dyn, scales_inits, scales_Mus, scales_Sgs) )
            # print shape(currDynParams), shape(currInits), shape(currIntMus), shape(currIntSgs)
            # print shape(currPar), shape(prevPar)
            acceptedWeights = self.compute_weights(nparticles, acceptedWeights, currPar, prevPar, scales=all_scales)
            print "acceptedWeights:"
            print acceptedWeights
            
            # update best estimates
            acceptedDynParams = deepcopy( currDynParams )
            acceptedInits = deepcopy( currInits )
            acceptedIntMus = deepcopy( currIntMus )
            acceptedIntSgs = deepcopy( currIntSgs )

        print "do_abc_smc : Completed successfully"
        return [acceptedDynParams, acceptedInits, acceptedIntMus, acceptedIntSgs, acceptedWeights]