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

查看:81
本文介绍了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天全站免登陆