Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12139 for trunk/sources


Ignore:
Timestamp:
03/05/15 14:17:33 (9 years ago)
Author:
ascheibe
Message:

#2351

  • renamed NormalDistribution class
  • added Silverman's rule of thumb for bandwidth estimation
  • made bandwidth configurable in UI
Location:
trunk/sources
Files:
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Analysis.Views/3.3/HistogramControl.Designer.cs

    r12012 r12139  
    4848    private void InitializeComponent() {
    4949      this.components = new System.ComponentModel.Container();
    50       System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    51       System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    52       System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
     50      System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
     51      System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
     52      System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
    5353      this.chart = new HeuristicLab.Visualization.ChartControlsExtensions.EnhancedChart();
    5454      this.binsNumericUpDown = new System.Windows.Forms.NumericUpDown();
    5555      this.label1 = new System.Windows.Forms.Label();
    5656      this.exactCheckBox = new System.Windows.Forms.CheckBox();
     57      this.label2 = new System.Windows.Forms.Label();
     58      this.BandwidthNumericUpDown = new System.Windows.Forms.NumericUpDown();
    5759      ((System.ComponentModel.ISupportInitialize)(this.chart)).BeginInit();
    5860      ((System.ComponentModel.ISupportInitialize)(this.binsNumericUpDown)).BeginInit();
     61      ((System.ComponentModel.ISupportInitialize)(this.BandwidthNumericUpDown)).BeginInit();
    5962      this.SuspendLayout();
    6063      //
    6164      // chart
    6265      //
    63       this.chart.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    64             | System.Windows.Forms.AnchorStyles.Left)
     66      this.chart.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
     67            | System.Windows.Forms.AnchorStyles.Left) 
    6568            | System.Windows.Forms.AnchorStyles.Right)));
    66       chartArea1.Name = "ChartArea1";
    67       this.chart.ChartAreas.Add(chartArea1);
    68       legend1.Alignment = System.Drawing.StringAlignment.Center;
    69       legend1.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Top;
    70       legend1.Name = "Default";
    71       this.chart.Legends.Add(legend1);
     69      chartArea2.Name = "ChartArea1";
     70      this.chart.ChartAreas.Add(chartArea2);
     71      legend2.Alignment = System.Drawing.StringAlignment.Center;
     72      legend2.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Top;
     73      legend2.Name = "Default";
     74      this.chart.Legends.Add(legend2);
    7275      this.chart.Location = new System.Drawing.Point(0, 27);
    7376      this.chart.Name = "chart";
    74       series1.ChartArea = "ChartArea1";
    75       series1.Legend = "Default";
    76       series1.Name = "Series1";
    77       this.chart.Series.Add(series1);
     77      series2.ChartArea = "ChartArea1";
     78      series2.Legend = "Default";
     79      series2.Name = "Series1";
     80      this.chart.Series.Add(series2);
    7881      this.chart.Size = new System.Drawing.Size(465, 336);
    7982      this.chart.TabIndex = 0;
     
    8891            0});
    8992      this.binsNumericUpDown.Minimum = new decimal(new int[] {
    90             1,
     93            2,
    9194            0,
    9295            0,
     
    115118      this.exactCheckBox.AutoSize = true;
    116119      this.exactCheckBox.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
    117       this.exactCheckBox.Location = new System.Drawing.Point(158, 4);
     120      this.exactCheckBox.Location = new System.Drawing.Point(291, 4);
    118121      this.exactCheckBox.Name = "exactCheckBox";
    119122      this.exactCheckBox.Size = new System.Drawing.Size(56, 17);
     
    123126      this.exactCheckBox.CheckedChanged += new System.EventHandler(this.exactCheckBox_CheckedChanged);
    124127      //
     128      // label2
     129      //
     130      this.label2.AutoSize = true;
     131      this.label2.Location = new System.Drawing.Point(158, 5);
     132      this.label2.Name = "label2";
     133      this.label2.Size = new System.Drawing.Size(60, 13);
     134      this.label2.TabIndex = 5;
     135      this.label2.Text = "Bandwidth:";
     136      //
     137      // BandwidthNumericUpDown
     138      //
     139      this.BandwidthNumericUpDown.DecimalPlaces = 2;
     140      this.BandwidthNumericUpDown.Location = new System.Drawing.Point(224, 3);
     141      this.BandwidthNumericUpDown.Maximum = new decimal(new int[] {
     142            100000000,
     143            0,
     144            0,
     145            0});
     146      this.BandwidthNumericUpDown.Minimum = new decimal(new int[] {
     147            1,
     148            0,
     149            0,
     150            65536});
     151      this.BandwidthNumericUpDown.Name = "BandwidthNumericUpDown";
     152      this.BandwidthNumericUpDown.Size = new System.Drawing.Size(61, 20);
     153      this.BandwidthNumericUpDown.TabIndex = 4;
     154      this.BandwidthNumericUpDown.Value = new decimal(new int[] {
     155            1,
     156            0,
     157            0,
     158            0});
     159      this.BandwidthNumericUpDown.ValueChanged += new System.EventHandler(this.BandwidthNumericUpDown_ValueChanged);
     160      //
    125161      // HistogramControl
    126162      //
    127163      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
     164      this.Controls.Add(this.label2);
     165      this.Controls.Add(this.BandwidthNumericUpDown);
    128166      this.Controls.Add(this.exactCheckBox);
    129167      this.Controls.Add(this.label1);
     
    134172      ((System.ComponentModel.ISupportInitialize)(this.chart)).EndInit();
    135173      ((System.ComponentModel.ISupportInitialize)(this.binsNumericUpDown)).EndInit();
     174      ((System.ComponentModel.ISupportInitialize)(this.BandwidthNumericUpDown)).EndInit();
    136175      this.ResumeLayout(false);
    137176      this.PerformLayout();
     
    145184    private System.Windows.Forms.Label label1;
    146185    private System.Windows.Forms.CheckBox exactCheckBox;
     186    private System.Windows.Forms.Label label2;
     187    private System.Windows.Forms.NumericUpDown BandwidthNumericUpDown;
    147188  }
    148189}
  • trunk/sources/HeuristicLab.Analysis.Views/3.3/HistogramControl.cs

    r12134 r12139  
    3333
    3434    protected Dictionary<string, List<double>> points;
     35    private bool suppressUpdate;
     36
    3537    public int NumberOfBins {
    3638      get { return (int)binsNumericUpDown.Value; }
     
    123125    }
    124126
    125     protected void UpdateHistogram() {
     127    protected void UpdateHistogram(double bandwith = double.NaN) {
    126128      if (InvokeRequired) {
    127         Invoke((Action)UpdateHistogram, null);
     129        Invoke((Action<double>)UpdateHistogram, bandwith);
    128130        return;
    129131      }
     
    164166        histogramSeries["PointWidth"] = "1";
    165167
    166         CalculateDensity(histogramSeries, point.Value);
     168        CalculateDensity(histogramSeries, point.Value, bandwith);
    167169
    168170        overallMax = Math.Max(overallMax, maxValue);
     
    184186    }
    185187
    186     protected void CalculateDensity(Series series, List<double> row) {
     188    protected void CalculateDensity(Series series, List<double> row, double bandwidth = double.NaN) {
    187189      string densitySeriesName = "Density " + series.Name;
    188190      double stepWidth = series.Points[1].XValue - series.Points[0].XValue;
     191      var rowArray = row.ToArray();
    189192
    190193      if (chart.Series.Any(x => x.Name == densitySeriesName)) {
     
    193196      }
    194197
    195       var density = NormalDistribution.Density(row.ToArray(), row.Count, stepWidth);
     198      if (double.IsNaN(bandwidth)) {
     199        bandwidth = KernelDensityEstimator.EstimateBandwidth(rowArray);
     200        suppressUpdate = true;
     201        BandwidthNumericUpDown.Value = (decimal)bandwidth;
     202      }
     203      var density = KernelDensityEstimator.Density(rowArray, rowArray.Length, stepWidth, bandwidth);
    196204
    197205      Series newSeries = new Series(densitySeriesName);
     
    225233      UpdateHistogram();
    226234    }
     235
     236    private void BandwidthNumericUpDown_ValueChanged(object sender, EventArgs e) {
     237      if (!suppressUpdate) {
     238        UpdateHistogram(decimal.ToDouble(BandwidthNumericUpDown.Value));
     239      }
     240      suppressUpdate = false;
     241    }
    227242  }
    228243}
  • trunk/sources/HeuristicLab.Analysis/3.3/HeuristicLab.Analysis-3.3.csproj

    r12069 r12139  
    167167    <Compile Include="Statistics\Fitting\LinearLeastSquaresFitting.cs" />
    168168    <Compile Include="Statistics\Fitting\LogFitting.cs" />
    169     <Compile Include="Statistics\NormalDistribution.cs" />
     169    <Compile Include="Statistics\KernelDensityEstimator.cs" />
    170170    <Compile Include="Statistics\PairwiseTest.cs" />
    171171    <Compile Include="Statistics\SampleSizeDetermination.cs" />
  • trunk/sources/HeuristicLab.Analysis/3.3/Statistics/KernelDensityEstimator.cs

    r12135 r12139  
    2626
    2727namespace HeuristicLab.Analysis.Statistics {
    28   public static class NormalDistribution {
     28  public static class KernelDensityEstimator {
    2929    public static double[] Density(double[] x, double mean, double stdDev) {
    3030      return x.Select(xi => Density(xi, mean, stdDev)).ToArray();
     
    3636                             (2.0 * Math.Pow(stdDev, 2.0))));
    3737    }
    38 
    3938
    4039    // the scale (sigma) of the kernel is a parameter
     
    6261      return newX.Zip(y, Tuple.Create).ToList();
    6362    }
     63
     64    //Silverman's rule of thumb for bandwidth estimation (sigma)
     65    public static double EstimateBandwidth(double[] x) {
     66      return 1.06 * x.StandardDeviation() * Math.Pow(x.Length, -0.2);
     67    }
    6468  }
    6569}
Note: See TracChangeset for help on using the changeset viewer.