LINQ 中的标准偏差 [英] Standard Deviation in LINQ

查看:34
本文介绍了LINQ 中的标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LINQ 是否对聚合 SQL 函数 STDDEV()(标准偏差)进行建模?

Does LINQ model the aggregate SQL function STDDEV() (standard deviation)?

如果不是,计算它的最简单/最佳实践的方法是什么?

If not, what is the simplest / best-practices way to calculate it?

示例:

  SELECT test_id, AVERAGE(result) avg, STDDEV(result) std 
    FROM tests
GROUP BY test_id

推荐答案

你可以让自己的扩展来计算它

You can make your own extension calculating it

public static class Extensions
{
    public static double StdDev(this IEnumerable<double> values)
    {
       double ret = 0;
       int count = values.Count();
       if (count  > 1)
       {
          //Compute the Average
          double avg = values.Average();

          //Perform the Sum of (value-avg)^2
          double sum = values.Sum(d => (d - avg) * (d - avg));

          //Put it all together
          ret = Math.Sqrt(sum / count);
       }
       return ret;
    }
}

如果你有一个样本的总体而不是整个总体,那么你应该使用 ret = Math.Sqrt(sum/(count - 1));.

If you have a sample of the population rather than the whole population, then you should use ret = Math.Sqrt(sum / (count - 1));.

添加标准转化为扩展Chris Bennett 对 LINQ 的偏差.

这篇关于LINQ 中的标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆