]> granicus.if.org Git - php/commitdiff
add possibility to calculate the standard deviation and the variance
authorAndrey Hristov <andrey@php.net>
Mon, 2 May 2005 12:29:38 +0000 (12:29 +0000)
committerAndrey Hristov <andrey@php.net>
Mon, 2 May 2005 12:29:38 +0000 (12:29 +0000)
on a sample - the formulae are slightly different.

ext/standard/math.c
ext/standard/tests/math/math_std_dev.phpt

index 3e27103e3c1a07f2c0ab847946735efbb4b38a25..3aa845fd67eda3c68d94eba3efb8b7c2052f32d3 100644 (file)
@@ -1183,11 +1183,14 @@ PHP_FUNCTION(fmod)
 /* }}} */
 
 
-static long double php_population_variance(zval *arr)
+static long double php_population_variance(zval *arr, zend_bool sample)
 {
        double mean, sum = 0.0, vr = 0.0;
        zval **entry;
        HashPosition pos;
+       int elements_num;
+
+       elements_num = zend_hash_num_elements(Z_ARRVAL_P(arr));
 
        zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
        while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) {
@@ -1195,7 +1198,7 @@ static long double php_population_variance(zval *arr)
                sum += Z_DVAL_PP(entry);
                zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);       
        }
-       mean = sum / zend_hash_num_elements(Z_ARRVAL_P(arr));
+       mean = sum / elements_num;
 
        zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
        while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) {
@@ -1205,7 +1208,10 @@ static long double php_population_variance(zval *arr)
                vr += d*d;
                zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);       
        }
-       return (vr / zend_hash_num_elements(Z_ARRVAL_P(arr)));
+       if (sample) {
+               --elements_num;
+       }
+       return (vr / elements_num);
 }
 
 /* {{{ proto float math_variance(array a)
@@ -1213,15 +1219,16 @@ static long double php_population_variance(zval *arr)
 PHP_FUNCTION(math_variance)
 {
        zval *arr;
+       zend_bool sample = 0;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a",  &arr) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b",  &arr, &sample) == FAILURE) {
                return;
        }
        if (zend_hash_num_elements(Z_ARRVAL_P(arr)) == 0) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "The array has zero elements");
                RETURN_FALSE;
        }
-       RETURN_DOUBLE(php_population_variance(arr));
+       RETURN_DOUBLE(php_population_variance(arr, sample));
 }
 /* }}} */
 
@@ -1231,15 +1238,16 @@ PHP_FUNCTION(math_variance)
 PHP_FUNCTION(math_std_dev)
 {
        zval *arr;
+       zend_bool sample = 0;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a",  &arr) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b",  &arr, &sample) == FAILURE) {
                return;
        }
        if (zend_hash_num_elements(Z_ARRVAL_P(arr)) == 0) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "The array has zero elements");
                RETURN_FALSE;
        }
-       RETURN_DOUBLE(sqrt(php_population_variance(arr)));
+       RETURN_DOUBLE(sqrt(php_population_variance(arr, sample)));
 }
 /* }}} */
 
index 6c9f9694acd2d1fb19dbc7e26493dfb868aacd41..a0d8fce6d77fa62da153bfc98b4205bef498b913 100644 (file)
@@ -6,14 +6,19 @@ $a=array(4, 1, 7);
 $dev=math_std_dev($a);
 var_dump(sprintf("%2.9f", $dev));
 var_dump(math_std_dev(array()));
+$a=array(5,7,8,10,10);
+var_dump(math_std_dev($a,1));
 echo "---Variance---\n";
 $a=array(5,7,8,10,10);
 var_dump(math_variance($a));
+var_dump(math_variance($a, true));
 ?>
 --EXPECTF--
 string(11) "2.449489743"
 
 Warning: math_std_dev(): The array has zero elements in %s on line %d
 bool(false)
+float(2.1213203435596)
 ---Variance---
-float(3.6)
\ No newline at end of file
+float(3.6)
+float(4.5)
\ No newline at end of file