num4normality 0.0.2-java → 0.0.4-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: b89f71cba5d40084df7feae50644d8df12b9501f486f9bdd3a534d2dce73a4df
4
- data.tar.gz: fc2f02a141dd85f32834cae5c44607fd601b5b1e13902bda51cbbd4f4bddc98a
3
+ metadata.gz: 58c526a9c71f5640706a0e7b57c5491b46bc93b7194ed7ef1c6031d49986d0a4
4
+ data.tar.gz: 51966044b28deae90af5f764bac62692243312cfcbd6a6013d4a54b8389d009f
5
5
  SHA512:
6
- metadata.gz: c7334b33fa778de4dd19980df1f5bf5933ef0c35e9b068775ecec0a091613007c9b25b4952ad5cb7714be3950f9a4bf640d7916a488424b65afef75857232b04
7
- data.tar.gz: d9924ebad4a9a5315453fadc8f3ba8eec7583ae2d02356294a5b53bf9acbbbf4d26fbad3f0b651f216c862da1763fc2339b00fd994061c2a0b3fd7433794dec7
6
+ metadata.gz: f7c158d1d3f06e7390757264b06b70ce6081f8d57e4b6d84627a10ba834b1ae8422d38e18216e7faee81f9fc15d649b701af3f42131be504225e31c1131fdc55
7
+ data.tar.gz: f109895d5434021a9073180c4717b1db3fa081f9ed89d116d8883ff03318d9be3739f40227f8dc713f8fc576984497bd84889099f9aefec689ee12a60d01978e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.4] - 2023-12-21
6
+
7
+ ### add
8
+ - add function of omnibustest.
9
+
10
+ ### Fixed
11
+ - fix fuction of kurtosistest.
12
+
13
+ ## [0.0.3] - 2023-12-18
14
+
15
+ ### add
16
+ - add function of kstest.
17
+
18
+ ### change
19
+ - chg function name to ksplot from kstest.
20
+
5
21
  ## [0.0.2] - 2023-12-12
6
22
 
7
23
  ### change
@@ -21,9 +21,12 @@ import java.io.IOException;
21
21
 
22
22
  import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
23
23
  import org.apache.commons.math3.distribution.NormalDistribution;
24
+ import org.apache.commons.math3.distribution.ChiSquaredDistribution;
24
25
  import org.apache.commons.math3.stat.regression.SimpleRegression;
25
26
  import java.util.Arrays;
26
27
  import java.text.DecimalFormat;
28
+
29
+ import org.apache.commons.math3.stat.inference.TestUtils;
27
30
  public class Normality {
28
31
  public static void qqplot(String dname, double[] xi) {
29
32
  ChartPlot plot = new QQPlot();
@@ -31,13 +34,18 @@ public class Normality {
31
34
 
32
35
  plot.writeJPEG("qqplot.jpeg", chart, 800, 500);
33
36
  }
34
- public static void kstest(String dname, double[] xi) {
35
- ChartPlot plot = new KSTest();
37
+ public static void ksplot(String dname, double[] xi) {
38
+ ChartPlot plot = new KSPlot();
36
39
  JFreeChart chart = plot.createChart(dname, xi);
37
40
 
38
- plot.writeJPEG("kstest.jpeg", chart, 800, 500);
41
+ plot.writeJPEG("ksplot.jpeg", chart, 800, 500);
39
42
 
40
43
  }
44
+ public static boolean kstest(double[] xi) {
45
+ KSTest ks = new KSTest();
46
+
47
+ return ks.test(xi);
48
+ }
41
49
  public static boolean skewnesstest(double[] xi) {
42
50
  DAgostinosTest daigo = new SkewnessTest();
43
51
 
@@ -50,9 +58,19 @@ public class Normality {
50
58
  double b2 = daigo.calcTestStatistic(xi);
51
59
  return daigo.test(b2, 0.05);
52
60
  }
61
+ public static boolean omnibustest(double[] xi) {
62
+ DAgostinosTest daigo = new OmnibusTest();
63
+
64
+ double x = daigo.calcTestStatistic(xi);
65
+ return daigo.test(x, 0.05);
66
+ }
53
67
 
54
68
 
55
69
  private interface ChartPlot {
70
+ /* フィールド */
71
+ static final double CLASS_MIN = -4.0;
72
+ static final double CLASS_MAX = 4.0;
73
+ /* メゾット */
56
74
  JFreeChart createChart(String dname, double[] xi);
57
75
  default void writeJPEG(String fname, JFreeChart chart, int width, int height) {
58
76
  File file = new File(fname);
@@ -104,8 +122,8 @@ public class Normality {
104
122
  plot.setDomainAxis(domainAxis);
105
123
  domainAxis.setLowerMargin(0.03);
106
124
  domainAxis.setUpperMargin(0.03);
107
- domainAxis.setLowerBound(-4);
108
- domainAxis.setUpperBound(4);
125
+ domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
126
+ domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
109
127
 
110
128
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
111
129
  return new JFreeChart("正規性の検定", plot);
@@ -157,7 +175,7 @@ public class Normality {
157
175
  double a = simpleReg.getSlope();
158
176
  double b = simpleReg.getIntercept();
159
177
 
160
- for (double x = -4.0; x < 4.0; x += 0.01) {
178
+ for (double x = ChartPlot.CLASS_MIN; x < ChartPlot.CLASS_MAX; x += 0.01) {
161
179
  double y = a * x + b;
162
180
 
163
181
  cu.add(x, y);
@@ -168,11 +186,7 @@ public class Normality {
168
186
  }
169
187
  }
170
188
  // コルモゴルフ・スミルノフ検定
171
- private static class KSTest implements ChartPlot {
172
- private NormalDistribution ndist = null;
173
- public KSTest() {
174
- ndist = new NormalDistribution(0, 1);
175
- }
189
+ private static class KSPlot implements ChartPlot {
176
190
  public JFreeChart createChart(String dname, double[] xi) {
177
191
  NumberAxis domainAxis = new NumberAxis("標準正規分布");
178
192
  XYPlot plot = createPlot(dname, xi);
@@ -230,13 +244,11 @@ public class Normality {
230
244
  double p = 0.0;
231
245
 
232
246
  XYSeries cu = new XYSeries(dname);
233
- for (int i = 0; i < xi.length; i++) {
247
+ for (int i = 0; i < n; i++) {
234
248
  double x = (xi[i] - m) / sd;
249
+
235
250
  p += xi[i] / sum;
236
- double y =
237
- ndist.inverseCumulativeProbability((i + 1.0) / (n + 1.0));
238
-
239
- cu.add(Math.min(x, y), p);
251
+ cu.add(x, p);
240
252
  }
241
253
  XYSeriesCollection series = new XYSeriesCollection();
242
254
 
@@ -244,7 +256,8 @@ public class Normality {
244
256
  return series;
245
257
  }
246
258
  private XYSeriesCollection createDataset1() {
247
- XYSeries cu = new XYSeries("累積");
259
+ NormalDistribution ndist = new NormalDistribution(0, 1);
260
+ XYSeries cu = new XYSeries("累積p");
248
261
 
249
262
  for (double x = -4; x < 4; x += 0.01) {
250
263
  double y = ndist.cumulativeProbability(x);
@@ -257,6 +270,23 @@ public class Normality {
257
270
  return series;
258
271
  }
259
272
  }
273
+ private static class KSTest {
274
+ public boolean test(double[] xi) {
275
+ double[] data = new double[xi.length];
276
+ Arrays.sort(xi);
277
+ DescriptiveStatistics stat = new DescriptiveStatistics();
278
+ Arrays.stream(xi).forEach(stat::addValue);
279
+ double m = stat.getMean(); // 平均
280
+ double sd = stat.getStandardDeviation();// 標準偏差
281
+ NormalDistribution ndist = new NormalDistribution(0, 1);
282
+
283
+ for (int i = 0; i < xi.length; i++) {
284
+ data[i] = (xi[i] - m) / sd;
285
+ }
286
+ boolean ret = TestUtils.kolmogorovSmirnovTest(ndist, data, 0.05);
287
+ return ret;
288
+ }
289
+ }
260
290
  // タコスディーノ検定(歪度)
261
291
  private static class SkewnessTest implements DAgostinosTest {
262
292
  private long n = 0;
@@ -275,14 +305,14 @@ public class Normality {
275
305
  public boolean test(double statistic, double a) {
276
306
  double ua_2 = ndist.inverseCumulativeProbability(1.0 - a / 2.0);
277
307
 
278
- return (Math.abs(statistic) > cnvb2tob2p(ua_2))
308
+ return (Math.abs(statistic) > calcNormStatic(ua_2))
279
309
  ? true : false;
280
310
  }
281
- private double cnvb2tob2p(double ua_2) {
311
+ private double calcNormStatic(double ua_2) {
282
312
  double el = (n + 1.0) * (n + 1.0) * (n + 3.0) * (n + 5.0); // 分子
283
313
  double den = 24 * n * (n - 2.0) * (n - 3.0); // 分母
284
314
  double b2p = Math.sqrt(el / den) *
285
- (long)(ua_2 + 3.0 / (2.0 * n) * (ua_2 * ua_2 * ua_2 - 3 * ua_2));
315
+ (ua_2 + 3.0 / (2.0 * n) * (ua_2 * ua_2 * ua_2 - 3 * ua_2));
286
316
 
287
317
  return b2p;
288
318
  }
@@ -299,33 +329,43 @@ public class Normality {
299
329
 
300
330
  Arrays.stream(xi).forEach(stat::addValue);
301
331
  n = stat.getN();
302
-
303
332
  return stat.getKurtosis();
304
333
  }
305
334
  public boolean test(double statistic, double a) {
306
335
  boolean ret = false;
307
336
  double ua_2 = ndist.inverseCumulativeProbability(1.0 - a / 2.0);
308
- double b2p = cnvb2tob2p(statistic);
309
337
 
310
338
  double r_val = ua_2 + Math.sqrt(6.0 / n) * (ua_2 * ua_2 - 1.0);
311
339
  double l_val = -1.0 * ua_2 + Math.sqrt(6.0 / n) * (ua_2 * ua_2 - 1.0);
312
340
 
313
- if (b2p > r_val) {
341
+ if (statistic > r_val) {
314
342
  ret = true;
315
343
  }
316
- if (b2p < l_val) {
344
+ if (statistic < l_val) {
317
345
  ret = true;
318
346
  }
319
347
  return ret;
320
348
  }
321
- private double cnvb2tob2p(double b2) {
322
- double el = (n + 1.0) * (n + 1.0) * (n + 3.0) * (n + 5.0); // 分子
323
- double den = 24 * n * (n - 2.0) * (n - 3.0); // 分母
349
+ }
350
+ // オムニバス検定
351
+ private static class OmnibusTest implements DAgostinosTest {
352
+ private DAgostinosTest skewness = null;
353
+ private DAgostinosTest kurtosis = null;
354
+ public OmnibusTest() {
355
+ skewness = new SkewnessTest();
356
+ kurtosis = new KurtosisTest();
357
+ }
358
+ public double calcTestStatistic(double[] xi) {
359
+ double x1 = skewness.calcTestStatistic(xi);
360
+ double x2 = kurtosis.calcTestStatistic(xi);
324
361
 
325
- double b2p = Math.sqrt(el / den) *
326
- (long)(b2 - 3 * (n - 1.0) / (n + 3.0));
327
-
328
- return b2p;
362
+ return x1 * x1 + x2 * x2;
363
+ }
364
+ public boolean test(double statistic, double a) {
365
+ ChiSquaredDistribution chi2Dist = new ChiSquaredDistribution(2);
366
+ double p = chi2Dist.cumulativeProbability(statistic);
367
+
368
+ return (p < a) ? true : false;
329
369
  }
330
370
  }
331
371
  }
data/lib/num4normality.rb CHANGED
@@ -16,7 +16,7 @@ module Num4NormalityLib
16
16
  # @param [Array] xi データ(double[])
17
17
  # @return [void] qqplot.jpegファイルを出力
18
18
  # @example
19
- # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209]
19
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
20
20
  # Num4NormalityLib.qqplot("LDH", xi)
21
21
  # => qqplot.jpeg
22
22
  # @note
@@ -24,28 +24,41 @@ module Num4NormalityLib
24
24
  def qqplot(dname, xi)
25
25
  Normality.qqplot(dname, xi.to_java(Java::double))
26
26
  end
27
- # コルモゴルフ・スミルノフ検定
27
+ # コルモゴルフ・スミルノフ検定プロット(1標本)
28
28
  #
29
- # @overload kstest(dname, xi)
29
+ # @overload ksplot(dname, xi)
30
30
  # @param [String] dname データ名
31
31
  # @param [Array] xi データ(double[])
32
- # @return [void] kstest.jpegファイルを出力
32
+ # @return [void] ksplot.jpegファイルを出力
33
33
  # @example
34
- # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209]
35
- # Num4NormalityLib.kstest("LDH", xi)
34
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
35
+ # Num4NormalityLib.ksplot("LDH", xi)
36
36
  # => kstest.jpeg
37
37
  # @note
38
38
  # グラフは、jfreechartを使用
39
- def kstest(dname, xi)
40
- Normality.kstest(dname, xi.to_java(Java::double))
39
+ def ksplot(dname, xi)
40
+ Normality.ksplot(dname, xi.to_java(Java::double))
41
41
  end
42
+ # コルモゴルフ・スミルノフ検定(1標本)
43
+ #
44
+ # @overload kstest(xi)
45
+ # @param [Array] xi データ(double[])
46
+ # @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
47
+ # @example
48
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
49
+ # Num4NormalityLib.kstest(xi)
50
+ # => false
51
+ def kstest(xi)
52
+ Normality.kstest(xi.to_java(Java::double))
53
+ end
54
+
42
55
  # タコスディーノ検定(歪度)
43
56
  #
44
57
  # @overload skewnesstest(xi)
45
58
  # @param [Array] xi データ(double[])
46
59
  # @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
47
60
  # @example
48
- # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209]
61
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
49
62
  # Num4NormalityLib.skewnesstest(xi)
50
63
  # => false
51
64
  def skewnesstest(xi)
@@ -57,12 +70,24 @@ module Num4NormalityLib
57
70
  # @param [Array] xi データ(double[])
58
71
  # @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
59
72
  # @example
60
- # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209]
73
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
61
74
  # Num4NormalityLib.kurtosistest(xi)
62
75
  # => false
63
76
  def kurtosistest(xi)
64
77
  Normality.kurtosistest(xi.to_java(Java::double))
65
78
  end
79
+ # オムニバス検定
80
+ #
81
+ # @overload omnibustest(xi)
82
+ # @param [Array] xi データ(double[])
83
+ # @return [boolean] 検定結果(true:棄却域内 false:棄却域外)
84
+ # @example
85
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
86
+ # Num4NormalityLib.omnibustest(xi)
87
+ # => false
88
+ def omnibustest(xi)
89
+ Normality.omnibustest(xi.to_java(Java::double))
90
+ end
66
91
  end
67
92
  end
68
93
 
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.2
4
+ version: 0.0.4
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-12 00:00:00.000000000 Z
11
+ date: 2023-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake