num4regana 0.0.1-java → 0.0.2-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: 616a5ce4aa14748f31750e7b1c2e69ca9af26295e7a173a8454ff9b3bc83cbfe
4
- data.tar.gz: 74b2370bb501a69ef6546840d3ed0b61315e13ef35f584d5dbe3f2f38725cbb2
3
+ metadata.gz: ad6fa72619eb7a02bdc561494b9aae197c37b6a8558fd21106bdf15210a9af81
4
+ data.tar.gz: c7a55ac44970c164dc7379a11955f29416392ceba4605118f8bcbba459724e94
5
5
  SHA512:
6
- metadata.gz: 7133ab852f61abbcb0e93bbc1be555bcfe16dfab0751ff1026be21dc8a46d829b3d03ba94313a7a196a0f385b7dbe01a6cb16d4678337bb063662e3faac88b43
7
- data.tar.gz: a9851b753e8bf686a1a5bccc5dc392dc229822e51e47a3b1b82c8f1f76dbaf8629c04ecc825679aee250ff1225a7cf9ab6ed2f774f61e927f32fcb58ba83e7ee
6
+ metadata.gz: dd9e09e45d35384a3bfa9eb50a0132f4b3b3e970c2f97d241264a18a08986c3a46753d2292caf10c0ae1fcab8ec4f45f6456c97fe14df2ee9334168cd429a60a
7
+ data.tar.gz: 1cf8a55a5444c5e1e1a151adbb4d142bceaa9344cde28a859848b5642ce36660b6b919410907f8964105ec35df991591670ba8722658a708822e0840d790ce23
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.2] - 2024-08-08
6
+
7
+ ### fix
8
+ - add function of equal variances in OLSMultRegAnaLib
9
+
5
10
  ## [0.0.1] - 2024-06-27
6
11
 
7
12
  ### Fixed
@@ -0,0 +1,206 @@
1
+ import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
2
+ import org.apache.commons.math3.stat.regression.GLSMultipleLinearRegression;
3
+ import org.apache.commons.math3.stat.correlation.Covariance;
4
+ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
5
+ import java.util.Arrays;
6
+ import org.apache.commons.math3.distribution.ChiSquaredDistribution;
7
+
8
+ public class MultRegAna {
9
+ private final double A = 0.05;
10
+ private static MultRegAna regana = new MultRegAna();
11
+ public static MultRegAna getInstance() {
12
+ return regana;
13
+ }
14
+ public LineReg lineRegAna(double[] yi, double xij[][]) {
15
+ double[][] data = createData(yi, xij);
16
+ LineRegAna line = createLineRegAna(data);
17
+
18
+ return line.lineRegAna(yi, xij);
19
+ }
20
+ public double getR2(double[] yi, double xij[][]) {
21
+ double[][] data = createData(yi, xij);
22
+ LineRegAna line = createLineRegAna(data);
23
+
24
+ return line.getR2(yi, xij);
25
+ }
26
+ public double getAdjR2(double[] yi, double xij[][]) {
27
+ double[][] data = createData(yi, xij);
28
+ LineRegAna line = createLineRegAna(data);
29
+
30
+ return line.getAdjR2(yi, xij);
31
+ }
32
+ private double[][] createData(double[] yi, double xij[][]) {
33
+ double[][] data = new double[yi.length][1 + xij[0].length];
34
+
35
+ for (int i = 0; i < yi.length; i++) {
36
+ data[i][0] = yi[i];
37
+ System.arraycopy(xij[i], 0, data[i], 1, xij[0].length);
38
+ }
39
+ return data;
40
+ }
41
+ private LineRegAna createLineRegAna(double data[][]) {
42
+ // 等分散性の検定
43
+ if (false == bartletTest(data)) { // 等分散性
44
+ return new OLSMultRegAna();
45
+ }
46
+ else { //
47
+ return new GLSMultRegAna(data);
48
+ }
49
+ }
50
+ private boolean bartletTest(double data[][]) {
51
+ OneWayAnovaTest anova = new BartletTest();
52
+ double statistic = anova.calcTestStatistic(data);
53
+
54
+ return anova.execute_test(statistic, A);
55
+ }
56
+ /*********************************/
57
+ /* interface define */
58
+ /*********************************/
59
+ private interface LineRegAna {
60
+ // 最小2乗法
61
+ LineReg lineRegAna(double[] yi, double xij[][]);
62
+ // 決定係数取得
63
+ double getR2(double[] yi, double xij[][]);
64
+ // 自由度調整済み決定係数
65
+ double getAdjR2(double[] yi, double xij[][]);
66
+ }
67
+ private interface OneWayAnovaTest {
68
+ double calcTestStatistic(double[][] xi);
69
+ boolean execute_test(double statistic, double a);
70
+ }
71
+ /*********************************/
72
+ /* class define */
73
+ /*********************************/
74
+ public class LineReg {
75
+ private double a = 0.0;
76
+ private double[] b = null;
77
+ public LineReg(double[] b) {
78
+ this.a = b[0];
79
+ this.b = new double[b.length - 1];
80
+ for (int i = 0; i < this.b.length; i++) {
81
+ this.b[i] = b[i + 1];
82
+ }
83
+ }
84
+ public double getIntercept() {
85
+ return a;
86
+ }
87
+ public double[] getSlope() {
88
+ return b;
89
+ }
90
+ }
91
+ // 等分散性検定
92
+ private class BartletTest implements OneWayAnovaTest {
93
+ private int n = 0;
94
+ public double calcTestStatistic(double[][] xi) {
95
+ n = xi.length;
96
+ double ln2L = logL(xi);
97
+
98
+ return calcB(ln2L, xi);
99
+ }
100
+ private double logL(double[][] xi) {
101
+ double[] si = new double[n];
102
+ DescriptiveStatistics stat = new DescriptiveStatistics();
103
+ double nisi2 = 0.0; // (Ni - 1)*si^2の合計
104
+ double nilogsi2 = 0.0; // (Ni - 1)*log(si^2)の合計
105
+ int sumN = 0;
106
+
107
+ for(int i = 0; i < n; i++) {
108
+ Arrays.stream(xi[i]).forEach(stat::addValue);
109
+ sumN += stat.getN();
110
+ si[i] = stat.getVariance();
111
+ nisi2 += (stat.getN() - 1) * si[i];
112
+ nilogsi2 += (stat.getN() - 1) * Math.log(si[i]);
113
+ stat.clear();
114
+ }
115
+ double sumNin = sumN - n;
116
+ return sumNin * (Math.log(nisi2 / sumNin) - nilogsi2 / sumNin);
117
+ }
118
+ private double calcB(double ln2L, double[][] xi) {
119
+ double invSumN = 0.0;
120
+ int sumN = 0;
121
+ DescriptiveStatistics stat = new DescriptiveStatistics();
122
+
123
+ for(int i = 0; i < n; i++) {
124
+ Arrays.stream(xi[i]).forEach(stat::addValue);
125
+ invSumN += 1.0 / (stat.getN() - 1.0);
126
+ sumN += stat.getN();
127
+ stat.clear();
128
+ }
129
+ double deno = 1 + 1.0 / (3 * (n - 1))
130
+ * (invSumN - 1.0 / (sumN - n));
131
+ return ln2L / deno;
132
+ }
133
+ public boolean execute_test(double statistic, double a) {
134
+ ChiSquaredDistribution chi2Dist = new ChiSquaredDistribution(n - 1);
135
+ double r_val = chi2Dist.inverseCumulativeProbability(1.0 - a);
136
+
137
+ return (r_val < statistic) ? true : false;
138
+ }
139
+ }
140
+
141
+ // 最小2乗法
142
+ private class OLSMultRegAna implements LineRegAna {
143
+ private OLSMultipleLinearRegression regression = null;
144
+ public OLSMultRegAna() {
145
+ regression = new OLSMultipleLinearRegression();
146
+ }
147
+ public LineReg lineRegAna(double[] yi, double xij[][]) {
148
+ regression.newSampleData(yi, xij);
149
+
150
+ double[] beta = regression.estimateRegressionParameters();
151
+
152
+ return new LineReg(beta);
153
+ }
154
+ // 決定係数取得
155
+ public double getR2(double[] yi, double xij[][]) {
156
+ regression.newSampleData(yi, xij);
157
+ return regression.calculateRSquared();
158
+ }
159
+ // 自由度調整済み決定係数
160
+ public double getAdjR2(double[] yi, double xij[][]) {
161
+ regression.newSampleData(yi, xij);
162
+ return regression.calculateAdjustedRSquared();
163
+ }
164
+
165
+ }
166
+ // 一般化最小2乗法
167
+ private class GLSMultRegAna implements LineRegAna {
168
+ private GLSMultipleLinearRegression regression = null;
169
+ private double[][] data = null;
170
+ public GLSMultRegAna(double data[][]) {
171
+ regression = new GLSMultipleLinearRegression();
172
+ this.data = data;
173
+ }
174
+ public LineReg lineRegAna(double[] yi, double xij[][]) {
175
+ double[][] omega = calcCovatrianceMatrix();
176
+ regression.newSampleData(yi, xij, omega);
177
+
178
+ double[] beta = regression.estimateRegressionParameters();
179
+ return new LineReg(beta);
180
+ }
181
+ // 決定係数取得
182
+ public double getR2(double[] yi, double xij[][]) {
183
+ return 0.0;
184
+ }
185
+ // 自由度調整済み決定係数
186
+ public double getAdjR2(double[] yi, double xij[][]) {
187
+ return 0.0;
188
+ }
189
+ private double[][] calcCovatrianceMatrix() {
190
+ Covariance corel = new Covariance();
191
+ double[][] omega = new double[data.length][data.length];
192
+
193
+ for(int i = 0; i < data.length; i++) {
194
+ for(int j = 0; j < data.length; j++) {
195
+ double[] xArray = data[i];
196
+ double[] yArray = data[j];
197
+
198
+ omega[i][j] = corel.covariance(xArray, yArray);
199
+ }
200
+ }
201
+ return omega;
202
+ }
203
+ }
204
+
205
+ }
206
+
data/lib/num4regana.rb CHANGED
@@ -3,7 +3,7 @@ require 'num4regana.jar'
3
3
  require 'commons-math3-3.6.1.jar'
4
4
 
5
5
  java_import 'SmplRegAna'
6
- java_import 'OLSMultRegAna'
6
+ java_import 'MultRegAna'
7
7
  # 回帰分析
8
8
  # (Apache commoms math3使用)
9
9
  module Num4RegAnaLib
@@ -67,10 +67,10 @@ module Num4RegAnaLib
67
67
  return @regana.getR(yi.to_java(Java::double), xi.to_java(Java::double))
68
68
  end
69
69
  end
70
- # 重回帰分析(最小2乗法)
70
+ # 重回帰分析(最小2乗法:等分散性checkあり)
71
71
  class OLSMultRegAnaLib
72
72
  def initialize
73
- @regana = OLSMultRegAna.getInstance()
73
+ @multana = MultRegAna.getInstance()
74
74
  end
75
75
  # 重回帰分析
76
76
  #
@@ -100,10 +100,11 @@ module Num4RegAnaLib
100
100
  # "slope": [3.47, 0.53], # 回帰係数
101
101
  # }
102
102
  def line_reg_ana(yi, xij)
103
- ret = @regana.lineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
103
+ multRet = @multana.lineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
104
+
104
105
  retRb = {
105
- "intercept": ret.getIntercept(), # 定数項
106
- "slope": ret.getSlope().to_a, # 回帰係数
106
+ "intercept": multRet.getIntercept(), # 定数項
107
+ "slope": multRet.getSlope().to_a, # 回帰係数
107
108
  }
108
109
  return retRb
109
110
  end
@@ -131,7 +132,7 @@ module Num4RegAnaLib
131
132
  # regana.getr2(yi, xi)
132
133
  # => 0.858
133
134
  def getr2(yi, xij)
134
- return @regana.getR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
135
+ return @multana.getR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
135
136
  end
136
137
  # 自由度調整済み決定係数
137
138
  #
@@ -157,7 +158,7 @@ module Num4RegAnaLib
157
158
  # regana.getadjr2(yi, xij)
158
159
  # => 0.8176
159
160
  def getadjr2(yi, xij)
160
- return @regana.getAdjR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
161
+ return @multana.getAdjR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
161
162
  end
162
163
 
163
164
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4regana
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: java
6
6
  authors:
7
7
  - siranovel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-27 00:00:00.000000000 Z
11
+ date: 2024-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -61,7 +61,7 @@ files:
61
61
  - Gemfile
62
62
  - LICENSE
63
63
  - Rakefile
64
- - ext/num4regana/OLSMultRegAna.java
64
+ - ext/num4regana/MultRegAna.java
65
65
  - ext/num4regana/SmplRegAna.java
66
66
  - lib/commons-math3-3.6.1.jar
67
67
  - lib/num4regana.rb
@@ -1,75 +0,0 @@
1
- import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
2
-
3
- public class OLSMultRegAna {
4
- private static OLSMultRegAna regana = new OLSMultRegAna();
5
- public static OLSMultRegAna getInstance() {
6
- return regana;
7
- }
8
- public LineReg lineRegAna(double[] yi, double xij[][]) {
9
- LineRegAna line = new LineRegAna();
10
-
11
- return line.lineRegAna(yi, xij);
12
- }
13
- public double getR2(double[] yi, double xij[][]) {
14
- LineRegAna line = new LineRegAna();
15
-
16
- return line.getR2(yi, xij);
17
- }
18
- public double getAdjR2(double[] yi, double xij[][]) {
19
- LineRegAna line = new LineRegAna();
20
-
21
- return line.getAdjR2(yi, xij);
22
- }
23
- /*********************************/
24
- /* interface define */
25
- /*********************************/
26
- /*********************************/
27
- /* class define */
28
- /*********************************/
29
- public class LineReg {
30
- private double a = 0.0;
31
- private double[] b = null;
32
- public LineReg(double[] b) {
33
- this.a = b[0];
34
- this.b = new double[b.length - 1];
35
- for (int i = 0; i < this.b.length; i++) {
36
- this.b[i] = b[i + 1];
37
- }
38
- }
39
- public double getIntercept() {
40
- return a;
41
- }
42
- public double[] getSlope() {
43
- return b;
44
- }
45
- }
46
- private class LineRegAna {
47
- // 最小2乗法
48
- public LineReg lineRegAna(double[] yi, double xij[][]) {
49
- OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
50
-
51
- regression.newSampleData(yi, xij);
52
-
53
- double[] beta = regression.estimateRegressionParameters();
54
-
55
- LineReg ret = new LineReg(beta);
56
- return ret;
57
- }
58
- // 決定係数取得
59
- public double getR2(double[] yi, double xij[][]) {
60
- OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
61
-
62
- regression.newSampleData(yi, xij);
63
- return regression.calculateRSquared();
64
- }
65
- // 自由度調整済み決定係数
66
- public double getAdjR2(double[] yi, double xij[][]) {
67
- OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
68
-
69
- regression.newSampleData(yi, xij);
70
- return regression.calculateAdjustedRSquared();
71
- }
72
-
73
- }
74
- }
75
-