C# 实现正态分布图的相关公式实现。标准偏差 STDEV 概率密度函数 NORM.DIST

话不多说,直接上代码

 class Program
    {
        static void Main(string[] args)
        {

            var list = new double[]{ 
                177.4656067141880,
                177.6392486051540,
                177.8128904961200,
                177.9865323870850,
                178.1601742780510,
                178.3338161690170,
                178.5074580599820,
                178.6810999509480,
                178.8547418419140,
                179.0283837328790,
                179.2020256238450,
                179.3756675148110,
                179.5493094057760,
                179.7229512967420,
                179.8965931877080,
                180.0702350786730,
                180.2438769696390,
                180.4175188606050,
                180.5911607515700,
                180.7648026425360};

            foreach (var item in list)
            {
                var value = NormDist(item, 179.115204678362, 0.412399491043473, false);
                Console.WriteLine(value.ToString("#0.0000000000000000000000000000000000000000"));
            }
            Console.Read();
        }

        /// 
        /// 样本标准偏差
        /// 
        /// 
        /// 
        private static double Stdev_S(IEnumerable<double> values)
        {
            double ret = 0;
            if (values.Count() > 0)
            {
                //  计算bai平均数   
                double avg = values.Average();
                //  计算各数值与平均数的差值的平方,然后求和 
                double sum = values.Sum(d => Math.Pow(d - avg, 2));
                //  除以数量-1,然后开方
                ret = Math.Sqrt(sum / (values.Count()-1));
            }
            return ret;
        }

        /// 
        /// 总体标准偏差
        /// 
        /// 
        /// 
        private static double Stdev_P(IEnumerable<double> values)
        {
            double ret = 0;
            if (values.Count() > 0)
            {
                //  计算bai平均数   
                double avg = values.Average();
                //  计算各数值与平均数的差值的平方,然后求和 
                double sum = values.Sum(d => Math.Pow(d - avg, 2));
                //  除以数量,然后开方
                ret = Math.Sqrt(sum / values.Count());
            }
            return ret;
        }

        /// 
        /// 返回指定平均值和标准偏差的正态分布函数
        /// 
        /// 需要计算其分布的数值
        /// 分布的算术平均值
        /// 分布的标准偏差
        /// 指明函数的形式。如果 cumulative 为 TRUE,函数 NORMDIST 返回累积分布函数;如果为 FALSE,返回概率密度函数。
        /// 
        private static double NormDist(double x,double mean,double standard_dev,bool cumulative)
        {
            if (cumulative) 
                return NormDistTrue(x, mean, standard_dev);
            else 
                return NormDistFalse(x, mean, standard_dev);
        }
        private static double NormDistTrue(double x, double mean, double standard_dev)
        {
            
            var x2 = (x - mean) / standard_dev;
            if (x2==0)
            {
                return 0.5;
            }
            else
            {
                var oor2pi = 1 / Math.Sqrt(2.0 * Math.PI);
                var t = 1 / (1.0 + 0.2316419 * Math.Abs(x2));
                t = t * oor2pi * Math.Exp(-0.5 * x2 * x2) 
                    * (0.31938153 + t 
                    * (-0.356563782 + t 
                    * (1.781477937 + t 
                    * (-1.821255978 + t 
                    * 1.330274429))));

                if (x2>0)
                {
                    return 1.0 - t;
                }
                else
                {
                    return t;
                }
            }
        }
        private static double NormDistFalse(double x, double mean, double standard_dev)
        {

            double PowOfE = -(Math.Pow(x - mean, 2) / (2.0 * Math.Pow(standard_dev, 2)));
            
            return (1 / (Math.Sqrt(2.0 * Math.PI) * standard_dev)) * Math.Pow(Math.E, PowOfE);
        }
    }

你可能感兴趣的:(C# 实现正态分布图的相关公式实现。标准偏差 STDEV 概率密度函数 NORM.DIST)