using BayesServer.Inference.RelevanceTree;
using System;
using System.Collections.Generic;
using System.Text;
namespace BayesServer.HelpSamples
{
public static class InferenceHybrid
{
public static void Main()
{
var network = new Network();
network.Load(@"Waste.bayes");
Console.WriteLine($"Network has {network.Variables.Count} variables.");
var wasteType = network.Variables["Waste type", true];
var co2Concentration = network.Variables["CO2 concentration", true];
var filterEfficiency = network.Variables["Filter efficiency", true];
var metalsInWaste = network.Variables["Metals in waste", true];
var metalsEmission = network.Variables["Metals emission", true];
var lightPenetrability = network.Variables["Light penetrability", true];
var factory = new RelevanceTreeInferenceFactory();
var inference = factory.CreateInferenceEngine(network);
var queryOptions = factory.CreateQueryOptions();
queryOptions.LogLikelihood = true;
var queryOutput = factory.CreateQueryOutput();
var evidence = inference.Evidence;
var industrial = wasteType.States["Industrial", true];
evidence.SetState(industrial);
evidence.Set(co2Concentration, -1.0);
var queryDistributions = inference.QueryDistributions;
var filterState = network.Variables["Filter state", true];
var queryFilterState = new Table(filterState);
queryDistributions.Add(queryFilterState);
var queryFilterEfficiency = new CLGaussian(filterEfficiency);
queryDistributions.Add(queryFilterEfficiency);
var queryJoint = new CLGaussian(new Variable[] { lightPenetrability, metalsEmission });
queryDistributions.Add(queryJoint);
inference.Query(queryOptions, queryOutput);
Console.WriteLine($"Log-likelihood: {queryOutput.LogLikelihood}");
var filterEfficiencyMean = queryFilterEfficiency.GetMean(filterEfficiency);
var filterEfficiencyVariance = queryFilterEfficiency.GetVariance(filterEfficiency);
Console.WriteLine($"{filterEfficiencyMean}, {Math.Sqrt(filterEfficiencyVariance)}");
foreach (var state in filterState.States)
{
Console.WriteLine($"Filter state: {state.Name} {queryFilterState[state]}");
}
Console.WriteLine($"Joint covariance = {queryJoint.GetCovariance(lightPenetrability, metalsEmission)}");
}
}
}