private String getName(){ if (dag!=null) return dag.getName(); else return ""; }
/** * 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)); }
public static BayesianNetwork createBN(int nVars) throws Exception { Variables dynamicVariables = new Variables(); Variable classVar = dynamicVariables.newMultinomialVariable("C", 2); for (int i = 0; i < nVars; i++) { dynamicVariables.newGaussianVariable("A" + i); } DAG dag = new DAG(dynamicVariables); for (int i = 0; i < nVars; i++) { dag.getParentSet(dynamicVariables.getVariableByName("A" + i)).addParent(classVar); } dag.setName("dbn1"); BayesianNetwork bn = new BayesianNetwork(dag); bn.randomInitialization(new Random(1)); return bn; }
/** * 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)); } } }
/** * 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; }
/** * Sets the distribution of a normal variable with multinomial parents in the AMIDST model from the corresponding * distribution in the Hugin model. * For each assignment of the multinomial parents, a univariate normal is set. * @param huginVar the Hugin variable with the distribution to be converted. * @throws ExceptionHugin */ private void setNormal_MultinomialParents(Node huginVar) throws ExceptionHugin { int indexNode = this.huginBN.getNodes().indexOf(huginVar); Variable amidstVar = this.amidstBN.getVariables().getVariableById(indexNode); List<Variable> conditioningVariables = this.amidstBN.getDAG().getParentSet(amidstVar).getParents(); int numParentAssignments = MultinomialIndex.getNumberOfPossibleAssignments(conditioningVariables); Normal_MultinomialParents dist = this.amidstBN.getConditionalDistribution(amidstVar); for (int i = 0; i < numParentAssignments; i++) { Normal normal = dist.getNormal(i); this.setNormal(huginVar, normal, i); } }
/** * 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); } } }
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 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 <E extends UnivariateDistribution> E getPosteriorDistribution(String varName) { if (learningAlgorithm !=null){ return (E)this.learningAlgorithm.getLearntBayesianNetwork() .getConditionalDistribution(dag.getVariables().getVariableByName(varName)); } else if (learningAlgorithmFlink != null ){ return (E)this.learningAlgorithmFlink.getLearntBayesianNetwork() .getConditionalDistribution(dag.getVariables().getVariableByName(varName)); } return null; }
@Override public String toString() { return this.getModel().toString(); }
/** * Returns the class variable of the classifier * @return A <code>Variable</code> object */ public Variable getClassVariable(){ return this.svb.getLearntBayesianNetwork().getVariables().getVariableById(this.classIndex); }
public MAPDynamicInstancesSampler(BayesianNetwork bn, Attributes attributes, Map<Variable,Boolean> hiddenVars, Map<Variable,Double> marVars, int seed) { this.bn = bn; this.seed = seed; this.attributes = attributes; this.hiddenVars = hiddenVars; this.marVars = marVars; this.causalOrder = Utils.getTopologicalOrder(this.bn.getDAG()); }
/** * 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(); }
/** * {@inheritDoc} */ @Override public String listOptionsRecursively() { return this.listOptions() + "\n" + BayesianNetwork.listOptionsRecursively() + "\n" + AmidstOptionsHandler.listOptionsRecursively(BayesianNetworkSampler.class); }
@Override protected void buildDAG() { Variable superParentVar = vars.newMultinomialVariable("superParentVar",getNumStates()); dag = new DAG(vars); dag.getParentSets() .stream() .filter(w -> !w.getMainVar().equals(classVar)) .filter(w -> !w.getMainVar().equals(superParentVar)) .forEach(w -> { w.addParent(classVar); w.addParent(superParentVar); }); dag.getParentSet(superParentVar).addParent(classVar); }
@Override protected void buildDAG() { /** Create a set of variables from the given attributes**/ Variables variables = new Variables(attributes); /** Create a hidden variable with two hidden states*/ Variable hiddenVar = variables.newMultinomialVariable("HiddenVar",2); //We create a standard naive Bayes DAG dag = new DAG(variables); for (Variable variable: variables){ if (variable==hiddenVar) continue; dag.getParentSet(variable).addParent(hiddenVar); } //This is needed to maintain coherence in the Model class. this.dag=dag; this.vars = variables; }
/** * {@inheritDoc} */ @Override public String listOptionsRecursively() { return this.listOptions() + "\n" + BayesianNetwork.listOptionsRecursively() + "\n" + AmidstOptionsHandler.listOptionsRecursively(BayesianNetworkSampler.class); }
/** * This method returns a DAG object with naive Bayes structure for the attributes of the passed data stream. * @param dataStream object of the class DataStream<DataInstance> * @param classIndex integer value indicating the position of the class * @return object of the class DAG */ public static DAG getNaiveBayesStructure(DataStream<DataInstance> dataStream, int classIndex){ //We create a Variables object from the attributes of the data stream Variables modelHeader = new Variables(dataStream.getAttributes()); //We define the predicitive class variable Variable classVar = modelHeader.getVariableById(classIndex); //Then, we create a DAG object with the defined model header DAG dag = new DAG(modelHeader); //We set the linkds of the DAG. dag.getParentSets().stream().filter(w -> w.getMainVar() != classVar).forEach(w -> w.addParent(classVar)); return dag; }
/** * Define the Fire Dectector Model's DAG. * @param attributes * @return */ public static DAG creatFireDectectorModel(Attributes attributes){ /********** Model Definition ************/ //Create the object handling the random variables of the model Variables variables = new Variables(); //Create the random variables of the model. Some of them are associated to one attribute to retrieve its observed values from the data set. Variable fire = variables.newMultinomialVariable(attributes.getAttributeByName("Fire")); Variable temperature = variables.newGaussianVariable("Temperature"); Variable smoke = variables.newMultinomialVariable("Smoke",2); Variable sensorT1 = variables.newGaussianVariable(attributes.getAttributeByName("SensorTemp1")); Variable sensorT2 = variables.newGaussianVariable(attributes.getAttributeByName("SensorTemp2")); Variable sensorSmoke = variables.newGaussianVariable(attributes.getAttributeByName("SensorSmoke")); //Create the directed acyclic graph object encoding the conditional independe relaionship among the variables of the model. DAG dag = new DAG(variables); //Define the parent set for each random variable dag.getParentSet(sensorT1).addParent(temperature); dag.getParentSet(sensorT2).addParent(temperature); dag.getParentSet(sensorSmoke).addParent(smoke); dag.getParentSet(temperature).addParent(fire); dag.getParentSet(smoke).addParent(fire); return dag; }