num4anova 0.0.13-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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84f7f49327e10a3da88a363daa4d6d5dec1dd4da00827d6f8e5c60ee6db4bc08
4
- data.tar.gz: 495495a96d218034014971770b654431bb0df61aec47f8f3171fe351285022c4
3
+ metadata.gz: 81c39fe39ac94774f6ce94343e91c968190c63c510c9742d0a4b1e33c6f1a399
4
+ data.tar.gz: 9ba93973bdf9ad7237dd02702b67cd493335659a9b9f9162976ecb703ae351db
5
5
  SHA512:
6
- metadata.gz: 1b46cc7670940eb88343959c80bad55209a981388acdd823c3d9eeafa72a629d2319cfb49707f3f998d72f0d080480785129c948c8ebedb388693476be59add1
7
- data.tar.gz: 83585b2c13c1f964b8bf92e507a005deb4d358a612e23b939cb9d9911175006a1378e16719c1b73073834aa510f245b4432f0aba9f5dfcdceb78ee2cf5638ba3
6
+ metadata.gz: c2d3ffd3cbf0bcb0c4955c64c587b48da9d878bb61ce5c3a2b547de4c6563bafbf6852aa032e378d318419e55d6583f4af6467a7456a598501d6acea5f2b9c91
7
+ data.tar.gz: 7d97f9780f7d68f601810ac5ba5bcda923050b80e11fbff53b78017ceb61d954a788bb62f9a1dc85bc79c7e4fd0be857c538d6647d7506f91c6100295891238d
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.15] - 2024-04-13
6
+
7
+ ### add
8
+ - add fuction of bonferrono_test in kruskalwallis_test.
9
+
10
+ ## [0.0.14] - 2024-04-08
11
+
12
+ ### add
13
+ - add fuction of bonferrono_test in NonParametrixTestLib.
14
+
5
15
  ## [0.0.13] - 2024-04-04
6
16
 
7
17
  ### chg
@@ -3,9 +3,17 @@ import java.util.Arrays;
3
3
  import java.util.List;
4
4
  import java.util.ArrayList;
5
5
  import org.apache.commons.math3.distribution.TDistribution;
6
+ import org.apache.commons.math3.stat.inference.MannWhitneyUTest;
6
7
 
7
8
  import org.apache.commons.math3.util.Combinations;
8
9
  public class MultiComp {
10
+ /*********************************/
11
+ /* interface define */
12
+ /*********************************/
13
+ private interface HypothesisTest {
14
+ double[][] calcTestStatistic(double[][] xi);
15
+ boolean[][] executeTest(double[][] statistic, double a);
16
+ }
9
17
  public static class ParametrixTest {
10
18
  private static ParametrixTest paramTest = new ParametrixTest();
11
19
  public static ParametrixTest getInstance() {
@@ -46,13 +54,6 @@ public class MultiComp {
46
54
  return hypoth.executeTest(statistic, a);
47
55
  }
48
56
  /*********************************/
49
- /* interface define */
50
- /*********************************/
51
- private interface HypothesisTest {
52
- double[][] calcTestStatistic(double[][] xi);
53
- boolean[][] executeTest(double[][] statistic, double a);
54
- }
55
- /*********************************/
56
57
  /* Class define */
57
58
  /*********************************/
58
59
  // turkey法
@@ -366,5 +367,55 @@ public class MultiComp {
366
367
  }
367
368
  }
368
369
  }
370
+ public static class NonParametrixTest {
371
+ private static NonParametrixTest nonParamTest = new NonParametrixTest();
372
+ public static NonParametrixTest getInstance() {
373
+ return nonParamTest;
374
+ }
375
+ public boolean[][] bonferronoTest(double[][] xi, double a) {
376
+ HypothesisTest hypoth = new BonferroniTest();
377
+
378
+ double[][] statistic = hypoth.calcTestStatistic(xi);
379
+ return hypoth.executeTest(statistic, a * 0.5);
380
+ }
381
+ /*********************************/
382
+ /* Class define */
383
+ /*********************************/
384
+ // ボンフェロー法
385
+ private class BonferroniTest implements HypothesisTest {
386
+ private int n = 0;
387
+ private int k = 0;
388
+ public double[][] calcTestStatistic(double[][] xi) {
389
+ n = xi.length;
390
+ double[][] statistic = new double[n][n];
391
+ //
392
+ Combinations c = new Combinations(n, 2);
393
+ List<int[]> al = new ArrayList<>();
394
+ for(int[] iterate : c) {
395
+ al.add(iterate);
396
+ }
397
+ k = al.size();
398
+ //
399
+ MannWhitneyUTest utest = new MannWhitneyUTest();
400
+ for(int[] array : al) {
401
+ int i = array[0];
402
+ int j = array[1];
403
+
404
+ statistic[i][j] = utest.mannWhitneyUTest(xi[i], xi[j]);
405
+ }
406
+ return statistic;
407
+ }
408
+ public boolean[][] executeTest(double[][] statistic, double a) {
409
+ boolean[][] ret = new boolean[n][n];
410
+
411
+ for(int i = 0; i < n; i++) {
412
+ for(int j = i+1; j < n; j++) {
413
+ ret[i][j] = (statistic[i][j] < a * k) ? true : false;
414
+ }
415
+ }
416
+ return ret;
417
+ }
418
+ }
419
+ }
369
420
  }
370
421
 
@@ -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/multicomp.rb CHANGED
@@ -43,7 +43,7 @@ module MultiCompLib
43
43
  ret = @paramTest.turkeyTest(xi.to_java(Java::double[]), a)
44
44
  return ret.to_a
45
45
  end
46
- # ボンフェロー二の不等式による多重比較
46
+ # ボンフェロー二の不等式による多重比較(T検定)
47
47
  #
48
48
  # @overload bonferrono_test(xi, a)
49
49
  # @param [array] xi データ(double[][])
@@ -74,5 +74,37 @@ module MultiCompLib
74
74
  end
75
75
  # ノンパラメトリック検定
76
76
  class NonParametrixTestLib
77
+ def initialize
78
+ @nonParamTest = MultiComp::NonParametrixTest.getInstance()
79
+ end
80
+ # ボンフェロー二の不等式による多重比較(マン・ホイットニーU検定)
81
+ #
82
+ # @overload bonferrono_test(xi, a)
83
+ # @param [array] xi データ(double[][])
84
+ # @param [double] a 有意水準
85
+ # @return [Array] 検定結果(boolean[][] true:棄却域内 false:棄却域外)
86
+ # @example
87
+ # xi = [
88
+ # [12.2, 18.8, 18.2],
89
+ # [22.2, 20.5, 14.6],
90
+ # [20.8, 19.5, 26.3],
91
+ # [26.4, 32.5, 31.3],
92
+ # [24.5, 21.2, 22.4],
93
+ # ]
94
+ # nonParaTest = MultiCompLib::NonParametrixTestLib.new
95
+ # nonParaTest.bonferrono_test(xi, 0.05)
96
+ # =>
97
+ # [
98
+ # [false, false, true, true, true],
99
+ # [false, false, false, true, true],
100
+ # [false, false, false, true, false],
101
+ # [false, false, false, false, true],
102
+ # [false, false, false, false, false],
103
+ # ]
104
+ def bonferrono_test(xi, a)
105
+ ret = @nonParamTest.bonferronoTest(xi.to_java(Java::double[]), a)
106
+ return ret.to_a
107
+ end
108
+
77
109
  end
78
110
  end
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.13
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-04 00:00:00.000000000 Z
11
+ date: 2024-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake