Пример #1
0
  public String toString() {
    StringBuffer buf = new StringBuffer();
    List<Node> variables = getVariableSource().getVariables();

    buf.append("\n");

    for (int i = 0; i < getNumVariables(); i++) {
      DiscreteVariable variable = (DiscreteVariable) variables.get(i);
      String name = variable.getName();
      buf.append(name);

      for (int j = name.length(); j < 5; j++) {
        buf.append(" ");
      }

      buf.append("\t");
    }

    for (int i = 0; i < getMaxNumCategories(); i++) {
      buf.append("\n");

      for (int j = 0; j < getNumVariables(); j++) {
        if (i < getNumCategories(j)) {
          boolean allowed = isAllowed(j, i);
          buf.append(allowed ? "true" : "*   ").append("\t");
        } else {
          buf.append("    \t");
        }
      }
    }

    return buf.toString();
  }
Пример #2
0
  private String reportIfDiscrete(Graph dag, DataSet dataSet) {
    List vars = dataSet.getVariables();
    Map<String, DiscreteVariable> nodesToVars = new HashMap<String, DiscreteVariable>();
    for (int i = 0; i < dataSet.getNumColumns(); i++) {
      DiscreteVariable var = (DiscreteVariable) vars.get(i);
      String name = var.getName();
      Node node = new GraphNode(name);
      nodesToVars.put(node.getName(), var);
    }

    BayesPm bayesPm = new BayesPm(new Dag(dag));
    List<Node> nodes = bayesPm.getDag().getNodes();

    for (Node node : nodes) {
      Node var = nodesToVars.get(node.getName());

      if (var instanceof DiscreteVariable) {
        DiscreteVariable var2 = nodesToVars.get(node.getName());
        int numCategories = var2.getNumCategories();
        List<String> categories = new ArrayList<String>();
        for (int j = 0; j < numCategories; j++) {
          categories.add(var2.getCategory(j));
        }
        bayesPm.setCategories(node, categories);
      }
    }

    BayesProperties properties = new BayesProperties(dataSet, dag);
    properties.setGraph(dag);

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(4);

    StringBuilder buf = new StringBuilder();
    buf.append("\nP-value = ").append(properties.getLikelihoodRatioP());
    buf.append("\nDf = ").append(properties.getPValueDf());
    buf.append("\nChi square = ").append(nf.format(properties.getPValueChisq()));
    buf.append("\nBIC score = ").append(nf.format(properties.getBic()));
    buf.append("\n\nH0: Completely disconnected graph.");

    return buf.toString();
  }
  /**
   * This method takes an instantiated Bayes net (BayesIm) whose graph include all the variables
   * (observed and latent) and computes estimated counts using the data in the DataSet mixedData.
   * The counts that are estimated correspond to cells in the conditional probability tables of the
   * Bayes net. The outermost loop (indexed by j) is over the set of variables. If the variable has
   * no parents, each case in the dataset is examined and the count for the observed value of the
   * variables is increased by 1.0; if the value of the variable is missing the marginal
   * probabilities its values given the values of the variables that are available for that case are
   * used to increment the corresponding estimated counts. If a variable has parents then there is a
   * loop which steps through all possible sets of values of its parents. This loop is indexed by
   * the variable "row". Each case in the dataset is examined. It the variable and all its parents
   * have values in the case the corresponding estimated counts are incremented by 1.0. If the
   * variable or any of its parents have missing values, the joint marginal is computed for the
   * variable and the set of values of its parents corresponding to "row" and the corresponding
   * estimated counts are incremented by the appropriate probability. The estimated counts are
   * stored in the double[][][] array estimatedCounts. The count (possibly fractional) of the number
   * of times each combination of parent values occurs is stored in the double[][] array
   * estimatedCountsDenom. These two arrays are used to compute the estimated conditional
   * probabilities of the output Bayes net.
   */
  private BayesIm expectation(BayesIm inputBayesIm) {
    // System.out.println("Entered method expectation.");

    int numCases = mixedData.getNumRows();
    // StoredCellEstCounts estCounts = new StoredCellEstCounts(variables);

    int numVariables = allVariables.size();
    RowSummingExactUpdater rseu = new RowSummingExactUpdater(inputBayesIm);

    for (int j = 0; j < numVariables; j++) {
      DiscreteVariable var = (DiscreteVariable) allVariables.get(j);
      String varName = var.getName();
      Node varNode = graph.getNode(varName);
      int varIndex = inputBayesIm.getNodeIndex(varNode);
      int[] parentVarIndices = inputBayesIm.getParents(varIndex);
      // System.out.println("graph = " + graph);

      // for(int col = 0; col < var.getNumSplits(); col++)
      //    System.out.println("Category " + col + " = " + var.getCategory(col));

      // System.out.println("Updating estimated counts for node " + varName);
      // This segment is for variables with no parents:
      if (parentVarIndices.length == 0) {
        // System.out.println("No parents");
        for (int col = 0; col < var.getNumCategories(); col++) {
          estimatedCounts[j][0][col] = 0.0;
        }

        for (int i = 0; i < numCases; i++) {
          // System.out.println("Case " + i);
          // If this case has a value for var
          if (mixedData.getInt(i, j) != -99) {
            estimatedCounts[j][0][mixedData.getInt(i, j)] += 1.0;
            // System.out.println("Adding 1.0 to " + varName +
            //        " row 0 category " + mixedData[j][i]);
          } else {
            // find marginal probability, given obs data in this case, p(v=0)
            Evidence evidenceThisCase = Evidence.tautology(inputBayesIm);
            boolean existsEvidence = false;

            // Define evidence for updating by using the values of the other vars.
            for (int k = 0; k < numVariables; k++) {
              if (k == j) {
                continue;
              }
              Node otherVar = allVariables.get(k);
              if (mixedData.getInt(i, k) == -99) {
                continue;
              }
              existsEvidence = true;
              String otherVarName = otherVar.getName();
              Node otherNode = graph.getNode(otherVarName);
              int otherIndex = inputBayesIm.getNodeIndex(otherNode);

              evidenceThisCase.getProposition().setCategory(otherIndex, mixedData.getInt(i, k));
            }

            if (!existsEvidence) {
              continue; // No other variable contained useful data
            }

            rseu.setEvidence(evidenceThisCase);

            for (int m = 0; m < var.getNumCategories(); m++) {
              estimatedCounts[j][0][m] += rseu.getMarginal(varIndex, m);
              // System.out.println("Adding " + p + " to " + varName +
              //        " row 0 category " + m);

              // find marginal probability, given obs data in this case, p(v=1)
              // estimatedCounts[j][0][1] += 0.5;
            }
          }
        }

        // Print estimated counts:
        // System.out.println("Estimated counts:  ");

        // Print counts for each value of this variable with no parents.
        // for(int m = 0; m < var.getNumSplits(); m++)
        //    System.out.print("    " + m + " " + estimatedCounts[j][0][m]);
        // System.out.println();
      } else { // For variables with parents:
        int numRows = inputBayesIm.getNumRows(varIndex);
        for (int row = 0; row < numRows; row++) {
          int[] parValues = inputBayesIm.getParentValues(varIndex, row);
          estimatedCountsDenom[varIndex][row] = 0.0;
          for (int col = 0; col < var.getNumCategories(); col++) {
            estimatedCounts[varIndex][row][col] = 0.0;
          }

          for (int i = 0; i < numCases; i++) {
            // for a case where the parent values = parValues increment the estCount

            boolean parentMatch = true;

            for (int p = 0; p < parentVarIndices.length; p++) {
              if (parValues[p] != mixedData.getInt(i, parentVarIndices[p])
                  && mixedData.getInt(i, parentVarIndices[p]) != -99) {
                parentMatch = false;
                break;
              }
            }

            if (!parentMatch) {
              continue; // Not a matching case; go to next.
            }

            boolean parentMissing = false;
            for (int parentVarIndice : parentVarIndices) {
              if (mixedData.getInt(i, parentVarIndice) == -99) {
                parentMissing = true;
                break;
              }
            }

            if (mixedData.getInt(i, j) != -99 && !parentMissing) {
              estimatedCounts[j][row][mixedData.getInt(i, j)] += 1.0;
              estimatedCountsDenom[j][row] += 1.0;
              continue; // Next case
            }

            // for a case with missing data (either var or one of its parents)
            // compute the joint marginal
            // distribution for var & this combination of values of its parents
            // and update the estCounts accordingly

            // To compute marginals create the evidence
            boolean existsEvidence = false;

            Evidence evidenceThisCase = Evidence.tautology(inputBayesIm);

            // "evidenceVars" not used.
            //                        List<String> evidenceVars = new LinkedList<String>();
            //                        for (int k = 0; k < numVariables; k++) {
            //                            //if(k == j) continue;
            //                            Variable otherVar = allVariables.get(k);
            //                            if (mixedData.getInt(i, k) == -99) {
            //                                continue;
            //                            }
            //                            existsEvidence = true;
            //                            String otherVarName = otherVar.getName();
            //                            Node otherNode = graph.getNode(otherVarName);
            //                            int otherIndex = inputBayesIm.getNodeIndex(
            //                                    otherNode);
            //                            evidenceThisCase.getProposition().setCategory(
            //                                    otherIndex, mixedData.getInt(i, k));
            //                            evidenceVars.add(otherVarName);
            //                        }

            if (!existsEvidence) {
              continue;
            }

            rseu.setEvidence(evidenceThisCase);

            estimatedCountsDenom[j][row] += rseu.getJointMarginal(parentVarIndices, parValues);

            int[] parPlusChildIndices = new int[parentVarIndices.length + 1];
            int[] parPlusChildValues = new int[parentVarIndices.length + 1];

            parPlusChildIndices[0] = varIndex;
            for (int pc = 1; pc < parPlusChildIndices.length; pc++) {
              parPlusChildIndices[pc] = parentVarIndices[pc - 1];
              parPlusChildValues[pc] = parValues[pc - 1];
            }

            for (int m = 0; m < var.getNumCategories(); m++) {

              parPlusChildValues[0] = m;

              /*
              if(varName.equals("X1") && i == 0 ) {
                  System.out.println("Calling getJointMarginal with parvalues");
                  for(int k = 0; k < parPlusChildIndices.length; k++) {
                      int pIndex = parPlusChildIndices[k];
                      Node pNode = inputBayesIm.getNode(pIndex);
                      String pName = pNode.getName();
                      System.out.println(pName + " " + parPlusChildValues[k]);
                  }
              }
              */

              /*
              if(varName.equals("X1") && i == 0 ) {
                  System.out.println("Evidence = " + evidenceThisCase);
                  //int[] vars = {l1Index, x1Index};
                  Node nodex1 = inputBayesIm.getNode("X1");
                  int x1Index = inputBayesIm.getNodeIndex(nodex1);
                  Node nodel1 = inputBayesIm.getNode("L1");
                  int l1Index = inputBayesIm.getNodeIndex(nodel1);

                  int[] vars = {l1Index, x1Index};
                  int[] vals = {0, 0};
                  double ptest = rseu.getJointMarginal(vars, vals);
                  System.out.println("Joint marginal (X1=0, L1 = 0) = " + p);
              }
              */

              estimatedCounts[j][row][m] +=
                  rseu.getJointMarginal(parPlusChildIndices, parPlusChildValues);

              // System.out.println("Case " + i + " parent values ");
              // for (int pp = 0; pp < parentVarIndices.length; pp++) {
              //    Variable par = (Variable) allVariables.get(parentVarIndices[pp]);
              //    System.out.print("    " + par.getName() + " " + parValues[pp]);
              // }

              // System.out.println();
              // System.out.println("Adding " + p + " to " + varName +
              //        " row " + row + " category " + m);

            }
            // }
          }

          // Print estimated counts:
          // System.out.println("Estimated counts:  ");
          // System.out.println("    Parent values:  ");
          // for (int i = 0; i < parentVarIndices.length; i++) {
          //    Variable par = (Variable) allVariables.get(parentVarIndices[i]);
          //    System.out.print("    " + par.getName() + " " + parValues[i] + "    ");
          // }
          // System.out.println();

          // for(int m = 0; m < var.getNumSplits(); m++)
          //    System.out.print("    " + m + " " + estimatedCounts[j][row][m]);
          // System.out.println();

        }
      } // else
    } // j < numVariables

    BayesIm outputBayesIm = new MlBayesIm(bayesPm);

    for (int j = 0; j < nodes.length; j++) {

      DiscreteVariable var = (DiscreteVariable) allVariables.get(j);
      String varName = var.getName();
      Node varNode = graph.getNode(varName);
      int varIndex = inputBayesIm.getNodeIndex(varNode);
      //            int[] parentVarIndices = inputBayesIm.getParents(varIndex);

      int numRows = inputBayesIm.getNumRows(j);
      // System.out.println("Conditional probabilities for variable " + varName);

      int numCols = inputBayesIm.getNumColumns(j);
      if (numRows == 1) {
        double sum = 0.0;
        for (int m = 0; m < numCols; m++) {
          sum += estimatedCounts[j][0][m];
        }

        for (int m = 0; m < numCols; m++) {
          condProbs[j][0][m] = estimatedCounts[j][0][m] / sum;
          // System.out.print("  " + condProbs[j][0][m]);
          outputBayesIm.setProbability(varIndex, 0, m, condProbs[j][0][m]);
        }
        // System.out.println();
      } else {

        for (int row = 0; row < numRows; row++) {
          //                    int[] parValues = inputBayesIm.getParentValues(varIndex,
          //                            row);
          // int numCols = inputBayesIm.getNumColumns(j);

          // for (int p = 0; p < parentVarIndices.length; p++) {
          //    Variable par = (Variable) allVariables.get(parentVarIndices[p]);
          //    System.out.print("    " + par.getName() + " " + parValues[p]);
          // }

          // double sum = 0.0;
          // for(int m = 0; m < numCols; m++)
          //    sum += estimatedCounts[j][row][m];

          for (int m = 0; m < numCols; m++) {
            if (estimatedCountsDenom[j][row] != 0.0) {
              condProbs[j][row][m] = estimatedCounts[j][row][m] / estimatedCountsDenom[j][row];
            } else {
              condProbs[j][row][m] = Double.NaN;
            }
            // System.out.print("  " + condProbs[j][row][m]);
            outputBayesIm.setProbability(varIndex, row, m, condProbs[j][row][m]);
          }
          // System.out.println();

        }
      }
    }

    return outputBayesIm;
  }
Пример #4
0
  public final DataSet filter(DataSet dataSet) {

    // Why does it have to be discrete? Why can't we simply expand
    // whatever discrete columns are there and leave the continuous
    // ones untouched? jdramsey 7/4/2005
    //        if (!(dataSet.isDiscrete())) {
    //            throw new IllegalArgumentException("Data set must be discrete.");
    //        }

    List<Node> variables = new LinkedList<>();

    // Add all of the variables to the new data set.
    for (int j = 0; j < dataSet.getNumColumns(); j++) {
      Node _var = dataSet.getVariable(j);

      if (!(_var instanceof DiscreteVariable)) {
        variables.add(_var);
        continue;
      }

      DiscreteVariable variable = (DiscreteVariable) _var;

      String oldName = variable.getName();
      List<String> oldCategories = variable.getCategories();
      List<String> newCategories = new LinkedList<>(oldCategories);

      String newCategory = "Missing";
      int _j = 0;

      while (oldCategories.contains(newCategory)) {
        newCategory = "Missing" + (++_j);
      }

      newCategories.add(newCategory);
      String newName = oldName + "+";
      DiscreteVariable newVariable = new DiscreteVariable(newName, newCategories);

      variables.add(newVariable);
    }

    DataSet newDataSet = new ColtDataSet(dataSet.getNumRows(), variables);

    // Copy old values to new data set, replacing missing values with new
    // "MissingValue" categories.
    for (int j = 0; j < dataSet.getNumColumns(); j++) {
      Node _var = dataSet.getVariable(j);

      if (_var instanceof ContinuousVariable) {
        for (int i = 0; i < dataSet.getNumRows(); i++) {
          newDataSet.setDouble(i, j, dataSet.getDouble(i, j));
        }
      } else if (_var instanceof DiscreteVariable) {
        DiscreteVariable variable = (DiscreteVariable) _var;
        int numCategories = variable.getNumCategories();

        for (int i = 0; i < dataSet.getNumRows(); i++) {
          int value = dataSet.getInt(i, j);

          if (value == DiscreteVariable.MISSING_VALUE) {
            newDataSet.setInt(i, j, numCategories);
          } else {
            newDataSet.setInt(i, j, value);
          }
        }
      }
    }

    return newDataSet;
  }