/** Copies the info out of the old proposition into a new proposition for the new BayesIm. */
  public Proposition(VariableSource variableSource, Proposition proposition) {
    this(variableSource);

    if (proposition == null) {
      throw new NullPointerException();
    }

    List<Node> variables = variableSource.getVariables();
    List<Node> oldVariables = proposition.getVariableSource().getVariables();

    for (int i = 0; i < variables.size(); i++) {
      DiscreteVariable variable = (DiscreteVariable) variables.get(i);
      int oldIndex = -1;

      for (int j = 0; j < oldVariables.size(); j++) {
        DiscreteVariable _variable = (DiscreteVariable) oldVariables.get(j);
        if (variable.equals(_variable)) {
          oldIndex = j;
          break;
        }
      }

      if (oldIndex != -1) {
        for (int j = 0; j < allowedCategories[i].length; j++) {
          allowedCategories[i][j] = proposition.isAllowed(oldIndex, j);
        }
      }
    }
  }
Esempio n. 2
0
  /**
   * @return the estimated conditional probability for the given assertion conditional on the given
   *     condition.
   */
  public double getConditionalProb(Proposition assertion, Proposition condition) {
    if (assertion.getVariableSource() != condition.getVariableSource()) {
      throw new IllegalArgumentException(
          "Assertion and condition must be " + "for the same Bayes IM.");
    }

    List<Node> assertionVars = assertion.getVariableSource().getVariables();
    List<Node> dataVars = dataSet.getVariables();

    assertionVars = GraphUtils.replaceNodes(assertionVars, dataVars);

    if (!new HashSet<Node>(assertionVars).equals(new HashSet<Node>(dataVars))) {
      throw new IllegalArgumentException(
          "Assertion variable and data variables"
              + " are either different or in a different order: "
              + "\n\tAssertion vars: "
              + assertionVars
              + "\n\tData vars: "
              + dataVars);
    }

    int[] point = new int[dims.length];
    int count1 = 0;
    int count2 = 0;
    this.missingValueCaseFound = false;

    point:
    for (int i = 0; i < numRows; i++) {
      for (int j = 0; j < dims.length; j++) {
        point[j] = dataSet.getInt(i, j);

        if (point[j] == DiscreteVariable.MISSING_VALUE) {
          continue point;
        }
      }

      if (condition.isPermissibleCombination(point)) {
        count1++;

        if (assertion.isPermissibleCombination(point)) {
          count2++;
        }
      }
    }

    return count2 / (double) count1;
  }
  /**
   * Restricts this proposition to the categories for the given variable that are true in the given
   * proposition.
   */
  public void restrictToProposition(Proposition proposition, int variable) {
    if (proposition.getVariableSource() != this.variableSource) {
      throw new IllegalArgumentException(
          "Can only restrict to " + "propositions for the same variable source.");
    }

    for (int j = 0; j < allowedCategories[variable].length; j++) {
      if (!proposition.isAllowed(variable, j)) {
        removeCategory(variable, j);
      }
    }
  }
  /**
   * Restricts this proposition to the categories for each variable that are true in the given
   * proposition.
   */
  public void restrictToProposition(Proposition proposition) {
    if (proposition.getVariableSource() != this.variableSource) {
      throw new IllegalArgumentException(
          "Can only restrict to " + "propositions for the same variable source.");
    }

    for (int i = 0; i < allowedCategories.length; i++) {
      for (int j = 0; j < allowedCategories[i].length; j++) {
        if (!proposition.allowedCategories[i][j]) {
          this.allowedCategories[i][j] = false;
        }
      }
    }
  }