/* * Eliminates all variables defined as evidence. The order of the variables * that are not eliminated is the same order in the original function. */ private ProbabilityFunction check_evidence(ProbabilityFunction pf) { int i, j, k, v, aux_i; boolean markers[] = new boolean[bn.number_variables()]; int n = build_evidence_markers(pf, markers); // Handle special cases if (n == 0) return (null); // No variable remains if (n == pf.number_variables()) return (pf); // No relevant evidence // Calculate necessary quantities in such a // way that the order of variables in the original // function is not altered. int joined_indexes[] = new int[n]; for (i = 0, j = 0, v = 1; i < pf.number_variables(); i++) { aux_i = pf.get_variable(i).get_index(); if (markers[aux_i] == true) { joined_indexes[j] = aux_i; j++; v *= bn.get_probability_variable(aux_i).number_values(); } } // Create new function to be filled with joined variables ProbabilityFunction new_pf = new ProbabilityFunction(bn, n, v, null); for (i = 0; i < n; i++) new_pf.set_variable(i, bn.get_probability_variable(joined_indexes[i])); // Loop through the values check_evidence_loop(new_pf, pf); return (new_pf); }
/* * Transform an observed ProbabilityVariable into a ProbabilityFunction to * handle the case where the query involves an observed variable. */ private ProbabilityFunction transform_to_probability_function( BayesNet bn, ProbabilityVariable pv) { ProbabilityFunction pf = new ProbabilityFunction(bn, 1, pv.number_values(), null); pf.set_variable(0, pv); int index_of_value = pv.get_observed_index(); pf.set_value(index_of_value, 1.0); return (pf); }