Normal Distribution (Bell Curve) in C# - Sajha Mobile
SAJHA MOBILE
Normal Distribution (Bell Curve) in C#
Posts 12 · Viewed 31362 · Go to Last Post
dhoti_prasad
· Snapshot
Like · Likedby · 0

Hi Gurus,


Have you ever coded Normal Distribution ( Bell Curve) in .NET? I need your help please.

SaagRaSisnu
· Snapshot
Like · Liked by · 0
Dhoti_Prasad = Dhoti Ko Prasad.
Lol...
dhoti_prasad
· Snapshot
Like · Liked by · 0

साग सिस्नो जी,


Your level does not match for this thread.

SaagRaSisnu
· Snapshot
Like · Liked by · 0
What do you know about my Level?
AlterEgo
· Snapshot
Like · Liked by · 0
La Dhoti,

Talai gaussian distribution ko class nai banai de ko cha.Download gar ani moj gar.

http://www.c-sharpcorner.com/UploadFile/trevormisfeldt/NormalDistribution08302005003434AM/NormalDistribution.aspx


Or u can create ur own class by using a uniform distribution and then  apply Central Limit theorem to get the Gaussian curve.

AE
Last edited: 06-Mar-09 01:00 PM
hakutheblack
· Snapshot
Like · Liked by · 0
NORMDIST is a statistical function. It has a "nice" formula that is given in the Excel help (go to index, write NORMDIST). C# supports all these functions, just be careful to use the largest possible data types in C# (long, double) otherwise you will lose accuracy.

You can see the help topic with the formula also here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/sec02.asp

More info:
http://support.microsoft.com/default.aspx?kbid=827371&product=xl2003

What normal distribution is:
http://pirate.shu.edu/~wachsmut/Teaching/MATH1101/Testing/distribution.html

 

SIMPLE C# Implementation

 /// <summary>
    /// Normal Distribution
    /// </summary>
    /// <param name="x">The value for which you want the distribution.</param>
    /// <param name="mean">The arithmetic mean of the distribution.</param>
    /// <param name="deviation">The standard deviation of the distribution.</param>
    /// <param name="cumulative">If cumulative is true, functions returns the cumulative distribution, otherwise the function returns the probability mass.</param>
    /// <returns>Returns the normal distribution for the specified mean and standard deviation.</returns>
    public static double NormalDistribution(double x, double mean, double deviation, bool cumulative)
    {
      if(cumulative)
        return CumulativeDistribution(x, mean, deviation);
      else
        return NormalDensity(x, mean, deviation);
    }

    private static double NormalDensity(double x, double mean, double deviation)
    {
      return Math.Exp(-(Math.Pow((x - mean)/deviation, 2)/2))/Math.Sqrt(2*Math.PI)/deviation;
    }

    private static double CumulativeDistribution(double x, double mean, double deviation)
    {
      // TODO: Change the number of iterations (16) for more or less precision.
      // You could also change the logic of the recursive function (stop calling
      // for more terms, when the values are below a specific threshold for example.
      return (ErrorFunction((x - mean)/deviation/Math.Sqrt(2), 0, 16) + 1)/2;
    }

    private static double ErrorFunction(double x, int iteration, int iterations)
    {
      double partValue;
      partValue = 2/Math.Sqrt(Math.PI)*Math.Pow(-1, iteration)*Math.Pow(x, 2*iteration + 1)/Factorial(iteration)/(2*iteration + 1);
     
      if(iteration==iterations)
        return partValue;
      else
        return ErrorFunction(x, iteration + 1, iterations) + partValue;
    }

    private static int Factorial(int x)
    {
      if(x==0)
        return 1;
      else
        return x*Factorial(x-1);
    }
hakutheblack
· Snapshot
Like · Liked by · 0
NORMDIST is a statistical function. It has a "nice" formula that is given in the Excel help (go to index, write NORMDIST). C# supports all these functions, just be careful to use the largest possible data types in C# (long, double) otherwise you will lose accuracy.

You can see the help topic with the formula also here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/sec02.asp

More info:
http://support.microsoft.com/default.aspx?kbid=827371&product=xl2003

What normal distribution is:
http://pirate.shu.edu/~wachsmut/Teaching/MATH1101/Testing/distribution.html

 

SIMPLE C# Implementation

 /// <summary>
    /// Normal Distribution
    /// </summary>
    /// <param name="x">The value for which you want the distribution.</param>
    /// <param name="mean">The arithmetic mean of the distribution.</param>
    /// <param name="deviation">The standard deviation of the distribution.</param>
    /// <param name="cumulative">If cumulative is true, functions returns the cumulative distribution, otherwise the function returns the probability mass.</param>
    /// <returns>Returns the normal distribution for the specified mean and standard deviation.</returns>
    public static double NormalDistribution(double x, double mean, double deviation, bool cumulative)
    {
      if(cumulative)
        return CumulativeDistribution(x, mean, deviation);
      else
        return NormalDensity(x, mean, deviation);
    }

    private static double NormalDensity(double x, double mean, double deviation)
    {
      return Math.Exp(-(Math.Pow((x - mean)/deviation, 2)/2))/Math.Sqrt(2*Math.PI)/deviation;
    }

    private static double CumulativeDistribution(double x, double mean, double deviation)
    {
      // TODO: Change the number of iterations (16) for more or less precision.
      // You could also change the logic of the recursive function (stop calling
      // for more terms, when the values are below a specific threshold for example.
      return (ErrorFunction((x - mean)/deviation/Math.Sqrt(2), 0, 16) + 1)/2;
    }

    private static double ErrorFunction(double x, int iteration, int iterations)
    {
      double partValue;
      partValue = 2/Math.Sqrt(Math.PI)*Math.Pow(-1, iteration)*Math.Pow(x, 2*iteration + 1)/Factorial(iteration)/(2*iteration + 1);
     
      if(iteration==iterations)
        return partValue;
      else
        return ErrorFunction(x, iteration + 1, iterations) + partValue;
    }

    private static int Factorial(int x)
    {
      if(x==0)
        return 1;
      else
        return x*Factorial(x-1);
    }
hakutheblack
· Snapshot
Like · Liked by · 0

Even Simple


 




using System;

class NormalDistribution {

static Random rand = new Random();

// returns a normally distributed random number in the range [0..1[
static double NextNormal() {
double x = 0;
for (int i = 0; i < 5; i++) x += rand.NextDouble();
return x / 5;
}

static void Main(string[] arg) {
int[] tab = new int[20];
// fill tab
for (int i = 0; i < 200; i++) tab[(int)(20 * NextNormal())]++;
// print tab as a histogram
for (int i = 0; i < 20; i++) {
Console.Write("{0,2}: ", i);
for (int j = 0; j < tab[i]; j++) Console.Write("*");
Console.WriteLine();
}
}
}
dhoti_prasad
· Snapshot
Like · Liked by · 0

hakutheblack Ji,


Thank you for your intellectual help. I am sure it will help me.


Also Thanks to AlterEgo for your resource.

dhoti_prasad
· Snapshot
Like · Liked by · 0

 


How can I get quantity values over the period of time in Bell Curve?


Assumption.


Curve used is the standard normal curve (mean = 0, standard deviation = 1).


End points used for the calculation are three standard deviations from the mean (i.e. since this the standard normal curve this means end points are -3 and +3: this interval captures 99.7% of the area under the curve.


Quantities distributed over N months where N is even will not show any one month with a peak value: months N/2 and N/2+1 will have the same value.

NayaSadak
· Snapshot
Like · Liked by · 0
I wish I had cuntinued my C++ class.
bhikhaari
· Snapshot
Like · Liked by · 0
haha, nayasadak, you wrote: CUNTinued

lol

and sorry guys, my level doesn't match with this thread either, may be just parallel (i do perl, unix)
Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...
· Posts 1 · Viewed 172
· Posts 19 · Viewed 7043 · Likes 2
· Posts 1 · Viewed 209
· Posts 1 · Viewed 215
· Posts 1 · Viewed 297
· Posts 1 · Viewed 297
· Posts 1 · Viewed 362
· Posts 2 · Viewed 523
· Posts 1 · Viewed 380
· Posts 2 · Viewed 766



Travel Partners
Travel House Nepal