public Scenario(string id, FObservation evidence, BayesianNetwork network) { _posteriorMarginals = new Dictionary <string, FDiscreteDistribution>(); _bayesianNetwork = network; Id = id; Evidence = evidence; }
public Scenario(string id, FObservation evidence, BayesianNetwork network) { _posteriorMarginals = new Dictionary<string, FDiscreteDistribution>(); _bayesianNetwork = network; Id = id; Evidence = evidence; }
void OnScenarioInferenceUpdated(IScenario scenario) { if (scenario.BayesianNetwork != Model.BayesianNetwork) { // Old inference result. return; } // Grab a copy of the results. var marginals = scenario.PosteriorMarginals; FObservation evidence = scenario.Evidence; var abbreviations = scenario.Workbench.BayesianNetworkVariableAbbreviations; Dispatcher.Invoke(delegate { // Make sure the scenario still exists by the time this is invoked. if (!Model.Scenarios.Any(s => s.Id == scenario.Id)) { return; } // Recover scenario id. int scenarioId = Convert.ToInt32(scenario.Id); // Update graph inspector with posteriors. xGraphInspector.SetInferenceResults( scenario.PosteriorMarginals, scenarioId, evidence); // Update distribution inspectors. if (Model.SelectedVariable != null && scenario.Id == "1") { var selectedVariable = Model .BayesianNetwork .GetVariable(Model.SelectedVariable); var selectedVariableParents = selectedVariable .Parents .Select(p => Model.BayesianNetwork.GetVariable(p)) .ToList(); DiscreteDistribution distribution; scenario.PosteriorMarginals.TryGetValue( Model.SelectedVariable, out distribution); xMarginalPosteriorDistributions.SetDistribution( selectedVariable, abbreviations, selectedVariableParents, distribution); } WriteMessage("updating posteriors..."); }); }
protected void ScenariosChanged(object sender, NotifyCollectionChangedEventArgs e) { var scenariosNew = (e.NewItems ?? new object[0]).Cast <Scenario>(); var scenariosOld = (e.OldItems ?? new object[0]).Cast <Scenario>(); lock (_scenariosInternal) { // Remove all. if (e.Action == NotifyCollectionChangedAction.Reset) { _scenariosInternal.Clear(); } // Remove individuals. else if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Replace) { foreach (var old in scenariosOld) { var oldRecord = _scenariosInternal .FirstOrDefault(o => o.Scenario == old); _scenariosInternal.Remove(oldRecord); } } // Add new. else if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace) { foreach (var @new in scenariosNew) { @new.Workbench = this; FObservation evidence = @new.Evidence; InferenceQuery query = new InferenceQuery( @new.BayesianNetwork, evidence); query.WarmupSize = this.InferenceWarmupSize; query.ParticleSeparation = this.InferenceParticleSeparation; ScenarioRecord record = new ScenarioRecord( @new, query); _scenariosInternal.Add(record); } } } }
public static DistributionSet ToDistributionSet(this JToken json0) { JArray json = (JArray)json0; List <Tuple <FObservation, DiscreteDistribution> > distributions = new List <Tuple <FObservation, DiscreteDistribution> >(); foreach (var d_j in json.Values <JObject>()) { FObservation o = d_j["key"].ToObservation(); DiscreteDistribution d = d_j["distribution"].ToDiscreteDistribution(); distributions.Add(new Tuple <FObservation, DiscreteDistribution>(o, d)); } DistributionSet set = new DistributionSet(distributions); return(set); }
public void SetInferenceResults( IDictionary <string, DiscreteDistribution> results, int scenarioId, FAI.Bayesian.Observation evidence) { if (scenarioId <= 0) { throw new ArgumentOutOfRangeException("ScenarioId"); } if (results != null && results.Count != 0) { foreach (var node in _nodes) { FRandomVariable variable = (FRandomVariable)node.Tag; DiscreteDistribution distribution; // See if this variable has a result in the incoming results. if (results.TryGetValue(variable.Name, out distribution)) { bool isEvidence = evidence.TryValueForVariable(variable.Name) != null; var slices = distribution .Masses .OrderBy(p => p.Key) .Select(p => p.Value) .ToArray(); node.SetSlices(slices, scenarioId, isEvidence); } } } else { foreach (var node in _nodes) { FRandomVariable variable = (FRandomVariable)node.Tag; bool isEvidence = evidence != null && evidence.TryValueForVariable(variable.Name) != null; node.SetSlices(new double[0], scenarioId, isEvidence); } } }
public void SetData( RandomVariable variable, IDictionary<string,string> variableAbbreviations, FDiscreteDistribution distribution, FObservation conditionedOn, IEnumerable<RandomVariable> parents) { _variable = variable; _distribution = distribution; _conditionedOn = conditionedOn; _variableAbbreviations = variableAbbreviations; _parents = parents .Where(p => conditionedOn.Any(kvp => kvp.Key == p.Name)) .ToArray(); RefreshUI(); }
public void SetData( RandomVariable variable, IDictionary <string, string> variableAbbreviations, FDiscreteDistribution distribution, FObservation conditionedOn, IEnumerable <RandomVariable> parents) { _variable = variable; _distribution = distribution; _conditionedOn = conditionedOn; _variableAbbreviations = variableAbbreviations; _parents = parents .Where(p => conditionedOn.Any(kvp => kvp.Key == p.Name)) .ToArray(); RefreshUI(); }
internal void RequestConfigureVariableWithEvidence(RandomVariable rv, int scenarioId, float?value) { if (scenarioId <= 0) { throw new ArgumentOutOfRangeException("ScenarioId"); } // Find existing scenario. IScenario oldScenario; oldScenario = Model.Scenarios.FirstOrDefault(s => s.Id == scenarioId.ToString()); if (oldScenario == null) { oldScenario = new Scenario(scenarioId.ToString(), new FObservation(), Model.BayesianNetwork); } else { oldScenario.InferenceUpdated -= OnScenarioInferenceUpdated; oldScenario.InferenceFinished -= OnScenarioInferenceFinished; } FObservation oldEvidence = oldScenario.Evidence; FObservation newEvidence; // Build new evidence. if (value == null) { newEvidence = FObservation.op_DotMinusDot(oldEvidence, rv.Name); } else { newEvidence = FObservation.op_DotPlusDot( oldEvidence, new Tuple <string, float>(rv.Name, value.Value) ); } if (object.Equals(newEvidence, oldEvidence)) { return; } // Create new scenario. IScenario newScenario = new Scenario(scenarioId.ToString(), newEvidence, Model.BayesianNetwork); newScenario.InferenceUpdated += OnScenarioInferenceUpdated; newScenario.InferenceFinished += OnScenarioInferenceFinished; Model.Scenarios.Remove(oldScenario); Model.Scenarios.Add(newScenario); // Update UI immediately. { xGraphInspector.SetInferenceResults(null, scenarioId, newEvidence); var evidencesForEvidenceInspector = Model.Scenarios.ToDictionary( e => string.Format("Scenario {0}", e.Id), e => new Tuple <FObservation, FRandomVariable[]>(e.Evidence, e.Workbench.BayesianNetwork.VariablesOrdered.ToArray()) ); xEvidenceInspector.SetEvidence(evidencesForEvidenceInspector, this.Model.BayesianNetworkVariableAbbreviations); } // Reset to simple node selection state. Model.SelectedVariable = rv.Name; Model.SelectedVariableMode = Mode.Inspecting; }
public static JObject ToJObject(this FAI.Bayesian.Observation o) { return(new JObject( o.Select(p => new JProperty(p.Key, p.Value)) )); }