Changeset 13963 for trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/VariableNetworks
- Timestamp:
- 06/30/16 16:13:39 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/VariableNetworks/VariableNetwork.cs
r13939 r13963 24 24 using System.Linq; 25 25 using System.Text; 26 using System.Windows.Forms; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; … … 96 97 97 98 List<string> description = new List<string>(); // store information how the variable is actually produced 99 List<string[]> inputVarNames = new List<string[]>(); // store information to produce graphviz file 98 100 99 101 var nrand = new NormalDistributedRandom(random, 0, 1); 100 102 for (int c = 0; c < numLvl0; c++) { 101 103 var datai = Enumerable.Range(0, TestPartitionEnd).Select(_ => nrand.NextDouble()).ToList(); 104 inputVarNames.Add(new string[] { }); 102 105 description.Add("~ N(0, 1)"); 103 106 lvl0.Add(datai); … … 108 111 int numLvl1 = (int)Math.Ceiling(numberOfFeatures * 0.33); 109 112 for (int c = 0; c < numLvl1; c++) { 110 string desc;111 var x = GenerateRandomFunction(random, lvl0, out desc);113 string[] selectedVarNames; 114 var x = GenerateRandomFunction(random, lvl0, out selectedVarNames); 112 115 var sigma = x.StandardDeviation(); 113 116 var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio))); 114 117 lvl1.Add(x.Select(t => t + noisePrng.NextDouble()).ToList()); 118 119 inputVarNames.Add(selectedVarNames); 120 var desc = string.Format("f({0})", string.Join(",", selectedVarNames)); 115 121 description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma)); 116 122 } … … 120 126 int numLvl2 = (int)Math.Ceiling(numberOfFeatures * 0.2); 121 127 for (int c = 0; c < numLvl2; c++) { 122 string desc;123 var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).ToList(), out desc);128 string[] selectedVarNames; 129 var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).ToList(), out selectedVarNames); 124 130 var sigma = x.StandardDeviation(); 125 131 var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio))); 126 132 lvl2.Add(x.Select(t => t + noisePrng.NextDouble()).ToList()); 133 134 inputVarNames.Add(selectedVarNames); 135 var desc = string.Format("f({0})", string.Join(",", selectedVarNames)); 127 136 description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma)); 128 137 } … … 132 141 int numLvl3 = numberOfFeatures - numLvl0 - numLvl1 - numLvl2; 133 142 for (int c = 0; c < numLvl3; c++) { 134 string desc;135 var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).Concat(lvl2).ToList(), out desc);143 string[] selectedVarNames; 144 var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).Concat(lvl2).ToList(), out selectedVarNames); 136 145 var sigma = x.StandardDeviation(); 137 146 var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio))); 138 147 lvl3.Add(x.Select(t => t + noisePrng.NextDouble()).ToList()); 148 149 inputVarNames.Add(selectedVarNames); 150 var desc = string.Format("f({0})", string.Join(",", selectedVarNames)); 139 151 description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma)); 140 152 } 153 154 networkDefinition = string.Join(Environment.NewLine, variableNames.Zip(description, (n, d) => n + d)); 155 // for graphviz 156 networkDefinition += Environment.NewLine + "digraph G {"; 157 foreach (var t in variableNames.Zip(inputVarNames, Tuple.Create).OrderBy(t => t.Item1)) { 158 var name = t.Item1; 159 var selectedVarNames = t.Item2; 160 foreach (var selectedVarName in selectedVarNames) { 161 networkDefinition += Environment.NewLine + selectedVarName + " -> " + name; 162 } 163 } 164 networkDefinition += Environment.NewLine + "}"; 141 165 142 166 // return a random permutation of all variables 143 167 var allVars = lvl0.Concat(lvl1).Concat(lvl2).Concat(lvl3).ToList(); 144 networkDefinition = string.Join(Environment.NewLine, variableNames.Zip(description, (n, d) => n + d));145 168 var orderedVars = allVars.Zip(variableNames, Tuple.Create).OrderBy(t => t.Item2).Select(t => t.Item1).ToList(); 146 169 variableNames = variableNames.OrderBy(n => n).ToArray(); … … 149 172 150 173 // sample the input variables that are actually used and sample from a Gaussian process 151 private IEnumerable<double> GenerateRandomFunction(IRandom rand, List<List<double>> xs, out string desc) { 152 int nRows = xs.First().Count; 153 174 private IEnumerable<double> GenerateRandomFunction(IRandom rand, List<List<double>> xs, out string[] selectedVarNames) { 154 175 double r = -Math.Log(1.0 - rand.NextDouble()) * 2.0; // r is exponentially distributed with lambda = 2 155 176 int nl = (int)Math.Floor(1.5 + r); // number of selected vars is likely to be between three and four … … 160 181 161 182 var selectedVars = selectedIdx.Select(i => xs[i]).ToArray(); 162 desc = string.Format("f({0})", string.Join(",", selectedIdx.Select(i => VariableNames[i])));183 selectedVarNames = selectedIdx.Select(i => VariableNames[i]).ToArray(); 163 184 return SampleGaussianProcess(random, selectedVars); 164 185 } … … 171 192 // sample length-scales 172 193 var l = Enumerable.Range(0, nl) 173 .Select(_ => random.NextDouble() *2+0.5)194 .Select(_ => random.NextDouble() * 2 + 0.5) 174 195 .ToArray(); 175 196 // calculate covariance matrix
Note: See TracChangeset
for help on using the changeset viewer.