Beispiel #1
0
        public static double mlfHelperNode(PositiveDefiniteMatrix prec, PositiveDefiniteMatrix[] gradK, Vector gradientVector, Vector[] functions_SampleMean,
                                           PositiveDefiniteMatrix[] functions_SampleVariance,
                                           DistributionStructArray <Gamma, double> nodeSignalPrecisions)
        {
            var    logDetPrec = VectorGaussianScaledPrecisionOp.PrecisionMeanLogDet(prec);
            double res        = 0;

            if (gradientVector != null)
            {
                for (int i = 0; i < gradientVector.Count; i++)
                {
                    gradientVector[i] = 0;
                }
            }

            for (int q = 0; q < functions_SampleMean.Length; q++)
            {
                res += VectorGaussianScaledPrecisionOp.AverageLogFactor(functions_SampleMean[q], nodeSignalPrecisions[q],
                                                                        VectorGaussianScaledPrecisionOp.ESamplePrecisionSample(functions_SampleMean[q], functions_SampleVariance[q], prec),
                                                                        logDetPrec);
                if (gradientVector != null)
                {
                    for (int i = 0; i < gradientVector.Count; i++)
                    {
                        gradientVector[i] += VectorGaussianScaledPrecisionOp.GradAverageLogFactor(
                            functions_SampleMean[q],
                            functions_SampleVariance[q],
                            nodeSignalPrecisions[q],
                            gradK[i],
                            prec);
                    }
                }
            }
            return(res);
        }
Beispiel #2
0
 public static double AverageLogFactor(
     Vector SampleMean,
     double scaling,
     GPBuffer Buffer)
 {
     return(VectorGaussianScaledPrecisionOp.AverageLogFactor(SampleMean, scaling, Buffer.ESamplePrecisionSample, Buffer.PrecisionMeanLogDet));
 }
Beispiel #3
0
        public static double mlfHelperWeight(PositiveDefiniteMatrix prec, PositiveDefiniteMatrix[] gradK, Vector gradientVector, Vector[][] functions_SampleMean,
                                             PositiveDefiniteMatrix[][] functions_SampleVariance)
        {
            double res        = 0;
            var    logDetPrec = VectorGaussianScaledPrecisionOp.PrecisionMeanLogDet(prec);

            if (gradientVector != null)
            {
                for (int i = 0; i < gradientVector.Count; i++)
                {
                    gradientVector[i] = 0;
                }
            }
            int D        = functions_SampleMean[0].Length;
            var resArray = new double[D];

            Vector[] gradVArray = gradientVector != null?System.Linq.Enumerable.Range(0, D).Select(_ => DenseVector.Zero(gradientVector.Count)).ToArray() : null;

            for (int q = 0; q < functions_SampleMean.Length; q++)
            {    //for (int d = 0; d < functions_SampleMean[0].Length; d++)
                Parallel.For(0, D, d =>
                {
                    resArray[d] = VectorGaussianScaledPrecisionOp.AverageLogFactor(functions_SampleMean[q][d], 1,
                                                                                   VectorGaussianScaledPrecisionOp.ESamplePrecisionSample(functions_SampleMean[q][d], functions_SampleVariance[q][d], prec),
                                                                                   logDetPrec);
                    if (gradientVector != null)
                    {
                        for (int i = 0; i < gradientVector.Count; i++)
                        {
                            gradVArray[d][i] = VectorGaussianScaledPrecisionOp.GradAverageLogFactor(
                                functions_SampleMean[q][d],
                                functions_SampleVariance[q][d],
                                Gamma.PointMass(1),
                                gradK[i],
                                prec);
                        }
                    }
                });
            }
            res = resArray.Sum();
            if (gradientVector != null)
            {
                gradientVector.SetTo(gradVArray.Aggregate((p, q) => p + q));
            }
            return(res);
        }
Beispiel #4
0
 /// <summary>
 /// Uses the KernelOptimiser class to optimise the hypers given the current variational posterior
 /// on the function values (which has mean SampleMean and covariance SampleCovariance)
 /// </summary>
 public static GPBuffer BufferHelper(int[] hypersToOptimise, GPBuffer Buffer, Vector[] x, Vector SampleMean, PositiveDefiniteMatrix SampleVariance, Gamma scaling)
 {
     if (SampleMean.All(o => o == 0.0))
     {
         Buffer.Precision = Utils.GramMatrix(Buffer.kernel, x).Inverse();
     }
     else
     {
         //Console.WriteLine(Utils.KernelToArray(Buffer.kernel).Select(o => o.ToString()).Aggregate((p, q) => p + " " + q));
         var helper = new KernelOptimiser(settings);
         helper.kernel           = Buffer.kernel;
         helper.xData            = x;
         helper.hypersToOptimise = hypersToOptimise;
         helper.Optimise((prec, gradK, gradientVector) =>
                         helperFunction(prec, gradK, gradientVector, scaling, SampleMean,
                                        SampleVariance), ref Buffer.Precision);
         Buffer.ESamplePrecisionSample = VectorGaussianScaledPrecisionOp.ESamplePrecisionSample(SampleMean, SampleVariance, Buffer.Precision);
         Buffer.PrecisionMeanLogDet    = VectorGaussianScaledPrecisionOp.PrecisionMeanLogDet(Buffer.Precision);
         //Console.WriteLine(Utils.KernelToArray(Buffer.kernel).Select(o => o.ToString()).Aggregate((p, q) => p + " " + q));
         rememberKernel = Buffer.kernel;
     }
     return(Buffer);
 }
Beispiel #5
0
        /// <summary>
        /// Calculates the variational lower bound and its derivatives wrt to the hypers. gradK is the gradient of the kernel matrix
        /// wrt to the hypers.
        /// </summary>
        public static double helperFunction(PositiveDefiniteMatrix prec, PositiveDefiniteMatrix[] gradK, Vector gradientVector, Gamma scaling,
                                            Vector functions_SampleMean,
                                            PositiveDefiniteMatrix functions_SampleVariance)
        {
            double res        = 0;
            var    logDetPrec = VectorGaussianScaledPrecisionOp.PrecisionMeanLogDet(prec);

            res = VectorGaussianScaledPrecisionOp.AverageLogFactor(functions_SampleMean, scaling,
                                                                   VectorGaussianScaledPrecisionOp.ESamplePrecisionSample(functions_SampleMean, functions_SampleVariance, prec),
                                                                   logDetPrec);
            if (gradientVector != null)
            {
                for (int i = 0; i < gradientVector.Count; i++)
                {
                    gradientVector[i] = VectorGaussianScaledPrecisionOp.GradAverageLogFactor(
                        functions_SampleMean,
                        functions_SampleVariance,
                        Gamma.PointMass(1),
                        gradK[i],
                        prec);
                }
            }
            return(res);
        }