예제 #1
0
        /// <summary>
        /// Use this model to evaluate a context and return an array of the likelihood of each outcome given that context.
        /// </summary>
        /// <param name="context">The names of the predicates which have been observed at the present decision point.</param>
        /// <param name="values">This is where the distribution is stored.</param>
        /// <param name="outsums">The outsums.</param>
        /// <returns>The normalized probabilities for the outcomes given the context. The indexes of the double[] are the outcome ids, and the actual string representation of the outcomes can be obtained from the method getOutcome(int i).</returns>
        public double[] Eval(string[] context, float[] values, double[] outsums)
        {
            var items = new int[context.Length];

            for (int i = 0; i < context.Length; i++)
            {
                items[i] = map[context[i]];
            }
            prior.LogPrior(outsums, items, values);

            return(Eval(items, values, outsums, evalParameters));
        }
예제 #2
0
        private void Compute(int threadIndex, int startIndex, int length)
        {
            var modelDistribution = new double[numOutcomes];

            // to avoid unnecessary comparisons during the loop
            var token = monitor != null
                ? monitor.Token
                : new CancellationToken();

            for (var ei = startIndex; ei < startIndex + length; ei++)
            {
                token.ThrowIfCancellationRequested();

                if (values != null)
                {
                    prior.LogPrior(modelDistribution, contexts[ei], values[ei]);
                    GISModel.Eval(contexts[ei], values[ei], modelDistribution, evalParams);
                }
                else
                {
                    prior.LogPrior(modelDistribution, contexts[ei]);
                    GISModel.Eval(contexts[ei], modelDistribution, evalParams);
                }

                for (var j = 0; j < contexts[ei].Length; j++)
                {
                    var pi = contexts[ei][j];

                    if (predicateCounts[pi] < cutoff)
                    {
                        continue;
                    }

                    var activeOutcomes = modelExpects[threadIndex][pi].Outcomes;
                    for (var aoi = 0; aoi < activeOutcomes.Length; aoi++)
                    {
                        var oi = activeOutcomes[aoi];

                        if (values != null && values[ei] != null)
                        {
                            modelExpects[threadIndex][pi].UpdateParameter(aoi, modelDistribution[oi] * values[ei][j] * numTimesEventsSeen[ei]);
                        }
                        else
                        {
                            modelExpects[threadIndex][pi].UpdateParameter(aoi, modelDistribution[oi] * numTimesEventsSeen[ei]);
                        }
                    }
                }

                threadLoglikelihood[threadIndex] += Math.Log(modelDistribution[outcomeList[ei]]) * numTimesEventsSeen[ei];
                threadNumEvents[threadIndex]     += numTimesEventsSeen[ei];

#if !DEBUG
                // only when we have a monitor to report...
                if (monitor != null)
                {
#endif
                var max = 0;
                for (var oi = 0; oi < numOutcomes; oi++)
                {
                    if (modelDistribution[oi] > modelDistribution[max])
                    {
                        max = oi;
                    }
                }
                if (max == outcomeList[ei])
                {
                    threadNumCorrect[threadIndex] += numTimesEventsSeen[ei];
                }
#if !DEBUG
            }
#endif
            }
        }