JAVA standard deviation

Asked

Viewed 1,329 times

1

Here are my methods for calculating standard deviation, which for some reason unknown by me does not work (I left my test method down here as well).

public strictfp Double getMedia(List<Double> valor) {
    try {
        return getSoma(valor) / valor.size();
    } catch (NullPointerException e) {
        throw new IllegalArgumentException("The list has null values");
    }
}

public strictfp Double getSoma(List<Double> valor) {
    Double soma = 0D;
    for (int i = 0; i < valor.size(); i++) {
        soma += valor.get(i);
    }
    return soma;
}

public strictfp Double getDesvioPadrao(List<Double> valor) {
    Double media = getMedia(valor);
    int tam = valor.size();
    Double desvPadrao = 0D;
    for (Double vlr : valor) {
        Double aux = vlr - media;
        desvPadrao += aux * aux;
    }
    return Math.sqrt(desvPadrao / tam);
}
#

Test method:

@SuppressWarnings("deprecation")
@Test
private void TesteDesvioPadrao() {
    List<Double> valor = new ArrayList<Double>();
    valor.add(13.0);
    valor.add(23.0);
    valor.add(4.0);
    valor.add(2.0);
    valor.add(11.0);
    valor.add(12.0);

    assertEquals(new Double(7.467708), ilbt.getDesvioPadrao(valor));

}
  • 1

    What is the mistake? The question is not clear

  • What’s the problem you’re having? Your question doesn’t tell you what you need.

  • When I run my test method, "initializationError"

  • 2

    Please do not put SOLVED in the title, the site does not work like this. From a look at the [tour]

1 answer

2


The error is in the calculation of the standard deviation at the end of the method getDesvioPadrao.

In it you return the square root of the variance division by the collection size. How are you calculating the standard deviation of a sample, should be the size of the collection subtracting 1.

public strictfp Double getDesvioPadrao(List<Double> valor) {
    Double media = getMedia(valor);
    int tam = valor.size();
    Double desvPadrao = 0D;
    for (Double vlr : valor) {
        Double aux = vlr - media;
        desvPadrao += aux * aux;
    }
    return Math.sqrt(desvPadrao / (tam - 1));
}

See working on repl.it

  • Thanks, this detail really was missing, but the error initializationError[Runner Junit4] still persists

  • 3

    @Strigi Please do not add "solved" to the question title. The only way to signal that the question has an answer is to mark some answer as correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.