public void test_pointwiseProduct() { IRandomVariable xRV = new RandVar("X", new BooleanDomain()); IRandomVariable yRV = new RandVar("Y", new BooleanDomain()); IRandomVariable zRV = new RandVar("Z", new BooleanDomain()); ProbabilityTable xyD = new ProbabilityTable(new double[] { // X = true, Y = true 1.0, // X = true, Y = false 2.0, // X = false, Y = true 3.0, // X = false, Y = false 4.0 }, xRV, yRV); ProbabilityTable zD = new ProbabilityTable(new double[] { 3.0, 7.0 }, zRV); // Not commutative assertArrayEquals(new double[] { 3.0, 7.0, 6.0, 14.0, 9.0, 21.0, 12.0, 28.0 }, xyD.pointwiseProduct(zD).getValues(), DELTA_THRESHOLD); assertArrayEquals(new double[] { 3.0, 6.0, 9.0, 12.0, 7.0, 14.0, 21.0, 28.0 }, zD.pointwiseProduct(xyD).getValues(), DELTA_THRESHOLD); }
public void testGibbsAsk_compare() { // create two nodes: parent and child with an arc from parent to child IRandomVariable rvParent = new RandVar("Parent", new BooleanDomain()); IRandomVariable rvChild = new RandVar("Child", new BooleanDomain()); FullCPTNode nodeParent = new FullCPTNode(rvParent, new double[] { 0.7, 0.3 }); new FullCPTNode(rvChild, new double[] { 0.8, 0.2, 0.2, 0.8 }, nodeParent); // create net BayesNet net = new BayesNet(nodeParent); // query parent probability IRandomVariable[] rvX = new IRandomVariable[] { rvParent }; // ...given child evidence (true) AssignmentProposition[] propE = new AssignmentProposition[] { new AssignmentProposition(rvChild, true) }; // sample with LikelihoodWeighting ICategoricalDistribution samplesLW = new LikelihoodWeighting().Ask(rvX, propE, net, 1000); Assert.AreEqual(0.9, samplesLW.getValue(true), DELTA_THRESHOLD); // sample with RejectionSampling ICategoricalDistribution samplesRS = new RejectionSampling().Ask(rvX, propE, net, 1000); Assert.AreEqual(0.9, samplesRS.getValue(true), DELTA_THRESHOLD); // sample with GibbsAsk ICategoricalDistribution samplesGibbs = new GibbsAsk().Ask(rvX, propE, net, 1000); Assert.AreEqual(0.9, samplesGibbs.getValue(true), DELTA_THRESHOLD); }
public void test_iterateOverTable_fixedValues() { RandVar aRV = new RandVar("A", new BooleanDomain()); RandVar bRV = new RandVar("B", new BooleanDomain()); RandVar cRV = new RandVar("C", new BooleanDomain()); ProbabilityTable ptABC = new ProbabilityTable(new double[] { // A = true, B = true, C = true 1.0, // A = true, B = true, C = false 10.0, // A = true, B = false, C = true 100.0, // A = true, B = false, C = false 1000.0, // A = false, B = true, C = true 10000.0, // A = false, B = true, C = false 100000.0, // A = false, B = false, C = true 1000000.0, // A = false, B = false, C = false 10000000.0 }, aRV, bRV, cRV); ICollection <double> answer = CollectionFactory.CreateQueue <double>(); ProbabilityTable.ProbabilityTableIterator pti = new iter(answer); answer.Clear(); ptABC.iterateOverTable(pti, new AssignmentProposition(aRV, true)); Assert.AreEqual(1111.0, sumOf(answer), DELTA_THRESHOLD); answer.Clear(); ptABC.iterateOverTable(pti, new AssignmentProposition(aRV, false)); Assert.AreEqual(11110000.0, sumOf(answer), DELTA_THRESHOLD); answer.Clear(); ptABC.iterateOverTable(pti, new aima.net.probability.proposition.AssignmentProposition(bRV, true)); Assert.AreEqual(110011.0, sumOf(answer), DELTA_THRESHOLD); answer.Clear(); ptABC.iterateOverTable(pti, new AssignmentProposition(bRV, false)); Assert.AreEqual(11001100.0, sumOf(answer), DELTA_THRESHOLD); answer.Clear(); ptABC.iterateOverTable(pti, new AssignmentProposition(cRV, true)); Assert.AreEqual(1010101.0, sumOf(answer), DELTA_THRESHOLD); answer.Clear(); ptABC.iterateOverTable(pti, new AssignmentProposition(cRV, false)); Assert.AreEqual(10101010.0, sumOf(answer), DELTA_THRESHOLD); answer.Clear(); ptABC.iterateOverTable(pti, new AssignmentProposition(bRV, true), new AssignmentProposition(cRV, true)); Assert.AreEqual(10001.0, sumOf(answer), DELTA_THRESHOLD); }
public void test_indexesOfValue() { RandVar X = new RandVar("X", new BooleanDomain()); RandVar Y = new RandVar("Y", new ArbitraryTokenDomain("A", "B", "C")); RandVar Z = new RandVar("Z", new BooleanDomain()); // An ordered X,Y,Z enumeration of values should look like: // 00: true, A, true // 01: true, A, false // 02: true, B, true // 03: true, B, false // 04: true, C, true // 05: true, C, false // 06: false, A, true // 07: false, A, false // 08: false, B, true // 09: false, B, false // 10: false, C, true // 11: false, C, false IRandomVariable[] vars = new IRandomVariable[] { X, Y, Z }; IMap <IRandomVariable, object> even = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>(); even.Put(X, true); CollectionAssert.AreEqual(new int[] { 0, 1, 2, 3, 4, 5 }, ProbUtil.indexesOfValue(vars, 0, even)); even.Put(X, false); CollectionAssert.AreEqual(new int[] { 6, 7, 8, 9, 10, 11 }, ProbUtil.indexesOfValue(vars, 0, even)); even.Put(Y, "A"); CollectionAssert.AreEqual(new int[] { 0, 1, 6, 7 }, ProbUtil.indexesOfValue(vars, 1, even)); even.Put(Y, "B"); CollectionAssert.AreEqual(new int[] { 2, 3, 8, 9 }, ProbUtil.indexesOfValue(vars, 1, even)); even.Put(Y, "C"); CollectionAssert.AreEqual(new int[] { 4, 5, 10, 11 }, ProbUtil.indexesOfValue(vars, 1, even)); even.Put(Z, true); CollectionAssert.AreEqual(new int[] { 0, 2, 4, 6, 8, 10 }, ProbUtil.indexesOfValue(vars, 2, even)); even.Put(Z, false); CollectionAssert.AreEqual(new int[] { 1, 3, 5, 7, 9, 11 }, ProbUtil.indexesOfValue(vars, 2, even)); }
/** * This method is the initialization phase of the algorithm. It has to be called to generate a set of samples of count N. * @param N the count of samples. * @return a set containing N samples. */ public ISet <P> generateCloud(int N) { ISet <P> samples = CollectionFactory.CreateSet <P>(); int[] indexes = new int[N]; for (int i = 0; i < N; ++i) { samples.Add(map.randomPose()); indexes[i] = i; } sampleIndexes = new RandVar(SAMPLE_INDEXES_NAME, new FiniteIntegerDomain(indexes)); return(samples); }
public ICategoricalDistribution forward(ICategoricalDistribution f1_t, ICollection <AssignmentProposition> e_tp1) { ICategoricalDistribution s1 = new ProbabilityTable(f1_t.getFor()); // Set up required working variables IProposition[] props = new IProposition[s1.getFor().Size()]; int i = 0; foreach (IRandomVariable rv in s1.getFor()) { props[i] = new RandVar(rv.getName(), rv.getDomain()); ++i; } IProposition Xtp1 = ProbUtil.constructConjunction(props); AssignmentProposition[] xt = new AssignmentProposition[tToTm1StateVarMap.Size()]; IMap <IRandomVariable, AssignmentProposition> xtVarAssignMap = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, AssignmentProposition>(); i = 0; foreach (IRandomVariable rv in tToTm1StateVarMap.GetKeys()) { xt[i] = new AssignmentProposition(tToTm1StateVarMap.Get(rv), "<Dummy Value>"); xtVarAssignMap.Put(rv, xt[i]); ++i; } // Step 1: Calculate the 1 time step prediction // ∑<sub>x<sub>t</sub></sub> CategoricalDistributionIterator if1_t = new CategoricalDistributionIteratorImpl(transitionModel, xtVarAssignMap, s1, Xtp1, xt); f1_t.iterateOver(if1_t); // Step 2: multiply by the probability of the evidence // and normalize // <b>P</b>(e<sub>t+1</sub> | X<sub>t+1</sub>) ICategoricalDistribution s2 = sensorModel.posteriorDistribution(ProbUtil .constructConjunction(e_tp1.ToArray()), Xtp1); return(s2.multiplyBy(s1).normalize()); }
public ICategoricalDistribution backward(ICategoricalDistribution b_kp2t, ICollection <AssignmentProposition> e_kp1) { ICategoricalDistribution b_kp1t = new ProbabilityTable(b_kp2t.getFor()); // Set up required working variables IProposition[] props = new IProposition[b_kp1t.getFor().Size()]; int i = 0; foreach (IRandomVariable rv in b_kp1t.getFor()) { IRandomVariable prv = tToTm1StateVarMap.Get(rv); props[i] = new RandVar(prv.getName(), prv.getDomain()); ++i; } IProposition Xk = ProbUtil.constructConjunction(props); AssignmentProposition[] ax_kp1 = new AssignmentProposition[tToTm1StateVarMap.Size()]; IMap <IRandomVariable, AssignmentProposition> x_kp1VarAssignMap = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, AssignmentProposition>(); i = 0; foreach (IRandomVariable rv in b_kp1t.getFor()) { ax_kp1[i] = new AssignmentProposition(rv, "<Dummy Value>"); x_kp1VarAssignMap.Put(rv, ax_kp1[i]); ++i; } IProposition x_kp1 = ProbUtil.constructConjunction(ax_kp1); props = e_kp1.ToArray(); IProposition pe_kp1 = ProbUtil.constructConjunction(props); // ∑<sub>x<sub>k+1</sub></sub> CategoricalDistributionIterator ib_kp2t = new CategoricalDistributionIteratorImpl2(x_kp1VarAssignMap, sensorModel, transitionModel, b_kp1t, pe_kp1, Xk, x_kp1); b_kp2t.iterateOver(ib_kp2t); return(b_kp1t); }
public void test_getConditioningCase() { RandVar aRV = new RandVar("A", new BooleanDomain()); RandVar bRV = new RandVar("B", new BooleanDomain()); RandVar cRV = new RandVar("C", new BooleanDomain()); CPT cpt = new CPT(cRV, new double[] { // A = true, B = true, C = true 0.1, // A = true, B = true, C = false 0.9, // A = true, B = false, C = true 0.2, // A = true, B = false, C = false 0.8, // A = false, B = true, C = true 0.3, // A = false, B = true, C = false 0.7, // A = false, B = false, C = true 0.4, // A = false, B = false, C = false 0.6 }, aRV, bRV); assertArrayEquals(new double[] { 0.1, 0.9 }, cpt .GetConditioningCase(true, true).getValues(), DELTA_THRESHOLD); assertArrayEquals(new double[] { 0.2, 0.8 }, cpt .GetConditioningCase(true, false).getValues(), DELTA_THRESHOLD); assertArrayEquals(new double[] { 0.3, 0.7 }, cpt .GetConditioningCase(false, true).getValues(), DELTA_THRESHOLD); assertArrayEquals(new double[] { 0.4, 0.6 }, cpt .GetConditioningCase(false, false).getValues(), DELTA_THRESHOLD); }
public void test_pointwiseProductPOS() { IRandomVariable xRV = new RandVar("X", new BooleanDomain()); IRandomVariable yRV = new RandVar("Y", new BooleanDomain()); IRandomVariable zRV = new RandVar("Z", new BooleanDomain()); ProbabilityTable xyD = new ProbabilityTable(new double[] { // X = true, Y = true 1.0, // X = true, Y = false 2.0, // X = false, Y = true 3.0, // X = false, Y = false 4.0 }, xRV, yRV); ProbabilityTable zD = new ProbabilityTable(new double[] { 3.0, 7.0 }, zRV); // Make commutative by specifying an order for the product assertArrayEquals(xyD.pointwiseProduct(zD).getValues(), zD .pointwiseProductPOS(xyD, xRV, yRV, zRV).getValues(), DELTA_THRESHOLD); }
public void test_indexOf() { RandVar X = new RandVar("X", new BooleanDomain()); RandVar Y = new RandVar("Y", new ArbitraryTokenDomain("A", "B", "C")); RandVar Z = new RandVar("Z", new BooleanDomain()); // An ordered X,Y,Z enumeration of values should look like: // 00: true, A, true // 01: true, A, false // 02: true, B, true // 03: true, B, false // 04: true, C, true // 05: true, C, false // 06: false, A, true // 07: false, A, false // 08: false, B, true // 09: false, B, false // 10: false, C, true // 11: false, C, false IRandomVariable[] vars = new IRandomVariable[] { X, Y, Z }; IMap <IRandomVariable, object> even = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, object>(); even.Put(X, true); even.Put(Y, "A"); even.Put(Z, true); Assert.AreEqual(0, ProbUtil.indexOf(vars, even)); even.Put(Z, false); Assert.AreEqual(1, ProbUtil.indexOf(vars, even)); even.Put(Y, "B"); even.Put(Z, true); Assert.AreEqual(2, ProbUtil.indexOf(vars, even)); even.Put(Z, false); Assert.AreEqual(3, ProbUtil.indexOf(vars, even)); even.Put(Y, "C"); even.Put(Z, true); Assert.AreEqual(4, ProbUtil.indexOf(vars, even)); even.Put(Z, false); Assert.AreEqual(5, ProbUtil.indexOf(vars, even)); // even.Put(X, false); even.Put(Y, "A"); even.Put(Z, true); Assert.AreEqual(6, ProbUtil.indexOf(vars, even)); even.Put(Z, false); Assert.AreEqual(7, ProbUtil.indexOf(vars, even)); even.Put(Y, "B"); even.Put(Z, true); Assert.AreEqual(8, ProbUtil.indexOf(vars, even)); even.Put(Z, false); Assert.AreEqual(9, ProbUtil.indexOf(vars, even)); even.Put(Y, "C"); even.Put(Z, true); Assert.AreEqual(10, ProbUtil.indexOf(vars, even)); even.Put(Z, false); Assert.AreEqual(11, ProbUtil.indexOf(vars, even)); }
public void test_divideBy() { IRandomVariable xRV = new RandVar("X", new BooleanDomain()); IRandomVariable yRV = new RandVar("Y", new BooleanDomain()); IRandomVariable zRV = new RandVar("Z", new BooleanDomain()); ProbabilityTable xyzD = new ProbabilityTable(new double[] { // X = true, Y = true, Z = true 1.0, // X = true, Y = true, Z = false 2.0, // X = true, Y = false, Z = true 3.0, // X = true, Y = false, Z = false 4.0, // X = false, Y = true, Z = true 5.0, // X = false, Y = true, Z = false 6.0, // X = false, Y = false, Z = true 7.0, // X = false, Y = false, Z = false 8.0, }, xRV, yRV, zRV); ProbabilityTable xzyD = new ProbabilityTable(new double[] { // X = true, Z = true, Y = true 1.0, // X = true, Z = true, Y = false 3.0, // X = true, Z = false, Y = true 2.0, // X = true, Z = false, Y = false 4.0, // X = false, Z = true, Y = true 5.0, // X = false, Z = true, Y = false 7.0, // X = false, Z = false, Y = true 6.0, // X = false, Z = false, Y = false 8.0, }, xRV, zRV, yRV); ProbabilityTable zxyD = new ProbabilityTable(new double[] { // Z = true, X = true, Y = true 1.0, // Z = true, X = true, Y = false 3.0, // Z = true, X = false, Y = true 5.0, // Z = true, X = false, Y = false 7.0, // Z = false, X = true, Y = true 2.0, // Z = false, X = true, Y = false 4.0, // Z = false, X = false, Y = true 6.0, // Z = false, X = false, Y = false 8.0, }, zRV, xRV, yRV); ProbabilityTable zD = new ProbabilityTable(new double[] { 0.5, 0.2 }, zRV); // The identity distribution (to order results for comparison purposes) ProbabilityTable iD = new ProbabilityTable(new double[] { 1.0 }); // Ensure the order of the dividends // makes no difference to the result assertArrayEquals(xyzD.divideBy(zD).getValues(), xzyD.divideBy(zD).pointwiseProductPOS(iD, xRV, yRV, zRV) .getValues(), DELTA_THRESHOLD); assertArrayEquals(xzyD.divideBy(zD).getValues(), zxyD.divideBy(zD).pointwiseProductPOS(iD, xRV, zRV, yRV) .getValues(), DELTA_THRESHOLD); }