Module: Dispersion

Included in:
Array
Defined in:
ext/ruby_native_statistics/ruby_native_statistics.c

Instance Method Summary collapse

Instance Method Details

#percentile(r_percentile) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/ruby_native_statistics/dispersion.c', line 67

VALUE rb_percentile(VALUE self, VALUE r_percentile)
{
  double result;
  Check_Type(self, T_ARRAY);

  long array_length = rb_array_len(self);
  double percentile = NUM2DBL(r_percentile);

  if (array_length == 0)
  {
    rb_raise(rb_eRangeError, "array must have at least one element");
  }

  if (percentile < 0 || percentile > 1)
  {
    rb_raise(rb_eRangeError, "percentile must be between 0 and 1 inclusive");
  }

  double *sorted_array = sorted_ruby_array(self, array_length);

  double h = (array_length - 1) * percentile + 1;

  if (trunc(h) == h)
  {
    result = sorted_array[(long)h - 1];
  }
  else
  {
    long h_floor = (long)trunc(h);
    result = (h - h_floor) * (sorted_array[h_floor] - sorted_array[h_floor - 1]) + sorted_array[h_floor - 1];
  }

  free(sorted_array);

  return DBL2NUM(result);
}

#stdevObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'ext/ruby_native_statistics/dispersion.c', line 3

VALUE rb_sample_standard_deviation(VALUE self)
{
  unsigned int array_length;

  Check_Type(self, T_ARRAY);

  array_length = rb_long2int(RARRAY_LEN(self));

  if (array_length <= 1)
  {
    rb_raise(rb_eRangeError, "array must have more than one element");
  }

  return DBL2NUM(sqrt((calculate_total_distance_from_mean(self, array_length) / (array_length - 1))));
}

#stdevpObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'ext/ruby_native_statistics/dispersion.c', line 35

VALUE rb_population_standard_deviation(VALUE self)
{
  unsigned int array_length;

  Check_Type(self, T_ARRAY);

  array_length = rb_long2int(RARRAY_LEN(self));

  if (array_length <= 1)
  {
    rb_raise(rb_eRangeError, "array must have more than one element");
  }

  return DBL2NUM(sqrt(calculate_total_distance_from_mean(self, array_length) / array_length));
}

#stdevsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'ext/ruby_native_statistics/dispersion.c', line 3

VALUE rb_sample_standard_deviation(VALUE self)
{
  unsigned int array_length;

  Check_Type(self, T_ARRAY);

  array_length = rb_long2int(RARRAY_LEN(self));

  if (array_length <= 1)
  {
    rb_raise(rb_eRangeError, "array must have more than one element");
  }

  return DBL2NUM(sqrt((calculate_total_distance_from_mean(self, array_length) / (array_length - 1))));
}

#varObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'ext/ruby_native_statistics/dispersion.c', line 19

VALUE rb_sample_variance(VALUE self)
{
  unsigned int array_length;

  Check_Type(self, T_ARRAY);

  array_length = rb_long2int(RARRAY_LEN(self));

  if (array_length <= 1)
  {
    rb_raise(rb_eRangeError, "array must have more than one element");
  }

  return DBL2NUM((calculate_total_distance_from_mean(self, array_length) / (array_length - 1)));
}

#varpObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/ruby_native_statistics/dispersion.c', line 51

VALUE rb_population_variance(VALUE self)
{
  unsigned int array_length;

  Check_Type(self, T_ARRAY);

  array_length = rb_long2int(RARRAY_LEN(self));

  if (array_length <= 1)
  {
    rb_raise(rb_eRangeError, "array must have more than one element");
  }

  return DBL2NUM(calculate_total_distance_from_mean(self, array_length) / array_length);
}