/** * 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() { dag = new DAG(vars); dag.getParentSets().stream().filter(w -> !w.getMainVar().equals(classVar)).forEach(w -> w.addParent(classVar)); }
/** * 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 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 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)); } }
/** * 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); }); }); }
@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 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)); } } /*
/** * 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; }
@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; }
/** * 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; }
/** * 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; }
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; }
/** * 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; }
@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_); }
/** * 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; }