BayesProjection { public void project() { srcPotentialIndex = 0; trgPotentialIndex = 0; recurse(0, 0); BayesAbsorption.normalize(trgPotentials); } BayesProjection(BayesVariable[] srcVars, double[] srcPotentials, int[] trgVarPos, int[] trgVarPosMultipliers, double[] trgPotentials); void project(); static void normalize(double[] darray, double sum); void recurse(int currentVar, int targetVarPos);  }
@Test public void testProjection1() { BayesVariable a = new BayesVariable<String>( "A", 0, new String[] {"A1", "A2"}, null); BayesVariable b = new BayesVariable<String>( "B", 1, new String[] {"B1", "B2"}, null); Graph<BayesVariable> graph = new BayesNetwork(); GraphNode x0 = addNode(graph); GraphNode x1 = addNode(graph); x0.setContent( a ); x1.setContent( b ); JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0011") ); JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0011") ); SeparatorState sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0011"), graph).createState(); double v = 0.1; for ( int i = 0; i < node1.getPotentials().length; i++ ) { node1.getPotentials()[i] = v; v += + 0.1; } BayesVariable[] vars = new BayesVariable[] {a, b}; BayesVariable[] sepVars = new BayesVariable[] { a, b }; int[] sepVarPos = PotentialMultiplier.createSubsetVarPos(vars, sepVars); int sepVarNumberOfStates = PotentialMultiplier.createNumberOfStates(sepVars); int[] sepVarMultipliers = PotentialMultiplier.createIndexMultipliers(sepVars, sepVarNumberOfStates); double[] projectedSepPotentials = new double[ sep.getPotentials().length]; BayesProjection p = new BayesProjection(vars, node1.getPotentials(), sepVarPos, sepVarMultipliers, projectedSepPotentials); p.project(); assertArray(new double[]{0.1, 0.2, 0.3, 0.4}, scaleDouble(3, projectedSepPotentials)); }
@Test public void testProjection2() { BayesVariable a = new BayesVariable<String>( "A", 0, new String[] {"A1", "A2"}, new double[][] {{0.1, 0.2}}); BayesVariable b = new BayesVariable<String>( "B", 1, new String[] {"B1", "B2"}, new double[][] {{0.1, 0.2}}); BayesVariable c = new BayesVariable<String>( "C", 2, new String[] {"C1", "C2"}, new double[][] {{0.1, 0.2}}); Graph<BayesVariable> graph = new BayesNetwork(); GraphNode x0 = addNode(graph); GraphNode x1 = addNode(graph); GraphNode x2 = addNode(graph); x0.setContent( a ); x1.setContent( b ); x2.setContent( c ); JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0111") ); JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0011") ); SeparatorState sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0011"), graph).createState(); double v = 0.1; for ( int i = 0; i < node1.getPotentials().length; i++ ) { node1.getPotentials()[i] = v; v = scaleDouble(3, v + 0.1 ); } BayesVariable[] vars = new BayesVariable[] {a, b, c}; BayesVariable[] sepVars = new BayesVariable[] { a, b }; int[] sepVarPos = PotentialMultiplier.createSubsetVarPos(vars, sepVars); int sepVarNumberOfStates = PotentialMultiplier.createNumberOfStates(sepVars); int[] sepVarMultipliers = PotentialMultiplier.createIndexMultipliers(sepVars, sepVarNumberOfStates); double[] projectedSepPotentials = new double[ sep.getPotentials().length]; BayesProjection p = new BayesProjection(vars, node1.getPotentials(), sepVarPos, sepVarMultipliers, projectedSepPotentials); p.project(); assertArray(new double[]{0.083, 0.194, 0.306, 0.417}, scaleDouble(3, projectedSepPotentials)); }
@Test public void testProjection3() { BayesVariable a = new BayesVariable<String>( "A", 0, new String[] {"A1", "A2"}, new double[][] {{0.1, 0.2}}); BayesVariable b = new BayesVariable<String>( "B", 1, new String[] {"B1", "B2"}, new double[][] {{0.1, 0.2}}); BayesVariable c = new BayesVariable<String>( "C", 2, new String[] {"C1", "C2"}, new double[][] {{0.1, 0.2}}); Graph<BayesVariable> graph = new BayesNetwork(); GraphNode x0 = addNode(graph); GraphNode x1 = addNode(graph); GraphNode x2 = addNode(graph); GraphNode x3 = addNode(graph); x0.setContent( a ); x1.setContent( b ); x2.setContent( c ); JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0111") ); JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0101") ); SeparatorState sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0101"), graph).createState(); double v = 0.1; for ( int i = 0; i < node1.getPotentials().length; i++ ) { node1.getPotentials()[i] = v; v = scaleDouble(3, v + 0.1 ); } BayesVariable[] vars = new BayesVariable[] {a, b, c}; BayesVariable[] sepVars = new BayesVariable[] { a, c }; int[] sepVarPos = PotentialMultiplier.createSubsetVarPos(vars, sepVars); int sepVarNumberOfStates = PotentialMultiplier.createNumberOfStates(sepVars); int[] sepVarMultipliers = PotentialMultiplier.createIndexMultipliers(sepVars, sepVarNumberOfStates); double[] projectedSepPotentials = new double[ sep.getPotentials().length]; BayesProjection p = new BayesProjection(vars, node1.getPotentials(), sepVarPos, sepVarMultipliers, projectedSepPotentials); p.project(); assertArray(new double[]{0.111, 0.167, 0.333, 0.389}, scaleDouble(3, projectedSepPotentials)); }
@Test public void testProjection4() { BayesVariable a = new BayesVariable<String>( "A", 0, new String[] {"A1", "A2"}, new double[][] {{0.1, 0.2}}); BayesVariable b = new BayesVariable<String>( "B", 1, new String[] {"B1", "B2"}, new double[][] {{0.1, 0.2}}); BayesVariable c = new BayesVariable<String>( "C", 2, new String[] {"C1", "C2"}, new double[][] {{0.1, 0.2}}); Graph<BayesVariable> graph = new BayesNetwork(); GraphNode x0 = addNode(graph); GraphNode x1 = addNode(graph); GraphNode x2 = addNode(graph); GraphNode x3 = addNode(graph); x0.setContent( a ); x1.setContent( b ); x2.setContent( c ); JunctionTreeClique node1 = new JunctionTreeClique(0, graph, bitSet("0111") ); JunctionTreeClique node2 = new JunctionTreeClique(1, graph, bitSet("0110") ); SeparatorState sep = new JunctionTreeSeparator(0, node1, node2, bitSet("0101"), graph).createState(); double v = 0.1; for ( int i = 0; i < node1.getPotentials().length; i++ ) { node1.getPotentials()[i] = v; v = scaleDouble(3, v + 0.1 ); } BayesVariable[] vars = new BayesVariable[] {a, b, c}; BayesVariable[] sepVars = new BayesVariable[] { b, c }; int[] sepVarPos = PotentialMultiplier.createSubsetVarPos(vars, sepVars); int sepVarNumberOfStates = PotentialMultiplier.createNumberOfStates(sepVars); int[] sepVarMultipliers = PotentialMultiplier.createIndexMultipliers(sepVars, sepVarNumberOfStates); double[] projectedSepPotentials = new double[ sep.getPotentials().length]; BayesProjection p = new BayesProjection(vars, node1.getPotentials(), sepVarPos, sepVarMultipliers, projectedSepPotentials); p.project(); assertArray(new double[]{0.167, 0.222, 0.278, 0.333}, scaleDouble(3, projectedSepPotentials)); }