/** * Tests if two Dynamic Bayesian networks are equals. * Two Dynamic Bayesian networks are considered equals if they have an equal conditional distribution for each variable at both Time 0 and T. * @param bnet a given DynamicBayesianNetwork to be compared with this DynamicBayesianNetwork. * @param threshold a threshold value. * @return a boolean indicating if the two DynamicBNs are equals or not. */ public boolean equalDBNs(DynamicBayesianNetwork bnet, double threshold) { boolean equals = true; if (this.getDynamicDAG().equals(bnet.getDynamicDAG())){ for (Variable var : this.getDynamicVariables()) { equals = equals && this.getConditionalDistributionTime0(var).equalDist(bnet.getConditionalDistributionTime0(var), threshold) && this.getConditionalDistributionTimeT(var).equalDist(bnet.getConditionalDistributionTimeT(var), threshold); } } return equals; }
public static void baseTest(ExecutionEnvironment env, DataStream<DataInstance> data, BayesianNetwork network, int batchSize, double error) throws IOException, ClassNotFoundException { DataStreamWriter.writeDataToFile(data, "./datasets/simulated/tmp.arff"); DataFlink<DataInstance> dataFlink = DataFlinkLoader.loadDataFromFile(env, "./datasets/simulated/tmp.arff", false); network.getDAG().getVariables().setAttributes(dataFlink.getAttributes()); //Structure learning is excluded from the test, i.e., we use directly the initial Asia network structure // and just learn then test the parameter learning //Parameter Learning dVMP parallelVB = new dVMP(); parallelVB.setOutput(true); parallelVB.setMaximumGlobalIterations(10); parallelVB.setSeed(5); parallelVB.setBatchSize(batchSize); parallelVB.setLocalThreshold(0.001); parallelVB.setGlobalThreshold(0.01); parallelVB.setMaximumLocalIterations(100); parallelVB.setMaximumGlobalIterations(100); parallelVB.setDAG(network.getDAG()); parallelVB.initLearning(); parallelVB.updateModel(dataFlink); BayesianNetwork bnet = parallelVB.getLearntBayesianNetwork(); //Check if the probability distributions of each node for (Variable var : network.getVariables()) { if (Main.VERBOSE) System.out.println("\n------ Variable " + var.getName() + " ------"); if (Main.VERBOSE) System.out.println("\nTrue distribution:\n" + network.getConditionalDistribution(var)); if (Main.VERBOSE) System.out.println("\nLearned distribution:\n" + bnet.getConditionalDistribution(var)); Assert.assertTrue(bnet.getConditionalDistribution(var).equalDist(network.getConditionalDistribution(var), error)); } //Or check directly if the true and learned networks are equals Assert.assertTrue(bnet.equalBNs(network, error)); }