public boolean almostEquals (Factor p) { return almostEquals (p, Maths.EPSILON); }
int[] largeIdxToSmall (DiscreteFactor smallPotential) // private int cachedlargeIdxToSmall (int largeIdx, MultinomialPotential smallPotential) { if (projectionCache == null) initializeProjectionCache (); // Special case where smallPtl has only one variable. Here // since ordering is not a problem, we can use a set-based // hash key. return cachedLargeIdxToSmall (smallPotential); // if (smallPotential.varSet ().size () == 1) { // return cachedLargeIdxToSmall (smallPotential); // } else { // return computeLargeIdxToSmall (smallPotential); // } }
public Factor marginalize (Collection vars) { assert varSet ().containsAll (vars); // Perhaps throw exception instead return marginalizeInternal (createBlankSubset (vars)); }
public Factor extractMax (Collection vars) { return extractMaxInternal (createBlankSubset (vars)); }
private void expandToContain (DiscreteFactor pot) { // if so, expand this potential. this is not pretty if (needsToExpand (varSet (), pot.varSet ())) { VarSet newVarSet = new HashVarSet (varSet ()); newVarSet.addAll (pot.varSet ()); AbstractTableFactor newPtl = createBlankSubset (newVarSet); newPtl.multiplyByInternal (this); vars = newPtl.vars; probs = newPtl.probs; numVars = newPtl.numVars; initializeProjectionCache (); } }
/** * Does the conceptual equivalent of this /= pot. * Assumes that pot's variables are a subset of * this potential's. */ public void divideBy (Factor pot) { if (pot instanceof DiscreteFactor) { DiscreteFactor pot1 = (DiscreteFactor) pot; // cheating expandToContain (pot1); pot1 = ensureOperandCompatible (pot1); divideByInternal (pot1); } else if (pot instanceof ConstantFactor) { timesEquals (1.0 / pot.value (new Assignment ())); } else { AbstractTableFactor tbl; try { tbl = pot.asTable (); } catch (UnsupportedOperationException e) { throw new UnsupportedOperationException ("Don't know how to multiply "+this+" by "+pot); } multiplyBy (tbl); } }
public static CPT normalizeAsCpt (AbstractTableFactor ptl, Variable var) double[] sums = new double [ptl.numLocations ()]; Arrays.fill (sums, Double.NEGATIVE_INFINITY); VarSet neighbors = new HashVarSet (ptl.varSet ()); neighbors.remove (var); for (AssignmentIterator it = ptl.assignmentIterator (); it.hasNext (); it.advance ()) { Assignment assn = it.assignment (); Assignment nbrAssn = (Assignment) assn.marginalizeOut (var); int idx = nbrAssn.singleIndex (); sums[idx] = Maths.sumLogProb (ptl.logValue (assn), sums[idx]); for (AssignmentIterator it = ptl.assignmentIterator (); it.hasNext (); it.advance ()) { Assignment assn = it.assignment (); double oldVal = ptl.logValue (assn); ptl.setLogValue (assn, Double.NEGATIVE_INFINITY); } else { ptl.setLogValue (assn, oldVal - logZ);
private void recomputeFactors () { lastResids = new double [factors ().size ()]; for (Iterator it = cliques.iterator(); it.hasNext();) { UnrolledVarSet clique = (UnrolledVarSet) it.next(); AbstractTableFactor oldF = (AbstractTableFactor) clique.getFactor (); AbstractTableFactor newF = clique.tmpl.computeFactor (clique); double dist = Factors.distLinf ((AbstractTableFactor) oldF.duplicate ().normalize (), (AbstractTableFactor) newF.duplicate ().normalize ()); lastResids [getIndex (oldF)] = dist; oldF.setValues (newF.getLogValueMatrix ()); clique.tmpl.modifyPotential (this, clique, oldF); } }
public int sampleLocation (Randoms r) { double sum = sum(); double sampled = r.nextUniform () * sum; double cum = 0; for (int idx = 0; idx < probs.numLocations (); idx++) { double val = value (idx); cum += val; if (sampled <= cum + EPS) { return idx; } } throw new RuntimeException ("Internal errors: Couldn't sample from potential "+this+"\n"+dumpToString ()+"\n Using value "+sampled); }
public Factor marginalizeOut (Variable var) { Set newVars = new HashVarSet (vars); newVars.remove (var); return marginalizeInternal (createBlankSubset (newVars)); }
private int[] cachedLargeIdxToSmall (DiscreteFactor smallPotential) { int hashval = computeSubsetHashValue (smallPotential); Object ints = projectionCache.get (hashval); if (ints != null) { return (int[]) ints; } else { int[] projection = computeLargeIdxToSmall (smallPotential); projectionCache.put (hashval, projection); return projection; } }
public double entropy () { double h = 0; double p; for (AssignmentIterator it = assignmentIterator (); it.hasNext ();) { p = logValue (it); if (!Double.isInfinite (p)) h -= p * Math.exp (p); it.advance (); } return h; }
public Object clone () { return duplicate (); }
private AbstractTableFactor createBlankSubset (Collection vars) { return createBlankSubset ((Variable[]) vars.toArray (new Variable [vars.size ()])); }
public AbstractTableFactor asTable () { return TableFactor.multiplyAll (factors).asTable (); }
private void expandToContain (DiscreteFactor pot) { // if so, expand this potential. this is not pretty if (needsToExpand (varSet (), pot.varSet ())) { VarSet newVarSet = new HashVarSet (varSet ()); newVarSet.addAll (pot.varSet ()); AbstractTableFactor newPtl = createBlankSubset (newVarSet); newPtl.multiplyByInternal (this); vars = newPtl.vars; probs = newPtl.probs; numVars = newPtl.numVars; initializeProjectionCache (); } }
/** * Does the conceptual equivalent of this /= pot. * Assumes that pot's variables are a subset of * this potential's. */ public void divideBy (Factor pot) { if (pot instanceof DiscreteFactor) { DiscreteFactor pot1 = (DiscreteFactor) pot; // cheating expandToContain (pot1); pot1 = ensureOperandCompatible (pot1); divideByInternal (pot1); } else if (pot instanceof ConstantFactor) { timesEquals (1.0 / pot.value (new Assignment ())); } else { AbstractTableFactor tbl; try { tbl = pot.asTable (); } catch (UnsupportedOperationException e) { throw new UnsupportedOperationException ("Don't know how to multiply "+this+" by "+pot); } multiplyBy (tbl); } }
public static CPT normalizeAsCpt (AbstractTableFactor ptl, Variable var) double[] sums = new double [ptl.numLocations ()]; Arrays.fill (sums, Double.NEGATIVE_INFINITY); VarSet neighbors = new HashVarSet (ptl.varSet ()); neighbors.remove (var); for (AssignmentIterator it = ptl.assignmentIterator (); it.hasNext (); it.advance ()) { Assignment assn = it.assignment (); Assignment nbrAssn = (Assignment) assn.marginalizeOut (var); int idx = nbrAssn.singleIndex (); sums[idx] = Maths.sumLogProb (ptl.logValue (assn), sums[idx]); for (AssignmentIterator it = ptl.assignmentIterator (); it.hasNext (); it.advance ()) { Assignment assn = it.assignment (); double oldVal = ptl.logValue (assn); ptl.setLogValue (assn, Double.NEGATIVE_INFINITY); } else { ptl.setLogValue (assn, oldVal - logZ);
public Factor extractMax (Variable var) { return extractMaxInternal (createBlankSubset (new Variable[] { var })); }
private void recomputeFactors () { lastResids = new double [factors ().size ()]; for (Iterator it = cliques.iterator(); it.hasNext();) { UnrolledVarSet clique = (UnrolledVarSet) it.next(); AbstractTableFactor oldF = (AbstractTableFactor) clique.getFactor (); AbstractTableFactor newF = clique.tmpl.computeFactor (clique); double dist = Factors.distLinf ((AbstractTableFactor) oldF.duplicate ().normalize (), (AbstractTableFactor) newF.duplicate ().normalize ()); lastResids [getIndex (oldF)] = dist; oldF.setValues (newF.getLogValueMatrix ()); clique.tmpl.modifyPotential (this, clique, oldF); } }