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 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); }
/** * {@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 normal variable in the Hugin model from the corresponding distribution in the AMIDST * model. This method is designed for setting the distribution in a given position in the list of distributions * indexed by the multinomial parents assignments. * * @param dist the AMIDST distribution to be converted. * @param i the position in which the distribution will be located. Note that <code>i</code> is equal to 0 when the * variable has no multinomial parents. * @throws ExceptionHugin */ private void setNormal(Normal dist, int i) throws ExceptionHugin { Variable amidstVar = dist.getVariable(); Node huginVar = this.huginBN.getNodeByName(amidstVar.getName()); double mean = dist.getMean(); double sd = dist.getSd(); ((ContinuousChanceNode)huginVar).setAlpha(mean, i); ((ContinuousChanceNode)huginVar).setGamma(Math.pow(sd,2),i); }
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"); } }
System.out.println("Estimated Temperature: " + temp.getMean() +", "+temp.getVariance());
tempprob.getNormal(1).setMean(tempprob.getNormal(0).getMean()+10); tempprob.getNormal(1).setVariance(tempprob.getNormal(0).getVariance()); 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());
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; }
normalMultiDist.getNormal(0).setMean(1.0); normalMultiDist.getNormal(0).setVariance(1.0); normalMultiDist.getNormal(1).setMean(0.0); normalMultiDist.getNormal(1).setVariance(1.0);
@Override public double classifyInstance(Instance instance) throws Exception { if(bnModel_ == null) { throw new UnsupportedOperationException("The model was not learnt"); } DataInstance dataInstance = new DataInstanceFromDataRow(new DataRowWeka(instance, this.attributes_)); double realValue = dataInstance.getValue(targetVar_); dataInstance.setValue(targetVar_, eu.amidst.core.utils.Utils.missingValue()); this.inferenceAlgorithm_.setEvidence(dataInstance); this.inferenceAlgorithm_.runInference(); Normal normal = this.inferenceAlgorithm_.getPosterior(targetVar_); dataInstance.setValue(targetVar_, realValue); return normal.getMean(); }
tempprob.getNormal(0).setMean(tempMean); tempprob.getNormal(0).setVariance(3); sensorSmokeProb.getNormal(0).setMean(0); sensorSmokeProb.getNormal(0).setVariance(0.1); sensorSmokeProb.getNormal(1).setMean(5); sensorSmokeProb.getNormal(1).setVariance(3);
/** * This method constains the code needed to learn the model and produce the output. * @param parallelSVB a parallel SVB (Streaming Variational Bayes) object * @return a Bayesian Network * @throws IOException if a file reading error occurs */ public static BayesianNetwork learnModel(ParallelSVB parallelSVB) throws IOException { //We access the hidden var Variable hiddenGaussian = parallelSVB.getSVBEngine().getDAG().getVariables().getVariableByName("HiddenGaussian"); //For each month of the period for (int i = 0; i < MONTHS; i++) { //We load the data for the given month DataStream<DataInstance> monthlyData = DataStreamLoader.open("./datasets/bnaic2015/BCC/Month" + i + ".arff"); //We update the model in parallel with the data from data month parallelSVB.updateModel(monthlyData); //We query the given var Normal normal = parallelSVB.getSVBEngine().getParameterPosterior(hiddenGaussian); //We print the mean of this Gaussian var System.out.println("E(H) at month "+i+":\t" + normal.getMean()); } //Finally we get the learnt Bayesian network and return it. return parallelSVB.getLearntBayesianNetwork(); }
tempprob.getNormal(0).setMean(18); tempprob.getNormal(0).setVariance(3); sensorSmokeProb.getNormal(0).setMean(0); sensorSmokeProb.getNormal(0).setVariance(0.1); sensorSmokeProb.getNormal(1).setMean(5); sensorSmokeProb.getNormal(1).setVariance(3);
/** * {@inheritDoc} */ @Override public double[] getVotesForInstance(Instance instance) { if(bnModel_ == null) { return new double[0]; } DataInstance dataInstance = new DataInstanceFromDataRow(new DataRowWeka(instance, this.attributes_)); double realValue = dataInstance.getValue(targetVar_); dataInstance.setValue(targetVar_, Utils.missingValue()); this.predictions_.setEvidence(dataInstance); this.predictions_.runInference(); Normal normal = this.predictions_.getPosterior(targetVar_); dataInstance.setValue(targetVar_, realValue); return new double[] {normal.getMean()}; } }
tempprob.getNormal(0).setMean(18); tempprob.getNormal(0).setVariance(3); sensorSmokeProb.getNormal(0).setMean(0); sensorSmokeProb.getNormal(0).setVariance(0.1); sensorSmokeProb.getNormal(1).setMean(5); sensorSmokeProb.getNormal(1).setVariance(3); sensorSmokeProbTimeT.getNormal(0).setMean(0); sensorSmokeProbTimeT.getNormal(0).setVariance(0.1); sensorSmokeProbTimeT.getNormal(1).setMean(5); sensorSmokeProbTimeT.getNormal(1).setVariance(3);
tempsMonth[i] = dist.getNormal(0).getMean();
tempprobBuilding.setMean(tempMean); tempprobBuilding.setVariance(3); sensorSmokeProb.getNormal(0).setMean(0); sensorSmokeProb.getNormal(0).setVariance(0.1); sensorSmokeProb.getNormal(1).setMean(5); sensorSmokeProb.getNormal(1).setVariance(3);
tempsMonth[i] = dist.getNormal(0).getMean();