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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'ext/stat_c/stat_c.c', line 69 static VALUE sc_mean(VALUE obj, VALUE ary) { unsigned long i = 0; long double sum = 0; size_t len = 0; 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).
148 149 150 151 152 153 154 155 156 |
# File 'ext/stat_c/stat_c.c', line 148 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).
177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'ext/stat_c/stat_c.c', line 177 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 = 0; 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).
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'ext/stat_c/stat_c.c', line 104 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 = 0; 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); } } |