Beispiel #1
0
  def propose(self,trace,scaffold):
    from particle import Particle

    assertTrace(trace,scaffold)

    pnodes = scaffold.getPrincipalNodes()
    currentValues = getCurrentValues(trace,pnodes)
    allSetsOfValues = getCartesianProductOfEnumeratedValues(trace,pnodes)
    registerDeterministicLKernels(trace,scaffold,pnodes,currentValues)

    detachAndExtract(trace,scaffold.border[0],scaffold)
    assertTorus(scaffold)
    xiWeights = []
    xiParticles = []

    for p in range(len(allSetsOfValues)):
      newValues = allSetsOfValues[p]
      xiParticle = Particle(trace)
      assertTorus(scaffold)
      registerDeterministicLKernels(trace,scaffold,pnodes,newValues)
      xiParticles.append(xiParticle)
      xiWeights.append(regenAndAttach(xiParticle,scaffold.border[0],scaffold,False,OmegaDB(),{}))

    # Now sample a NEW particle in proportion to its weight
    finalIndex = sampleLogCategorical(xiWeights)
    self.finalParticle = xiParticles[finalIndex]
    return self.finalParticle,0
Beispiel #2
0
  def propose(self,trace,scaffold):
    self.trace = trace
    self.scaffold = scaffold
    if not registerVariationalLKernels(trace,scaffold):
      self.delegate = MHOperator()
      return self.delegate.propose(trace,scaffold)
    _,self.rhoDB = detachAndExtract(trace,scaffold.border[0],scaffold)
    assertTorus(scaffold)

    for _ in range(self.numIters):
      gradients = {}
      gain = regenAndAttach(trace,scaffold.border[0],scaffold,False,OmegaDB(),gradients)
      detachAndExtract(trace,scaffold.border[0],scaffold)
      assertTorus(scaffold)
      for node,lkernel in scaffold.lkernels.iteritems():
        if isinstance(lkernel,VariationalLKernel):
          assert node in gradients
          lkernel.updateParameters(gradients[node],gain,self.stepSize)

    rhoWeight = regenAndAttach(trace,scaffold.border[0],scaffold,True,self.rhoDB,{})
    detachAndExtract(trace,scaffold.border[0],scaffold)
    assertTorus(scaffold)

    xiWeight = regenAndAttach(trace,scaffold.border[0],scaffold,False,OmegaDB(),{})
    return trace,xiWeight - rhoWeight
Beispiel #3
0
 def reject(self):
   # TODO This is the same as MHOperator reject except for the
   # delegation thing -- abstract
   if self.delegate is None:
     detachAndExtract(self.trace,self.scaffold.border[0],self.scaffold)
     assertTorus(self.scaffold)
     regenAndAttach(self.trace,self.scaffold.border[0],self.scaffold,True,self.rhoDB,{})
   else:
     self.delegate.reject()
Beispiel #4
0
 def propose(self, trace, scaffold):
   self.prepare(trace, scaffold)
   logBound = computeRejectionBound(trace, scaffold, scaffold.border[0])
   accept = False
   while not accept:
     xiWeight = regenAndAttach(trace, scaffold.border[0], scaffold, False, self.rhoDB, {})
     accept = random.random() < math.exp(xiWeight - logBound)
     if not accept:
       detachAndExtract(trace, scaffold.border[0], scaffold)
   return trace, 0
Beispiel #5
0
  def propose(self,trace,scaffold):
    self.trace = trace
    self.scaffold = scaffold

    assertTrace(self.trace,self.scaffold)

    self.T = len(self.scaffold.border)
    T = self.T
    P = self.P

    rhoWeights = [None for t in range(T)]
    omegaDBs = [[None for p in range(P+1)] for t in range(T)]
    ancestorIndices = [[None for p in range(P)] + [P] for t in range(T)]

    self.omegaDBs = omegaDBs
    self.ancestorIndices = ancestorIndices

    for t in reversed(range(T)):
      (rhoWeights[t],omegaDBs[t][P]) = detachAndExtract(trace,scaffold.border[t],scaffold)

    assertTorus(scaffold)
    xiWeights = [None for p in range(P)]

    # Simulate and calculate initial xiWeights
    for p in range(P):
      regenAndAttach(trace,scaffold.border[0],scaffold,False,OmegaDB(),{})
      (xiWeights[p],omegaDBs[0][p]) = detachAndExtract(trace,scaffold.border[0],scaffold)

#   for every time step,
    for t in range(1,T):
      newWeights = [None for p in range(P)]
      # Sample new particle and propagate
      for p in range(P):
        extendedWeights = xiWeights + [rhoWeights[t-1]]
        ancestorIndices[t][p] = sampleLogCategorical(extendedWeights)
        path = constructAncestorPath(ancestorIndices,t,p)
        restoreAncestorPath(trace,self.scaffold.border,self.scaffold,omegaDBs,t,path)
        regenAndAttach(trace,self.scaffold.border[t],self.scaffold,False,OmegaDB(),{})
        (newWeights[p],omegaDBs[t][p]) = detachAndExtract(trace,self.scaffold.border[t],self.scaffold)
        detachRest(trace,self.scaffold.border,self.scaffold,t)
      xiWeights = newWeights

    # Now sample a NEW particle in proportion to its weight
    finalIndex = sampleLogCategorical(xiWeights)

    path = constructAncestorPath(ancestorIndices,T-1,finalIndex) + [finalIndex]
    assert len(path) == T
    restoreAncestorPath(trace,self.scaffold.border,self.scaffold,omegaDBs,T,path)
    assertTrace(self.trace,self.scaffold)

    return trace,self._compute_alpha(rhoWeights[T-1], xiWeights, finalIndex)
 def evalOneLocalSection(self, trace, local_scaffold, compute_gradient = False):
     assert(self.global_scaffold.globalBorder is not None)
     # Detach and extract
     _,local_rhoDB = detachAndExtract(trace, local_scaffold.border[0], local_scaffold, compute_gradient)
     # Regen and attach with the old value
     proposed_value = trace.valueAt(self.global_scaffold.globalBorder)
     trace.setValueAt(self.global_scaffold.globalBorder, self.global_rhoDB.getValue(self.global_scaffold.globalBorder))
     regenAndAttach(trace,local_scaffold.border[0],local_scaffold,False,local_rhoDB,{})
     
     # Detach and extract
     rhoWeight,local_rhoDB = detachAndExtract(trace, local_scaffold.border[0], local_scaffold, compute_gradient)
     
     # Regen and attach with the new value
     trace.setValueAt(self.global_scaffold.globalBorder, proposed_value)
     xiWeight = regenAndAttach(trace,local_scaffold.border[0],local_scaffold,False,local_rhoDB,{})
     return xiWeight - rhoWeight
Beispiel #7
0
  def propose(self,trace,scaffold):
    from particle import Particle
    self.trace = trace
    self.scaffold = scaffold

    assertTrace(self.trace,self.scaffold)

    #print map(len, scaffold.border)

    self.T = len(self.scaffold.border)
    T = self.T
    P = self.P

#    assert T == 1 # TODO temporary
    rhoDBs = [None for t in range(T)]
    rhoWeights = [None for t in range(T)]

    for t in reversed(range(T)):
      rhoWeights[t],rhoDBs[t] = detachAndExtract(trace,scaffold.border[t],scaffold)

    assertTorus(scaffold)

    particles = [Particle(trace) for p in range(P+1)]
    self.particles = particles

    particleWeights = [None for p in range(P+1)]


    # Simulate and calculate initial xiWeights

    for p in range(P):
      particleWeights[p] = regenAndAttach(particles[p],scaffold.border[0],scaffold,False,OmegaDB(),{})

    particleWeights[P] = regenAndAttach(particles[P],scaffold.border[0],scaffold,True,rhoDBs[0],{})
    assert_almost_equal(particleWeights[P],rhoWeights[0])

#   for every time step,
    for t in range(1,T):
      newParticles = [None for p in range(P+1)]
      newParticleWeights = [None for p in range(P+1)]
      # Sample new particle and propagate
      for p in range(P):
        parent = sampleLogCategorical(particleWeights)
        newParticles[p] = Particle(particles[parent])
        newParticleWeights[p] = regenAndAttach(newParticles[p],self.scaffold.border[t],self.scaffold,False,OmegaDB(),{})
      newParticles[P] = Particle(particles[P])
      newParticleWeights[P] = regenAndAttach(newParticles[P],self.scaffold.border[t],self.scaffold,True,rhoDBs[t],{})
      assert_almost_equal(newParticleWeights[P],rhoWeights[t])
      particles = newParticles
      particleWeights = newParticleWeights

    # Now sample a NEW particle in proportion to its weight
    finalIndex = sampleLogCategorical(particleWeights[0:-1])
    assert finalIndex < P

    self.finalIndex = finalIndex
    self.particles = particles

    return particles[finalIndex],self._compute_alpha(particleWeights, finalIndex)
 def prepare(self, trace, scaffold, compute_gradient = False):
     """Record the trace and scaffold for accepting or rejecting later;
         detach along the scaffold and return the weight thereof."""
     self.trace = trace
     self.scaffold = scaffold
     rhoWeight,self.rhoDB = detachAndExtract(trace, scaffold.border[0], scaffold, compute_gradient)
     assertTorus(scaffold)
     return rhoWeight
 def makeConsistent(self,trace,indexer):
     # Go through every local child and do extra and regen.
     # This is to be called at the end of a number of transitions.
     if not hasattr(self, "global_scaffold"):
         self.global_scaffold = indexer.sampleGlobalIndex(trace)
     for local_child in self.global_scaffold.local_children:
         local_scaffold = indexer.sampleLocalIndex(trace,local_child)
         _,local_rhoDB = detachAndExtract(trace, local_scaffold.border[0], local_scaffold)
         regenAndAttach(trace,local_scaffold.border[0],local_scaffold,False,local_rhoDB,{})
Beispiel #10
0
 def __call__(self, values):
   """Returns the gradient of the weight of regenerating along
   an (implicit) scaffold starting with the given values.  Smashes
   the trace, but leaves it a torus.  Assumes there are no delta
   kernels around."""
   # TODO Assert that no delta kernels are requested?
   self.fixed_regen(values)
   new_scaffold = constructScaffold(self.trace, [set(self.pnodes)])
   registerDeterministicLKernels(self.trace, new_scaffold, self.pnodes, values)
   (_, rhoDB) = detachAndExtract(self.trace, new_scaffold.border[0], new_scaffold, True)
   self.scaffold = new_scaffold
   return [rhoDB.getPartial(pnode) for pnode in self.pnodes]
Beispiel #11
0
 def reject(self):
     detachAndExtract(self.trace,self.scaffold.border[0],self.scaffold)
     assertTorus(self.scaffold)
     regenAndAttach(self.trace,self.scaffold.border[0],self.scaffold,True,self.rhoDB,{})
Beispiel #12
0
 def reject(self):
     # Only restore the global section.
     detachAndExtract(self.trace,self.global_scaffold.border[0],self.global_scaffold)
     assertTorus(self.global_scaffold)
     regenAndAttach(self.trace,self.global_scaffold.border[0],self.global_scaffold,True,self.global_rhoDB,{})
Beispiel #13
0
def detachRest(trace,border,scaffold,t):
  for i in reversed(range(t)):
    detachAndExtract(trace,border[i],scaffold)