num4anova 0.0.14-java → 0.0.16-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: 844b392125f78a4966a67dd1162bef6676857f03ed9f49e48d4f00e9f294d6e9
4
- data.tar.gz: da4999753cf5b2b73336b365c53e79ad5eb084fcb44151fdf6c05b5c691a40d2
3
+ metadata.gz: fb1dd347cd1156e1cc9e06ee7b4daff32442d6fc721e35553cbb406ae5d94cf1
4
+ data.tar.gz: 17d4c4084a40fe8e721829169e2551455d258f64dd4ef6c537b499ef9a5dab15
5
5
  SHA512:
6
- metadata.gz: 6c8e9884146305d0db99633f36997de0dc3ec4593e2b497fa6f97c1d9cf957721ad233914454e2eb1f8334b86871fdb389f073d62492904432da76415f9a302f
7
- data.tar.gz: 79b9cc886c7f2f8b794c204db714ca5663e9b9acb0388eee8afd60cef393b9f107e3f32f05855f848c53e39fc67f42212d352d87c0fdc2262718178f6d36d1d1
6
+ metadata.gz: 768b64803a235265e1dae41341428a0bda9fb12245e7f2fae54a3f75f8aeee607a4cd3709b578b1c7aeb6869fcc7f227b287d2142b99411ed8955248eeafedd4
7
+ data.tar.gz: da4c649d32a38c9f9a83adadc54d20605bfebe42f822fdd353df7ba8e00df8e3be8fbd8c4775474da5299957a477ec98c4912ec1ac5b8fb0d305c0f11ad91ff0
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.16] - 2024-04-16
6
+
7
+ ### add
8
+ - add fuction of bonferrono_test in friedman_test.
9
+
10
+ ## [0.0.15] - 2024-04-13
11
+
12
+ ### add
13
+ - add fuction of bonferrono_test in kruskalwallis_test.
14
+
5
15
  ## [0.0.14] - 2024-04-08
6
16
 
7
17
  ### add
@@ -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
 
@@ -1,6 +1,11 @@
1
1
  import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
2
2
  import org.apache.commons.math3.distribution.FDistribution;
3
+ import org.apache.commons.math3.distribution.ChiSquaredDistribution;
3
4
  import java.util.Arrays;
5
+
6
+ import org.apache.commons.math3.stat.ranking.NaNStrategy;
7
+ import org.apache.commons.math3.stat.ranking.NaturalRanking;
8
+ import org.apache.commons.math3.stat.ranking.TiesStrategy;
4
9
  public class TwoWayLayout {
5
10
  private static TwoWayLayout twoWay = new TwoWayLayout();
6
11
  public static TwoWayLayout getInstance() {
@@ -18,16 +23,26 @@ public class TwoWayLayout {
18
23
  double[] statistic = twoway.calcTestStatistic(xij);
19
24
  return twoway.execute_test(statistic, a);
20
25
  }
26
+ public boolean friedmanTest(double[][] xij, double a) {
27
+ OneWayAnovaTest twoway = new FriedmanTest();
28
+
29
+ double statistic = twoway.calcTestStatistic(xij);
30
+ return twoway.execute_test(statistic, a);
31
+ }
21
32
  /*********************************/
22
33
  /* interface define */
23
34
  /*********************************/
24
35
  private interface TwoWayAnovaTest {
25
36
  double[] calcTestStatistic(double[][][] xij);
26
- boolean[] execute_test(double statistic[], double a);
37
+ boolean[] execute_test(double[] statistic, double a);
27
38
  }
28
39
  private interface TwoWay2AnovaTest {
29
40
  double[] calcTestStatistic(double[][] xij);
30
- boolean[] execute_test(double statistic[], double a);
41
+ boolean[] execute_test(double[] statistic, double a);
42
+ }
43
+ private interface OneWayAnovaTest {
44
+ double calcTestStatistic(double[][] xi);
45
+ boolean execute_test(double statistic, double a);
31
46
  }
32
47
  /*********************************/
33
48
  /* class define */
@@ -162,7 +177,7 @@ public class TwoWayLayout {
162
177
  }
163
178
  return sumDrift;
164
179
  }
165
- public boolean[] execute_test(double statistic[], double a) {
180
+ public boolean[] execute_test(double[] statistic, double a) {
166
181
  boolean[] ret = new boolean[3];
167
182
 
168
183
  ret[0] = evaluation(new FDistribution(an, en), statistic[0], a);
@@ -269,7 +284,7 @@ public class TwoWayLayout {
269
284
  return sumDrift;
270
285
  }
271
286
 
272
- public boolean[] execute_test(double statistic[], double a) {
287
+ public boolean[] execute_test(double[] statistic, double a) {
273
288
  boolean[] ret = new boolean[2];
274
289
 
275
290
  ret[0] = evaluation(new FDistribution(an, en), statistic[0], a);
@@ -282,5 +297,64 @@ public class TwoWayLayout {
282
297
  return (statistic >= r_val) ? true : false;
283
298
  }
284
299
  }
300
+ // フリードマンの検定
301
+ private class FriedmanTest implements OneWayAnovaTest{
302
+ private NaturalRanking naturalRanking;
303
+ private int n = 0;
304
+ private int k = 0;
305
+ public FriedmanTest() {
306
+ naturalRanking = new NaturalRanking(NaNStrategy.FIXED,
307
+ TiesStrategy.AVERAGE);
308
+ }
309
+ public double calcTestStatistic(double[][] xij) {
310
+ n = xij[0].length;
311
+ k = xij.length;
312
+ double[][] z = concatSample(xij);
313
+ double[][] ranks = calcRankij(z);
314
+ double[] sumRankXi = calcSumRankXi(ranks);
315
+ double kw = 0.0;
316
+
317
+ for(int i = 0; i < sumRankXi.length; i++) {
318
+ kw += sumRankXi[i] * sumRankXi[i];
319
+ }
320
+ return 12.0 / (n * k * (k + 1.0)) * kw - 3.0 * n * (k + 1.0);
321
+ }
322
+ public boolean execute_test(double statistic, double a) {
323
+ ChiSquaredDistribution chi2Dist = new ChiSquaredDistribution(k - 1);
324
+
325
+ double r_val = chi2Dist.inverseCumulativeProbability(1.0 - a);
326
+
327
+ return (r_val < statistic) ? true : false;
328
+ }
329
+ private double[][] concatSample(double[][] xi) {
330
+ double[][] z = new double[n][k];
331
+
332
+ for(int i = 0; i < k; i++) {
333
+ for(int j = 0; j < n; j++) {
334
+ z[j][i] = xi[i][j];
335
+ }
336
+ }
337
+ return z;
338
+ }
339
+ private double[][] calcRankij(double[][] z) {
340
+ double[][] ranks = new double[n][k];
341
+
342
+ for(int i = 0; i < n; i++) {
343
+ ranks[i] = naturalRanking.rank(z[i]);
344
+ }
345
+ return ranks;
346
+ }
347
+ private double[] calcSumRankXi(double[][] ranks) {
348
+ double[] sumRi = new double[k];
349
+
350
+ for(int i = 0; i < k; i++) {
351
+ sumRi[i] = 0.0;
352
+ for(int j = 0; j < n; j++) {
353
+ sumRi[i] += ranks[j][i];
354
+ }
355
+ }
356
+ return sumRi;
357
+ }
358
+ }
285
359
  }
286
360
 
data/lib/num4anova.rb CHANGED
@@ -151,6 +151,24 @@ 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
+ # @example
161
+ # xi = [
162
+ # [12.2, 18.8, 18.2],
163
+ # [22.2, 20.5, 14.6, 20.8, 19.5, 26.3],
164
+ # [26.4, 32.5, 31.3, 24.5, 21.2, 22.4],
165
+ # ]
166
+ # oneWay = Num4AnovaLib::OneWayLayoutLib.new
167
+ # oneWay.kruskalwallis_test(xi, 0.05)
168
+ # => true
169
+ def kruskalwallis_test(xi, a)
170
+ return @oneWay.kruskalWallisTest(xi.to_java(Java::double[]), a)
171
+ end
154
172
  end
155
173
 
156
174
  # 二元配置の分散分析
@@ -217,6 +235,27 @@ module Num4AnovaLib
217
235
  def twoway2_anova(xij, a)
218
236
  ret = @twoWay.twoway2Anova(xij.to_java(Java::double[]), a)
219
237
  return ret.to_a
238
+ end
239
+ # フリードマン検定
240
+ #
241
+ # @overload friedman_test(xij, a)
242
+ # @param [array] xij データ(double[][])
243
+ # @param [double] a 有意水準
244
+ # @return [boolean] 検定結果(boolean true:棄却域内 false:棄却域外)
245
+ # @example
246
+ # xij = [
247
+ # [13.6, 15.6, 9.2],
248
+ # [22.3, 23.3, 13.3],
249
+ # [26.7, 28.8, 15.0],
250
+ # [28.0, 31.2, 15.8],
251
+ # ]
252
+ # twoWay = Num4AnovaLib::TwoWayLayoutLib.new
253
+ # twoWay.friedman_test(xij, 0.05)
254
+ # =>
255
+ # true
256
+ def friedman_test(xij, a)
257
+ ret = @twoWay.friedmanTest(xij.to_java(Java::double[]), a)
258
+ return ret
220
259
  end
221
260
  end
222
261
  # 共分散分析
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.14
4
+ version: 0.0.16
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-08 00:00:00.000000000 Z
11
+ date: 2024-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake