Normal Distribution (Bell Curve) in C#

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

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

dhoti_prasad · Mar 6, 2009 12:16 PM · 35,430 views

11 Replies

Dhoti_Prasad = Dhoti Ko Prasad. Lol...

SaagRaSisnu · Mar 6, 2009 12:22 PM

साग सिस्नो जी, Your level does not match for this thread.

dhoti_prasad · Mar 6, 2009 12:50 PM

What do you know about my Level?

SaagRaSisnu · Mar 6, 2009 1:00 PM

La Dhoti, Talai gaussian distribution ko class nai banai de ko cha.Download gar ani moj gar. http://www.c-sharpcorner.com/U... 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

AlterEgo · Mar 6, 2009 1:00 PM

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/libr... More info: http://support.microsoft.com/d... What normal distribution is: http://pirate.shu.edu/~wachsmu...   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 · Mar 6, 2009 1:06 PM

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/libr... More info: http://support.microsoft.com/d... What normal distribution is: http://pirate.shu.edu/~wachsmu...   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 · Mar 6, 2009 1:06 PM

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(); } } }

hakutheblack · Mar 6, 2009 1:08 PM

hakutheblack Ji, Thank you for your intellectual help. I am sure it will help me. Also Thanks to AlterEgo for your resource.

dhoti_prasad · Mar 6, 2009 1:30 PM

  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.

dhoti_prasad · Mar 9, 2009 1:44 PM

I wish I had cuntinued my C++ class.

NayaSadak · Mar 10, 2009 9:49 AM

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)

bhikhaari · Mar 10, 2009 9:58 AM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all