I was wondering if someone could help me a bit. Im developing a program that extracts mean and standard deviation from a list of double values. Im using org.apache.commons.math4.stat.descriptive to do so, this library has a method called GetStandardDeviation() that gives you the std of a list, but when i calculate this std manually i dont get the same value that Method returns me. Here is my code:

public class PDC extends AppCompatActivity {

private DescriptiveStatistics stats = new DescriptiveStatistics();

private double media;
private double dvt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pdc);

    double K = 0.00;

    Bundle extras;

    extras = getIntent().getExtras();
    assert extras != null;

    String nombrelista = extras.getString("nombrelista");
    ArrayList<String> listDatos = extras.getStringArrayList("listDatos");

    assert listDatos != null;

    if(listDatos.size()==9){ K = 1.92;}
    if(listDatos.size()==10){K = 1.96;}
    if(listDatos.size()==11){K = 2.00;}
    if(listDatos.size()==12){K = 2.04;}
    if(listDatos.size()==13){K = 2.07;}
    if(listDatos.size()==14){K = 2.10;}

    //This for parses to double the positions from listdatos Arraylist and put it in the DescriptiveStatistics type variable called "stats"

    for (int i = 0; i < listDatos.size(); i++) {
        stats.addValue(Double.parseDouble(listDatos.get(i)));
    }

    // so now, when i have my arraylist saved in "stats" i call getStandardDeviation

    media = stats.getMean();
    dvt = stats.getStandardDeviation();

    int numeroMed = listDatos.size();

    double imin = media - (K * dvt);
    double imax = media + (K * dvt);

    //then i pass all the values to another activity where it shows up in a TextView Field

    Intent A = new Intent(this, Resultados.class);
    A.putExtra("nombreLista", nombrelista);
    A.putExtra("NumeroMedidas", numeroMed);
    A.putStringArrayListExtra("listDatos", listDatos);
    A.putExtra("Media", media);
    A.putExtra("dvt", dvt);
    A.putExtra("Imin", imin);
    A.putExtra("Imax", imax);
    A.putExtra("K",K);
    startActivity(A);
}
} 

Im not sure why it doesnt work. Maybe i loose some information when i pass through activities since the values use to be very long numbers.

¿Would it be better if i dont use this library and i design my own method that can do the standard deviation?

Also if you see something to improve in my code let me know, ill really apreciate it since im kind of new to this. Thank you so much!

1

There are 1 best solutions below

1
lantian On

It is the Loss of precision in Java .

You can use strictfp to modify the onCreate method,Or put the double to BigDecimal will solve this problem