Exemplo n.º 1
0
 /// <summary>
 /// Constructs a new instance of the <see cref="Exp"/> type.
 /// </summary>
 /// <param name="arg">The exponent of the function.</param>
 public Gp(Term[] args, GaussianProcess gp, int dc)
 {
     DivCount = dc;
     Args     = args;
     Gpr      = gp;
     //GeneralMatrix cur = new GeneralMatrix(0, args.Length);
 }
Exemplo n.º 2
0
        public void SetUp()
        {
            var kernel = new GaussianKernel(1, 1);

            _gp = new GaussianProcess(kernel);
            _gp.AddDataPoint(new DataPoint(1.02, 0.79));
            _gp.AddDataPoint(new DataPoint(1.99, 0.94));
            _gp.AddDataPoint(new DataPoint(4.04, 0.65));
        }
        public override void Run()
        {
            var prices = GetPrices("SPY").Select(t => t.Item2).ToArray();
            var inputs = prices.Take(prices.Length - 1).ToArray();
            var inputVectors = inputs.Select(i => Vector.FromArray(new[] { i })).ToArray();
            var outputs = prices.Skip(1).ToArray();

            // Set up the GP prior, which will be filled in later
            var prior = Variable.New<SparseGP>().Named("prior");

            // The sparse GP variable - a distribution over functions
            var f = Variable<IFunction>.Random(prior).Named("f");

            // The locations to evaluate the function
            var x = Variable.Observed(inputVectors).Named("x");
            var j = x.Range.Named("j");
            var y = Variable.Observed(outputs, j).Named("y");
            y[j] = Variable.GaussianFromMeanAndVariance(Variable.FunctionEvaluate(f, x[j]), 0.1);
            var kf = new SquaredExponential(-0.5);

            // The basis
            var rand = new Random(Environment.TickCount);
            var basis = Enumerable.Range(1, 10).Select(i => Vector.FromArray(new double[1] {i*10})).ToArray();
            //var basis = new Vector[] {
            //    Vector.FromArray(new double[1] {80}),
            //    Vector.FromArray(new double[1] {90}),
            //    Vector.FromArray(new double[1] {100})
            //};

            var gp = new GaussianProcess(new ConstantFunction(0), kf);

            prior.ObservedValue = new SparseGP(new SparseGPFixed(gp, basis));
            var engine = new InferenceEngine(new ExpectationPropagation());
            var sgp = engine.Infer<SparseGP>(f);

            var means = sgp.Mean(inputVectors).ToArray();
            var stdDevs = inputVectors.Select(iv => Math.Sqrt(sgp.Variance(iv))).ToArray();

            this.Series = new[]
            {
                new LabelledSeries<Tuple<double,double>>(
                    "input",
                    Enumerable.Range(0,inputs.Length)
                    .Select(i=> Tuple.Create((double)i, inputs[i]))),
                new LabelledSeries<Tuple<double,double>>(
                    "infered mean",
                    Enumerable.Range(0,inputs.Length)
                    .Select(i=> Tuple.Create((double)i, means[i]))),
                new LabelledSeries<Tuple<double,double>>(
                    "infered stddev",
                    Enumerable.Range(0,inputs.Length)
                    .Select(i=> Tuple.Create((double)i, stdDevs[i]))),
            };
        }
Exemplo n.º 4
0
        public static (Vector[] dataX, double[] dataY) GenerateRandomData(int numData, double proportionCorrupt)
        {
            int randomSeed = 9876;

            Random rng = new Random(randomSeed);

            Rand.Restart(randomSeed);

            InferenceEngine engine = Utilities.GetInferenceEngine();

            // The points to evaluate
            Vector[] randomInputs = Utilities.VectorRange(0, 1, numData, null);

            var gaussianProcessGenerator = new GaussianProcessRegressor(randomInputs);

            // The basis
            Vector[] basis = Utilities.VectorRange(0, 1, 6, rng);

            // The kernel
            var kf = new SummationKernel(new SquaredExponential(-1)) + new WhiteNoise();

            // Fill in the sparse GP prior
            GaussianProcess gp = new GaussianProcess(new ConstantFunction(0), kf);

            gaussianProcessGenerator.Prior.ObservedValue = new SparseGP(new SparseGPFixed(gp, basis));

            // Infer the posterior Sparse GP, and sample a random function from it
            SparseGP sgp        = engine.Infer <SparseGP>(gaussianProcessGenerator.F);
            var      randomFunc = sgp.Sample();

            double[] randomOutputs = new double[randomInputs.Length];
            int      numCorrupted  = (int)Math.Ceiling(numData * proportionCorrupt);
            var      subset        = Enumerable.Range(0, randomInputs.Length + 1).OrderBy(x => rng.Next()).Take(numCorrupted);

            // get random data
            for (int i = 0; i < randomInputs.Length; i++)
            {
                double post = randomFunc.Evaluate(randomInputs[i]);
                // corrupt data point if it we haven't exceed the proportion we wish to corrupt
                if (subset.Contains(i))
                {
                    double sign     = rng.NextDouble() > 0.5 ? 1 : -1;
                    double distance = rng.NextDouble() * 1;
                    post = (sign * distance) + post;
                }

                randomOutputs[i] = post;
            }

            Console.WriteLine("Model complete: Generated {0} points with {1} corrupted", numData, numCorrupted);

            return(randomInputs, randomOutputs);
        }
Exemplo n.º 5
0
        static void FitDataset(bool useSynthetic)
        {
            Vector[] trainingInputs;
            double[] trainingOutputs;

            if (!useSynthetic)
            {
                var trainingData = Utilities.LoadAISDataset();
                trainingInputs = trainingData.Select(tup => Vector.FromArray(new double[1] {
                    tup.x
                })).ToArray();
                trainingOutputs = trainingData.Select(tup => tup.y).ToArray();
            }
            else
            {
                (trainingInputs, trainingOutputs) = GaussianProcessDataGenerator.GenerateRandomData(30, 0.3);
            }

            InferenceEngine engine = Utilities.GetInferenceEngine();

            // First fit standard GP, then fit Student-T GP
            foreach (var useStudentTLikelihood in new[] { false, true })
            {
                var gaussianProcessRegressor = new GaussianProcessRegressor(trainingInputs, useStudentTLikelihood, trainingOutputs);

                // Log length scale estimated as -1
                var             noiseVariance = 0.8;
                var             kf            = new SummationKernel(new SquaredExponential(-1)) + new WhiteNoise(Math.Log(noiseVariance) / 2);
                GaussianProcess gp            = new GaussianProcess(new ConstantFunction(0), kf);

                // Convert SparseGP to full Gaussian Process by evaluating at all the training points
                gaussianProcessRegressor.Prior.ObservedValue = new SparseGP(new SparseGPFixed(gp, trainingInputs.ToArray()));
                double logOdds = engine.Infer <Bernoulli>(gaussianProcessRegressor.Evidence).LogOdds;
                Console.WriteLine("{0} evidence = {1}", kf, logOdds.ToString("g4"));

                // Infer the posterior Sparse GP
                SparseGP sgp = engine.Infer <SparseGP>(gaussianProcessRegressor.F);

#if NETFULL
                string datasetName = useSynthetic ? "Synthetic" : "AIS";
                Utilities.PlotPredictions(sgp, trainingInputs, trainingOutputs, useStudentTLikelihood, datasetName);
#endif
            }
        }
Exemplo n.º 6
0
        internal void HeteroscedasticGPR()
        {
            // This model is based on the paper "Most Likely Heteroscedastic Gaussian Process Regression" by Kersting et al, ICML 2007
            // Silverman's motorcycle benchmark dataset
            double[] inputs = new double[]
            {
                2.4, 2.6, 3.2, 3.6, 4, 6.2, 6.6, 6.8, 7.8, 8.199999999999999, 8.800000000000001, 8.800000000000001,
                9.6, 10, 10.2, 10.6, 11, 11.4, 13.2, 13.6, 13.8, 14.6, 14.6, 14.6, 14.6, 14.6, 14.6, 14.8, 15.4, 15.4,
                15.4, 15.4, 15.6, 15.6, 15.8, 15.8, 16, 16, 16.2, 16.2, 16.2, 16.4, 16.4, 16.6, 16.8, 16.8, 16.8, 17.6,
                17.6, 17.6, 17.6, 17.8, 17.8, 18.6, 18.6, 19.2, 19.4, 19.4, 19.6, 20.2, 20.4, 21.2, 21.4, 21.8, 22, 23.2,
                23.4, 24, 24.2, 24.2, 24.6, 25, 25, 25.4, 25.4, 25.6, 26, 26.2, 26.2, 26.4, 27, 27.2, 27.2, 27.2, 27.6,
                28.2, 28.4, 28.4, 28.6, 29.4, 30.2, 31, 31.2, 32, 32, 32.8, 33.4, 33.8, 34.4, 34.8, 35.2, 35.2, 35.4, 35.6,
                35.6, 36.2, 36.2, 38, 38, 39.2, 39.4, 40, 40.4, 41.6, 41.6, 42.4, 42.8, 42.8, 43, 44, 44.4, 45, 46.6, 47.8,
                47.8, 48.8, 50.6, 52, 53.2, 55, 55, 55.4, 57.6
            };
            double[] outputs = new double[]
            {
                0, -1.3, -2.7, 0, -2.7, -2.7, -2.7, -1.3, -2.7, -2.7, -1.3, -2.7, -2.7, -2.7, -5.4,
                -2.7, -5.4, 0, -2.7, -2.7, 0, -13.3, -5.4, -5.4, -9.300000000000001, -16, -22.8, -2.7, -22.8, -32.1, -53.5,
                -54.9, -40.2, -21.5, -21.5, -50.8, -42.9, -26.8, -21.5, -50.8, -61.7, -5.4, -80.40000000000001, -59, -71,
                -91.09999999999999, -77.7, -37.5, -85.59999999999999, -123.1, -101.9, -99.09999999999999, -104.4, -112.5,
                -50.8, -123.1, -85.59999999999999, -72.3, -127.2, -123.1, -117.9, -134, -101.9, -108.4, -123.1, -123.1, -128.5,
                -112.5, -95.09999999999999, -81.8, -53.5, -64.40000000000001, -57.6, -72.3, -44.3, -26.8, -5.4, -107.1, -21.5,
                -65.59999999999999, -16, -45.6, -24.2, 9.5, 4, 12, -21.5, 37.5, 46.9, -17.4, 36.2, 75, 8.1, 54.9, 48.2, 46.9,
                16, 45.6, 1.3, 75, -16, -54.9, 69.59999999999999, 34.8, 32.1, -37.5, 22.8, 46.9, 10.7, 5.4, -1.3, -21.5, -13.3,
                30.8, -10.7, 29.4, 0, -10.7, 14.7, -1.3, 0, 10.7, 10.7, -26.8, -14.7, -13.3, 0, 10.7, -14.7, -2.7, 10.7, -2.7, 10.7
            };
            Range j = new Range(inputs.Length);

            Vector[] inputsVec       = Util.ArrayInit(inputs.Length, i => Vector.FromArray(inputs[i]));
            VariableArray <Vector> x = Variable.Observed(inputsVec, j).Named("x");
            VariableArray <double> y = Variable.Observed(outputs, j).Named("y");
            // Set up the GP prior, which will be filled in later
            Variable <SparseGP> prior  = Variable.New <SparseGP>().Named("prior");
            Variable <SparseGP> prior2 = Variable.New <SparseGP>().Named("prior2");

            // The sparse GP variable - a distribution over functions
            Variable <IFunction> f = Variable <IFunction> .Random(prior).Named("f");

            Variable <IFunction> r = Variable <IFunction> .Random(prior2).Named("r");

            Variable <double> mean        = Variable.FunctionEvaluate(f, x[j]).Named("mean");
            Variable <double> logVariance = Variable.FunctionEvaluate(r, x[j]).Named("logVariance");
            Variable <double> variance    = Variable.Exp(logVariance);

            y[j] = Variable.GaussianFromMeanAndVariance(mean, variance);


            InferenceEngine engine = new InferenceEngine();
            GaussianProcess gp     = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
            GaussianProcess gp2    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));

            // Fill in the sparse GP prior
            //Vector[] basis = Util.ArrayInit(120, i => Vector.FromArray(0.5*i));
            Vector[] basis = Util.ArrayInit(60, i => Vector.FromArray(1.0 * i));
            prior.ObservedValue  = new SparseGP(new SparseGPFixed(gp, basis));
            prior2.ObservedValue = new SparseGP(new SparseGPFixed(gp2, basis));
            // Infer the posterior Sparse GP
            SparseGP sgp = engine.Infer <SparseGP>(f);

            // Check that training set is classified correctly
            Console.WriteLine();
            Console.WriteLine("Predictions on training set:");
            for (int i = 0; i < outputs.Length; i++)
            {
                Gaussian post = sgp.Marginal(inputsVec[i]);
                //double postMean = post.GetMean();
                Console.WriteLine("f({0}) = {1}", inputs[i], post);
            }
            //TODO: change path for cross platform using
            using (MatlabWriter writer = new MatlabWriter(@"..\..\HGPR.mat"))
            {
                int      n = outputs.Length;
                double[] m = new double[n];
                double[] s = new double[n];
                for (int i = 0; i < n; i++)
                {
                    Gaussian post = sgp.Marginal(inputsVec[i]);
                    double   mi, vi;
                    post.GetMeanAndVariance(out mi, out vi);
                    m[i] = mi;
                    s[i] = System.Math.Sqrt(vi);
                }
                writer.Write("mean", m);
                writer.Write("std", s);
            }
        }
Exemplo n.º 7
0
            public void Initialize(bool skipStringDistributions = false)
            {
                // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing
                // you are trying to deserialize.
                this.pareto  = new Pareto(1.2, 3.5);
                this.poisson = new Poisson(2.3);
                this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] {
                    { 22, 21 }, { 21, 23 }
                }));
                this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] {
                    { 16, 15 }, { 15, 17 }
                }));
                this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3));
                this.pointMass            = PointMass <double> .Create(1.1);

                this.gaussian             = new Gaussian(11.0, 12.0);
                this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5);
                this.gamma              = new Gamma(9.0, 10.0);
                this.gammaPower         = new GammaPower(5.6, 2.8, 3.4);
                this.discrete           = new Discrete(6.0, 7.0, 8.0);
                this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5);
                this.dirichlet          = new Dirichlet(3.0, 4.0, 5.0);
                this.beta      = new Beta(2.0, 1.0);
                this.binomial  = new Binomial(5, 0.8);
                this.bernoulli = new Bernoulli(0.6);

                this.sparseBernoulliList    = SparseBernoulliList.Constant(4, new Bernoulli(0.1));
                this.sparseBernoulliList[1] = new Bernoulli(0.9);
                this.sparseBernoulliList[3] = new Bernoulli(0.7);

                this.sparseBetaList    = SparseBetaList.Constant(5, new Beta(2.0, 2.0));
                this.sparseBetaList[0] = new Beta(3.0, 4.0);
                this.sparseBetaList[1] = new Beta(5.0, 6.0);

                this.sparseGaussianList    = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2));
                this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4);
                this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6);

                this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0));

                this.truncatedGamma    = new TruncatedGamma(1.2, 2.3, 3.4, 4.5);
                this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8);
                this.wrappedGaussian   = new WrappedGaussian(1.2, 2.3, 3.4);

                ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian });

                vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian });

                ga2D = Distribution <double> .Array(new[, ] {
                    { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian }
                });

                vga2D = Distribution <Vector> .Array(new[, ] {
                    { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian }
                });

                gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } });

                vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } });

                var gp    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
                var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i));

                this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis));

                this.quantileEstimator = new QuantileEstimator(0.01);
                this.quantileEstimator.Add(5);
                this.outerQuantiles = OuterQuantiles.FromDistribution(3, this.quantileEstimator);
                this.innerQuantiles = InnerQuantiles.FromDistribution(3, this.outerQuantiles);

                if (!skipStringDistributions)
                {
                    // String distributions can not be serialized by some formatters (namely BinaryFormatter)
                    // That is fine because this combination is never used in practice
                    this.stringDistribution1 = StringDistribution.String("aa")
                                               .Append(StringDistribution.OneOf("b", "ccc")).Append("dddd");
                    this.stringDistribution2 = new StringDistribution();
                    this.stringDistribution2.SetToProduct(StringDistribution.OneOf("a", "b"),
                                                          StringDistribution.OneOf("b", "c"));
                }
            }
Exemplo n.º 8
0
 public ProbabilityOfImprovement(Goal goal, GaussianProcess gp, double[] xs) : base(gp, xs)
 {
     Goal = goal;
 }
        public void Run()
        {
            InferenceEngine engine = new InferenceEngine();

            if (!(engine.Algorithm is Algorithms.ExpectationPropagation))
            {
                Console.WriteLine("This example only runs with Expectation Propagation");
                return;
            }

            // The data
            Vector[] inputs = new Vector[]
            {
                Vector.FromArray(new double[2] {
                    0, 0
                }),
                Vector.FromArray(new double[2] {
                    0, 1
                }),
                Vector.FromArray(new double[2] {
                    1, 0
                }),
                Vector.FromArray(new double[2] {
                    0, 0.5
                }),
                Vector.FromArray(new double[2] {
                    1.5, 0
                }),
                Vector.FromArray(new double[2] {
                    0.5, 1.0
                })
            };

            bool[] outputs = { true, true, false, true, false, false };

            // Open an evidence block to allow model scoring
            Variable <bool> evidence = Variable.Bernoulli(0.5).Named("evidence");
            IfBlock         block    = Variable.If(evidence);

            // Set up the GP prior, a distribution over functions, which will be filled in later
            Variable <SparseGP> prior = Variable.New <SparseGP>().Named("prior");

            // The sparse GP variable - a random function
            Variable <IFunction> f = Variable <IFunction> .Random(prior).Named("f");

            // The locations to evaluate the function
            VariableArray <Vector> x = Variable.Observed(inputs).Named("x");
            Range j = x.Range.Named("j");

            // The observation model
            VariableArray <bool> y     = Variable.Observed(outputs, j).Named("y");
            Variable <double>    score = Variable.FunctionEvaluate(f, x[j]).Named("score");

            y[j] = (Variable.GaussianFromMeanAndVariance(score, 0.1) > 0);

            // Close the evidence block
            block.CloseBlock();

            // The basis
            Vector[] basis = new Vector[]
            {
                Vector.FromArray(new double[2] {
                    0.2, 0.2
                }),
                Vector.FromArray(new double[2] {
                    0.2, 0.8
                }),
                Vector.FromArray(new double[2] {
                    0.8, 0.2
                }),
                Vector.FromArray(new double[2] {
                    0.8, 0.8
                })
            };

            for (int trial = 0; trial < 3; trial++)
            {
                // The kernel
                IKernelFunction kf;
                if (trial == 0)
                {
                    kf = new SquaredExponential(-0.0);
                }
                else if (trial == 1)
                {
                    kf = new SquaredExponential(-0.5);
                }
                else
                {
                    kf = new NNKernel(new double[] { 0.0, 0.0 }, -1.0);
                }

                // Fill in the sparse GP prior
                GaussianProcess gp = new GaussianProcess(new ConstantFunction(0), kf);
                prior.ObservedValue = new SparseGP(new SparseGPFixed(gp, basis));

                // Model score
                double NNscore = engine.Infer <Bernoulli>(evidence).LogOdds;
                Console.WriteLine("{0} evidence = {1}", kf, NNscore.ToString("g4"));
            }

            // Infer the posterior Sparse GP
            SparseGP sgp = engine.Infer <SparseGP>(f);

            // Check that training set is classified correctly
            Console.WriteLine("");
            Console.WriteLine("Predictions on training set:");
            for (int i = 0; i < outputs.Length; i++)
            {
                Gaussian post     = sgp.Marginal(inputs[i]);
                double   postMean = post.GetMean();
                string   comment  = (outputs[i] == (postMean > 0.0)) ? "correct" : "incorrect";
                Console.WriteLine("f({0}) = {1} ({2})", inputs[i], post, comment);
            }
        }
Exemplo n.º 10
0
            public void Initialize()
            {
                // DO NOT make this a constructor, because it makes the test not notice complete lack of serialization as an empty object is set up exactly as the thing
                // you are trying to deserialize.
                this.pareto  = new Pareto(1.2, 3.5);
                this.poisson = new Poisson(2.3);
                this.wishart = new Wishart(20, new PositiveDefiniteMatrix(new double[, ] {
                    { 22, 21 }, { 21, 23 }
                }));
                this.vectorGaussian = new VectorGaussian(Vector.FromArray(13, 14), new PositiveDefiniteMatrix(new double[, ] {
                    { 16, 15 }, { 15, 17 }
                }));
                this.unnormalizedDiscrete = UnnormalizedDiscrete.FromLogProbs(DenseVector.FromArray(5.1, 5.2, 5.3));
                this.pointMass            = PointMass <double> .Create(1.1);

                this.gaussian             = new Gaussian(11.0, 12.0);
                this.nonconjugateGaussian = new NonconjugateGaussian(1.2, 2.3, 3.4, 4.5);
                this.gamma              = new Gamma(9.0, 10.0);
                this.gammaPower         = new GammaPower(5.6, 2.8, 3.4);
                this.discrete           = new Discrete(6.0, 7.0, 8.0);
                this.conjugateDirichlet = new ConjugateDirichlet(1.2, 2.3, 3.4, 4.5);
                this.dirichlet          = new Dirichlet(3.0, 4.0, 5.0);
                this.beta      = new Beta(2.0, 1.0);
                this.binomial  = new Binomial(5, 0.8);
                this.bernoulli = new Bernoulli(0.6);

                this.sparseBernoulliList    = SparseBernoulliList.Constant(4, new Bernoulli(0.1));
                this.sparseBernoulliList[1] = new Bernoulli(0.9);
                this.sparseBernoulliList[3] = new Bernoulli(0.7);

                this.sparseBetaList    = SparseBetaList.Constant(5, new Beta(2.0, 2.0));
                this.sparseBetaList[0] = new Beta(3.0, 4.0);
                this.sparseBetaList[1] = new Beta(5.0, 6.0);

                this.sparseGaussianList    = SparseGaussianList.Constant(6, Gaussian.FromMeanAndPrecision(0.1, 0.2));
                this.sparseGaussianList[4] = Gaussian.FromMeanAndPrecision(0.3, 0.4);
                this.sparseGaussianList[5] = Gaussian.FromMeanAndPrecision(0.5, 0.6);

                this.sparseGammaList = SparseGammaList.Constant(1, Gamma.FromShapeAndRate(1.0, 2.0));

                this.truncatedGamma    = new TruncatedGamma(1.2, 2.3, 3.4, 4.5);
                this.truncatedGaussian = new TruncatedGaussian(1.2, 3.4, 5.6, 7.8);
                this.wrappedGaussian   = new WrappedGaussian(1.2, 2.3, 3.4);

                ga = Distribution <double> .Array(new[] { this.gaussian, this.gaussian });

                vga = Distribution <Vector> .Array(new[] { this.vectorGaussian, this.vectorGaussian });

                ga2D = Distribution <double> .Array(new[, ] {
                    { this.gaussian, this.gaussian }, { this.gaussian, this.gaussian }
                });

                vga2D = Distribution <Vector> .Array(new[, ] {
                    { this.vectorGaussian, this.vectorGaussian }, { this.vectorGaussian, this.vectorGaussian }
                });

                gaJ = Distribution <double> .Array(new[] { new[] { this.gaussian, this.gaussian }, new[] { this.gaussian, this.gaussian } });

                vgaJ = Distribution <Vector> .Array(new[] { new[] { this.vectorGaussian, this.vectorGaussian }, new[] { this.vectorGaussian, this.vectorGaussian } });

                var gp    = new GaussianProcess(new ConstantFunction(0), new SquaredExponential(0));
                var basis = Util.ArrayInit(2, i => Vector.FromArray(1.0 * i));

                this.sparseGp = new SparseGP(new SparseGPFixed(gp, basis));

                this.quantileEstimator = new QuantileEstimator(0.01);
                this.quantileEstimator.Add(5);
            }
Exemplo n.º 11
0
 public Explorer(GaussianProcess gp, double[] Xs) : base(gp, Xs)
 {
 }
Exemplo n.º 12
0
		public void Run()
		{
			InferenceEngine engine = new InferenceEngine();
			if (!(engine.Algorithm is ExpectationPropagation))
			{
				Console.WriteLine("This example only runs with Expectation Propagation");
				return;
			}
	
			// The data
			Vector[] inputs = new Vector[] {
				Vector.FromArray(new double[2] {0, 0}),
				Vector.FromArray(new double[2] {0, 1}),
				Vector.FromArray(new double[2] {1, 0}),
				Vector.FromArray(new double[2] {0, 0.5}),
				Vector.FromArray(new double[2] {1.5, 0}),
				Vector.FromArray(new double[2] {0.5, 1.0})
			};
			
			bool[] outputs = { true, true, false, true, false, false };

			// Open an evidence block to allow model scoring
			Variable<bool> evidence = Variable.Bernoulli(0.5).Named("evidence");
			IfBlock block = Variable.If(evidence);

			// Set up the GP prior, which will be filled in later
			Variable<SparseGP> prior = Variable.New<SparseGP>().Named("prior");

			// The sparse GP variable - a distribution over functions
			Variable<IFunction> f = Variable<IFunction>.Random(prior).Named("f");

			// The locations to evaluate the function
			VariableArray<Vector> x = Variable.Observed(inputs).Named("x");
			Range j = x.Range.Named("j");

			// The observation model
			VariableArray<bool> y = Variable.Observed(outputs, j).Named("y");
			Variable<double> score = Variable.FunctionEvaluate(f, x[j]);
			y[j] = (Variable.GaussianFromMeanAndVariance(score, 0.1) > 0);

			// Close the evidence block
			block.CloseBlock();

			// The basis
			Vector[] basis = new Vector[] {
				Vector.FromArray(new double[2] {0.2, 0.2}),
				Vector.FromArray(new double[2] {0.2, 0.8}),
				Vector.FromArray(new double[2] {0.8, 0.2}),
				Vector.FromArray(new double[2] {0.8, 0.8})
			};

			for (int trial = 0; trial < 3; trial++)
			{
				// The kernel
				IKernelFunction kf;
				if (trial == 0)
				{
					kf = new SquaredExponential(-0.0);
				}
				else if (trial == 1)
				{
					kf = new SquaredExponential(-0.5);
				}
				else
				{
					kf = new NNKernel(new double[] { 0.0, 0.0 }, -1.0);
				}

				// Fill in the sparse GP prior
				GaussianProcess gp = new GaussianProcess(new ConstantFunction(0), kf);
				prior.ObservedValue = new SparseGP(new SparseGPFixed(gp, basis));

				// Model score
				double NNscore = engine.Infer<Bernoulli>(evidence).LogOdds;
				Console.WriteLine("{0} evidence = {1}", kf, NNscore.ToString("g4"));
			}

			// Infer the posterior Sparse GP
			SparseGP sgp = engine.Infer<SparseGP>(f);

			// Check that training set is classified correctly
			Console.WriteLine("");
			Console.WriteLine("Predictions on training set:");
			for (int i = 0; i < outputs.Length; i++)
			{
				Gaussian post = sgp.Marginal(inputs[i]);
				double postMean = post.GetMean();
				string comment = (outputs[i] == (postMean > 0.0)) ? "correct" : "incorrect";
				Console.WriteLine("f({0}) = {1} ({2})", inputs[i], post, comment);
			}
		}
Exemplo n.º 13
0
 public AquisitionFunction(GaussianProcess gp, double[] xs)
 {
     Gp = gp;
     Xs = xs;
 }
Exemplo n.º 14
0
 public ExpectedfImprovement(Goal goal, GaussianProcess gp, double[] xs) : base(gp, xs)
 {
     Goal = goal;
 }