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

样本方差与样本标准差

 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 @   jasen.kin  阅读(11369)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示