/** * Returns the total number of variables in this DynamicBayesianNetwork. * @return an {@code int} that represents the number of variables. */ public int getNumberOfDynamicVars() { return this.getDynamicVariables().getNumberOfVars(); }
/** * Returns the total number of variables in this DynamicBayesianNetwork. * @return an {@code int} that represents the number of variables. */ public int getNumberOfVars() { return this.getDynamicVariables().getNumberOfVars(); }
/** * Return the list of non-observed variables for Time 0. * @return a {@code List} of {@link Variable}. */ private List<Variable> getTargetVarsTime0(){ return this.model.getDynamicVariables().getListOfDynamicVariables().stream() .filter(var -> Utils.isMissingValue(this.assignment.getValue(var))) .collect(Collectors.toList()); }
/** * Returns the class variable of the classifier * @return A <code>Variable</code> object */ public Variable getClassVariable(){ return this.svb.getLearntDynamicBayesianNetwork().getDynamicVariables().getVariableById(this.classIndex); }
private Assignment updateDynamicAssignmentTime0(DynamicAssignment dynamicAssignment){ HashMapAssignment assignment = new HashMapAssignment(); this.model.getDynamicVariables() .getListOfDynamicVariables() .stream() .forEach(var -> { double value = dynamicAssignment.getValue(var); assignment.setValue(var, value); }); return assignment; }
/** * Returns the log probability of a valid assignment at Time T. * @param assignment an object of type {@link Assignment}. * @return a {@code double} that represents the log probability of the assignment. */ public double getLogProbabiltyOfFullAssignmentTimeT(Assignment assignment){ double logProb = 0; for (Variable var: this.getDynamicVariables()){ if (assignment.getValue(var)== Utils.missingValue()) { throw new UnsupportedOperationException("This method can not compute the probabilty of a partial assignment."); } logProb += this.distributionsTimeT.get(var.getVarID()).getLogConditionalProbability(assignment); } return logProb; }
/** * 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; }
/** * Returns the log probability of a valid assignment at Time 0. * @param assignment an object of type {@link Assignment}. * @return a {@code double} that represents the log probability of the assignment. */ public double getLogProbabiltyOfFullAssignmentTime0(Assignment assignment){ double logProb = 0; for (Variable var: this.getDynamicVariables()){ if (assignment.getValue(var) == Utils.missingValue()) { throw new UnsupportedOperationException("This method can not compute the probabilty of a partial assignment."); } logProb += this.distributionsTime0.get(var.getVarID()).getLogConditionalProbability(assignment); } return logProb; }
/** * Return the list of non-observed and temporally connected variables for time T. * @return a {@code List} of {@link Variable}. */ private List<Variable> getTargetVarsTimeT(){ return this.model.getDynamicVariables().getListOfDynamicVariables().stream() .filter(var -> !var.isInterfaceVariable()) .filter(var -> Utils.isMissingValue(this.assignment.getValue(var))) .filter(var -> { boolean notContainInterfaceVar = true; for (Variable variable : this.model.getDynamicDAG().getParentSetTimeT(var)) { notContainInterfaceVar = notContainInterfaceVar && !variable.isInterfaceVariable(); } return !notContainInterfaceVar; }) .collect(Collectors.toList()); }
/** * Creates a new temporal data stream. * @param sampler1 a {@link DynamicBayesianNetworkSampler} object. * @param nSequences1 an {@code int} that represents the number of sequences in the data stream to be sampled. * @param sequenceLength1 an {@code int} that represents the length of each sequence. */ TemporalDataStream(DynamicBayesianNetworkSampler sampler1, int nSequences1, int sequenceLength1){ this.sampler=sampler1; this.nSequences = nSequences1; this.sequenceLength = sequenceLength1; List<Attribute> list = new ArrayList<>(); list.add(new Attribute(0,Attributes.SEQUENCE_ID_ATT_NAME, new RealStateSpace())); list.add(new Attribute(1,Attributes.TIME_ID_ATT_NAME, new RealStateSpace())); list.addAll(this.sampler.network.getDynamicVariables().getListOfDynamicVariables().stream() .filter(var -> !sampler1.getLatentVars().contains(var)) .map(var -> new Attribute(var.getVarID() + 2, var.getName(), var.getStateSpaceType())).collect(Collectors.toList())); this.atts= new Attributes(list); }
/** * 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); }
/** * Initializes the distributions of this DynamicBayesianNetwork. * The initialization is performed for each variable at time ) and T depending on its distribution type * as well as the distribution type of its parent set (if that variable has parents). */ private void initializeDistributions() { //Parents should have been assigned before calling this method (from dynamicmodelling.models) this.distributionsTime0 = new ArrayList(this.getDynamicVariables().getNumberOfVars()); this.distributionsTimeT = new ArrayList(this.getDynamicVariables().getNumberOfVars()); for (Variable var : this.getDynamicVariables()) { int varID = var.getVarID(); /* Distributions at time t */ this.distributionsTimeT.add(varID, var.newConditionalDistribution(this.dynamicDAG.getParentSetTimeT(var).getParents())); this.dynamicDAG.getParentSetTimeT(var).blockParents(); /* Distributions at time 0 */ this.distributionsTime0.add(varID, var.newConditionalDistribution(this.dynamicDAG.getParentSetTime0(var).getParents())); this.dynamicDAG.getParentSetTime0(var).blockParents(); } //distributionsTimeT = Collections.unmodifiableList(this.distributionsTimeT); //distributionsTime0 = Collections.unmodifiableList(this.distributionsTime0); }
public static void generateEvidenceData() throws Exception { DynamicBayesianNetwork network = createDynamicFireDetectorModel(); DynamicBayesianNetworkSampler sampler = new DynamicBayesianNetworkSampler(network); sampler.setSeed(1); sampler.setLatentVar(network.getDynamicVariables().getVariableByName("Temperature")); sampler.setLatentVar(network.getDynamicVariables().getVariableByName("Smoke")); DataStream<DynamicDataInstance> dataStream = sampler.sampleToDataBase(1, 10); DataStreamWriter.writeDataToFile(dataStream, "./datasets/TimeIndexedSensorReadingsEvidence.arff"); }
/** * {@inheritDoc} */ @Override public double getValue(Attribute att, boolean present) { if (att.getIndex() == 0) { return this.sequenceID; } else if (att.getIndex() == 1){ return this.timeID; }else{ return this.getValue(dbn.getDynamicVariables().getVariableById(att.getIndex() - 2)); } }
/** * {@inheritDoc} */ @Override public void setValue(Attribute att, double val, boolean present) { if (att.getIndex() == 0) { this.sequenceID = (int)val; } else if (att.getIndex() == 1){ this.timeID = (int) val; }else { this.setValue(dbn.getDynamicVariables().getVariableById(att.getIndex() - 2), val); } }
public static void generateData() throws Exception { DynamicBayesianNetwork network = createDynamicFireDetectorModel(); DynamicBayesianNetworkSampler sampler = new DynamicBayesianNetworkSampler(network); sampler.setSeed(1); sampler.setLatentVar(network.getDynamicVariables().getVariableByName("Temperature")); sampler.setLatentVar(network.getDynamicVariables().getVariableByName("Smoke")); DataStream<DynamicDataInstance> dataStream = sampler.sampleToDataBase(100,1000); DataStreamWriter.writeDataToFile(dataStream, "./datasets/TimeIndexedSensorReadings.arff"); dataStream = sampler.sampleToDataBase(1,10); DataStreamWriter.writeDataToFile(dataStream, "./datasets/TimeIndexedSensorReadingsEvidence.arff"); } public static void generateEvidenceData() throws Exception {
/** * Returns the Bayesian network at Time 0. * @return a {@link BayesianNetwork} object. */ public BayesianNetwork toBayesianNetworkTime0(){ DAG dagTime0 = this.getDynamicDAG().toDAGTime0(); BayesianNetwork bnTime0 = new BayesianNetwork(dagTime0); for (Variable dynamicVar : this.getDynamicVariables()) { Variable staticVar = dagTime0.getVariables().getVariableByName(dynamicVar.getName()); ConditionalDistribution deepCopy = Serialization.deepCopy(this.getConditionalDistributionTime0(dynamicVar)); deepCopy.setVar(staticVar); List<Variable> newParents = deepCopy.getConditioningVariables().stream().map(var -> dagTime0.getVariables().getVariableByName(var.getName())).collect(Collectors.toList()); deepCopy.setConditioningVariables(newParents); bnTime0.setConditionalDistribution(staticVar,deepCopy); } return bnTime0; }
/** * Returns the Bayesian network at Time T. * @return a {@link BayesianNetwork} object. */ public BayesianNetwork toBayesianNetworkTimeT(){ DAG dagTimeT = this.getDynamicDAG().toDAGTimeT(); BayesianNetwork bnTimeT = new BayesianNetwork(dagTimeT); for (Variable dynamicVar : this.getDynamicVariables()) { Variable staticVar = dagTimeT.getVariables().getVariableByName(dynamicVar.getName()); ConditionalDistribution deepCopy = Serialization.deepCopy(this.getConditionalDistributionTimeT(dynamicVar)); deepCopy.setVar(staticVar); List<Variable> newParents = deepCopy.getConditioningVariables().stream().map(var -> dagTimeT.getVariables().getVariableByName(var.getName())).collect(Collectors.toList()); deepCopy.setConditioningVariables(newParents); bnTimeT.setConditionalDistribution(staticVar,deepCopy); } return bnTimeT; }
/** * Sets the AMIDST evidence into a given time slice of the expanded Hugin model. * @param assignment the evidence to be propagated. * @param time the time slice in which the evidence is entered. */ public void setAssignmentToHuginModel(DynamicAssignment assignment, int time) { List<Variable> dynamicVariables = amidstDBN.getDynamicVariables().getListOfDynamicVariables(); for (Variable var : dynamicVariables) { //Skip non-observed variables if(!Double.isNaN(assignment.getValue(var))){ if (var.isMultinomial()) { try { LabelledDCNode node = (LabelledDCNode) domainObject.getNodeByName("T" + time + "." + var.getName()); node.selectState((int)assignment.getValue(var)); } catch (ExceptionHugin exceptionHugin) { exceptionHugin.printStackTrace(); } } } } }
/** * Tests if two Dynamic Bayesian networks are equals. * Two Dynamic Bayesian networks are considered equals if they have an equal conditional distribution for each variable at both Time 0 and T. * @param bnet a given DynamicBayesianNetwork to be compared with this DynamicBayesianNetwork. * @param threshold a threshold value. * @return a boolean indicating if the two DynamicBNs are equals or not. */ public boolean equalDBNs(DynamicBayesianNetwork bnet, double threshold) { boolean equals = true; if (this.getDynamicDAG().equals(bnet.getDynamicDAG())){ for (Variable var : this.getDynamicVariables()) { equals = equals && this.getConditionalDistributionTime0(var).equalDist(bnet.getConditionalDistributionTime0(var), threshold) && this.getConditionalDistributionTimeT(var).equalDist(bnet.getConditionalDistributionTimeT(var), threshold); } } return equals; }