private Assignment sampleContinuousVars (Variable[] contVars, Randoms r) { Collection contFactors = allFactorsContaining (Arrays.asList (contVars)); FactorGraph contFg = new FactorGraph (contVars); for (Iterator it = contFactors.iterator (); it.hasNext ();) { Factor factor = (Factor) it.next (); contFg.multiplyBy (factor); } return contFg.sampleInternal (r); }
public static void addRandomNodePotentials (Random r, FactorGraph mdl) { int size = mdl.numVariables (); for (int i = 0; i < size; i++) { Variable var = mdl.get (i); TableFactor ptl = randomNodePotential (r, var); mdl.addFactor (ptl); } }
/** * Adds a factor to the model. * <P> * If a factor has already been added for the variables in the * given clique, the effects of this method are (currently) * undefined. * <p> * All convenience methods for adding factors eventually call through * to this one, so this is the method for subclasses to override if they * wish to perform additional actions when a factor is added to the graph. * * @param factor A factor over the variables in clique. */ public void addFactor (Factor factor) { beforeFactorAdd (factor); VarSet varSet = factor.varSet (); addVarsIfNecessary (varSet); factors.add (factor); factorsAlphabet.lookupIndex (factor); addToListMap (clique2ptl, varSet, factor); // cache the factor cacheFactor (varSet, factor); afterFactorAdd (factor); }
public void testLogNormalize () { FactorGraph fg = new FactorGraph (); fg.multiplyBy (ltbl1); fg.multiplyBy (ltbl2); fg.normalize (); assertEquals (1.0, fg.sum(), 1e-5); }
public void testNumVariables () { FactorGraph fg = new FactorGraph (); fg.multiplyBy (tbl1); fg.multiplyBy (tbl2); assertEquals (3, fg.numVariables ()); }
public void testEmbeddedFactorGraph () { FactorGraph embeddedFg = new FactorGraph (); embeddedFg.multiplyBy (tbl1); embeddedFg.multiplyBy (tbl2); FactorGraph fg = new FactorGraph (); fg.multiplyBy (embeddedFg); fg.multiplyBy (tbl3); assertEquals (4, fg.varSet ().size ()); assertEquals (2, fg.factors ().size ()); Assignment assn = new Assignment (fg.varSet ().toVariableArray (), new int [4]); assertEquals (0.032, fg.value (assn), 1e-5); AbstractTableFactor tbl = fg.asTable (); assertEquals (4, tbl.varSet ().size ()); assertEquals (0.032, tbl.value (assn), 1e-5); }
public Factor extractMax (Variable[] vars) { if (numVariables () < 5) { return asTable ().extractMax (vars); } else { throw new UnsupportedOperationException ("not yet implemented"); } }
/** * Tests that models can be created that have multiple factors over the same variable, and that * potentialOfVertex returns the product in that case. */ public void testMultipleNodePotentials () { Variable var = new Variable (2); FactorGraph mdl = new FactorGraph (new Variable[]{var}); Factor ptl1 = new TableFactor (var, new double[]{0.5, 0.5}); mdl.addFactor (ptl1); Factor ptl2 = new TableFactor (var, new double[]{0.25, 0.25}); mdl.addFactor (ptl2); // verify that factorOf(var) doesn't work try { mdl.factorOf (var); fail (); } catch (RuntimeException e) {} // expected List factors = mdl.allFactorsOf (var); Factor total = TableFactor.multiplyAll (factors); double[] expected = {0.125, 0.125}; assertTrue ("Arrays not equal\n Expected " + ArrayUtils.toString (expected) + "\n Actual " + ArrayUtils.toString (((TableFactor) total).toValueArray ()), Arrays.equals (expected, ((TableFactor) total).toValueArray ())); }
public void testValue () { FactorGraph fg = new FactorGraph (); fg.multiplyBy (tbl1); fg.multiplyBy (tbl2); Assignment assn = new Assignment (fg.varSet ().toVariableArray (), new int[] { 0, 1, 0 }); assertEquals (0.08, fg.value (assn), 1e-5); }
/** * Searches the graphical model for a factor over the given * collection of variables. * @return The factor defined over the given collection. Returns null if * no such factor exists. Will not return * factors defined over subsets or supersets of the given collection. * @throws RuntimeException If multiple factors exist over the given collection. * @see #allFactorsOf(java.util.Collection) * @see #addFactor(Factor) * @see #factorOf(VarSet) */ public Factor factorOf (Collection c) { List factors = allFactorsOf (c); return firstIfSingleton (factors, c.toString ()); }
public void testMultiply () { FactorGraph fg = new FactorGraph (); fg.multiplyBy (tbl1); fg.multiplyBy (tbl2); FactorGraph fg2 = (FactorGraph) fg.multiply (tbl3); assertEquals (2, fg.factors ().size()); assertEquals (3, fg2.factors ().size()); assertTrue (!fg.factors ().contains (tbl3)); assertTrue (fg2.factors ().contains (tbl3)); }
public void testAsTable () { FactorGraph fg = new FactorGraph (); fg.multiplyBy (tbl1); fg.multiplyBy (tbl2); AbstractTableFactor actual = fg.asTable (); AbstractTableFactor expected = (AbstractTableFactor) tbl1.multiply (tbl2); assertTrue (expected.almostEquals (actual)); }
public void testAllFactorsOf2 () throws IOException { Variable x1 = new Variable (2); Variable x2 = new Variable (2); FactorGraph fg = new FactorGraph (); fg.addFactor (new TableFactor (x1)); fg.addFactor (new TableFactor (x2)); fg.addFactor (new TableFactor (new Variable[] { x1, x2 })); List lst = fg.allFactorsOf (x1); assertEquals (1, lst.size ()); for (Iterator it = lst.iterator (); it.hasNext ();) { Factor f = (Factor) it.next (); assertEquals (1, f.varSet().size()); assertTrue (f.varSet ().contains (x1)); } HashVarSet vs = new HashVarSet (new Variable[]{x1, x2}); List lst2 = fg.allFactorsOf (vs); assertEquals (1, lst2.size ()); Factor f = (Factor) lst2.get (0); assertTrue (f.varSet ().equals (vs)); }
/** Returns the degree of a given variable in this factor graph, * that is, the number of factors in which the variable is * an argument. */ public int getDegree (Variable var) { return allFactorsContaining (var).size (); }
public void testSum () { FactorGraph fg = new FactorGraph (); fg.multiplyBy (tbl1); fg.multiplyBy (tbl2); assertEquals (1.71, fg.sum (), 1e-5); }
public void testMarginalize () { FactorGraph fg = new FactorGraph (); fg.multiplyBy (tbl1); fg.multiplyBy (tbl2); Factor marg = fg.marginalize (vars[1]); Factor expected = new TableFactor (vars[1], new double[] { 0.81, 0.9 }); assertTrue (expected.almostEquals (marg)); }
public FactorizedRegion (List factors) { super (varsForFactors (factors), factors); subMdl = new FactorGraph ((Variable[]) vars.toArray (new Variable[0])); for (Iterator it = factors.iterator (); it.hasNext ();) { Factor factor = (Factor) it.next (); subMdl.addFactor (factor); } }
public void testLogTableTimesFg () { FactorGraph fg = new FactorGraph (); fg.multiplyBy (tbl1); fg.multiplyBy (tbl2); Factor product = ltbl1.multiply (fg); assertTrue (product instanceof AbstractTableFactor); assertEquals (3, product.varSet ().size ()); Assignment assn = new Assignment (product.varSet ().toVariableArray (), new int [3]); assertEquals (0.128, product.value (assn), 1e-5); }
private void recacheFactors () { numNodes = 0; for (Iterator it = factors.iterator (); it.hasNext ();) { Factor ptl = (Factor) it.next (); VarSet vs = ptl.varSet (); addVarsIfNecessary (vs); cacheFactor (vs, ptl); } }