Exemple #1
0
        public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices)
        {
            if (p.Length > 0)
            {
                throw new ArgumentException("No parameters allowed for model-based mean function.", "p");
            }
            var solution      = RegressionSolution;
            var variableNames = solution.ProblemData.AllowedInputVariables.ToArray();

            if (variableNames.Length != columnIndices.Length)
            {
                throw new ArgumentException("The number of input variables does not match in MeanModel");
            }
            var variableValues = variableNames.Select(_ => new List <double>()
            {
                0.0
            }).ToArray();                                                                   // or of zeros
            // uses modifyable dataset to pass values to the model
            var ds    = new ModifiableDataset(variableNames, variableValues);
            var mf    = new ParameterizedMeanFunction();
            var model = solution.Model; // effort for parameter access only once

            mf.Mean = (x, i) => {
                ds.ReplaceRow(0, Util.GetRow(x, i, columnIndices).OfType <object>());
                return(model.GetEstimatedValues(ds, 0.ToEnumerable()).Single()); // evaluate the model on the specified row only
            };
            mf.Gradient = (x, i, k) => {
                if (k > 0)
                {
                    throw new ArgumentException();
                }
                return(0.0);
            };
            return(mf);
        }
Exemple #2
0
        public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable <int> columnIndices)
        {
            var termMf = new List <ParameterizedMeanFunction>();
            int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);

            int[] termIndexMap           = new int[totalNumberOfParameters]; // maps k-th parameter to the correct mean-term
            int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the l-th parameter of the correct mean-term
            int   c = 0;

            // get the parameterized mean function for each term
            for (int termIndex = 0; termIndex < terms.Count; termIndex++)
            {
                var numberOfParameters = terms[termIndex].GetNumberOfParameters(numberOfVariables);
                termMf.Add(terms[termIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
                p = p.Skip(numberOfParameters).ToArray();

                for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++)
                {
                    termIndexMap[c]           = termIndex;
                    hyperParameterIndexMap[c] = hyperParameterIndex;
                    c++;
                }
            }

            var mf = new ParameterizedMeanFunction();

            mf.Mean     = (x, i) => termMf.Select(t => t.Mean(x, i)).Sum();
            mf.Gradient = (x, i, k) => {
                return(termMf[termIndexMap[k]].Gradient(x, i, hyperParameterIndexMap[k]));
            };
            return(mf);
        }
Exemple #3
0
 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
   double c;
   GetParameters(p, out c);
   var mf = new ParameterizedMeanFunction();
   mf.Mean = (x, i) => c;
   mf.Gradient = (x, i, k) => {
     if (k > 0) throw new ArgumentException();
     return 1.0;
   };
   return mf;
 }
Exemple #4
0
 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) {
   if (p.Length > 0) throw new ArgumentException("No parameters allowed for zero mean function.", "p");
   var mf = new ParameterizedMeanFunction();
   mf.Mean = (x, i) => 0.0;
   mf.Gradient = (x, i, k) => {
     if (k > 0)
       throw new ArgumentException();
     return 0.0;
   };
   return mf;
 }
 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, IEnumerable<int> columnIndices) {
   double[] weights;
   int[] columns = columnIndices.ToArray();
   GetParameter(p, out weights);
   var mf = new ParameterizedMeanFunction();
   mf.Mean = (x, i) => {
     // sanity check
     if (weights.Length != columns.Length) throw new ArgumentException("The number of rparameters must match the number of variables for the linear mean function.");
     return Util.ScalarProd(weights, Util.GetRow(x, i, columns));
   };
   mf.Gradient = (x, i, k) => {
     if (k > columns.Length) throw new ArgumentException();
     return x[i, columns[k]];
   };
   return mf;
 }
        public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices)
        {
            var factorMf = new List <ParameterizedMeanFunction>();
            int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);

            int[] factorIndexMap         = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the correct mean-term
            int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the l-th hyperparameter of the correct mean-term
            int   c = 0;

            // get the parameterized mean function for each term
            for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++)
            {
                var numberOfParameters = factors[factorIndex].GetNumberOfParameters(numberOfVariables);
                factorMf.Add(factors[factorIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
                p = p.Skip(numberOfParameters).ToArray();

                for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++)
                {
                    factorIndexMap[c]         = factorIndex;
                    hyperParameterIndexMap[c] = hyperParameterIndex;
                    c++;
                }
            }

            var mf = new ParameterizedMeanFunction();

            mf.Mean     = (x, i) => factorMf.Select(t => t.Mean(x, i)).Aggregate((a, b) => a * b);
            mf.Gradient = (x, i, k) => {
                double result = 1.0;
                int    hyperParameterFactorIndex = factorIndexMap[k];
                for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++)
                {
                    if (factorIndex == hyperParameterFactorIndex)
                    {
                        // multiply gradient
                        result *= factorMf[factorIndex].Gradient(x, i, hyperParameterIndexMap[k]);
                    }
                    else
                    {
                        // multiply mean
                        result *= factorMf[factorIndex].Mean(x, i);
                    }
                }
                return(result);
            };
            return(mf);
        }
Exemple #7
0
        public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices)
        {
            double c;

            GetParameters(p, out c);
            var mf = new ParameterizedMeanFunction();

            mf.Mean     = (x, i) => c;
            mf.Gradient = (x, i, k) => {
                if (k > 0)
                {
                    throw new ArgumentException();
                }
                return(1.0);
            };
            return(mf);
        }
Exemple #8
0
        public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices)
        {
            if (p.Length > 0)
            {
                throw new ArgumentException("No parameters allowed for zero mean function.", "p");
            }
            var mf = new ParameterizedMeanFunction();

            mf.Mean     = (x, i) => 0.0;
            mf.Gradient = (x, i, k) => {
                if (k > 0)
                {
                    throw new ArgumentException();
                }
                return(0.0);
            };
            return(mf);
        }
Exemple #9
0
 public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
   if (p.Length > 0) throw new ArgumentException("No parameters allowed for model-based mean function.", "p");
   var solution = RegressionSolution;
   var variableNames = solution.ProblemData.AllowedInputVariables.ToArray();
   if (variableNames.Length != columnIndices.Length)
     throw new ArgumentException("The number of input variables does not match in MeanModel");
   var variableValues = variableNames.Select(_ => new List<double>() { 0.0 }).ToArray(); // or of zeros
   // uses modifyable dataset to pass values to the model
   var ds = new ModifiableDataset(variableNames, variableValues);
   var mf = new ParameterizedMeanFunction();
   var model = solution.Model; // effort for parameter access only once
   mf.Mean = (x, i) => {
     ds.ReplaceRow(0, Util.GetRow(x, i, columnIndices).OfType<object>());
     return model.GetEstimatedValues(ds, 0.ToEnumerable()).Single(); // evaluate the model on the specified row only
   };
   mf.Gradient = (x, i, k) => {
     if (k > 0)
       throw new ArgumentException();
     return 0.0;
   };
   return mf;
 }
Exemple #10
0
        public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices)
        {
            double[] weights;
            int[]    columns = columnIndices;
            GetParameter(p, out weights);
            var mf = new ParameterizedMeanFunction();

            mf.Mean = (x, i) => {
                // sanity check
                if (weights.Length != columns.Length)
                {
                    throw new ArgumentException("The number of rparameters must match the number of variables for the linear mean function.");
                }
                return(Util.ScalarProd(weights, Util.GetRow(x, i, columns).ToArray()));
            };
            mf.Gradient = (x, i, k) => {
                if (k > columns.Length)
                {
                    throw new ArgumentException();
                }
                return(x[i, columns[k]]);
            };
            return(mf);
        }
Exemple #11
0
    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
      var factorMf = new List<ParameterizedMeanFunction>();
      int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);
      int[] factorIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the correct mean-term
      int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th hyperparameter to the l-th hyperparameter of the correct mean-term
      int c = 0;
      // get the parameterized mean function for each term
      for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++) {
        var numberOfParameters = factors[factorIndex].GetNumberOfParameters(numberOfVariables);
        factorMf.Add(factors[factorIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
        p = p.Skip(numberOfParameters).ToArray();

        for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) {
          factorIndexMap[c] = factorIndex;
          hyperParameterIndexMap[c] = hyperParameterIndex;
          c++;
        }
      }

      var mf = new ParameterizedMeanFunction();
      mf.Mean = (x, i) => factorMf.Select(t => t.Mean(x, i)).Aggregate((a, b) => a * b);
      mf.Gradient = (x, i, k) => {
        double result = 1.0;
        int hyperParameterFactorIndex = factorIndexMap[k];
        for (int factorIndex = 0; factorIndex < factors.Count; factorIndex++) {
          if (factorIndex == hyperParameterFactorIndex) {
            // multiply gradient
            result *= factorMf[factorIndex].Gradient(x, i, hyperParameterIndexMap[k]);
          } else {
            // multiply mean
            result *= factorMf[factorIndex].Mean(x, i);
          }
        }
        return result;
      };
      return mf;
    }
Exemple #12
0
    public ParameterizedMeanFunction GetParameterizedMeanFunction(double[] p, int[] columnIndices) {
      var termMf = new List<ParameterizedMeanFunction>();
      int totalNumberOfParameters = GetNumberOfParameters(numberOfVariables);
      int[] termIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the correct mean-term
      int[] hyperParameterIndexMap = new int[totalNumberOfParameters]; // maps k-th parameter to the l-th parameter of the correct mean-term
      int c = 0;
      // get the parameterized mean function for each term
      for (int termIndex = 0; termIndex < terms.Count; termIndex++) {
        var numberOfParameters = terms[termIndex].GetNumberOfParameters(numberOfVariables);
        termMf.Add(terms[termIndex].GetParameterizedMeanFunction(p.Take(numberOfParameters).ToArray(), columnIndices));
        p = p.Skip(numberOfParameters).ToArray();

        for (int hyperParameterIndex = 0; hyperParameterIndex < numberOfParameters; hyperParameterIndex++) {
          termIndexMap[c] = termIndex;
          hyperParameterIndexMap[c] = hyperParameterIndex;
          c++;
        }
      }

      var mf = new ParameterizedMeanFunction();
      mf.Mean = (x, i) => termMf.Select(t => t.Mean(x, i)).Sum();
      mf.Gradient = (x, i, k) => {
        return termMf[termIndexMap[k]].Gradient(x, i, hyperParameterIndexMap[k]);
      };
      return mf;
    }