Module: Enumerable

Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#meanNumber

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.

Returns:

  • (Number)

    A mean value



1456
1457
1458
1459
1460
1461
1462
# File 'ext/enumerable/statistics/extension/statistics.c', line 1456

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

Returns:



1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
# File 'ext/enumerable/statistics/extension/statistics.c', line 1523

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.

Returns:

  • (mean, variance)

    Two element array consists of mean and variance values



1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
# File 'ext/enumerable/statistics/extension/statistics.c', line 1430

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))

Returns:

  • (Number)

    A standard deviation value



1553
1554
1555
1556
1557
1558
1559
# File 'ext/enumerable/statistics/extension/statistics.c', line 1553

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.

Redefines sum (Ruby >= 2.4). Original is aliased as __sum__.

Returns:

  • (Number)

    A summation value



1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
# File 'ext/enumerable/statistics/extension/statistics.c', line 1270

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 {
    sum = rb_funcall(obj, id_builtin_sum, argc, &init);
  }
#else
  enum_sum_count(obj, init, skip_na, &sum, NULL);
#endif

  return sum;
}

#value_counts(*args) ⇒ Object



2084
2085
2086
2087
2088
# File 'ext/enumerable/statistics/extension/statistics.c', line 2084

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$).

Returns:

  • (Number)

    A variance value



1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
# File 'ext/enumerable/statistics/extension/statistics.c', line 1478

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;
}