Module: StatC::Test::T

Defined in:
ext/stat_c/stat_c.c,
ext/stat_c/stat_c.c

Overview

Module containing T test methods.

Class Method Summary collapse

Class Method Details

.dof_welch(var1, len1, var2, len2) ⇒ Numeric

Note:

Uses non pooled variance

Calculate degrees of freedom for Welch’s t statistic

Examples:

Get dof for two samples

StatC::Test::T.dof_welch(2.3, 5, 1.282, 5).round(2) #=> 7.40

Parameters:

  • var1 (Numeric)

    sample variance of sample 1

  • len1 (Numeric)

    size of sample 1

  • var2 (Numeric)

    sample variance of sample 2

  • len1 (Numeric)

    size of sample 2

Returns:

  • (Numeric)

    dof

Raises:



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'ext/stat_c/stat_c.c', line 254

static VALUE
sc_dof_welch(VALUE obj,
             VALUE var1, VALUE len1,
             VALUE var2, VALUE len2)
{
  double v1, l1, v2, l2, num, denom, val, d1, d2;

  v1 = NUM2DBL(var1);
  l1 = NUM2DBL(len1);

  v2 = NUM2DBL(var2);
  l2 = NUM2DBL(len2);

  if (l1 <= 0 || l2 <= 0) {
    rb_raise(sc_eError, "Sample sizes must be > 0");
  }

  num = pow( (v1 / l1) + (v2 / l2), 2);

  d1 = pow(l1, 2) * (l1 - 1);
  d2 = pow(l2, 2) * (l2 - 1);

  if (d1 == 0|| d2 == 0) {
    rb_raise(sc_eError, "Divide by zero error");
  }

  denom =
    (pow(v1, 2) / d1) + (pow(v2, 2) / d2);

  if (denom == 0) {
    rb_raise(sc_eError, "Divide by zero error");
  }

  val = num / denom;

  return DBL2NUM(val);

}

.t_stat_welch(mean1, var1, len1, mean2, var2, len2) ⇒ Numeric

Calculate Welch’s t statistic

Examples:

Get Welch’s t stat for two samples

StatC::Test::T.t_stat_welch(-0.1, 2.3, 5, 2.42, 1.282, 5).round(2) #=> -2.98

Parameters:

  • mean1 (Numeric)

    mean of sample 1

  • var1 (Numeric)

    sample variance of sample 1

  • len1 (Numeric)

    size of sample 1

  • mean2 (Numeric)

    mean of sample 2

  • var2 (Numeric)

    sample variance of sample 2

  • len1 (Numeric)

    size of sample 2

Returns:

  • (Numeric)

    Welch’s t statistic

Raises:



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'ext/stat_c/stat_c.c', line 209

static VALUE
sc_t_stat_welch(VALUE obj,
                VALUE mean1, VALUE var1, VALUE len1,
                VALUE mean2, VALUE var2, VALUE len2)
{
  double m1, v1, l1, m2, v2, l2, val;

  m1 = NUM2DBL(mean1);
  v1 = NUM2DBL(var1);
  l1 = NUM2DBL(len1);

  m2 = NUM2DBL(mean2);
  v2 = NUM2DBL(var2);
  l2 = NUM2DBL(len2);

  if (l1 <= 0 || l2 <= 0) {
    rb_raise(sc_eError, "Sample sizes must be > 0");
  }

  val = (v1 / l1) + (v2 / l2);

  if (val == 0.0) {
    rb_raise(sc_eError, "Divide by zero error");
  }

  return DBL2NUM((m1 - m2) / sqrt(val));
}