- Timestamp:
- 07/02/12 09:31:33 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionResidualHistogram.cs
r8104 r8173 42 42 /// </summary> 43 43 protected const double bins = 25; 44 /// <summary>45 /// keeps for all series a list for every bin with the position of the bin, the relative frequency of the46 /// residuals and the beginning and the end of the interval of the bin47 /// </summary>48 protected readonly Dictionary<string, List<List<double>>> relativeFrequencies;49 44 #endregion 50 45 … … 57 52 : base() { 58 53 InitializeComponent(); 59 relativeFrequencies = new Dictionary<string, List<List<double>>>();60 54 foreach (string series in new List<String>() { ALL_SAMPLES, TRAINING_SAMPLES, TEST_SAMPLES }) { 61 55 chart.Series.Add(series); … … 67 61 chart.Series[series].BorderColor = Color.Black; 68 62 chart.Series[series].ToolTip = series + " Y = #VALY from #CUSTOMPROPERTY(from) to #CUSTOMPROPERTY(to)"; 69 relativeFrequencies[series] = new List<List<double>>();70 63 } 71 64 //configure axis … … 85 78 foreach (Series series in chart.Series) { 86 79 series.Points.Clear(); 87 relativeFrequencies[series.Name].Clear();88 80 } 89 81 if (Content != null) { 90 List<double> residuals = CalculateResiduals(); 91 double realMax = Math.Max(Math.Abs(residuals.Min()), Math.Abs(residuals.Max())); 92 double roundedMax = HumanRoundMax(realMax); 93 double intervalWidth = (roundedMax * 2.0) / bins; 94 intervalWidth = HumanRoundMax(intervalWidth); 95 // sets roundedMax to a value, so that zero will be in the middle of the x axis 96 double help = realMax / intervalWidth; 97 help = help % 1 < 0.5 ? (int)help : (int)help + 1; 98 roundedMax = help * intervalWidth; 99 82 List<double> residuals = CalculateResiduals(Content); 83 84 double max = 0.0; 100 85 foreach (Series series in chart.Series) { 101 CalculateFrequencies(residuals, series .Name, roundedMax, intervalWidth);102 if (!series.Equals(ALL_SAMPLES))103 ShowValues(series);86 CalculateFrequencies(residuals, series); 87 double seriesMax = series.Points.Select(p => p.YValues.First()).Max(); 88 max = max < seriesMax ? seriesMax : max; 104 89 } 90 91 // ALL_SAMPLES has to be calculated to know its highest frequency, but it is not shown in the beginning 92 chart.Series.Where(s => s.Name.Equals(ALL_SAMPLES)).First().Points.Clear(); 93 94 double roundedMax, intervalWidth; 95 CalculateResidualParameters(residuals, out roundedMax, out intervalWidth); 105 96 106 97 ChartArea chartArea = chart.ChartAreas[0]; … … 108 99 chartArea.AxisX.Maximum = roundedMax + intervalWidth; 109 100 // get the highest frequency of a residual of any series 110 chartArea.AxisY.Maximum = (from series in relativeFrequencies.Values 111 select (from residual in series 112 select residual.ElementAt(1)).Max()).Max(); 101 chartArea.AxisY.Maximum = max; 113 102 if (chartArea.AxisY.Maximum < 0.1) { 114 103 chartArea.AxisY.Interval = 0.01; … … 128 117 } 129 118 130 private List<double> CalculateResiduals( ) {119 private List<double> CalculateResiduals(IRegressionSolution solution) { 131 120 List<double> residuals = new List<double>(); 132 121 133 IRegressionProblemData problemdata = Content.ProblemData;122 IRegressionProblemData problemdata = solution.ProblemData; 134 123 List<double> targetValues = problemdata.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToList(); 135 List<double> estimatedValues = Content.EstimatedValues.ToList();136 137 for (int i = 0; i < Content.ProblemData.Dataset.Rows; i++) {124 List<double> estimatedValues = solution.EstimatedValues.ToList(); 125 126 for (int i = 0; i < solution.ProblemData.Dataset.Rows; i++) { 138 127 double residual = estimatedValues[i] - targetValues[i]; 139 128 residuals.Add(residual); … … 142 131 } 143 132 144 private void CalculateFrequencies(List<double> residualValues, string series, double max, double intervalWidth) { 133 private void CalculateFrequencies(List<double> residualValues, Series series) { 134 double roundedMax, intervalWidth; 135 CalculateResidualParameters(residualValues, out roundedMax, out intervalWidth); 136 145 137 IEnumerable<double> relevantResiduals = residualValues; 146 138 IRegressionProblemData problemdata = Content.ProblemData; 147 if (series. Equals(TRAINING_SAMPLES)) {139 if (series.Name.Equals(TRAINING_SAMPLES)) { 148 140 relevantResiduals = residualValues.Skip(problemdata.TrainingPartition.Start).Take(problemdata.TrainingPartition.Size); 149 } else if (series. Equals(TEST_SAMPLES)) {141 } else if (series.Name.Equals(TEST_SAMPLES)) { 150 142 relevantResiduals = residualValues.Skip(problemdata.TestPartition.Start).Take(problemdata.TestPartition.Size); 151 143 } … … 153 145 double intervalCenter = intervalWidth / 2.0; 154 146 double sampleCount = relevantResiduals.Count(); 155 double current = -max; 147 double current = -roundedMax; 148 DataPointCollection seriesPoints = series.Points; 156 149 157 150 for (int i = 0; i <= bins; i++) { 158 151 IEnumerable<double> help = relevantResiduals.Where(x => x >= (current - intervalCenter) && x < (current + intervalCenter)); 159 relativeFrequencies[series].Add(new List<double>() { current, help.Count() / sampleCount, current - intervalCenter, current + intervalCenter }); 152 seriesPoints.AddXY(current, help.Count() / sampleCount); 153 seriesPoints[seriesPoints.Count - 1]["from"] = (current - intervalCenter).ToString(); 154 seriesPoints[seriesPoints.Count - 1]["to"] = (current + intervalCenter).ToString(); 160 155 current += intervalWidth; 161 }162 }163 164 private void ShowValues(Series series) {165 List<List<double>> relativeSeriesFrequencies = relativeFrequencies[series.Name];166 DataPointCollection seriesPoints = series.Points;167 168 foreach (var valueList in relativeSeriesFrequencies) {169 seriesPoints.AddXY(valueList[0], valueList[1]);170 seriesPoints[seriesPoints.Count - 1]["from"] = valueList[2].ToString();171 seriesPoints[seriesPoints.Count - 1]["to"] = valueList[3].ToString();172 156 } 173 157 } … … 179 163 } 180 164 } else if (Content != null) { 181 ShowValues(series); 165 List<double> residuals = CalculateResiduals(Content); 166 CalculateFrequencies(residuals, series); 182 167 chart.Legends[series.Legend].ForeColor = Color.Black; 183 168 chart.Refresh(); … … 185 170 } 186 171 187 private double HumanRoundMax(double max) { 172 private static void CalculateResidualParameters(List<double> residuals, out double roundedMax, out double intervalWidth) { 173 double realMax = Math.Max(Math.Abs(residuals.Min()), Math.Abs(residuals.Max())); 174 roundedMax = HumanRoundMax(realMax); 175 intervalWidth = (roundedMax * 2.0) / bins; 176 intervalWidth = HumanRoundMax(intervalWidth); 177 // sets roundedMax to a value, so that zero will be in the middle of the x axis 178 double help = realMax / intervalWidth; 179 help = help % 1 < 0.5 ? (int)help : (int)help + 1; 180 roundedMax = help * intervalWidth; 181 } 182 183 private static double HumanRoundMax(double max) { 188 184 double base10; 189 185 if (max > 0) base10 = Math.Pow(10.0, Math.Floor(Math.Log10(max)));
Note: See TracChangeset
for help on using the changeset viewer.