Example #1
0
        private static (double[], double[], double[]) AdaptiveParzenNormal(double[] mus, double priorMu, double priorSigma)
        {
            int[]    order     = ArrayMath.ArgSort(mus);
            double[] sortedMus = ArrayMath.Index(mus, order);
            int      priorPos  = ArrayMath.SearchSorted(sortedMus, priorMu);

            sortedMus = ArrayMath.Insert(sortedMus, priorPos, priorMu);

            int length = mus.Length;

            double[] sigma = new double[length + 1];

            if (length == 0)
            {
                sigma[0] = priorSigma;
            }
            else if (length == 1)
            {
                sigma[priorPos]     = priorSigma;
                sigma[1 - priorPos] = priorSigma * 0.5;
            }
            else
            {
                sigma[0]      = sortedMus[1] - sortedMus[0];
                sigma[length] = sortedMus[length] - sortedMus[length - 1];
                for (int i = 1; i < length; i++)
                {
                    sigma[i] = Math.Max(sortedMus[i] - sortedMus[i - 1], sortedMus[i + 1] - sortedMus[i]);
                }
            }

            double maxSigma = priorSigma / 1.0;
            double minSigma = priorSigma / Math.Min(100.0, length + 2);

            sigma           = ArrayMath.Clip(sigma, minSigma, maxSigma);
            sigma[priorPos] = priorSigma;

            double[] sortedWeights;
            if (lf < length)
            {
                sortedWeights = ArrayMath.Index(LinearForgettingWeights(length), order);
            }
            else
            {
                sortedWeights = Enumerable.Repeat(1.0, length).ToArray();
            }
            sortedWeights = ArrayMath.Insert(sortedWeights, priorPos, priorWeight);
            sortedWeights = ArrayMath.DivSum(sortedWeights);

            return(sortedWeights, sortedMus, sigma);
        }
Example #2
0
        private static int SuggestCategorical(List <Result> history, string tag, int size)
        {
            if (history.Count < nStartupJobs)
            {
                return(rng.Integer(size));
            }

            var(obsBelow, obsAbove) = ApSplitTrials(history, tag);

            double[] weights   = LinearForgettingWeights(obsBelow.Length);
            double[] counts    = Bincount(obsBelow, weights, size);
            double[] p         = ArrayMath.DivSum(ArrayMath.Add(counts, priorWeight));
            int[]    sample    = rng.Categorical(p, nEiCandidates);
            double[] belowLLik = ArrayMath.Log(ArrayMath.Index(p, sample));

            weights = LinearForgettingWeights(obsAbove.Length);
            counts  = Bincount(obsAbove, weights, size);
            p       = ArrayMath.DivSum(ArrayMath.Add(counts, priorWeight));
            double[] aboveLLik = ArrayMath.Log(ArrayMath.Index(p, sample));

            return(FindBest(sample, belowLLik, aboveLLik));
        }