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



1327
1328
1329
1330
1331
1332
1333
# File 'ext/enumerable/statistics/extension/statistics.c', line 1327

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:



1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
# File 'ext/enumerable/statistics/extension/statistics.c', line 1392

static VALUE
enum_mean_stdev(int argc, VALUE* argv, VALUE obj)
{
  VALUE opts, mean, variance;
  size_t ddof = 1;

  rb_scan_args(argc, argv, "0:", &opts);
  if (opt_population_p(opts))
    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



1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
# File 'ext/enumerable/statistics/extension/statistics.c', line 1303

static VALUE
enum_mean_variance_m(int argc, VALUE* argv, VALUE obj)
{
  VALUE opts, mean, variance;
  size_t ddof = 1;

  rb_scan_args(argc, argv, "0:", &opts);
  if (opt_population_p(opts))
    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



1420
1421
1422
1423
1424
1425
1426
# File 'ext/enumerable/statistics/extension/statistics.c', line 1420

static VALUE
enum_stdev(int argc, VALUE* argv, VALUE obj)
{
  VALUE variance = enum_variance(argc, argv, obj);
  VALUE stdev = sqrt_value(variance);
  return stdev;
}

#sumNumber

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.

Returns:

  • (Number)

    A summation value



1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
# File 'ext/enumerable/statistics/extension/statistics.c', line 1154

static VALUE
enum_sum(int argc, VALUE* argv, VALUE obj)
{
  VALUE sum, init;

  if (rb_scan_args(argc, argv, "01", &init) == 0)
    init = LONG2FIX(0);

  enum_sum_count(obj, init, &sum, NULL);

  return sum;
}

#value_counts(*args) ⇒ Object



1964
1965
1966
1967
1968
# File 'ext/enumerable/statistics/extension/statistics.c', line 1964

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



1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
# File 'ext/enumerable/statistics/extension/statistics.c', line 1349

static VALUE
enum_variance(int argc, VALUE* argv, VALUE obj)
{
  VALUE opts, variance;
  size_t ddof = 1;

  rb_scan_args(argc, argv, "0:", &opts);
  if (opt_population_p(opts))
    ddof = 0;

  enum_mean_variance(obj, NULL, &variance, ddof);
  return variance;
}