码迷,mamicode.com
首页 > 其他好文 > 详细

数学计算公式

时间:2015-06-08 17:10:56      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

        /// <summary>
        /// 求出数据平均值,并保留三位小数
        /// </summary>
        /// <param name="Valist">数据集合</param>
        /// <returns></returns>
        public static double average(List<double> Valist)
        {
            double sum = 0;
            foreach (double d in Valist)
            {
                sum = sum + d;
            }
            double revl = System.Math.Round(sum / Valist.Count, 3);
            return revl;
        }
        /// <summary>
        /// 求数据集合标准差
        /// </summary>
        /// <param name="ValList"></param>
        /// <returns></returns>
        public static double stdev(List<double> ValList)
        {
            double avg = average(ValList);
            double sumstdev = 0;
            foreach (double d in ValList)
            {
                sumstdev = sumstdev + (d - avg) * (d - avg);
            }
            double stdeval = System.Math.Sqrt(sumstdev);
            return System.Math.Round(stdeval, 3);
        }
        /// <summary>
        /// 相关系数,要求两个集合数量必须相同
        /// </summary>
        /// <param name="array1">数组一</param>
        /// <param name="array2">数组二</param>
        /// <returns></returns>
        public double correl(List<double> array1, List<double> array2)
        {
            //数组一
            double avg1 = average(array1);
            double stdev1 = stdev(array1);
            //数组二
            double avg2 = average(array2);
            double stdev2 = stdev(array2);

            double sum = 0;
            for (int i = 0; i < array1.Count && i < array2.Count; i++)
            {
                sum = sum + ((array1[i] - avg1) / stdev1) * ((array2[i] - avg2) / stdev2);
            }
            return Math.Round(sum, 3);
        }

  

数学计算公式

标签:

原文地址:http://www.cnblogs.com/Aamir-Ye/p/4561205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!