Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Friedman/FriedmanRandomFunction.cs

Last change on this file was 18032, checked in by chaider, 3 years ago

#3075 noise generation method to ValueGenerator; use same method for generating noise in friedman and feynman instances

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Random;
28
29namespace HeuristicLab.Problems.Instances.DataAnalysis {
30  public class FriedmanRandomFunction : ArtificialRegressionDataDescriptor {
31    private readonly int nTrainingSamples;
32    private readonly int nTestSamples;
33
34    private readonly int numberOfFeatures;
35    private readonly double noiseRatio;
36    private readonly IRandom random;
37
38    public override string Name { get { return string.Format("FriedmanRandomFunction-{0:0%} ({1} dim)", noiseRatio, numberOfFeatures); } }
39    public override string Description {
40      get {
41        return "The data are generated using the random function generator described in 'Friedman: Greedy Function Approximation: A Gradient Boosting Machine, 1999'.";
42      }
43    }
44
45    public FriedmanRandomFunction(int numberOfFeatures, double noiseRatio,
46      IRandom rand)
47      : this(500, 5000, numberOfFeatures, noiseRatio, rand) { }
48
49    public FriedmanRandomFunction(int nTrainingSamples, int nTestSamples,
50      int numberOfFeatures, double noiseRatio, IRandom rand) {
51      this.nTrainingSamples = nTrainingSamples;
52      this.nTestSamples = nTestSamples;
53      this.noiseRatio = noiseRatio;
54      this.random = rand;
55      this.numberOfFeatures = numberOfFeatures;
56    }
57
58    protected override string TargetVariable { get { return "Y"; } }
59
60    protected override string[] VariableNames {
61      get { return AllowedInputVariables.Concat(new string[] { "Y" }).ToArray(); }
62    }
63
64    protected override string[] AllowedInputVariables {
65      get {
66        return Enumerable.Range(1, numberOfFeatures)
67          .Select(i => string.Format("X{0:000}", i))
68          .ToArray();
69      }
70    }
71
72    protected override int TrainingPartitionStart { get { return 0; } }
73    protected override int TrainingPartitionEnd { get { return nTrainingSamples; } }
74    protected override int TestPartitionStart { get { return nTrainingSamples; } }
75    protected override int TestPartitionEnd { get { return nTrainingSamples + nTestSamples; } }
76
77
78    protected override List<List<double>> GenerateValues() {
79      List<List<double>> data = new List<List<double>>();
80
81      var nrand = new NormalDistributedRandom(random, 0, 1);
82      for (int c = 0; c < numberOfFeatures; c++) {
83        var datai = Enumerable.Range(0, TestPartitionEnd).Select(_ => nrand.NextDouble()).ToList();
84        data.Add(datai);
85      }
86      var y = GenerateRandomFunction(random, data);
87
88      //var targetSigma = y.StandardDeviation();
89      //var noisePrng = new NormalDistributedRandom(random, 0, targetSigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
90
91      //data.Add(y.Select(t => t + noisePrng.NextDouble()).ToList());
92
93      data.Add(ValueGenerator.GenerateNoise(y, random, noiseRatio));
94
95      return data;
96    }
97
98    // as described in Greedy Function Approximation paper
99    private IEnumerable<double> GenerateRandomFunction(IRandom rand, List<List<double>> xs, int nTerms = 20) {
100      int nRows = xs.First().Count;
101
102      var gz = new List<double[]>();
103      for (int i = 0; i < nTerms; i++) {
104
105        // alpha ~ U(-1, 1)
106        double alpha = rand.NextDouble() * 2 - 1;
107        double r = -Math.Log(1.0 - rand.NextDouble()) * 2.0; // r is exponentially distributed with lambda = 2
108        int nl = (int)Math.Floor(1.5 + r); // number of selected vars is likely to be between three and four
109
110
111        var selectedVars = xs.Shuffle(random).Take(nl).ToArray();
112        gz.Add(SampleRandomFunction(random, selectedVars)
113          .Select(f => alpha * f)
114          .ToArray());
115      }
116      // sum up
117      return Enumerable.Range(0, nRows)
118        .Select(r => gz.Sum(gzi => gzi[r]));
119    }
120
121    private IEnumerable<double> SampleRandomFunction(IRandom random, List<double>[] xs) {
122      int nl = xs.Length;
123      // mu is generated from same distribution as x
124      double[] mu = Enumerable.Range(0, nl).Select(_ => random.NextDouble() * 2 - 1).ToArray();
125      var condNum = 4.0 / 0.01; // as given in the paper for max and min eigen values
126
127      // temporarily use different random number generator in alglib
128      var curRand = alglib.math.rndobject;
129      alglib.math.rndobject = new System.Random(random.Next());
130
131      alglib.spdmatrixrndcond(nl, condNum, out var v);
132      // restore
133      alglib.math.rndobject = curRand;
134
135      int nRows = xs.First().Count;
136      var z = new double[nl];
137      var y = new double[nl];
138      for (int i = 0; i < nRows; i++) {
139        for (int j = 0; j < nl; j++) z[j] = xs[j][i] - mu[j];
140        alglib.rmatrixmv(nl, nl, v, 0, 0, 0, z, 0, ref y, 0);
141
142        // dot prod
143        var s = 0.0;
144        for (int j = 0; j < nl; j++) s += z[j] * y[j];
145
146        yield return s;
147      }
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.