4

I have a Histogram statistics bar chart with below data.

Count, HistogramBin
    0,    -1615.25
    0,    -1056.42
    0,     -497.48
    1,       61.25
    1,      620.05
    1,     1178.92
    0,     1737.76
    0,     2296.59

I need to form Gauss curve based on above values. Could anyone guide me how to achieve the same.

I have written a function based on Wikipedia link: https://en.wikipedia.org/wiki/Gaussian_function

Our average is : 340.67 SD: Standard deviation: 488.98001098632812

 private DataTable GenerateGaussTable1(DataTable histogramDataTable, 
                                       HistogramValueItem histogramValueDBItem)
    {
      double amplitude = (Average + 3 * Sigma) / 2;
      double mean = Average;
      double sd = Sigma;
      DataTable dt = new DataTable();
      dt.Columns.Add("x", typeof(float));
      dt.Columns.Add("Y", typeof(float));
      foreach (DataRow row in histogramDataTable.Rows)// top provided data
      {
        double x = Convert.ToDouble(row[1]) / 2;
        double var1 = 1 / sd * Math.Sqrt(2 * 3.14);
        double var2 = -0.5 *  Math.Pow((x - mean)/sd, 2);

        double var4= Math.Exp(var2);
        double var5 = var1 * var4;
        // Y = Amplitude * exp(-0.5 * ((X - Mean) / SD) ^ 2)
        double y = var5;
        dt.Rows.Add((float)x, (float)y);
      }
      return dt;
    }
JohnMetta
  • 18,782
  • 5
  • 31
  • 57
Sakti Behera
  • 97
  • 3
  • 9
  • 3
    did you look at any libraries for drawing plots? – Michał Żołnieruk Apr 05 '18 at 14:04
  • What are you targetting: Winforms, WPF, ASP..? __Always__ TAG your question correctly! - Winfroms: You can feed the data into a MSChart with a Series.ChartType.Curve or Line.. – TaW Apr 05 '18 at 14:45
  • Download the msdn sample charts. There are lots of examples and c# code : https://code.msdn.microsoft.com/Samples-Environments-for-b01e9c61 – jdweng Apr 05 '18 at 15:04
  • See [here](https://stackoverflow.com/questions/18744092/chart-databinding-to-datatable-chart-not-updating) for a simple example of binding the data table to the chart! – TaW Apr 05 '18 at 15:06
  • @TaW: I am targeting Asp.net and i am using devexpress charts for generating the charts. I don't have problem in binding the data to chart. I have problem in creating a data series for Gaussian curve. The code sample that i have mentioned above is correct or not to generate a series for Gaussian curve. – Sakti Behera Apr 06 '18 at 04:16
  • @Michał Żołnieruk: I have problem in creating a data series for Gaussian curve. I am using devexpress bootstrap chart to plot the graphs. – Sakti Behera Apr 06 '18 at 04:18

2 Answers2

4

enter image description here

Here is my code:

double gauss(double x, double a, double b, double c)
{
    var v1 = ( x - b) / (2d * c * c);
    var v2 = -v1 * v1 / 2d;
    var v3 = a * Math.Exp(v2);

    return v3;
}

and:

private void button_Click(object sender, EventArgs e)
{
    Series s1 = chart2.Series[0];
    s1.ChartType = SeriesChartType.Line;
    s1.Name = "Line";
    Series s2 = chart2.Series.Add("Spline");
    s2.ChartType = SeriesChartType.Spline;

    double avg = 1.8;
    double amp = 3;
    double sd = 0.53;

    List<double> xes = new List<double> 
       { 0, 0, 0.05, 0.1, 0.4, 0.9, 1.3, 1.6, 2, 2.4, 2.8, 3.2, 4 };

    foreach (var x in xes)
    {
        s1.Points.AddXY(x, gauss(x, amp, avg, sd));
        s2.Points.AddXY(x, gauss(x, amp, avg, sd));
    }
}

The math was taken from wikipedia

I think your SD is way too large to create a bell curve; try dividing by 10-100..! - Of course your SD actually is very large and so you really won't get a meaningful bell curve for those data..

TaW
  • 53,122
  • 8
  • 69
  • 111
2

I've tried your function, but it gives wrong curves, The gauss function is wrong, why do you use "2d"?

Here the function :

enter image description here

so first, v1 = (x-b). Then v2 = (x-b)² / 2 c² And finaly v3 = a exp (v2)

 double gauss(double x, double a, double b, double c)
        {
            var v1 = (x - b);
            var v2 = (v1 * v1) / (2 * (c*c));
            var v3 = a * Math.Exp(-v2);
            return v3;
        }

After this fix, the curves are much better.