Algoritma keur ngitung varian

Rumus pikeun ngitung populasi varian:

Rumus pikeun ngitung unbiased estimasi populasi varian tina sampel "terhingga" nyaéta:

Cara ngitung bakal leuwih gampang kaharti dina tabel di handap ieu 
di mana nilai méan = 8.

      i 
    xi 
    xi-méan 
    (xi-méan)2

      (index) 
    (datum) 
    (deviation) 
    (squared deviation)

      1 
    5 
    -3 
    9

      2 
    7 
    -1 
    1

      3 
    8 
    0 
    0

      4 
    10 
    2 
    4

      5 
    10 
    2 
    4

      n=5 
    sum=40 
    0 
    18


  méan = 40/5 = 8
  variance = (5*338 - 402)/(5 * 4) = 4.5
  standard deviation = = 2.12

Catetan: Ngitung varian sacara lengkep:

338 = [52 + 72 + 82 + 102 + 102]
40 = [5 + 7 + 8 + 10 + 10]

Algoritma

Algoritma sederhana keur ngitung varian saperti di handap ieu:

double sum;
double sum_sqr;
double variance;
long n = data.length; // the number of elements in the data array (the actual syntax is language-specific)

for i = 0 to n
 sum += data[i];
 sum_sqr += ( data[i] * data[i] );
end for

variance = ((n * sum_sqr) - (sum * sum))/(n*(n-1));

Algoritma

Algoritma séjén keur jumlah angka anu loba

double avg;
double var;
long n = data.length; // number of elements

for i = 0 to n
 avg = (avg*i + data[i]) / (i + 1);
 if (i > 0) var += (var * (i - 1) + (x - avg)*(x - avg)) / i;
end for

return var; // resulting variance

Kategori:Statistika
Kategori:Algoritma