Package aima.core.probability.util

Examples of aima.core.probability.util.ProbabilityTable


    return rVal;
  }

  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()];
      int i = 0;
      for (RandomVariable rv : vars) {
        distVars[i] = rv;
        i++;
      }

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

      CategoricalDistribution.Iterator di = new CategoricalDistribution.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);
          }
        }
      };

      RandomVariable[] X = conjProp.getScope().toArray(
          new RandomVariable[conjProp.getScope().size()]);
      bayesInference.ask(X, new AssignmentProposition[0], bayesNet)
          .iterateOver(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


    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);
          }
        }

        public Object getPostIterateValue() {
          return null; // N/A
        }
      };

      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

      // <b>N</b>[x] <- <b>N</b>[x] + 1
      // where x is the value of X in <b>x</b>
      N[ProbUtil.indexOf(X, x)] += 1.0;
    }
    // return NORMALIZE(<b>N</b>)
    return new ProbabilityTable(N, X).normalize();
  }
View Full Code Here

        // where x is the value of X in <b>x</b>
        N[ProbUtil.indexOf(X, x)] += 1.0;
      }
    }
    // return NORMALIZE(<b>N</b>)
    return new ProbabilityTable(N, X).normalize();
  }
View Full Code Here

          e);
      // W[x] <- W[x] + w where x is the value of X in <b>x</b>
      W[ProbUtil.indexOf(X, x_w.getFirst())] += x_w.getSecond();
    }
    // return NORMALIZE(W)
    return new ProbabilityTable(W, X).normalize();
  }
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++;
      }

      public Object getPostIterateValue() {
        return null; // N/A
      }
    };
    Q.iterateOverTable(di);

    // return NORMALIZE(Q(X))
    return Q.normalize();
  }
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()]);
    ProbabilityTable d = (ProbabilityTable) bayesInference.ask(X,
        new AssignmentProposition[0], bayesNet);

    // Then calculate the probability of the propositions phi
    // be seeing where they hold.
    ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
      private double probSum = 0;

      public void iterate(Map<RandomVariable, Object> possibleWorld,
          double probability) {
        if (conjunct.holds(possibleWorld)) {
          probSum += probability;
        }
      }

      public Object getPostIterateValue() {
        return probSum;
      }
    };
    d.iterateOverTable(di);

    return ((Double) di.getPostIterateValue()).doubleValue();
  }
View Full Code Here

    return rVal;
  }

  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()];
      int i = 0;
      for (RandomVariable rv : vars) {
        distVars[i] = rv;
        i++;
      }

      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);
          }
        }

        public Object getPostIterateValue() {
          return null; // N/A
        }
      };

      RandomVariable[] X = conjProp.getScope().toArray(
          new RandomVariable[conjProp.getScope().size()]);
      ((ProbabilityTable) bayesInference.ask(X,
          new AssignmentProposition[0], bayesNet))
          .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

    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

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.