Frage

Does LINQ modelliert das Aggregat SQL-Funktion STDDEV() (Standardabweichung)?

Wenn nicht, was ist die einfachste / beste Praktiken, wie es zu berechnen?

Beispiel:

  SELECT test_id, AVERAGE(result) avg, STDDEV(result) std 
    FROM tests
GROUP BY test_id
War es hilfreich?

Lösung

Sie können Ihre eigene Erweiterung Berechnung machen

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

Wenn Sie eine Probe haben der Bevölkerung eher als die gesamte Bevölkerung, dann sollten Sie ret = Math.Sqrt(sum / (count - 1)); verwenden.

Verwandelt in Erweiterung von Norm Hinzufügen Abweichung zu LINQ von Chris Bennett .

Andere Tipps

Dynami Antwort funktioniert, aber macht mehrere Durchgänge durch die Daten ein Ergebnis zu erhalten. Dies ist eine Single-Pass-Methode, die die Proben-Standardabweichung berechnet :

public static double StdDev(this IEnumerable<double> values)
{
    // ref: http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/
    double mean = 0.0;
    double sum = 0.0;
    double stdDev = 0.0;
    int n = 0;
    foreach (double val in values)
    {
        n++;
        double delta = val - mean;
        mean += delta / n;
        sum += delta * (val - mean);
    }
    if (1 < n)
        stdDev = Math.Sqrt(sum / (n - 1));

    return stdDev;
}

Dies ist die Proben-Standardabweichung , da es durch n - 1 teilt. Für die normale Standardabweichung müssen Sie teilen durch n statt.

Diese nutzt Verfahren die im Vergleich zu dem Verfahren Average(x^2)-Average(x)^2 höhere numerische Genauigkeit hat.

Dieses wandelt David Clarke Antwort in eine Verlängerung, die die gleiche Form wie die anderen Aggregat LINQ-Funktionen wie folgt Durchschnitt.

Verwendung wäre: var stdev = data.StdDev(o => o.number)

public static class Extensions
{
    public static double StdDev<T>(this IEnumerable<T> list, Func<T, double> values)
    {
        // ref: https://stackoverflow.com/questions/2253874/linq-equivalent-for-standard-deviation
        // ref: http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/ 
        var mean = 0.0;
        var sum = 0.0;
        var stdDev = 0.0;
        var n = 0;
        foreach (var value in list.Select(values))
        {
            n++;
            var delta = value - mean;
            mean += delta / n;
            sum += delta * (value - mean);
        }
        if (1 < n)
            stdDev = Math.Sqrt(sum / (n - 1));

        return stdDev; 

    }
} 
var stddev = Math.Sqrt(data.Average(z=>z*z)-Math.Pow(data.Average(),2));

Gerade auf den Punkt (und C #> 6.0), wird Dynamis Antwort folgt aus:

    public static double StdDev(this IEnumerable<double> values)
    {
        var count = values?.Count() ?? 0;
        if (count <= 1) return 0;

        var avg = values.Average();
        var sum = values.Sum(d => Math.Pow(d - avg, 2));

        return Math.Sqrt(sum / count);
    }
public static double StdDev(this IEnumerable<int> values, bool as_sample = false)
{
    var count = values.Count();
    if (count > 0) // check for divide by zero
    // Get the mean.
    double mean = values.Sum() / count;

    // Get the sum of the squares of the differences
    // between the values and the mean.
    var squares_query =
        from int value in values
        select (value - mean) * (value - mean);
    double sum_of_squares = squares_query.Sum();
    return Math.Sqrt(sum_of_squares / (count - (as_sample ? 1 : 0)))
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top