Module: StatC::Array
- Defined in:
- ext/stat_c/stat_c.c,
ext/stat_c/stat_c.c
Overview
Statistical methods operating on the values of an array
Class Method Summary collapse
-
.mean(ary) ⇒ Numeric
Calculate the mean of values in the given array.
-
.sd(*args) ⇒ Numeric
Calculate the standard deviation of values in given array.
-
.se(*args) ⇒ Numeric
Calculate the standard deviation of values in given array.
-
.var(*args) ⇒ Numeric
Calculate the variance of values in given array.
Class Method Details
.mean(ary) ⇒ Numeric
Calculate the mean of values in the given array.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'ext/stat_c/stat_c.c', line 65 static VALUE sc_mean(VALUE obj, VALUE ary) { unsigned long i = 0; long double sum = 0; size_t len = assert_array_not_empty(ary); for (i = 0; i < len; ++i) { sum += NUM2DBL(rb_ary_entry(ary, i)); } return DBL2NUM(sum / len); } |
.sd(*args) ⇒ Numeric
Calculate the standard deviation of values in given array.
If pop param is set to true, the standard deviation is based on population variance. Otherwise, sample variance is used (default).
142 143 144 145 146 147 148 149 150 |
# File 'ext/stat_c/stat_c.c', line 142 static VALUE sc_sd(int argc, VALUE* argv, VALUE obj) { VALUE ary, calc_pop_var; /* one required and one optional argument */ rb_scan_args(argc, argv, "11", &ary, &calc_pop_var); return DBL2NUM(sqrt(NUM2DBL(sc_var(argc, argv, obj)))); } |
.se(*args) ⇒ Numeric
Calculate the standard deviation of values in given array.
If pop param is set to true, the standard error of the mean is based on population variance. Otherwise, sample variance is used (default).
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'ext/stat_c/stat_c.c', line 171 static VALUE sc_se(int argc, VALUE* argv, VALUE obj) { VALUE ary, calc_pop_var; /* one required and one optional argument */ rb_scan_args(argc, argv, "11", &ary, &calc_pop_var); long double sd = NUM2DBL(sc_sd(argc, argv, obj)); size_t len = assert_array_not_empty(ary); return DBL2NUM(sd / sqrt(len)); } |
.var(*args) ⇒ Numeric
Calculate the variance of values in given array.
If pop param is set to true, calculates the population variance of values in the array. Otherwise, the sample variance is calculated (default).
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'ext/stat_c/stat_c.c', line 99 static VALUE sc_var(int argc, VALUE* argv, VALUE obj) { VALUE ary, calc_pop_var; /* one required and one optional argument */ rb_scan_args(argc, argv, "11", &ary, &calc_pop_var); unsigned long i = 0; long double sum = 0; size_t len = assert_array_not_empty(ary); long double mean = NUM2DBL(sc_mean(obj, ary)); for (i = 0; i < len; ++i) { sum += pow(sc_ary_entry(ary, i) - mean, 2); } if (NIL_P(calc_pop_var) || FALSE_P(calc_pop_var)) { /* sample variance */ return DBL2NUM(sum / (len - 1)); } else { /* population variance */ return DBL2NUM(sum / len); } } |