/** * 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)); } } } } /*
/** * Builds the DAG over the set of variables given with the naive Bayes structure */ @Override protected void buildDAG() { //Obtain the predictive attributes List<Variable> attrVars = vars.getListOfVariables().stream() .filter(v -> !v.equals(classVar)).collect(Collectors.toList()); //Create the hidden variabels hiddenMultinomial = vars.newMultinomialVariable("M", numStatesHidden); contHiddenList = new ArrayList<Variable>(); for(int i=0; i<numContinuousHidden; i++) { contHiddenList.add(vars.newGaussianVariable("Z"+Integer.toString(i))); } dag = new DAG(vars); //arcs from the class to the hidden variables dag.getParentSet(hiddenMultinomial).addParent(classVar); contHiddenList.stream().forEach(z -> dag.getParentSet(z).addParent(classVar)); //arcs from the hidden vars to each attribute attrVars.stream().forEach(x->dag.getParentSet(x).addParent(hiddenMultinomial)); for (Variable z : contHiddenList) { attrVars.stream().forEach(x->dag.getParentSet(x).addParent(z)); } } /*
/** * Builds the DAG over the set of variables given with the structure of the model */ @Override protected void buildDAG() { hiddenVar = vars.newMultinomialVariable("HiddenVar",numStatesHiddenVar); //We create a standard naive Bayes dag = new DAG(vars); dag.getParentSets().stream().filter(w -> !w.getMainVar().equals(hiddenVar)).forEach(w -> w.addParent(hiddenVar)); // if it is not diagonal add the links between the attributes (features) if(!isDiagonal()) { List<Variable> attrVars = vars.getListOfVariables().stream().filter(v -> !v.equals(hiddenVar)).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)); } } } }
/** * Builds the graph */ @Override protected void buildDAG() { List<Variable> observableVariables = new ArrayList<>(); List<Variable> latentVariables = new ArrayList<>(); vars.forEach(observableVariables::add); Variable discreteLatentVar = vars.newMultinomialVariable("DiscreteLatentVar",numberOfStatesLatentDiscreteVar); IntStream.range(0,numberOfLatentVariables).forEach(i -> { Variable latentVar = vars.newGaussianVariable("LatentVar" + i); latentVariables.add(latentVar); }); dag = new DAG(vars); for (Variable variable : observableVariables) { dag.getParentSet(variable).addParent(discreteLatentVar); latentVariables.forEach(latentVariable -> dag.getParentSet(variable).addParent(latentVariable)); } }
@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() { //Obtain the predictive attributes List<Variable> attrVars = vars.getListOfVariables().stream() .filter(v -> !v.equals(classVar)).collect(Collectors.toList()); int numAttr = attrVars.size(); /** Create a hidden variable with two hidden states */ Variable globalHiddenVar = vars.newMultinomialVariable("globalHiddenVar",2); /** Create a list of local hidden variables */ List<Variable> localHidden = new ArrayList<Variable>(); for(int i= 0; i< numAttr; i++) { localHidden.add(vars.newMultinomialVariable("locallHiddenVar_"+i,2)); } /** We create a standard naive Bayes */ DAG dag = new DAG(vars); /** Add the links */ for (int i=0; i<numAttr; i++) { dag.getParentSet(attrVars.get(i)).addParent(localHidden.get(i)); dag.getParentSet(attrVars.get(i)).addParent(globalHiddenVar); dag.getParentSet(attrVars.get(i)).addParent(classVar); } //This is needed to maintain coherence in the Model class. this.dag=dag; }
/** * Builds the DAG of the model. */ @Override protected void buildDAG() { dag = new DAG(vars); //arcs from the features to the class vars.getListOfVariables().stream() .filter(v -> !v.equals(classVar)) .forEach(v -> dag.getParentSet(classVar).addParent(v)); // 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)); } } } }
/** * Builds the DAG */ @Override protected void buildDAG() { List<Variable> observableVariables = new ArrayList<>(); List<Variable> latentVariables = new ArrayList<>(); vars.forEach(observableVariables::add); IntStream.range(0,numberOfLatentVariables).forEach(i -> { Variable latentVar = vars.newGaussianVariable("LatentVar" + i); latentVariables.add(latentVar); }); dag = new DAG(vars); for (Variable variable : observableVariables) { latentVariables.forEach(latentVariable -> dag.getParentSet(variable).addParent(latentVariable)); } IntStream.range(0,numberOfLatentVariables).forEach(i -> { Variable latentVarChildren = vars.getVariableByName("LatentVar" + i); IntStream.range(0,i).forEach(j -> { Variable latentVarParent = vars.getVariableByName("LatentVar" + j); dag.getParentSet(latentVarChildren).addParent(latentVarParent); }); }); }
/** * Builds the DAG over the set of variables given with the structure of the model */ @Override protected void buildDAG() { String className = atts.getFullListOfAttributes().get(classIndex).getName(); hiddenVars = new ArrayList<Variable>(); for (int i = 0; i < this.numberOfGlobalVars ; i++) { hiddenVars.add(vars.newGaussianVariable("GlobalHidden_"+i)); } Variable classVariable = vars.getVariableByName(className); dag = new DAG(vars); for (Attribute att : atts.getListOfNonSpecialAttributes()) { if (att.getName().equals(className)) continue; Variable variable = vars.getVariableByName(att.getName()); dag.getParentSet(variable).addParent(classVariable); if (this.globalHidden) { for (int i = 0; i < this.numberOfGlobalVars ; i++) { dag.getParentSet(variable).addParent(hiddenVars.get(i)); } } } }
/** * 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()); }
/** * 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; }
@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; }
/** * 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; }
/** * Returns the DAG at time T of this DynamicDAG. * @return a {@link DAG} object. */ public DAG toDAGTimeT(){ List<Variable> allVariables = new ArrayList<>(); allVariables.addAll(this.getDynamicVariables().getListOfDynamicVariables()); allVariables.addAll(this.getDynamicVariables().getListOfInterfaceVariables()); Variables staticVariables = Variables.auxiliarBuilder(allVariables); DAG dag = new DAG(staticVariables); dag.setName(this.getName()); for (Variable dynamicVariable : dynamicVariables) { for (Variable parent : this.getParentSetTimeT(dynamicVariable)) { dag.getParentSet(dynamicVariable).addParent(parent); } } return dag; }
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; }
/** * 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); } } }
/** * 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 distribution of a multinomial variable with multinomial parents in the AMIDST model * from the corresponding distribution in the Hugin model. * @param huginVar the Hugin variable with the distribution to be converted. * @throws ExceptionHugin */ private void setMultinomial_MultinomialParents(Node huginVar) throws ExceptionHugin { int indexNode = this.huginBN.getNodes().indexOf(huginVar); Variable amidstVar = this.amidstBN.getVariables().getVariableById(indexNode); int numStates = amidstVar.getNumberOfStates(); double[] huginProbabilities = huginVar.getTable().getData(); List<Variable> parents = this.amidstBN.getDAG().getParentSet(amidstVar).getParents(); int numParentAssignments = MultinomialIndex.getNumberOfPossibleAssignments(parents); for (int i = 0; i < numParentAssignments; i++) { double[] amidstProbabilities = new double[numStates]; for (int k = 0; k < numStates; k++) { amidstProbabilities[k] = huginProbabilities[i * numStates + k]; } Multinomial_MultinomialParents dist = this.amidstBN.getConditionalDistribution(amidstVar); dist.getMultinomial(i).setProbabilities(amidstProbabilities); } }