private void Calculate(ContingencyTableNode ct, ulong currentBase, ulong index, Dictionary<ulong, int> paCounts, int variable, Varset variables, int previousVariable, ref double score) { // if this is a leaf if (ct.IsLeaf()) { // update the instantiation count of this set of parents int count = paCounts.ContainsKey(index) ? paCounts[index] : 0; count += ct.Value; paCounts[index] = count; // update the score for this variable, parent instantiation score += ilogi[ct.Value]; return; } // which actual variable are we looking at int thisVariable = previousVariable + 1; for (; thisVariable < network.Size(); thisVariable++) { if (variables.Get(thisVariable)) { break; } } // update the base and index if this is part of the parent set ulong nextBase = currentBase; if (thisVariable != variable) { nextBase *= (ulong)network.GetCardinality(thisVariable); } // recurse for (int k = 0; k < network.GetCardinality(thisVariable); k++) { ContingencyTableNode child = ct.GetChild(k); if (child != null) { ulong newIndex = index; if (thisVariable != variable) { newIndex += currentBase * (ulong)k; } Calculate(child, nextBase, newIndex, paCounts, variable, variables, thisVariable, ref score); } } }
public void Subtract(ContingencyTableNode other) { if (IsLeaf()) { value -= other.value; return; } for (int k = 0; k < children.Count; k++) { if (children[k] == null || other.children[k] == null) { continue; } children[k].Subtract(other.children[k]); } }
private ContingencyTableNode MakeContabLeafList(Varset variables, BitArray records) { Varset variablesCp = new Varset(variables); if (variablesCp.Equals(zero)) { int count = 0; for (int i = 0; i < records.Count; i++) { if (records[i]) { count += 1; } } return new ContingencyTableNode(count, 0, 1); } int firstIndex = variables.FindFirst(); int cardinality = network.GetCardinality(firstIndex); ContingencyTableNode ct = new ContingencyTableNode(0, cardinality, 0); variablesCp.Set(firstIndex, false); Varset remainingVariables = new Varset(variablesCp); for (int k = 0; k < cardinality; k++) { BitArray r = new BitArray(recordCount); r = r.Or(records); r = r.And(consistentRecords[firstIndex][k]); int count = 0; for (int i = 0; i < r.Count; i++) { if (r[i]) { count += 1; } } if (count > 0) { ContingencyTableNode child = MakeContabLeafList(remainingVariables, r); ct.SetChild(k, child); ct.LeafCount += child.LeafCount; } } return ct; }
private ContingencyTableNode MakeContab(Varset remainingVariables, ADNode node, int nodeIndex) { // check base case if (remainingVariables.Equals(zero)) { ContingencyTableNode ctn = new ContingencyTableNode(node.Count, 0, 1); return ctn; } int firstIndex = remainingVariables.FindFirst(); int n = network.GetCardinality(firstIndex); VaryNode vn = node.GetChild(firstIndex - nodeIndex - 1); ContingencyTableNode ct = new ContingencyTableNode(0, n, 0); Varset newVariables = Varset.ClearCopy(remainingVariables, firstIndex); ContingencyTableNode ctMcv = MakeContab(newVariables, node, nodeIndex); for (int k = 0; k < n; k++) { if (vn.GetChild(k) == null) { continue; } ADNode adn = vn.GetChild(k); ContingencyTableNode child = null; if (adn.LeafList.Count == 0) // これ注意 { child = MakeContab(newVariables, adn, firstIndex); } else { child = MakeContabLeafList(newVariables, adn.LeafList); } ct.SetChild(k, child); ct.LeafCount += child.LeafCount; ctMcv.Subtract(ct.GetChild(k)); } ct.SetChild(vn.Mcv, ctMcv); ct.LeafCount += ctMcv.LeafCount; return ct; }
private void Calculate(ContingencyTableNode ct, ulong currentBase, ulong index, Dictionary<ulong, int> paCounts, Varset variables, int previousVariable, ref Scratch s) { Varset variablesCp = new Varset(variables); // if this is a leaf in the AD-tree if (ct.IsLeaf()) { // update the instantiation count of this set of parents int count = paCounts.ContainsKey(index) ? paCounts[index] : 0; count += ct.Value; if (count > 0) { paCounts[index] = count; // update the score for this variable, parent instantiation double temp = ScoreCalculator.GammaLn(s.Aijk + ct.Value); s.Score -= s.Lgijk; s.Score += temp; } return; } // which actual variable are we looking at int thisVariable = previousVariable + 1; for (; thisVariable < network.Size(); thisVariable++) { if (variables.Get(thisVariable)) { break; } } // update the base and index if this is part of the parent set ulong nextBase = currentBase; if (thisVariable != s.Variable) { nextBase *= (ulong)network.GetCardinality(thisVariable); } // recurse for (int k = 0; k < network.GetCardinality(thisVariable); k++) { ContingencyTableNode child = ct.GetChild(k); if (child != null) { ulong newIndex = index; if (thisVariable != s.Variable) { newIndex += currentBase * (ulong)k; } Calculate(child, nextBase, newIndex, paCounts, variables, thisVariable, ref s); } } }
public void SetChild(int index, ContingencyTableNode child) { children[index] = child; }