/** * Initializes the distributions of this DynamicBayesianNetwork randomly. * @param random an object of type {@link java.util.Random}. */ public void randomInitialization(Random random){ this.distributionsTimeT.stream().forEach(w -> w.randomInitialization(random)); this.distributionsTime0.stream().forEach(w -> w.randomInitialization(random)); }
fireprob.setProbabilities(new double[]{0.999, 0.001}); tempprob.getNormal(1).setMean(tempprob.getNormal(0).getMean()+10); tempprob.getNormal(1).setVariance(tempprob.getNormal(0).getVariance()); smokeProb.getMultinomial(1).setProbabilities(new double[]{0.001, 0.999}); fireprobTimeT.getMultinomial(0).setProbabilities(new double[]{0.999, 0.001}); fireprobTimeT.getMultinomial(1).setProbabilities(new double[]{0.01, 0.99}); smokeProbTimeT.getMultinomial(1).setProbabilities(new double[]{0.001, 0.999}); smokeProbTimeT.getMultinomial(3).setProbabilities(new double[]{0.001, 0.999}); tempTimeT.getNormal_NormalParentsDistribution(1).setIntercept(tempprob.getNormal(0).getMean()+10); tempTimeT.getNormal_NormalParentsDistribution(1).setCoeffForParent(temperature.getInterfaceVariable(),0.0); tempTimeT.getNormal_NormalParentsDistribution(1).setVariance(tempprob.getNormal(0).getVariance());
/** * Sets the distribution of a normal variable in the AMIDST model from the ith-normal distribution in the list * of multinomial parent assignments for this variable in the Hugin model. * @param huginVar the Hugin variable with the distribution to be converted. * @param normal the <code>Normal</code> distribution to be modified. * @param i the position in which an assignment of the multinomial parents is indexed in Hugin. This is needed to * obtain the mean and variance of the normal distribution associated to the assignment. * Note that <code>i</code> is equal to 0 when the variable has no multinomial parents. * @throws ExceptionHugin */ private void setNormal(Node huginVar, Normal normal, int i) throws ExceptionHugin { double huginMean = ((ContinuousChanceNode)huginVar).getAlpha(i); double huginVariance = ((ContinuousChanceNode)huginVar).getGamma(i); normal.setMean(huginMean); normal.setVariance(huginVariance); }
public static void main(String[] args) throws Exception { //Load the learnt model BayesianNetwork fireDetector = BayesianNetworkLoader.loadFromFile("./models/LearntFireDetectorModel.bn"); //Access the variable of interest. Variable fire = fireDetector.getVariables().getVariableByName("Fire"); Variable temperature = fireDetector.getVariables().getVariableByName("Temperature"); Variable smoke = fireDetector.getVariables().getVariableByName("Smoke"); //Modify the parameters of the model according to our prior knowledge. Multinomial fireprob = fireDetector.getConditionalDistribution(fire); fireprob.setProbabilities(new double[]{0.999, 0.001}); Normal_MultinomialParents tempprob = fireDetector.getConditionalDistribution(temperature); tempprob.getNormal(1).setMean(tempprob.getNormal(0).getMean()+10); tempprob.getNormal(1).setVariance(tempprob.getNormal(0).getVariance()); Multinomial_MultinomialParents smokeProb = fireDetector.getConditionalDistribution(smoke); smokeProb.getMultinomial(1).setProbabilities(new double[]{0.001, 0.999}); //Print the model System.out.println(fireDetector); //Save to disk the new model BayesianNetworkWriter.save(fireDetector,"./models/FireDetectorModel.bn"); } }
multinomial.setProbabilities(new double[]{0.2, 0.8}); normalMultiDist.getNormal(0).setMean(1.0); normalMultiDist.getNormal(0).setVariance(1.0); normalMultiDist.getNormal(1).setMean(0.0); normalMultiDist.getNormal(1).setVariance(1.0);
/** * {@inheritDoc} */ @Override public <E extends UnivariateDistribution> E getPosterior(Variable var) { try { Node huginNode = huginBN.getNodeByName(var.getName()); if (var.isMultinomial()) { Multinomial dist = new Multinomial(var); for(int i=0;i<var.getNumberOfStates();i++){ dist.setProbabilityOfState(i, ((DiscreteNode) huginNode).getBelief(i)); } return (E)dist; } else if (var.isNormal()) { Normal dist = new Normal(var); dist.setMean(((ContinuousChanceNode)huginNode).getMean()); dist.setVariance(((ContinuousChanceNode) huginNode).getVariance()); return (E)dist; } else { throw new IllegalArgumentException("Variable type not allowed."); } } catch (ExceptionHugin exceptionHugin) { exceptionHugin.printStackTrace(); } return null; }
/** * Sets the distribution of a multinomial variable with multinomial parents in the Hugin model from the * corresponding distribution in the AMIDST model. * * @param dist the AMIDST distribution to be converted. * @throws ExceptionHugin */ private void setMultinomial_MultinomialParents(Multinomial_MultinomialParents dist) throws ExceptionHugin { Variable amidstVar = dist.getVariable(); Node huginVar = this.huginBN.getNodeByName(amidstVar.getName()); List<Multinomial> probabilities = dist.getMultinomialDistributions(); List<Variable> conditioningVariables = dist.getConditioningVariables(); int numParentAssignments = MultinomialIndex.getNumberOfPossibleAssignments(conditioningVariables); int nStates = amidstVar.getNumberOfStates(); int sizeArray = numParentAssignments * nStates; double[] finalArray = new double[sizeArray]; for(int i=0;i<numParentAssignments;i++){ double[] sourceArray = probabilities.get(i).getProbabilities(); System.arraycopy(sourceArray, 0, finalArray, i*nStates, nStates); } huginVar.getTable().setData(finalArray); }
public double[] updateModelWithNewTimeSlice(DataFlink<DataInstance> data){ svb.updateModel(data); double[] out = new double[hiddenVars.size()]; for (int i = 0; i < out.length; i++) { Variable hiddenVar = this.hiddenVars.get(i); Normal normal = svb.getParameterPosterior(hiddenVar); out[i] = normal.getMean(); } return out; }
/** * Sets the distribution of a multinomial variable in the Hugin model from the corresponding distribution in the * AMIDST model. * @param dist the AMIDST distribution to be converted. * @throws ExceptionHugin */ private void setMultinomial(Multinomial dist) throws ExceptionHugin { Variable amidstVar = dist.getVariable(); Node huginVar = this.huginBN.getNodeByName(amidstVar.getName()); int nStates = amidstVar.getNumberOfStates(); double[] finalArray = new double[nStates]; double[] sourceArray = dist.getProbabilities(); System.arraycopy(sourceArray, 0, finalArray, 0, nStates); huginVar.getTable().setData(finalArray); }
/** * Sets the distribution of a normal variable with multinomial and normal parents in the Hugin model from the * corresponding distribution in the AMIDST model. * @param dist the AMIDST distribution to be converted. * @throws ExceptionHugin */ private void setNormal_MultinomialNormalParents(Normal_MultinomialNormalParents dist) throws ExceptionHugin { List<Variable> multinomialParents = dist.getMultinomialParents(); int numParentAssignments = MultinomialIndex.getNumberOfPossibleAssignments(multinomialParents); for(int i=0;i<numParentAssignments;i++) { this.setNormal_NormalParents(dist.getNormal_NormalParentsDistribution(i),i); } }
/** * Sets the distribution of a normal variable with multinomial parents in the Hugin model from the corresponding * distribution in the AMIDST model. * @param dist the AMIDST distribution to be converted. * @throws ExceptionHugin */ private void setNormal_MultinomialParents(Normal_MultinomialParents dist) throws ExceptionHugin { List<Variable> conditioningVariables = dist.getConditioningVariables(); int numParentAssignments = MultinomialIndex.getNumberOfPossibleAssignments(conditioningVariables); for(int i=0;i<numParentAssignments;i++) { Normal normal = dist.getNormal(i); this.setNormal(normal, i); } }
/** * Samples an {@link Assignment} randomly from a {@link BayesianNetwork} conditioned to a given Assignment. * The assignment must only contains assignments from head nodes. * @param random, a random number generator * @param conditionAssignment, an {@link Assignment} object. * @return A sampled Assignment */ public static Assignment sample(BayesianNetwork network, List<Variable> causalOrder, Random random, Assignment conditionAssignment) { HashMapAssignment assignment = new HashMapAssignment(network.getNumberOfVars()); for (Variable var : causalOrder) { if (Utils.isMissingValue(conditionAssignment.getValue(var))) { double sampledValue = network.getConditionalDistribution(var).getUnivariateDistribution(assignment).sample(random); assignment.setValue(var, sampledValue); }else{ assignment.setValue(var,conditionAssignment.getValue(var)); } } return assignment; }
/** * 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; }
/** * Creates a new EF_BayesianNetwork object given a {@link DynamicBayesianNetwork} object. * @param dbn a {@link DynamicBayesianNetwork} object. */ public EF_DynamicBayesianNetwork(DynamicBayesianNetwork dbn){ this.bayesianNetworkTime0 = new EF_BayesianNetwork(); this.bayesianNetworkTimeT = new EF_BayesianNetwork(); this.bayesianNetworkTime0.setDistributionList(dbn.getConditionalDistributionsTime0().stream().map(dist -> dist.<EF_ConditionalDistribution>toEFConditionalDistribution()).collect(Collectors.toList())); this.bayesianNetworkTimeT.setDistributionList(dbn.getConditionalDistributionsTimeT().stream().map(dist -> dist.<EF_ConditionalDistribution>toEFConditionalDistribution()).collect(Collectors.toList())); }
/** * 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); }
fireprobTime0.setProbabilities(new double[]{1.0, 0.0}); tempprob.getNormal(0).setMean(18); tempprob.getNormal(0).setVariance(3); t1prob.setCoeffForParent(temperature, 1.0); t1prob.setIntercept(0.0); t1prob.setVariance(1.5); t2prob.setCoeffForParent(temperature, 1.0); t2prob.setIntercept(0.0); t2prob.setVariance(1.5); smokeProb.getMultinomial(0).setProbabilities(new double[]{0.9, 0.1}); sensorSmokeProb.getNormal(0).setMean(0); sensorSmokeProb.getNormal(0).setVariance(0.1); sensorSmokeProb.getNormal(1).setMean(5); sensorSmokeProb.getNormal(1).setVariance(3); fireprobTimeT.getMultinomial(0).setProbabilities(new double[]{1.0, 0.0}); tempprobTimeT.getNormal_NormalParentsDistribution(0).setIntercept(0); tempprobTimeT.getNormal_NormalParentsDistribution(0).setCoeffForParent(temperature.getInterfaceVariable(),1); tempprobTimeT.getNormal_NormalParentsDistribution(0).setVariance(0.00001); t1probTimeT.setCoeffForParent(temperature, 1.0); t1probTimeT.setIntercept(0.0); t1probTimeT.setVariance(1.5);
public double[] updateModelWithNewTimeSlice(int timeSlice, DataFlink<DynamicDataInstance> data){ svb.updateModelWithNewTimeSlice(timeSlice,data); double[] out = new double[hiddenVars.size()]; for (int i = 0; i < out.length; i++) { Variable hiddenVar = this.hiddenVars.get(i); if (timeSlice == 0) { Normal normal = svb.getParameterPosteriorTime0(hiddenVar); out[i] = normal.getMean(); }else{ Normal normal = svb.getParameterPosteriorTimeT(hiddenVar); out[i] = normal.getMean(); } } return out; }
fireprob.setProbabilities(new double[]{1.0, 0.0}); tempprob.getNormal(0).setMean(tempMean); tempprob.getNormal(0).setVariance(3); t1prob.setCoeffForParent(temperature, 1.0); t1prob.setIntercept(0.0); t1prob.setVariance(1.5); t2prob.setCoeffForParent(temperature, 1.0); t2prob.setIntercept(0.0); t2prob.setVariance(1.5); smokeProb.getMultinomial(0).setProbabilities(new double[]{0.9, 0.1}); sensorSmokeProb.getNormal(0).setMean(0); sensorSmokeProb.getNormal(0).setVariance(0.1); sensorSmokeProb.getNormal(1).setMean(5); sensorSmokeProb.getNormal(1).setVariance(3);
tempprobBuilding.setMean(tempMean); tempprobBuilding.setVariance(3); fireprob.setProbabilities(new double[]{1.0, 0.0}); tempprob.getNormal_NormalParentsDistribution(0).setIntercept(0.0); tempprob.getNormal_NormalParentsDistribution(0).setCoeffForParent(temperatureBuilding,1.0); tempprob.getNormal_NormalParentsDistribution(0).setVariance(1); tempprob.getNormal_NormalParentsDistribution(1).setIntercept(10.0); tempprob.getNormal_NormalParentsDistribution(1).setCoeffForParent(temperatureBuilding,1.0); tempprob.getNormal_NormalParentsDistribution(1).setVariance(3); t1prob.setCoeffForParent(temperature[i], 1.0); t1prob.setIntercept(0.0); t1prob.setVariance(1.5); t2prob.setCoeffForParent(temperature[i], 1.0); t2prob.setIntercept(0.0); t2prob.setVariance(1.5); smokeProb.getMultinomial(0).setProbabilities(new double[]{0.9, 0.1}); sensorSmokeProb.getNormal(0).setMean(0); sensorSmokeProb.getNormal(0).setVariance(0.1); sensorSmokeProb.getNormal(1).setMean(5); sensorSmokeProb.getNormal(1).setVariance(3);
fireprob.setProbabilities(new double[]{1.0, 0.0}); tempprob.getNormal(0).setMean(18); tempprob.getNormal(0).setVariance(3); t1prob.setCoeffForParent(temperature, 1.0); t1prob.setIntercept(0.0); t1prob.setVariance(1.5); t2prob.setCoeffForParent(temperature, 1.0); t2prob.setIntercept(0.0); t2prob.setVariance(1.5); smokeProb.getMultinomial(0).setProbabilities(new double[]{0.9, 0.1}); sensorSmokeProb.getNormal(0).setMean(0); sensorSmokeProb.getNormal(0).setVariance(0.1); sensorSmokeProb.getNormal(1).setMean(5); sensorSmokeProb.getNormal(1).setVariance(3);