rust 0.4 → 0.10

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rust-tests.rb DELETED
@@ -1,165 +0,0 @@
1
- require_relative 'rust-core'
2
-
3
- module Rust::StatisticalTests
4
- class Result
5
- attr_accessor :name
6
- attr_accessor :statistics
7
- attr_accessor :pvalue
8
- attr_accessor :exact
9
- attr_accessor :alpha
10
-
11
- def initialize
12
- @statistics = {}
13
- end
14
-
15
- def [](name)
16
- return @statistics[name.to_sym]
17
- end
18
-
19
- def []=(name, value)
20
- @statistics[name.to_sym] = value
21
- end
22
-
23
- def significant
24
- pvalue < alpha
25
- end
26
-
27
- def to_s
28
- return "#{name}. P-value = #{pvalue} " +
29
- "(#{significant ? "significant" : "not significant"} w/ alpha = #{alpha}); " +
30
- "#{ statistics.map { |k, v| k.to_s + " -> " + v.to_s }.join(", ") }." +
31
- (!exact ? " P-value is not exact." : "")
32
- end
33
- end
34
- end
35
-
36
- module Rust::StatisticalTests::Wilcoxon
37
- class << self
38
- def paired(d1, d2, alpha = 0.05)
39
- raise TypeError, "Expecting Array of numerics" if !d1.is_a?(Array) || !d1.all? { |e| e.is_a?(Numeric) }
40
- raise TypeError, "Expecting Array of numerics" if !d2.is_a?(Array) || !d2.all? { |e| e.is_a?(Numeric) }
41
- raise "The two distributions have different size" if d1.size != d2.size
42
-
43
- Rust.exclusive do
44
- Rust["wilcox.a"] = d1
45
- Rust["wilcox.b"] = d2
46
-
47
- _, warnings = Rust._eval("wilcox.result = wilcox.test(wilcox.a, wilcox.b, alternative='two.sided', paired=T)", true)
48
- result = Rust::StatisticalTests::Result.new
49
- result.name = "Wilcoxon Signed-Rank test"
50
- result.pvalue = Rust._pull("wilcox.result$p.value")
51
- result[:w] = Rust._pull("wilcox.result$statistic")
52
- result.exact = !warnings.include?("cannot compute exact p-value with zeroes")
53
- result.alpha = alpha
54
-
55
- return result
56
- end
57
- end
58
-
59
- def unpaired(d1, d2, alpha = 0.05)
60
- raise TypeError, "Expecting Array of numerics" if !d1.is_a?(Array) || !d1.all? { |e| e.is_a?(Numeric) }
61
- raise TypeError, "Expecting Array of numerics" if !d2.is_a?(Array) || !d2.all? { |e| e.is_a?(Numeric) }
62
-
63
- Rust.exclusive do
64
- Rust["wilcox.a"] = d1
65
- Rust["wilcox.b"] = d2
66
-
67
- _, warnings = Rust._eval("wilcox.result = wilcox.test(wilcox.a, wilcox.b, alternative='two.sided', paired=F)", true)
68
- result = Rust::StatisticalTests::Result.new
69
- result.name = "Wilcoxon Ranked-Sum test (a.k.a. Mann–Whitney U test)"
70
- result.pvalue = Rust._pull("wilcox.result$p.value")
71
- result[:w] = Rust._pull("wilcox.result$statistic")
72
- result.exact = !warnings.include?("cannot compute exact p-value with ties")
73
- result.alpha = alpha
74
-
75
- return result
76
- end
77
- end
78
- end
79
- end
80
-
81
- module Rust::StatisticalTests::T
82
- class << self
83
- def paired(d1, d2, alpha = 0.05)
84
- raise TypeError, "Expecting Array of numerics" if !d1.is_a?(Array) || !d1.all? { |e| e.is_a?(Numeric) }
85
- raise TypeError, "Expecting Array of numerics" if !d2.is_a?(Array) || !d2.all? { |e| e.is_a?(Numeric) }
86
- raise "The two distributions have different size" if d1.size != d2.size
87
-
88
- Rust.exclusive do
89
- Rust["t.a"] = d1
90
- Rust["t.b"] = d2
91
-
92
- warnings = Rust._eval("t.result = t.test(t.a, t.b, alternative='two.sided', paired=T)")
93
- result = Rust::StatisticalTests::Result.new
94
- result.name = "Paired t-test"
95
- result.pvalue = Rust._pull("t.result$p.value")
96
- result[:t] = Rust._pull("t.result$statistic")
97
- result.exact = true
98
- result.alpha = alpha
99
-
100
- return result
101
- end
102
- end
103
-
104
- def unpaired(d1, d2, alpha = 0.05)
105
- raise TypeError, "Expecting Array of numerics" if !d1.is_a?(Array) || !d1.all? { |e| e.is_a?(Numeric) }
106
- raise TypeError, "Expecting Array of numerics" if !d2.is_a?(Array) || !d2.all? { |e| e.is_a?(Numeric) }
107
-
108
- Rust.exclusive do
109
- Rust["t.a"] = d1
110
- Rust["t.b"] = d2
111
-
112
- Rust._eval("t.result = t.test(t.a, t.b, alternative='two.sided', paired=F)")
113
- result = Rust::StatisticalTests::Result.new
114
- result.name = "Welch Two Sample t-test"
115
- result.pvalue = Rust._pull("t.result$p.value")
116
- result[:t] = Rust._pull("t.result$statistic")
117
- result.exact = true
118
- result.alpha = alpha
119
-
120
- return result
121
- end
122
- end
123
- end
124
- end
125
-
126
- module Rust::StatisticalTests::Shapiro
127
- class << self
128
- def compute(vector, alpha = 0.05)
129
- raise TypeError, "Expecting Array of numerics" if !vector.is_a?(Array) || !vector.all? { |e| e.is_a?(Numeric) }
130
- Rust.exclusive do
131
- Rust['shapiro.v'] = vector
132
-
133
- Rust._eval("shapiro.result = shapiro.test(shapiro.v)")
134
- result = Rust::StatisticalTests::Result.new
135
- result.name = "Shapiro-Wilk normality test"
136
- result.pvalue = Rust._pull("shapiro.result$p.value")
137
- result[:W] = Rust._pull("shapiro.result$statistic")
138
- result.exact = true
139
- result.alpha = alpha
140
-
141
- return result
142
- end
143
- end
144
- end
145
- end
146
-
147
- module Rust::RBindings
148
- def wilcox_test(d1, d2, **args)
149
- paired = args[:paired] || false
150
- if paired
151
- return Rust::StatisticalTests::Wilcoxon.paired(d1, d2)
152
- else
153
- return Rust::StatisticalTests::Wilcoxon.unpaired(d1, d2)
154
- end
155
- end
156
-
157
- def t_test(d1, d2, **args)
158
- paired = args[:paired] || false
159
- if paired
160
- return Rust::StatisticalTests::T.paired(d1, d2)
161
- else
162
- return Rust::StatisticalTests::T.unpaired(d1, d2)
163
- end
164
- end
165
- end