Package aima.core.probability.util

Examples of aima.core.probability.util.ProbabilityTable


    return new Matrix(values, values.length);
  }

  @Override
  public CategoricalDistribution convert(Matrix fromMessage) {
    return new ProbabilityTable(fromMessage.getRowPackedCopy(),
        stateVariable);
  }
View Full Code Here


    if (null == vars) {
      throw new IllegalArgumentException(
          "Random Variables describing the model's representation of the World need to be specified.");
    }

    distribution = new ProbabilityTable(values, vars);

    representation = new LinkedHashSet<RandomVariable>();
    for (int i = 0; i < vars.length; i++) {
      representation.add(vars[i]);
    }
View Full Code Here

    return dAandB.divideBy(dEvidence);
  }

  public CategoricalDistribution jointDistribution(
      Proposition... propositions) {
    ProbabilityTable d = null;
    final Proposition conjProp = ProbUtil
        .constructConjunction(propositions);
    final LinkedHashSet<RandomVariable> vars = new LinkedHashSet<RandomVariable>(
        conjProp.getUnboundScope());

    if (vars.size() > 0) {
      RandomVariable[] distVars = new RandomVariable[vars.size()];
      vars.toArray(distVars);

      final ProbabilityTable ud = new ProbabilityTable(distVars);
      final Object[] values = new Object[vars.size()];

      ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {

        public void iterate(Map<RandomVariable, Object> possibleWorld,
            double probability) {
          if (conjProp.holds(possibleWorld)) {
            int i = 0;
            for (RandomVariable rv : vars) {
              values[i] = possibleWorld.get(rv);
              i++;
            }
            int dIdx = ud.getIndex(values);
            ud.setValue(dIdx, ud.getValues()[dIdx] + probability);
          }
        }
      };

      distribution.iterateOverTable(di);

      d = ud;
    } else {
      // No Unbound Variables, therefore just return
      // the singular probability related to the proposition.
      d = new ProbabilityTable();
      d.setValue(0, prior(propositions));
    }
    return d;
  }
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);
 
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

  //
  // PRIVATE METHODS
  //
  private CategoricalDistribution initBackwardMessage() {
    ProbabilityTable b = new ProbabilityTable(tToTm1StateVarMap.keySet());

    for (int i = 0; i < b.size(); i++) {
      b.setValue(i, 1.0);
    }

    return b;
  }
View Full Code Here

  public CategoricalDistribution enumerationAsk(final RandomVariable[] X,
      final AssignmentProposition[] observedEvidence,
      final BayesianNetwork bn) {

    // Q(X) <- a distribution over X, initially empty
    final ProbabilityTable Q = new ProbabilityTable(X);
    final ObservedEvidence e = new ObservedEvidence(X, observedEvidence, bn);
    // for each value x<sub>i</sub> of X do
    ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
      int cnt = 0;

      /**
       * <pre>
       * Q(x<sub>i</sub>) <- ENUMERATE-ALL(bn.VARS, e<sub>x<sub>i</sub></sub>)
       *   where e<sub>x<sub>i</sub></sub> is e extended with X = x<sub>i</sub>
       * </pre>
       */
      public void iterate(Map<RandomVariable, Object> possibleWorld,
          double probability) {
        for (int i = 0; i < X.length; i++) {
          e.setExtendedValue(X[i], possibleWorld.get(X[i]));
        }
        Q.setValue(cnt,
            enumerateAll(bn.getVariablesInTopologicalOrder(), e));
        cnt++;
      }
    };
    Q.iterateOverTable(di);

    // return NORMALIZE(Q(X))
    return Q.normalize();
  }
View Full Code Here

    for (int i = 0; i < conditionedOn.length; i++) {
      tableVars[i] = conditionedOn[i];
      parents.add(conditionedOn[i]);
    }
    tableVars[conditionedOn.length] = on;
    table = new ProbabilityTable(values, tableVars);
    onDomain.addAll(((FiniteDomain) on.getDomain()).getPossibleValues());

    checkEachRowTotalsOne();
  }
View Full Code Here

          "The number of parent value arguments ["
              + parentValues.length
              + "] is not equal to the number of parents ["
              + parents.size() + "] for this CPT.");
    }
    final ProbabilityTable cc = new ProbabilityTable(getOn());
    ProbabilityTable.Iterator pti = new ProbabilityTable.Iterator() {
      private int idx = 0;

      @Override
      public void iterate(Map<RandomVariable, Object> possibleAssignment,
          double probability) {
        cc.getValues()[idx] = probability;
        idx++;
      }
    };
    table.iterateOverTable(pti, parentValues);
View Full Code Here

    Set<RandomVariable> fofVars = new LinkedHashSet<RandomVariable>(
        table.getFor());
    for (AssignmentProposition ap : evidence) {
      fofVars.remove(ap.getTermVariable());
    }
    final ProbabilityTable fof = new ProbabilityTable(fofVars);
    // Otherwise need to iterate through the table for the
    // non evidence variables.
    final Object[] termValues = new Object[fofVars.size()];
    ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
      public void iterate(Map<RandomVariable, Object> possibleWorld,
          double probability) {
        if (0 == termValues.length) {
          fof.getValues()[0] += probability;
        } else {
          int i = 0;
          for (RandomVariable rv : fof.getFor()) {
            termValues[i] = possibleWorld.get(rv);
            i++;
          }
          fof.getValues()[fof.getIndex(termValues)] += probability;
        }
      }
    };
    table.iterateOverTable(di, evidence);
View Full Code Here

TOP

Related Classes of aima.core.probability.util.ProbabilityTable

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.