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; }
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; }
/** * 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); }
@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(); }
/** * 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(); }
/** * {@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()}; } }
System.out.println("Estimated Temperature: " + temp.getMean() +", "+temp.getVariance());
tempsMonth[i] = dist.getNormal(0).getMean();
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"); } }
tempsMonth[i] = dist.getNormal(0).getMean();
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());