/** * Builds the DAG over the set of variables given with the structure of the model */ @Override protected void buildDAG() { dag = new DAG(vars); // add the links between the attributes (features) List<Variable> attrVars = vars.getListOfVariables(); for (int i=0; i<attrVars.size()-1; i++){ for(int j=i+1; j<attrVars.size(); j++) { // Add the links dag.getParentSet(attrVars.get(i)).addParent(attrVars.get(j)); } } }
/** * Builds the DAG over the set of variables given with the naive Bayes structure */ @Override protected void buildDAG() { //We create a standard naive Bayes dag = new DAG(vars); dag.getParentSets().stream().filter(w -> !w.getMainVar().equals(classVar)).forEach(w -> w.addParent(classVar)); // if it is not diagonal add the links between the attributes (features) if(!isDiagonal()) { List<Variable> attrVars = vars.getListOfVariables().stream().filter(v -> !v.equals(classVar)).collect(Collectors.toList()); for (int i=0; i<attrVars.size()-1; i++){ for(int j=i+1; j<attrVars.size(); j++) { // Add the links dag.getParentSet(attrVars.get(i)).addParent(attrVars.get(j)); } } } } /*
/** * This method contains the code needed to build the NaiveBayes DAG with a global hidden variable modelling * concept drift. * @return A poperly created {@link DAG} object. * @throws Exception if an error occurs while reading the file. */ public static DAG modelBuilding() throws Exception { //We load the data for one month DataStream<DataInstance> instances = DataStreamLoader.open("./datasets/bnaic2015/BCC/Month0.arff"); //Define the variables. By default, a random variable is created for each attribute Variables variables = new Variables(instances.getAttributes()); //We create a new global hidden Gaussian variable Variable hiddenGaussian = variables.newGaussianVariable("HiddenGaussian"); //We get the variable Default Variable defaultVariable = variables.getVariableByName("default"); //We define the DAG DAG dag = new DAG(variables); //We add the links of the DAG dag.getVariables() .getListOfVariables() .stream() .filter(var -> var != defaultVariable) .filter(var -> var != hiddenGaussian) .forEach(var -> { dag.getParentSet(var).addParent(defaultVariable); dag.getParentSet(var).addParent(hiddenGaussian); }); return dag; }
/** * Builds the DAG over the set of variables given with the naive Bayes structure */ @Override protected void buildDAG() { dag = new DAG(vars); dag.getParentSets().stream().filter(w -> !w.getMainVar().equals(classVar)).forEach(w -> w.addParent(classVar)); }
dag = new DAG(variables); dag.getParentSets().stream() .filter(parentSet -> !parentSet.getMainVar().equals(classVar) && !parentSet.getMainVar().getName().startsWith("GaussianSPVar_") && !parentSet.getMainVar().getName().startsWith("DiscreteSPVar")) .forEach(hv -> dag.getParentSet(variables.getVariableByName("GaussianSPVar_" + hv)).addParent(classVar)); dag.getParentSet(variables.getVariableByName("DiscreteSPVar")).addParent(classVar); System.out.println(dag.toString());
/** * Returns the DAG at time 0 of this DynamicDAG. * @return a {@link DAG} object. */ public DAG toDAGTime0(){ Variables staticVariables = Variables.auxiliarBuilder(this.getDynamicVariables().getListOfDynamicVariables()); DAG dag = new DAG(staticVariables); dag.setName(this.getName()); for (Variable dynamicVariable : dynamicVariables) { for (Variable parent : this.getParentSetTime0(dynamicVariable)) { dag.getParentSet(dynamicVariable).addParent(parent); } } return dag; }
/** * Builds the DAG structure of a Naive Bayes classifier with a global hidden Gaussian variable. */ private void buildGlobalDAG(){ Variables variables = new Variables(attributes); String className = attributes.getFullListOfAttributes().get(classIndex).getName(); hiddenVars = new ArrayList<Variable>(); for (int i = 0; i < this.numberOfGlobalVars ; i++) { hiddenVars.add(variables.newGaussianVariable("GlobalHidden_"+i)); } Variable classVariable = variables.getVariableByName(className); this.globalDAG = new DAG(variables); for (Attribute att : attributes.getListOfNonSpecialAttributes()) { if (att.getName().equals(className)) continue; Variable variable = variables.getVariableByName(att.getName()); globalDAG.getParentSet(variable).addParent(classVariable); for (int i = 0; i < this.numberOfGlobalVars ; i++) { globalDAG.getParentSet(variable).addParent(hiddenVars.get(i)); } } System.out.println(globalDAG.toString()); }
dagLDA = new DAG(variables); dagLDA.setName("LDA"); dagLDA.getParentSet(word).addParent(topicIndicator); List<EF_ConditionalDistribution> dists = dagLDA.getParentSets().stream() .map(pSet -> pSet.getMainVar().getDistributionType().<EF_ConditionalDistribution>newEFConditionalDistribution(pSet.getParents())) .collect(Collectors.toList());
@Override public void buildClusterer(Instances data) throws Exception { attributes_ = Converter.convertAttributes(data.enumerateAttributes()); Variables modelHeader = new Variables(attributes_); clusterVar_ = modelHeader.newMultinomialVariable("clusterVar", this.numberOfClusters()); inferenceAlgorithm_ = new ImportanceSampling(); inferenceAlgorithm_.setSeed(this.getSeed()); dag = new DAG(modelHeader); /* Set DAG structure. */ /* Add the hidden cluster variable as a parent of all the predictive attributes. */ dag.getParentSets().stream() .filter(w -> w.getMainVar().getVarID() != clusterVar_.getVarID()) .filter(w -> w.getMainVar().isObservable()) .forEach(w -> w.addParent(clusterVar_)); System.out.println(dag.toString()); parameterLearningAlgorithm_ = new SVB(); parameterLearningAlgorithm_.setDAG(dag); DataOnMemoryListContainer<DataInstance> batch_ = new DataOnMemoryListContainer(attributes_); data.stream().forEach(instance -> batch_.add(new DataInstanceFromDataRow(new DataRowWeka(instance, this.attributes_))) ); parameterLearningAlgorithm_.setDataStream(batch_); parameterLearningAlgorithm_.initLearning(); parameterLearningAlgorithm_.runLearning(); bnModel_ = parameterLearningAlgorithm_.getLearntBayesianNetwork(); System.out.println(bnModel_); inferenceAlgorithm_.setModel(bnModel_); }
DAG dag = new DAG(variables); dag.getParentSet(e).addParent(a); dag.getParentSet(e).addParent(b); dag.getParentSet(h).addParent(a); dag.getParentSet(h).addParent(b); dag.getParentSet(i).addParent(a); dag.getParentSet(i).addParent(b); dag.getParentSet(i).addParent(c); dag.getParentSet(i).addParent(d); dag.getParentSet(g).addParent(c); dag.getParentSet(g).addParent(d); if (dag.containCycles()) { try { } catch (Exception ex) { System.out.println(dag.toString());
DAG dag = new DAG(variables); dag.getParentSets().stream() .filter(parentSet -> !parentSet.getMainVar().equals(classVar) && !parentSet.getMainVar().getName().startsWith("GaussianSPVar_") && !parentSet.getMainVar().getName().startsWith("DiscreteSPVar")) System.out.println("\n Number of variables \n " + bn.getDAG().getVariables().getNumberOfVars()); System.out.println(dag.toString());
public ParallelVBTranslate(DAG dag, List<String> latentNames, List<String> latentInterfaceNames, List<String> noLatentVariablesName) { latentVariables = latentNames.stream().map(name -> dag.getVariables().getVariableByName(name)).collect(Collectors.toList()); latentInterfaceVariables = latentInterfaceNames.stream().map(name -> dag.getVariables().getVariableByName(name)).collect(Collectors.toList()); allVariables = noLatentVariablesName.stream().map(name -> dag.getVariables().getVariableByName(name)).collect(Collectors.toList()); }
Variables modelHeader = new Variables(dataStream.getAttributes()); this.targetVar = modelHeader.getVariableByName(this.nameTarget); DAG dag = new DAG(modelHeader); BayesianNetwork bn = new BayesianNetwork(dag); Variable var = bn.getDAG().getVariables().getVariableById(i); Node n = nodeList.get(i); try { dagLearned = (BNConverterToAMIDST.convertToAmidst(huginNetwork)).getDAG(); dagLearned.getVariables().setAttributes(dataStream.getAttributes()); } catch (ExceptionHugin exceptionHugin) { System.out.println("ParallelTan LearnDAG Error 6");
/** * Sets the Hugin model structure from the AMIDST DAG. * @param amidstBN the Bayesian network model in AMIDST format. * @throws ExceptionHugin */ private void setStructure (BayesianNetwork amidstBN) throws ExceptionHugin { DAG dag = amidstBN.getDAG(); for (Variable amidstChild: amidstBN.getVariables()) { for (Variable amidstParent: dag.getParentSet(amidstChild)) { Node huginChild = this.huginBN.getNodeByName(amidstChild.getName()); Node huginParent = this.huginBN.getNodeByName(amidstParent.getName()); huginChild.addParent(huginParent); } } }
/** * This is the main method of the class which contains the sequence of executions included in the demo. * @param args input arguments (not used) * @throws Exception if an error occurs while reading the file. */ public static void main(String[] args) throws Exception { //Step 1. We show how to compute the monthly average value of the "expenses" variable. System.out.println("-----------------------CREDIT MONTHLY AVERAGE--------------------------"); BCC.computeMonthlyAverage(); System.out.println("-----------------------------------------------------------------------"); //Step 2. We build the NaiveBayes DAG with a global hidden var to track the concept drift System.out.println("--------------------------------DAG------------------------------------"); DAG dag = BCC.modelBuilding(); System.out.println(dag.toString()); System.out.println("-----------------------------------------------------------------------"); //Step 3. We set up the plateau structure use for learning System.out.println("------------------------DEFINING PLATEAU MODEL-------------------------"); ParallelSVB parallelSVB = BCC.plateuModelSetUp(dag); System.out.println("-----------------------------------------------------------------------"); //Step 4. We learn the model and print the results. System.out.println("------------------------------LEARNING---------------------------------"); BayesianNetwork bayesianNetwork = BCC.learnModel(parallelSVB); System.out.println("\n\nLearnt Bayesian network:\n\n"); System.out.println(bayesianNetwork.toString()); System.out.println("-----------------------------------------------------------------------"); }
public static void main(String[] args) throws Exception { final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); DataFlink<DataInstance> dataFlink = DataFlinkLoader.loadDataFromFile(env, "./data.arff", false); DAG dag = SetBNwithHidden.getHiddenNaiveBayesStructure(dataFlink); BayesianNetwork bnet = new BayesianNetwork(dag); System.out.println("\n Number of variables \n " + bnet.getDAG().getVariables().getNumberOfVars()); System.out.println(dag.toString()); BayesianNetworkWriter.save(bnet, "./BNHiddenExample.bn"); }
this.dag = new DAG(this.vars);
.registerAggregationConvergenceCriterion("ELBO_" + this.dagTimeT.getName(), new DoubleSumAggregator(), new eu.amidst.flinklink.core.learning.parametric.ParallelVB.ConvergenceELBO( this.globalThreshold, System.nanoTime())); config.setString(eu.amidst.flinklink.core.learning.parametric.ParameterLearningAlgorithm.BN_NAME, this.dagTimeT.getName()); config.setBytes(eu.amidst.flinklink.core.learning.parametric.ParallelVB.SVB, Serialization.serializeObject(svbTimeT)); config.setBytes(LATENT_INTERFACE_VARIABLE_NAMES, Serialization.serializeObject(this.latentInterfaceVariablesNames)); .map(new DynamicParallelVB.ParallelVBMap(data.getAttributes(), this.dagTimeT.getVariables().getListOfVariables(),randomStart, idenitifableModelling)) .withParameters(config) .withBroadcastSet(loop, "VB_PARAMS_" + this.dagTimeT.getName()) .reduce(new eu.amidst.flinklink.core.learning.parametric.ParallelVB.ParallelVBReduce()); config.setString(eu.amidst.flinklink.core.learning.parametric.ParameterLearningAlgorithm.BN_NAME, this.dagTimeT.getName()); config.setBytes(eu.amidst.flinklink.core.learning.parametric.ParallelVB.SVB, Serialization.serializeObject(svbTimeT)); config.setBytes(LATENT_VARIABLE_NAMES, Serialization.serializeObject(this.latentVariablesNames)); .flatMap(new CajaMarLearnMapInferenceAssignment(data.getAttributes(), this.dagTimeT.getVariables().getListOfVariables())) .withParameters(config);
/** * Tests if this DynamicDAG contains cycles. * @return a boolean indicating if this DynamicDAG contains cycles (true) or not (false). */ public boolean containCycles() { return this.toDAGTimeT().containCycles() || this.toDAGTime0().containCycles(); }
dag = new DAG(variables); dag.getParentSets().stream() .filter(parentSet -> !parentSet.getMainVar().equals(classVar) && !parentSet.getMainVar().getName().startsWith("GaussianSPVar_") && !parentSet.getMainVar().getName().startsWith("DiscreteSPVar")) .forEach(hv -> dag.getParentSet(variables.getVariableByName("GaussianSPVar_" + hv)).addParent(classVar)); dag.getParentSet(variables.getVariableByName("DiscreteSPVar")).addParent(classVar); System.out.println(dag.toString());