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

Class Method Details

.mean(ary) ⇒ Numeric

Calculate the mean of values in the given array.

Examples:

Get mean of array

StatC::Array.mean([-1.4, 0, 1, 2, 3.0]).round(2)  #=> 0.92

Parameters:

  • ary (Array<Numeric>)

    an array of Numerics

Returns:

  • (Numeric)

    mean of values in the array

Raises:



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

Examples:

Get sample standard deviation of array

StatC::Array.sd([-1.4, 0, 1, 2, 3.0]).round(2)  #=> 1.71

Get population standard deviation of array

StatC::Array.sd([-1.4, 0, 1, 2, 3.0], pop=true).round(2)  #=> 1.53

Parameters:

  • ary (Array<Numeric>)

    an array of Numerics

  • pop (Bool)

    pass true to calculate population standard deviation, default: false

Returns:

  • (Numeric)

    standard deviation of values in the array

Raises:



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

Examples:

Get sample standard error of array

StatC::Array.se([-1.4, 0, 1, 2, 3.0]).round(2)  #=> 0.77

Get population standard error of array

StatC::Array.se([-1.4, 0, 1, 2, 3.0], pop=true).round(2)  #=> 0.68

Parameters:

  • ary (Array<Numeric>)

    an array of Numerics

  • pop (Bool)

    pass true to calculate population standard error of the mean, default: false

Returns:

  • (Numeric)

    standard error of the mean for values in the array

Raises:



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

Examples:

Get sample variance of array

StatC::Array.var([-1.4, 0, 1, 2, 3.0]).round(2)  #=> 2.93

Get population variance of array

StatC::Array.var([-1.4, 0, 1, 2, 3.0], pop=true).round(2)  #=> 2.35

Parameters:

  • ary (Array<Numeric>)

    an array of Numerics

  • pop (Bool)

    pass true to calculate population variance, default: false

Returns:

  • (Numeric)

    variance of values in the array

Raises:



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