statistics3 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.semver +5 -0
- data/.travis.yml +9 -0
- data/Gemfile +22 -0
- data/Gemfile.lock +127 -0
- data/History.rdoc +12 -0
- data/LICENSE.txt +20 -0
- data/README.org +38 -0
- data/Rakefile +73 -0
- data/examples/mklist.rb +18 -0
- data/examples/show.rb +12 -0
- data/ext/Makefile +260 -0
- data/ext/_statistics3.c +824 -0
- data/ext/_statistics3.o +0 -0
- data/ext/_statistics3.so +0 -0
- data/ext/extconf.rb +2 -0
- data/lib/statistics3/base.rb +525 -0
- data/lib/statistics3/no_ext.rb +6 -0
- data/lib/statistics3.rb +13 -0
- data/spec/spec_helper.rb +103 -0
- data/statistics3.gemspec +117 -0
- data/test/sample_tbl.rb +129 -0
- data/test/test_ext.rb +46 -0
- data/test/test_inv.rb +53 -0
- metadata +313 -0
data/ext/_statistics3.o
ADDED
Binary file
|
data/ext/_statistics3.so
ADDED
Binary file
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,525 @@
|
|
1
|
+
module Statistics3
|
2
|
+
module Base
|
3
|
+
SQ2PI = Math.sqrt(2 * Math::PI)
|
4
|
+
|
5
|
+
LOG_2PI = Math.log(2 * Math::PI) # log(2PI)
|
6
|
+
N = 8
|
7
|
+
B0 = 1.0
|
8
|
+
B1 = -1.0 / 2.0
|
9
|
+
B2 = 1.0 / 6.0
|
10
|
+
B4 = -1.0 / 30.0
|
11
|
+
B6 = 1.0 / 42.0
|
12
|
+
B8 = -1.0 / 30.0
|
13
|
+
B10 = 5.0 / 66.0
|
14
|
+
B12 = -691.0 / 2730.0
|
15
|
+
B14 = 7.0 / 6.0
|
16
|
+
B16 = -3617.0 / 510.0
|
17
|
+
|
18
|
+
# Newton approximation
|
19
|
+
def newton_a(y, ini, epsilon = 1.0e-6, limit = 30)
|
20
|
+
x = ini
|
21
|
+
limit.times do |i|
|
22
|
+
prev = x
|
23
|
+
f, df = yield(prev)
|
24
|
+
x = (y - f)/df + prev
|
25
|
+
if (x - prev).abs < epsilon
|
26
|
+
return x
|
27
|
+
end
|
28
|
+
end
|
29
|
+
$stderr.puts("Warning(newton approximation): over limit")
|
30
|
+
x
|
31
|
+
end
|
32
|
+
|
33
|
+
# Gamma function
|
34
|
+
def loggamma(x)
|
35
|
+
v = 1.0
|
36
|
+
while (x < N)
|
37
|
+
v *= x
|
38
|
+
x += 1.0
|
39
|
+
end
|
40
|
+
w = 1.0 / (x * x)
|
41
|
+
ret = B16 / (16 * 15)
|
42
|
+
ret = ret * w + B14 / (14 * 13)
|
43
|
+
ret = ret * w + B12 / (12 * 11)
|
44
|
+
ret = ret * w + B10 / (10 * 9)
|
45
|
+
ret = ret * w + B8 / ( 8 * 7)
|
46
|
+
ret = ret * w + B6 / ( 6 * 5)
|
47
|
+
ret = ret * w + B4 / ( 4 * 3)
|
48
|
+
ret = ret * w + B2 / ( 2 * 1)
|
49
|
+
ret = ret / x + 0.5 * LOG_2PI - Math.log(v) - x + (x - 0.5) * Math.log(x)
|
50
|
+
ret
|
51
|
+
end
|
52
|
+
|
53
|
+
def gamma(x)
|
54
|
+
if (x < 0.0)
|
55
|
+
return Math::PI / (Math.sin(Math.PI * x) * Math.exp(loggamma(1 - x))) #/
|
56
|
+
end
|
57
|
+
Math.exp(loggamma(x))
|
58
|
+
end
|
59
|
+
|
60
|
+
#normal-distribution
|
61
|
+
# (-\infty, z]
|
62
|
+
def p_nor(z)
|
63
|
+
if z < -12 then return 0.0 end
|
64
|
+
if z > 12 then return 1.0 end
|
65
|
+
if z == 0.0 then return 0.5 end
|
66
|
+
|
67
|
+
if z > 0.0
|
68
|
+
e = true
|
69
|
+
else
|
70
|
+
e = false
|
71
|
+
z = -z
|
72
|
+
end
|
73
|
+
z = z.to_f
|
74
|
+
z2 = z * z
|
75
|
+
t = q = z * Math.exp(-0.5 * z2) / SQ2PI
|
76
|
+
|
77
|
+
3.step(199, 2) do |i|
|
78
|
+
prev = q
|
79
|
+
t *= z2 / i
|
80
|
+
q += t
|
81
|
+
if q <= prev
|
82
|
+
return(e ? 0.5 + q : 0.5 - q)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
e ? 1.0 : 0.0
|
86
|
+
end
|
87
|
+
|
88
|
+
# inverse of normal distribution ([2])
|
89
|
+
# Pr( (-\infty, x] ) = qn -> x
|
90
|
+
def pnorm(qn)
|
91
|
+
b = [1.570796288, 0.03706987906, -0.8364353589e-3,
|
92
|
+
-0.2250947176e-3, 0.6841218299e-5, 0.5824238515e-5,
|
93
|
+
-0.104527497e-5, 0.8360937017e-7, -0.3231081277e-8,
|
94
|
+
0.3657763036e-10, 0.6936233982e-12]
|
95
|
+
|
96
|
+
if(qn < 0.0 || 1.0 < qn)
|
97
|
+
$stderr.printf("Error : qn <= 0 or qn >= 1 in pnorm()!\n")
|
98
|
+
return 0.0;
|
99
|
+
end
|
100
|
+
qn == 0.5 and return 0.0
|
101
|
+
|
102
|
+
w1 = qn
|
103
|
+
qn > 0.5 and w1 = 1.0 - w1
|
104
|
+
w3 = -Math.log(4.0 * w1 * (1.0 - w1))
|
105
|
+
w1 = b[0]
|
106
|
+
1.upto 10 do |i|
|
107
|
+
w1 += b[i] * w3**i;
|
108
|
+
end
|
109
|
+
qn > 0.5 and return Math.sqrt(w1 * w3)
|
110
|
+
-Math.sqrt(w1 * w3)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Returns the integral of normal distribution over (-Infty, x].
|
114
|
+
def normaldist(z)
|
115
|
+
p_nor(z)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Returns the P-value of normaldist(x).
|
119
|
+
def pnormaldist(y)
|
120
|
+
pnorm(y)
|
121
|
+
end
|
122
|
+
|
123
|
+
#chi-square distribution ([1])
|
124
|
+
#[x, \infty)
|
125
|
+
def q_chi2(df, chi2)
|
126
|
+
chi2 = chi2.to_f
|
127
|
+
if (df & 1) != 0
|
128
|
+
chi = Math.sqrt(chi2)
|
129
|
+
if (df == 1) then return 2 * normal___x(chi); end
|
130
|
+
s = t = chi * Math.exp(-0.5 * chi2) / SQ2PI
|
131
|
+
k = 3
|
132
|
+
while k < df
|
133
|
+
t *= chi2 / k; s += t;
|
134
|
+
k += 2
|
135
|
+
end
|
136
|
+
2 * (normal___x(chi) + s)
|
137
|
+
else
|
138
|
+
s = t = Math.exp(-0.5 * chi2)
|
139
|
+
k = 2
|
140
|
+
while k < df
|
141
|
+
t *= chi2 / k; s += t;
|
142
|
+
k += 2
|
143
|
+
end
|
144
|
+
s
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def chi2dens(n, x)
|
149
|
+
if n == 1
|
150
|
+
1.0/Math.sqrt(2 * Math::PI * x) * Math::E**(-x/2.0)
|
151
|
+
elsif n == 2
|
152
|
+
0.5 * Math::E**(-x/2.0)
|
153
|
+
else
|
154
|
+
n = n.to_f
|
155
|
+
n2 = n/2
|
156
|
+
x = x.to_f
|
157
|
+
1.0 / 2**n2 / gamma(n2) * x**(n2 - 1.0) * Math.exp(-x/2.0)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
# [x, \infty)
|
162
|
+
# Pr([x, \infty)) = y -> x
|
163
|
+
def pchi2(n, y)
|
164
|
+
if n == 1
|
165
|
+
w = pnorm(1 - y/2) # = pnormal___x(y/2)
|
166
|
+
w * w
|
167
|
+
elsif n == 2
|
168
|
+
# v = (1.0 / y - 1.0) / 33.0
|
169
|
+
# newton_a(y, v) {|x| [q_chi2(n, x), -chi2dens(n, x)] }
|
170
|
+
-2.0 * Math.log(y)
|
171
|
+
else
|
172
|
+
eps = 1.0e-5
|
173
|
+
v = 0.0
|
174
|
+
s = 10.0
|
175
|
+
loop do
|
176
|
+
v += s
|
177
|
+
if s <= eps then break end
|
178
|
+
if (qe = q_chi2(n, v) - y) == 0.0 then break end
|
179
|
+
if qe < 0.0
|
180
|
+
v -= s
|
181
|
+
s /= 10.0 #/
|
182
|
+
end
|
183
|
+
end
|
184
|
+
v
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# Returns the integral of Chi-squared distribution with n degrees of freedom over [0, x].
|
189
|
+
def chi2dist(n, x); 1.0 - q_chi2(n, x); end
|
190
|
+
|
191
|
+
# Returns the P-value of chi2dist().
|
192
|
+
def pchi2dist(n, y); pchi2(n, 1.0 - y); end
|
193
|
+
|
194
|
+
# t-distribution ([1])
|
195
|
+
# (-\infty, x]
|
196
|
+
def p_t(df, t)
|
197
|
+
c2 = df.to_f / (df + t * t);
|
198
|
+
s = Math.sqrt(1.0 - c2)
|
199
|
+
s = -s if t < 0.0
|
200
|
+
p = 0.0;
|
201
|
+
i = df % 2 + 2
|
202
|
+
while i <= df
|
203
|
+
p += s
|
204
|
+
s *= (i - 1) * c2 / i
|
205
|
+
i += 2
|
206
|
+
end
|
207
|
+
if df & 1 != 0
|
208
|
+
0.5+(p*Math.sqrt(c2)+Math.atan(t/Math.sqrt(df)))/Math::PI
|
209
|
+
else
|
210
|
+
(1.0 + p) / 2.0
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
# inverse of t-distribution ([2])
|
215
|
+
# (-\infty, -q/2] + [q/2, \infty)
|
216
|
+
def ptsub(q, n)
|
217
|
+
q = q.to_f
|
218
|
+
if(n == 1 && 0.001 < q && q < 0.01)
|
219
|
+
eps = 1.0e-4
|
220
|
+
elsif (n == 2 && q < 0.0001)
|
221
|
+
eps = 1.0e-4
|
222
|
+
elsif (n == 1 && q < 0.001)
|
223
|
+
eps = 1.0e-2
|
224
|
+
else
|
225
|
+
eps = 1.0e-5
|
226
|
+
end
|
227
|
+
s = 10000.0
|
228
|
+
w = 0.0
|
229
|
+
loop do
|
230
|
+
w += s
|
231
|
+
if(s <= eps) then return w end
|
232
|
+
if((qe = 2.0 - p_t(n, w)*2.0 - q) == 0.0) then return w end
|
233
|
+
if(qe < 0.0)
|
234
|
+
w -= s
|
235
|
+
s /= 10.0 #/
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
def pt(q, n)
|
241
|
+
q = q.to_f
|
242
|
+
if(q < 1.0e-5 || q > 1.0 || n < 1)
|
243
|
+
$stderr.printf("Error : Illigal parameter in pt()!\n")
|
244
|
+
return 0.0
|
245
|
+
end
|
246
|
+
|
247
|
+
if(n <= 5) then return ptsub(q, n) end
|
248
|
+
if(q <= 5.0e-3 && n <= 13) then return ptsub(q, n) end
|
249
|
+
|
250
|
+
f1 = 4.0 * (f = n.to_f)
|
251
|
+
f5 = (f4 = (f3 = (f2 = f * f) * f) * f) * f
|
252
|
+
f2 *= 96.0
|
253
|
+
f3 *= 384.0
|
254
|
+
f4 *= 92160.0
|
255
|
+
f5 *= 368640.0
|
256
|
+
u = pnormaldist(1.0 - q / 2.0)
|
257
|
+
|
258
|
+
w0 = (u2 = u * u) * u
|
259
|
+
w1 = w0 * u2
|
260
|
+
w2 = w1 * u2
|
261
|
+
w3 = w2 * u2
|
262
|
+
w4 = w3 * u2
|
263
|
+
w = (w0 + u) / f1
|
264
|
+
w += (5.0 * w1 + 16.0 * w0 + 3.0 * u) / f2
|
265
|
+
w += (3.0 * w2 + 19.0 * w1 + 17.0 * w0 - 15.0 * u) / f3
|
266
|
+
w += (79.0 * w3 + 776.0 * w2 + 1482.0 * w1 - 1920.0 * w0 - 9450.0 * u) / f4
|
267
|
+
w += (27.0 * w4 + 339.0 * w3 + 930.0 * w2 - 1782.0 * w1 - 765.0 * w0 + 17955.0 * u) / f5
|
268
|
+
u + w
|
269
|
+
end
|
270
|
+
|
271
|
+
# Returns the integral of t-distribution with n degrees of freedom over (-Infty, x].
|
272
|
+
def tdist(n, t); p_t(n, t); end
|
273
|
+
|
274
|
+
# Returns the P-value of tdist().
|
275
|
+
def ptdist(n, y)
|
276
|
+
if y > 0.5
|
277
|
+
pt(2.0 - y*2.0, n)
|
278
|
+
else
|
279
|
+
- pt(y*2.0, n)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
# F-distribution ([1])
|
284
|
+
# [x, \infty)
|
285
|
+
def q_f(df1, df2, f)
|
286
|
+
if (f <= 0.0) then return 1.0; end
|
287
|
+
if (df1 % 2 != 0 && df2 % 2 == 0)
|
288
|
+
return 1.0 - q_f(df2, df1, 1.0 / f)
|
289
|
+
end
|
290
|
+
cos2 = 1.0 / (1.0 + df1.to_f * f / df2.to_f)
|
291
|
+
sin2 = 1.0 - cos2
|
292
|
+
|
293
|
+
if (df1 % 2 == 0)
|
294
|
+
prob = cos2 ** (df2.to_f / 2.0)
|
295
|
+
temp = prob
|
296
|
+
i = 2
|
297
|
+
while i < df1
|
298
|
+
temp *= (df2.to_f + i - 2) * sin2 / i
|
299
|
+
prob += temp
|
300
|
+
i += 2
|
301
|
+
end
|
302
|
+
return prob
|
303
|
+
end
|
304
|
+
prob = Math.atan(Math.sqrt(df2.to_f / (df1.to_f * f)))
|
305
|
+
temp = Math.sqrt(sin2 * cos2)
|
306
|
+
i = 3
|
307
|
+
while i <= df1
|
308
|
+
prob += temp
|
309
|
+
temp *= (i - 1).to_f * sin2 / i.to_f;
|
310
|
+
i += 2.0
|
311
|
+
end
|
312
|
+
temp *= df1.to_f
|
313
|
+
i = 3
|
314
|
+
while i <= df2
|
315
|
+
prob -= temp
|
316
|
+
temp *= (df1.to_f + i - 2) * cos2 / i.to_f
|
317
|
+
i += 2
|
318
|
+
end
|
319
|
+
prob * 2.0 / Math::PI
|
320
|
+
end
|
321
|
+
|
322
|
+
# inverse of F-distribution ([2])
|
323
|
+
def pfsub(x, y, z)
|
324
|
+
(Math.sqrt(z) - y) / x / 2.0
|
325
|
+
end
|
326
|
+
|
327
|
+
# [x, \infty)
|
328
|
+
def pf(q, n1, n2)
|
329
|
+
if(q < 0.0 || q > 1.0 || n1 < 1 || n2 < 1)
|
330
|
+
$stderr.printf("Error : Illegal parameter in pf()!\n")
|
331
|
+
return 0.0
|
332
|
+
end
|
333
|
+
|
334
|
+
if n1 <= 240 || n2 <= 240
|
335
|
+
eps = 1.0e-5
|
336
|
+
if(n2 == 1) then eps = 1.0e-4 end
|
337
|
+
fw = 0.0
|
338
|
+
s = 1000.0
|
339
|
+
loop do
|
340
|
+
fw += s
|
341
|
+
if s <= eps then return fw end
|
342
|
+
if (qe = q_f(n1, n2, fw) - q) == 0.0 then return fw end
|
343
|
+
if qe < 0.0
|
344
|
+
fw -= s
|
345
|
+
s /= 10.0 #/
|
346
|
+
end
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
eps = 1.0e-6
|
351
|
+
qn = q
|
352
|
+
if q < 0.5 then qn = 1.0 - q
|
353
|
+
u = pnorm(qn)
|
354
|
+
w1 = 2.0 / n1 / 9.0
|
355
|
+
w2 = 2.0 / n2 / 9.0
|
356
|
+
w3 = 1.0 - w1
|
357
|
+
w4 = 1.0 - w2
|
358
|
+
u2 = u * u
|
359
|
+
a = w4 * w4 - u2 * w2
|
360
|
+
b = -2. * w3 * w4
|
361
|
+
c = w3 * w3 - u2 * w1
|
362
|
+
d = b * b - 4 * a * c
|
363
|
+
if(d < 0.0)
|
364
|
+
fw = pfsub(a, b, 0.0)
|
365
|
+
else
|
366
|
+
if(a.abs > eps)
|
367
|
+
fw = pfsub(a, b, d)
|
368
|
+
else
|
369
|
+
if(b.abs > eps) then return -c / b end
|
370
|
+
fw = pfsub(a, b, 0.0)
|
371
|
+
end
|
372
|
+
end
|
373
|
+
fw * fw * fw
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
# Returns the integral of F-distribution with n1 and n2 degrees of freedom over [0, x].
|
378
|
+
def fdist(n1, n2, f); 1.0 - q_f(n1, n2, f); end
|
379
|
+
|
380
|
+
# Returns the P-value of fdist().
|
381
|
+
def pfdist(n1, n2, y); pf(1.0 - y, n1, n2); end
|
382
|
+
|
383
|
+
############################################################################
|
384
|
+
# discrete distributions
|
385
|
+
|
386
|
+
def perm(n, x = n)
|
387
|
+
raise RangeError if n < 0 || x < 0
|
388
|
+
r = 1
|
389
|
+
while x >= 1
|
390
|
+
r *= n
|
391
|
+
n -= 1
|
392
|
+
x -= 1
|
393
|
+
end
|
394
|
+
r
|
395
|
+
end
|
396
|
+
|
397
|
+
def combi(n, x)
|
398
|
+
raise RangeError if n < 0 || x < 0
|
399
|
+
x = n - x if x*2 > n
|
400
|
+
perm(n, x) / perm(x, x)
|
401
|
+
end
|
402
|
+
|
403
|
+
def bindens(n, p, x)
|
404
|
+
p = p.to_f
|
405
|
+
q = 1.0 - p
|
406
|
+
combi(n, x) * p**x * q**(n - x)
|
407
|
+
end
|
408
|
+
|
409
|
+
def bindist(n, p, x)
|
410
|
+
(0..x).inject(0.0) do |s, k|
|
411
|
+
s + bindens(n, p, k)
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
def poissondens(m, x)
|
416
|
+
return 0.0 if x < 0
|
417
|
+
m = m.to_f
|
418
|
+
m ** x * Math::E ** (-m) / perm(x)
|
419
|
+
end
|
420
|
+
|
421
|
+
def poissondist(m, x)
|
422
|
+
(0..x).inject(0.0) do |s, k|
|
423
|
+
s + poissondens(m, k)
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
############################################################################
|
428
|
+
# normal-distribution
|
429
|
+
|
430
|
+
# Returns the integral of normal distribution over (-Infty, x].
|
431
|
+
def normalxXX_(z); normaldist(z); end
|
432
|
+
|
433
|
+
# Returns the integral of normal distribution over [0, x].
|
434
|
+
def normal__X_(z); normaldist(z) - 0.5; end
|
435
|
+
|
436
|
+
# Returns the integral of normal distribution over [x, Infty).
|
437
|
+
def normal___x(z); 1.0 - normaldist(z); end
|
438
|
+
|
439
|
+
# Returns the integral of normal distribution over (-Infty, -x] + [x, Infty).
|
440
|
+
def normalx__x(z); 2.0 - normaldist(z) * 2.0; end
|
441
|
+
|
442
|
+
# inverse of normal-distribution
|
443
|
+
|
444
|
+
# Return the P-value of the corresponding integral.
|
445
|
+
def pnormalxXX_(z); pnormaldist(z); end
|
446
|
+
|
447
|
+
# Return the P-value of the corresponding integral.
|
448
|
+
def pnormal__X_(y); pnormalxXX_(y + 0.5); end
|
449
|
+
|
450
|
+
# Return the P-value of the corresponding integral.
|
451
|
+
def pnormal___x(y); pnormalxXX_(1.0 - y); end
|
452
|
+
|
453
|
+
# Return the P-value of the corresponding integral.
|
454
|
+
def pnormalx__x(y); pnormalxXX_(1.0 - y/2.0); end
|
455
|
+
|
456
|
+
# chi2-distribution
|
457
|
+
|
458
|
+
# Returns the integral of Chi-squared distribution with n degrees of freedom over [x, Infty).
|
459
|
+
def chi2_x(n, x); 1.0 - chi2dist(n, x); end
|
460
|
+
|
461
|
+
# Returns the integral of Chi-squared distribution with n degrees of freedom over [0, x].
|
462
|
+
def chi2X_(n, x); chi2dist(n, x); end
|
463
|
+
|
464
|
+
# inverse of chi2-distribution
|
465
|
+
|
466
|
+
# Return the P-value of the corresponding integral.
|
467
|
+
def pchi2_x(n, y); pchi2dist(n, 1.0 - y); end
|
468
|
+
|
469
|
+
# Return the P-value of the corresponding integral.
|
470
|
+
def pchi2X_(n, y); pchi2dist(n, y); end
|
471
|
+
|
472
|
+
# t-distribution
|
473
|
+
|
474
|
+
# Returns the integral of normal distribution with n degrees of freedom over (-Infty, -x] + [x, Infty).
|
475
|
+
def tx__x(n, x); 2.0 - tdist(n, x) * 2.0; end
|
476
|
+
|
477
|
+
# Returns the integral of t-distribution with n degrees of freedom over (-Infty, x].
|
478
|
+
def txXX_(n, x); tdist(n, x); end
|
479
|
+
|
480
|
+
# Returns the integral of t-distribution with n degrees of freedom over [0, x].
|
481
|
+
def t__X_(n, x); tdist(n, x) - 0.5; end
|
482
|
+
|
483
|
+
# Returns the integral of t-distribution with n degrees of freedom over [x, Infty).
|
484
|
+
def t___x(n, x); 1.0 - tdist(n, x); end
|
485
|
+
|
486
|
+
# inverse of t-distribution
|
487
|
+
|
488
|
+
# Return the P-value of the corresponding integral.
|
489
|
+
def ptx__x(n, y); ptdist(n, 1.0 - y / 2.0); end
|
490
|
+
|
491
|
+
# Return the P-value of the corresponding integral.
|
492
|
+
def ptxXX_(n, y); ptdist(n, y); end
|
493
|
+
|
494
|
+
# Return the P-value of the corresponding integral.
|
495
|
+
def pt__X_(n, y); ptdist(n, 0.5 + y); end
|
496
|
+
|
497
|
+
# Return the P-value of the corresponding integral.
|
498
|
+
def pt___x(n, y); ptdist(n, 1.0 - y); end
|
499
|
+
|
500
|
+
# F-distribution
|
501
|
+
|
502
|
+
# Returns the integral of F-distribution with n1 and n2 degrees of freedom over [x, Infty).
|
503
|
+
def f_x(n1, n2, x); 1.0 - fdist(n1, n2, x); end
|
504
|
+
|
505
|
+
# Returns the integral of F-distribution with n1 and n2 degrees of freedom over [0, x].
|
506
|
+
def fX_(n1, n2, x); fdist(n1, n2, x); end
|
507
|
+
|
508
|
+
|
509
|
+
# inverse of F-distribution
|
510
|
+
|
511
|
+
# Return the P-value of the corresponding integral.
|
512
|
+
def pf_x(n1, n2, x); pfdist(n1, n2, 1.0 - x); end
|
513
|
+
|
514
|
+
# Return the P-value of the corresponding integral.
|
515
|
+
def pfX_(n1, n2, x); pfdist(n1, n2, x); end
|
516
|
+
|
517
|
+
# discrete distributions
|
518
|
+
def binX_(n, p, x); bindist(n, p, x); end
|
519
|
+
def bin_x(n, p, x); bindist(n, 1.0 - p, n - x); end
|
520
|
+
|
521
|
+
def poissonX_(m, x); poissondist(m, x); end
|
522
|
+
def poisson_x(m, x); 1.0 - poissondist(m, x-1); end
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
data/lib/statistics3.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
44
|
+
# have no way to turn it off -- the option exists only for backwards
|
45
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
46
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
47
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
48
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
49
|
+
|
50
|
+
# The settings below are suggested to provide a good initial experience
|
51
|
+
# with RSpec, but feel free to customize to your heart's content.
|
52
|
+
=begin
|
53
|
+
# This allows you to limit a spec run to individual examples or groups
|
54
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
55
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
56
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
57
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
58
|
+
config.filter_run_when_matching :focus
|
59
|
+
|
60
|
+
# Allows RSpec to persist some state between runs in order to support
|
61
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
62
|
+
# you configure your source control system to ignore this file.
|
63
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
64
|
+
|
65
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
66
|
+
# recommended. For more details, see:
|
67
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
68
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
69
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
70
|
+
config.disable_monkey_patching!
|
71
|
+
|
72
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
73
|
+
# be too noisy due to issues in dependencies.
|
74
|
+
config.warnings = true
|
75
|
+
|
76
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
77
|
+
# file, and it's useful to allow more verbose output when running an
|
78
|
+
# individual spec file.
|
79
|
+
if config.files_to_run.one?
|
80
|
+
# Use the documentation formatter for detailed output,
|
81
|
+
# unless a formatter has already been configured
|
82
|
+
# (e.g. via a command-line flag).
|
83
|
+
config.default_formatter = 'doc'
|
84
|
+
end
|
85
|
+
|
86
|
+
# Print the 10 slowest examples and example groups at the
|
87
|
+
# end of the spec run, to help surface which specs are running
|
88
|
+
# particularly slow.
|
89
|
+
config.profile_examples = 10
|
90
|
+
|
91
|
+
# Run specs in random order to surface order dependencies. If you find an
|
92
|
+
# order dependency and want to debug it, you can fix the order by providing
|
93
|
+
# the seed, which is printed after each run.
|
94
|
+
# --seed 1234
|
95
|
+
config.order = :random
|
96
|
+
|
97
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
98
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
99
|
+
# test failures related to randomization by passing the same `--seed` value
|
100
|
+
# as the one that triggered the failure.
|
101
|
+
Kernel.srand config.seed
|
102
|
+
=end
|
103
|
+
end
|