Class: CheapStats

Inherits:
Object
  • Object
show all
Defined in:
lib/cheap_stats.rb,
lib/cheap_stats/version.rb,
ext/cheap_stats/rb_cheap_stats.c

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(samples) ⇒ Object

initialize object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'ext/cheap_stats/rb_cheap_stats.c', line 89

static VALUE
rb_cheap_stats_initialize(VALUE self, VALUE samples)
{
  rb_cheap_stats_t* ptr;
  VALUE exp;
  const char* msg;
  int err;
  double *a;
  int i;
  int n;
  VALUE v;
  int t;
  char str[64];

  /*
   * strip context data
   */
  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  /*
   * check argument
   */
  Check_Type(samples, T_ARRAY);

  do {
    exp = Qnil;
    a   = NULL;

    /*
     * allocate work buffer
     */
    n   = RARRAY_LEN(samples);
    a   = malloc(sizeof(double) * n);

    if (a == NULL) {
      exp = rb_eNoMemError;
      msg = "Memory allocation failed";
      break;
    }

    /*
     * copy source value
     */
    for (i = 0; i < n; i++) {
      v = RARRAY_AREF(samples, i);
      t = TYPE(v);

      if (!IS_NUMERIC(t)) break;

      a[i] = NUM2DBL(v);
    }

    if (i != n) {
      exp = rb_eTypeError;
      msg = "the value that not numeric was included";
    }

    /*
     * create statistic context
     */
    err = cheap_stats_new(a, n, &ptr->stats);
    if (err) {
      sprintf(str, "cheap_stats_new() failed [err=%d]", err); 

      exp = rb_eRuntimeError;
      msg = str;
      break;
    }
  } while (0);

  /*
   * post porcess 
   */
  if (a != NULL) free(a);

  if (exp != Qnil) {
    if (ptr->stats != NULL) {
      cheap_stats_destroy(ptr->stats);
      ptr->stats = NULL;
    }

    rb_raise(exp, msg);
  }

  return self;
}

Instance Method Details

#cdf(x) ⇒ Float

calc CDF

Parameters:

  • x (Numeric)

    taregt value

Returns:

  • (Float)

    CDF value that coresspoinding to target.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'ext/cheap_stats/rb_cheap_stats.c', line 318

static VALUE
rb_cheap_stats_cdf(VALUE self, VALUE x)
{
  rb_cheap_stats_t* ptr;
  int err;
  double ret;

  /*
   * strip context data
   */
  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  /*
   * check argument
   */
  err = cheap_stats_cdf(ptr->stats, rb_num2dbl(x), &ret);
  if (err) {
    RUNTIME_ERROR("cheap_stats_cdf() failed [err=%d]", err);
  }

  return DBL2NUM(ret);
}

#central_moment(k) ⇒ Float

calc central moment

Parameters:

  • k (Numeric)

    order number

Returns:

  • (Float)

    moment value



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'ext/cheap_stats/rb_cheap_stats.c', line 378

static VALUE
rb_cheap_stats_central_moment(VALUE self, VALUE k)
{
  rb_cheap_stats_t* ptr;
  int err;
  double ret;

  /*
   * strip context data
   */
  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  /*
   * check argument
   */
  err = cheap_stats_central_moment(ptr->stats, rb_num2dbl(k), &ret);
  if (err) {
    RUNTIME_ERROR("cheap_stats_central_moment() failed [err=%d]", err);
  }

  return DBL2NUM(ret);
}

#maxFloat

get max value of samples

Returns:

  • (Float)

    max value



226
227
228
229
230
231
232
233
234
# File 'ext/cheap_stats/rb_cheap_stats.c', line 226

static VALUE
rb_cheap_stats_max(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->max);
}

#meanFloat

get mean of samples

Returns:

  • (Float)

    mean



196
197
198
199
200
201
202
203
204
# File 'ext/cheap_stats/rb_cheap_stats.c', line 196

static VALUE
rb_cheap_stats_mean(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->mean);
}

#medianFloat

get median of samples

Returns:

  • (Float)

    median



271
272
273
274
275
276
277
278
279
# File 'ext/cheap_stats/rb_cheap_stats.c', line 271

static VALUE
rb_cheap_stats_median(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->median);
}

#minFloat

get min value of samples

Returns:

  • (Float)

    min value



211
212
213
214
215
216
217
218
219
# File 'ext/cheap_stats/rb_cheap_stats.c', line 211

static VALUE
rb_cheap_stats_min(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->min);
}

#moment(k) ⇒ Float

calc moment

Parameters:

  • k (Numeric)

    order number

Returns:

  • (Float)

    moment value



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'ext/cheap_stats/rb_cheap_stats.c', line 348

static VALUE
rb_cheap_stats_moment(VALUE self, VALUE k)
{
  rb_cheap_stats_t* ptr;
  int err;
  double ret;

  /*
   * strip context data
   */
  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  /*
   * check argument
   */
  err = cheap_stats_moment(ptr->stats, rb_num2dbl(k), &ret);
  if (err) {
    RUNTIME_ERROR("cheap_stats_moment() failed [err=%d]", err);
  }

  return DBL2NUM(ret);
}

#pearson_skewnessFloat

calc pearson median skewness value

Returns:

  • (Float)

    skewness value



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'ext/cheap_stats/rb_cheap_stats.c', line 464

static VALUE
rb_cheap_stats_pearson_skewness(VALUE self)
{
  rb_cheap_stats_t* ptr;
  int err;
  double ret;

  /*
   * strip context data
   */
  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  /*
   * check argument
   */
  err = cheap_stats_pearson_skewness(ptr->stats,  &ret);
  if (err) {
    RUNTIME_ERROR("cheap_stats_pearson_skewness() failed [err=%d]", err);
  }

  return DBL2NUM(ret);
}

#q1Float

get 1/4 quartile value of samples

Returns:

  • (Float)

    1/4 quartile value



241
242
243
244
245
246
247
248
249
# File 'ext/cheap_stats/rb_cheap_stats.c', line 241

static VALUE
rb_cheap_stats_q1(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->q1);
}

#q3Float

get 3/4 quartile value of samples

Returns:

  • (Float)

    3/4 quartile value



256
257
258
259
260
261
262
263
264
# File 'ext/cheap_stats/rb_cheap_stats.c', line 256

static VALUE
rb_cheap_stats_q3(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->q3);
}

#skewnessFloat

calc skewness value

Returns:

  • (Float)

    skewness value



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'ext/cheap_stats/rb_cheap_stats.c', line 436

static VALUE
rb_cheap_stats_skewness(VALUE self)
{
  rb_cheap_stats_t* ptr;
  int err;
  double ret;

  /*
   * strip context data
   */
  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  /*
   * check argument
   */
  err = cheap_stats_skewness(ptr->stats,  &ret);
  if (err) {
    RUNTIME_ERROR("cheap_stats_skewness() failed [err=%d]", err);
  }

  return DBL2NUM(ret);
}

#stdFloat

get standard division of samples

Returns:

  • (Float)

    standard division



286
287
288
289
290
291
292
293
294
# File 'ext/cheap_stats/rb_cheap_stats.c', line 286

static VALUE
rb_cheap_stats_std(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->std);
}

#std_moment(k) ⇒ Float

calc std (standard division) moment

Parameters:

  • k (Numeric)

    order number

Returns:

  • (Float)

    moment value



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'ext/cheap_stats/rb_cheap_stats.c', line 408

static VALUE
rb_cheap_stats_std_moment(VALUE self, VALUE k)
{
  rb_cheap_stats_t* ptr;
  int err;
  double ret;

  /*
   * strip context data
   */
  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  /*
   * check argument
   */
  err = cheap_stats_std_moment(ptr->stats, rb_num2dbl(k), &ret);
  if (err) {
    RUNTIME_ERROR("cheap_stats_std_moment() failed [err=%d]", err);
  }

  return DBL2NUM(ret);
}

#totalFloat

get total value of samples

Returns:

  • (Float)

    total value



181
182
183
184
185
186
187
188
189
# File 'ext/cheap_stats/rb_cheap_stats.c', line 181

static VALUE
rb_cheap_stats_total(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->total);
}

#varianceFloat

get variance of samples

Returns:

  • (Float)

    variance



301
302
303
304
305
306
307
308
309
# File 'ext/cheap_stats/rb_cheap_stats.c', line 301

static VALUE
rb_cheap_stats_variance(VALUE self)
{
  rb_cheap_stats_t* ptr;

  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  return DBL2NUM(ptr->stats->variance);
}

#z_score(v) ⇒ Float

calc Z-score

Parameters:

  • v (Numeric)

    target value

Returns:

  • (Float)

    skewness value



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'ext/cheap_stats/rb_cheap_stats.c', line 494

static VALUE
rb_cheap_stats_z_score(VALUE self, VALUE v)
{
  rb_cheap_stats_t* ptr;
  int err;
  double ret;

  /*
   * strip context data
   */
  TypedData_Get_Struct(self, rb_cheap_stats_t, &rb_cheap_stats_data_type, ptr);

  /*
   * check argument
   */
  err = cheap_stats_z_score(ptr->stats, rb_num2dbl(v), &ret);
  if (err) {
    RUNTIME_ERROR("cheap_stats_z_score() failed [err=%d]", err);
  }

  return DBL2NUM(ret);
}