Package aima.core.probability

Examples of aima.core.probability.CategoricalDistribution


      // else <b>B</b> <- <b>BTO</b><sub>t</sub>
      B = B.times(hmm.getTransitionModel()).times(O_t);
    }

    // if t > d then return NORMALIZE(<b>f</b> * <b>B1</b>) else return null
    CategoricalDistribution rVal = null;
    if (t > d) {
      rVal = hmm
          .convert(hmm.normalize(f.arrayTimes(B.times(unitMessage))));
    }
    // t <- t + 1
View Full Code Here


      Proposition... evidence) {

    Proposition conjEvidence = ProbUtil.constructConjunction(evidence);

    // P(A | B) = P(A AND B)/P(B) - (13.3 AIMA3e)
    CategoricalDistribution dAandB = jointDistribution(phi, conjEvidence);
    CategoricalDistribution dEvidence = jointDistribution(conjEvidence);

    return dAandB.divideBy(dEvidence);
  }
View Full Code Here

      List<List<AssignmentProposition>> ev, CategoricalDistribution prior) {
    // local variables: fv, a vector of forward messages for steps 0,...,t
    List<CategoricalDistribution> fv = new ArrayList<CategoricalDistribution>(
        ev.size() + 1);
    // b, a representation of the backward message, initially all 1s
    CategoricalDistribution b = initBackwardMessage();
    // sv, a vector of smoothed estimates for steps 1,...,t
    List<CategoricalDistribution> sv = new ArrayList<CategoricalDistribution>(
        ev.size());

    // fv[0] <- prior
View Full Code Here

  }

  @Override
  public CategoricalDistribution forward(CategoricalDistribution f1_t,
      List<AssignmentProposition> e_tp1) {
    final CategoricalDistribution s1 = new ProbabilityTable(f1_t.getFor());
    // Set up required working variables
    Proposition[] props = new Proposition[s1.getFor().size()];
    int i = 0;
    for (RandomVariable rv : s1.getFor()) {
      props[i] = new RandVar(rv.getName(), rv.getDomain());
      i++;
    }
    final Proposition Xtp1 = ProbUtil.constructConjunction(props);
    final AssignmentProposition[] xt = new AssignmentProposition[tToTm1StateVarMap
        .size()];
    final Map<RandomVariable, AssignmentProposition> xtVarAssignMap = new HashMap<RandomVariable, AssignmentProposition>();
    i = 0;
    for (RandomVariable rv : tToTm1StateVarMap.keySet()) {
      xt[i] = new AssignmentProposition(tToTm1StateVarMap.get(rv),
          "<Dummy Value>");
      xtVarAssignMap.put(rv, xt[i]);
      i++;
    }

    // Step 1: Calculate the 1 time step prediction
    // &sum;<sub>x<sub>t</sub></sub>
    CategoricalDistribution.Iterator if1_t = new CategoricalDistribution.Iterator() {
      public void iterate(Map<RandomVariable, Object> possibleWorld,
          double probability) {
        // <b>P</b>(X<sub>t+1</sub> | x<sub>t</sub>)*
        // P(x<sub>t</sub> | e<sub>1:t</sub>)
        for (Map.Entry<RandomVariable, Object> av : possibleWorld
            .entrySet()) {
          xtVarAssignMap.get(av.getKey()).setValue(av.getValue());
        }
        int i = 0;
        for (double tp : transitionModel
            .posteriorDistribution(Xtp1, xt).getValues()) {
          s1.setValue(i, s1.getValues()[i] + (tp * probability));
          i++;
        }
      }
    };
    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>)
    CategoricalDistribution s2 = sensorModel.posteriorDistribution(ProbUtil
        .constructConjunction(e_tp1.toArray(new Proposition[e_tp1
            .size()])), Xtp1);

    return s2.multiplyBy(s1).normalize();
  }
View Full Code Here

  }

  @Override
  public CategoricalDistribution backward(CategoricalDistribution b_kp2t,
      List<AssignmentProposition> e_kp1) {
    final CategoricalDistribution b_kp1t = new ProbabilityTable(
        b_kp2t.getFor());
    // Set up required working variables
    Proposition[] props = new Proposition[b_kp1t.getFor().size()];
    int i = 0;
    for (RandomVariable rv : b_kp1t.getFor()) {
      RandomVariable prv = tToTm1StateVarMap.get(rv);
      props[i] = new RandVar(prv.getName(), prv.getDomain());
      i++;
    }
    final Proposition Xk = ProbUtil.constructConjunction(props);
    final AssignmentProposition[] ax_kp1 = new AssignmentProposition[tToTm1StateVarMap
        .size()];
    final Map<RandomVariable, AssignmentProposition> x_kp1VarAssignMap = new HashMap<RandomVariable, AssignmentProposition>();
    i = 0;
    for (RandomVariable rv : b_kp1t.getFor()) {
      ax_kp1[i] = new AssignmentProposition(rv, "<Dummy Value>");
      x_kp1VarAssignMap.put(rv, ax_kp1[i]);
      i++;
    }
    final Proposition x_kp1 = ProbUtil.constructConjunction(ax_kp1);
    props = new Proposition[e_kp1.size()];
    final Proposition pe_kp1 = ProbUtil.constructConjunction(e_kp1
        .toArray(props));

    // &sum;<sub>x<sub>k+1</sub></sub>
    CategoricalDistribution.Iterator ib_kp2t = new CategoricalDistribution.Iterator() {
      public void iterate(Map<RandomVariable, Object> possibleWorld,
          double probability) {
        // Assign current values for x<sub>k+1</sub>
        for (Map.Entry<RandomVariable, Object> av : possibleWorld
            .entrySet()) {
          x_kp1VarAssignMap.get(av.getKey()).setValue(av.getValue());
        }

        // P(e<sub>k+1</sub> | x<sub>k+1</sub>)
        // P(e<sub>k+2:t</sub> | x<sub>k+1</sub>)
        double p = sensorModel.posterior(pe_kp1, x_kp1) * probability;

        // <b>P</b>(x<sub>k+1</sub> | X<sub>k</sub>)
        int i = 0;
        for (double tp : transitionModel.posteriorDistribution(x_kp1,
            Xk).getValues()) {
          b_kp1t.setValue(i, b_kp1t.getValues()[i] + (tp * p));
          i++;
        }
      }
    };
    b_kp2t.iterateOver(ib_kp2t);
 
View Full Code Here

    // just query over the scope of proposition phi in order
    // to get a joint distribution for these
    final Proposition conjunct = ProbUtil.constructConjunction(phi);
    RandomVariable[] X = conjunct.getScope().toArray(
        new RandomVariable[conjunct.getScope().size()]);
    CategoricalDistribution d = bayesInference.ask(X,
        new AssignmentProposition[0], bayesNet);

    // Then calculate the probability of the propositions phi
    // be seeing where they hold.
    final double[] probSum = new double[1];
    CategoricalDistribution.Iterator di = new CategoricalDistribution.Iterator() {
      public void iterate(Map<RandomVariable, Object> possibleWorld,
          double probability) {
        if (conjunct.holds(possibleWorld)) {
          probSum[0] += probability;
        }
      }
    };
    d.iterateOver(di);

    return probSum[0];
  }
View Full Code Here

      Proposition... evidence) {

    Proposition conjEvidence = ProbUtil.constructConjunction(evidence);

    // P(A | B) = P(A AND B)/P(B) - (13.3 AIMA3e)
    CategoricalDistribution dAandB = jointDistribution(phi, conjEvidence);
    CategoricalDistribution dEvidence = jointDistribution(conjEvidence);

    CategoricalDistribution rVal = dAandB.divideBy(dEvidence);
    // Note: Need to ensure normalize() is called
    // in order to handle the case where an approximate
    // algorithm is used (i.e. won't evenly divide
    // as will have calculated on separate approximate
    // runs). However, this should only be done
    // if the all of the evidences scope are bound (if not
    // you are returning in essence a set of conditional
    // distributions, which you do not want normalized).
    boolean unboundEvidence = false;
    for (Proposition e : evidence) {
      if (e.getUnboundScope().size() > 0) {
        unboundEvidence = true;
        break;
      }
    }
    if (!unboundEvidence) {
      rVal.normalize();
    }

    return rVal;
  }
View Full Code Here

      Proposition... evidence) {

    Proposition conjEvidence = ProbUtil.constructConjunction(evidence);

    // P(A | B) = P(A AND B)/P(B) - (13.3 AIMA3e)
    CategoricalDistribution dAandB = jointDistribution(phi, conjEvidence);
    CategoricalDistribution dEvidence = jointDistribution(conjEvidence);

    return dAandB.divideBy(dEvidence);
  }
View Full Code Here

      Proposition... evidence) {

    Proposition conjEvidence = ProbUtil.constructConjunction(evidence);

    // P(A | B) = P(A AND B)/P(B) - (13.3 AIMA3e)
    CategoricalDistribution dAandB = jointDistribution(phi, conjEvidence);
    CategoricalDistribution dEvidence = jointDistribution(conjEvidence);

    CategoricalDistribution rVal = dAandB.divideBy(dEvidence);
    // Note: Need to ensure normalize() is called
    // in order to handle the case where an approximate
    // algorithm is used (i.e. won't evenly divide
    // as will have calculated on separate approximate
    // runs). However, this should only be done
    // if the all of the evidences scope are bound (if not
    // you are returning in essence a set of conditional
    // distributions, which you do not want normalized).
    boolean unboundEvidence = false;
    for (Proposition e : evidence) {
      if (e.getUnboundScope().size() > 0) {
        unboundEvidence = true;
        break;
      }
    }
    if (!unboundEvidence) {
      rVal.normalize();
    }

    return rVal;
  }
View Full Code Here

  protected void test_RollingPairFairDiceModel_Distributions(
      FiniteProbabilityModel model) {

    AssignmentProposition ad1_1 = new AssignmentProposition(
        ExampleRV.DICE_1_RV, 1);
    CategoricalDistribution dD1_1 = model.priorDistribution(ad1_1);
    Assert.assertArrayEquals(new double[] { 1.0 / 6.0 }, dD1_1.getValues(),
        DELTA_THRESHOLD);

    CategoricalDistribution dPriorDice1 = model
        .priorDistribution(ExampleRV.DICE_1_RV);
    Assert.assertArrayEquals(new double[] { 1.0 / 6.0, 1.0 / 6.0,
        1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0 },
        dPriorDice1.getValues(), DELTA_THRESHOLD);

    CategoricalDistribution dPriorDice2 = model
        .priorDistribution(ExampleRV.DICE_2_RV);
    Assert.assertArrayEquals(new double[] { 1.0 / 6.0, 1.0 / 6.0,
        1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0 },
        dPriorDice2.getValues(), DELTA_THRESHOLD);

    CategoricalDistribution dJointDice1Dice2 = model.jointDistribution(
        ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);
    Assert.assertEquals(36, dJointDice1Dice2.getValues().length);
    for (int i = 0; i < dJointDice1Dice2.getValues().length; i++) {
      Assert.assertEquals(1.0 / 36.0, dJointDice1Dice2.getValues()[i],
          DELTA_THRESHOLD);
    }

    CategoricalDistribution dJointDice2Dice1 = model.jointDistribution(
        ExampleRV.DICE_2_RV, ExampleRV.DICE_1_RV);
    Assert.assertEquals(36, dJointDice2Dice1.getValues().length);
    for (int i = 0; i < dJointDice2Dice1.getValues().length; i++) {
      Assert.assertEquals(1.0 / 36.0, dJointDice2Dice1.getValues()[i],
          DELTA_THRESHOLD);
    }

    //
    // Test Sets of events
    IntegerSumProposition total11 = new IntegerSumProposition("Total",
        new FiniteIntegerDomain(11), ExampleRV.DICE_1_RV,
        ExampleRV.DICE_2_RV);
    // P<>(Total = 11) = <2.0/36.0>
    Assert.assertArrayEquals(new double[] { 2.0 / 36.0 }, model
        .priorDistribution(total11).getValues(), DELTA_THRESHOLD);

    // P<>(Dice1, Total = 11)
    // = <0.0, 0.0, 0.0, 0.0, 1.0/36.0, 1.0/36.0>
    Assert.assertArrayEquals(new double[] { 0, 0, 0, 0, 1.0 / 36.0,
        1.0 / 36.0 },
        model.priorDistribution(ExampleRV.DICE_1_RV, total11)
            .getValues(), DELTA_THRESHOLD);

    EquivalentProposition doubles = new EquivalentProposition("Doubles",
        ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);
    // P(Doubles) = <1.0/6.0>
    Assert.assertArrayEquals(new double[] { 1.0 / 6.0 }, model
        .priorDistribution(doubles).getValues(), DELTA_THRESHOLD);

    //
    // Test posterior
    //
    // P<>(Dice1, Total = 11)
    // = <0.0, 0.0, 0.0, 0.0, 0.5, 0.5>
    Assert.assertArrayEquals(new double[] { 0, 0, 0, 0, 0.5, 0.5 }, model
        .posteriorDistribution(ExampleRV.DICE_1_RV, total11)
        .getValues(), DELTA_THRESHOLD);

    // P<>(Dice1 | Doubles) = <1/6, 1/6, 1/6, 1/6, 1/6, 1/6>
    Assert.assertArrayEquals(new double[] { 1.0 / 6.0, 1.0 / 6.0,
        1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0 }, model
        .posteriorDistribution(ExampleRV.DICE_1_RV, doubles)
        .getValues(), DELTA_THRESHOLD);

    CategoricalDistribution dPosteriorDice1GivenDice2 = model
        .posteriorDistribution(ExampleRV.DICE_1_RV, ExampleRV.DICE_2_RV);
    Assert.assertEquals(36, dPosteriorDice1GivenDice2.getValues().length);
    for (int i = 0; i < dPosteriorDice1GivenDice2.getValues().length; i++) {
      Assert.assertEquals(1.0 / 6.0,
          dPosteriorDice1GivenDice2.getValues()[i], DELTA_THRESHOLD);
    }

    CategoricalDistribution dPosteriorDice2GivenDice1 = model
        .posteriorDistribution(ExampleRV.DICE_2_RV, ExampleRV.DICE_1_RV);
    Assert.assertEquals(36, dPosteriorDice2GivenDice1.getValues().length);
    for (int i = 0; i < dPosteriorDice2GivenDice1.getValues().length; i++) {
      Assert.assertEquals(1.0 / 6.0,
          dPosteriorDice2GivenDice1.getValues()[i], DELTA_THRESHOLD);
    }
  }
View Full Code Here

TOP

Related Classes of aima.core.probability.CategoricalDistribution

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.