num4anova 0.0.10-java → 0.0.12-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: 3dc8b0870b6c837111c8c59d391502dcea83fa1bb055834ccbe89823d24ba64a
4
- data.tar.gz: d079fbc907bb2b0f72dc79affb51441efbbad8973842703194dac216be783e0b
3
+ metadata.gz: 87febfd8031fd924430223c93fad6b0606aa212ab109e1b758db2c1d23b6dace
4
+ data.tar.gz: 48b610b93d2c5341f05c25ef230acea09b02fa75c924129f5dafba56fcb6051f
5
5
  SHA512:
6
- metadata.gz: 2ab2456e85fa85bb45fe021e5e6606353f7d957874ac6f5fe8f335ab78a4449c50f866ce5b8af14c5cfb5ece7b61bffc700c499d584e0d16df14c1d4e2370ab2
7
- data.tar.gz: 0e43ed4956da47b8964f4f129ef27b46333c18f4c1cd84af0826f7f533ca7df185e2e9af83ce4cea202ad15d2115e4ed80c4335adfb7cf69654f39fbfd430af5
6
+ metadata.gz: 979fe122d0cf17086e80a45ff6c44b67153822a8d89d83bceb84a25d3d68190f087c3ed8bf4b192e9afd48a38b3ec0d1456fa0821c84256113154e04d5a6e461
7
+ data.tar.gz: 0bcad9f7a234a96759697440f540f0caddf70c1a25cf46409dead11478faa0017cfe9e98d2a23feaad2648bea641d5bca80cb73adab5001ec46d93ef8899ce70
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.12] - 2024-03-27
6
+
7
+ ### add
8
+ - add fuction of interval_estim.
9
+
10
+ ## [0.0.11] - 2024-03-21
11
+
12
+ ### add
13
+ - add fuction of difference_test.
14
+
5
15
  ## [0.0.10] - 2024-03-05
6
16
 
7
17
  ### add
@@ -1,4 +1,5 @@
1
1
  import org.apache.commons.math3.distribution.FDistribution;
2
+ import org.apache.commons.math3.distribution.TDistribution;
2
3
 
3
4
  public class Ancova {
4
5
  private static Ancova ancova = new Ancova();
@@ -17,6 +18,17 @@ public class Ancova {
17
18
  double statistic = hypoth.calcTestStatistic(xi);
18
19
  return hypoth.executeTest(statistic, a);
19
20
  }
21
+ public boolean differenceTest(double[][][] xi, double a) {
22
+ HypothesisTest hypoth = new DifferenceTest();
23
+
24
+ double statistic = hypoth.calcTestStatistic(xi);
25
+ return hypoth.executeTest(statistic, a);
26
+ }
27
+ public Interval intervalEstim(double[][][] xi, double a) {
28
+ Estim estim = new IntervalEstim();
29
+
30
+ return estim.calcInterval(xi, a);
31
+ }
20
32
  /*********************************/
21
33
  /* interface define */
22
34
  /*********************************/
@@ -24,9 +36,22 @@ public class Ancova {
24
36
  double calcTestStatistic(double[][][] xi);
25
37
  boolean executeTest(double statistic, double a);
26
38
  }
39
+ private interface Estim {
40
+ Interval calcInterval(double[][][] xi, double a);
41
+ }
27
42
  /*********************************/
28
43
  /* class define */
29
44
  /*********************************/
45
+ public class Interval {
46
+ private double min;
47
+ private double max;
48
+ public Interval(double min, double max) {
49
+ this.min = min;
50
+ this.max = max;
51
+ }
52
+ public double getMin() { return this.min; }
53
+ public double getMax() { return this.max; }
54
+ }
30
55
  private class RegressionLine {
31
56
  protected int calcSumn(double[][][] xi) {
32
57
  int sum = 0;
@@ -36,34 +61,54 @@ public class Ancova {
36
61
  }
37
62
  return sum;
38
63
  }
39
- protected double calcSex(double[][][] xi, int sumn) {
64
+ // 全変動
65
+ private double calcSty(double[][][] xi, int sumn) {
66
+ double sumy2 = calcSumy2(xi);
67
+ double sumy = calcSumy(xi);
68
+
69
+ return sumy2 - sumy*sumy / sumn;
70
+ }
71
+ private double calcStx(double[][][] xi, int sumn) {
40
72
  double sumx2 = calcSumx2(xi);
41
73
  double sumx = calcSumx(xi);
42
74
 
43
- double sumtx = sumx2 - sumx*sumx / sumn;
44
- double sumax = calcSumax(xi) - sumx*sumx / sumn;
75
+ return sumx2 - sumx*sumx / sumn;
76
+ }
77
+ private double calcStyx(double[][][] xi, int sumn) {
78
+ double sumx = calcSumx(xi);
79
+ double sumy = calcSumy(xi);
80
+ double sumyx = calcSumyx(xi);
45
81
 
46
- return sumtx - sumax;
82
+ return sumyx - sumy*sumx / sumn;
47
83
  }
48
- protected double calcSey(double[][][] xi, int sumn) {
49
- double sumy2 = calcSumy2(xi);
84
+ // 水準間変動
85
+ private double calcSay(double[][][] xi, int sumn) {
50
86
  double sumy = calcSumy(xi);
51
87
 
52
- double sumay = calcSumay(xi) - sumy*sumy / sumn;
53
- double sumty = sumy2 - sumy*sumy / sumn;
88
+ return calcSumay(xi) - sumy*sumy / sumn;
89
+ }
90
+ private double calcSax(double[][][] xi, int sumn) {
91
+ double sumx = calcSumx(xi);
54
92
 
55
- return sumty - sumay;
93
+ return calcSumax(xi) - sumx*sumx / sumn;
56
94
  }
57
- protected double calcSeyx(double[][][] xi, int sumn) {
95
+ private double calcSayx(double[][][] xi, int sumn) {
58
96
  double sumx = calcSumx(xi);
59
97
  double sumy = calcSumy(xi);
60
- double sumyx = calcSumyx(xi);
61
98
 
62
- double sumayx = calcSumayx(xi) - sumy*sumx / sumn;
63
- double sumtyx = sumyx - sumy*sumx / sumn;
64
-
65
- return sumtyx - sumayx;
99
+ return calcSumayx(xi) - sumy*sumx / sumn;
100
+ }
101
+ // 水準内変動
102
+ protected double calcSex(double[][][] xi, int sumn) {
103
+ return calcStx(xi, sumn) - calcSax(xi, sumn);
104
+ }
105
+ protected double calcSey(double[][][] xi, int sumn) {
106
+ return calcSty(xi, sumn) - calcSay(xi, sumn);
107
+ }
108
+ protected double calcSeyx(double[][][] xi, int sumn) {
109
+ return calcStyx(xi, sumn) - calcSayx(xi, sumn);
66
110
  }
111
+
67
112
  // 平行性の検定
68
113
  protected double calcbx(double[][][] xi) {
69
114
  double sum = 0.0;
@@ -89,8 +134,20 @@ public class Ancova {
89
134
  }
90
135
  return sum;
91
136
  }
137
+ // 差の検定
138
+ protected double calcSa(double[][][] xi, int sumn) {
139
+ double sumty = calcSty(xi, sumn);
140
+ double sumtyx = calcStyx(xi, sumn);
141
+ double sumtx = calcStx(xi, sumn);
142
+ double sumey = calcSey(xi, sumn);
143
+ double sumeyx = calcSeyx(xi, sumn);
144
+ double sumex = calcSex(xi, sumn);
92
145
 
146
+ return (sumty - sumtyx * sumtyx / sumtx)
147
+ - (sumey - sumeyx * sumeyx / sumex);
148
+ }
93
149
 
150
+ // ETC
94
151
  private double calcSumay(double[][][] xi) {
95
152
  double sum = 0.0;
96
153
 
@@ -157,7 +214,6 @@ public class Ancova {
157
214
  }
158
215
  return sum;
159
216
  }
160
- // 水準間変動
161
217
  private double calcSumax(double[][][] xi) {
162
218
  double sum = 0.0;
163
219
 
@@ -180,6 +236,7 @@ public class Ancova {
180
236
  }
181
237
  return sum;
182
238
  }
239
+
183
240
  }
184
241
  // 回帰直線モデルの平行性の検定
185
242
  private class Parallettest extends RegressionLine implements HypothesisTest {
@@ -232,6 +289,7 @@ public class Ancova {
232
289
 
233
290
  double vr = calcVr(xi, sumn);
234
291
  double ve = calcVe(xi, sumn);
292
+
235
293
  return vr / ve;
236
294
  }
237
295
  public boolean executeTest(double statistic, double a) {
@@ -254,5 +312,116 @@ public class Ancova {
254
312
  return (sumey * sumex - sumeyx * sumeyx) / (m * sumex);
255
313
  }
256
314
  }
315
+ // 水準間の差の検定
316
+ private class DifferenceTest extends RegressionLine implements HypothesisTest {
317
+ private int n = 0;
318
+ private int m = 0;
319
+ public double calcTestStatistic(double[][][] xi) {
320
+ int sumn = calcSumn(xi);
321
+ n = xi.length - 1;
322
+ m = sumn - xi.length - 1;
323
+
324
+ double va = calcSa(xi, sumn) / n;
325
+ double ve = calcVe(xi, sumn);
326
+
327
+ return va / ve;
328
+ }
329
+ public boolean executeTest(double statistic, double a) {
330
+ FDistribution fDist = new FDistribution(n, m);
331
+ double f = fDist.inverseCumulativeProbability(1.0 - a);
332
+
333
+ return (statistic >= f) ? true : false;
334
+ }
335
+ private double calcVe(double[][][] xi, int sumn) {
336
+ double sumey = calcSey(xi, sumn);
337
+ double sumex = calcSex(xi, sumn);
338
+ double sumeyx = calcSeyx(xi, sumn);
339
+
340
+ return (sumey * sumex - sumeyx * sumeyx) / (m * sumex);
341
+ }
342
+ }
343
+ //
344
+ private class IntervalEstim extends RegressionLine
345
+ implements Estim {
346
+ private int n = 0;
347
+ private int[] ni = null;
348
+ private double sumex = 0.0;
349
+ public Interval calcInterval(double[][][] xi, double a) {
350
+ ni = calcNi(xi);
351
+ int sumn = calcSumn(xi);
352
+ n = sumn - xi.length - 1;
353
+ sumex = calcSex(xi, sumn);
354
+ double ve = calcVe(xi, sumn);
355
+ double b = calcB(xi, sumn);
356
+
357
+ double[] meanyi = calcMeanyi(xi);
358
+ double[] meanxi = calcMeanxi(xi);
359
+ double meanx = calcMeanx(xi);
360
+
361
+ TDistribution tDist = new TDistribution(n);
362
+ double t = tDist.inverseCumulativeProbability(1.0 - a / 2.0);
363
+ double wk = (meanxi[0] - meanx);
364
+ double wk2 = t * Math.sqrt((1/ni[0] + wk * wk / sumex) * ve);
365
+ double min = meanyi[0] - b * wk - wk2;
366
+ double max = meanyi[0] - b * wk + wk2;
367
+
368
+ return new Interval(min, max);
369
+ }
370
+ private int[] calcNi(double[][][] xi) {
371
+ int[] ni = new int[xi.length];
372
+
373
+ for(int i = 0; i < xi.length; i++) {
374
+ ni[i] = xi[i].length;
375
+ }
376
+ return ni;
377
+ }
378
+ private double calcVe(double[][][] xi, int sumn) {
379
+ double sumey = calcSey(xi, sumn);
380
+ double sumeyx = calcSeyx(xi, sumn);
381
+
382
+ return (sumey * sumex - sumeyx * sumeyx) / (n * sumex);
383
+ }
384
+ private double calcB(double[][][] xi, int sumn) {
385
+ double sumeyx = calcSeyx(xi, sumn);
386
+ double sex = calcSex(xi, sumn);
387
+
388
+ return sumeyx / sex;
389
+ }
390
+ private double[] calcMeanyi(double[][][] xi) {
391
+ double[] meanyi = new double[xi.length];
392
+
393
+ for(int i = 0; i < xi.length; i++) {
394
+ double sum = 0.0;
395
+ for(int j = 0; j < xi[i].length; j++) {
396
+ sum += xi[i][j][0];
397
+ }
398
+ meanyi[i] = sum / xi[i].length;
399
+ }
400
+ return meanyi;
401
+ }
402
+ private double[] calcMeanxi(double[][][] xi) {
403
+ double[] meanxi = new double[xi.length];
404
+
405
+ for(int i = 0; i < xi.length; i++) {
406
+ double sum = 0.0;
407
+ for(int j = 0; j < xi[i].length; j++) {
408
+ sum += xi[i][j][1];
409
+ }
410
+ meanxi[i] = sum / xi[i].length;
411
+ }
412
+ return meanxi;
413
+ }
414
+ private double calcMeanx(double[][][] xi) {
415
+ double sum = 0.0;
416
+ double n = 0;
417
+ for(int i = 0; i < xi.length; i++) {
418
+ for(int j = 0; j < xi[i].length; j++) {
419
+ sum += xi[i][j][1];
420
+ n++;
421
+ }
422
+ }
423
+ return sum / n;
424
+ }
425
+ }
257
426
  }
258
427
 
data/lib/num4anova.rb CHANGED
@@ -256,7 +256,7 @@ module Num4AnovaLib
256
256
  end
257
257
  # 回帰直線の有意性検定
258
258
  #
259
- # @overload significance_testt(xi, a)
259
+ # @overload significance_test(xi, a)
260
260
  # @param [array] xi データ(double[][][])
261
261
  # @param [double] a 有意水準
262
262
  # @return [boolean] 検定結果(boolean true:棄却域内 false:棄却域外)
@@ -284,6 +284,72 @@ module Num4AnovaLib
284
284
  def significance_test(xi, a)
285
285
  @ancova.significanceTest(xi.to_java(Java::double[][]), a)
286
286
  end
287
+ # 水準間の差の検定
288
+ #
289
+ # @overload difference_test(xi, a)
290
+ # @param [array] xi データ(double[][][])
291
+ # @param [double] a 有意水準
292
+ # @return [boolean] 検定結果(boolean true:棄却域内 false:棄却域外)
293
+ # @example
294
+ # xi = [
295
+ # [
296
+ # [3,35], [5,38], [3,39],
297
+ # ],
298
+ # [
299
+ # [3,36], [3,39], [8,54],
300
+ # ],
301
+ # [
302
+ # [2,40], [2,45], [2,39],
303
+ # ],
304
+ # [
305
+ # [3,47], [4,52], [2,48],
306
+ # ],
307
+ # [
308
+ # [1,64], [2,80], [0,70],
309
+ # ],
310
+ # ]
311
+ # ancova = Num4AnovaLib::Num4AncovaLib.new
312
+ # ancova.difference_test(xi, 0.05)
313
+ # => true
314
+ def difference_test(xi, a)
315
+ @ancova.differenceTest(xi.to_java(Java::double[][]), a)
316
+ end
317
+ # 区間推定
318
+ #
319
+ # @overload interval_estim(xi, a)
320
+ # @param [array] xi データ(double[][][])
321
+ # @param [double] a 有意水準
322
+ # @return [Hash] (min:下限信頼区間 max:上限信頼区間)
323
+ # @example
324
+ # xi = [
325
+ # [
326
+ # [3,35], [5,38], [3,39],
327
+ # ],
328
+ # [
329
+ # [3,36], [3,39], [8,54],
330
+ # ],
331
+ # [
332
+ # [2,40], [2,45], [2,39],
333
+ # ],
334
+ # [
335
+ # [3,47], [4,52], [2,48],
336
+ # ],
337
+ # [
338
+ # [1,64], [2,80], [0,70],
339
+ # ],
340
+ # ]
341
+ # ancova = Num4AnovaLib::Num4AncovaLib.new
342
+ # ancova.interval_estim(xi, 0.05)
343
+ # =>
344
+ # {:min=>4.466605469341916, :max=>7.1909253948556096}
345
+ def interval_estim(xi, a)
346
+ retJava = @ancova.intervalEstim(xi.to_java(Java::double[][]), a)
347
+ retHash = {
348
+ "min": retJava.getMin(),
349
+ "max": retJava.getMax()
350
+ }
351
+ return retHash
352
+ end
287
353
  end
288
354
  end
289
355
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4anova
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: java
6
6
  authors:
7
7
  - siranovel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-05 00:00:00.000000000 Z
11
+ date: 2024-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake