/** * 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)); }
.filter(w -> w.getMainVar() != classVariable) .filter(w -> w.getMainVar() != globalHiddenDiscrete) .filter(w -> w.getMainVar() != globalHiddenGaussian) .filter(w -> w.getMainVar().isMultinomial()) .forEach(w -> w.addParent(globalHiddenDiscrete)); .filter(w -> w.getMainVar() != classVariable) .filter(w -> w.getMainVar() != globalHiddenDiscrete) .filter(w -> w.getMainVar() != globalHiddenGaussian) .filter(w -> w.getMainVar().isNormal()) .forEach(w -> w.addParent(globalHiddenGaussian)); .filter(w -> w.getMainVar() != classVariable) .forEach(w -> w.addParent(classVariable));
/** * Sets the dynamic DAG for this this DynamicPlateauStructure. * @param dbnModel a {@link DynamicDAG} object. */ public void setDBNModel(DynamicDAG dbnModel) { this.dbnModel = dbnModel; List<EF_ConditionalDistribution> distTim0 = this.dbnModel.getParentSetsTime0().stream().map(pSet -> pSet.getMainVar().getDistributionType().<EF_ConditionalDistribution>newEFConditionalDistribution(pSet.getParents())).collect(Collectors.toList()); this.setEFBayesianNetworkTime0(new EF_LearningBayesianNetwork(distTim0)); List<EF_ConditionalDistribution> distTimT = this.dbnModel.getParentSetsTimeT().stream().map(pSet -> pSet.getMainVar().getDistributionType().<EF_ConditionalDistribution>newEFConditionalDistribution(pSet.getParents())).collect(Collectors.toList()); this.setEFBayesianNetworkTimeT(new EF_LearningBayesianNetwork(distTimT)); }
public static DynamicDAG createDynamicNaiveBayes(Attributes attributes){ // Create a Variables object from the attributes of the input data stream. DynamicVariables variables = new DynamicVariables(attributes); // Define the class variable. Variable classVar = variables.getVariableById(0); // Create a DAG object with the defined variables. DynamicDAG dynamicDAG = new DynamicDAG(variables); // Link the class as parent of all attributes dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != classVar) .forEach(w -> w.addParent(classVar)); // Link the class and the attributes through time dynamicDAG.getParentSetsTimeT() .stream() .forEach(w -> w.addParent(w.getMainVar().getInterfaceVariable())); // Return the DAG. return dynamicDAG; }
/** * 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 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)); } } } }
public static DynamicDAG createDynamicNaiveBayesWithHidden(Attributes attributes){ // Create a Variables object from the attributes of the input data stream. DynamicVariables variables = new DynamicVariables(attributes); // Define the global latent binary variable. Variable globalHiddenVar = variables.newMultinomialDynamicVariable("GlobalHidden", 2); // Define the class variable. Variable classVar = variables.getVariableById(0); // Create a DAG object with the defined variables. DynamicDAG dynamicDAG = new DynamicDAG(variables); // Link the class as parent of all attributes (and the hidden) dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != classVar) .forEach(w -> w.addParent(classVar)); // Link the hidden as parent of all attributes dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != classVar) .forEach(w -> w.addParent(globalHiddenVar)); // Link the class, the hidden and the attributes through time dynamicDAG.getParentSetsTimeT() .stream() .forEach(w -> w.addParent(w.getMainVar().getInterfaceVariable())); // Return the DAG. return dynamicDAG; }
@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); }
/** * 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 discreteHiddenVar = this.variables.newMultinomialDynamicVariable("discreteHiddenVar", getNumStatesHiddenVar()); dynamicDAG = new DynamicDAG(this.variables); dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != discreteHiddenVar) .forEach(w -> w.addParent(discreteHiddenVar)); dynamicDAG.getParentSetTimeT(discreteHiddenVar).addParent(discreteHiddenVar.getInterfaceVariable()); /* * Learn full covariance matrix */ if(!isDiagonal()) { List<Variable> observedVars = this.variables.getListOfDynamicVariables() .stream() .filter(v -> !v.equals(discreteHiddenVar)) .peek(v-> { if(v.isMultinomial()) throw new UnsupportedOperationException("Full covariance matrix cannot be used with" + " multinomial attributes"); }) .collect(Collectors.toList()); for (int i=0; i<observedVars.size()-1; i++){ for(int j=i+1; j<observedVars.size(); j++) { dynamicDAG.getParentSetTime0(observedVars.get(i)).addParent(observedVars.get(j)); dynamicDAG.getParentSetTimeT(observedVars.get(i)).addParent(observedVars.get(j)); } } } }
dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != discreteHiddenVar) .forEach(w -> { w.addParent(discreteHiddenVar); w.addParent(w.getMainVar().getInterfaceVariable()); });
.map(pSet -> pSet.getMainVar().getDistributionType().<EF_ConditionalDistribution>newEFConditionalDistribution(pSet.getParents())) .collect(Collectors.toList());
.map(pSet -> pSet.getMainVar().getDistributionType().<EF_ConditionalDistribution>newEFConditionalDistribution(pSet.getParents())) .collect(Collectors.toList());
/** * Generates a Dynamic Naive Bayes DAG with a Multinomial class variable. * @param numberClassStates the number of states for the class variable. * @param connectChildrenTemporally a {@code boolean} that indicates whether the children are connected temporally or not. * @return a {@link DynamicDAG} object. */ public static DynamicDAG generateDynamicNaiveBayesDAG(int numberClassStates, boolean connectChildrenTemporally){ DynamicVariables dynamicVariables = new DynamicVariables(); //class variable which is always discrete Variable classVar = dynamicVariables.newMultinomialDynamicVariable(classVarName, numberClassStates); //Discrete variables IntStream.range(1, numberOfDiscreteVars+1) .forEach(i -> dynamicVariables.newMultinomialDynamicVariable(discreteVarName + i, DynamicBayesianNetworkGenerator.numberOfStates)); //Continuous variables IntStream.range(1,numberOfContinuousVars+1) .forEach(i -> dynamicVariables.newGaussianDynamicVariable(continuousVarName + i)); DynamicDAG dag = new DynamicDAG(dynamicVariables); dag.getParentSetsTimeT().stream() .filter(var -> var.getMainVar().getVarID()!=classVar.getVarID()) .forEach(w -> { w.addParent(classVar); if (connectChildrenTemporally) { w.addParent(dynamicVariables.getInterfaceVariable(w.getMainVar())); } } ); dag.getParentSetTimeT(classVar).addParent(dynamicVariables.getInterfaceVariable(classVar)); return dag; }
dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != discreteHiddenVar) .filter(w -> w.getMainVar() != gaussianHiddenVar) .forEach(w -> { w.addParent(discreteHiddenVar);
.filter(parentSet -> !parentSet.getMainVar().equals(classVar) && !parentSet.getMainVar().getName().startsWith("GaussianSPVar_") && !parentSet.getMainVar().getName().startsWith("DiscreteSPVar")) .forEach(w -> { w.addParent(classVar); w.addParent(discreteHiddenVar); if (getNumHiddenGaussVars() > 0 && w.getMainVar().isNormal()) IntStream.rangeClosed(0, getNumHiddenGaussVars() - 1).forEach(i -> w.addParent(variables.getVariableByName("GaussianSPVar_" + i))); });
/** * This method returns a DynamicDAG object with naive Bayes structure for the given attributes. * @param attributes object of the class Attributes * @param classIndex integer value indicating the possition of the class * @return object of the class DynamicDAG */ public static DynamicDAG getNaiveBayesStructure(Attributes attributes, int classIndex){ //We create a Variables object from the attributes of the data stream DynamicVariables dynamicVariables = new DynamicVariables(attributes); //We define the predicitive class variable Variable classVar = dynamicVariables.getVariableById(classIndex); //Then, we create a DAG object with the defined model header DynamicDAG dag = new DynamicDAG(dynamicVariables); //We set the links of the DAG. dag.getParentSetsTimeT().stream() .filter(var -> var.getMainVar().getVarID()!=classVar.getVarID()) .forEach(w -> { w.addParent(classVar); //Connect children in consecutive time steps w.addParent(dynamicVariables.getInterfaceVariable(w.getMainVar())); } ); //Connect the class variable in consecutive time steps dag.getParentSetTimeT(classVar).addParent(dynamicVariables.getInterfaceVariable(classVar)); return dag; }
.filter(parentSet -> !parentSet.getMainVar().equals(classVar) && !parentSet.getMainVar().getName().startsWith("GaussianSPVar_") && !parentSet.getMainVar().getName().startsWith("DiscreteSPVar")) .forEach(w -> { w.addParent(classVar); w.addParent(discreteHiddenVar); if (getNumHiddenGaussVars() > 0 && w.getMainVar().isNormal()) IntStream.rangeClosed(0, getNumHiddenGaussVars() - 1).forEach(i -> w.addParent(variables.getVariableByName("GaussianSPVar_" + i))); });
/** * Learns the dynamic graphical structure for this DynamicNaiveBayesClassifier given an input data stream. * @param dataStream a {@link DataStream} of {@link DynamicDataInstance}s. * @return a {@link DynamicDAG} object. */ private DynamicDAG dynamicNaiveBayesStructure(DataStream<DynamicDataInstance> dataStream){ DynamicVariables dynamicVariables = new DynamicVariables(dataStream.getAttributes()); Variable classVar = dynamicVariables.getVariableById(this.getClassVarID()); DynamicDAG dag = new DynamicDAG(dynamicVariables); dag.getParentSetsTimeT().stream() .filter(w -> w.getMainVar().getVarID() != classVar.getVarID()) .forEach(w -> { w.addParent(classVar); if(isConnectChildrenTemporally()) w.addParent(dynamicVariables.getInterfaceVariable(w.getMainVar())); }); dag.getParentSetTimeT(classVar).addParent(dynamicVariables.getInterfaceVariable(classVar)); 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_); }