/// <summary> /// Creates a Bayesian Network from a given DagPattern which has to be a DAG and uses the given distributions if they fit the DAG structure. /// </summary> /// <exception cref="ArgumentException">Thrown when the given DagPattern is no DAG or the distributions don't fit the DAG structure</exception> public static BayesianNetwork FromDagPattern(DagPattern <RandomVariable> dag, IEnumerable <ProbabilityDistribution> distributions) { if (!dag.IsDag()) { throw new ArgumentException("BayesianNetworks need a DAG, but the given DAG was actually a DAG pattern!"); } var network = new BayesianNetwork(dag); var processed = new bool[network.RandomVariables.Count]; foreach (var distribution in distributions) { var rvar = distribution.RandomVariable; var parents = dag.GetParents(rvar); // do the conditions fit with the dag structure? if (parents.Except(distribution.Conditions).Any() || parents.Count != distribution.Conditions.Count) { throw new ArgumentException($"The random variable {rvar.Name} has parents {string.Join(",", parents)} but conditions {string.Join(",", distribution.Conditions)}"); } network.Distributions[rvar] = distribution; processed[network.RandomVariables.IndexOf(rvar)] = true; // fill distributions in random variables without parents if (parents.Count == 0) { rvar.Probability = distribution.Distribution; } } if (!processed.All(p => p)) { throw new ArgumentException("The given probability distribution did not include all random variables!"); } return(network); }
public ConstraintBasedStructureLearner(IList <RandomVariable> randomVariables, DualKeyDictionary <RandomVariable, ICollection <ISet <RandomVariable> > > independencies) { _randomVariables = randomVariables; _independencies = independencies; _dag = DagPattern <RandomVariable> .InitCompleteDag(randomVariables); _collectedConditions = new DualKeyDictionary <RandomVariable, ISet <RandomVariable> >(); foreach (var x in _dag.Nodes) { foreach (var y in _dag.Nodes.Where(y => x != y)) { _collectedConditions[x, y] = new HashSet <RandomVariable>(); } } }
/// <summary> /// Creates a Bayesian Network from a given DagPattern which has to be a DAG and calculates the conditional distributions according to the DAG structure. /// </summary> /// <exception cref="ArgumentException">Thrown when the given DagPattern is no DAG</exception> public static BayesianNetwork FromDagPattern(DagPattern <RandomVariable> dag, IProbabilityDistributionCalculator calc) { if (!dag.IsDag()) { throw new ArgumentException("BayesianNetworks need a DAG, but the given DAG was actually a DAG pattern!"); } var network = new BayesianNetwork(dag); foreach (var rvar in dag.Nodes) { var parents = dag.GetParents(rvar).ToList(); var distribution = calc.CalculateConditionalProbabilityDistribution(rvar, parents); network.Distributions[rvar] = new ProbabilityDistribution(rvar, parents, distribution); } return(network); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var randomVariableNames = _randomVariables.Select(rvar => rvar.Name).ToList(); var randomVariableMapping = new Dictionary <string, RandomVariable>(); foreach (var randomVariable in _randomVariables) { randomVariableMapping[randomVariable.Name] = randomVariable; } var json = JObject.Load(reader); // construct DagPattern var dag = json.Property(DagProperty); var edges = dag.Value.Value <JArray>(EdgesProperty).ToObject <int[]>(); var nodes = dag.Value.Value <JArray>(NodesProperty).ToObject <string[]>(); var realEdges = new int[nodes.Length, nodes.Length]; for (var i = 0; i < nodes.Length; i++) { for (var j = 0; j < nodes.Length; j++) { // given random variables could be in another order, so lookup the index realEdges[randomVariableNames.IndexOf(nodes[i]), randomVariableNames.IndexOf(nodes[j])] = edges[i * nodes.Length + j]; } } var dagPattern = DagPattern <RandomVariable> .InitDagWithMatrix(_randomVariables, realEdges); // construct probability distributions var distributions = json.Property(DistributionsProperty).Value.Children(); var realDistributions = new List <ProbabilityDistribution>(); foreach (var distribution in distributions) { var randomVariable = distribution.Value <string>(RandomVariableProperty); var realRandomVariable = randomVariableMapping[randomVariable]; var conditions = distribution.Value <JArray>(ConditionsProperty).ToObject <string[]>(); var realConditions = conditions.Select(condition => randomVariableMapping[condition]).ToList(); var distributionValues = distribution.Value <JArray>(DistributionProperty).ToObject <double[]>(); var realDistributionValues = distributionValues.Select(distValue => new Probability(distValue)).ToList(); realDistributions.Add(new ProbabilityDistribution(realRandomVariable, realConditions, realDistributionValues)); } return(BayesianNetwork.FromDagPattern(dagPattern, realDistributions)); }
private BayesianNetwork ToBayesianNetwork(BayesianNetworkResult result) { var allVariables = MapToAllVariables(); var nodes = result.Nodes.Select(node => allVariables[node]).ToList(); var dag = DagPattern <RandomVariable> .InitEmptyDag(nodes); foreach (var arc in result.Arcs) { dag.AddEdge(allVariables[arc.From], allVariables[arc.To]); } var distributions = new List <ProbabilityDistribution>(); foreach (var probTable in result.ProbTables) { var rvar = allVariables[probTable.Rvar.First()]; var conditions = probTable.Conditions.Select(condition => allVariables[condition]).ToList(); var probabilities = ToProbabilityArray(probTable.Probs, conditions.Count + 1); distributions.Add(new ProbabilityDistribution(rvar, conditions, probabilities)); } return(BayesianNetwork.FromDagPattern(dag, distributions)); }
private BayesianNetwork(DagPattern <RandomVariable> dag) { Dag = dag; Distributions = new Dictionary <RandomVariable, ProbabilityDistribution>(); }