算法--樣本方差、樣本標准差、方差、標准方差與加權平均


樣本方差與樣本標准差

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


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM