stat_c 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd7506aea370251a6f91d5e6b6dd7dec6f8e6662
4
- data.tar.gz: 17bc1d29da62b1a7308c054d832a089cc0ae4d2a
3
+ metadata.gz: d1b08af1cb1a9851bd0f686e5c07caaba4c54c7f
4
+ data.tar.gz: bb31ab302a6c65a1c52d6d24a1e244505f02a6ac
5
5
  SHA512:
6
- metadata.gz: 07d8a9391e1c0c24bc8f52fd85019996e3fde773fb6e4c487d0656ce4b7d2039d117b1b424941c2fb405a21866c7fefc2c09465d2cb371235af43c1aa5c7528c
7
- data.tar.gz: 6b374a44fa989f01c1c6369c6358b3255418325500a11ea80855226e147a796e338e05bf6d3399ec2a7cbc57fb75c44f3132c062a237f00be72451efc08b96ca
6
+ metadata.gz: 61cfacdfb80f683eb7731c56544ec2351ed951a612b916ec27f060055b2860ae26d1d62787e1f44ed7ee94a8c33844c7be6808967078c0773e725a1d281199b3
7
+ data.tar.gz: 7db9a53a35b78cc6e918d0d060697c78ea2a690394b83bfeb742f745fe0fbb93a2606469872e170a22ea7f5ef2256f79505d8d0178d2169855b90c04bcef033b
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  lib/stat_c/stat_c.bundle
11
+ todo.md
data/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  Fast, well documented C stats extension for Ruby.
6
6
 
7
+ Pronounce it like "Statsie" and you will feel all warm and fuzzy inside!
8
+
7
9
  ## Installation
8
10
 
9
11
  Add this line to your application's Gemfile:
@@ -26,10 +26,14 @@ along with StatC. If not, see <http://www.gnu.org/licenses/>.
26
26
  /* based on NIL_P in ruby.h */
27
27
  #define FALSE_P(v) !((VALUE)(v) != Qfalse)
28
28
 
29
- /* classes and modules */
29
+ /* modules */
30
30
  VALUE sc_mStatC;
31
31
  VALUE sc_mArray;
32
32
  VALUE sc_mError;
33
+ VALUE sc_mTest;
34
+ VALUE sc_mT;
35
+
36
+ /* classes */
33
37
  VALUE sc_eError;
34
38
 
35
39
  /* @private */
@@ -185,6 +189,112 @@ static VALUE sc_se(int argc, VALUE* argv, VALUE obj)
185
189
  return DBL2NUM(sd / sqrt(len));
186
190
  }
187
191
 
192
+ /* Calculate Welch's t statistic
193
+
194
+ @param mean1 [Numeric] mean of sample 1
195
+ @param var1 [Numeric] sample variance of sample 1
196
+ @param len1 [Numeric] size of sample 1
197
+ @param mean2 [Numeric] mean of sample 2
198
+ @param var2 [Numeric] sample variance of sample 2
199
+ @param len1 [Numeric] size of sample 2
200
+
201
+ @example Get Welch's t stat for two samples
202
+ StatC::Test::T.t_stat_welch(-0.1, 2.3, 5, 2.42, 1.282, 5).round(2) #=> -2.98
203
+
204
+ @raise [StatC::Error::Error] if divide by zero error
205
+
206
+ @return [Numeric] Welch's t statistic
207
+
208
+ */
209
+ static VALUE
210
+ sc_t_stat_welch(VALUE obj,
211
+ VALUE mean1, VALUE var1, VALUE len1,
212
+ VALUE mean2, VALUE var2, VALUE len2)
213
+ {
214
+ double m1, v1, l1, m2, v2, l2, val;
215
+
216
+ m1 = NUM2DBL(mean1);
217
+ v1 = NUM2DBL(var1);
218
+ l1 = NUM2DBL(len1);
219
+
220
+ m2 = NUM2DBL(mean2);
221
+ v2 = NUM2DBL(var2);
222
+ l2 = NUM2DBL(len2);
223
+
224
+ if (l1 <= 0 || l2 <= 0) {
225
+ rb_raise(sc_eError, "Sample sizes must be > 0");
226
+ }
227
+
228
+ val = (v1 / l1) + (v2 / l2);
229
+
230
+ if (val == 0.0) {
231
+ rb_raise(sc_eError, "Divide by zero error");
232
+ }
233
+
234
+ return DBL2NUM((m1 - m2) / sqrt(val));
235
+ }
236
+
237
+ /* Calculate degrees of freedom for Welch's t statistic
238
+
239
+ @note Uses non pooled variance
240
+
241
+ @param var1 [Numeric] sample variance of sample 1
242
+ @param len1 [Numeric] size of sample 1
243
+ @param var2 [Numeric] sample variance of sample 2
244
+ @param len1 [Numeric] size of sample 2
245
+
246
+ @example Get dof for two samples
247
+ StatC::Test::T.dof_welch(2.3, 5, 1.282, 5).round(2) #=> 7.40
248
+
249
+ @raise [StatC::Error::Error] if divide by zero error
250
+
251
+ @return [Numeric] dof
252
+
253
+ */
254
+ static VALUE
255
+ sc_dof_welch(VALUE obj,
256
+ VALUE var1, VALUE len1,
257
+ VALUE var2, VALUE len2)
258
+ {
259
+ double v1, l1, v2, l2, num, denom, val, d1, d2;
260
+
261
+ v1 = NUM2DBL(var1);
262
+ l1 = NUM2DBL(len1);
263
+
264
+ v2 = NUM2DBL(var2);
265
+ l2 = NUM2DBL(len2);
266
+
267
+ if (l1 <= 0 || l2 <= 0) {
268
+ rb_raise(sc_eError, "Sample sizes must be > 0");
269
+ }
270
+
271
+ num = pow( (v1 / l1) + (v2 / l2), 2);
272
+
273
+ d1 = pow(l1, 2) * (l1 - 1);
274
+ d2 = pow(l2, 2) * (l2 - 1);
275
+
276
+ if (d1 == 0|| d2 == 0) {
277
+ rb_raise(sc_eError, "Divide by zero error");
278
+ }
279
+
280
+ denom =
281
+ (pow(v1, 2) / d1) + (pow(v2, 2) / d2);
282
+
283
+ if (denom == 0) {
284
+ rb_raise(sc_eError, "Divide by zero error");
285
+ }
286
+
287
+ val = num / denom;
288
+
289
+ return DBL2NUM(val);
290
+
291
+ }
292
+ /*********************************************************************
293
+
294
+ Initializers
295
+
296
+ *********************************************************************/
297
+
188
298
  /* Document-module: StatC::Array
189
299
 
190
300
  Statistical methods operating on the values of an array
@@ -225,6 +335,30 @@ static void sc_init_eError(void)
225
335
  sc_eError = rb_define_class_under(sc_mError, "Error", rb_eStandardError);
226
336
  }
227
337
 
338
+ /* Document-module: StatC::Test
339
+
340
+ Module containing various statistical tests.
341
+
342
+ */
343
+ static void sc_init_mTest(void)
344
+ {
345
+ sc_mTest = rb_define_module_under(sc_mStatC, "Test");
346
+ }
347
+
348
+
349
+ /* Document-module: StatC::Test::T
350
+
351
+ Module containing T test methods.
352
+
353
+ */
354
+ static void sc_init_mT(void)
355
+ {
356
+ sc_mT = rb_define_module_under(sc_mTest, "T");
357
+
358
+ rb_define_singleton_method(sc_mT, "t_stat_welch", sc_t_stat_welch, 6);
359
+ rb_define_singleton_method(sc_mT, "dof_welch", sc_dof_welch, 4);
360
+ }
361
+
228
362
  /* Document-module: StatC
229
363
 
230
364
  C stats module for Ruby.
@@ -235,6 +369,10 @@ void Init_stat_c(void)
235
369
  sc_mStatC = rb_define_module("StatC");
236
370
 
237
371
  sc_init_mArray();
372
+
238
373
  sc_init_mError();
239
374
  sc_init_eError();
375
+
376
+ sc_init_mTest();
377
+ sc_init_mT();
240
378
  }
@@ -1,4 +1,4 @@
1
1
  module StatC
2
2
  # module version number
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stat_c
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Moore
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-04 00:00:00.000000000 Z
11
+ date: 2016-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler