num4anova 0.0.14-java → 0.0.15-java
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/ext/num4anova/OneWayLayout.java +73 -1
- data/lib/num4anova.rb +19 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81c39fe39ac94774f6ce94343e91c968190c63c510c9742d0a4b1e33c6f1a399
|
4
|
+
data.tar.gz: 9ba93973bdf9ad7237dd02702b67cd493335659a9b9f9162976ecb703ae351db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2d3ffd3cbf0bcb0c4955c64c587b48da9d878bb61ce5c3a2b547de4c6563bafbf6852aa032e378d318419e55d6583f4af6467a7456a598501d6acea5f2b9c91
|
7
|
+
data.tar.gz: 7d97f9780f7d68f601810ac5ba5bcda923050b80e11fbff53b78017ceb61d954a788bb62f9a1dc85bc79c7e4fd0be857c538d6647d7506f91c6100295891238d
|
data/CHANGELOG.md
CHANGED
@@ -30,6 +30,11 @@ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
|
|
30
30
|
import org.apache.commons.math3.distribution.ChiSquaredDistribution;
|
31
31
|
import org.apache.commons.math3.distribution.FDistribution;
|
32
32
|
import java.util.Map;
|
33
|
+
|
34
|
+
import org.apache.commons.math3.stat.ranking.NaNStrategy;
|
35
|
+
import org.apache.commons.math3.stat.ranking.NaturalRanking;
|
36
|
+
import org.apache.commons.math3.stat.ranking.TiesStrategy;
|
37
|
+
import java.util.stream.IntStream;
|
33
38
|
public class OneWayLayout {
|
34
39
|
private static OneWayLayout oneWay = new OneWayLayout();
|
35
40
|
public static OneWayLayout getInstance() {
|
@@ -72,6 +77,12 @@ public class OneWayLayout {
|
|
72
77
|
double statistic = oneway.calcTestStatistic(xi);
|
73
78
|
return oneway.execute_test(statistic, a);
|
74
79
|
}
|
80
|
+
public boolean kruskalWallisTest(double[][] xi, double a) {
|
81
|
+
OneWayAnovaTest oneway = new KruskalWallisTest();
|
82
|
+
|
83
|
+
double statistic = oneway.calcTestStatistic(xi);
|
84
|
+
return oneway.execute_test(statistic, a);
|
85
|
+
}
|
75
86
|
/*********************************/
|
76
87
|
/* interface define */
|
77
88
|
/*********************************/
|
@@ -380,5 +391,66 @@ public class OneWayLayout {
|
|
380
391
|
return (statistic >= f) ? true : false;
|
381
392
|
}
|
382
393
|
}
|
383
|
-
|
394
|
+
// クラスカル・ウォリス検定
|
395
|
+
private class KruskalWallisTest implements OneWayAnovaTest {
|
396
|
+
private NaturalRanking naturalRanking;
|
397
|
+
private int[] ni = null;
|
398
|
+
public KruskalWallisTest() {
|
399
|
+
naturalRanking = new NaturalRanking(NaNStrategy.FIXED,
|
400
|
+
TiesStrategy.AVERAGE);
|
401
|
+
}
|
402
|
+
public double calcTestStatistic(double[][] xi) {
|
403
|
+
double[] z = concatSample(xi); // 全てのデータをつなぐ
|
404
|
+
double[] ranks = naturalRanking.rank(z); // rankに順位値に変換
|
405
|
+
double[] sumRankXi = calcSumRankXi(ranks);
|
406
|
+
double kw = 0.0;
|
407
|
+
int n = z.length;
|
408
|
+
|
409
|
+
for(int i = 0; i < sumRankXi.length; i++) {
|
410
|
+
kw += sumRankXi[i] * sumRankXi[i] / ni[i];
|
411
|
+
}
|
412
|
+
return 12.0 / (n * (n + 1.0)) * kw - 3.0 * (n + 1.0);
|
413
|
+
}
|
414
|
+
public boolean execute_test(double statistic, double a) {
|
415
|
+
ChiSquaredDistribution chi2Dist = new ChiSquaredDistribution(ni.length - 1);
|
416
|
+
double r_val = chi2Dist.inverseCumulativeProbability(1.0 - a);
|
417
|
+
|
418
|
+
return (r_val < statistic) ? true : false;
|
419
|
+
}
|
420
|
+
private int[] calcNi(double[][] xi) {
|
421
|
+
int[] ni = new int[xi.length];
|
422
|
+
|
423
|
+
for(int i = 0; i < ni.length; i++) {
|
424
|
+
ni[i] = xi[i].length;
|
425
|
+
}
|
426
|
+
return ni;
|
427
|
+
}
|
428
|
+
private double[] concatSample(double[][] xi) {
|
429
|
+
DescriptiveStatistics stat = new DescriptiveStatistics();
|
430
|
+
ni = calcNi(xi);
|
431
|
+
Arrays.stream(ni).forEach(stat::addValue);
|
432
|
+
int n = Double.valueOf(stat.getSum()).intValue();
|
433
|
+
|
434
|
+
double[] z = new double[n];
|
435
|
+
int idx = 0;
|
436
|
+
for(int cnt = 0; cnt < xi.length; cnt++) {
|
437
|
+
System.arraycopy(xi[cnt], 0, z, idx, ni[cnt]);
|
438
|
+
idx += ni[cnt];
|
439
|
+
}
|
440
|
+
return z;
|
441
|
+
}
|
442
|
+
private double[] calcSumRankXi(double[] ranks) {
|
443
|
+
double[] sumRi = new double[ni.length];
|
444
|
+
int idx = 0;
|
445
|
+
|
446
|
+
for(int cnt = 0; cnt < ni.length; cnt++) {
|
447
|
+
sumRi[cnt] = IntStream.range(idx, idx + ni[cnt])
|
448
|
+
.mapToDouble(i -> ranks[i])
|
449
|
+
.sum();
|
450
|
+
idx += ni[cnt];
|
451
|
+
}
|
452
|
+
return sumRi;
|
453
|
+
}
|
454
|
+
}
|
455
|
+
}
|
384
456
|
|
data/lib/num4anova.rb
CHANGED
@@ -151,6 +151,25 @@ module Num4AnovaLib
|
|
151
151
|
def replicate_test(xi, a)
|
152
152
|
return @oneWay.replicateTest(xi.to_java(Java::double[]), a)
|
153
153
|
end
|
154
|
+
# クラスカル・ウォリスの検定
|
155
|
+
#
|
156
|
+
# @overload kruskalwallis_test(xi, a)
|
157
|
+
# @param [array] xi データ(double[][])
|
158
|
+
# @param [double] a 有意水準
|
159
|
+
# @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
|
160
|
+
# xi = [
|
161
|
+
# [12.2, 18.8, 18.2],
|
162
|
+
# [22.2, 20.5, 14.6],
|
163
|
+
# [20.8, 19.5, 26.3],
|
164
|
+
# [26.4, 32.5, 31.3],
|
165
|
+
# [24.5, 21.2, 22.4],
|
166
|
+
# ]
|
167
|
+
# oneWay = Num4AnovaLib::OneWayLayoutLib.new
|
168
|
+
# oneWay.kruskalwallis_test(xi, 0.05)
|
169
|
+
# => true
|
170
|
+
def kruskalwallis_test(xi, a)
|
171
|
+
return @oneWay.kruskalWallisTest(xi.to_java(Java::double[]), a)
|
172
|
+
end
|
154
173
|
end
|
155
174
|
|
156
175
|
# 二元配置の分散分析
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: num4anova
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- siranovel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|