Changeset 16058


Ignore:
Timestamp:
08/07/18 14:28:47 (3 years ago)
Author:
fholzing
Message:

#2904: Added Unit-Tests for Regression

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2904_CalculateImpacts/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/VariableImpactCalculationTest.cs

    r16023 r16058  
    22using System.Collections.Generic;
    33using System.Linq;
    4 using HeuristicLab.Algorithms.ALPS;
    5 using HeuristicLab.Algorithms.DataAnalysis;
    6 using HeuristicLab.Data;
     4using HeuristicLab.Common;
    75using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    86using HeuristicLab.Problems.DataAnalysis.Symbolic;
    97using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    108using HeuristicLab.Problems.Instances.DataAnalysis;
    11 using HeuristicLab.Selection;
    12 using HeuristicLab.Tests;
     9using HeuristicLab.Random;
    1310using Microsoft.VisualStudio.TestTools.UnitTesting;
    1411
     
    1714  [TestClass()]
    1815  public class VariableImpactCalculationTest {
    19     [TestMethod]
    20     [TestCategory("Problems.DataAnalysis")]
    21     [TestProperty("Time", "medium")]
    22     public void RunAlpsSymRegFactoryVariableMibaC1Test() {
    23       var alpsGA = CreateAlpsGaSymRegMibaC1Sample();
    24       alpsGA.Start();
    25       var ers = alpsGA.Results.FirstOrDefault(v => v.Name == "Variable impacts").Value as DoubleMatrix;
    26       Assert.IsNotNull(ers);
    27       Assert.IsTrue(ers.Rows == 22);
    28       List<string> expectedVariableOrder = new List<string> { "Oil", "Grooving", "Material_Cat", "x20", "Material", "x14", "x12", "x3", "x7", "x2", "x16", "x15", "x8", "x10", "x11", "x22", "x4", "x5", "x6", "x9", "x17", "x13" };
    29       List<double> expectedVariableImpacts = new List<double> { 0.206, 0.179, 0.136, 0.099, 0.092, 0.07, 0.048, 0.033, 0.029, 0.026, 0.017, 0.01, 0.007, 0.007, 0.007, 0.006, 0.005, 0.005, 0.005, 0.005, 0.005, 0.003 };
    30       Assert.IsTrue(ers.RowNames.SequenceEqual(expectedVariableOrder));
    31       Assert.IsTrue(ers.SequenceEqual(expectedVariableImpacts));
    32     }
    33 
    34     [TestMethod]
    35     [TestCategory("Problems.DataAnalysis")]
    36     [TestProperty("Time", "medium")]
    37     public void RunAlpsSymRegFactoryVariableMibaWear1Test() {
    38       var alpsGA = CreateAlpsGaSymRegMibaWear1Sample();
    39       alpsGA.Start();
    40       var ers = alpsGA.Results.FirstOrDefault(v => v.Name == "Variable impacts").Value as DoubleMatrix;
    41       Assert.IsNotNull(ers);
    42       Assert.IsTrue(ers.Rows == 27);
    43       List<string> expectedVariableOrder = new List<string> { "Oil", "Material_Cat", "Material", "Grooving", "x8", "x1", "x14", "x11", "x19", "x10", "x5", "x9", "x17", "x6", "x18", "x7", "Source1", "x2", "x3", "x4", "x16", "x20", "x12", "x13", "x15", "x22", "x21" };
    44       List<double> expectedVariableImpacts = new List<double> { 0.194, 0.184, 0.163, 0.155, 0.022, 0.019, 0.019, 0.018, 0.017, 0.016, 0.015, 0.015, 0.015, 0.014, 0.014, 0.013, 0.012, 0.011, 0.011, 0.011, 0.011, 0.011, 0.01, 0.009, 0.009, 0.008, 0.006 };
    45       Assert.IsTrue(ers.RowNames.SequenceEqual(expectedVariableOrder));
    46       Assert.IsTrue(ers.SequenceEqual(expectedVariableImpacts));
    47     }
    48 
    49     [TestMethod]
    50     [TestCategory("Problems.DataAnalysis")]
    51     [TestProperty("Time", "medium")]
    52     public void RunAlpsSymRegFactoryVariableRealworldTowerTest() {
    53       var alpsGA = CreateAlpsGaSymReRealworldTowerSample();
    54       alpsGA.Start();
    55       var ers = alpsGA.Results.FirstOrDefault(v => v.Name == "Variable impacts").Value as DoubleMatrix;
    56       Assert.IsNotNull(ers);
    57       Assert.IsTrue(ers.Rows == 25);
    58       List<string> expectedVariableOrder = new List<string> { "x5", "x4", "x21", "x22", "x13", "x3", "x9", "x19", "x1", "x12", "x8", "x10", "x25", "x11", "x23", "x7", "x24", "x6", "x14", "x15", "x20", "x2", "x16", "x18", "x17" };
    59       List<double> expectedVariableImpacts = new List<double> { 0.057, 0.051, 0.047, 0.047, 0.046, 0.045, 0.044, 0.044, 0.043, 0.043, 0.041, 0.04, 0.04, 0.038, 0.038, 0.037, 0.037, 0.036, 0.036, 0.034, 0.033, 0.031, 0.031, 0.031, 0.03 };
    60       Assert.IsTrue(ers.RowNames.SequenceEqual(expectedVariableOrder));
    61       Assert.IsTrue(ers.SequenceEqual(expectedVariableImpacts));
    62     }
    63 
    64     [TestMethod]
    65     [TestCategory("Problems.DataAnalysis")]
    66     [TestProperty("Time", "short")]
    67     public void RunRandomForestPolyTenTest() {
    68       var randomForestRegression = new RandomForestRegression() {
    69         M = 0.4,
    70         R = 0.3,
    71         NumberOfTrees = 50,
    72         SetSeedRandomly = false,
    73         Seed = 42
    74       };
    75 
    76       var symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
    77       symbRegProblem.ProblemData = new PolyTen(555000).GenerateRegressionData();
    78       randomForestRegression.Problem = symbRegProblem;
    79       randomForestRegression.Start();
    80 
    81       var rfs = randomForestRegression.Results.FirstOrDefault(v => v.Name == "Random forest regression solution").Value as RandomForestRegressionSolution;
    82       Assert.IsNotNull(rfs);
    83       var result = new RegressionSolutionVariableImpactsCalculator().Calculate(rfs);
    84 
    85       //Set expected impacts
    86       List<Tuple<string, double>> aux = new List<Tuple<string, double>>();
    87       aux.Add(new Tuple<string, double>("X6", 0.14984398650032071));
    88       aux.Add(new Tuple<string, double>("X5", 0.14361275357221248));
    89       aux.Add(new Tuple<string, double>("X1", 0.10607502238071009));
    90       aux.Add(new Tuple<string, double>("X3", 0.1056849194554953));
    91       aux.Add(new Tuple<string, double>("X4", 0.0906750779077492));
    92       aux.Add(new Tuple<string, double>("X2", 0.089817766590950532));
    93       aux.Add(new Tuple<string, double>("X8", 0.042797597332305481));
    94       aux.Add(new Tuple<string, double>("X9", 0.038609311722408446));
    95       aux.Add(new Tuple<string, double>("X10", 0.036202503161574362));
    96       aux.Add(new Tuple<string, double>("X7", 0.033539038256832465));
    97       Assert.IsTrue(result.SequenceEqual(aux));
    98     }
    99 
    100     [TestMethod]
    101     [TestCategory("Problems.DataAnalysis")]
    102     [TestProperty("Time", "short")]
    103     public void RunLinearRegressionBreimanITest() {
    104       LinearRegression lr = new LinearRegression();
    105       var symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
    106       symbRegProblem.ProblemData = new BreimanOne(1234).GenerateRegressionData();
    107 
    108       lr.Problem = symbRegProblem;
    109       lr.Start();
    110 
    111       var lrs = lr.Results.FirstOrDefault(v => v.Name == "Linear regression solution").Value as SymbolicRegressionSolution;
    112       Assert.IsNotNull(lrs);
    113       var result = new RegressionSolutionVariableImpactsCalculator().Calculate(lrs);
    114 
    115       //Set expected impacts
    116       List<Tuple<string, double>> aux = new List<Tuple<string, double>>();
    117       aux.Add(new Tuple<string, double>("X1", 0.43328518823918716));
    118       aux.Add(new Tuple<string, double>("X2", 0.073284548674773631));
    119       aux.Add(new Tuple<string, double>("X5", 0.070306657566311159));
    120       aux.Add(new Tuple<string, double>("X3", 0.035352205426012917));
    121       aux.Add(new Tuple<string, double>("X6", 0.031710492680145475));
    122       aux.Add(new Tuple<string, double>("X4", 0.0081659530036176653));
    123       aux.Add(new Tuple<string, double>("X7", 0.0070869550705255913));
    124       aux.Add(new Tuple<string, double>("X9", 8.0546619615096127E-05));
    125       aux.Add(new Tuple<string, double>("X8", 6.2072921147349192E-05));
    126       aux.Add(new Tuple<string, double>("X10", 1.9113559758254794E-06));
    127 
    128       Assert.IsTrue(result.SequenceEqual(aux));
    129     }
    130 
    131     private AlpsGeneticAlgorithm CreateAlpsGaSymRegMibaC1Sample() {
    132       AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
    133       #region Problem Configuration
    134       var provider = new MibaFrictionRegressionInstanceProvider();
    135       var instance = provider.GetDataDescriptors().Single(x => x.Name.StartsWith("CF1"));
    136       var symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
    137       symbRegProblem.Load(provider.LoadData(instance));
    138 
    139       symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 35;
    140       symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 35;
    141 
    142       var grammar = (TypeCoherentExpressionGrammar)symbRegProblem.SymbolicExpressionTreeGrammar;
    143       grammar.Symbols.OfType<Exponential>().Single().Enabled = false;
    144       grammar.Symbols.OfType<Logarithm>().Single().Enabled = false;
    145 
    146       #endregion
    147       #region Algorithm Configuration
    148       alpsGa.Name = "ALPS Genetic Programming - Symbolic Regression";
    149       alpsGa.Description = "An ALPS-GP to solve a symbolic regression problem";
    150       alpsGa.Problem = symbRegProblem;
    151       SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(alpsGa,
    152         numberOfLayers: 1000,
    153         popSize: 100,
    154         mutationRate: 0.25,
    155         elites: 1,
    156         plusSelection: false,
    157         agingScheme: AgingScheme.Polynomial,
    158         ageGap: 15,
    159         ageInheritance: 1.0,
    160         maxGens: 10);
    161 
    162       alpsGa.SetSeedRandomly.Value = false;
    163       alpsGa.Seed.Value = 1234;
    164       #endregion
    165       return alpsGa;
    166     }
    167     private AlpsGeneticAlgorithm CreateAlpsGaSymRegMibaWear1Sample() {
    168       AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
    169       #region Problem Configuration
    170       var provider = new MibaFrictionRegressionInstanceProvider();
    171       var instance = provider.GetDataDescriptors().Single(x => x.Name.StartsWith("Wear1"));
    172       var symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
    173       symbRegProblem.Load(provider.LoadData(instance));
    174 
    175       symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 77;
    176       symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 77;
    177 
    178       var grammar = (TypeCoherentExpressionGrammar)symbRegProblem.SymbolicExpressionTreeGrammar;
    179 
    180       #endregion
    181       #region Algorithm Configuration
    182       alpsGa.Name = "ALPS Genetic Programming - Symbolic Regression";
    183       alpsGa.Description = "An ALPS-GP to solve a symbolic regression problem";
    184       alpsGa.Problem = symbRegProblem;
    185       SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<RandomSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(alpsGa,
    186         numberOfLayers: 1000,
    187         popSize: 200,
    188         mutationRate: 0.25,
    189         elites: 1,
    190         plusSelection: false,
    191         agingScheme: AgingScheme.Polynomial,
    192         ageGap: 15,
    193         ageInheritance: 1.0,
    194         maxGens: 10);
    195 
    196       alpsGa.SetSeedRandomly.Value = false;
    197       alpsGa.Seed.Value = 11121314;
    198       #endregion
    199       return alpsGa;
    200     }
    201     private AlpsGeneticAlgorithm CreateAlpsGaSymReRealworldTowerSample() {
    202       AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
    203       #region Problem Configuration
    204       var provider = new RegressionRealWorldInstanceProvider();
    205       var instance = provider.GetDataDescriptors().Single(x => x.Name.StartsWith("Tower"));
    206       var symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
    207       symbRegProblem.Load(provider.LoadData(instance));
    208 
    209       symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 77;
    210       symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 77;
    211 
    212       var grammar = (TypeCoherentExpressionGrammar)symbRegProblem.SymbolicExpressionTreeGrammar;
    213 
    214       #endregion
    215       #region Algorithm Configuration
    216       alpsGa.Name = "ALPS Genetic Programming - Symbolic Regression";
    217       alpsGa.Description = "An ALPS-GP to solve a symbolic regression problem";
    218       alpsGa.Problem = symbRegProblem;
    219       SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<RandomSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(alpsGa,
    220         numberOfLayers: 1000,
    221         popSize: 200,
    222         mutationRate: 0.25,
    223         elites: 1,
    224         plusSelection: false,
    225         agingScheme: AgingScheme.Polynomial,
    226         ageGap: 15,
    227         ageInheritance: 1.0,
    228         maxGens: 10);
    229 
    230       alpsGa.SetSeedRandomly.Value = false;
    231       alpsGa.Seed.Value = 1111;
    232       #endregion
    233       return alpsGa;
    234     }
    235 
    236     //TODO: Add Function-Tests once the Branch of #2904 is done
     16    private static readonly double epsilon = 0.00001;
     17
     18    [TestMethod]
     19    [TestCategory("Problems.DataAnalysis")]
     20    [TestProperty("Time", "short")]
     21    public void ConstantModelVariableImpactTest() {
     22      IRegressionProblemData problemData = LoadDefaultTowerProblem();
     23      IRegressionModel model = new ConstantModel(5, "y");
     24      IRegressionSolution solution = new RegressionSolution(model, problemData);
     25      Dictionary<string, double> expectedImpacts = GetExpectedValuesForConstantModel();
     26
     27      CheckDefaultAsserts(solution, expectedImpacts);
     28    }
     29
     30    [TestMethod]
     31    [TestCategory("Problems.DataAnalysis")]
     32    [TestProperty("Time", "short")]
     33    public void LinearRegressionModelVariableImpactTowerTest() {
     34      IRegressionProblemData problemData = LoadDefaultTowerProblem();
     35      ISymbolicExpressionTree tree = CreateLRExpressionTree(problemData);
     36      IRegressionModel model = new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter());
     37      IRegressionSolution solution = new RegressionSolution(model, (IRegressionProblemData)problemData.Clone());
     38      Dictionary<string, double> expectedImpacts = GetExpectedValuesForLRTower();
     39
     40      CheckDefaultAsserts(solution, expectedImpacts);
     41    }
     42
     43    [TestMethod]
     44    [TestCategory("Problems.DataAnalysis")]
     45    [TestProperty("Time", "short")]
     46    public void LinearRegressionModelVariableImpactMibaTest() {
     47      IRegressionProblemData problemData = LoadDefaultMibaProblem();
     48      ISymbolicExpressionTree tree = CreateLRExpressionTree(problemData);
     49      IRegressionModel model = new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter());
     50      IRegressionSolution solution = new RegressionSolution(model, (IRegressionProblemData)problemData.Clone());
     51      Dictionary<string, double> expectedImpacts = GetExpectedValuesForLRMiba();
     52
     53      CheckDefaultAsserts(solution, expectedImpacts);
     54    }
     55
     56    [TestMethod]
     57    [TestCategory("Problems.DataAnalysis")]
     58    [TestProperty("Time", "short")]
     59    public void CustomModelVariableImpactTest() {
     60      IRegressionProblemData problemData = CreateDefaultProblem();
     61      ISymbolicExpressionTree tree = CreateCustomExpressionTree();
     62      IRegressionModel model = new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter());
     63      IRegressionSolution solution = new RegressionSolution(model, (IRegressionProblemData)problemData.Clone());
     64      Dictionary<string, double> expectedImpacts = GetExpectedValuesForCustomProblem();
     65
     66      CheckDefaultAsserts(solution, expectedImpacts);
     67    }
     68
     69    [TestMethod]
     70    [TestCategory("Problems.DataAnalysis")]
     71    [TestProperty("Time", "short")]
     72    public void CustomModelVariableImpactNoInfluenceTest() {
     73      IRegressionProblemData problemData = CreateDefaultProblem();
     74      ISymbolicExpressionTree tree = CreateCustomExpressionTreeNoInfluenceX1();
     75      IRegressionModel model = new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter());
     76      IRegressionSolution solution = new RegressionSolution(model, (IRegressionProblemData)problemData.Clone());
     77      Dictionary<string, double> expectedImpacts = GetExpectedValuesForCustomProblemNoInfluence();
     78
     79      CheckDefaultAsserts(solution, expectedImpacts);
     80    }
     81
     82    #region Load RegressionProblemData
     83    private IRegressionProblemData LoadDefaultTowerProblem() {
     84      RegressionRealWorldInstanceProvider provider = new RegressionRealWorldInstanceProvider();
     85      var instance = provider.GetDataDescriptors().Where(x => x.Name.Equals("Tower")).Single();
     86      return provider.LoadData(instance);
     87    }
     88    private IRegressionProblemData LoadDefaultMibaProblem() {
     89      MibaFrictionRegressionInstanceProvider provider = new MibaFrictionRegressionInstanceProvider();
     90      var instance = provider.GetDataDescriptors().Where(x => x.Name.Equals("CF1")).Single();
     91      return provider.LoadData(instance);
     92    }
     93    private IRegressionProblemData CreateDefaultProblem() {
     94      List<string> allowedInputVariables = new List<string>() { "x1", "x2", "x3", "x4", "x5" };
     95      string targetVariable = "y";
     96      var variableNames = allowedInputVariables.Union(targetVariable.ToEnumerable());
     97      double[,] variableValues = new double[100, variableNames.Count()];
     98
     99      FastRandom random = new FastRandom(12345);
     100      for (int i = 0; i < variableValues.GetLength(0); i++) {
     101        for (int j = 0; j < variableValues.GetLength(1); j++) {
     102          variableValues[i, j] = random.Next(1, 100);
     103        }
     104      }
     105
     106      Dataset dataset = new Dataset(variableNames, variableValues);
     107      return new RegressionProblemData(dataset, allowedInputVariables, targetVariable);
     108    }
     109    #endregion
     110
     111    #region Create SymbolicExpressionTree
     112    private ISymbolicExpressionTree CreateLRExpressionTree(IRegressionProblemData problemData) {
     113      IEnumerable<int> rows = problemData.TrainingIndices;
     114      var doubleVariables = problemData.AllowedInputVariables.Where(problemData.Dataset.VariableHasType<double>);
     115      var factorVariableNames = problemData.AllowedInputVariables.Where(problemData.Dataset.VariableHasType<string>);
     116      var factorVariables = problemData.Dataset.GetFactorVariableValues(factorVariableNames, rows);
     117      double[,] binaryMatrix = problemData.Dataset.ToArray(factorVariables, rows);
     118      double[,] doubleVarMatrix = problemData.Dataset.ToArray(doubleVariables.Concat(new string[] { problemData.TargetVariable }), rows);
     119      var inputMatrix = binaryMatrix.HorzCat(doubleVarMatrix);
     120
     121      alglib.linearmodel lm = new alglib.linearmodel();
     122      alglib.lrreport ar = new alglib.lrreport();
     123      int nRows = inputMatrix.GetLength(0);
     124      int nFeatures = inputMatrix.GetLength(1) - 1;
     125      double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant
     126
     127      int retVal = 1;
     128      alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
     129      if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression solution");
     130
     131      alglib.lrunpack(lm, out coefficients, out nFeatures);
     132
     133      int nFactorCoeff = binaryMatrix.GetLength(1);
     134      int nVarCoeff = doubleVariables.Count();
     135      return LinearModelToTreeConverter.CreateTree(factorVariables, coefficients.Take(nFactorCoeff).ToArray(),
     136        doubleVariables.ToArray(), coefficients.Skip(nFactorCoeff).Take(nVarCoeff).ToArray(),
     137        @const: coefficients[nFeatures]);
     138    }
     139    private ISymbolicExpressionTree CreateCustomExpressionTree() {
     140      return new InfixExpressionParser().Parse("x1*x2 - x2*x2 + x3*x3 + x4*x4 - x5*x5 + 14/12");
     141    }
     142    private ISymbolicExpressionTree CreateCustomExpressionTreeNoInfluenceX1() {
     143      return new InfixExpressionParser().Parse("x1/x1*x2 - x2*x2 + x3*x3 + x4*x4 - x5*x5 + 14/12");
     144    }
     145    #endregion
     146
     147    #region Get Expected Values
     148    private Dictionary<string, double> GetExpectedValuesForConstantModel() {
     149      Dictionary<string, double> expectedImpacts = new Dictionary<string, double>();
     150      expectedImpacts.Add("x1", 0);
     151      expectedImpacts.Add("x10", 0);
     152      expectedImpacts.Add("x11", 0);
     153      expectedImpacts.Add("x12", 0);
     154      expectedImpacts.Add("x13", 0);
     155      expectedImpacts.Add("x14", 0);
     156      expectedImpacts.Add("x15", 0);
     157      expectedImpacts.Add("x16", 0);
     158      expectedImpacts.Add("x17", 0);
     159      expectedImpacts.Add("x18", 0);
     160      expectedImpacts.Add("x19", 0);
     161      expectedImpacts.Add("x2", 0);
     162      expectedImpacts.Add("x20", 0);
     163      expectedImpacts.Add("x21", 0);
     164      expectedImpacts.Add("x22", 0);
     165      expectedImpacts.Add("x23", 0);
     166      expectedImpacts.Add("x24", 0);
     167      expectedImpacts.Add("x25", 0);
     168      expectedImpacts.Add("x3", 0);
     169      expectedImpacts.Add("x4", 0);
     170      expectedImpacts.Add("x5", 0);
     171      expectedImpacts.Add("x6", 0);
     172      expectedImpacts.Add("x7", 0);
     173      expectedImpacts.Add("x8", 0);
     174      expectedImpacts.Add("x9", 0);
     175
     176      return expectedImpacts;
     177    }
     178    private Dictionary<string, double> GetExpectedValuesForLRTower() {
     179      Dictionary<string, double> expectedImpacts = new Dictionary<string, double>();
     180      expectedImpacts.Add("x1", 0.639933657675427);
     181      expectedImpacts.Add("x10", 0.0127006885259798);
     182      expectedImpacts.Add("x11", 0.648236047877475);
     183      expectedImpacts.Add("x12", 0.248350173524562);
     184      expectedImpacts.Add("x13", 0.550889987109547);
     185      expectedImpacts.Add("x14", 0.0882824237877192);
     186      expectedImpacts.Add("x15", 0.0391276799061169);
     187      expectedImpacts.Add("x16", 0.743632451088798);
     188      expectedImpacts.Add("x17", 0.00254276857715308);
     189      expectedImpacts.Add("x18", 0.0021548147614302);
     190      expectedImpacts.Add("x19", 0.00513473927463037);
     191      expectedImpacts.Add("x2", 0.0107583487931443);
     192      expectedImpacts.Add("x20", 0.18085069746933);
     193      expectedImpacts.Add("x21", 0.138053600700762);
     194      expectedImpacts.Add("x22", 0.000339539790460086);
     195      expectedImpacts.Add("x23", 0.362111965467117);
     196      expectedImpacts.Add("x24", 0.0320167935572304);
     197      expectedImpacts.Add("x25", 0.57460423230969);
     198      expectedImpacts.Add("x3", 0.688142635515862);
     199      expectedImpacts.Add("x4", 0.000176632348454664);
     200      expectedImpacts.Add("x5", 0.0213915503114581);
     201      expectedImpacts.Add("x6", 0.807976486909701);
     202      expectedImpacts.Add("x7", 0.716217843319252);
     203      expectedImpacts.Add("x8", 0.772701841392564);
     204      expectedImpacts.Add("x9", 0.178418730050997);
     205
     206      return expectedImpacts;
     207    }
     208    private Dictionary<string, double> GetExpectedValuesForLRMiba() {
     209      Dictionary<string, double> expectedImpacts = new Dictionary<string, double>();
     210      expectedImpacts.Add("Grooving", 0.0380558091030508);
     211      expectedImpacts.Add("Material", 0.02195836766156);
     212      expectedImpacts.Add("Material_Cat", 0.000338687689067418);
     213      expectedImpacts.Add("Oil", 0.363464994447857);
     214      expectedImpacts.Add("x10", 0.0015309669014415);
     215      expectedImpacts.Add("x11", -3.60432578908609E-05);
     216      expectedImpacts.Add("x12", 0.00118953859087612);
     217      expectedImpacts.Add("x13", 0.00164240977191832);
     218      expectedImpacts.Add("x14", 0.000688363685380056);
     219      expectedImpacts.Add("x15", -4.75067203969948E-05);
     220      expectedImpacts.Add("x16", 0.00130388206125076);
     221      expectedImpacts.Add("x17", 0.132351838646134);
     222      expectedImpacts.Add("x2", -2.47981401556574E-05);
     223      expectedImpacts.Add("x20", 0.716541716605016);
     224      expectedImpacts.Add("x22", 0.174959377282835);
     225      expectedImpacts.Add("x3", -2.65979754026091E-05);
     226      expectedImpacts.Add("x4", -1.24764212947603E-05);
     227      expectedImpacts.Add("x5", 0.001184959455798);
     228      expectedImpacts.Add("x6", 0.000743336665237626);
     229      expectedImpacts.Add("x7", 0.00188965927889773);
     230      expectedImpacts.Add("x8", 0.00415201581536351);
     231      expectedImpacts.Add("x9", 0.00365653880518491);
     232
     233      return expectedImpacts;
     234    }
     235    private Dictionary<string, double> GetExpectedValuesForCustomProblem() {
     236      Dictionary<string, double> expectedImpacts = new Dictionary<string, double>();
     237      expectedImpacts.Add("x1", -0.000573340275115796);
     238      expectedImpacts.Add("x2", 0.000781819784095592);
     239      expectedImpacts.Add("x3", -0.000390473234921058);
     240      expectedImpacts.Add("x4", -0.00116083274627995);
     241      expectedImpacts.Add("x5", -0.00036161186207545);
     242
     243      return expectedImpacts;
     244    }
     245    private Dictionary<string, double> GetExpectedValuesForCustomProblemNoInfluence() {
     246      Dictionary<string, double> expectedImpacts = new Dictionary<string, double>();
     247      expectedImpacts.Add("x1", 0);
     248      expectedImpacts.Add("x2", 0.00263393690342982);
     249      expectedImpacts.Add("x3", -0.00053248037514929);
     250      expectedImpacts.Add("x4", 0.00450365819257568);
     251      expectedImpacts.Add("x5", -0.000550911612888904);
     252
     253      return expectedImpacts;
     254    }
     255    #endregion
     256
     257    private void CheckDefaultAsserts(IRegressionSolution solution, Dictionary<string, double> expectedImpacts) {
     258      IRegressionProblemData problemData = solution.ProblemData;
     259      IEnumerable<double> estimatedValues = solution.GetEstimatedValues(solution.ProblemData.TrainingIndices);
     260
     261      var solutionImpacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(solution);
     262      var modelImpacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(solution.Model, problemData, estimatedValues, problemData.TrainingIndices);
     263      //Both ways should return equal results
     264      Assert.IsTrue(solutionImpacts.SequenceEqual(modelImpacts));
     265
     266      //Check if impacts are as expected
     267      Assert.AreEqual(modelImpacts.Count(), expectedImpacts.Count);
     268      foreach (var entry in modelImpacts.OrderBy(v => v.Item1)) {
     269        Assert.IsTrue(Math.Abs(expectedImpacts[entry.Item1] - entry.Item2) < epsilon);
     270      }
     271    }
    237272  }
    238273}
Note: See TracChangeset for help on using the changeset viewer.