bullshit 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,40 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'test/unit'
4
- require 'bullshit'
5
-
6
- class TestContinuedFraction < Test::Unit::TestCase
7
- include Bullshit
8
-
9
- def setup
10
- @none1 = ContinuedFraction.for_a proc {}
11
- @none2 = ContinuedFraction.for_a {}
12
- @phi = ContinuedFraction.new
13
- @finite = ContinuedFraction.for_a [3, 4, 12, 3, 1]
14
- @sqrt_2 = ContinuedFraction.for_a { |n| n == 0 ? 1 : 2 }
15
- @pi_finite = ContinuedFraction.for_a [3, 7, 15, 1, 292, 1, 1, 1, 2]
16
- @e = ContinuedFraction.for_a { |n| (n + 1) }.for_b { |n| (n + 1) }
17
- @a113011 = ContinuedFraction.for_a { |n| 2 * n + 1 }.for_b { |n| 2 * n }
18
- @a073333 = ContinuedFraction.for_a { |n| n }.for_b { |n| n }
19
- @atan = ContinuedFraction.for_a do |n, x|
20
- n == 0 ? 0 : 2 * n - 1
21
- end.for_b do |n, x|
22
- n <= 1 ? x : ((n - 1) * x) ** 2
23
- end
24
- @pi = lambda { 4 * @atan[1] }
25
- end
26
-
27
- def test_continued_fractions
28
- assert @none1[1].nan?
29
- assert @none2[1].nan?
30
- assert_in_delta 1.618033, @phi[], 1E-6
31
- assert_in_delta 3.245, @finite[], 1E-4
32
- assert_in_delta Math.sqrt(2), @sqrt_2[], 1E-10
33
- assert_in_delta Math::PI, @pi_finite[], 1E-10
34
- assert_in_delta Math::E, 1 + @e[], 1E-10
35
- assert_in_delta 1.541494, @a113011[], 1E-6
36
- assert_in_delta 0.581976, @a073333[], 1E-6
37
- assert_in_delta Math.atan(0.5), @atan[0.5], 1E-10
38
- assert_in_delta Math::PI, @pi[], 1E-10
39
- end
40
- end
@@ -1,69 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'test/unit'
4
- require 'bullshit'
5
-
6
- class TestDistribution < Test::Unit::TestCase
7
- include Bullshit
8
-
9
- def setup
10
- @td23 = TDistribution.new 23
11
- @td100 = TDistribution.new 100
12
- @chi23 = ChiSquareDistribution.new 23
13
- @chi100 = ChiSquareDistribution.new 100
14
- end
15
-
16
- def test_tdistribution
17
- xs = [ 75, 80, 85, 90, 95, 97.5, 99, 99.5, 99.75, 99.9, 99.95 ].map { |x|
18
- x / 100.0 }
19
- ys = [ 0.685, 0.858, 1.060, 1.319, 1.714, 2.069, 2.500, 2.807, 3.104,
20
- 3.485, 3.767 ]
21
- xs.zip(ys) do |x, y|
22
- assert_in_delta y, @td23.inverse_probability(x), 1E-2
23
- end
24
- assert_equal @td23.inverse_probability(-0.1), -1 / 0.0
25
- assert_equal @td23.inverse_probability(1.1), 1 / 0.0
26
- ys = [ 0.677, 0.845, 1.042, 1.290, 1.660, 1.984, 2.364, 2.626, 2.871,
27
- 3.174, 3.390 ]
28
- xs.zip(ys) do |x, y|
29
- assert_in_delta y, @td100.inverse_probability(x), 1E-2
30
- end
31
- assert_equal @td100.inverse_probability(-0.1), -1 / 0.0
32
- assert_equal @td100.inverse_probability(1.1), 1 / 0.0
33
- end
34
-
35
- def test_standard_normal_distribution
36
- std = STD_NORMAL_DISTRIBUTION
37
- ps = [ 0.001, 0.005, 0.010, 0.025, 0.050, 0.100 ]
38
- zs = [ -3.090, -2.576, -2.326, -1.960, -1.645, -1.282 ]
39
- ps.zip(zs) do |p, z|
40
- assert_in_delta z, std.inverse_probability(p), 1E-2
41
- end
42
- assert_equal std.inverse_probability(-0.1), -1 / 0.0
43
- assert_equal std.inverse_probability(1.1), 1 / 0.0
44
- ps.zip(zs) do |p, z|
45
- assert_in_delta(-z, std.inverse_probability(1 - p), 1E-2)
46
- end
47
- end
48
-
49
- def test_chisquaredistribution
50
- xs = [ 0.0, 1, 9.260, 11.689, 28.429, 32.007, 35.172, 38.076, 38.968, 41.638,
51
- 44.181, 47.391, 49.728 ]
52
- ys = [ 0.0, 1.593887e-12, 0.004998304, 0.025006129, 0.800007427, 0.900002084, 0.949994698,
53
- 0.975002306, 0.979998428, 0.989998939, 0.994999617, 0.997999729,
54
- 0.998999930 ]
55
- xs.zip(ys) do |x, y|
56
- assert_in_delta y, @chi23.probability(x), 1E-6
57
- assert_in_delta x, @chi23.inverse_probability(y), 1E-6
58
- end
59
- assert_in_delta @chi23.probability(-0.1), 0.0, 1E-6
60
- ys = [ 0.0, 1.788777e-80, 6.705663e-34, 2.337654e-29, 1.321036e-13, 8.695875e-12,
61
- 2.085529e-10, 2.691731e-09, 5.559949e-09, 4.192445e-08, 2.373762e-07,
62
- 1.678744e-06, 6.034997e-06 ]
63
- xs.zip(ys) do |x, y|
64
- assert_in_delta y, @chi100.probability(x), 1E-6
65
- assert_in_delta x, @chi100.inverse_probability(y), 1E-6
66
- end
67
- assert_in_delta @chi100.probability(-0.1), 0.0, 1E-6
68
- end
69
- end
@@ -1,33 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'test/unit'
4
- require 'bullshit'
5
-
6
- class TestFunctions < Test::Unit::TestCase
7
- include Bullshit::Functions
8
-
9
- def gammaP5_2(x)
10
- gammaP_regularized(x, 5 / 2.0)
11
- end
12
-
13
- def gammaQ5_2(x)
14
- gammaQ_regularized(x, 5 / 2.0)
15
- end
16
-
17
- X1 = [ 0.0, 0.1, 0.5, 0.9, 0.95, 0.995, 1.0, 2, 3, 5, 10, 100 ]
18
-
19
- Y1 = [ 0.0000000000, 0.0008861388, 0.0374342268, 0.1239315997, 0.1371982774,
20
- 0.1494730091, 0.1508549639, 0.4505840486, 0.6937810816, 0.9247647539,
21
- 0.9987502694, 1.0000000000 ]
22
-
23
- def test_gammaPQ
24
- assert gammaQ_regularized(1, -1).nan?
25
- assert gammaP_regularized(1, -1).nan?
26
- assert gammaP5_2(-1).nan?
27
- assert gammaQ5_2(-1).nan?
28
- X1.zip(Y1) do |x, y|
29
- assert_in_delta y, gammaP5_2(x), 1E-10
30
- assert_in_delta y, 1 - gammaQ5_2(x), 1E-10
31
- end
32
- end
33
- end
@@ -1,28 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'test/unit'
4
- require 'bullshit'
5
-
6
- class TestNewtonBisection < Test::Unit::TestCase
7
- include Bullshit
8
-
9
- def test_bracket
10
- solver = NewtonBisection.new { |x| x ** 2 - 3 }
11
- assert_raises(ArgumentError) { solver.bracket(1..1) }
12
- assert_raises(ArgumentError) { solver.bracket(1..-1) }
13
- range = solver.bracket(0..0.1)
14
- assert_in_delta range.first, 0, 1E-6
15
- assert_in_delta range.last, 1.7576, 1E-6
16
- range = solver.bracket(2..3)
17
- assert_in_delta range.first, 0.4, 1E-6
18
- assert_in_delta range.last, 3, 1E-6
19
- end
20
-
21
- def test_zero
22
- solver = NewtonBisection.new { |x| x ** 2 - 3 }
23
- assert_in_delta 1.73205, solver.solve, 1E-6
24
- assert_in_delta(-1.73205, solver.solve(-5..-1), 1E-6)
25
- assert_in_delta 1.73205, solver.solve(solver.bracket(0..0.1)), 1E-6
26
- assert_in_delta 1.73205, solver.solve(solver.bracket(2..3)), 1E-6
27
- end
28
- end