public void TestSampling1() { BayesianNetwork network = new BayesianNetwork(); BayesianEvent a = network.CreateEvent("a"); BayesianEvent b = network.CreateEvent("b"); network.CreateDependency(a, b); network.FinalizeStructure(); a.Table.AddLine(0.5, true); // P(A) = 0.5 b.Table.AddLine(0.2, true, true); // p(b|a) = 0.2 b.Table.AddLine(0.8, true, false);// p(b|~a) = 0.8 network.Validate(); SamplingQuery query = new SamplingQuery(network); query.DefineEventType(a, EventType.Evidence); query.DefineEventType(b, EventType.Outcome); query.SetEventValue(b, true); query.SetEventValue(a, true); query.Execute(); TestPercent(query.Probability, 20); }
/// <inheritdoc/> public Object Read(Stream istream) { BayesianNetwork result = new BayesianNetwork(); EncogReadHelper input = new EncogReadHelper(istream); EncogFileSection section; String queryType = ""; String queryStr = ""; String contentsStr = ""; while ((section = input.ReadNextSection()) != null) { if (section.SectionName.Equals("BAYES-NETWORK") && section.SubSectionName.Equals("BAYES-PARAM")) { IDictionary<String, String> p = section.ParseParams(); queryType = p["queryType"]; queryStr = p["query"]; contentsStr = p["contents"]; } if (section.SectionName.Equals("BAYES-NETWORK") && section.SubSectionName.Equals("BAYES-TABLE")) { result.Contents = contentsStr; // first, define relationships (1st pass) foreach (String line in section.Lines) { result.DefineRelationship(line); } result.FinalizeStructure(); // now define the probabilities (2nd pass) foreach (String line in section.Lines) { result.DefineProbability(line); } } if (section.SectionName.Equals("BAYES-NETWORK") && section.SubSectionName.Equals("BAYES-PROPERTIES")) { IDictionary<String, String> paras = section.ParseParams(); EngineArray.PutAll(paras, result.Properties); } } // define query, if it exists if (queryType.Length > 0) { IBayesianQuery query = null; if (queryType.Equals("EnumerationQuery")) { query = new EnumerationQuery(result); } else { query = new SamplingQuery(result); } if (query != null && queryStr.Length > 0) { result.Query = query; result.DefineClassificationStructure(queryStr); } } return result; }
public void TestSampling2() { BayesianNetwork network = new BayesianNetwork(); BayesianEvent a = network.CreateEvent("a"); BayesianEvent x1 = network.CreateEvent("x1"); BayesianEvent x2 = network.CreateEvent("x2"); BayesianEvent x3 = network.CreateEvent("x3"); network.CreateDependency(a, x1, x2, x3); network.FinalizeStructure(); a.Table.AddLine(0.5, true); // P(A) = 0.5 x1.Table.AddLine(0.2, true, true); // p(x1|a) = 0.2 x1.Table.AddLine(0.6, true, false);// p(x1|~a) = 0.6 x2.Table.AddLine(0.2, true, true); // p(x2|a) = 0.2 x2.Table.AddLine(0.6, true, false);// p(x2|~a) = 0.6 x3.Table.AddLine(0.2, true, true); // p(x3|a) = 0.2 x3.Table.AddLine(0.6, true, false);// p(x3|~a) = 0.6 network.Validate(); SamplingQuery query = new SamplingQuery(network); query.DefineEventType(x1, EventType.Evidence); query.DefineEventType(x2, EventType.Evidence); query.DefineEventType(x3, EventType.Evidence); query.DefineEventType(a, EventType.Outcome); query.SetEventValue(a, true); query.SetEventValue(x1, true); query.SetEventValue(x2, true); query.SetEventValue(x3, false); query.Execute(); TestPercent(query.Probability, 18); }