Exemplo n.º 1
0
def update_particle_weights(particles, log_weights, settings):
    for n, p in enumerate(particles):
        if settings.verbose >= 2:
            print 'pid = %5d, log_sis_ratio = %f' % (n, p.log_sis_ratio)
        log_weights[n] += p.log_sis_ratio
    weights_norm = softmax(log_weights)     # normalized weights
    ess = 1. / np.sum(weights_norm ** 2) / settings.n_particles
    log_pd = logsumexp(log_weights)
    return (log_pd, ess, log_weights, weights_norm)
Exemplo n.º 2
0
def update_particle_weights(particles,log_weights,settings):
	for n,p in enumerate(particles):
		if settings.verbose >=2:
			print('pid = %5d, log_sis_ratio =%f' % (n,p.log_sis_ratio))
		log_weights[n] +=p.log_sis_ratio
	norm_of_weights = softmax(log_weights) # normalize the weights
	ess = 1./ np.sum(norm_of_weights**2)/ settings.n_particles
	log_pd = logsumexp(log_weights)
	return (log_pd,ess,log_weights,norm_of_weights)
Exemplo n.º 3
0
 def update_p(self, particles, log_weights, log_pd, settings):
     node_info_old = {}
     first_iter = False
     if settings.verbose >= 2:
         print('log_weights = %s' % log_weights)
     k = sample_multinomial(softmax(log_weights))
     try:
         node_info_old = self.p.node_info
     except AttributeError:
         first_iter = True
         # first iteration probably: self.p would not be present
         pass
     same_tree = node_info_old == particles[k].node_info
     self.p = particles[k]
     self.log_pd = log_pd
     if settings.verbose >= 2:
         print('pid_sampled = %s' % k)
         print('new tree:')
         self.p.print_tree()
     if not same_tree and settings.verbose >= 1:
         print('non-identical trees')
     if k == 0 and not first_iter:
         assert same_tree
     elif same_tree and not first_iter:  # particles from pmcmc during init might be different
         if settings.verbose >= 1:
             print('identical tree without k == 0')
         try:
             check_if_zero(log_weights[k] - log_weights[0])
         except AssertionError:
             print('node_info_old = %s' % node_info_old)
             print('same_tree = %s' % same_tree)
             print('k = %s, particles[k].node_info = %s' %
                   (k, particles[k].node_info))
             print('log_weights[0] = %s, log_weights[k] = %s' % \
                     (log_weights[0], log_weights[k]))
             if not first_iter:
                 print(p_old.log_sis_ratio_d)
             print(particles[0].log_sis_ratio_d)
             print(particles[k].log_sis_ratio_d)
             raise AssertionError
     self.p.check_depth()
     if settings.verbose >= 2:
         print('sampled particle = %5d, ancestry = %s' %
               (k, self.p.ancestry))
     return not same_tree
Exemplo n.º 4
0
 def update_p(self, particles, log_weights, log_pd, settings):
     node_info_old = {}
     first_iter = False
     if settings.verbose >= 2:
         print 'log_weights = %s' % log_weights
     k = sample_multinomial(softmax(log_weights))
     try:
         node_info_old = self.p.node_info 
     except AttributeError:
         first_iter = True
         # first iteration probably: self.p would not be present
         pass
     same_tree = node_info_old == particles[k].node_info
     self.p = particles[k] 
     self.log_pd = log_pd
     if settings.verbose >= 2:
         print 'pid_sampled = %s' % k
         print 'new tree:'
         self.p.print_tree()
     if not same_tree and settings.verbose >=1:
         print 'non-identical trees'
     if k == 0 and not first_iter:
         assert same_tree
     elif same_tree and not first_iter:  # particles from pmcmc during init might be different
         if settings.verbose >= 1:
             print 'identical tree without k == 0'
         try:
             check_if_zero(log_weights[k] - log_weights[0])
         except AssertionError:
             print 'node_info_old = %s' % node_info_old
             print 'same_tree = %s' % same_tree
             print 'k = %s, particles[k].node_info = %s' % (k, particles[k].node_info)
             print 'log_weights[0] = %s, log_weights[k] = %s' % \
                     (log_weights[0], log_weights[k])
             if not first_iter:
                 print p_old.log_sis_ratio_d
             print particles[0].log_sis_ratio_d
             print particles[k].log_sis_ratio_d
             raise AssertionError
     self.p.check_depth()
     if settings.verbose >= 2:
         print 'sampled particle = %5d, ancestry = %s' % (k, self.p.ancestry)
     return not same_tree