[9519] | 1 | PROBLEM SymbRegKoza
|
---|
| 2 | CODE <<
|
---|
[9696] | 3 | double[,] x;
|
---|
| 4 | double[] y;
|
---|
[10086] | 5 | int[] rows;
|
---|
[9519] | 6 | string[] variableNames;
|
---|
[10086] | 7 | double[] randomConsts;
|
---|
| 8 |
|
---|
[9519] | 9 | Dictionary<string,int> nameToCol;
|
---|
| 10 |
|
---|
| 11 | double GetValue(double[,] data, string varName, int row) {
|
---|
| 12 | if(nameToCol == null) {
|
---|
| 13 | /* init mapping */
|
---|
| 14 | nameToCol = new Dictionary<string, int>();
|
---|
| 15 | for(int i=0; i<variableNames.Length; i++) {
|
---|
| 16 | nameToCol[variableNames[i]] = i;
|
---|
| 17 | }
|
---|
| 18 | }
|
---|
[9696] | 19 | return x[row, nameToCol[varName]];
|
---|
[9519] | 20 | }
|
---|
| 21 |
|
---|
| 22 | double RSquared(IEnumerable<double> xs, IEnumerable<double> ys) {
|
---|
[10086] | 23 | // calculate Pearson's correlation in one pass over xs and ys
|
---|
| 24 | double sumx = 0.0;
|
---|
| 25 | double sumy = 0.0;
|
---|
| 26 | double sumxSq = 0.0;
|
---|
| 27 | double sumySq = 0.0;
|
---|
| 28 | double sumxy = 0.0;
|
---|
| 29 | int n = 0;
|
---|
| 30 | var xEnum = xs.GetEnumerator();
|
---|
| 31 | var yEnum = ys.GetEnumerator();
|
---|
| 32 | while(xEnum.MoveNext() & yEnum.MoveNext()) {
|
---|
| 33 | sumx += xEnum.Current;
|
---|
| 34 | sumy += yEnum.Current;
|
---|
| 35 | sumxSq += xEnum.Current * xEnum.Current;
|
---|
| 36 | sumySq += yEnum.Current * yEnum.Current;
|
---|
| 37 | sumxy += xEnum.Current * yEnum.Current;
|
---|
| 38 | n++;
|
---|
| 39 | }
|
---|
| 40 | System.Diagnostics.Debug.Assert(!(xEnum.MoveNext() | yEnum.MoveNext()));
|
---|
| 41 |
|
---|
| 42 | double num;
|
---|
| 43 | double den;
|
---|
| 44 | double r = 0.0;
|
---|
| 45 | num = sumxy - ( ( sumx * sumy ) / n );
|
---|
| 46 | den = Math.Sqrt( ( sumxSq - ( sumx*sumx ) / n ) *
|
---|
| 47 | ( sumySq - ( sumy*sumy ) / n ) );
|
---|
| 48 | if(den > 0){
|
---|
| 49 | r = num / den;
|
---|
| 50 | }
|
---|
| 51 | return r*r;
|
---|
| 52 | }
|
---|
[9696] | 53 | >>
|
---|
[9519] | 54 |
|
---|
[9696] | 55 | INIT <<
|
---|
[10099] | 56 | // generate 500 cases of poly-10 benchmark function
|
---|
[9696] | 57 | int n = 500;
|
---|
| 58 | variableNames = new string[] {"x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10" };
|
---|
| 59 | var rand = new System.Random();
|
---|
| 60 | x = new double[n, 10];
|
---|
| 61 | y = new double[n];
|
---|
[10086] | 62 | for(int row = 0; row < n; row++) {
|
---|
[9696] | 63 | for(int col = 0; col < 10; col++) {
|
---|
| 64 | x[row, col] = rand.NextDouble() * 2.0 - 1.0;
|
---|
[9519] | 65 | }
|
---|
[9696] | 66 | y[row] = x[row, 0] * x[row, 1] +
|
---|
| 67 | x[row, 2] * x[row, 3] +
|
---|
| 68 | x[row, 4] * x[row, 5] +
|
---|
| 69 | x[row, 0] * x[row, 6] + x[row, 8] +
|
---|
| 70 | x[row, 2] * x[row, 5] + x[row, 9];
|
---|
[9519] | 71 | }
|
---|
[10086] | 72 |
|
---|
| 73 | rows = System.Linq.Enumerable.Range(0, n).ToArray();
|
---|
| 74 |
|
---|
[10099] | 75 | // generate 100 random constants in [-10.0 .. 10.0[
|
---|
| 76 | randomConsts = Enumerable.Range(0, 100).Select(i => rand.NextDouble()*20.0 - 10.0).ToArray();
|
---|
[9519] | 77 | >>
|
---|
| 78 |
|
---|
| 79 | NONTERMINALS
|
---|
| 80 | Model<<int row, out double val>>.
|
---|
| 81 | RPB<<int row, out double val>>.
|
---|
| 82 | Addition<<int row, out double val>>.
|
---|
| 83 | Subtraction<<int row, out double val>>.
|
---|
| 84 | Multiplication<<int row, out double val>>.
|
---|
| 85 | Division<<int row, out double val>>.
|
---|
| 86 |
|
---|
| 87 | TERMINALS
|
---|
| 88 | ERC<<out double val>>
|
---|
| 89 | CONSTRAINTS
|
---|
[10086] | 90 | val IN SET << randomConsts >>
|
---|
[9519] | 91 | .
|
---|
| 92 |
|
---|
| 93 | Var<<out string varName>>
|
---|
| 94 | CONSTRAINTS
|
---|
[10086] | 95 | varName IN SET << variableNames >>
|
---|
[9519] | 96 | .
|
---|
| 97 |
|
---|
| 98 | RULES
|
---|
| 99 | Model<<int row, out double val>> =
|
---|
| 100 | RPB<<row, out val>> .
|
---|
| 101 |
|
---|
| 102 | RPB<<int row, out double val>> = LOCAL << string varName; >>
|
---|
| 103 | Addition<<row, out val>>
|
---|
| 104 | | Subtraction<<row, out val>>
|
---|
| 105 | | Division<<row, out val>>
|
---|
| 106 | | Multiplication<<row, out val>>
|
---|
[9696] | 107 | | Var<<out varName>> SEM << val = GetValue(x, varName, row); >>
|
---|
[10086] | 108 | /* | ERC<<out val>> */
|
---|
[9519] | 109 | .
|
---|
| 110 |
|
---|
| 111 | Addition<<int row, out double val>> = LOCAL << double x1, x2; >>
|
---|
| 112 | RPB<<row, out x1>> RPB<<row, out x2>> SEM<< val = x1 + x2; >>
|
---|
| 113 | .
|
---|
| 114 | Subtraction<<int row, out double val>> = LOCAL << double x1, x2; >>
|
---|
| 115 | RPB<<row, out x1>> RPB<<row, out x2>> SEM<< val = x1 - x2; >>
|
---|
| 116 | .
|
---|
| 117 | Division<<int row, out double val>> = LOCAL << double x1, x2; >>
|
---|
| 118 | RPB<<row, out x1>> RPB<<row, out x2>> SEM<< val = x1 / x2; >>
|
---|
| 119 | .
|
---|
| 120 | Multiplication<<int row, out double val>> = LOCAL << double x1, x2; >>
|
---|
| 121 | RPB<<row, out x1>> RPB<<row, out x2>> SEM<< val = x1 * x2; >>
|
---|
| 122 | .
|
---|
| 123 |
|
---|
[10086] | 124 | MAXIMIZE
|
---|
[9519] | 125 | <<
|
---|
| 126 | var predicted = rows.Select(r => {
|
---|
| 127 | double result;
|
---|
| 128 | Model(r, out result); /* we can call the root symbol directly */
|
---|
| 129 | return result;
|
---|
| 130 | });
|
---|
[9696] | 131 | return RSquared(predicted, y);
|
---|
[9519] | 132 | >>
|
---|
| 133 | END SymbRegKoza.
|
---|