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

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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 = 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).

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

Raises:



144
145
146
147
148
149
150
151
152
# File 'ext/stat_c/stat_c.c', line 144

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

Raises:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'ext/stat_c/stat_c.c', line 173

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

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

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/stat_c/stat_c.c', line 100

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