/** * Estimates a Bayes IM using the variables, graph, and parameters in the given Bayes PM and the * data columns in the given data set. Each variable in the given Bayes PM must be equal to a * variable in the given data set. The Bayes IM so estimated is used as the initial Bayes net in * the iterative procedure implemented in the maximize method. */ private void estimateIM(BayesPm bayesPm, DataSet dataSet) { if (bayesPm == null) { throw new NullPointerException(); } if (dataSet == null) { throw new NullPointerException(); } // Make sure all of the variables in the PM are in the data set; // otherwise, estimation is impossible. // List pmvars = bayesPm.getVariables(); // List dsvars = dataSet.getVariables(); // List obsVars = observedIm.getBayesPm().getVariables(); // System.out.println("Bayes PM as received by estimateMixedIM: "); // System.out.println(bayesPm); // Graph g = bayesPm.getDag(); // System.out.println(g); // DEBUG Prints: // System.out.println("PM VARS " + pmvars); // System.out.println("DS VARS " + dsvars); // System.out.println("OBS IM Vars" + obsVars); BayesUtils.ensureVarsInData(bayesPm.getVariables(), dataSet); // DataUtils.ensureVariablesExist(bayesPm, dataSet); // Create a new Bayes IM to store the estimated values. this.estimatedIm = new MlBayesIm(bayesPm, MlBayesIm.RANDOM); int numNodes = estimatedIm.getNumNodes(); for (int node = 0; node < numNodes; node++) { int numRows = estimatedIm.getNumRows(node); int numCols = estimatedIm.getNumColumns(node); int[] parentVarIndices = estimatedIm.getParents(node); if (nodes[node].getNodeType() == NodeType.LATENT) { continue; } // int nodeObsIndex = estimatedIm.getCorrespondingNodeIndex(node, observedIm); // System.out.println("nodes[node] name = " + nodes[node].getName()); Node nodeObs = observedIm.getNode(nodes[node].getName()); // System.out.println("nodeObs name = " + nodeObs.getName()); int nodeObsIndex = observedIm.getNodeIndex(nodeObs); // int[] parentsObs = observedIm.getParents(nodeObsIndex); // System.out.println("For node " + nodes[node] + " parents are: "); boolean anyParentLatent = false; for (int parentVarIndice : parentVarIndices) { // System.out.println(nodes[parentVarIndices[p]]); if (nodes[parentVarIndice].getNodeType() == NodeType.LATENT) { anyParentLatent = true; break; } } if (anyParentLatent) { continue; } // At this point node is measured in bayesPm and so are its parents. for (int row = 0; row < numRows; row++) { // int[] parentValues = estimatedIm.getParentValues(node, row); // estimatedIm.randomizeRow(node, row); // if the node and all its parents are measured get the probs // from observedIm // loop: for (int col = 0; col < numCols; col++) { double p = observedIm.getProbability(nodeObsIndex, row, col); estimatedIm.setProbability(node, row, col, p); } } } }