Вопрос

I've been doing an app since few days ago but it's wrong and I do not know why.

I've done the same operation in various ways. I've searched here on the blog, but I still get the incorrect result.

I hope you can help me:

I'm calculating the ** Mean and Standard Deviation**. The Mean is OK. The Standard Deviation is wrong. This is my code:

    LinkedList<Double> lista = new LinkedList<Double>();
    int contador = 0;

    private void btnAgregar_Click(object sender, EventArgs e)
    {
        lista.AddLast(Convert.ToDouble(txtNum.Text));
        MessageBox.Show("Se agregó el número: " + txtNum.Text);
        contador++;
        txtNum.Text = "";
        txtNum.Focus();
    }

    Double media;
    Double desviacionE = 0;
    Double suma = 0;

    private void btnCalcular_Click(object sender, EventArgs e)
    {
        media = 0;
        calculaMedia();
        calculaDesviacionE();
    }

    public void calculaMedia()
    {
        foreach (var item in lista)
        {
            String valorItem = item.ToString();
            suma = suma + Convert.ToDouble(valorItem);
        }
        media = suma / contador;
        txtMedia.Text = "" + media;
    }

    public void calculaDesviacionE()
    {
        Double average = lista.Average();
        Double sum = 0;
        foreach (var item in lista)
        {
            sum += ((Convert.ToDouble(item.ToString()))*(Convert.ToDouble(item.ToString())));
        }
        Double sumProm = sum / lista.Count();
        Double desvE = Math.Sqrt(sumProm-(average*average));
        txtDesv.Text = "" + desvE;
    }

I hope You can help me! Thank You

Это было полезно?

Решение

Following the rules for standard deviation found at http://en.wikipedia.org/wiki/Standard_deviation

        LinkedList<Double> list = new LinkedList<Double>();
        double sumOfSquares = 0;
        double deviation;
        double delta;
        list.AddLast(2);
        list.AddLast(4);
        list.AddLast(4);
        list.AddLast(4);
        list.AddLast(5);
        list.AddLast(5);
        list.AddLast(7);
        list.AddLast(9);


        double average = list.Average();
        Console.WriteLine("Average: " + average);

        foreach (double item in list)
        {
            delta = Math.Abs(item - average);
            sumOfSquares += (delta * delta);
        }
        Console.WriteLine("Sum of Squares: " + sumOfSquares);

        deviation = Math.Sqrt(sumOfSquares / list.Count());
        Console.WriteLine("Standard Deviation: " + deviation); //Final result is 2.0

Другие советы

You need to subtract the average before squaring.

// Population Standard Deviation
public void populationStandardDev()
{
    Double average = lista.Average();
    Double sum = 0;
    foreach (var item in lista)
    {
        Double difference = item - average;
        sum += difference*difference;
    }
    Double sumProd = sum / lista.Count(); // divide by n
    Double desvE = Math.Sqrt(sumProd);
}

// Standard deviation
public void standardDev()
{
    Double average = lista.Average();
    Double sum = 0;
    foreach (var item in lista)
    {
        Double difference = item - average;
        sum += difference*difference;
    }
    Double sumProd = sum / (lista.Count()-1); // divide by n-1 
    Double desvE = Math.Sqrt(sumProd);
}

The formula depends on the set of data you have.

  1. Is it the entire population? Then you should use the Population Standard Deviation (divisor: n).

  2. Is the data a sample of a set? Then you should use the Sample Standard Deviation (divisor: n - 1)

You may find an easier-to-understand guide here: Laerd Statistics - Standard Deviation, which also has a handy Calculator for both solutions.

So, it is as @Greg answered, though I would first check if the list holds any values to avoid division by zero.

double stdDeviation = 0;
if (lista.Any())
{
    var avg = lista.Average();
    var sumOfSquares = lista.Sum(item => Math.Pow(item - avg, 2));

    stdDeviation = Math.Sqrt(sumOfSquares / [divisor goes here]);
}
return stdDeviation;

Where the divisor be lista.Count() for population or (lista.Count() - 1) for samples.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top