Exemplo n.º 1
0
Arquivo: abcsmc.py Projeto: bcmd/BCMD
    def computeParticleWeights(self):
        if self.debug == 2:print "\t***computeParticleWeights"

        for k in range(self.nparticles):
            this_model = self.model_curr[k]
            this_param = self.parameters_curr[k]
            
            # calculate model prior probility 
            mprob = self.modelprior[ this_model ]

            np = len(self.parameters_curr[k])
            # particle prior probability
            pprob = 1
            for n in range(0,np):
                x = 1.0
                if self.models[ this_model ].prior[n][0]==0:
                    x=1

                if self.models[ this_model ].prior[n][0]==1: 
                    x=statistics.getPdfGauss(self.models[ this_model ].prior[n][1],
                                             numpy.sqrt(self.models[ this_model ].prior[n][2]),
                                             this_param[n])
                
                if self.models[ this_model ].prior[n][0]==2: 
                    x=statistics.getPdfUniform(self.models[ this_model ].prior[n][1],
                                               self.models[ this_model ].prior[n][2],
                                               this_param[n])
    
                if self.models[ this_model ].prior[n][0]==3: 
                    x=statistics.getPdfLognormal(self.models[ this_model ].prior[n][1],
                                                 numpy.sqrt(self.models[ this_model ].prior[n][2]),
                                                 this_param[n])
                pprob = pprob*x

            numer = self.b[k] * mprob * pprob
        
            denom_m = 0
            for i in range(self.nmodel):
                denom_m = denom_m + self.margins_prev[i]*getPdfModelKernel(this_model, i, self.modelKernel, self.nmodel, self.dead_models)
            denom = 0
            # print "Calculating denom\t", selected_model, sampleParameters
            for j in range(self.nparticles):

                if(int(this_model) == int(self.model_prev[j]) ):
                    # print "\t", j, model_prev[j], weights_prev[j], parameters_prev[j]
                    if self.debug == 2:
                        print "\tj, weights_prev, kernelpdf", j, self.weights_prev[j],
                        self.kernelpdffn(this_param, self.parameters_prev[j], self.models[this_model].prior, self.kernels[this_model], self.kernel_aux[j], self.kernel_type )
                    denom = denom + self.weights_prev[j] * self.kernelpdffn(this_param, self.parameters_prev[j], self.models[this_model].prior, self.kernels[this_model], self.kernel_aux[j], self.kernel_type )

                if self.debug == 2: print "\tnumer/denom_m/denom/m(t-1) : ", numer,denom_m, denom, self.margins_prev[this_model]

            self.weights_curr[k] = numer/(denom_m*denom/self.margins_prev[this_model])
Exemplo n.º 2
0
def getPdfParameterKernel(params, params0, priors, kernel, auxilliary,
                          kernel_type):
    if kernel_type == 1:
        prob = 1
        # n refers to the index of the parameter (integer between 0 and np-1)
        # ind is an integer between 0 and len(kernel[0])-1 which enables to determine the kernel to use
        ind = 0
        for n in kernel[0]:
            kern = statistics.getPdfUniform(params0[n] + kernel[2][ind][0],
                                            params0[n] + kernel[2][ind][1],
                                            params[n])
            prob = prob * kern
            ind += 1
        return prob

    elif kernel_type == 2:
        # n refers to the index of the parameter (integer between 0 and np-1)
        # ind is an integer between 0 and len(kernel[0])-1 which enables to determine the kernel to use
        prob = 1
        ind = 0
        for n in kernel[0]:
            mean = params0[n]
            scale = numpy.sqrt(kernel[2][ind])
            kern = statistics.getPdfGauss(mean, scale, params[n])
            kern = kern / auxilliary[n]
            prob = prob * kern
            ind += 1
        return prob

    elif kernel_type == 3:
        p0 = list()
        p = list()
        for n in kernel[0]:
            p0.append(params0[n])
            p.append(params[n])
        kern = statistics.getPdfMultinormal(p0, kernel[2], p)
        kern = kern / auxilliary
        return kern

    elif (kernel_type == 4 or kernel_type == 5):
        p0 = list()
        p = list()
        D = kernel[2]
        for n in kernel[0]:
            p0.append(params0[n])
            p.append(params[n])
        kern = statistics.getPdfMultinormal(p0, D[str(params0)], p)
        kern = kern / auxilliary
        return kern
Exemplo n.º 3
0
def getPdfParameterKernel(params, params0, priors, kernel, auxilliary, kernel_type):
    if kernel_type == 1:
        prob = 1
        # n refers to the index of the parameter (integer between 0 and np-1)
        # ind is an integer between 0 and len(kernel[0])-1 which enables to determine the kernel to use
        ind = 0
        for n in kernel[0]:
            kern = statistics.getPdfUniform(params0[n] + kernel[2][ind][0], params0[n] + kernel[2][ind][1], params[n])
            prob = prob * kern
            ind += 1
        return prob

    elif kernel_type == 2:
        # n refers to the index of the parameter (integer between 0 and np-1)
        # ind is an integer between 0 and len(kernel[0])-1 which enables to determine the kernel to use
        prob = 1
        ind = 0
        for n in kernel[0]:
            mean = params0[n]
            scale = numpy.sqrt(kernel[2][ind])
            kern = statistics.getPdfGauss(mean, scale, params[n])
            kern = kern / auxilliary[n]
            prob = prob * kern
            ind += 1
        return prob

    elif kernel_type == 3:
        p0 = list()
        p = list()
        for n in kernel[0]:
            p0.append(params0[n])
            p.append(params[n])
        kern = statistics.getPdfMultinormal(p0, kernel[2], p)
        kern = kern / auxilliary
        return kern

    elif kernel_type == 4 or kernel_type == 5:
        p0 = list()
        p = list()
        D = kernel[2]
        for n in kernel[0]:
            p0.append(params0[n])
            p.append(params[n])
        kern = statistics.getPdfMultinormal(p0, D[str(params0)], p)
        kern = kern / auxilliary
        return kern
Exemplo n.º 4
0
    def computeParticleWeights(self):
        if self.debug == 2: print "\t***computeParticleWeights"

        for k in range(self.nparticles):
            this_model = self.model_curr[k]
            this_param = self.parameters_curr[k]

            # calculate model prior probility
            mprob = self.modelprior[this_model]

            np = len(self.parameters_curr[k])
            # particle prior probability
            pprob = 1
            for n in range(0, np):
                x = 1.0
                if self.models[this_model].prior[n][0] == 0:
                    x = 1

                if self.models[this_model].prior[n][0] == 1:
                    x = statistics.getPdfGauss(
                        self.models[this_model].prior[n][1],
                        numpy.sqrt(self.models[this_model].prior[n][2]),
                        this_param[n])

                if self.models[this_model].prior[n][0] == 2:
                    x = statistics.getPdfUniform(
                        self.models[this_model].prior[n][1],
                        self.models[this_model].prior[n][2], this_param[n])

                if self.models[this_model].prior[n][0] == 3:
                    x = statistics.getPdfLognormal(
                        self.models[this_model].prior[n][1],
                        numpy.sqrt(self.models[this_model].prior[n][2]),
                        this_param[n])
                pprob = pprob * x

            numer = self.b[k] * mprob * pprob

            denom_m = 0
            for i in range(self.nmodel):
                denom_m = denom_m + self.margins_prev[i] * getPdfModelKernel(
                    this_model, i, self.modelKernel, self.nmodel,
                    self.dead_models)
            denom = 0
            # print "Calculating denom\t", selected_model, sampleParameters
            for j in range(self.nparticles):

                if (int(this_model) == int(self.model_prev[j])):
                    # print "\t", j, model_prev[j], weights_prev[j], parameters_prev[j]
                    if self.debug == 2:
                        print "\tj, weights_prev, kernelpdf", j, self.weights_prev[
                            j],
                        self.kernelpdffn(this_param, self.parameters_prev[j],
                                         self.models[this_model].prior,
                                         self.kernels[this_model],
                                         self.kernel_aux[j], self.kernel_type)
                    denom = denom + self.weights_prev[j] * self.kernelpdffn(
                        this_param, self.parameters_prev[j], self.
                        models[this_model].prior, self.kernels[this_model],
                        self.kernel_aux[j], self.kernel_type)

                if self.debug == 2:
                    print "\tnumer/denom_m/denom/m(t-1) : ", numer, denom_m, denom, self.margins_prev[
                        this_model]

            self.weights_curr[k] = numer / (denom_m * denom /
                                            self.margins_prev[this_model])