算法--样本方差、样本标准差、方差、标准方差与加权平均

样本方差与样本标准差

 1、定义:样本中各数据与样本平均数的差的平方和的平均数叫做样本方差;样本方差的算术平方根叫做样本标准差。

      注:样本方差和样本标准差都是衡量一个样本波动大小的量,样本方差或样本标准差越大,样本数据的波动就越大。

标准差与标准方差

1、定义:方差是各个数据与平均数之差的平方和的平均数。在概率论和数理统计中,方差用来度量随机变量和其数学期望(即均值)之间的偏离程度。标准差在概率统计中最常使用作为统计分布程度上的测量。标准差定义为方差的算术平方根,反映组内个体间的离散程度。

加权平均

1、定义:加权平均数(weighted average)是不同比重数据的平均数,就是把原始数据按照合理的比例来计算。

 

算法代码如下:

        public static double StandardDeviation(this IList<double> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (source.Count == 0)
            {
                return double.NaN;
            }

            double variance = source.Variance();

            return Math.Sqrt(variance);
        }

        public static double SampleStandardDeviation(this IList<double> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (source.Count == 0 || source.Count == 1)
            {
                return double.NaN;
            }

            double variance = source.SampleVariance();

            return Math.Sqrt(variance);
        }

        public static double Variance(this IList<double> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (source.Count == 0)
            {
                return double.NaN;
            }

            int count = source.Count();
            double deviation = CalculateDeviation(source, count);

            return deviation / count;
        }

        public static double SampleVariance(this IList<double> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source"); ;
            }

            if (source.Count == 0 || source.Count == 1)
            {
                return double.NaN;
            }

            int count = source.Count();
            double deviation = CalculateDeviation(source, count);

            return deviation / (count - 1);
        }

        public static double WeightedAverage(this IList<double> source, IList<double> factors)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (source.Count != factors.Count)
            {
                throw new ArgumentException("source count is not equal to factors count.");
            }

            if (source.Count == 0)
            {
                return double.NaN;
            }

            double sum = factors.Sum();

            if (sum == 0)
            {
                return double.NaN;
            }

            double weight = 0;

            for (int index = 0; index < factors.Count; index++)
            {
                weight += source[index] * (factors[index] / sum);
            }

            return weight;
        }

        private static double CalculateDeviation(IList<double> source, int count)
        {
            double avg = source.Average();
            double deviation = 0;

            for (int index = 0; index < count; index++)
            {
                deviation += (source[index] - avg) * (source[index] - avg);
            }

            return deviation;
        }


以上在金融方面用得比较多.....

posted @ 2013-07-13 00:10  jasen.kin  阅读(11308)  评论(0编辑  收藏  举报