num4normality 0.0.7-java → 0.0.9-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: d445077e57757b20233ab78457bdd1b11a3b1851a8341ab7e28d778ef180d68d
4
- data.tar.gz: 1e7b9c47b46bffbc4ae6dafa164b6f2b6d3c0707b6ad4478267740248b098498
3
+ metadata.gz: 28c1bcf4f9952a2f18587d5b23d301a43ea31cd109d6a2d99ce665f900877bbf
4
+ data.tar.gz: 29a902d3ef52739cb913bc523c5488ebcc14ac3288d8babc8d7e64fd28448cbf
5
5
  SHA512:
6
- metadata.gz: 5bb019e46c2b0deae1b6f2e2e5776926621620fd787903a3fec119bece63b04e64b979f3c5b3de08883a487fb6ec503d49f43c7acc229b1ab33134c24e5d1179
7
- data.tar.gz: 8f60222acffc237d2961b909209182cdf9ef2e7fecad9248553be02853c9690cfeefef425f49a4ad5bdff5a0e733d3bd9a434b74b09ef934d94f2ac3f851345b
6
+ metadata.gz: 66fe7346cae038b6a399395d46863b2a7aaecff96687b451075e2affa08236c1f8eb5b18066eed22eafac9f1ac7c719989e43c07082fceb3d17b9b6d4310fe4b
7
+ data.tar.gz: 3b621221e49cfaabf960cb5f8d413aadb53ec1e280e7f3c65b1cb2d70dd8c960129e239aee017365d9910c609b84b35d875f7e12fcc5a3f0f84284db3a9d78b6
data/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.9] - 2024-05-11
6
+ ### add
7
+ - add function of adtest.
8
+
9
+ ## [0.0.8] - 2023-12-30
10
+
11
+ ### Fixed
12
+ - fix fuction of ppplot.
13
+
5
14
  ## [0.0.7] - 2023-12-26
6
15
 
7
16
  ### add
@@ -1,6 +1,7 @@
1
1
  import org.jfree.chart.JFreeChart;
2
2
  import org.jfree.chart.StandardChartTheme;
3
3
  import org.jfree.chart.ChartFactory;
4
+ import org.jfree.chart.ChartUtils;
4
5
  import org.jfree.data.xy.XYSeriesCollection;
5
6
  import org.jfree.data.xy.XYSeries;
6
7
 
@@ -22,70 +23,77 @@ import java.io.File;
22
23
  import java.io.IOException;
23
24
 
24
25
  import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
26
+ import org.apache.commons.math3.stat.StatUtils;
25
27
  import org.apache.commons.math3.distribution.NormalDistribution;
26
28
  import org.apache.commons.math3.distribution.ChiSquaredDistribution;
29
+ import org.apache.commons.math3.distribution.TDistribution;
27
30
  import org.apache.commons.math3.stat.regression.SimpleRegression;
28
31
  import java.util.Arrays;
29
32
  import java.text.DecimalFormat;
30
33
 
31
34
  import org.apache.commons.math3.stat.inference.TestUtils;
32
35
  public class Normality {
33
- public static void qqplot(String dname, double[] xi) {
36
+ public static void qqPlot(String dname, double[] xi) {
34
37
  ChartPlot plot = new QQChartPlot();
35
38
  JFreeChart chart = plot.createChart("正規Q-Qプロット", dname, xi);
36
39
 
37
40
  plot.writeJPEG("qqplot.jpeg", chart, 800, 500);
38
41
  }
39
- public static void ksplot(String dname, double[] xi) {
42
+ public static void ksPlot(String dname, double[] xi) {
40
43
  ChartPlot plot = new KSChartPlot();
41
44
  JFreeChart chart = plot.createChart("コルモゴルフ・スミルノフ検定", dname, xi);
42
45
 
43
46
  plot.writeJPEG("ksplot.jpeg", chart, 800, 500);
44
47
 
45
48
  }
46
- public static void qqksplot(String dname, double[] xi) {
47
- ChartPlot plot = new QQKSChartPlot();
49
+ public static void qqksPlot(String dname, double[] xi) {
50
+ ChartPlot plot = new QQKSChartPlot();
48
51
  JFreeChart chart = plot.createChart("Q-Q and コルモゴルフ・スミルノフ", dname, xi);
49
52
 
50
53
  plot.writeJPEG("qqksplot.jpeg", chart, 1000, 800);
51
54
  }
52
- public static void ppplot(String dname, double[] xi) {
55
+ public static void ppPlot(String dname, double[] xi) {
53
56
  ChartPlot plot = new PPChartPlot();
54
57
  JFreeChart chart = plot.createChart("正規P-Pプロット", dname, xi);
55
58
 
56
59
  plot.writeJPEG("ppplot.jpeg", chart, 800, 500);
57
60
  }
58
- public static void ppksplot(String dname, double[] xi) {
61
+ public static void ppksPlot(String dname, double[] xi) {
59
62
  ChartPlot plot = new PPKSChartPlot();
60
63
  JFreeChart chart = plot.createChart("P-P and コルモゴルフ・スミルノフ", dname, xi);
61
64
 
62
65
  plot.writeJPEG("ppksplot.jpeg", chart, 800, 500);
63
66
 
64
67
  }
65
- public static boolean kstest(double[] xi) {
68
+ public static boolean ksTest(double[] xi) {
66
69
  KSTest ks = new KSTest();
67
70
 
68
71
  return ks.test(xi);
69
72
  }
70
- public static boolean skewnesstest(double[] xi) {
71
- DAgostinosTest daigo = new SkewnessTest();
73
+ public static boolean skewnessTest(double[] xi) {
74
+ HypothesisTest daigo = new SkewnessTest();
72
75
 
73
76
  double b1 = daigo.calcTestStatistic(xi);
74
- return daigo.test(b1, 0.05);
77
+ return daigo.test(b1, HypothesisTest.P);
75
78
  }
76
- public static boolean kurtosistest(double[] xi) {
77
- DAgostinosTest daigo = new KurtosisTest();
79
+ public static boolean kurtosisTest(double[] xi) {
80
+ HypothesisTest daigo = new KurtosisTest();
78
81
 
79
82
  double b2 = daigo.calcTestStatistic(xi);
80
- return daigo.test(b2, 0.05);
83
+ return daigo.test(b2, HypothesisTest.P);
81
84
  }
82
- public static boolean omnibustest(double[] xi) {
83
- DAgostinosTest daigo = new OmnibusTest();
85
+ public static boolean omnibusTest(double[] xi) {
86
+ HypothesisTest daigo = new OmnibusTest();
84
87
 
85
88
  double x = daigo.calcTestStatistic(xi);
86
- return daigo.test(x, 0.05);
89
+ return daigo.test(x, HypothesisTest.P);
90
+ }
91
+ public static boolean adTest(double[] xi) {
92
+ HypothesisTest daigo = new ADTest();
93
+ double x = daigo.calcTestStatistic(xi);
94
+
95
+ return daigo.test(x, HypothesisTest.P);
87
96
  }
88
-
89
97
  /*********************************/
90
98
  /* interface define */
91
99
  /*********************************/
@@ -107,7 +115,11 @@ public class Normality {
107
115
  private interface CreatePlot {
108
116
  XYPlot createPlot(String dname, double[] xi);
109
117
  }
110
- private interface DAgostinosTest {
118
+ private interface HypothesisTest {
119
+ /* フィールド */
120
+ static final double P = 0.05;
121
+
122
+ /* メゾット */
111
123
  double calcTestStatistic(double[] xi);
112
124
  boolean test(double statistic, double a);
113
125
  }
@@ -120,7 +132,10 @@ public class Normality {
120
132
  XYPlot plot = createPlot(dname, xi);
121
133
 
122
134
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
123
- return new JFreeChart(title, plot);
135
+ JFreeChart chart = new JFreeChart(title, plot);
136
+
137
+ ChartUtils.applyCurrentTheme(chart);
138
+ return chart;
124
139
  }
125
140
  private XYPlot createPlot(String dname, double[] xi) {
126
141
  CreatePlot plotImpl = new QQPlot();
@@ -223,7 +238,10 @@ public class Normality {
223
238
  XYPlot plot = createPlot(dname, xi);
224
239
 
225
240
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
226
- return new JFreeChart(title, plot);
241
+ JFreeChart chart = new JFreeChart(title, plot);
242
+
243
+ ChartUtils.applyCurrentTheme(chart);
244
+ return chart;
227
245
  }
228
246
  private XYPlot createPlot(String dname, double[] xi) {
229
247
  CreatePlot plotImpl = new KSPlot();
@@ -237,15 +255,14 @@ public class Normality {
237
255
  int n = xi.length;
238
256
  Arrays.sort(xi);
239
257
  Arrays.stream(xi).forEach(stat::addValue);
240
- double m = stat.getMean(); // 平均
241
- double sd = stat.getStandardDeviation();// 標準偏差
242
258
  double sum = stat.getSum();
243
259
  double[][] data = new double[n][2];
244
260
  double p = 0.0;
261
+ double z[] = StatUtils.normalize(xi);
245
262
 
246
263
  for (int i = 0; i < n; i++) {
247
264
  p += xi[i] / sum;
248
- data[i][0] = (xi[i] - m) / sd;
265
+ data[i][0] = z[i];
249
266
  data[i][1] = p;
250
267
  }
251
268
  return data;
@@ -326,7 +343,10 @@ public class Normality {
326
343
  XYPlot plot = createPlot(dname, xi);
327
344
 
328
345
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
329
- return new JFreeChart(title, plot);
346
+ JFreeChart chart = new JFreeChart(title, plot);
347
+
348
+ ChartUtils.applyCurrentTheme(chart);
349
+ return chart;
330
350
  }
331
351
  private XYPlot createPlot(String dname, double[] xi) {
332
352
  /*--- 横軸 ---*/
@@ -351,7 +371,10 @@ public class Normality {
351
371
  XYPlot plot = createPlot(dname, xi);
352
372
 
353
373
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
354
- return new JFreeChart(title, plot);
374
+ JFreeChart chart = new JFreeChart(title, plot);
375
+
376
+ ChartUtils.applyCurrentTheme(chart);
377
+ return chart;
355
378
  }
356
379
  private XYPlot createPlot(String dname, double[] xi) {
357
380
  CreatePlot plotImpl = new PPPlot();
@@ -364,20 +387,17 @@ public class Normality {
364
387
  NormalDistribution ndist = new NormalDistribution(0, 1);
365
388
 
366
389
  int n = xi.length;
390
+ double[][] data = new double[n][2];
367
391
  Arrays.sort(xi);
368
392
  Arrays.stream(xi).forEach(stat::addValue);
369
- double m = stat.getMean(); // 平均
370
- double sd = stat.getStandardDeviation();// 標準偏差
371
393
  double sum = stat.getSum();
372
- double[][] data = new double[n][2];
373
394
  double p = 0.0;
395
+ double z[] = StatUtils.normalize(xi);
374
396
 
375
397
  for (int i = 0; i < n; i++) {
376
398
  p += xi[i] / sum;
377
- double x = (xi[i] - m) / sd;
378
399
 
379
- ndist.cumulativeProbability(x);
380
- data[i][0] = ndist.cumulativeProbability(x);
400
+ data[i][0] = ndist.cumulativeProbability(z[i]);
381
401
  data[i][1] = p;
382
402
  }
383
403
  return data;
@@ -463,7 +483,10 @@ public class Normality {
463
483
  XYPlot plot = createPlot(dname, xi);
464
484
 
465
485
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
466
- return new JFreeChart(title, plot);
486
+ JFreeChart chart = new JFreeChart(title, plot);
487
+
488
+ ChartUtils.applyCurrentTheme(chart);
489
+ return chart;
467
490
  }
468
491
  private XYPlot createPlot(String dname, double[] xi) {
469
492
  /*--- 縦軸 ---*/
@@ -485,23 +508,15 @@ public class Normality {
485
508
  // KS検定
486
509
  private static class KSTest {
487
510
  public boolean test(double[] xi) {
488
- double[] data = new double[xi.length];
489
511
  Arrays.sort(xi);
490
- DescriptiveStatistics stat = new DescriptiveStatistics();
491
- Arrays.stream(xi).forEach(stat::addValue);
492
- double m = stat.getMean(); // 平均
493
- double sd = stat.getStandardDeviation();// 標準偏差
494
512
  NormalDistribution ndist = new NormalDistribution(0, 1);
513
+ double data[] = StatUtils.normalize(xi);
495
514
 
496
- for (int i = 0; i < xi.length; i++) {
497
- data[i] = (xi[i] - m) / sd;
498
- }
499
- boolean ret = TestUtils.kolmogorovSmirnovTest(ndist, data, 0.05);
500
- return ret;
515
+ return TestUtils.kolmogorovSmirnovTest(ndist, data, 0.05);
501
516
  }
502
517
  }
503
518
  // タコスディーノ検定(歪度)
504
- private static class SkewnessTest implements DAgostinosTest {
519
+ private static class SkewnessTest implements HypothesisTest {
505
520
  private long n = 0;
506
521
  private NormalDistribution ndist = null;
507
522
  public SkewnessTest() {
@@ -531,7 +546,7 @@ public class Normality {
531
546
  }
532
547
  }
533
548
  // タコスディーノ検定(尖度)
534
- private static class KurtosisTest implements DAgostinosTest {
549
+ private static class KurtosisTest implements HypothesisTest {
535
550
  private long n = 0;
536
551
  private NormalDistribution ndist = null;
537
552
  public KurtosisTest() {
@@ -561,9 +576,9 @@ public class Normality {
561
576
  }
562
577
  }
563
578
  // オムニバス検定
564
- private static class OmnibusTest implements DAgostinosTest {
565
- private DAgostinosTest skewness = null;
566
- private DAgostinosTest kurtosis = null;
579
+ private static class OmnibusTest implements HypothesisTest {
580
+ private HypothesisTest skewness = null;
581
+ private HypothesisTest kurtosis = null;
567
582
  public OmnibusTest() {
568
583
  skewness = new SkewnessTest();
569
584
  kurtosis = new KurtosisTest();
@@ -581,5 +596,53 @@ public class Normality {
581
596
  return (p < a) ? true : false;
582
597
  }
583
598
  }
599
+ // Anderson-Darling検定
600
+ private static class ADTest implements HypothesisTest {
601
+ public double calcTestStatistic(double[] xi) {
602
+ int n = xi.length;
603
+ double a2 = calcA2(xi, n);
604
+
605
+ return calcAD(a2, n);
606
+ }
607
+ public boolean test(double statistic, double a) {
608
+ double p = 0.0;
609
+ double statistic2 = statistic * statistic;
610
+
611
+ if (statistic <= 0.2) {
612
+ p = 1.0 - Math.exp(-13.436 + 101.14 * statistic - 223.73 * statistic);
613
+ }
614
+ else if ((0.2 < statistic) && (statistic <= 0.34)) {
615
+ p = 1.0 - Math.exp(-8.318 + 42.796 * statistic - 59.938 * statistic2);
616
+ }
617
+ else if ((0.34 < statistic) && (statistic <= 0.6)) {
618
+ p = Math.exp(0.9177 - 4.279 * statistic - 1.38 * statistic2);
619
+ }
620
+ else {
621
+ p = Math.exp(1.2937 - 5.709 * statistic + 0.0186 * statistic2);
622
+ }
623
+ return (p <= a) ? true : false;
624
+ }
625
+ private double calcA2(double[] xi, int n) {
626
+ Arrays.sort(xi);
627
+ double z[] = StatUtils.normalize(xi);
628
+ NormalDistribution ndist = new NormalDistribution(0, 1);
629
+ double s = 0.0;
630
+
631
+ for(int i = 0; i < n; i++) {
632
+ double fzi = ndist.cumulativeProbability(z[i]);
633
+ double fzni = ndist.cumulativeProbability(z[n - (i + 1)]);
634
+ double logzi = Math.log(fzi);
635
+ double logzni = Math.log(1 - fzni);
636
+
637
+ s += (2.0 * (i + 1.0) - 1.0) * (logzi + logzni);
638
+
639
+ }
640
+ return -1.0 * n - s / n;
641
+ }
642
+ // 補正値の計算(平均未知、分散未知の場合)
643
+ private double calcAD(double a2, int n) {
644
+ return a2 * (1.0 + 0.75 / n + 2.25 / (n * n));
645
+ }
646
+ }
584
647
  }
585
648
 
data/lib/num4normality.rb CHANGED
@@ -22,7 +22,7 @@ module Num4NormalityLib
22
22
  # @note
23
23
  # グラフは、jfreechartを使用
24
24
  def qqplot(dname, xi)
25
- Normality.qqplot(dname, xi.to_java(Java::double))
25
+ Normality.qqPlot(dname, xi.to_java(Java::double))
26
26
  end
27
27
  # コルモゴルフ・スミルノフ検定プロット(1標本)
28
28
  #
@@ -33,11 +33,11 @@ module Num4NormalityLib
33
33
  # @example
34
34
  # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
35
35
  # Num4NormalityLib.ksplot("LDH", xi)
36
- # => kstest.jpeg
36
+ # => ksplot.jpeg
37
37
  # @note
38
38
  # グラフは、jfreechartを使用
39
39
  def ksplot(dname, xi)
40
- Normality.ksplot(dname, xi.to_java(Java::double))
40
+ Normality.ksPlot(dname, xi.to_java(Java::double))
41
41
  end
42
42
  # Q-Q and コルモゴルフ・スミルノフ検定プロット(1標本)
43
43
  #
@@ -52,7 +52,7 @@ module Num4NormalityLib
52
52
  # @note
53
53
  # グラフは、jfreechartを使用
54
54
  def qqksplot(dname, xi)
55
- Normality.qqksplot(dname, xi.to_java(Java::double))
55
+ Normality.qqksPlot(dname, xi.to_java(Java::double))
56
56
  end
57
57
  # P-Pプロット
58
58
  #
@@ -67,14 +67,14 @@ module Num4NormalityLib
67
67
  # @note
68
68
  # グラフは、jfreechartを使用
69
69
  def ppplot(dname, xi)
70
- Normality.ppplot(dname, xi.to_java(Java::double))
70
+ Normality.ppPlot(dname, xi.to_java(Java::double))
71
71
  end
72
72
  # P-P and コルモゴルフ・スミルノフ検定プロット(1標本)
73
73
  #
74
74
  # @overload ppksplot(dname, xi)
75
75
  # @param [String] dname データ名
76
76
  # @param [Array] xi データ(double[])
77
- # @return [void] ppplot.jpegファイルを出力
77
+ # @return [void] ppksplot.jpegファイルを出力
78
78
  # @example
79
79
  # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
80
80
  # Num4NormalityLib.ppksplot("LDH", xi)
@@ -82,7 +82,7 @@ module Num4NormalityLib
82
82
  # @note
83
83
  # グラフは、jfreechartを使用
84
84
  def ppksplot(dname, xi)
85
- Normality.ppksplot(dname, xi.to_java(Java::double))
85
+ Normality.ppksPlot(dname, xi.to_java(Java::double))
86
86
  end
87
87
  # コルモゴルフ・スミルノフ検定(1標本)
88
88
  #
@@ -94,7 +94,7 @@ module Num4NormalityLib
94
94
  # Num4NormalityLib.kstest(xi)
95
95
  # => false
96
96
  def kstest(xi)
97
- Normality.kstest(xi.to_java(Java::double))
97
+ Normality.ksTest(xi.to_java(Java::double))
98
98
  end
99
99
 
100
100
  # タコスディーノ検定(歪度)
@@ -107,7 +107,7 @@ module Num4NormalityLib
107
107
  # Num4NormalityLib.skewnesstest(xi)
108
108
  # => false
109
109
  def skewnesstest(xi)
110
- Normality.skewnesstest(xi.to_java(Java::double))
110
+ Normality.skewnessTest(xi.to_java(Java::double))
111
111
  end
112
112
  # タコスディーノ検定(尖度)
113
113
  #
@@ -119,7 +119,7 @@ module Num4NormalityLib
119
119
  # Num4NormalityLib.kurtosistest(xi)
120
120
  # => false
121
121
  def kurtosistest(xi)
122
- Normality.kurtosistest(xi.to_java(Java::double))
122
+ Normality.kurtosisTest(xi.to_java(Java::double))
123
123
  end
124
124
  # オムニバス検定
125
125
  #
@@ -131,7 +131,19 @@ module Num4NormalityLib
131
131
  # Num4NormalityLib.omnibustest(xi)
132
132
  # => false
133
133
  def omnibustest(xi)
134
- Normality.omnibustest(xi.to_java(Java::double))
134
+ Normality.omnibusTest(xi.to_java(Java::double))
135
+ end
136
+ # Anderson-darling検定
137
+ #
138
+ # @overload adtest(xi)
139
+ # @param [Array] xi データ(double[])
140
+ # @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
141
+ # @example
142
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
143
+ # Num4NormalityLib.adtest(xi)
144
+ # => false
145
+ def adtest(xi)
146
+ Normality.adTest(xi.to_java(Java::double))
135
147
  end
136
148
  end
137
149
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4normality
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: java
6
6
  authors:
7
7
  - siranovel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-26 00:00:00.000000000 Z
11
+ date: 2024-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake