Module: Enumerable
- Defined in:
- (unknown)
Instance Method Summary collapse
-
#mean ⇒ Number
Calculate a mean of the values in
enum. -
#mean_stdev(population: false) ⇒ mean, stdev
Calculate a mean and a standard deviation of the values in
enum. -
#mean_variance(population: false) ⇒ mean, variance
Calculate a mean and a variance of the values in
enum. -
#stdev(population: false) ⇒ Number
Calculate a standard deviation of the values in
enum. -
#sum(skip_na: false) ⇒ Number
Calculate the sum of the values in
enum. - #value_counts(*args) ⇒ Object
-
#variance(population: false) ⇒ Number
Calculate a variance of the values in
enum.
Instance Method Details
#mean ⇒ Number
Calculate a mean of the values in enum.
This method utilizes
Kahan summation algorithm
to compensate the result precision when the enum includes Float values.
1455 1456 1457 1458 1459 1460 1461 |
# File 'ext/enumerable/statistics/extension/statistics.c', line 1455
static VALUE
enum_mean(VALUE obj)
{
VALUE mean;
enum_mean_variance(obj, &mean, NULL, 1);
return mean;
}
|
#mean_stdev(population: false) ⇒ mean, stdev
Calculate a mean and a standard deviation of the values in enum.
The first element of the result array is the mean,
and the second is the standard deviation.
This method is equivalent to:
def mean_stdev(population: false)
m, v = mean_variance(population: population)
[m, Math.sqrt(v)]
end
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 |
# File 'ext/enumerable/statistics/extension/statistics.c', line 1522
static VALUE
enum_mean_stdev(int argc, VALUE* argv, VALUE obj)
{
struct variance_opts options;
VALUE opts, mean, variance;
size_t ddof = 1;
rb_scan_args(argc, argv, "0:", &opts);
get_variance_opts(opts, &options);
if (options.population)
ddof = 0;
enum_mean_variance(obj, &mean, &variance, ddof);
VALUE stdev = sqrt_value(variance);
return rb_assoc_new(mean, stdev);
}
|
#mean_variance(population: false) ⇒ mean, variance
Calculate a mean and a variance of the values in enum.
The first element of the result array is the mean, and the second is the variance.
When the population: keyword parameter is true,
the variance is calculated as a population variance (divided by $n$).
The default population: keyword parameter is false;
this means the variance is a sample variance (divided by $n-1$).
This method scan values in enum only once,
and does not cache the values on memory.
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 |
# File 'ext/enumerable/statistics/extension/statistics.c', line 1429
static VALUE
enum_mean_variance_m(int argc, VALUE* argv, VALUE obj)
{
struct variance_opts options;
VALUE opts, mean, variance;
size_t ddof = 1;
rb_scan_args(argc, argv, "0:", &opts);
get_variance_opts(opts, &options);
if (options.population)
ddof = 0;
enum_mean_variance(obj, &mean, &variance, ddof);
return rb_assoc_new(mean, variance);
}
|
#stdev(population: false) ⇒ Number
Calculate a standard deviation of the values in enum.
This method is equivalent to:
Math.sqrt(enum.variance(population: population))
1552 1553 1554 1555 1556 1557 1558 |
# File 'ext/enumerable/statistics/extension/statistics.c', line 1552
static VALUE
enum_stdev(int argc, VALUE* argv, VALUE obj)
{
VALUE variance = enum_variance(argc, argv, obj);
VALUE stdev = sqrt_value(variance);
return stdev;
}
|
#sum(skip_na: false) ⇒ Number
Calculate the sum of the values in enum.
This method utilizes
Kahan summation algorithm
to compensate the result precision when the enum includes Float values.
Note that This library does not redefine sum method introduced in Ruby 2.4.
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 |
# File 'ext/enumerable/statistics/extension/statistics.c', line 1269
static VALUE
enum_sum(int argc, VALUE* argv, VALUE obj)
{
VALUE sum, init, opts;
int skip_na;
if (rb_scan_args(argc, argv, "01:", &init, &opts) == 0) {
init = LONG2FIX(0);
}
skip_na = opt_skip_na(opts);
#ifndef HAVE_ENUM_SUM
if (skip_na) {
enum_sum_count(obj, init, skip_na, &sum, NULL);
}
else {
rb_funcall(orig_enum_sum, rb_intern("call"), argc, &init);
}
#else
enum_sum_count(obj, init, skip_na, &sum, NULL);
#endif
return sum;
}
|
#value_counts(*args) ⇒ Object
2083 2084 2085 2086 2087 |
# File 'ext/enumerable/statistics/extension/statistics.c', line 2083
static VALUE
enum_value_counts(int argc, VALUE* argv, VALUE obj)
{
return any_value_counts(argc, argv, obj, enum_value_counts_without_sort);
}
|
#variance(population: false) ⇒ Number
Calculate a variance of the values in enum.
This method scan values in enum only once,
and does not cache the values on memory.
When the population: keyword parameter is true,
the variance is calculated as a population variance (divided by $n$).
The default population: keyword parameter is false;
this means the variance is a sample variance (divided by $n-1$).
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 |
# File 'ext/enumerable/statistics/extension/statistics.c', line 1477
static VALUE
enum_variance(int argc, VALUE* argv, VALUE obj)
{
struct variance_opts options;
VALUE opts, variance;
size_t ddof = 1;
rb_scan_args(argc, argv, "0:", &opts);
get_variance_opts(opts, &options);
if (options.population)
ddof = 0;
enum_mean_variance(obj, NULL, &variance, ddof);
return variance;
}
|