/** * Creates a new EF_BayesianNetwork object given a {@link DynamicDAG} object. * @param dag a {@link DynamicDAG} object. */ public EF_DynamicBayesianNetwork(DynamicDAG dag) { this.bayesianNetworkTime0 = new EF_BayesianNetwork(dag.getParentSetsTime0()); this.bayesianNetworkTimeT = new EF_BayesianNetwork(dag.getParentSetsTimeT()); }
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; }
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; }
if (this.parentSetTimeT.size() != dyndag.getParentSetsTimeT().size()) { return false; } else {
/** * 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)); }
dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> !binaryHiddenVars.contains(w.getMainVar()))
dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> !gaussianHiddenVars.contains(w.getMainVar()))
@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)); } } } }
dag.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != classVar) dag.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() == classVar)
dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != discreteHiddenVar)
dynamicDAG.getParentSetsTimeT() .stream() .filter(w -> w.getMainVar() != discreteHiddenVar)
/** * 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; }
this.noLatentVariablesNames =this.dynamicDAG.getParentSetsTimeT() .stream() .filter(p -> p.getMainVar().isObservable()) .collect(Collectors.toList()); this.latentVariablesNames = this.dynamicDAG.getParentSetsTimeT() .stream() .flatMap(p -> p.getParents().stream()) .collect(Collectors.toList()); this.latentInterfaceVariablesNames = this.dynamicDAG.getParentSetsTimeT() .stream() .flatMap(p -> p.getParents().stream())
/** * 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; }
/** * 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; }
dag.getParentSetsTimeT().stream() .filter(v -> !v.getMainVar().getName().contains("A") && !v.getMainVar().getName().contains("C") && dag.getParentSetsTimeT().stream() .filter(var -> !var.getMainVar().getName().contains("H")) .forEach(w -> { dag.getParentSetsTimeT().stream() .filter(var -> var.getMainVar().getVarID()!=varH1.getVarID()) .forEach(w -> {
dynamicDAG.getParentSetsTimeT().stream() .filter(pset -> pset.getMainVar().getVarID() != A.getVarID()) .filter(pset -> pset.getMainVar().getVarID() != H.getVarID())