num4normality 0.0.4-java → 0.0.6-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58c526a9c71f5640706a0e7b57c5491b46bc93b7194ed7ef1c6031d49986d0a4
4
- data.tar.gz: 51966044b28deae90af5f764bac62692243312cfcbd6a6013d4a54b8389d009f
3
+ metadata.gz: 9803b3bcb0408424c204ebda20e538aa178a2f35573e280dd222add412987cff
4
+ data.tar.gz: 1258e8ad4f82177478d2f35d689f56b4d8d58d1177f7a6f45896f05ee6c50189
5
5
  SHA512:
6
- metadata.gz: f7c158d1d3f06e7390757264b06b70ce6081f8d57e4b6d84627a10ba834b1ae8422d38e18216e7faee81f9fc15d649b701af3f42131be504225e31c1131fdc55
7
- data.tar.gz: f109895d5434021a9073180c4717b1db3fa081f9ed89d116d8883ff03318d9be3739f40227f8dc713f8fc576984497bd84889099f9aefec689ee12a60d01978e
6
+ metadata.gz: 16ad0542c142e6b74871df83ae43fa4ac62a9bd977e3bd466b9a520f849f0852460a1b71051b072c5de14b93a4b7530e2ef1821300b7b02303cfd1b307f0a43c
7
+ data.tar.gz: c49ca950c085da6760c1d0b171b7daad90354c7804e6521711e7469373a47aca58bec5d7fad47fb5d6c1a882c754ff389bdcae8e957fa93467cdc9db500ffa8a
@@ -4,6 +4,7 @@ import org.jfree.chart.ChartFactory;
4
4
  import org.jfree.data.xy.XYSeriesCollection;
5
5
  import org.jfree.data.xy.XYSeries;
6
6
 
7
+ import org.jfree.chart.plot.CombinedDomainXYPlot;
7
8
  import org.jfree.chart.plot.XYPlot;
8
9
  import org.jfree.chart.axis.NumberAxis;
9
10
  import org.jfree.chart.axis.ValueAxis;
@@ -29,18 +30,24 @@ import java.text.DecimalFormat;
29
30
  import org.apache.commons.math3.stat.inference.TestUtils;
30
31
  public class Normality {
31
32
  public static void qqplot(String dname, double[] xi) {
32
- ChartPlot plot = new QQPlot();
33
- JFreeChart chart = plot.createChart(dname, xi);
33
+ ChartPlot plot = new QQChartPlot();
34
+ JFreeChart chart = plot.createChart("正規Q-Qプロット", dname, xi);
34
35
 
35
36
  plot.writeJPEG("qqplot.jpeg", chart, 800, 500);
36
37
  }
37
38
  public static void ksplot(String dname, double[] xi) {
38
- ChartPlot plot = new KSPlot();
39
- JFreeChart chart = plot.createChart(dname, xi);
39
+ ChartPlot plot = new KSChartPlot();
40
+ JFreeChart chart = plot.createChart("コルモゴルフ・スミルノフ検定", dname, xi);
40
41
 
41
42
  plot.writeJPEG("ksplot.jpeg", chart, 800, 500);
42
43
 
43
44
  }
45
+ public static void qqksplot(String dname, double[] xi) {
46
+ ChartPlot plot = new QQKSChartPlot();
47
+ JFreeChart chart = plot.createChart("Q-Q and コルモゴルフ・スミルノフ検定", dname, xi);
48
+
49
+ plot.writeJPEG("qqksplot.jpeg", chart, 1000, 800);
50
+ }
44
51
  public static boolean kstest(double[] xi) {
45
52
  KSTest ks = new KSTest();
46
53
 
@@ -65,13 +72,15 @@ public class Normality {
65
72
  return daigo.test(x, 0.05);
66
73
  }
67
74
 
68
-
75
+ /*********************************/
76
+ /* interface define */
77
+ /*********************************/
69
78
  private interface ChartPlot {
70
79
  /* フィールド */
71
80
  static final double CLASS_MIN = -4.0;
72
81
  static final double CLASS_MAX = 4.0;
73
82
  /* メゾット */
74
- JFreeChart createChart(String dname, double[] xi);
83
+ JFreeChart createChart(String title, String dname, double[] xi);
75
84
  default void writeJPEG(String fname, JFreeChart chart, int width, int height) {
76
85
  File file = new File(fname);
77
86
  try {
@@ -81,43 +90,22 @@ public class Normality {
81
90
  }
82
91
  }
83
92
  }
93
+ private interface CreatePlot {
94
+ XYPlot createPlot(String dname, double[] xi);
95
+ }
84
96
  private interface DAgostinosTest {
85
97
  double calcTestStatistic(double[] xi);
86
98
  boolean test(double statistic, double a);
87
99
  }
88
-
100
+ /*********************************/
101
+ /* Class define */
102
+ /*********************************/
89
103
  // Q-Qplot
90
- private static class QQPlot implements ChartPlot {
91
- private DescriptiveStatistics stat = null;
92
- private NormalDistribution ndist = null;
93
- public QQPlot() {
94
- stat = new DescriptiveStatistics();
95
- ndist = new NormalDistribution(0, 1);
96
- }
97
- private double[][] createData(double[] xi) {
98
- int n = xi.length;
99
- Arrays.sort(xi);
100
- Arrays.stream(xi).forEach(stat::addValue);
101
- double sum = stat.getSum();
102
- double[][] data = new double[n][2];
103
- double p = 0.0;
104
-
105
- for (int i = 0; i < n; i++) {
106
- p += xi[i] / sum;
107
- double x =
108
- ndist.inverseCumulativeProbability(p * (i + 1.0) / (n + 1.0));
109
-
110
- data[i][0] = x;
111
- data[i][1] = xi[i];
112
- }
113
- return data;
114
- }
115
- public JFreeChart createChart(String dname, double[] xi) {
116
- double[][] data = createData(xi);
117
-
118
- XYPlot plot = createPlot(dname, data);
104
+ private static class QQChartPlot implements ChartPlot {
105
+ public JFreeChart createChart(String title, String dname, double[] xi) {
106
+ XYPlot plot = createPlot(dname, xi);
119
107
  /*--- 横軸 ---*/
120
- NumberAxis domainAxis = new NumberAxis("標準正規分布");
108
+ NumberAxis domainAxis = new NumberAxis("期待値");
121
109
 
122
110
  plot.setDomainAxis(domainAxis);
123
111
  domainAxis.setLowerMargin(0.03);
@@ -126,150 +114,231 @@ public class Normality {
126
114
  domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
127
115
 
128
116
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
129
- return new JFreeChart("正規性の検定", plot);
117
+ return new JFreeChart(title, plot);
130
118
  }
131
- private XYPlot createPlot(String dname, double[][] data) {
132
- XYItemRenderer renderer0 = new XYLineAndShapeRenderer(false, true);
133
- XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
134
- XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
135
-
136
- renderer0.setDefaultToolTipGenerator(toolTipGenerator);
137
- renderer0.setURLGenerator(null);
138
- renderer1.setDefaultToolTipGenerator(toolTipGenerator);
139
- renderer1.setURLGenerator(null);
140
-
141
- XYPlot plot = new XYPlot();
142
- plot.setOrientation(PlotOrientation.VERTICAL);
143
- plot.mapDatasetToRangeAxis(0,0);
144
- plot.mapDatasetToRangeAxis(1,0);
145
- plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
146
-
147
- /*--- 縦軸 ---*/
148
- NumberAxis valueAxis0 = new NumberAxis("観測値");
149
- plot.setRangeAxis(valueAxis0);
150
-
151
- plot.setRenderer(0, renderer0);
152
- plot.setDataset(0, createDataset0(dname, data));
153
-
154
- plot.setRenderer(1, renderer1);
155
- plot.setDataset(1, createDataset1(data));
119
+ private XYPlot createPlot(String dname, double[] xi) {
120
+ CreatePlot plotImpl = new QQPlot();
156
121
 
157
- return plot;
122
+ return plotImpl.createPlot(dname, xi);
158
123
  }
159
- private XYSeriesCollection createDataset0(String dname, double[][] data) {
160
- XYSeries cu = new XYSeries(dname);
161
-
162
- for (int i = 0; i < data.length; i++) {
163
- cu.add(data[i][0], data[i][1]);
124
+ public static class QQPlot implements CreatePlot {
125
+ private DescriptiveStatistics stat = null;
126
+ private NormalDistribution ndist = null;
127
+ public QQPlot() {
128
+ stat = new DescriptiveStatistics();
129
+ ndist = new NormalDistribution(0, 1);
164
130
  }
165
- XYSeriesCollection series = new XYSeriesCollection();
131
+ private double[][] createData(double[] xi) {
132
+ int n = xi.length;
133
+ Arrays.sort(xi);
134
+ Arrays.stream(xi).forEach(stat::addValue);
135
+ double sum = stat.getSum();
136
+ double[][] data = new double[n][2];
137
+ double p = 0.0;
138
+
139
+ for (int i = 0; i < n; i++) {
140
+ p += xi[i] / sum;
141
+ double x =
142
+ ndist.inverseCumulativeProbability(p * (i + 1.0) / (n + 1.0));
143
+
144
+ data[i][0] = x;
145
+ data[i][1] = xi[i];
146
+ }
147
+ return data;
148
+ }
149
+ public XYPlot createPlot(String dname, double[] xi) {
150
+ double[][] data = createData(xi);
151
+ XYItemRenderer renderer0 = new XYLineAndShapeRenderer(false, true);
152
+ XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
153
+ XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
154
+
155
+ renderer0.setDefaultToolTipGenerator(toolTipGenerator);
156
+ renderer0.setURLGenerator(null);
157
+ renderer1.setDefaultToolTipGenerator(toolTipGenerator);
158
+ renderer1.setURLGenerator(null);
159
+
160
+ XYPlot plot = new XYPlot();
161
+ plot.setOrientation(PlotOrientation.VERTICAL);
162
+ plot.mapDatasetToRangeAxis(0,0);
163
+ plot.mapDatasetToRangeAxis(1,0);
164
+ plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
165
+
166
+ /*--- 縦軸 ---*/
167
+ NumberAxis valueAxis0 = new NumberAxis("観測値");
168
+ plot.setRangeAxis(valueAxis0);
169
+
170
+ plot.setRenderer(0, renderer0);
171
+ plot.setDataset(0, createDataset0(dname, data));
172
+
173
+ plot.setRenderer(1, renderer1);
174
+ plot.setDataset(1, createDataset1(data));
175
+
176
+ return plot;
177
+ }
178
+ private XYSeriesCollection createDataset0(String dname, double[][] data) {
179
+ XYSeries cu = new XYSeries(dname);
180
+
181
+ for (int i = 0; i < data.length; i++) {
182
+ cu.add(data[i][0], data[i][1]);
183
+ }
184
+ XYSeriesCollection series = new XYSeriesCollection();
166
185
 
167
- series.addSeries(cu);
168
- return series;
169
- }
170
- private XYSeriesCollection createDataset1(double[][] data) {
171
- SimpleRegression simpleReg = new SimpleRegression(true);
172
- XYSeries cu = new XYSeries("累積");
186
+ series.addSeries(cu);
187
+ return series;
188
+ }
189
+ private XYSeriesCollection createDataset1(double[][] data) {
190
+ SimpleRegression simpleReg = new SimpleRegression(true);
191
+ XYSeries cu = new XYSeries("累積");
173
192
 
174
- simpleReg.addData(data);
175
- double a = simpleReg.getSlope();
176
- double b = simpleReg.getIntercept();
193
+ simpleReg.addData(data);
194
+ double a = simpleReg.getSlope();
195
+ double b = simpleReg.getIntercept();
177
196
 
178
- for (double x = ChartPlot.CLASS_MIN; x < ChartPlot.CLASS_MAX; x += 0.01) {
179
- double y = a * x + b;
180
-
181
- cu.add(x, y);
197
+ for (double x = ChartPlot.CLASS_MIN; x < ChartPlot.CLASS_MAX; x += 0.01) {
198
+ double y = a * x + b;
199
+
200
+ cu.add(x, y);
201
+ }
202
+ XYSeriesCollection series = new XYSeriesCollection();
203
+ series.addSeries(cu);
204
+ return series;
182
205
  }
183
- XYSeriesCollection series = new XYSeriesCollection();
184
- series.addSeries(cu);
185
- return series;
186
206
  }
187
207
  }
188
208
  // コルモゴルフ・スミルノフ検定
189
- private static class KSPlot implements ChartPlot {
190
- public JFreeChart createChart(String dname, double[] xi) {
191
- NumberAxis domainAxis = new NumberAxis("標準正規分布");
209
+ private static class KSChartPlot implements ChartPlot {
210
+ public JFreeChart createChart(String title, String dname, double[] xi) {
211
+ /*--- 横軸 ---*/
212
+ NumberAxis domainAxis = new NumberAxis("期待値");
192
213
  XYPlot plot = createPlot(dname, xi);
193
214
 
194
- /*--- 横軸 ---*/
195
215
  plot.setDomainAxis(domainAxis);
196
216
  domainAxis.setLowerMargin(0.03);
197
217
  domainAxis.setUpperMargin(0.03);
198
- domainAxis.setLowerBound(-4);
199
- domainAxis.setUpperBound(4);
218
+ domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
219
+ domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
200
220
 
201
221
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
202
- return new JFreeChart("コルモゴルフ・スミルノフ検定", plot);
222
+ return new JFreeChart(title, plot);
203
223
  }
204
224
  private XYPlot createPlot(String dname, double[] xi) {
205
- XYItemRenderer renderer0 = new XYLineAndShapeRenderer(false, true);
206
- XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
207
- XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
208
-
209
- renderer0.setDefaultToolTipGenerator(toolTipGenerator);
210
- renderer0.setURLGenerator(null);
211
- renderer1.setDefaultToolTipGenerator(toolTipGenerator);
212
- renderer1.setURLGenerator(null);
213
-
214
- XYPlot plot = new XYPlot();
215
- plot.setOrientation(PlotOrientation.VERTICAL);
216
- plot.mapDatasetToRangeAxis(0,0);
217
- plot.mapDatasetToRangeAxis(1,0);
218
- plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
219
-
220
- /*--- 縦軸 ---*/
221
- NumberAxis valueAxis0 = new NumberAxis("確率");
222
- plot.setRangeAxis(valueAxis0);
223
- valueAxis0.setLowerBound(0);
224
- valueAxis0.setUpperBound(1);
225
- valueAxis0.setTickUnit(new NumberTickUnit(0.1));
226
- valueAxis0.setNumberFormatOverride(new DecimalFormat("0.0#"));
227
-
228
- plot.setRenderer(0, renderer0);
229
- plot.setDataset(0, createDataset0(dname, xi));
230
-
231
- plot.setRenderer(1, renderer1);
232
- plot.setDataset(1, createDataset1());
225
+ CreatePlot plotImpl = new KSPlot();
233
226
 
234
- return plot;
227
+ return plotImpl.createPlot(dname, xi);
235
228
  }
236
- private XYSeriesCollection createDataset0(String dname, double[] xi) {
237
- int n = xi.length;
238
- Arrays.sort(xi);
239
- DescriptiveStatistics stat = new DescriptiveStatistics();
240
- Arrays.stream(xi).forEach(stat::addValue);
241
- double m = stat.getMean(); // 平均
242
- double sd = stat.getStandardDeviation();// 標準偏差
243
- double sum = stat.getSum();
244
- double p = 0.0;
229
+ public static class KSPlot implements CreatePlot {
230
+ private DescriptiveStatistics stat = null;
231
+ public KSPlot() {
232
+ stat = new DescriptiveStatistics();
233
+ }
234
+ private double[][] createData(double[] xi) {
235
+ int n = xi.length;
236
+ Arrays.sort(xi);
237
+ Arrays.stream(xi).forEach(stat::addValue);
238
+ double m = stat.getMean(); // 平均
239
+ double sd = stat.getStandardDeviation();// 標準偏差
240
+ double sum = stat.getSum();
241
+ double[][] data = new double[n][2];
242
+ double p = 0.0;
243
+
244
+ for (int i = 0; i < n; i++) {
245
+ p += xi[i] / sum;
246
+ data[i][0] = (xi[i] - m) / sd;
247
+ data[i][1] = p;
248
+ }
249
+ return data;
250
+ }
251
+ public XYPlot createPlot(String dname, double[] xi) {
252
+ XYItemRenderer renderer0 = new XYLineAndShapeRenderer(false, true);
253
+ XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
254
+ XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
255
+
256
+ renderer0.setDefaultToolTipGenerator(toolTipGenerator);
257
+ renderer0.setURLGenerator(null);
258
+ renderer1.setDefaultToolTipGenerator(toolTipGenerator);
259
+ renderer1.setURLGenerator(null);
260
+
261
+ XYPlot plot = new XYPlot();
262
+ plot.setOrientation(PlotOrientation.VERTICAL);
263
+ plot.mapDatasetToRangeAxis(0,0);
264
+ plot.mapDatasetToRangeAxis(1,0);
265
+ plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
266
+
267
+ /*--- 縦軸 ---*/
268
+ NumberAxis valueAxis0 = new NumberAxis("確率");
269
+ plot.setRangeAxis(valueAxis0);
270
+ valueAxis0.setLowerBound(0);
271
+ valueAxis0.setUpperBound(1);
272
+ valueAxis0.setTickUnit(new NumberTickUnit(0.1));
273
+ valueAxis0.setNumberFormatOverride(new DecimalFormat("0.0#"));
274
+
275
+ plot.setRenderer(0, renderer0);
276
+ plot.setDataset(0, createDataset0(dname, createData(xi)));
277
+
278
+ plot.setRenderer(1, renderer1);
279
+ plot.setDataset(1, createDataset1());
280
+
281
+ return plot;
282
+ }
283
+ private XYSeriesCollection createDataset0(String dname, double[][] data) {
284
+ XYSeries cu = new XYSeries(dname);
245
285
 
246
- XYSeries cu = new XYSeries(dname);
247
- for (int i = 0; i < n; i++) {
248
- double x = (xi[i] - m) / sd;
286
+ for (int i = 0; i < data.length; i++) {
287
+ cu.add(data[i][0], data[i][1]);
288
+ }
289
+ XYSeriesCollection series = new XYSeriesCollection();
249
290
 
250
- p += xi[i] / sum;
251
- cu.add(x, p);
291
+ series.addSeries(cu);
292
+ return series;
252
293
  }
253
- XYSeriesCollection series = new XYSeriesCollection();
294
+ private XYSeriesCollection createDataset1() {
295
+ NormalDistribution ndist = new NormalDistribution(0, 1);
296
+ XYSeries cu = new XYSeries("累積p");
254
297
 
255
- series.addSeries(cu);
256
- return series;
257
- }
258
- private XYSeriesCollection createDataset1() {
259
- NormalDistribution ndist = new NormalDistribution(0, 1);
260
- XYSeries cu = new XYSeries("累積p");
298
+ for (double x = ChartPlot.CLASS_MIN; x < ChartPlot.CLASS_MAX; x += 0.01) {
299
+ double y = ndist.cumulativeProbability(x);
261
300
 
262
- for (double x = -4; x < 4; x += 0.01) {
263
- double y = ndist.cumulativeProbability(x);
301
+ cu.add(x, y);
302
+ }
303
+ XYSeriesCollection series = new XYSeriesCollection();
264
304
 
265
- cu.add(x, y);
305
+ series.addSeries(cu);
306
+ return series;
266
307
  }
267
- XYSeriesCollection series = new XYSeriesCollection();
308
+ }
309
+ }
310
+ // Q-QandKSplot
311
+ private static class QQKSChartPlot implements ChartPlot {
312
+ private CreatePlot plot0 = null;
313
+ private CreatePlot plot1 = null;
314
+ public QQKSChartPlot() {
315
+ plot0 = new QQChartPlot.QQPlot();
316
+ plot1 = new KSChartPlot.KSPlot();
317
+ }
318
+ public JFreeChart createChart(String title, String dname, double[] xi) {
319
+ XYPlot plot = createPlot(dname, xi);
320
+
321
+ /*--- 横軸 ---*/
322
+ NumberAxis domainAxis = (NumberAxis)plot.getDomainAxis();
323
+ domainAxis.setLabel("期待値");
324
+ domainAxis.setLowerMargin(0.03);
325
+ domainAxis.setUpperMargin(0.03);
326
+ domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
327
+ domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
268
328
 
269
- series.addSeries(cu);
270
- return series;
329
+ ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
330
+ return new JFreeChart(title, plot);
271
331
  }
332
+ private XYPlot createPlot(String dname, double[] xi) {
333
+ CombinedDomainXYPlot plot = new CombinedDomainXYPlot();
334
+
335
+ plot.add(plot0.createPlot(dname, xi), 1);
336
+ plot.add(plot1.createPlot(dname, xi), 1);
337
+ return plot;
338
+ }
339
+
272
340
  }
341
+ // KS検定
273
342
  private static class KSTest {
274
343
  public boolean test(double[] xi) {
275
344
  double[] data = new double[xi.length];
data/lib/num4normality.rb CHANGED
@@ -39,6 +39,9 @@ module Num4NormalityLib
39
39
  def ksplot(dname, xi)
40
40
  Normality.ksplot(dname, xi.to_java(Java::double))
41
41
  end
42
+ def qqksplot(dname, xi)
43
+ Normality.qqksplot(dname, xi.to_java(Java::double))
44
+ end
42
45
  # コルモゴルフ・スミルノフ検定(1標本)
43
46
  #
44
47
  # @overload kstest(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.4
4
+ version: 0.0.6
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-20 00:00:00.000000000 Z
11
+ date: 2023-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake