num4normality 0.0.1-java → 0.0.3-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: 6be725c6a9b20d5c4ca46f2543c96e293be67c9bcf2638816cb29331eadb8180
4
- data.tar.gz: d18160bcab51e0760a2e4b533be709235f06d18b2150bb05cc4a9ef66a96179f
3
+ metadata.gz: a735c6ff8848bf8f8c56b7e1bc0b1794a5675ab93d3e9a13477cf8d3041b50d9
4
+ data.tar.gz: 87b102e787cd5835ce2c78ebf11e0abf8a2092462cbf9ff7fd73e06a3884d43b
5
5
  SHA512:
6
- metadata.gz: 1e9c798eaa8043b712da5940f361c8adffa82f4436cccc34843286baad3109d481354fea2a8f6f914f4f8205f2ff0393c31f47cdde589cdb408506257ba06491
7
- data.tar.gz: 9f257c671f62f84c224a03641d802d7f275a6d042c80d336f4a666283def61b53a169527c6ec854676ff3344da8e60bcb2a0e6716b8afa8e31d700172d91fed2
6
+ metadata.gz: c808547090cef242d28bbfbc46243fc44726a69d22c18a03916f76ccb7e91b58b7c3a074f1fe7fd7a17c492da9978197358db941c20c1255fcd90773cc28e54d
7
+ data.tar.gz: ff78669f413ae51fae98f4fb683981b12793f2a93eaeb17438124e1ad3771b476a34955e4482bc669bcd4044ec31a95f08e9c0c93b89a802b03fe7d664d03c4c
data/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.3] - 2023-12-18
6
+
7
+ ### add
8
+ - add function of kstest.
9
+
10
+ ### change
11
+ - chg function name to ksplot from kstest.
12
+
13
+ ## [0.0.2] - 2023-12-12
14
+
15
+ ### change
16
+ - chg Q-QPlot and kstest
17
+ - chg from one-side test to two side test.
18
+
5
19
  ## [0.0.1] - 2023-12-05
6
20
 
7
21
  ### Fixed
@@ -21,8 +21,11 @@ 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.stat.regression.SimpleRegression;
24
25
  import java.util.Arrays;
25
26
  import java.text.DecimalFormat;
27
+
28
+ import org.apache.commons.math3.stat.inference.TestUtils;
26
29
  public class Normality {
27
30
  public static void qqplot(String dname, double[] xi) {
28
31
  ChartPlot plot = new QQPlot();
@@ -30,13 +33,18 @@ public class Normality {
30
33
 
31
34
  plot.writeJPEG("qqplot.jpeg", chart, 800, 500);
32
35
  }
33
- public static void kstest(String dname, double[] xi) {
34
- ChartPlot plot = new KSTest();
36
+ public static void ksplot(String dname, double[] xi) {
37
+ ChartPlot plot = new KSPlot();
35
38
  JFreeChart chart = plot.createChart(dname, xi);
36
39
 
37
- plot.writeJPEG("kstest.jpeg", chart, 800, 500);
40
+ plot.writeJPEG("ksplot.jpeg", chart, 800, 500);
38
41
 
39
42
  }
43
+ public static boolean kstest(double[] xi) {
44
+ KSTest ks = new KSTest();
45
+
46
+ return ks.test(xi);
47
+ }
40
48
  public static boolean skewnesstest(double[] xi) {
41
49
  DAgostinosTest daigo = new SkewnessTest();
42
50
 
@@ -52,6 +60,10 @@ public class Normality {
52
60
 
53
61
 
54
62
  private interface ChartPlot {
63
+ /* フィールド */
64
+ static final double CLASS_MIN = -4.0;
65
+ static final double CLASS_MAX = 4.0;
66
+ /* メゾット */
55
67
  JFreeChart createChart(String dname, double[] xi);
56
68
  default void writeJPEG(String fname, JFreeChart chart, int width, int height) {
57
69
  File file = new File(fname);
@@ -70,30 +82,46 @@ public class Normality {
70
82
  // Q-Qplot
71
83
  private static class QQPlot implements ChartPlot {
72
84
  private DescriptiveStatistics stat = null;
73
- private double m = 0.0;
74
- private double sd = 0.0;
85
+ private NormalDistribution ndist = null;
75
86
  public QQPlot() {
76
87
  stat = new DescriptiveStatistics();
88
+ ndist = new NormalDistribution(0, 1);
77
89
  }
78
- public JFreeChart createChart(String dname, double[] xi) {
90
+ private double[][] createData(double[] xi) {
91
+ int n = xi.length;
92
+ Arrays.sort(xi);
79
93
  Arrays.stream(xi).forEach(stat::addValue);
80
- m = stat.getMean(); // 平均
81
- sd = stat.getStandardDeviation();// 標準偏差
94
+ double sum = stat.getSum();
95
+ double[][] data = new double[n][2];
96
+ double p = 0.0;
82
97
 
83
- XYPlot plot = createPlot(dname, xi);
98
+ for (int i = 0; i < n; i++) {
99
+ p += xi[i] / sum;
100
+ double x =
101
+ ndist.inverseCumulativeProbability(p * (i + 1.0) / (n + 1.0));
102
+
103
+ data[i][0] = x;
104
+ data[i][1] = xi[i];
105
+ }
106
+ return data;
107
+ }
108
+ public JFreeChart createChart(String dname, double[] xi) {
109
+ double[][] data = createData(xi);
110
+
111
+ XYPlot plot = createPlot(dname, data);
84
112
  /*--- 横軸 ---*/
85
113
  NumberAxis domainAxis = new NumberAxis("標準正規分布");
86
114
 
87
115
  plot.setDomainAxis(domainAxis);
88
116
  domainAxis.setLowerMargin(0.03);
89
117
  domainAxis.setUpperMargin(0.03);
90
- domainAxis.setLowerBound(-4);
91
- domainAxis.setUpperBound(4);
118
+ domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
119
+ domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
92
120
 
93
121
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
94
122
  return new JFreeChart("正規性の検定", plot);
95
123
  }
96
- private XYPlot createPlot(String dname, double[] xi) {
124
+ private XYPlot createPlot(String dname, double[][] data) {
97
125
  XYItemRenderer renderer0 = new XYLineAndShapeRenderer(false, true);
98
126
  XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
99
127
  XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
@@ -114,48 +142,44 @@ public class Normality {
114
142
  plot.setRangeAxis(valueAxis0);
115
143
 
116
144
  plot.setRenderer(0, renderer0);
117
- plot.setDataset(0, createDataset0(dname, xi));
145
+ plot.setDataset(0, createDataset0(dname, data));
118
146
 
119
147
  plot.setRenderer(1, renderer1);
120
- plot.setDataset(1, createDataset1());
148
+ plot.setDataset(1, createDataset1(data));
121
149
 
122
150
  return plot;
123
151
  }
124
- private XYSeriesCollection createDataset0(String dname, double[] xi) {
125
- double den = sd / Math.sqrt(xi.length);
126
- XYSeries series = new XYSeries(dname);
152
+ private XYSeriesCollection createDataset0(String dname, double[][] data) {
153
+ XYSeries cu = new XYSeries(dname);
127
154
 
128
- for (int i = 0; i < xi.length; i++) {
129
- double x = (xi[i] - m) / den;
130
-
131
- series.add(x, xi[i]);
155
+ for (int i = 0; i < data.length; i++) {
156
+ cu.add(data[i][0], data[i][1]);
132
157
  }
133
- XYSeriesCollection data = new XYSeriesCollection();
158
+ XYSeriesCollection series = new XYSeriesCollection();
134
159
 
135
- data.addSeries(series);
136
- return data;
160
+ series.addSeries(cu);
161
+ return series;
137
162
  }
138
- private XYSeriesCollection createDataset1() {
139
- XYSeries series = new XYSeries("累積");
140
- NormalDistribution ndist = new NormalDistribution(0, 1);
141
-
142
- for (double p = 0.001; p < 1.0; p += 0.01) {
143
- double x = ndist.inverseCumulativeProbability(p);
144
-
145
- series.add(x, x * sd + m);
163
+ private XYSeriesCollection createDataset1(double[][] data) {
164
+ SimpleRegression simpleReg = new SimpleRegression(true);
165
+ XYSeries cu = new XYSeries("累積");
166
+
167
+ simpleReg.addData(data);
168
+ double a = simpleReg.getSlope();
169
+ double b = simpleReg.getIntercept();
170
+
171
+ for (double x = ChartPlot.CLASS_MIN; x < ChartPlot.CLASS_MAX; x += 0.01) {
172
+ double y = a * x + b;
173
+
174
+ cu.add(x, y);
146
175
  }
147
-
148
- XYSeriesCollection data = new XYSeriesCollection();
149
- data.addSeries(series);
150
- return data;
176
+ XYSeriesCollection series = new XYSeriesCollection();
177
+ series.addSeries(cu);
178
+ return series;
151
179
  }
152
180
  }
153
181
  // コルモゴルフ・スミルノフ検定
154
- private static class KSTest implements ChartPlot {
155
- private NormalDistribution ndist = null;
156
- public KSTest() {
157
- ndist = new NormalDistribution(0, 1);
158
- }
182
+ private static class KSPlot implements ChartPlot {
159
183
  public JFreeChart createChart(String dname, double[] xi) {
160
184
  NumberAxis domainAxis = new NumberAxis("標準正規分布");
161
185
  XYPlot plot = createPlot(dname, xi);
@@ -203,39 +227,57 @@ public class Normality {
203
227
  return plot;
204
228
  }
205
229
  private XYSeriesCollection createDataset0(String dname, double[] xi) {
230
+ int n = xi.length;
206
231
  Arrays.sort(xi);
207
232
  DescriptiveStatistics stat = new DescriptiveStatistics();
208
233
  Arrays.stream(xi).forEach(stat::addValue);
209
234
  double m = stat.getMean(); // 平均
210
235
  double sd = stat.getStandardDeviation();// 標準偏差
211
- double den = sd / Math.sqrt(xi.length);
212
236
  double sum = stat.getSum();
213
237
  double p = 0.0;
214
238
 
215
- XYSeries series = new XYSeries(dname);
216
- for (int n = 0; n < xi.length; n++) {
217
- double x = (xi[n] - m) / den;
218
-
219
- p += xi[n] / sum;
220
- series.add(x, p);
239
+ XYSeries cu = new XYSeries(dname);
240
+ for (int i = 0; i < n; i++) {
241
+ double x = (xi[i] - m) / sd;
242
+
243
+ p += xi[i] / sum;
244
+ cu.add(x, p);
221
245
  }
222
- XYSeriesCollection data = new XYSeriesCollection();
246
+ XYSeriesCollection series = new XYSeriesCollection();
223
247
 
224
- data.addSeries(series);
225
- return data;
248
+ series.addSeries(cu);
249
+ return series;
226
250
  }
227
251
  private XYSeriesCollection createDataset1() {
228
- XYSeries series = new XYSeries("累積");
252
+ NormalDistribution ndist = new NormalDistribution(0, 1);
253
+ XYSeries cu = new XYSeries("累積p");
229
254
 
230
255
  for (double x = -4; x < 4; x += 0.01) {
231
256
  double y = ndist.cumulativeProbability(x);
232
257
 
233
- series.add(x, y);
258
+ cu.add(x, y);
234
259
  }
235
- XYSeriesCollection data = new XYSeriesCollection();
260
+ XYSeriesCollection series = new XYSeriesCollection();
236
261
 
237
- data.addSeries(series);
238
- return data;
262
+ series.addSeries(cu);
263
+ return series;
264
+ }
265
+ }
266
+ private static class KSTest {
267
+ public boolean test(double[] xi) {
268
+ double[] data = new double[xi.length];
269
+ Arrays.sort(xi);
270
+ DescriptiveStatistics stat = new DescriptiveStatistics();
271
+ Arrays.stream(xi).forEach(stat::addValue);
272
+ double m = stat.getMean(); // 平均
273
+ double sd = stat.getStandardDeviation();// 標準偏差
274
+ NormalDistribution ndist = new NormalDistribution(0, 1);
275
+
276
+ for (int i = 0; i < xi.length; i++) {
277
+ data[i] = (xi[i] - m) / sd;
278
+ }
279
+ boolean ret = TestUtils.kolmogorovSmirnovTest(ndist, data, 0.05);
280
+ return ret;
239
281
  }
240
282
  }
241
283
  // タコスディーノ検定(歪度)
@@ -254,7 +296,7 @@ public class Normality {
254
296
  }
255
297
 
256
298
  public boolean test(double statistic, double a) {
257
- double ua_2 = ndist.inverseCumulativeProbability(1.0 - a);
299
+ double ua_2 = ndist.inverseCumulativeProbability(1.0 - a / 2.0);
258
300
 
259
301
  return (Math.abs(statistic) > cnvb2tob2p(ua_2))
260
302
  ? true : false;
@@ -285,7 +327,7 @@ public class Normality {
285
327
  }
286
328
  public boolean test(double statistic, double a) {
287
329
  boolean ret = false;
288
- double ua_2 = ndist.inverseCumulativeProbability(1.0 - a);
330
+ double ua_2 = ndist.inverseCumulativeProbability(1.0 - a / 2.0);
289
331
  double b2p = cnvb2tob2p(statistic);
290
332
 
291
333
  double r_val = ua_2 + Math.sqrt(6.0 / n) * (ua_2 * ua_2 - 1.0);
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,7 +70,7 @@ 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)
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.1
4
+ version: 0.0.3
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-05 00:00:00.000000000 Z
11
+ date: 2023-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake