public Factor multiply (Factor other) { // special handling of identity factor if (Maths.almostEquals (c, 1.0)) { return other.duplicate (); } else if (other instanceof ConstantFactor) { return new ConstantFactor (c * ((ConstantFactor)other).c); } else { return other.multiply (this); } }
public Factor multiply (Factor other) { // special handling of identity factor if (Maths.almostEquals (c, 1.0)) { return other.duplicate (); } else if (other instanceof ConstantFactor) { return new ConstantFactor (c * ((ConstantFactor)other).c); } else { return other.multiply (this); } }
public Factor multiply (Factor other) { // special handling of identity factor if (Maths.almostEquals (c, 1.0)) { return other.duplicate (); } else if (other instanceof ConstantFactor) { return new ConstantFactor (c * ((ConstantFactor)other).c); } else { return other.multiply (this); } }
private void initJtCpts(FactorGraph mdl, JunctionTree jt) { for (Iterator it = jt.getVerticesIterator(); it.hasNext();) { VarSet c = (VarSet) it.next(); // DiscreteFactor ptl = createBlankFactor (c); // jt.setCPF(c, ptl); jt.setCPF (c, new ConstantFactor (1.0)); } for (Iterator it = mdl.factors ().iterator(); it.hasNext();) { Factor ptl = (Factor) it.next(); VarSet parent = jt.findParentCluster(ptl.varSet()); assert parent != null : "Unable to find parent cluster for ptl " + ptl + "in jt " + jt; Factor cpf = jt.getCPF(parent); Factor newCpf = cpf.multiply(ptl); jt.setCPF (parent, newCpf); /* debug if (jt.isNaN()) { throw new RuntimeException ("Got a NaN"); } */ } }
private void initJtCpts(FactorGraph mdl, JunctionTree jt) { for (Iterator it = jt.getVerticesIterator(); it.hasNext();) { VarSet c = (VarSet) it.next(); // DiscreteFactor ptl = createBlankFactor (c); // jt.setCPF(c, ptl); jt.setCPF (c, new ConstantFactor (1.0)); } for (Iterator it = mdl.factors ().iterator(); it.hasNext();) { Factor ptl = (Factor) it.next(); VarSet parent = jt.findParentCluster(ptl.varSet()); assert parent != null : "Unable to find parent cluster for ptl " + ptl + "in jt " + jt; Factor cpf = jt.getCPF(parent); Factor newCpf = cpf.multiply(ptl); jt.setCPF (parent, newCpf); /* debug if (jt.isNaN()) { throw new RuntimeException ("Got a NaN"); } */ } }
private void initJtCpts(FactorGraph mdl, JunctionTree jt) { for (Iterator it = jt.getVerticesIterator(); it.hasNext();) { VarSet c = (VarSet) it.next(); // DiscreteFactor ptl = createBlankFactor (c); // jt.setCPF(c, ptl); jt.setCPF (c, new ConstantFactor (1.0)); } for (Iterator it = mdl.factors ().iterator(); it.hasNext();) { Factor ptl = (Factor) it.next(); VarSet parent = jt.findParentCluster(ptl.varSet()); assert parent != null : "Unable to find parent cluster for ptl " + ptl + "in jt " + jt; Factor cpf = jt.getCPF(parent); Factor newCpf = cpf.multiply(ptl); jt.setCPF (parent, newCpf); /* debug if (jt.isNaN()) { throw new RuntimeException ("Got a NaN"); } */ } }
public void testSumGradLog2 () { Assignment alphaAssn = new Assignment (alpha, 1.0); double[] values = new double[] { 0.4, 0.1, 0.3, 0.2 }; Factor q1 = new TableFactor (vars, values); Factor q2 = new TableFactor (new Variable(2), new double[] { 0.7, 0.3 }); Factor q = q1.multiply (q2); double grad = factor.sumGradLog (q, alpha, alphaAssn); assertEquals (-0.4, grad, 1e-5); }
public void testSumGradLog2 () { Assignment alphaAssn = new Assignment (alpha, 1.0); double[] values = new double[] { 0.4, 0.1, 0.3, 0.2 }; Factor q1 = new TableFactor (vars, values); Factor q2 = new TableFactor (new Variable(2), new double[] { 0.7, 0.3 }); Factor q = q1.multiply (q2); double grad = factor.sumGradLog (q, alpha, alphaAssn); assertEquals (-0.4, grad, 1e-5); }
public void testMutualInfo1 () { Factor ptl1 = new TableFactor (new Variable (2), new double[] { 0.7, 0.3 }); Factor ptl2 = new TableFactor (new Variable (2), new double[] { 0.2, 0.8 }); Factor joint = ptl1.multiply (ptl2); assertEquals (0.0, Factors.mutualInformation (joint), 1e-5); }
public void testMutualInfo1 () { Factor ptl1 = new TableFactor (new Variable (2), new double[] { 0.7, 0.3 }); Factor ptl2 = new TableFactor (new Variable (2), new double[] { 0.2, 0.8 }); Factor joint = ptl1.multiply (ptl2); assertEquals (0.0, Factors.mutualInformation (joint), 1e-5); }
/** * This sends a sum-product message, normalized to avoid * underflow. */ public void sendMessage (JunctionTree jt, VarSet from, VarSet to) { Collection sepset = jt.getSepset (from, to); Factor fromCpf = jt.getCPF (from); Factor toCpf = jt.getCPF (to); Factor oldSepsetPot = jt.getSepsetPot (from, to); Factor lambda = fromCpf.marginalize (sepset); lambda.normalize (); jt.setSepsetPot (lambda, from, to); toCpf = toCpf.multiply (lambda); toCpf.divideBy (oldSepsetPot); toCpf.normalize (); jt.setCPF (to, toCpf); }
/** * This sends a max-product message. */ public void sendMessage (JunctionTree jt, VarSet from, VarSet to) { // System.err.println ("Send message "+from+" --> "+to); Collection sepset = jt.getSepset (from, to); Factor fromCpf = jt.getCPF (from); Factor toCpf = jt.getCPF (to); Factor oldSepsetPot = jt.getSepsetPot (from, to); Factor lambda = fromCpf.extractMax (sepset); lambda.normalize (); jt.setSepsetPot (lambda, from, to); toCpf = toCpf.multiply (lambda); toCpf.divideBy (oldSepsetPot); toCpf.normalize (); jt.setCPF (to, toCpf); }
/** * This sends a max-product message. */ public void sendMessage (JunctionTree jt, VarSet from, VarSet to) { // System.err.println ("Send message "+from+" --> "+to); Collection sepset = jt.getSepset (from, to); Factor fromCpf = jt.getCPF (from); Factor toCpf = jt.getCPF (to); Factor oldSepsetPot = jt.getSepsetPot (from, to); Factor lambda = fromCpf.extractMax (sepset); lambda.normalize (); jt.setSepsetPot (lambda, from, to); toCpf = toCpf.multiply (lambda); toCpf.divideBy (oldSepsetPot); toCpf.normalize (); jt.setCPF (to, toCpf); }
/** * This sends a sum-product message, normalized to avoid * underflow. */ public void sendMessage (JunctionTree jt, VarSet from, VarSet to) { Collection sepset = jt.getSepset (from, to); Factor fromCpf = jt.getCPF (from); Factor toCpf = jt.getCPF (to); Factor oldSepsetPot = jt.getSepsetPot (from, to); Factor lambda = fromCpf.marginalize (sepset); lambda.normalize (); jt.setSepsetPot (lambda, from, to); toCpf = toCpf.multiply (lambda); toCpf.divideBy (oldSepsetPot); toCpf.normalize (); jt.setCPF (to, toCpf); }
/** * This sends a sum-product message, normalized to avoid * underflow. */ public void sendMessage (JunctionTree jt, VarSet from, VarSet to) { Collection sepset = jt.getSepset (from, to); Factor fromCpf = jt.getCPF (from); Factor toCpf = jt.getCPF (to); Factor oldSepsetPot = jt.getSepsetPot (from, to); Factor lambda = fromCpf.marginalize (sepset); lambda.normalize (); jt.setSepsetPot (lambda, from, to); toCpf = toCpf.multiply (lambda); toCpf.divideBy (oldSepsetPot); toCpf.normalize (); jt.setCPF (to, toCpf); }
/** * This sends a max-product message. */ public void sendMessage (JunctionTree jt, VarSet from, VarSet to) { // System.err.println ("Send message "+from+" --> "+to); Collection sepset = jt.getSepset (from, to); Factor fromCpf = jt.getCPF (from); Factor toCpf = jt.getCPF (to); Factor oldSepsetPot = jt.getSepsetPot (from, to); Factor lambda = fromCpf.extractMax (sepset); lambda.normalize (); jt.setSepsetPot (lambda, from, to); toCpf = toCpf.multiply (lambda); toCpf.divideBy (oldSepsetPot); toCpf.normalize (); jt.setCPF (to, toCpf); }