Descriptive statistics

ddanbe 1 Tallied Votes 477 Views Share

A bunch of basic statistical functions.
I could have made a class here and I could have used more features of C#. But just to keep it simple I left it as a console application. So the modifications you have to make if you're into C, C++, Java etc. should be minor.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Statistics
{
    class Program
    {
        static void Main(string[] args)
        {
            Intro();
            //////////////////////////////////////////////////////////
            // TestAr could come from a file or userinput or whatever.
            // Here TestAr is an array just for simplicity.
            /////////////////////////////////////////////////////////
            double[] TestAr = { 3.3, 5.77, 7.34, 9, 4.1, 13.66, 6.2, 7.65, 11.3, 12.67, 9, 10.03, 12.6, 3.76 };

            Console.WriteLine("\tThe mean is  ............. : {0}", mean(TestAr.Length, TestAr));
            Console.WriteLine("\tThe standard error is  ... : {0}", stderr(TestAr.Length, TestAr));
            Console.WriteLine("\tThe median is ............ : {0}", median(TestAr.Length, TestAr));
            Console.WriteLine("\tThe standard deviation is  : {0}", stddev(TestAr.Length, TestAr));
            Console.WriteLine("\tThe variance is .......... : {0}", variance(TestAr.Length, TestAr));
            Console.WriteLine("\tThe range is ............. : {0}", range(TestAr.Length, TestAr));
            Console.WriteLine("\tThe minimum is ........... : {0}", min(TestAr.Length, TestAr));
            Console.WriteLine("\tThe maximum is ........... : {0}", max(TestAr.Length, TestAr));
            Console.WriteLine("\tThe sum is ............... : {0}", sum(TestAr.Length, TestAr));
            Console.WriteLine("\tThe number of data is .... : {0}", TestAr.Length);
            Console.WriteLine("");
            Console.WriteLine("=========================================================");
    
            Console.ReadKey();
        }

        static void Intro()
        {
            Console.WriteLine("=========================================================");
            Console.WriteLine("|                                                       |");
            Console.WriteLine("|                   Descriptive STATISTICS              |");
            Console.WriteLine("|                                                       |");
            Console.WriteLine("=========================================================");
            Console.WriteLine("");
        }

        /// ==================================================================
        /// Sums up all the numbers in an array arr with length len.
        /// //////////////////////////////////////////////////////////////////
        static double sum(int len, double[] arr)
        {
            double sum = 0.0;
            for (int i = 0; i < len; i++)
            {
                sum += arr[i];
            }
            return sum;
        }

        /// ==================================================================
        /// Calculates the MEAN of an array arr with length len.
        /// The mean is given by sum of numbers/amount of numbers.
        /// //////////////////////////////////////////////////////////////////
        static double mean(int len, double[] arr)
        {
            return sum(len, arr) / len;
        }

        /// ==================================================================
        /// Calculates the sample VARIANCE of an array arr with length len.
        /// It is the sum of the squares of the difference of the numbers with
        /// their mean, divided by the amount of numbers minus one.
        /// //////////////////////////////////////////////////////////////////
        static double variance(int len, double[] arr)
        {
            double m = mean(len, arr);
            double sum = 0.0;
            double d = 0.0;

            for (int i = 0; i < len; i++)
            {
                d = arr[i] - m;
                sum += d * d;
            }
            return sum / (len - 1);
        }

        /// ==================================================================
        /// Calculates the STANDARD DEVIATION of an array arr with length len.
        /// It is the square root of the variance
        /// //////////////////////////////////////////////////////////////////
        static double stddev(int len, double[] arr)
        {
            return Math.Sqrt(variance(len,arr));
        }

        /// ==================================================================
        /// Calculates the STANDARD ERROR of an array arr with length len.
        /// It is the standard deviation divided by the square root of the
        /// amount of numbers.
        /// //////////////////////////////////////////////////////////////////
        static double stderr(int len, double[] arr)
        {
            return stddev(len, arr) / Math.Sqrt(len);
        }

        /// ==================================================================
        /// Calculates the MINIMUM of an array arr with length len.
        /// //////////////////////////////////////////////////////////////////
        static double min(int len, double[] arr)
        {
            double mini = double.MaxValue;
            for (int i = 0; i < len; i++)
            {
                if (arr[i] < mini)
                {
                    mini = arr[i];
                }
            }
            return mini;
        }

        /// ==================================================================
        /// Calculates the MAXIMUM of an array arr with length len.
        /// //////////////////////////////////////////////////////////////////
        static double max(int len, double[] arr)
        {
            double maxi = double.MinValue;
            for (int i = 0; i < len; i++)
            {
                if (arr[i] > maxi)
                {
                    maxi = arr[i];
                }
            }
            return maxi;
        }

        /// ==================================================================
        /// Calculates the RANGE of an array arr with length len.
        /// It is the difference between the largest and the smallest number.
        /// //////////////////////////////////////////////////////////////////
        static double range(int len, double[] arr)
        {
            return max(len, arr) - min(len, arr);
        }

        /// ==================================================================
        /// Calculates the MEDIAN of an array arr with length len.
        /// It is the middle value of an array of sorted numbers.
        /// //////////////////////////////////////////////////////////////////
        static double median(int len, double[] arr)
        {
            int i, j;
            double temp;
            for (i = 0; i < len - 1; i++)
            {
                for (j = i + 1; j < len; j++)
                {
                    if (arr[j] < arr[i])
                    {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
            if (len % 2 == 0)
                return ((arr[len / 2] + arr[len / 2 - 1]) / 2.0);
            else
                return arr[len / 2];
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.