Skip to main content

Value of Information with a Bayesian network in Matlab

% There are a number of ways you can tell Matlab about the Bayes Server API
% Here is one way. For more information please see the Matlab Java help

% clear java;

bayesVersion = '7.24'; % TODO change to the version you are using
networkPath = ['C:\ProgramData\Bayes Server ', bayesVersion, '\Sample Networks\Waste.bayes']; % TODO update to the path of the Bayes Server sample network Waste.bayes
jarPath = ['C:\Program Files\Bayes Server\Bayes Server ', bayesVersion, '\API\Java\BayesServer-', bayesVersion, '.jar']; % TODO update to the path of the Bayes Server jar
javaaddpath(jarPath);

import com.bayesserver.*;
import com.bayesserver.inference.*;
import com.bayesserver.analysis.*;

% For licensed software, uncomment the following line, and replace the text with your license key
% License.validate('license-key-goes-here');

% load an existing network
network = Network();
network.load(networkPath);

disp(['Network has ', num2str(network.getNodes.size), ' nodes']);

filterState = network.getVariables().get('Filter state', true);
filterStateIntact = filterState.getStates().get('Intact', true);
dustEmission = network.getVariables().get('Dust emission', true);
wasteType = network.getVariables().get('Waste type', true);
wasteTypeIndustrial = wasteType.getStates().get('Industrial', true);
wasteTypeHousehold = wasteType.getStates().get('Household', true);

evidence = DefaultEvidence(network);
options = ValueOfInformationOptions();

output = ValueOfInformation.calculate(wasteType, network.getVariables(), evidence, RelevanceTreeInferenceFactory(), options);

disp(['Hypothesis statistic = ', num2str(output.getHypothesisStatistic())]);

testOutputs = output.getTestOutputs();

for i=0:testOutputs.size()-1
testOutput = testOutputs.get(i);
variableName = testOutput.getVariable().getVariable().getName();
hypothesisStat = testOutput.getHypothesisStatistic().doubleValue();
disp([char(variableName), ', MI = ', num2str(hypothesisStat)])
end