/** * Returns an interface variable given its name. * @param name a {@code String} that represents a valid interface variable name. * @return a {@link Variable} object. */ public Variable getInterfaceVariableByName(String name) { return this.getInterfaceVariable(this.getVariableByName(name)); }
/** * Runs inference at time T. */ public void runInferenceTimeT() { this.vmpTimeT.runInference(); this.plateuNodesTimeT.get(this.nRepetitions-1).stream().filter(node -> !node.isObserved() && !node.getMainVariable().isParameterVariable()).forEach(node -> { Variable temporalClone = this.dbnModel.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.getNodeOfVarTimeT(temporalClone,0), node); }); }
/** * Moves the posterior distribution of a given {@link Variable} from a {@link BayesianNetwork} object to another. * @param infAlg an {@link InferenceAlgorithm} object. * @param bnTo a {@link BayesianNetwork} object to which the posterior distribution should be moved. * @param var a given {@link Variable}. */ private void moveNodeQDist(InferenceAlgorithm infAlg, BayesianNetwork bnTo, Variable var){ //Recover original model and do the copy, then set again. Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(var); UnivariateDistribution posteriorDist = infAlg.getPosterior(var).deepCopy(temporalClone); bnTo.setConditionalDistribution(temporalClone, posteriorDist); }
/** * Runs inference at time 0. */ public void runInferenceTime0() { this.vmpTime0.runInference(); this.vmpTime0.getNodes().stream().filter(node -> !node.isObserved() && !node.getMainVariable().isParameterVariable()).forEach(node -> { Variable temporalClone = this.dbnModel.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.getNodeOfVarTimeT(temporalClone,0), node); }); }
/** * 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; }
/** * {@inheritDoc} */ @Override public void setModel(DynamicBayesianNetwork model_) { model = model_; ef_model = new EF_DynamicBayesianNetwork(this.model); this.vmpTime0.setEFModel(ef_model.getBayesianNetworkTime0()); nodesTimeT = this.ef_model.getBayesianNetworkTimeT().getDistributionList() .stream() .map(dist -> new Node(dist)) .collect(Collectors.toList()); nodesClone = this.ef_model.getBayesianNetworkTime0().getDistributionList() .stream() .map(dist -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(dist.getVariable()); EF_UnivariateDistribution uni = temporalClone.getDistributionType().newUnivariateDistribution().toEFUnivariateDistribution(); Node node = new Node(uni); node.setActive(false); return node; }) .collect(Collectors.toList()); List<Node> allNodes = new ArrayList(); allNodes.addAll(nodesTimeT); allNodes.addAll(nodesClone); this.vmpTimeT.setNodes(allNodes); this.vmpTimeT.updateChildrenAndParents(); }
.filter(dist -> !dist.getVariable().isParameterVariable()) .map(dist -> { Variable temporalClone = this.dbnModel.getDynamicVariables().getInterfaceVariable(dist.getVariable()); EF_UnivariateDistribution uni = temporalClone.getDistributionType().newUnivariateDistribution().toEFUnivariateDistribution(); Node node = new Node(uni);
/** * 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; }
/** * {@inheritDoc} */ @Override public <E extends UnivariateDistribution> E getPredictivePosterior(Variable var, int nTimesAhead) { if (timeID==-1){ this.vmpTime0.setEvidence(null); this.vmpTime0.runInference(); this.vmpTime0.getNodes().stream().filter(node -> !node.isObserved()).forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); }); this.moveWindow(nTimesAhead-1); E resultQ = this.getFilteredPosterior(var); this.vmpTime0.resetQs(); this.vmpTimeT.resetQs(); return resultQ; }else { Map<Variable, EF_UnivariateDistribution> map = new HashMap<>(); //Create at copy of Qs this.vmpTimeT.getNodes().stream().filter(node -> !node.isObserved()).forEach(node -> map.put(node.getMainVariable(), node.getQDist().deepCopy())); this.moveWindow(nTimesAhead); E resultQ = this.getFilteredPosterior(var); //Come to the original state map.entrySet().forEach(e -> this.vmpTimeT.getNodeOfVar(e.getKey()).setQDist(e.getValue())); return resultQ; } }
/** * 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; }
/** * Replicates the present data in the past time. * @param dataPresent a {@link HashMapAssignment} object that represents the present data. * @return a {@link HashMapAssignment} object that represents the past data. */ private HashMapAssignment replicateOnPast(HashMapAssignment dataPresent){ HashMapAssignment dataPast = new HashMapAssignment(network.getNumberOfVars()); for (Variable var : network.getDynamicVariables().getListOfDynamicVariables()) { dataPast.setValue(network.getDynamicVariables().getInterfaceVariable(var), dataPresent.getValue(var)); } return dataPast; }
/** * Moves the window ahead for a given number of time steps. * @param nsteps an {@link int} that represents a given number of time steps. */ private void moveWindow(int nsteps){ //The first step we need to manually move the evidence from master to clone variables. HashMapDynamicAssignment newassignment = null; if (this.assignment!=null) { newassignment=new HashMapDynamicAssignment(this.model.getNumberOfDynamicVars()); for (Variable var : this.model.getDynamicVariables()) { newassignment.setValue(this.model.getDynamicVariables().getInterfaceVariable(var), this.assignment.getValue(var)); newassignment.setValue(var, Utils.missingValue()); } } for (int i = 0; i < nsteps; i++) { this.vmpTimeT.setEvidence(newassignment); this.vmpTimeT.runInference(); this.vmpTimeT.getNodes().stream() .filter(node -> !node.getMainVariable().isInterfaceVariable()) .filter(node -> !node.isObserved()) .forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); }); newassignment=null; } }
this.timeID=0; this.vmpTime0.getNodes().stream().filter(node -> !node.isObserved()).forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); }); .filter(node -> !node.isObserved()) .forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); }); .filter(node -> !node.isObserved()) .forEach(node -> { Variable temporalClone = this.model.getDynamicVariables().getInterfaceVariable(node.getMainVariable()); moveNodeQDist(this.vmpTimeT.getNodeOfVar(temporalClone), node); });
/** * In this example we show how to create an input-outputString KF with Gaussian mixtures (as in Figure 4.29 of Deliverable 2.1). */ public static void VerdandeInputOutputHMM() throws IOException { DataStream<DynamicDataInstance> data = DynamicDataStreamLoader.loadFromFile("datasets/simulated/syntheticDataVerdandeScenario3.arff"); Attribute attDepth = data.getAttributes().getAttributeByName("depth"); Attribute attGammaDiff = data.getAttributes().getAttributeByName("gammaDiff"); DynamicVariables dynamicVariables = new DynamicVariables(); Variable observedDepth = dynamicVariables.newDynamicVariable(attDepth); Variable observedGammaDiff = dynamicVariables.newDynamicVariable(attGammaDiff); Variable formationNo = dynamicVariables.newMultinomialLogisticDynamicVariable("FormationNo", 2); Variable shift = dynamicVariables.newMultinomialDynamicVariable("Shift",2); DynamicDAG dynamicDAG = new DynamicDAG(dynamicVariables); dynamicDAG.getParentSetTimeT(formationNo).addParent(observedDepth); dynamicDAG.getParentSetTimeT(formationNo).addParent(dynamicVariables.getInterfaceVariable(formationNo)); //TODO Error trying to add a duplicate parent. A -> B <- Aclone. We are considering A and AClone the same variables? Is that right? dynamicDAG.getParentSetTimeT(shift).addParent(formationNo); //dynamicDAG.getParentSetTimeT(shift).addParent(dynamicVariables.getInterfaceVariable(formationNo)); dynamicDAG.getParentSetTimeT(shift).addParent(dynamicVariables.getInterfaceVariable(shift)); dynamicDAG.getParentSetTimeT(observedGammaDiff).addParent(shift); System.out.println("-------------------------------------\n"); System.out.println("Input-outputString HMM (Figure 4.31 of D2.1)\n"); System.out.println(dynamicDAG.toString()); DynamicBayesianNetwork dbn = new DynamicBayesianNetwork(dynamicDAG); System.out.println(dbn.toString()); DynamicBayesianNetworkWriter.save(dbn, "networks/simulated/HuginVerdandeIOHMM.dbn"); }
/** * Moves the window ahead for a given number of time steps. * @param nsteps an {@link int} that represents a given number of time steps. */ private void moveWindow(int nsteps){ //The first step we need to manually move the evidence from master to clone variables. HashMapDynamicAssignment newassignment =null; if (this.assignment!=null) { newassignment=new HashMapDynamicAssignment(this.model.getNumberOfDynamicVars()); for (Variable var : this.model.getDynamicVariables()) { newassignment.setValue(this.model.getDynamicVariables().getInterfaceVariable(var), this.assignment.getValue(var)); newassignment.setValue(var, Utils.missingValue()); } } for (int i = 0; i < nsteps; i++) { this.infAlgTimeT.setModel(this.bnTimeT); this.infAlgTimeT.setEvidence(updateDynamicAssignmentTimeT(newassignment)); this.infAlgTimeT.runInference(); this.getTargetVarsTimeT().stream() .forEach(var -> moveNodeQDist(this.infAlgTimeT, this.bnTimeT, var)); } }
Variable G = dynamicVariables.getVariableByName("G"); Variable A_Interface = dynamicVariables.getInterfaceVariable(A); Variable B_Interface = dynamicVariables.getInterfaceVariable(B); Variable E_Interface = dynamicVariables.getInterfaceVariable(E); Variable G_Interface = dynamicVariables.getInterfaceVariable(G); Variable H_Interface = dynamicVariables.getInterfaceVariable(H);
.forEach(v -> w.addParent(v)); if(this.isConnectChildrenTemporally()) w.addParent(dynamicVariables.getInterfaceVariable(w.getMainVar())); }); w.addParent(varH1); if(this.isConnectChildrenTemporally()) w.addParent(dynamicVariables.getInterfaceVariable(w.getMainVar()));
Variable amidstClone = dynamicVariables.getInterfaceVariable(amidstChild); dynamicDAG.getParentSetTimeT(amidstChild).addParent(amidstClone);
Variable G = dynamicVariables.getVariableByName("G"); Variable A_Interface = dynamicVariables.getInterfaceVariable(A); Variable B_Interface = dynamicVariables.getInterfaceVariable(B); Variable E_Interface = dynamicVariables.getInterfaceVariable(E); Variable G_Interface = dynamicVariables.getInterfaceVariable(G);
dynamicDAG.getParentSetTimeT(observedTRQ).addParent(hidden); dynamicDAG.getParentSetTimeT(realTRQ).addParent(dynamicVariables.getInterfaceVariable(realTRQ)); dynamicDAG.getParentSetTimeT(realTRQ).addParent(normalAbnormal); dynamicDAG.getParentSetTimeT(hidden).addParent(dynamicVariables.getInterfaceVariable(hidden)); dynamicDAG.getParentSetTimeT(normalAbnormal).addParent(dynamicVariables.getInterfaceVariable(normalAbnormal)); dynamicDAG.getParentSetTimeT(normalAbnormal).addParent(observedROP);