Exemple #1
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            if (terms.Count == 0)
            {
                throw new ArgumentException("at least one term is necessary for the product covariance function.");
            }
            var functions = new List <ParameterizedCovarianceFunction>();

            foreach (var t in terms)
            {
                var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
                functions.Add(t.GetParameterizedCovarianceFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
                p = p.Skip(numberOfParameters).ToArray();
            }

            var sum = new ParameterizedCovarianceFunction();

            sum.Covariance         = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Sum();
            sum.CrossCovariance    = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Sum();
            sum.CovarianceGradient = (x, i, j) => {
                var g = new List <double>();
                foreach (var e in functions)
                {
                    g.AddRange(e.CovarianceGradient(x, i, j));
                }
                return(g);
            };
            return(sum);
        }
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double inverseLength, period, scale;

            GetParameterValues(p, out scale, out period, out inverseLength);
            var fixedInverseLength = HasFixedInverseLengthParameter;
            var fixedPeriod        = HasFixedPeriodParameter;
            var fixedScale         = HasFixedScaleParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => {
                double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
                k = Math.PI * k / period;
                k = Math.Sin(k) * inverseLength;
                k = k * k;

                return(scale * Math.Exp(-2.0 * k));
            };
            cov.CrossCovariance = (x, xt, i, j) => {
                double k = GetDistance(x, xt, i, j, columnIndices);
                k = Math.PI * k / period;
                k = Math.Sin(k) * inverseLength;
                k = k * k;

                return(scale * Math.Exp(-2.0 * k));
            };
            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength, fixedInverseLength, fixedPeriod, fixedScale);
            return(cov);
        }
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double scale, shape;

            double[] inverseLength;
            GetParameterValues(p, out scale, out shape, out inverseLength);
            var fixedInverseLength = HasFixedInverseLengthParameter;
            var fixedScale         = HasFixedScaleParameter;
            var fixedShape         = HasFixedShapeParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => {
                double d = i == j
                    ? 0.0
                    : Util.SqrDist(x, i, j, inverseLength, columnIndices);
                return(scale * Math.Pow(1 + 0.5 * d / shape, -shape));
            };
            cov.CrossCovariance = (x, xt, i, j) => {
                double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
                return(scale * Math.Pow(1 + 0.5 * d / shape, -shape));
            };
            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
            return(cov);
        }
        private static double[,] CalculateL(double[,] x, ParameterizedCovarianceFunction cov, double sqrSigmaNoise)
        {
            int n = x.GetLength(0);
            var l = new double[n, n];

            // calculate covariances
            for (int i = 0; i < n; i++)
            {
                for (int j = i; j < n; j++)
                {
                    l[j, i] = cov.Covariance(x, i, j) / sqrSigmaNoise;
                    if (j == i)
                    {
                        l[j, i] += 1.0;
                    }
                }
            }

            // cholesky decomposition
            var res = alglib.trfac.spdmatrixcholesky(ref l, n, false);

            if (!res)
            {
                throw new ArgumentException("Matrix is not positive semidefinite");
            }
            return(l);
        }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
   if (p.Length > 0) throw new ArgumentException("No parameters are allowed for the linear covariance function.");
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, 1, columnIndices);
   cov.CrossCovariance = (x, xt, i, j) =>  Util.ScalarProd(x, i, xt, j, 1.0 , columnIndices);
   cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();
   return cov;
 }
Exemple #6
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double length, scale;
            int    v = VParameter.Value.Value;

            GetParameterValues(p, out length, out scale);
            var fixedLength = HasFixedLengthParameter;
            var fixedScale  = HasFixedScaleParameter;
            int exp         = (int)Math.Floor(columnIndices.Count() / 2.0) + v + 1;

            Func <double, double> f;
            Func <double, double> df;

            switch (v)
            {
            case 0:
                f  = (r) => 1.0;
                df = (r) => 0.0;
                break;

            case 1:
                f  = (r) => 1 + (exp + 1) * r;
                df = (r) => exp + 1;
                break;

            case 2:
                f  = (r) => 1 + (exp + 2) * r + (exp * exp + 4.0 * exp + 3) / 3.0 * r * r;
                df = (r) => (exp + 2) + 2 * (exp * exp + 4.0 * exp + 3) / 3.0 * r;
                break;

            case 3:
                f = (r) => 1 + (exp + 3) * r + (6.0 * exp * exp + 36 * exp + 45) / 15.0 * r * r +
                    (exp * exp * exp + 9 * exp * exp + 23 * exp + 45) / 15.0 * r * r * r;
                df = (r) => (exp + 3) + 2 * (6.0 * exp * exp + 36 * exp + 45) / 15.0 * r +
                     (exp * exp * exp + 9 * exp * exp + 23 * exp + 45) / 5.0 * r * r;
                break;

            default: throw new ArgumentException();
            }

            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => {
                double k = Math.Sqrt(Util.SqrDist(x, i, x, j, columnIndices, 1.0 / length));
                return(scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k));
            };
            cov.CrossCovariance = (x, xt, i, j) => {
                double k = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, 1.0 / length));
                return(scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k));
            };
            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, v, exp, f, df, columnIndices, fixedLength, fixedScale);
            return(cov);
        }
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable <int> columnIndices)
        {
            if (p.Length > 0)
            {
                throw new ArgumentException("No parameters are allowed for the linear covariance function.");
            }
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance         = (x, i, j) => Util.ScalarProd(x, i, j, 1, columnIndices);
            cov.CrossCovariance    = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, 1.0, columnIndices);
            cov.CovarianceGradient = (x, i, j) => Enumerable.Empty <double>();
            return(cov);
        }
Exemple #8
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double scale;

            GetParameterValues(p, out scale);
            var fixedScale = HasFixedScaleParameter;
            var subCov     = CovarianceFunctionParameter.Value.GetParameterizedCovarianceFunction(p.Skip(1).ToArray(), columnIndices);
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => scale *subCov.Covariance(x, i, j);

            cov.CrossCovariance = (x, xt, i, j) => scale *subCov.CrossCovariance(x, xt, i, j);

            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov, fixedScale);
            return(cov);
        }
Exemple #9
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            if (p.Length != GetNumberOfParameters(columnIndices.Length))
            {
                throw new ArgumentException("Illegal parametrization");
            }
            var myClone = (KernelBase)Clone();

            myClone.SetParameter(p);
            var cov = new ParameterizedCovarianceFunction {
                Covariance         = (x, i, j) => myClone.Get(GetNorm(x, x, i, j, columnIndices)),
                CrossCovariance    = (x, xt, i, j) => myClone.Get(GetNorm(x, xt, i, j, columnIndices)),
                CovarianceGradient = (x, i, j) => new List <double> {
                    myClone.GetGradient(GetNorm(x, x, i, j, columnIndices))
                }
            };

            return(cov);
        }
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double scale;

            GetParameterValues(p, out scale);
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance      = (x, i, j) => scale;
            cov.CrossCovariance = (x, xt, i, j) => scale;
            if (HasFixedScaleParameter)
            {
                cov.CovarianceGradient = (x, i, j) => new double[0];
            }
            else
            {
                cov.CovarianceGradient = (x, i, j) => new[] { 2.0 * scale };
            }
            return(cov);
        }
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable <int> columnIndices)
        {
            double[] inverseLength;
            GetParameterValues(p, out inverseLength);
            var fixedInverseLength = HasFixedInverseLengthParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance      = (x, i, j) => Util.ScalarProd(x, i, j, inverseLength, columnIndices);
            cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices);
            if (fixedInverseLength)
            {
                cov.CovarianceGradient = (x, i, j) => Enumerable.Empty <double>();
            }
            else
            {
                cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices);
            }
            return(cov);
        }
Exemple #12
0
    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
      if (terms.Count == 0) throw new ArgumentException("at least one term is necessary for the product covariance function.");
      var functions = new List<ParameterizedCovarianceFunction>();
      foreach (var t in terms) {
        var numberOfParameters = t.GetNumberOfParameters(numberOfVariables);
        functions.Add(t.GetParameterizedCovarianceFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
        p = p.Skip(numberOfParameters).ToArray();
      }

      var sum = new ParameterizedCovarianceFunction();
      sum.Covariance = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Sum();
      sum.CrossCovariance = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Sum();
      sum.CovarianceGradient = (x, i, j) => {
        var g = new List<double>();
        foreach (var e in functions)
          g.AddRange(e.CovarianceGradient(x, i, j));
        return g;
      };
      return sum;
    }
Exemple #13
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable <int> columnIndices)
        {
            double scale;

            GetParameterValues(p, out scale);
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance      = (x, i, j) => scale;
            cov.CrossCovariance = (x, xt, i, j) => scale;
            if (HasFixedScaleParameter)
            {
                cov.CovarianceGradient = (x, i, j) => Enumerable.Empty <double>();
            }
            else
            {
                cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, columnIndices);
            }
            return(cov);
        }
Exemple #14
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double length, scale;

            GetParameterValues(p, out scale, out length);
            var fixedLength = HasFixedLengthParameter;
            var fixedScale  = HasFixedScaleParameter;

            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => {
                double sx = 1.0;
                double s1 = 1.0;
                double s2 = 1.0;
                for (int c = 0; c < columnIndices.Length; c++)
                {
                    var col = columnIndices[c];
                    sx += x[i, col] * x[j, col];
                    s1 += x[i, col] * x[i, col];
                    s2 += x[j, col] * x[j, col];
                }

                return(scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
            };
            cov.CrossCovariance = (x, xt, i, j) => {
                double sx = 1.0;
                double s1 = 1.0;
                double s2 = 1.0;
                for (int c = 0; c < columnIndices.Length; c++)
                {
                    var col = columnIndices[c];
                    sx += x[i, col] * xt[j, col];
                    s1 += x[i, col] * x[i, col];
                    s2 += xt[j, col] * xt[j, col];
                }

                return(scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
            };
            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, columnIndices, fixedLength, fixedScale);
            return(cov);
        }
Exemple #15
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable <int> columnIndices)
        {
            double scale;

            GetParameterValues(p, out scale);
            var fixedScale = HasFixedScaleParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance      = (x, i, j) => i == j ? scale : 0.0;
            cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, 1.0, columnIndices) < 1e-9 ? scale : 0.0;
            if (fixedScale)
            {
                cov.CovarianceGradient = (x, i, j) => Enumerable.Empty <double>();
            }
            else
            {
                cov.CovarianceGradient = (x, i, j) => Enumerable.Repeat(i == j ? 2.0 * scale : 0.0, 1);
            }
            return(cov);
        }
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double[] weight, frequency, lengthScale;
            GetParameterValues(p, out weight, out frequency, out lengthScale);
            var fixedWeight      = HasFixedWeightParameter;
            var fixedFrequency   = HasFixedFrequencyParameter;
            var fixedLengthScale = HasFixedLengthScaleParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => {
                return(GetCovariance(x, x, i, j, QParameter.Value.Value, weight, frequency,
                                     lengthScale, columnIndices));
            };
            cov.CrossCovariance = (x, xt, i, j) => {
                return(GetCovariance(x, xt, i, j, QParameter.Value.Value, weight, frequency,
                                     lengthScale, columnIndices));
            };
            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, QParameter.Value.Value, weight, frequency,
                                                              lengthScale, columnIndices, fixedWeight, fixedFrequency, fixedLengthScale);
            return(cov);
        }
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double @const, scale;
            int    degree = DegreeParameter.Value.Value;

            if (degree <= 0)
            {
                throw new ArgumentException("The degree parameter for CovariancePolynomial must be greater than zero.");
            }
            GetParameterValues(p, out @const, out scale);
            var fixedConst = HasFixedConstParameter;
            var fixedScale = HasFixedScaleParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => scale *Math.Pow(@const + Util.ScalarProd(x, i, j, columnIndices, 1.0), degree);

            cov.CrossCovariance = (x, xt, i, j) => scale *Math.Pow(@const + Util.ScalarProd(x, i, xt, j, columnIndices, 1.0), degree);

            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices, fixedConst, fixedScale);
            return(cov);
        }
Exemple #18
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            if (factors.Count == 0)
            {
                throw new ArgumentException("at least one factor is necessary for the product covariance function.");
            }
            var functions = new List <ParameterizedCovarianceFunction>();

            foreach (var f in factors)
            {
                int numberOfParameters = f.GetNumberOfParameters(numberOfVariables);
                functions.Add(f.GetParameterizedCovarianceFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
                p = p.Skip(numberOfParameters).ToArray();
            }


            var product = new ParameterizedCovarianceFunction();

            product.Covariance         = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Aggregate((a, b) => a * b);
            product.CrossCovariance    = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Aggregate((a, b) => a * b);
            product.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, functions);
            return(product);
        }
Exemple #19
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double scale;

            GetParameterValues(p, out scale);
            var fixedScale = HasFixedScaleParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance      = (x, i, j) => i == j ? scale : 0.0;
            cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, columnIndices, 1.0) < 1e-9 ? scale : 0.0;
            if (fixedScale)
            {
                cov.CovarianceGradient = (x, i, j) => new double[0];
            }
            else
            {
                cov.CovarianceGradient = (x, i, j) => new double[1] {
                    i == j ? 2.0 * scale : 0.0
                }
            };
            return(cov);
        }
    }
Exemple #20
0
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices)
        {
            double inverseLength, scale;
            int    d = DParameter.Value.Value;

            GetParameterValues(p, out scale, out inverseLength);
            var fixedInverseLength = HasFixedInverseLengthParameter;
            var fixedScale         = HasFixedScaleParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => {
                double dist = i == j
                       ? 0.0
                       : Math.Sqrt(Util.SqrDist(x, i, j, columnIndices, Math.Sqrt(d) * inverseLength));
                return(scale * m(d, dist));
            };
            cov.CrossCovariance = (x, xt, i, j) => {
                double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, Math.Sqrt(d) * inverseLength));
                return(scale * m(d, dist));
            };
            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, d, scale, inverseLength, columnIndices, fixedInverseLength, fixedScale);
            return(cov);
        }
        public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable <int> columnIndices)
        {
            double inverseLength, scale;

            GetParameterValues(p, out scale, out inverseLength);
            var fixedInverseLength = HasFixedInverseLengthParameter;
            var fixedScale         = HasFixedScaleParameter;
            // create functions
            var cov = new ParameterizedCovarianceFunction();

            cov.Covariance = (x, i, j) => {
                double d = i == j
                ? 0.0
                : Util.SqrDist(x, i, j, inverseLength, columnIndices);
                return(scale * Math.Exp(-d / 2.0));
            };
            cov.CrossCovariance = (x, xt, i, j) => {
                double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
                return(scale * Math.Exp(-d / 2.0));
            };
            cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices,
                                                              fixedInverseLength, fixedScale);
            return(cov);
        }
Exemple #22
0
 private static IList<double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, ParameterizedCovarianceFunction cov,
   bool fixedScale) {
   var gr = new List<double>((!fixedScale ? 1 : 0) + cov.CovarianceGradient(x, i, j).Count);
   if (!fixedScale) {
     gr.Add(2 * scale * cov.Covariance(x, i, j));
   }
   foreach (var g in cov.CovarianceGradient(x, i, j))
     gr.Add(scale * g);
   return gr;
 }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
   double[] weight, frequency, lengthScale;
   GetParameterValues(p, out weight, out frequency, out lengthScale);
   var fixedWeight = HasFixedWeightParameter;
   var fixedFrequency = HasFixedFrequencyParameter;
   var fixedLengthScale = HasFixedLengthScaleParameter;
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => {
     return GetCovariance(x, x, i, j, QParameter.Value.Value, weight, frequency,
                          lengthScale, columnIndices);
   };
   cov.CrossCovariance = (x, xt, i, j) => {
     return GetCovariance(x, xt, i, j, QParameter.Value.Value, weight, frequency,
                          lengthScale, columnIndices);
   };
   cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, QParameter.Value.Value, weight, frequency,
                          lengthScale, columnIndices, fixedWeight, fixedFrequency, fixedLengthScale);
   return cov;
 }
Exemple #24
0
 private static IEnumerable <double> GetGradient(double[,] x, int i, int j, IEnumerable <int> columnIndices, double scale, ParameterizedCovarianceFunction cov,
                                                 bool fixedScale)
 {
     if (!fixedScale)
     {
         yield return(2 * scale * cov.Covariance(x, i, j));
     }
     foreach (var g in cov.CovarianceGradient(x, i, j))
     {
         yield return(scale * g);
     }
 }
    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
      if (factors.Count == 0) throw new ArgumentException("at least one factor is necessary for the product covariance function.");
      var functions = new List<ParameterizedCovarianceFunction>();
      foreach (var f in factors) {
        int numberOfParameters = f.GetNumberOfParameters(numberOfVariables);
        functions.Add(f.GetParameterizedCovarianceFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
        p = p.Skip(numberOfParameters).ToArray();
      }


      var product = new ParameterizedCovarianceFunction();
      product.Covariance = (x, i, j) => functions.Select(e => e.Covariance(x, i, j)).Aggregate((a, b) => a * b);
      product.CrossCovariance = (x, xt, i, j) => functions.Select(e => e.CrossCovariance(x, xt, i, j)).Aggregate((a, b) => a * b);
      product.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, functions);
      return product;
    }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
   double scale;
   GetParameterValues(p, out scale);
   var fixedScale = HasFixedScaleParameter;
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => i == j ? scale : 0.0;
   cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, 1.0, columnIndices) < 1e-9 ? scale : 0.0;
   if (fixedScale)
     cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();
   else
     cov.CovarianceGradient = (x, i, j) => Enumerable.Repeat(i == j ? 2.0 * scale : 0.0, 1);
   return cov;
 }
    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
      double length, scale;
      int v = VParameter.Value.Value;
      GetParameterValues(p, out length, out scale);
      var fixedLength = HasFixedLengthParameter;
      var fixedScale = HasFixedScaleParameter;
      int exp = (int)Math.Floor(columnIndices.Count() / 2.0) + v + 1;

      Func<double, double> f;
      Func<double, double> df;
      switch (v) {
        case 0:
          f = (r) => 1.0;
          df = (r) => 0.0;
          break;
        case 1:
          f = (r) => 1 + (exp + 1) * r;
          df = (r) => exp + 1;
          break;
        case 2:
          f = (r) => 1 + (exp + 2) * r + (exp * exp + 4.0 * exp + 3) / 3.0 * r * r;
          df = (r) => (exp + 2) + 2 * (exp * exp + 4.0 * exp + 3) / 3.0 * r;
          break;
        case 3:
          f = (r) => 1 + (exp + 3) * r + (6.0 * exp * exp + 36 * exp + 45) / 15.0 * r * r +
                     (exp * exp * exp + 9 * exp * exp + 23 * exp + 45) / 15.0 * r * r * r;
          df = (r) => (exp + 3) + 2 * (6.0 * exp * exp + 36 * exp + 45) / 15.0 * r +
                      (exp * exp * exp + 9 * exp * exp + 23 * exp + 45) / 5.0 * r * r;
          break;
        default: throw new ArgumentException();
      }

      // create functions
      var cov = new ParameterizedCovarianceFunction();
      cov.Covariance = (x, i, j) => {
        double k = Math.Sqrt(Util.SqrDist(x, i, x, j, columnIndices, 1.0 / length));
        return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k);
      };
      cov.CrossCovariance = (x, xt, i, j) => {
        double k = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, 1.0 / length));
        return scale * Math.Pow(Math.Max(1 - k, 0), exp + v) * f(k);
      };
      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, v, exp, f, df, columnIndices, fixedLength, fixedScale);
      return cov;
    }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
   double inverseLength, scale;
   int d = DParameter.Value.Value;
   GetParameterValues(p, out scale, out inverseLength);
   var fixedInverseLength = HasFixedInverseLengthParameter;
   var fixedScale = HasFixedScaleParameter;
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => {
     double dist = i == j
                    ? 0.0
                    : Math.Sqrt(Util.SqrDist(x, i, j, columnIndices, Math.Sqrt(d) * inverseLength));
     return scale * m(d, dist);
   };
   cov.CrossCovariance = (x, xt, i, j) => {
     double dist = Math.Sqrt(Util.SqrDist(x, i, xt, j, columnIndices, Math.Sqrt(d) * inverseLength));
     return scale * m(d, dist);
   };
   cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, d, scale, inverseLength, columnIndices, fixedInverseLength, fixedScale);
   return cov;
 }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
   double scale, shape, inverseLength;
   GetParameterValues(p, out scale, out shape, out inverseLength);
   var fixedInverseLength = HasFixedInverseLengthParameter;
   var fixedScale = HasFixedScaleParameter;
   var fixedShape = HasFixedShapeParameter;
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => {
     double d = i == j
                 ? 0.0
                 : Util.SqrDist(x, i, j, columnIndices, inverseLength);
     return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
   };
   cov.CrossCovariance = (x, xt, i, j) => {
     double d = Util.SqrDist(x, i, xt, j, columnIndices, inverseLength);
     return scale * Math.Pow(1 + 0.5 * d / shape, -shape);
   };
   cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, shape, inverseLength, fixedInverseLength, fixedScale, fixedShape);
   return cov;
 }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
   double @const, scale;
   int degree = DegreeParameter.Value.Value;
   if (degree <= 0) throw new ArgumentException("The degree parameter for CovariancePolynomial must be greater than zero.");
   GetParameterValues(p, out @const, out scale);
   var fixedConst = HasFixedConstParameter;
   var fixedScale = HasFixedScaleParameter;
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, j, columnIndices, 1.0), degree);
   cov.CrossCovariance = (x, xt, i, j) => scale * Math.Pow(@const + Util.ScalarProd(x, i, xt, j, columnIndices, 1.0), degree);
   cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, @const, scale, degree, columnIndices, fixedConst, fixedScale);
   return cov;
 }
    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
      double length, scale;
      GetParameterValues(p, out scale, out length);
      var fixedLength = HasFixedLengthParameter;
      var fixedScale = HasFixedScaleParameter;

      var cov = new ParameterizedCovarianceFunction();
      cov.Covariance = (x, i, j) => {
        double sx = 1.0;
        double s1 = 1.0;
        double s2 = 1.0;
        foreach (var col in columnIndices) {
          sx += x[i, col] * x[j, col];
          s1 += x[i, col] * x[i, col];
          s2 += x[j, col] * x[j, col];
        }

        return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
      };
      cov.CrossCovariance = (x, xt, i, j) => {
        double sx = 1.0;
        double s1 = 1.0;
        double s2 = 1.0;
        foreach (var col in columnIndices) {
          sx += x[i, col] * xt[j, col];
          s1 += x[i, col] * x[i, col];
          s2 += xt[j, col] * xt[j, col];
        }

        return (scale * Math.Asin(sx / (Math.Sqrt((length + s1) * (length + s2)))));
      };
      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, length, scale, columnIndices, fixedLength, fixedScale);
      return cov;
    }
 private static IEnumerable<double> GetGradient(double[,] x, int i, int j, IEnumerable<int> columnIndices, double scale, ParameterizedCovarianceFunction cov,
   bool fixedScale) {
   if (!fixedScale) {
     yield return 2 * scale * cov.Covariance(x, i, j);
   }
   foreach (var g in cov.CovarianceGradient(x, i, j))
     yield return scale * g;
 }
    private static double[,] CalculateL(double[,] x, ParameterizedCovarianceFunction cov, double sqrSigmaNoise) {
      int n = x.GetLength(0);
      var l = new double[n, n];

      // calculate covariances
      for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
          l[j, i] = cov.Covariance(x, i, j) / sqrSigmaNoise;
          if (j == i) l[j, i] += 1.0;
        }
      }

      // cholesky decomposition
      var res = alglib.trfac.spdmatrixcholesky(ref l, n, false);
      if (!res) throw new ArgumentException("Matrix is not positive semidefinite");
      return l;
    }
    public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
      double inverseLength, period, scale;
      GetParameterValues(p, out scale, out period, out inverseLength);
      var fixedInverseLength = HasFixedInverseLengthParameter;
      var fixedPeriod = HasFixedPeriodParameter;
      var fixedScale = HasFixedScaleParameter;
      // create functions
      var cov = new ParameterizedCovarianceFunction();
      cov.Covariance = (x, i, j) => {
        double k = i == j ? 0.0 : GetDistance(x, x, i, j, columnIndices);
        k = Math.PI * k / period;
        k = Math.Sin(k) * inverseLength;
        k = k * k;

        return scale * Math.Exp(-2.0 * k);
      };
      cov.CrossCovariance = (x, xt, i, j) => {
        double k = GetDistance(x, xt, i, j, columnIndices);
        k = Math.PI * k / period;
        k = Math.Sin(k) * inverseLength;
        k = k * k;

        return scale * Math.Exp(-2.0 * k);
      };
      cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, period, inverseLength, fixedInverseLength, fixedPeriod, fixedScale);
      return cov;
    }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
   double scale;
   GetParameterValues(p, out scale);
   var fixedScale = HasFixedScaleParameter;
   var subCov = CovarianceFunctionParameter.Value.GetParameterizedCovarianceFunction(p.Skip(1).ToArray(), columnIndices);
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => scale * subCov.Covariance(x, i, j);
   cov.CrossCovariance = (x, xt, i, j) => scale * subCov.CrossCovariance(x, xt, i, j);
   cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, columnIndices, scale, subCov, fixedScale);
   return cov;
 }
Exemple #36
0
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
   double scale;
   GetParameterValues(p, out scale);
   var fixedScale = HasFixedScaleParameter;
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => i == j ? scale : 0.0;
   cov.CrossCovariance = (x, xt, i, j) => Util.SqrDist(x, i, xt, j, columnIndices, 1.0) < 1e-9 ? scale : 0.0;
   if (fixedScale)
     cov.CovarianceGradient = (x, i, j) => new double[0];
   else
     cov.CovarianceGradient = (x, i, j) => new double[1] { i == j ? 2.0 * scale : 0.0 };
   return cov;
 }
Exemple #37
0
        private static IList <double> GetGradient(double[,] x, int i, int j, int[] columnIndices, double scale, ParameterizedCovarianceFunction cov,
                                                  bool fixedScale)
        {
            var gr = new List <double>((!fixedScale ? 1 : 0) + cov.CovarianceGradient(x, i, j).Count);

            if (!fixedScale)
            {
                gr.Add(2 * scale * cov.Covariance(x, i, j));
            }
            foreach (var g in cov.CovarianceGradient(x, i, j))
            {
                gr.Add(scale * g);
            }
            return(gr);
        }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
   double[] inverseLength;
   GetParameterValues(p, out inverseLength);
   var fixedInverseLength = HasFixedInverseLengthParameter;
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => Util.ScalarProd(x, i, j, inverseLength, columnIndices);
   cov.CrossCovariance = (x, xt, i, j) => Util.ScalarProd(x, i, xt, j, inverseLength, columnIndices);
   if (fixedInverseLength)
     cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();
   else
     cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, inverseLength, columnIndices);
   return cov;
 }
Exemple #39
0
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, int[] columnIndices) {
   double scale;
   GetParameterValues(p, out scale);
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => scale;
   cov.CrossCovariance = (x, xt, i, j) => scale;
   if (HasFixedScaleParameter) {
     cov.CovarianceGradient = (x, i, j) => new double[0];
   } else {
     cov.CovarianceGradient = (x, i, j) => new[] { 2.0 * scale };
   }
   return cov;
 }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
   double inverseLength, scale;
   GetParameterValues(p, out scale, out inverseLength);
   var fixedInverseLength = HasFixedInverseLengthParameter;
   var fixedScale = HasFixedScaleParameter;
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => {
     double d = i == j
             ? 0.0
             : Util.SqrDist(x, i, j, inverseLength, columnIndices);
     return scale * Math.Exp(-d / 2.0);
   };
   cov.CrossCovariance = (x, xt, i, j) => {
     double d = Util.SqrDist(x, i, xt, j, inverseLength, columnIndices);
     return scale * Math.Exp(-d / 2.0);
   };
   cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, inverseLength, columnIndices,
     fixedInverseLength, fixedScale);
   return cov;
 }
 public ParameterizedCovarianceFunction GetParameterizedCovarianceFunction(double[] p, IEnumerable<int> columnIndices) {
   double scale;
   GetParameterValues(p, out scale);
   // create functions
   var cov = new ParameterizedCovarianceFunction();
   cov.Covariance = (x, i, j) => scale;
   cov.CrossCovariance = (x, xt, i, j) => scale;
   if (HasFixedScaleParameter) {
     cov.CovarianceGradient = (x, i, j) => Enumerable.Empty<double>();
   } else {
     cov.CovarianceGradient = (x, i, j) => GetGradient(x, i, j, scale, columnIndices);
   }
   return cov;
 }