public BDeuScoringFunction(double ess, BayesianNetwork network, ADTree adTree, Constraints constraints)
        {
            this.network = network;
            this.adTree = adTree;
            this.constraints = constraints;
            this.ess = ess;

            for (int x = 0; x < network.Size(); x++)
            {
                Scratch s = new Scratch();
                s.Variable = x;
                s.Lri = Math.Log(network.GetCardinality(x));
                s.invalidParents = new HashSet<ulong>();
                scratchSpace.Add(s);
            }
        }
        private void Lg(Varset parents, ref Scratch s)
        {
            int r = 1;
            for (int pa = 0; pa < network.Size(); pa++)
            {
                if (parents.Get(pa))
                {
                    r *= network.GetCardinality(pa);
                }
            }

            s.Aij = ess / r;
            s.Lgij = ScoreCalculator.GammaLn(s.Aij);
            r *= network.GetCardinality(s.Variable);
            s.Aijk = ess / r;
            s.Lgijk = ScoreCalculator.GammaLn(s.Aijk);
        }
        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);
                }
            }
        }