Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/05/11 21:55:55 (13 years ago)
Author:
abeham
Message:

#1614

  • updated branch from trunk
Location:
branches/GeneralizedQAP
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP

  • branches/GeneralizedQAP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4

    • Property svn:ignore
      •  

        old new  
        44HeuristicLabProblemsDataAnalysisSymbolicViewsPlugin.cs
        55*.vs10x
         6Plugin.cs
  • branches/GeneralizedQAP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/RunCollectionVariableImpactView.cs

    r5975 r6878  
    3535  public sealed partial class RunCollectionVariableImpactView : AsynchronousContentView {
    3636    private const string variableImpactResultName = "Variable impacts";
     37    private const string crossValidationFoldsResultName = "CrossValidation Folds";
     38    private const string numberOfFoldsParameterName = "Folds";
    3739    public RunCollectionVariableImpactView() {
    3840      InitializeComponent();
     
    9597    }
    9698
     99    private void comboBox_SelectedValueChanged(object sender, EventArgs e) {
     100      if (comboBox.SelectedItem != null) {
     101        var cvRuns = from r in Content
     102                     where r.Visible
     103                     where r.Parameters.ContainsKey(numberOfFoldsParameterName)
     104                     select r;
     105        if (comboBox.SelectedIndex == 0) {
     106          var selectedFolds = cvRuns
     107            .SelectMany(r => (RunCollection)r.Results[crossValidationFoldsResultName])
     108            .Where(r => r.Results.ContainsKey(variableImpactResultName));
     109          matrixView.Content = CalculateVariableImpactMatrix(selectedFolds.ToArray());
     110        } else {
     111          var selectedFolds = from r in cvRuns
     112                              let foldCollection = (RunCollection)r.Results[crossValidationFoldsResultName]
     113                              let run = foldCollection.ElementAt(comboBox.SelectedIndex - 1)
     114                              where run.Results.ContainsKey(variableImpactResultName)
     115                              select new { run, r.Name };
     116          matrixView.Content = CalculateVariableImpactMatrix(selectedFolds.Select(x => x.run).ToArray(), selectedFolds.Select(x => x.Name).ToArray());
     117        }
     118      }
     119    }
     120
     121
    97122    private void UpdateData() {
    98       matrixView.Content = CalculateVariableImpactMatrix();
    99     }
    100 
    101     private DoubleMatrix CalculateVariableImpactMatrix() {
     123      if (Content != null) {
     124        comboBox.Items.Clear();
     125        comboBox.Enabled = false;
     126        var visibleRuns = Content.Where(r => r.Visible).ToArray();
     127        var representativeCvRun =
     128          visibleRuns.Where(r => r.Parameters.ContainsKey(numberOfFoldsParameterName)).FirstOrDefault();
     129        if (representativeCvRun != null) {
     130          // make sure all runs have the same number of folds
     131          int nFolds = ((IntValue)representativeCvRun.Parameters[numberOfFoldsParameterName]).Value;
     132          var cvRuns = visibleRuns.Where(r => r.Parameters.ContainsKey(numberOfFoldsParameterName));
     133          if (cvRuns.All(r => ((IntValue)r.Parameters[numberOfFoldsParameterName]).Value == nFolds)) {
     134            // populate combobox
     135            comboBox.Items.Add("Overall");
     136            for (int foldIndex = 0; foldIndex < nFolds; foldIndex++) {
     137              comboBox.Items.Add("Fold " + foldIndex);
     138            }
     139            comboBox.SelectedIndex = 0;
     140            comboBox.Enabled = true;
     141          } else {
     142            matrixView.Content = null;
     143          }
     144        } else {
     145          var runsWithVariables = visibleRuns.Where(r => r.Results.ContainsKey(variableImpactResultName)).ToArray();
     146          matrixView.Content = CalculateVariableImpactMatrix(runsWithVariables);
     147        }
     148      }
     149    }
     150
     151    private IStringConvertibleMatrix CalculateVariableImpactMatrix(IRun[] runs) {
     152      return CalculateVariableImpactMatrix(runs, runs.Select(r => r.Name).ToArray());
     153    }
     154
     155    private DoubleMatrix CalculateVariableImpactMatrix(IRun[] runs, string[] runNames) {
    102156      DoubleMatrix matrix = null;
    103       if (Content != null) {
    104         List<IRun> runsWithVariables = Content.Where(r => r.Visible && r.Results.ContainsKey(variableImpactResultName)).ToList();
    105         IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runsWithVariables
    106                                                         select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
    107         IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts
    108                                              from variableName in variableImpact.RowNames
    109                                              select variableName)
    110                                             .Distinct();
    111         // filter variableNames: only include names that have at least one non-zero value in a run
    112         List<string> variableNamesList = (from variableName in variableNames
    113                                           where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
    114                                           select variableName)
    115                                          .ToList();
    116 
    117         List<string> statictics = new List<string> { "Median Rank", "Mean", "StdDev", "pValue" };
    118         List<string> columnNames = runsWithVariables.Select(r => r.Name).ToList();
    119         columnNames.AddRange(statictics);
    120         int runs = runsWithVariables.Count();
    121 
    122         matrix = new DoubleMatrix(variableNamesList.Count, runs + statictics.Count);
    123         matrix.SortableView = true;
    124         matrix.RowNames = variableNamesList;
    125         matrix.ColumnNames = columnNames;
    126 
    127         for (int i = 0; i < runsWithVariables.Count; i++) {
    128           IRun run = runsWithVariables[i];
    129           DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
    130           for (int j = 0; j < runVariableImpacts.Rows; j++) {
    131             int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
    132             if (rowIndex > -1) {
    133               matrix[rowIndex, i] = runVariableImpacts[j, 0];
    134             }
    135           }
    136         }
    137 
    138         List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList
    139                                                       select GetVariableImpacts(variableName, allVariableImpacts).ToList())
    140                                                      .ToList();
    141         List<List<double>> variableRanks = (from variableName in variableNamesList
    142                                             select GetVariableImpactRanks(variableName, allVariableImpacts).ToList())
    143                                         .ToList();
    144         if (variableImpactsOverRuns.Count() > 0) {
    145           // the variable with the worst median impact value is chosen as the reference variable
    146           // this is problematic if all variables are relevant, however works often in practice
    147           List<double> referenceImpacts = (from impacts in variableImpactsOverRuns
    148                                            let avg = impacts.Median()
    149                                            orderby avg
    150                                            select impacts)
    151                                            .First();
    152           // for all variables
    153           for (int row = 0; row < variableImpactsOverRuns.Count; row++) {
    154             // median rank
    155             matrix[row, runs] = variableRanks[row].Median();
    156             // also show mean and std.dev. of relative variable impacts to indicate the relative difference in impacts of variables
    157             matrix[row, runs + 1] = variableImpactsOverRuns[row].Average();
    158             matrix[row, runs + 2] = variableImpactsOverRuns[row].StandardDeviation();
    159 
    160             double leftTail = 0; double rightTail = 0; double bothTails = 0;
    161             // calc differences of impacts for current variable and reference variable
    162             double[] z = new double[referenceImpacts.Count];
    163             for (int i = 0; i < z.Length; i++) {
    164               z[i] = variableImpactsOverRuns[row][i] - referenceImpacts[i];
    165             }
    166             // wilcoxon signed rank test is used because the impact values of two variables in a single run are not independent
    167             alglib.wsr.wilcoxonsignedranktest(z, z.Length, 0, ref bothTails, ref leftTail, ref rightTail);
    168             matrix[row, runs + 3] = bothTails;
    169           }
    170         }
    171       }
    172       return matrix;
     157      IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs
     158                                                      select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
     159      IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts
     160                                           from variableName in variableImpact.RowNames
     161                                           select variableName)
     162                                          .Distinct();
     163      // filter variableNames: only include names that have at least one non-zero value in a run
     164      List<string> variableNamesList = (from variableName in variableNames
     165                                        where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
     166                                        select variableName)
     167                                       .ToList();
     168
     169      List<string> statictics = new List<string> { "Median Rank", "Mean", "StdDev", "pValue" };
     170      List<string> columnNames = new List<string>(runNames);
     171      columnNames.AddRange(statictics);
     172      int numberOfRuns = runs.Length;
     173
     174      matrix = new DoubleMatrix(variableNamesList.Count, numberOfRuns + statictics.Count);
     175      matrix.SortableView = true;
     176      matrix.ColumnNames = columnNames;
     177
     178      // calculate statistics
     179      List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList
     180                                                    select GetVariableImpacts(variableName, allVariableImpacts).ToList())
     181                                             .ToList();
     182      List<List<double>> variableRanks = (from variableName in variableNamesList
     183                                          select GetVariableImpactRanks(variableName, allVariableImpacts).ToList())
     184                                      .ToList();
     185      if (variableImpactsOverRuns.Count() > 0) {
     186        // the variable with the worst median impact value is chosen as the reference variable
     187        // this is problematic if all variables are relevant, however works often in practice
     188        List<double> referenceImpacts = (from impacts in variableImpactsOverRuns
     189                                         let avg = impacts.Median()
     190                                         orderby avg
     191                                         select impacts)
     192                                         .First();
     193        // for all variables
     194        for (int row = 0; row < variableImpactsOverRuns.Count; row++) {
     195          // median rank
     196          matrix[row, numberOfRuns] = variableRanks[row].Median();
     197          // also show mean and std.dev. of relative variable impacts to indicate the relative difference in impacts of variables
     198          matrix[row, numberOfRuns + 1] = Math.Round(variableImpactsOverRuns[row].Average(), 3);
     199          matrix[row, numberOfRuns + 2] = Math.Round(variableImpactsOverRuns[row].StandardDeviation(), 3);
     200
     201          double leftTail = 0; double rightTail = 0; double bothTails = 0;
     202          // calc differences of impacts for current variable and reference variable
     203          double[] z = new double[referenceImpacts.Count];
     204          for (int i = 0; i < z.Length; i++) {
     205            z[i] = variableImpactsOverRuns[row][i] - referenceImpacts[i];
     206          }
     207          // wilcoxon signed rank test is used because the impact values of two variables in a single run are not independent
     208          alglib.wsr.wilcoxonsignedranktest(z, z.Length, 0, ref bothTails, ref leftTail, ref rightTail);
     209          matrix[row, numberOfRuns + 3] = Math.Round(bothTails, 4);
     210        }
     211      }
     212
     213      // fill matrix with impacts from runs
     214      for (int i = 0; i < runs.Length; i++) {
     215        IRun run = runs[i];
     216        DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
     217        for (int j = 0; j < runVariableImpacts.Rows; j++) {
     218          int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
     219          if (rowIndex > -1) {
     220            matrix[rowIndex, i] = Math.Round(runVariableImpacts[j, 0], 3);
     221          }
     222        }
     223      }
     224      // sort by median
     225      var sortedMatrix = (DoubleMatrix)matrix.Clone();
     226      var sortedIndexes = from i in Enumerable.Range(0, sortedMatrix.Rows)
     227                          orderby matrix[i, numberOfRuns]
     228                          select i;
     229
     230      int targetIndex = 0;
     231      foreach (var sourceIndex in sortedIndexes) {
     232        for (int c = 0; c < matrix.Columns; c++)
     233          sortedMatrix[targetIndex, c] = matrix[sourceIndex, c];
     234        targetIndex++;
     235      }
     236      sortedMatrix.RowNames = sortedIndexes.Select(i => variableNamesList[i]);
     237
     238      return sortedMatrix;
    173239    }
    174240
     
    213279      }
    214280    }
     281
    215282  }
    216283}
Note: See TracChangeset for help on using the changeset viewer.