public static void main(String[] args) throws Exception { //We can load a Bayesian network using the static class BayesianNetworkLoader BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn"); //Now we print the loaded model System.out.println(bn.toString()); //Now we change the parameters of the model bn.randomInitialization(new Random(0)); //We can save this Bayesian network to using the static class BayesianNetworkWriter BayesianNetworkWriter.save(bn, "networks/simulated/tmp.bn"); } }
public static void main(String[] args) throws Exception { // load the true Bayesian network BayesianNetwork originalBnet = BayesianNetworkLoader.loadFromFile(args[0]); System.out.println("\n Network \n " + args[0]); System.out.println("\n Number of variables \n " + originalBnet.getDAG().getVariables().getNumberOfVars()); //Sampling from the input BN BayesianNetworkSampler sampler = new BayesianNetworkSampler(originalBnet); sampler.setSeed(0); // Defines the size of the data to be generated from the input BN int sizeData = Integer.parseInt(args[1]); System.out.println("\n Sampling and saving the data... \n "); DataStream<DataInstance> data = sampler.sampleToDataStream(sizeData); DataStreamWriter.writeDataToFile(data, "./data.arff"); }
public static void main(String[] agrs) throws IOException, ClassNotFoundException { //We first load the WasteIncinerator bayesian network which has multinomial and Gaussian variables. BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn"); //We simply create an BayesianNetworkSampler object, passing to the constructor the BN model. BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); sampler.setSeed(0); //The method sampleToDataStream returns a DataStream with ten DataInstance objects. DataStream<DataInstance> dataStream = sampler.sampleToDataStream(10); //We finally save the sampled data set to a arff file. DataStreamWriter.writeDataToFile(dataStream, "datasets/simulated/sample-WasteIncinerator.arff"); } }
BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("networks/dataWeka/Pigs.bn"); BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn);
public static void demoPigs() throws IOException, ClassNotFoundException { //It needs GBs, so avoid putting this file in a Dropbox folder!! //String dataFile = new String("/Users/afa/Pigs.arff"); BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("networks/dataWeka/Pigs.bn"); int sampleSize = 10000; BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); ArrayList<Integer> vSamplesOnMemory = new ArrayList(Arrays.asList(5000)); ArrayList<Integer> vNumCores = new ArrayList(Arrays.asList(1, 2, 3, 4)); for (Integer samplesOnMemory : vSamplesOnMemory) { for (Integer numCores : vNumCores) { System.out.println("Learning TAN: " + samplesOnMemory + " samples on memory, " + numCores + " core/s ..."); DataStream<DataInstance> data = sampler.sampleToDataStream(sampleSize); ParallelTAN tan = new ParallelTAN(); tan.setNumCores(numCores); tan.setNumSamplesOnMemory(samplesOnMemory); tan.setNameRoot(bn.getVariables().getListOfVariables().get(0).getName()); tan.setNameTarget(bn.getVariables().getListOfVariables().get(1).getName()); Stopwatch watch = Stopwatch.createStarted(); BayesianNetwork model = tan.learn(data); System.out.println(watch.stop()); } } }
public static void main(String[] args) throws Exception { //We first load the WasteIncinerator bayesian network which has multinomial and Gaussian variables. BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn"); //We recover the relevant variables for this example: Mout which is normally distributed, and W which is multinomial. Variable varMout = bn.getVariables().getVariableByName("Mout"); Variable varW = bn.getVariables().getVariableByName("W"); //Set the evidence. Assignment assignment = new HashMapAssignment(1); assignment.setValue(varW,0); //Then we query the posterior of System.out.println("P(Mout|W=0) = " + InferenceEngine.getPosterior(varMout, bn, assignment)); //Or some more refined queries System.out.println("P(0.7<Mout<6.59 | W=0) = " + InferenceEngine.getExpectedValue(varMout, bn, v -> (0.7 < v && v < 6.59) ? 1.0 : 0.0 )); }
public static void main(String[] args) throws Exception { //Setting up spark SparkConf conf = new SparkConf().setAppName("SparkLink!").setMaster("local"); JavaSparkContext jsc = new JavaSparkContext(conf); SQLContext sqlContext = new SQLContext(jsc); // Open a Bayesian network String file = "networks/simulated/WasteIncinerator.bn"; BayesianNetwork bn = BayesianNetworkLoader.loadFromFile(file); System.out.println(bn); // Sample from the BN int nSamples = 1000; int parallelism = 4; BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); DataSpark data = sampler.sampleToDataSpark(jsc, nSamples, parallelism); // Save it as a json file DataSparkWriter.writeDataToFolder(data, "datasets/simulated/WI_samples.json", sqlContext); //DataSparkWriter.writeDataToFolder(data, "datasets/simulated/WI_samples.parquet", sqlContext); }
public static void main(String[] args) throws Exception { BayesianNetwork originalBnet = BayesianNetworkLoader.loadFromFile(args[0]); System.out.println("\n Network \n " + args[0]);
public static void main(String[] args) throws Exception { int sampleSize = 10000; BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("networks/simulated/asia.bn"); System.out.println(bn); BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); DataStream<DataInstance> data = sampler.sampleToDataStream(sampleSize); ParallelTAN tan = new ParallelTAN(); tan.setNumCores(4); tan.setNumSamplesOnMemory(1000); tan.setNameRoot("X"); tan.setNameTarget("E"); BayesianNetwork model = tan.learn(data); System.out.println(model.toString()); } }
public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn");
public static void main(String[] args) throws IOException, ClassNotFoundException { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("networks/simulated/exampleBN.bn"); Variables variables = bn.getVariables(); //Variabeles of interest Variable varTarget = variables.getVariableByName("LatentVar1"); Variable varObserved = null; //we set the evidence Assignment assignment = new HashMapAssignment(2); varObserved = variables.getVariableByName("Income"); assignment.setValue(varObserved,0.0); //we set the algorithm InferenceAlgorithm infer = new VMP(); //new HuginInference(); new ImportanceSampling(); infer.setModel(bn); infer.setEvidence(assignment); //query infer.runInference(); Distribution p = infer.getPosterior(varTarget); System.out.println("P(LatentVar1|Income=0.0) = "+p); //Or some more refined queries System.out.println("P(0.7<LatentVar1<6.59 |Income=0.0) = " + infer.getExpectedValue(varTarget, v -> (0.7 < v && v < 6.59) ? 1.0 : 0.0 )); }
public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn");
public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn"); List<Variable> topologicalOrder = Utils.getTopologicalOrder(bn.getDAG());
public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn");
public static void main(String[] args) throws IOException, ClassNotFoundException { BayesianNetwork oneNormalVarBN = BayesianNetworkLoader.loadFromFile("./networks/simulated/Normal.bn");
public static void main(String[] args) throws IOException, ClassNotFoundException { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn");
public static void main(String[] args) throws Exception { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn"); List<Variable> topologicalOrder = Utils.getTopologicalOrder(bn.getDAG());
BayesianNetwork asianet = BayesianNetworkLoader.loadFromFile("./networks/dataWeka/asia.bn");
public static void main(String[] args) throws Exception { String[] bns = {"./networks/simulated/Normal.bn", "./networks/simulated/Normal_1NormalParents.bn", "./networks/simulated/Normal_NormalParents.bn"}; for (String bnname : bns) { BayesianNetwork bn = BayesianNetworkLoader.loadFromFile(bnname); BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); sampler.setSeed(2); DataStream<DataInstance> data = sampler.sampleToDataStream(10000); //Set-up Flink session. Configuration conf = new Configuration(); conf.setInteger("taskmanager.network.numberOfBuffers", 12000); final ExecutionEnvironment env = ExecutionEnvironment.createLocalEnvironment(conf); env.getConfig().disableSysoutLogging(); env.setParallelism(Main.PARALLELISM); baseTest(env, data, bn, 1000, 0.1); } } }
public static void main(String[] args) throws Exception { //Load the learnt model BayesianNetwork fireDetector = BayesianNetworkLoader.loadFromFile("./models/LearntFireDetectorModel.bn"); //Access the variable of interest. Variable fire = fireDetector.getVariables().getVariableByName("Fire"); Variable temperature = fireDetector.getVariables().getVariableByName("Temperature"); Variable smoke = fireDetector.getVariables().getVariableByName("Smoke"); //Modify the parameters of the model according to our prior knowledge. Multinomial fireprob = fireDetector.getConditionalDistribution(fire); fireprob.setProbabilities(new double[]{0.999, 0.001}); Normal_MultinomialParents tempprob = fireDetector.getConditionalDistribution(temperature); tempprob.getNormal(1).setMean(tempprob.getNormal(0).getMean()+10); tempprob.getNormal(1).setVariance(tempprob.getNormal(0).getVariance()); Multinomial_MultinomialParents smokeProb = fireDetector.getConditionalDistribution(smoke); smokeProb.getMultinomial(1).setProbabilities(new double[]{0.001, 0.999}); //Print the model System.out.println(fireDetector); //Save to disk the new model BayesianNetworkWriter.save(fireDetector,"./models/FireDetectorModel.bn"); } }