num4normality 0.0.6-java → 0.0.8-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: 9803b3bcb0408424c204ebda20e538aa178a2f35573e280dd222add412987cff
4
- data.tar.gz: 1258e8ad4f82177478d2f35d689f56b4d8d58d1177f7a6f45896f05ee6c50189
3
+ metadata.gz: 6bdb8267fad8c0c825bffbdc4ce48f03b5f7044b42c7b4575acd0c3bd1d24485
4
+ data.tar.gz: '0299ec982c1d8c30ee2613f2511d13125c95fb3113ccc48e630ad9496485f92b'
5
5
  SHA512:
6
- metadata.gz: 16ad0542c142e6b74871df83ae43fa4ac62a9bd977e3bd466b9a520f849f0852460a1b71051b072c5de14b93a4b7530e2ef1821300b7b02303cfd1b307f0a43c
7
- data.tar.gz: c49ca950c085da6760c1d0b171b7daad90354c7804e6521711e7469373a47aca58bec5d7fad47fb5d6c1a882c754ff389bdcae8e957fa93467cdc9db500ffa8a
6
+ metadata.gz: f5d1bb2fb5de97f2480715cff1b0183ff22cf7f04f2d776050edf32f49377bd9dce4b6d48eacdbcde1fe1a67118dcc1e21b1232d84f582a9f65fa695528ca9f5
7
+ data.tar.gz: 15d6d2d3bbd2b18361064577273fdb66f43f906ebfa87f9671cc31e9a1a11e3203f16b3c828fe828c87aacfcb9be3663370e6249992bf222eb1265109ea6c038
data/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.8] - 2023-12-30
6
+
7
+ ### Fixed
8
+ - fix fuction of ppplot.
9
+
10
+ ## [0.0.7] - 2023-12-26
11
+
12
+ ### add
13
+ - add function of ppplot.
14
+ - add function of ppksplot.
15
+
16
+ ## [0.0.6] - 2023-12-23
17
+
18
+ ### add
19
+ - add function of qqksplot.
20
+
5
21
  ## [0.0.4] - 2023-12-21
6
22
 
7
23
  ### add
@@ -5,6 +5,7 @@ import org.jfree.data.xy.XYSeriesCollection;
5
5
  import org.jfree.data.xy.XYSeries;
6
6
 
7
7
  import org.jfree.chart.plot.CombinedDomainXYPlot;
8
+ import org.jfree.chart.plot.CombinedRangeXYPlot;
8
9
  import org.jfree.chart.plot.XYPlot;
9
10
  import org.jfree.chart.axis.NumberAxis;
10
11
  import org.jfree.chart.axis.ValueAxis;
@@ -21,6 +22,7 @@ import java.io.File;
21
22
  import java.io.IOException;
22
23
 
23
24
  import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
25
+ import org.apache.commons.math3.stat.StatUtils;
24
26
  import org.apache.commons.math3.distribution.NormalDistribution;
25
27
  import org.apache.commons.math3.distribution.ChiSquaredDistribution;
26
28
  import org.apache.commons.math3.stat.regression.SimpleRegression;
@@ -44,10 +46,23 @@ public class Normality {
44
46
  }
45
47
  public static void qqksplot(String dname, double[] xi) {
46
48
  ChartPlot plot = new QQKSChartPlot();
47
- JFreeChart chart = plot.createChart("Q-Q and コルモゴルフ・スミルノフ検定", dname, xi);
49
+ JFreeChart chart = plot.createChart("Q-Q and コルモゴルフ・スミルノフ", dname, xi);
48
50
 
49
51
  plot.writeJPEG("qqksplot.jpeg", chart, 1000, 800);
50
52
  }
53
+ public static void ppplot(String dname, double[] xi) {
54
+ ChartPlot plot = new PPChartPlot();
55
+ JFreeChart chart = plot.createChart("正規P-Pプロット", dname, xi);
56
+
57
+ plot.writeJPEG("ppplot.jpeg", chart, 800, 500);
58
+ }
59
+ public static void ppksplot(String dname, double[] xi) {
60
+ ChartPlot plot = new PPKSChartPlot();
61
+ JFreeChart chart = plot.createChart("P-P and コルモゴルフ・スミルノフ", dname, xi);
62
+
63
+ plot.writeJPEG("ppksplot.jpeg", chart, 800, 500);
64
+
65
+ }
51
66
  public static boolean kstest(double[] xi) {
52
67
  KSTest ks = new KSTest();
53
68
 
@@ -104,14 +119,6 @@ public class Normality {
104
119
  private static class QQChartPlot implements ChartPlot {
105
120
  public JFreeChart createChart(String title, String dname, double[] xi) {
106
121
  XYPlot plot = createPlot(dname, xi);
107
- /*--- 横軸 ---*/
108
- NumberAxis domainAxis = new NumberAxis("期待値");
109
-
110
- plot.setDomainAxis(domainAxis);
111
- domainAxis.setLowerMargin(0.03);
112
- domainAxis.setUpperMargin(0.03);
113
- domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
114
- domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
115
122
 
116
123
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
117
124
  return new JFreeChart(title, plot);
@@ -122,13 +129,10 @@ public class Normality {
122
129
  return plotImpl.createPlot(dname, xi);
123
130
  }
124
131
  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);
130
- }
131
132
  private double[][] createData(double[] xi) {
133
+ DescriptiveStatistics stat = new DescriptiveStatistics();
134
+ NormalDistribution ndist = new NormalDistribution(0, 1);
135
+
132
136
  int n = xi.length;
133
137
  Arrays.sort(xi);
134
138
  Arrays.stream(xi).forEach(stat::addValue);
@@ -163,6 +167,15 @@ public class Normality {
163
167
  plot.mapDatasetToRangeAxis(1,0);
164
168
  plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
165
169
 
170
+ /*--- 横軸 ---*/
171
+ NumberAxis domainAxis = new NumberAxis("期待値");
172
+
173
+ plot.setDomainAxis(domainAxis);
174
+ domainAxis.setLowerMargin(0.03);
175
+ domainAxis.setUpperMargin(0.03);
176
+ domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
177
+ domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
178
+
166
179
  /*--- 縦軸 ---*/
167
180
  NumberAxis valueAxis0 = new NumberAxis("観測値");
168
181
  plot.setRangeAxis(valueAxis0);
@@ -208,15 +221,7 @@ public class Normality {
208
221
  // コルモゴルフ・スミルノフ検定
209
222
  private static class KSChartPlot implements ChartPlot {
210
223
  public JFreeChart createChart(String title, String dname, double[] xi) {
211
- /*--- 横軸 ---*/
212
- NumberAxis domainAxis = new NumberAxis("期待値");
213
224
  XYPlot plot = createPlot(dname, xi);
214
-
215
- plot.setDomainAxis(domainAxis);
216
- domainAxis.setLowerMargin(0.03);
217
- domainAxis.setUpperMargin(0.03);
218
- domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
219
- domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
220
225
 
221
226
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
222
227
  return new JFreeChart(title, plot);
@@ -227,23 +232,20 @@ public class Normality {
227
232
  return plotImpl.createPlot(dname, xi);
228
233
  }
229
234
  public static class KSPlot implements CreatePlot {
230
- private DescriptiveStatistics stat = null;
231
- public KSPlot() {
232
- stat = new DescriptiveStatistics();
233
- }
234
235
  private double[][] createData(double[] xi) {
236
+ DescriptiveStatistics stat = new DescriptiveStatistics();
237
+
235
238
  int n = xi.length;
236
239
  Arrays.sort(xi);
237
240
  Arrays.stream(xi).forEach(stat::addValue);
238
- double m = stat.getMean(); // 平均
239
- double sd = stat.getStandardDeviation();// 標準偏差
240
241
  double sum = stat.getSum();
241
242
  double[][] data = new double[n][2];
242
243
  double p = 0.0;
244
+ double z[] = StatUtils.normalize(xi);
243
245
 
244
246
  for (int i = 0; i < n; i++) {
245
247
  p += xi[i] / sum;
246
- data[i][0] = (xi[i] - m) / sd;
248
+ data[i][0] = z[i];
247
249
  data[i][1] = p;
248
250
  }
249
251
  return data;
@@ -264,6 +266,15 @@ public class Normality {
264
266
  plot.mapDatasetToRangeAxis(1,0);
265
267
  plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
266
268
 
269
+ /*--- 横軸 ---*/
270
+ NumberAxis domainAxis = new NumberAxis("期待値");
271
+ plot.setDomainAxis(domainAxis);
272
+
273
+ domainAxis.setLowerMargin(0.03);
274
+ domainAxis.setUpperMargin(0.03);
275
+ domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
276
+ domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
277
+
267
278
  /*--- 縦軸 ---*/
268
279
  NumberAxis valueAxis0 = new NumberAxis("確率");
269
280
  plot.setRangeAxis(valueAxis0);
@@ -307,53 +318,175 @@ public class Normality {
307
318
  }
308
319
  }
309
320
  }
310
- // Q-QandKSplot
321
+ // Q-Q and KSplot
311
322
  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
- }
323
+ private CreatePlot plot0 = new QQChartPlot.QQPlot();
324
+ private CreatePlot plot1 = new KSChartPlot.KSPlot();
318
325
  public JFreeChart createChart(String title, String dname, double[] xi) {
319
326
  XYPlot plot = createPlot(dname, xi);
320
327
 
328
+ ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
329
+ return new JFreeChart(title, plot);
330
+ }
331
+ private XYPlot createPlot(String dname, double[] xi) {
321
332
  /*--- 横軸 ---*/
322
- NumberAxis domainAxis = (NumberAxis)plot.getDomainAxis();
333
+ NumberAxis domainAxis = new NumberAxis("期待値");
334
+
335
+ CombinedDomainXYPlot plot = new CombinedDomainXYPlot(domainAxis);
323
336
  domainAxis.setLabel("期待値");
324
337
  domainAxis.setLowerMargin(0.03);
325
338
  domainAxis.setUpperMargin(0.03);
326
339
  domainAxis.setLowerBound(ChartPlot.CLASS_MIN);
327
340
  domainAxis.setUpperBound(ChartPlot.CLASS_MAX);
328
341
 
342
+ plot.add(plot0.createPlot(dname, xi), 1);
343
+ plot.add(plot1.createPlot(dname, xi), 1);
344
+ return plot;
345
+ }
346
+
347
+ }
348
+ // P-Pplot
349
+ private static class PPChartPlot implements ChartPlot {
350
+ public JFreeChart createChart(String title, String dname, double[] xi) {
351
+ XYPlot plot = createPlot(dname, xi);
352
+
329
353
  ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
330
354
  return new JFreeChart(title, plot);
331
355
  }
332
356
  private XYPlot createPlot(String dname, double[] xi) {
333
- CombinedDomainXYPlot plot = new CombinedDomainXYPlot();
357
+ CreatePlot plotImpl = new PPPlot();
334
358
 
335
- plot.add(plot0.createPlot(dname, xi), 1);
336
- plot.add(plot1.createPlot(dname, xi), 1);
337
- return plot;
359
+ return plotImpl.createPlot(dname, xi);
338
360
  }
361
+ public static class PPPlot implements CreatePlot {
362
+ private double[][] createData(double[] xi) {
363
+ DescriptiveStatistics stat = new DescriptiveStatistics();
364
+ NormalDistribution ndist = new NormalDistribution(0, 1);
339
365
 
366
+ int n = xi.length;
367
+ double[][] data = new double[n][2];
368
+ Arrays.sort(xi);
369
+ Arrays.stream(xi).forEach(stat::addValue);
370
+ double sum = stat.getSum();
371
+ double p = 0.0;
372
+ double z[] = StatUtils.normalize(xi);
373
+
374
+ for (int i = 0; i < n; i++) {
375
+ p += xi[i] / sum;
376
+
377
+ data[i][0] = ndist.cumulativeProbability(z[i]);
378
+ data[i][1] = p;
379
+ }
380
+ return data;
381
+ }
382
+ public XYPlot createPlot(String dname, double[] xi) {
383
+ double[][] data = createData(xi);
384
+ XYItemRenderer renderer0 = new XYLineAndShapeRenderer(false, true);
385
+ XYItemRenderer renderer1 = new XYLineAndShapeRenderer(true, false);
386
+ XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator();
387
+
388
+ renderer0.setDefaultToolTipGenerator(toolTipGenerator);
389
+ renderer0.setURLGenerator(null);
390
+ renderer1.setDefaultToolTipGenerator(toolTipGenerator);
391
+ renderer1.setURLGenerator(null);
392
+
393
+ XYPlot plot = new XYPlot();
394
+ plot.setOrientation(PlotOrientation.VERTICAL);
395
+ plot.mapDatasetToRangeAxis(0,0);
396
+ plot.mapDatasetToRangeAxis(1,0);
397
+ plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
398
+
399
+ /*--- 横軸 ---*/
400
+ NumberAxis domainAxis = new NumberAxis("観測累積確率");
401
+
402
+ plot.setDomainAxis(0, domainAxis);
403
+ domainAxis.setLowerMargin(0.03);
404
+ domainAxis.setUpperMargin(0.03);
405
+ domainAxis.setLowerBound(0.0);
406
+ domainAxis.setUpperBound(1.0);
407
+ domainAxis.setTickUnit(new NumberTickUnit(0.1));
408
+ domainAxis.setNumberFormatOverride(new DecimalFormat("0.0#"));
409
+ /*--- 縦軸 ---*/
410
+ NumberAxis valueAxis0 = new NumberAxis("予測累積確率");
411
+ plot.setRangeAxis(valueAxis0);
412
+ valueAxis0.setLowerBound(0);
413
+ valueAxis0.setUpperBound(1);
414
+ valueAxis0.setTickUnit(new NumberTickUnit(0.1));
415
+ valueAxis0.setNumberFormatOverride(new DecimalFormat("0.0#"));
416
+
417
+ plot.setRenderer(0, renderer0);
418
+ plot.setDataset(0, createDataset0(dname, data));
419
+
420
+ plot.setRenderer(1, renderer1);
421
+ plot.setDataset(1, createDataset1(data));
422
+ return plot;
423
+ }
424
+ private XYSeriesCollection createDataset0(String dname, double[][] data) {
425
+ XYSeries cu = new XYSeries(dname);
426
+
427
+ for (int i = 0; i < data.length; i++) {
428
+ cu.add(data[i][0], data[i][1]);
429
+ }
430
+ XYSeriesCollection series = new XYSeriesCollection();
431
+
432
+ series.addSeries(cu);
433
+ return series;
434
+ }
435
+ private XYSeriesCollection createDataset1(double[][] data) {
436
+ SimpleRegression simpleReg = new SimpleRegression(true);
437
+ XYSeries cu = new XYSeries("累積");
438
+
439
+ simpleReg.addData(data);
440
+ double a = simpleReg.getSlope();
441
+ double b = simpleReg.getIntercept();
442
+
443
+ for (double x = ChartPlot.CLASS_MIN; x < ChartPlot.CLASS_MAX; x += 0.01) {
444
+ double y = a * x + b;
445
+
446
+ cu.add(x, y);
447
+ }
448
+ XYSeriesCollection series = new XYSeriesCollection();
449
+ series.addSeries(cu);
450
+ return series;
451
+ }
452
+
453
+ }
454
+ }
455
+ // P-P and KSplot
456
+ private static class PPKSChartPlot implements ChartPlot {
457
+ private CreatePlot plot0 = new PPChartPlot.PPPlot();
458
+ private CreatePlot plot1 = new KSChartPlot.KSPlot();
459
+ public JFreeChart createChart(String title, String dname, double[] xi) {
460
+ XYPlot plot = createPlot(dname, xi);
461
+
462
+ ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
463
+ return new JFreeChart(title, plot);
464
+ }
465
+ private XYPlot createPlot(String dname, double[] xi) {
466
+ /*--- 縦軸 ---*/
467
+ NumberAxis rangeAxis = new NumberAxis("予測累積確率");
468
+ CombinedRangeXYPlot plot = new CombinedRangeXYPlot(rangeAxis);
469
+ rangeAxis.setLowerMargin(0.03);
470
+ rangeAxis.setUpperMargin(0.03);
471
+ rangeAxis.setLowerBound(0.0);
472
+ rangeAxis.setUpperBound(1.0);
473
+ rangeAxis.setTickUnit(new NumberTickUnit(0.1));
474
+ rangeAxis.setNumberFormatOverride(new DecimalFormat("0.0#"));
475
+
476
+ plot.add(plot0.createPlot(dname, xi), 2);
477
+ plot.add(plot1.createPlot(dname, xi), 3);
478
+
479
+ return plot;
480
+ }
340
481
  }
341
482
  // KS検定
342
483
  private static class KSTest {
343
484
  public boolean test(double[] xi) {
344
- double[] data = new double[xi.length];
345
485
  Arrays.sort(xi);
346
- DescriptiveStatistics stat = new DescriptiveStatistics();
347
- Arrays.stream(xi).forEach(stat::addValue);
348
- double m = stat.getMean(); // 平均
349
- double sd = stat.getStandardDeviation();// 標準偏差
350
486
  NormalDistribution ndist = new NormalDistribution(0, 1);
487
+ double data[] = StatUtils.normalize(xi);
351
488
 
352
- for (int i = 0; i < xi.length; i++) {
353
- data[i] = (xi[i] - m) / sd;
354
- }
355
- boolean ret = TestUtils.kolmogorovSmirnovTest(ndist, data, 0.05);
356
- return ret;
489
+ return TestUtils.kolmogorovSmirnovTest(ndist, data, 0.05);
357
490
  }
358
491
  }
359
492
  // タコスディーノ検定(歪度)
data/lib/num4normality.rb CHANGED
@@ -33,15 +33,57 @@ 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
40
  Normality.ksplot(dname, xi.to_java(Java::double))
41
41
  end
42
+ # Q-Q and コルモゴルフ・スミルノフ検定プロット(1標本)
43
+ #
44
+ # @overload qqksplot(dname, xi)
45
+ # @param [String] dname データ名
46
+ # @param [Array] xi データ(double[])
47
+ # @return [void] qqksplot.jpegファイルを出力
48
+ # @example
49
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
50
+ # Num4NormalityLib.qqksplot("LDH", xi)
51
+ # => qqksplot.jpeg
52
+ # @note
53
+ # グラフは、jfreechartを使用
42
54
  def qqksplot(dname, xi)
43
55
  Normality.qqksplot(dname, xi.to_java(Java::double))
44
56
  end
57
+ # P-Pプロット
58
+ #
59
+ # @overload ppplot(dname, xi)
60
+ # @param [String] dname データ名
61
+ # @param [Array] xi データ(double[])
62
+ # @return [void] ppplot.jpegファイルを出力
63
+ # @example
64
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
65
+ # Num4NormalityLib.ppplot("LDH", xi)
66
+ # => ppplot.jpeg
67
+ # @note
68
+ # グラフは、jfreechartを使用
69
+ def ppplot(dname, xi)
70
+ Normality.ppplot(dname, xi.to_java(Java::double))
71
+ end
72
+ # P-P and コルモゴルフ・スミルノフ検定プロット(1標本)
73
+ #
74
+ # @overload ppksplot(dname, xi)
75
+ # @param [String] dname データ名
76
+ # @param [Array] xi データ(double[])
77
+ # @return [void] ppksplot.jpegファイルを出力
78
+ # @example
79
+ # xi = [320, 240, 402, 325, 440, 286, 362, 281, 560, 212, 198, 209, 374]
80
+ # Num4NormalityLib.ppksplot("LDH", xi)
81
+ # => ppksplot.jpeg
82
+ # @note
83
+ # グラフは、jfreechartを使用
84
+ def ppksplot(dname, xi)
85
+ Normality.ppksplot(dname, xi.to_java(Java::double))
86
+ end
45
87
  # コルモゴルフ・スミルノフ検定(1標本)
46
88
  #
47
89
  # @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.6
4
+ version: 0.0.8
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-23 00:00:00.000000000 Z
11
+ date: 2023-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake