num4regana 0.0.3-java → 0.0.5-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/ext/num4regana/AbstractGLM.java +63 -0
- data/ext/num4regana/AbstractGLMM.java +127 -0
- data/ext/num4regana/LogitBayesRegAna.java +85 -0
- data/ext/num4regana/LogitRegAna.java +36 -50
- data/ext/num4regana/PoissonBayesRegAna.java +74 -0
- data/ext/num4regana/PoissonHierBayesRegAna.java +65 -0
- data/ext/num4regana/PoissonRegAna.java +36 -50
- data/ext/num4regana/ProBitRegAna.java +71 -0
- data/lib/num4glmmregana.rb +180 -0
- data/lib/num4glmregana.rb +210 -23
- data/lib/num4hbmregana.rb +44 -0
- data/lib/num4regana.rb +5 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a35c59c0d504b97992d42739340fb1370e5fd80fc547987bb4d0a138f628b284
|
4
|
+
data.tar.gz: fec825eed34dd9c230fed6065accc753e3539bd5af5bb9b8a697c1a3752c06c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10a7cbe03f96c05fc5995b696e6946fbc222d6b5a6b0f8b487cd5c50dfccca9d931d9312d5ab3589473b2589f6887885a142e6fcb467aa526499fb6812248368
|
7
|
+
data.tar.gz: 97009fb18fcb77dd771b6290eea892092710bf49d647e451cb720d94f2ead2a07826657b139e9842fc14d8c6b3aa3ac3fd938819bea090faa403bfe43384fb8e
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
import java.util.Arrays;
|
2
|
+
|
3
|
+
abstract class AbstractGLM {
|
4
|
+
private final double eta = 0.005;
|
5
|
+
abstract double regression(double[] b, double[] xi);
|
6
|
+
abstract double linkFunc(double q);
|
7
|
+
// 勾配降下法
|
8
|
+
protected double[] grand_metod(double[] yi, double[] b, double[][] xij) {
|
9
|
+
// 交差エントロピー計算
|
10
|
+
double[] ei = calcE(yi, b, xij);
|
11
|
+
|
12
|
+
// パラメータ更新
|
13
|
+
for(int i = 0; i < ei.length; i++) {
|
14
|
+
b[i] -= eta * ei[i];
|
15
|
+
}
|
16
|
+
return b;
|
17
|
+
}
|
18
|
+
// AIC
|
19
|
+
protected double calcAIC(double[] b, double[][] xij) {
|
20
|
+
// 尤度計算
|
21
|
+
double maxL = calcLogL(b,xij);
|
22
|
+
int k = 1 + xij[0].length;
|
23
|
+
|
24
|
+
return -2 * (maxL - k);
|
25
|
+
}
|
26
|
+
// 交差エントロピー計算
|
27
|
+
private double[] calcE(double[] yi, double[] b, double[][] xij) {
|
28
|
+
double[] xi = new double[b.length];
|
29
|
+
double[] ei = new double[b.length];
|
30
|
+
|
31
|
+
Arrays.fill(ei, 0.0);
|
32
|
+
for(int i = 0; i < yi.length; i++) {
|
33
|
+
xi[0] = 1.0;
|
34
|
+
System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
|
35
|
+
|
36
|
+
double q = regression(b, xi);
|
37
|
+
double p = linkFunc(q);
|
38
|
+
|
39
|
+
for(int j = 0; j < xi.length; j++) {
|
40
|
+
ei[j] += (p - yi[i]) * xi[j];
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
return ei;
|
45
|
+
}
|
46
|
+
// 対数尤度計算(パラメータ)
|
47
|
+
private double calcLogL(double[] b, double[][] xij) {
|
48
|
+
double l = 0.0;
|
49
|
+
double[] xi = new double[b.length];
|
50
|
+
|
51
|
+
for(int i = 0; i < xij.length; i++) {
|
52
|
+
xi[0] = 1.0;
|
53
|
+
System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
|
54
|
+
|
55
|
+
double q = regression(b, xi);
|
56
|
+
double p = linkFunc(q);
|
57
|
+
|
58
|
+
l += Math.log(p);
|
59
|
+
}
|
60
|
+
return l;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
@@ -0,0 +1,127 @@
|
|
1
|
+
import java.util.Arrays;
|
2
|
+
import org.apache.commons.math3.distribution.BetaDistribution;
|
3
|
+
import org.apache.commons.math3.distribution.NormalDistribution;
|
4
|
+
|
5
|
+
abstract class AbstractGLMM {
|
6
|
+
private NormalDistribution nDist = new NormalDistribution(0,1);
|
7
|
+
abstract double regression(double[] b, double[] xi, double r);
|
8
|
+
abstract double linkFunc(double q);
|
9
|
+
// mcmc法
|
10
|
+
// (メトロポリス法,ギブスサンプリング)
|
11
|
+
protected double[] mcmcGS(double[] yi, double[] b, double[][] xij) {
|
12
|
+
BetaDistribution beDist = new BetaDistribution(50, 50);
|
13
|
+
BetaDistribution beDist2 = new BetaDistribution(1, 1); // 確率用
|
14
|
+
double[] newB = new double[b.length];
|
15
|
+
double oldL = 0.0;
|
16
|
+
double newL = 0.0;
|
17
|
+
|
18
|
+
for(int i = 0; i < b.length; i++) {
|
19
|
+
newB = Arrays.copyOf(b, b.length);
|
20
|
+
oldL = calcLx(b,xij);
|
21
|
+
newB[i] = beDist.sample();
|
22
|
+
newL = calcLx(newB,xij);
|
23
|
+
|
24
|
+
double r = newL / oldL;
|
25
|
+
if (r > 1.0) {
|
26
|
+
b[i] = newB[i];
|
27
|
+
}
|
28
|
+
else {
|
29
|
+
double r2 = beDist2.sample();
|
30
|
+
|
31
|
+
if (r2 < (1.0 - r)) {
|
32
|
+
b[i] = newB[i];
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
return b;
|
37
|
+
}
|
38
|
+
// BIC
|
39
|
+
protected double calcBIC(double[] b, double[][] xij) {
|
40
|
+
// 尤度計算
|
41
|
+
double maxL = calcLogLx(b,xij);
|
42
|
+
int k = 1 + xij[0].length;
|
43
|
+
int n = xij.length;
|
44
|
+
|
45
|
+
return -2 * maxL + k * Math.log(n);
|
46
|
+
}
|
47
|
+
// EMアルゴリズム
|
48
|
+
protected double[] mcmcEM(double[] yi, double[] b, double[][] xij) {
|
49
|
+
double[] newB = new double[b.length];
|
50
|
+
|
51
|
+
double[] bE = calcEStep(yi, b);
|
52
|
+
double[] bM = calcMStep(yi, bE, xij);
|
53
|
+
|
54
|
+
for(int i = 0; i < newB.length; i++) {
|
55
|
+
newB[i] = b[i] + bM[i];
|
56
|
+
}
|
57
|
+
return newB;
|
58
|
+
}
|
59
|
+
/* ------------------------------------------------------------------ */
|
60
|
+
// 尤度計算(パラメータ)
|
61
|
+
private double calcLx(double[] b, double[][] xij) {
|
62
|
+
double l = 1.0;
|
63
|
+
double[] xi = new double[b.length];
|
64
|
+
|
65
|
+
for(int i = 0; i < xij.length; i++) {
|
66
|
+
xi[0] = 1.0;
|
67
|
+
System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
|
68
|
+
double q = regression(b, xi, nDist.sample());
|
69
|
+
double p = linkFunc(q);
|
70
|
+
|
71
|
+
l *= p;
|
72
|
+
}
|
73
|
+
return l;
|
74
|
+
}
|
75
|
+
// 対数尤度計算(パラメータ)
|
76
|
+
private double calcLogLx(double[] b, double[][] xij) {
|
77
|
+
double l = 0.0;
|
78
|
+
double[] xi = new double[b.length];
|
79
|
+
|
80
|
+
for(int i = 0; i < xij.length; i++) {
|
81
|
+
xi[0] = 1.0;
|
82
|
+
System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
|
83
|
+
double q = regression(b, xi, nDist.sample());
|
84
|
+
double p = linkFunc(q);
|
85
|
+
|
86
|
+
l += Math.log(p);
|
87
|
+
}
|
88
|
+
return l;
|
89
|
+
}
|
90
|
+
// E-Step
|
91
|
+
// (自己エントロピー)
|
92
|
+
private double[] calcEStep(double[] yi, double[] b) {
|
93
|
+
double[] bh = new double[b.length];
|
94
|
+
|
95
|
+
Arrays.fill(bh, 0.0);
|
96
|
+
for(int j = 0; j < b.length; j++) {
|
97
|
+
for(int i = 0; i < yi.length; i++) {
|
98
|
+
double p = linkFunc(yi[i]);
|
99
|
+
|
100
|
+
bh[j] += p * Math.log(p);
|
101
|
+
}
|
102
|
+
bh[j] *= -1;
|
103
|
+
}
|
104
|
+
return bh;
|
105
|
+
}
|
106
|
+
// M-Step
|
107
|
+
// (KLダイバージェンス)
|
108
|
+
private double[] calcMStep(double[] yi, double[] b, double[][] xij) {
|
109
|
+
double[] xi = new double[b.length];
|
110
|
+
double[] ei = new double[b.length];
|
111
|
+
|
112
|
+
Arrays.fill(ei, 0.0);
|
113
|
+
for(int j = 0; j < b.length; j++) {
|
114
|
+
for(int i = 0; i < xij.length; i++) {
|
115
|
+
xi[0] = 1.0;
|
116
|
+
System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
|
117
|
+
|
118
|
+
double q = linkFunc(yi[i]);
|
119
|
+
double p = linkFunc(regression(b, xi, 0));
|
120
|
+
|
121
|
+
ei[j] += q * (Math.log(q) - Math.log(p)) * xi[j];
|
122
|
+
}
|
123
|
+
}
|
124
|
+
return ei;
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
@@ -0,0 +1,85 @@
|
|
1
|
+
import java.util.Map;
|
2
|
+
import java.util.Arrays;
|
3
|
+
import org.apache.commons.math3.distribution.BetaDistribution;
|
4
|
+
|
5
|
+
public class LogitBayesRegAna extends AbstractGLMM {
|
6
|
+
private final int NUM = 1000;
|
7
|
+
private final int TIM = 3;
|
8
|
+
private static LogitBayesRegAna regana = new LogitBayesRegAna();
|
9
|
+
public static LogitBayesRegAna getInstance() {
|
10
|
+
return regana;
|
11
|
+
}
|
12
|
+
public LineReg nonLineRegAna(double[] yi, double xij[][]) {
|
13
|
+
double[] b = initB(xij[0].length);
|
14
|
+
|
15
|
+
for (int i = 0; i < NUM; i++) {
|
16
|
+
b = mcmcGS(yi, b, xij);
|
17
|
+
}
|
18
|
+
|
19
|
+
return new LineReg(b);
|
20
|
+
}
|
21
|
+
public double getBIC(Map<String, Object> regCoe, double[][] xij) {
|
22
|
+
double[] b = new double[1 + xij[0].length];
|
23
|
+
|
24
|
+
b[0] = (double)regCoe.get("intercept");
|
25
|
+
System.arraycopy(regCoe.get("slope"), 0, b, 1, xij[0].length);
|
26
|
+
return calcBIC(b, xij);
|
27
|
+
}
|
28
|
+
private double[] initB(int xsie) {
|
29
|
+
double[] b = new double[1 + xsie];
|
30
|
+
BetaDistribution beDist = new BetaDistribution(50, 50);
|
31
|
+
|
32
|
+
for(int i = 0; i < b.length; i++) {
|
33
|
+
b[i] = beDist.sample();
|
34
|
+
}
|
35
|
+
return b;
|
36
|
+
}
|
37
|
+
private double[] calcMeanBy(double[] yi, double[] b) {
|
38
|
+
double[] meanB = new double[b.length];
|
39
|
+
|
40
|
+
Arrays.fill(meanB, 0.0);
|
41
|
+
for(int i = 0; i < meanB.length; i++) {
|
42
|
+
for(int j = 0; j < yi.length; j++) {
|
43
|
+
meanB[i] += yi[j] * b[i];
|
44
|
+
}
|
45
|
+
}
|
46
|
+
return meanB;
|
47
|
+
}
|
48
|
+
// q = b0 + b1 * x0
|
49
|
+
double regression(double[] b, double[] xi, double r) {
|
50
|
+
double ret = 0.0;
|
51
|
+
|
52
|
+
for(int i = 0; i < xi.length; i++) {
|
53
|
+
ret += b[i] * xi[i];
|
54
|
+
}
|
55
|
+
return ret;
|
56
|
+
}
|
57
|
+
// p = 1 / (1 + exp( -q))
|
58
|
+
double linkFunc(double q) {
|
59
|
+
return 1.0 / (1.0 + Math.exp(-1.0 * q));
|
60
|
+
}
|
61
|
+
/*********************************/
|
62
|
+
/* interface define */
|
63
|
+
/*********************************/
|
64
|
+
/*********************************/
|
65
|
+
/* class define */
|
66
|
+
/*********************************/
|
67
|
+
public class LineReg {
|
68
|
+
private double a = 0.0;
|
69
|
+
private double[] b = null;
|
70
|
+
public LineReg(double[] b) {
|
71
|
+
this.a = b[0];
|
72
|
+
this.b = new double[b.length - 1];
|
73
|
+
for (int i = 0; i < this.b.length; i++) {
|
74
|
+
this.b[i] = b[i + 1];
|
75
|
+
}
|
76
|
+
}
|
77
|
+
public double getIntercept() {
|
78
|
+
return a;
|
79
|
+
}
|
80
|
+
public double[] getSlope() {
|
81
|
+
return b;
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
@@ -1,12 +1,45 @@
|
|
1
|
-
|
1
|
+
import java.util.Arrays;
|
2
|
+
import java.util.Map;
|
3
|
+
|
4
|
+
public class LogitRegAna extends AbstractGLM {
|
5
|
+
private final int NUM = 1000;
|
2
6
|
private static LogitRegAna regana = new LogitRegAna();
|
3
7
|
public static LogitRegAna getInstance() {
|
4
8
|
return regana;
|
5
9
|
}
|
6
10
|
public LineReg nonLineRegAna(double[] yi, double xij[][]) {
|
7
|
-
|
11
|
+
double[] b = initB(xij[0].length);
|
12
|
+
|
13
|
+
for (int i = 0; i < NUM; i++) {
|
14
|
+
b = grand_metod(yi, b, xij);
|
15
|
+
}
|
16
|
+
return new LineReg(b);
|
17
|
+
}
|
18
|
+
public double getAIC(Map<String, Object> regCoe, double[][] xij) {
|
19
|
+
double[] b = new double[1 + xij[0].length];
|
8
20
|
|
9
|
-
|
21
|
+
b[0] = (double)regCoe.get("intercept");
|
22
|
+
System.arraycopy(regCoe.get("slope"), 0, b, 1, xij[0].length);
|
23
|
+
return calcAIC(b, xij);
|
24
|
+
}
|
25
|
+
private double[] initB(int xsie) {
|
26
|
+
double[] b = new double[1 + xsie];
|
27
|
+
|
28
|
+
Arrays.fill(b, 0.0);
|
29
|
+
return b;
|
30
|
+
}
|
31
|
+
// q = b0 + b1 * x0
|
32
|
+
double regression(double[] b, double[] xi) {
|
33
|
+
double ret = 0.0;
|
34
|
+
|
35
|
+
for(int i = 0; i < xi.length; i++) {
|
36
|
+
ret += b[i] * xi[i];
|
37
|
+
}
|
38
|
+
return ret;
|
39
|
+
}
|
40
|
+
// p = 1 / (1 + exp( -q))
|
41
|
+
double linkFunc(double q) {
|
42
|
+
return 1.0 / (1.0 + Math.exp(-1.0 * q));
|
10
43
|
}
|
11
44
|
/*********************************/
|
12
45
|
/* interface define */
|
@@ -31,52 +64,5 @@ public class LogitRegAna {
|
|
31
64
|
return b;
|
32
65
|
}
|
33
66
|
}
|
34
|
-
private class NonLineRegAna {
|
35
|
-
private final double eta = 0.001;
|
36
|
-
private final int num = 10000;
|
37
|
-
public LineReg nonLineRegAna(double[] yi, double[][] xij) {
|
38
|
-
double[] b = new double[1 + xij[0].length];
|
39
|
-
|
40
|
-
for(int i = 0; i < b.length; i++) {
|
41
|
-
b[i] = 0.0;
|
42
|
-
}
|
43
|
-
for (int i = 0; i < num; i++) {
|
44
|
-
b = grand_metod(yi, b, xij);
|
45
|
-
}
|
46
|
-
return new LineReg(b);
|
47
|
-
}
|
48
|
-
// q = b0 + b1 * x0
|
49
|
-
private double rereion(double[] b, double[] xi) {
|
50
|
-
double ret = b[0];
|
51
|
-
|
52
|
-
for(int i = 0; i < xi.length; i++) {
|
53
|
-
ret += b[i + 1] * xi[i];
|
54
|
-
}
|
55
|
-
return ret;
|
56
|
-
}
|
57
|
-
// p = 1 / (1 + exp( -q))
|
58
|
-
private double sigmoid(double q) {
|
59
|
-
return 1.0 / (1.0 + Math.exp(-1.0 * q));
|
60
|
-
}
|
61
|
-
private double[] grand_metod(double[] yi, double[] b, double[][] xij) {
|
62
|
-
double e0 = 0.0;
|
63
|
-
double[] en = new double[xij[0].length];
|
64
|
-
|
65
|
-
for(int i = 0; i < yi.length; i++) {
|
66
|
-
double q = rereion(b, xij[i]);
|
67
|
-
double p = sigmoid(q);
|
68
|
-
|
69
|
-
e0 += (yi[i] - p);
|
70
|
-
for(int j = 0; j < en.length; j++) {
|
71
|
-
en[j] += (yi[i] - p) * xij[i][j];
|
72
|
-
}
|
73
|
-
}
|
74
|
-
b[0] = b[0] + eta * e0;
|
75
|
-
for(int j = 0; j < en.length; j++) {
|
76
|
-
b[1 + j] += eta * en[j];
|
77
|
-
}
|
78
|
-
return b;
|
79
|
-
}
|
80
|
-
}
|
81
67
|
}
|
82
68
|
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import java.util.Arrays;
|
2
|
+
import java.util.Map;
|
3
|
+
import org.apache.commons.math3.distribution.BetaDistribution;
|
4
|
+
|
5
|
+
public class PoissonBayesRegAna extends AbstractGLMM {
|
6
|
+
private final int NUM = 1000;
|
7
|
+
private final int TIM = 3;
|
8
|
+
private static PoissonBayesRegAna regana = new PoissonBayesRegAna();
|
9
|
+
public static PoissonBayesRegAna getInstance() {
|
10
|
+
return regana;
|
11
|
+
}
|
12
|
+
public LineReg nonLineRegAna(double[] yi, double xij[][]) {
|
13
|
+
double[] b = initB(xij[0].length);
|
14
|
+
|
15
|
+
for (int i = 0; i < NUM; i++) {
|
16
|
+
b = mcmcGS(yi, b, xij);
|
17
|
+
}
|
18
|
+
return new LineReg(b);
|
19
|
+
}
|
20
|
+
public double getBIC(Map<String, Object> regCoe, double[][] xij) {
|
21
|
+
double[] b = new double[1 + xij[0].length];
|
22
|
+
|
23
|
+
b[0] = (double)regCoe.get("intercept");
|
24
|
+
System.arraycopy(regCoe.get("slope"), 0, b, 1, xij[0].length);
|
25
|
+
return calcBIC(b, xij);
|
26
|
+
}
|
27
|
+
private double[] initB(int xsie) {
|
28
|
+
double[] b = new double[1 + xsie];
|
29
|
+
BetaDistribution beDist = new BetaDistribution(50, 50);
|
30
|
+
|
31
|
+
for(int i = 0; i < b.length; i++) {
|
32
|
+
b[i] = beDist.sample();
|
33
|
+
}
|
34
|
+
return b;
|
35
|
+
}
|
36
|
+
// q = b0 + b1 * x0
|
37
|
+
double regression(double[] b, double[] xi, double r) {
|
38
|
+
double ret = 0.0;
|
39
|
+
|
40
|
+
for(int i = 0; i < xi.length; i++) {
|
41
|
+
ret += b[i] * xi[i];
|
42
|
+
}
|
43
|
+
return ret;
|
44
|
+
}
|
45
|
+
// p = exp(q)
|
46
|
+
double linkFunc(double q) {
|
47
|
+
return Math.exp(q);
|
48
|
+
}
|
49
|
+
/*********************************/
|
50
|
+
/* interface define */
|
51
|
+
/*********************************/
|
52
|
+
/*********************************/
|
53
|
+
/* class define */
|
54
|
+
/*********************************/
|
55
|
+
public class LineReg {
|
56
|
+
private double a = 0.0;
|
57
|
+
private double[] b = null;
|
58
|
+
public LineReg(double[] b) {
|
59
|
+
this.a = b[0];
|
60
|
+
this.b = new double[b.length - 1];
|
61
|
+
for (int i = 0; i < this.b.length; i++) {
|
62
|
+
this.b[i] = b[i + 1];
|
63
|
+
}
|
64
|
+
}
|
65
|
+
public double getIntercept() {
|
66
|
+
return a;
|
67
|
+
}
|
68
|
+
public double[] getSlope() {
|
69
|
+
return b;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
}
|
74
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
import java.util.Arrays;
|
2
|
+
import java.util.Map;
|
3
|
+
import org.apache.commons.math3.distribution.BetaDistribution;
|
4
|
+
|
5
|
+
public class PoissonHierBayesRegAna extends AbstractGLMM {
|
6
|
+
private final int NUM = 1000;
|
7
|
+
private static PoissonHierBayesRegAna regana = new PoissonHierBayesRegAna();
|
8
|
+
public static PoissonHierBayesRegAna getInstance() {
|
9
|
+
return regana;
|
10
|
+
}
|
11
|
+
public LineReg nonLineRegAna(double[] yi, double xij[][]) {
|
12
|
+
double[] b = initB(xij[0].length);
|
13
|
+
|
14
|
+
for (int i = 0; i < NUM; i++) {
|
15
|
+
b = mcmcGS(yi, b, xij);
|
16
|
+
}
|
17
|
+
return new LineReg(b);
|
18
|
+
}
|
19
|
+
private double[] initB(int xsie) {
|
20
|
+
double[] b = new double[1 + xsie];
|
21
|
+
BetaDistribution beDist = new BetaDistribution(50, 50);
|
22
|
+
|
23
|
+
for(int i = 0; i < b.length; i++) {
|
24
|
+
b[i] = beDist.sample();
|
25
|
+
}
|
26
|
+
return b;
|
27
|
+
}
|
28
|
+
// q = b0 + b1 * x0 + r
|
29
|
+
// (ランダム切片モデル)
|
30
|
+
double regression(double[] b, double[] xi, double r) {
|
31
|
+
double ret = 0.0;
|
32
|
+
|
33
|
+
for(int i = 0; i < xi.length; i++) {
|
34
|
+
ret += b[i] * xi[i];
|
35
|
+
}
|
36
|
+
return ret + r;
|
37
|
+
}
|
38
|
+
// p = exp(q)
|
39
|
+
double linkFunc(double q) {
|
40
|
+
return Math.exp(q);
|
41
|
+
}
|
42
|
+
/*********************************/
|
43
|
+
/* interface define */
|
44
|
+
/*********************************/
|
45
|
+
/*********************************/
|
46
|
+
/* class define */
|
47
|
+
/*********************************/
|
48
|
+
public class LineReg {
|
49
|
+
private double a = 0.0;
|
50
|
+
private double[] b = null;
|
51
|
+
public LineReg(double[] b) {
|
52
|
+
this.a = b[0];
|
53
|
+
this.b = new double[b.length - 1];
|
54
|
+
for (int i = 0; i < this.b.length; i++) {
|
55
|
+
this.b[i] = b[i + 1];
|
56
|
+
}
|
57
|
+
}
|
58
|
+
public double getIntercept() {
|
59
|
+
return a;
|
60
|
+
}
|
61
|
+
public double[] getSlope() {
|
62
|
+
return b;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
@@ -1,12 +1,45 @@
|
|
1
|
-
|
1
|
+
import java.util.Arrays;
|
2
|
+
import java.util.Map;
|
3
|
+
|
4
|
+
public class PoissonRegAna extends AbstractGLM {
|
5
|
+
private final int NUM = 1000;
|
2
6
|
private static PoissonRegAna regana = new PoissonRegAna();
|
3
7
|
public static PoissonRegAna getInstance() {
|
4
8
|
return regana;
|
5
9
|
}
|
6
10
|
public LineReg nonLineRegAna(double[] yi, double[][] xij) {
|
7
|
-
|
11
|
+
double[] b = initB(xij[0].length);
|
12
|
+
|
13
|
+
for (int i = 0; i < NUM; i++) {
|
14
|
+
b = grand_metod(yi, b, xij);
|
15
|
+
}
|
16
|
+
return new LineReg(b);
|
17
|
+
}
|
18
|
+
public double getAIC(Map<String, Object> regCoe, double[][] xij) {
|
19
|
+
double[] b = new double[1 + xij[0].length];
|
20
|
+
|
21
|
+
b[0] = (double)regCoe.get("intercept");
|
22
|
+
System.arraycopy(regCoe.get("slope"), 0, b, 1, xij[0].length);
|
23
|
+
return calcAIC(b, xij);
|
24
|
+
}
|
25
|
+
private double[] initB(int xsie) {
|
26
|
+
double[] b = new double[1 + xsie];
|
27
|
+
|
28
|
+
Arrays.fill(b, 0.0);
|
29
|
+
return b;
|
30
|
+
}
|
31
|
+
// q = b0 + b1 * x0
|
32
|
+
double regression(double[] b, double[] xi) {
|
33
|
+
double ret = 0.0;
|
8
34
|
|
9
|
-
|
35
|
+
for(int i = 0; i < xi.length; i++) {
|
36
|
+
ret += b[i] * xi[i];
|
37
|
+
}
|
38
|
+
return ret;
|
39
|
+
}
|
40
|
+
// p = exp(q)
|
41
|
+
double linkFunc(double q) {
|
42
|
+
return Math.exp(q);
|
10
43
|
}
|
11
44
|
/*********************************/
|
12
45
|
/* interface define */
|
@@ -31,52 +64,5 @@ public class PoissonRegAna {
|
|
31
64
|
return b;
|
32
65
|
}
|
33
66
|
}
|
34
|
-
private class NonLineRegAna {
|
35
|
-
private final double eta = 0.005;
|
36
|
-
private final int num = 1000;
|
37
|
-
public LineReg nonLineRegAna(double[] yi, double[][] xij) {
|
38
|
-
double[] b = new double[1 + xij[0].length];
|
39
|
-
|
40
|
-
for(int i = 0; i < b.length; i++) {
|
41
|
-
b[i] = 0.0;
|
42
|
-
}
|
43
|
-
for (int i = 0; i < num; i++) {
|
44
|
-
b = grand_metod(yi, b, xij);
|
45
|
-
}
|
46
|
-
|
47
|
-
return new LineReg(b);
|
48
|
-
}
|
49
|
-
// q = b0 + b1 * x0
|
50
|
-
private double rereion(double[] b, double[] xi) {
|
51
|
-
double ret = b[0];
|
52
|
-
|
53
|
-
for(int i = 0; i < xi.length; i++) {
|
54
|
-
ret += b[i + 1] * xi[i];
|
55
|
-
}
|
56
|
-
return ret;
|
57
|
-
}
|
58
|
-
private double linkFunc(double q) {
|
59
|
-
return Math.exp(q);
|
60
|
-
}
|
61
|
-
private double[] grand_metod(double[] yi, double[] b, double[][] xij) {
|
62
|
-
double e0 = 0.0;
|
63
|
-
double[] en = new double[xij[0].length];
|
64
|
-
|
65
|
-
for(int i = 0; i < yi.length; i++) {
|
66
|
-
double q = rereion(b, xij[i]);
|
67
|
-
double p = linkFunc(q);
|
68
|
-
|
69
|
-
e0 += (yi[i] - p);
|
70
|
-
for(int j = 0; j < en.length; j++) {
|
71
|
-
en[j] += (yi[i] - p) * xij[i][j];
|
72
|
-
}
|
73
|
-
}
|
74
|
-
b[0] += eta * e0;
|
75
|
-
for(int j = 0; j < en.length; j++) {
|
76
|
-
b[1 + j] += eta * en[j];
|
77
|
-
}
|
78
|
-
return b;
|
79
|
-
}
|
80
|
-
}
|
81
67
|
}
|
82
68
|
|
@@ -0,0 +1,71 @@
|
|
1
|
+
import java.util.Arrays;
|
2
|
+
import org.apache.commons.math3.distribution.NormalDistribution;
|
3
|
+
import java.util.Map;
|
4
|
+
|
5
|
+
public class ProBitRegAna extends AbstractGLM {
|
6
|
+
private final int NUM = 1000;
|
7
|
+
private static ProBitRegAna regana = new ProBitRegAna();
|
8
|
+
private NormalDistribution ndist = new NormalDistribution(0, 1);
|
9
|
+
|
10
|
+
public static ProBitRegAna getInstance() {
|
11
|
+
return regana;
|
12
|
+
}
|
13
|
+
public LineReg nonLineRegAna(double[] yi, double[][] xij) {
|
14
|
+
double[] b = initB(xij[0].length);
|
15
|
+
|
16
|
+
for (int i = 0; i < NUM; i++) {
|
17
|
+
b = grand_metod(yi, b, xij);
|
18
|
+
}
|
19
|
+
return new LineReg(b);
|
20
|
+
}
|
21
|
+
public double getAIC(Map<String, Object> regCoe, double[][] xij) {
|
22
|
+
double[] b = new double[1 + xij[0].length];
|
23
|
+
|
24
|
+
b[0] = (double)regCoe.get("intercept");
|
25
|
+
System.arraycopy(regCoe.get("slope"), 0, b, 1, xij[0].length);
|
26
|
+
return calcAIC(b, xij);
|
27
|
+
}
|
28
|
+
private double[] initB(int xsie) {
|
29
|
+
double[] b = new double[1 + xsie];
|
30
|
+
|
31
|
+
Arrays.fill(b, 0.0);
|
32
|
+
return b;
|
33
|
+
}
|
34
|
+
// q = b0 + b1 * x0
|
35
|
+
double regression(double[] b, double[] xi) {
|
36
|
+
double ret = 0.0;
|
37
|
+
|
38
|
+
for(int i = 0; i < xi.length; i++) {
|
39
|
+
ret += b[i] * xi[i];
|
40
|
+
}
|
41
|
+
return ret;
|
42
|
+
}
|
43
|
+
//
|
44
|
+
double linkFunc(double q) {
|
45
|
+
return ndist.cumulativeProbability(q);
|
46
|
+
}
|
47
|
+
/*********************************/
|
48
|
+
/* interface define */
|
49
|
+
/*********************************/
|
50
|
+
/*********************************/
|
51
|
+
/* class define */
|
52
|
+
/*********************************/
|
53
|
+
public class LineReg {
|
54
|
+
private double a = 0.0;
|
55
|
+
private double[] b = null;
|
56
|
+
public LineReg(double[] b) {
|
57
|
+
this.a = b[0];
|
58
|
+
this.b = new double[b.length - 1];
|
59
|
+
for (int i = 0; i < this.b.length; i++) {
|
60
|
+
this.b[i] = b[i + 1];
|
61
|
+
}
|
62
|
+
}
|
63
|
+
public double getIntercept() {
|
64
|
+
return a;
|
65
|
+
}
|
66
|
+
public double[] getSlope() {
|
67
|
+
return b;
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'num4regana.jar'
|
3
|
+
require 'commons-math3-3.6.1.jar'
|
4
|
+
|
5
|
+
java_import 'LogitBayesRegAna'
|
6
|
+
java_import 'PoissonBayesRegAna'
|
7
|
+
java_import 'java.util.HashMap'
|
8
|
+
|
9
|
+
# 一般化線形混合モデル
|
10
|
+
# (Apache commoms math3使用)
|
11
|
+
module Num4GLMMRegAnaLib
|
12
|
+
# (2項)ベイズロジスティック回帰分析
|
13
|
+
class LogitBayesRegAnaLib
|
14
|
+
def initialize
|
15
|
+
@multana = LogitBayesRegAna.getInstance()
|
16
|
+
end
|
17
|
+
# (2項)ベイズロジスティック回帰分析
|
18
|
+
#
|
19
|
+
# @overload non_line_reg_ana(yi, xij)
|
20
|
+
# @param [Array] yi yの値(double[])
|
21
|
+
# @param [Array] xij xの値(double[][])
|
22
|
+
# @return [Hash] (intercept:定数項 slope:回帰係数)
|
23
|
+
# @example
|
24
|
+
# glsyi = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
25
|
+
# glsxij = [
|
26
|
+
# [1, 24],
|
27
|
+
# [1, 18],
|
28
|
+
# [0, 15],
|
29
|
+
# [1, 16],
|
30
|
+
# [0, 10],
|
31
|
+
# [1, 26],
|
32
|
+
# [1, 2],
|
33
|
+
# [0, 24],
|
34
|
+
# [1, 18],
|
35
|
+
# [1, 22],
|
36
|
+
# [1, 3],
|
37
|
+
# [1, 6],
|
38
|
+
# [0, 15],
|
39
|
+
# [0, 12],
|
40
|
+
# [1, 6],
|
41
|
+
# [0, 6],
|
42
|
+
# [1, 12],
|
43
|
+
# [0, 12],
|
44
|
+
# [1, 18],
|
45
|
+
# [1, 3],
|
46
|
+
# [1, 8],
|
47
|
+
# [0, 9],
|
48
|
+
# [0, 12],
|
49
|
+
# [0, 6],
|
50
|
+
# [0, 8],
|
51
|
+
# [1, 12],
|
52
|
+
# ]
|
53
|
+
# regana = Num4GLMMRegAnaLib::LogitBayesRegAnaLib.new
|
54
|
+
# regana.non_line_reg_ana(glsyi, glsxij)
|
55
|
+
# =>
|
56
|
+
# {
|
57
|
+
# :intercept=>0.5742886218005325, # 定数項
|
58
|
+
# # 回帰係数
|
59
|
+
# :slope=>[0.5517212822536828, 0.5748054561700319]
|
60
|
+
# }
|
61
|
+
def non_line_reg_ana(yi, xij)
|
62
|
+
multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
|
63
|
+
retRb = {
|
64
|
+
"intercept": multRet.getIntercept(), # 定数項
|
65
|
+
"slope": multRet.getSlope().to_a, # 回帰係数
|
66
|
+
}
|
67
|
+
return retRb
|
68
|
+
end
|
69
|
+
# BIC
|
70
|
+
#
|
71
|
+
# @overload get_bic(regcoe, xij)
|
72
|
+
# @param [Hash] regcoe 回帰係数(intercept:定数項 slope:回帰係数)
|
73
|
+
# @param [Array] xij xの値(double[][])
|
74
|
+
# @return double AIC値
|
75
|
+
# @example
|
76
|
+
# reg = {
|
77
|
+
# :intercept=> -6.2313, # 定数項
|
78
|
+
# :slope=> [2.5995, 0.1652], # 回帰係数
|
79
|
+
# }
|
80
|
+
# xij = [
|
81
|
+
# [1, 24],
|
82
|
+
# [1, 18],
|
83
|
+
# [0, 15],
|
84
|
+
# [1, 16],
|
85
|
+
# [0, 10],
|
86
|
+
# [1, 26],
|
87
|
+
# [1, 2],
|
88
|
+
# [0, 24],
|
89
|
+
# [1, 18],
|
90
|
+
# [1, 22],
|
91
|
+
# [1, 3],
|
92
|
+
# [1, 6],
|
93
|
+
# [0, 15],
|
94
|
+
# [0, 12],
|
95
|
+
# [1, 6],
|
96
|
+
# [0, 6],
|
97
|
+
# [1, 12],
|
98
|
+
# [0, 12],
|
99
|
+
# [1, 18],
|
100
|
+
# [1, 3],
|
101
|
+
# [1, 8],
|
102
|
+
# [0, 9],
|
103
|
+
# [0, 12],
|
104
|
+
# [0, 6],
|
105
|
+
# [0, 8],
|
106
|
+
# [1, 12],
|
107
|
+
# ]
|
108
|
+
# regana = Num4GLMMRegAnaLib::LogitBayesRegAnaLib.new
|
109
|
+
# regana.get_bic(reg, xij)
|
110
|
+
# => 159.386
|
111
|
+
def get_bic(regcoe, xij)
|
112
|
+
o = HashMap.new
|
113
|
+
o["intercept"] = regcoe[:intercept]
|
114
|
+
o["slope"] = regcoe[:slope].to_java(Java::double)
|
115
|
+
@multana.getBIC(o, xij.to_java(Java::double[]))
|
116
|
+
end
|
117
|
+
end
|
118
|
+
# ベイズポアソン回帰分析
|
119
|
+
class PoissonBayesRegAnaLib
|
120
|
+
def initialize
|
121
|
+
@multana = PoissonBayesRegAna.getInstance()
|
122
|
+
end
|
123
|
+
# ベイズポアソン回帰分析
|
124
|
+
#
|
125
|
+
# @overload non_line_reg_ana(yi, xij)
|
126
|
+
# @param [Array] yi yの値(double[])
|
127
|
+
# @param [Array] xij xの値(double[][])
|
128
|
+
# @return [Hash] (intercept:定数項 slope:回帰係数)
|
129
|
+
# @example
|
130
|
+
# glsyi = [4, 10, 7, 14]
|
131
|
+
# glsxij = [
|
132
|
+
# [1],
|
133
|
+
# [2],
|
134
|
+
# [3],
|
135
|
+
# [4],
|
136
|
+
# ]
|
137
|
+
# regana = Num4GLMMRegAnaLib::PoissonBayesRegAnaLib.new
|
138
|
+
# regana.non_line_reg_ana(glsyi, glsxij)
|
139
|
+
# =>
|
140
|
+
# {
|
141
|
+
# :intercept=>0.4341885635221602, # 定数項
|
142
|
+
# :slope=>[0.5703137378188881] # 回帰係数
|
143
|
+
# }
|
144
|
+
def non_line_reg_ana(yi, xij)
|
145
|
+
multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
|
146
|
+
retRb = {
|
147
|
+
"intercept": multRet.getIntercept(), # 定数項
|
148
|
+
"slope": multRet.getSlope().to_a, # 回帰係数
|
149
|
+
}
|
150
|
+
return retRb
|
151
|
+
end
|
152
|
+
# BIC
|
153
|
+
#
|
154
|
+
# @overload get_bic(regcoe, xij)
|
155
|
+
# @param [Hash] regcoe 回帰係数(intercept:定数項 slope:回帰係数)
|
156
|
+
# @param [Array] xij xの値(double[][])
|
157
|
+
# @return double AIC値
|
158
|
+
# @example
|
159
|
+
# reg = {
|
160
|
+
# :intercept=>0.4341885635221602, # 定数項
|
161
|
+
# :slope=>[0.5703137378188881] # 回帰係数
|
162
|
+
# }
|
163
|
+
# xij = [
|
164
|
+
# [1],
|
165
|
+
# [2],
|
166
|
+
# [3],
|
167
|
+
# [4],
|
168
|
+
# ]
|
169
|
+
# regana = Num4GLMMRegAnaLib::BayesPoissonRegAnaLib.new
|
170
|
+
# regana.get_bic(reg, xij)
|
171
|
+
# => -13.157
|
172
|
+
def get_bic(regcoe, xij)
|
173
|
+
o = HashMap.new
|
174
|
+
o["intercept"] = regcoe[:intercept]
|
175
|
+
o["slope"] = regcoe[:slope].to_java(Java::double)
|
176
|
+
@multana.getBIC(o, xij.to_java(Java::double[]))
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
data/lib/num4glmregana.rb
CHANGED
@@ -4,6 +4,8 @@ require 'commons-math3-3.6.1.jar'
|
|
4
4
|
|
5
5
|
java_import 'LogitRegAna'
|
6
6
|
java_import 'PoissonRegAna'
|
7
|
+
java_import 'ProBitRegAna'
|
8
|
+
java_import 'java.util.HashMap'
|
7
9
|
|
8
10
|
# 一般化線形回帰分析
|
9
11
|
# (Apache commoms math3使用)
|
@@ -20,35 +22,41 @@ module Num4GLMRegAnaLib
|
|
20
22
|
# @param [Array] xij xの値(double[][])
|
21
23
|
# @return [Hash] (intercept:定数項 slope:回帰係数)
|
22
24
|
# @example
|
23
|
-
# glsyi = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
25
|
+
# glsyi = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
24
26
|
# glsxij = [
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
27
|
+
# [1, 24],
|
28
|
+
# [1, 18],
|
29
|
+
# [0, 15],
|
30
|
+
# [1, 16],
|
31
|
+
# [0, 10],
|
32
|
+
# [1, 26],
|
33
|
+
# [1, 2],
|
34
|
+
# [0, 24],
|
35
|
+
# [1, 18],
|
36
|
+
# [1, 22],
|
37
|
+
# [1, 3],
|
38
|
+
# [1, 6],
|
39
|
+
# [0, 15],
|
40
|
+
# [0, 12],
|
41
|
+
# [1, 6],
|
42
|
+
# [0, 6],
|
43
|
+
# [1, 12],
|
44
|
+
# [0, 12],
|
45
|
+
# [1, 18],
|
46
|
+
# [1, 3],
|
47
|
+
# [1, 8],
|
48
|
+
# [0, 9],
|
49
|
+
# [0, 12],
|
50
|
+
# [0, 6],
|
51
|
+
# [0, 8],
|
52
|
+
# [1, 12],
|
45
53
|
# ]
|
46
54
|
# regana = Num4RegAnaLib::LogitRegAnaLib.new
|
47
55
|
# regana.non_line_reg_ana(glsyi, glsxij)
|
48
56
|
# =>
|
49
57
|
# {
|
50
|
-
#
|
51
|
-
#
|
58
|
+
# "intercept": -6.2313, # 定数項
|
59
|
+
# "slope": [2.5995, 0.1652], # 回帰係数
|
52
60
|
# }
|
53
61
|
def non_line_reg_ana(yi, xij)
|
54
62
|
multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
|
@@ -58,6 +66,54 @@ module Num4GLMRegAnaLib
|
|
58
66
|
}
|
59
67
|
return retRb
|
60
68
|
end
|
69
|
+
# AIC
|
70
|
+
#
|
71
|
+
# @overload get_aic(regcoe, xij)
|
72
|
+
# @param [Hash] regcoe 回帰係数(intercept:定数項 slope:回帰係数)
|
73
|
+
# @param [Array] xij xの値(double[][])
|
74
|
+
# @return double AIC値
|
75
|
+
# @example
|
76
|
+
# reg = {
|
77
|
+
# :intercept=> -6.2313, # 定数項
|
78
|
+
# :slope=> [2.5995, 0.1652], # 回帰係数
|
79
|
+
# }
|
80
|
+
# xij = [
|
81
|
+
# [1, 24],
|
82
|
+
# [1, 18],
|
83
|
+
# [0, 15],
|
84
|
+
# [1, 16],
|
85
|
+
# [0, 10],
|
86
|
+
# [1, 26],
|
87
|
+
# [1, 2],
|
88
|
+
# [0, 24],
|
89
|
+
# [1, 18],
|
90
|
+
# [1, 22],
|
91
|
+
# [1, 3],
|
92
|
+
# [1, 6],
|
93
|
+
# [0, 15],
|
94
|
+
# [0, 12],
|
95
|
+
# [1, 6],
|
96
|
+
# [0, 6],
|
97
|
+
# [1, 12],
|
98
|
+
# [0, 12],
|
99
|
+
# [1, 18],
|
100
|
+
# [1, 3],
|
101
|
+
# [1, 8],
|
102
|
+
# [0, 9],
|
103
|
+
# [0, 12],
|
104
|
+
# [0, 6],
|
105
|
+
# [0, 8],
|
106
|
+
# [1, 12],
|
107
|
+
# ]
|
108
|
+
# regana = Num4RegAnaLib::LogitRegAnaLib.new
|
109
|
+
# regana.get_aic(reg, xij)
|
110
|
+
# => 155.612
|
111
|
+
def get_aic(regcoe, xij)
|
112
|
+
o = HashMap.new
|
113
|
+
o["intercept"] = regcoe[:intercept]
|
114
|
+
o["slope"] = regcoe[:slope].to_java(Java::double)
|
115
|
+
@multana.getAIC(o, xij.to_java(Java::double[]))
|
116
|
+
end
|
61
117
|
end
|
62
118
|
# ポアソン回帰分析
|
63
119
|
class PoissonRegAnaLib
|
@@ -85,6 +141,89 @@ module Num4GLMRegAnaLib
|
|
85
141
|
# "intercept": 1.3138, # 定数項
|
86
142
|
# "slope": [0.3173], # 回帰係数
|
87
143
|
# }
|
144
|
+
def non_line_reg_ana(yi, xij)
|
145
|
+
multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
|
146
|
+
retRb = {
|
147
|
+
"intercept": multRet.getIntercept(), # 定数項
|
148
|
+
"slope": multRet.getSlope().to_a, # 回帰係数
|
149
|
+
}
|
150
|
+
return retRb
|
151
|
+
end
|
152
|
+
# AIC
|
153
|
+
#
|
154
|
+
# @overload get_aic(regcoe, xij)
|
155
|
+
# @param [Hash] regcoe 回帰係数(intercept:定数項 slope:回帰係数)
|
156
|
+
# @param [Array] xij xの値(double[][])
|
157
|
+
# @return double AIC値
|
158
|
+
# @example
|
159
|
+
# reg = {
|
160
|
+
# :intercept => 1.3138, # 定数項
|
161
|
+
# :slope => [0.3173], # 回帰係数
|
162
|
+
# }
|
163
|
+
# xij = [
|
164
|
+
# [1],
|
165
|
+
# [2],
|
166
|
+
# [3],
|
167
|
+
# [4],
|
168
|
+
# ]
|
169
|
+
# regana = Num4RegAnaLib::PoissonRegAnaLib.new
|
170
|
+
# regana.get_aic(reg, xij)
|
171
|
+
# => -12.856
|
172
|
+
def get_aic(regcoe, xij)
|
173
|
+
o = HashMap.new
|
174
|
+
o["intercept"] = regcoe[:intercept]
|
175
|
+
o["slope"] = regcoe[:slope].to_java(Java::double)
|
176
|
+
@multana.getAIC(o, xij.to_java(Java::double[]))
|
177
|
+
end
|
178
|
+
end
|
179
|
+
# プロビット回帰分析
|
180
|
+
class ProBitRegAnaLib
|
181
|
+
def initialize
|
182
|
+
@multana = ProBitRegAna.getInstance()
|
183
|
+
end
|
184
|
+
# プロビット回帰分析
|
185
|
+
#
|
186
|
+
# @overload non_line_reg_ana(yi, xij)
|
187
|
+
# @param [Array] yi yの値(double[])
|
188
|
+
# @param [Array] xij xの値(double[][])
|
189
|
+
# @return [Hash] (intercept:定数項 slope:回帰係数)
|
190
|
+
# @example
|
191
|
+
# glsyi = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
192
|
+
# glsxij = [
|
193
|
+
# [1, 24],
|
194
|
+
# [1, 18],
|
195
|
+
# [0, 15],
|
196
|
+
# [1, 16],
|
197
|
+
# [0, 10],
|
198
|
+
# [1, 26],
|
199
|
+
# [1, 2],
|
200
|
+
# [0, 24],
|
201
|
+
# [1, 18],
|
202
|
+
# [1, 22],
|
203
|
+
# [1, 3],
|
204
|
+
# [1, 6],
|
205
|
+
# [0, 15],
|
206
|
+
# [0, 12],
|
207
|
+
# [1, 6],
|
208
|
+
# [0, 6],
|
209
|
+
# [1, 12],
|
210
|
+
# [0, 12],
|
211
|
+
# [1, 18],
|
212
|
+
# [1, 3],
|
213
|
+
# [1, 8],
|
214
|
+
# [0, 9],
|
215
|
+
# [0, 12],
|
216
|
+
# [0, 6],
|
217
|
+
# [0, 8],
|
218
|
+
# [1, 12],
|
219
|
+
# ]
|
220
|
+
# regana = Num4RegAnaLib::ProBitRegAnaLib.new
|
221
|
+
# regana.non_line_reg_ana(glsyi, glsxij)
|
222
|
+
# =>
|
223
|
+
# {
|
224
|
+
# "intercept": -5.0497, # 定数項
|
225
|
+
# "slope": [2.2379, 0.2973], # 回帰係数
|
226
|
+
# }
|
88
227
|
def non_line_reg_ana(yi, xij)
|
89
228
|
multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
|
90
229
|
retRb = {
|
@@ -93,6 +232,54 @@ module Num4GLMRegAnaLib
|
|
93
232
|
}
|
94
233
|
return retRb
|
95
234
|
end
|
235
|
+
# AIC
|
236
|
+
#
|
237
|
+
# @overload get_aic(regcoe, xij)
|
238
|
+
# @param [Hash] regcoe 回帰係数(intercept:定数項 slope:回帰係数)
|
239
|
+
# @param [Array] xij xの値(double[][])
|
240
|
+
# @return double AIC値
|
241
|
+
# @example
|
242
|
+
# reg = {
|
243
|
+
# :intercept=> -5.0497, # 定数項
|
244
|
+
# :slope=> [2.2379, 0.2973], # 回帰係数
|
245
|
+
# }
|
246
|
+
# xij = [
|
247
|
+
# [1, 24],
|
248
|
+
# [1, 18],
|
249
|
+
# [0, 15],
|
250
|
+
# [1, 16],
|
251
|
+
# [0, 10],
|
252
|
+
# [1, 26],
|
253
|
+
# [1, 2],
|
254
|
+
# [0, 24],
|
255
|
+
# [1, 18],
|
256
|
+
# [1, 22],
|
257
|
+
# [1, 3],
|
258
|
+
# [1, 6],
|
259
|
+
# [0, 15],
|
260
|
+
# [0, 12],
|
261
|
+
# [1, 6],
|
262
|
+
# [0, 6],
|
263
|
+
# [1, 12],
|
264
|
+
# [0, 12],
|
265
|
+
# [1, 18],
|
266
|
+
# [1, 3],
|
267
|
+
# [1, 8],
|
268
|
+
# [0, 9],
|
269
|
+
# [0, 12],
|
270
|
+
# [0, 6],
|
271
|
+
# [0, 8],
|
272
|
+
# [1, 12],
|
273
|
+
# ]
|
274
|
+
# regana = Num4RegAnaLib::ProBitRegAnaLib.new
|
275
|
+
# regana.get_aic(reg, xij)
|
276
|
+
# => 119.674
|
277
|
+
def get_aic(regcoe, xij)
|
278
|
+
o = HashMap.new
|
279
|
+
o["intercept"] = regcoe[:intercept]
|
280
|
+
o["slope"] = regcoe[:slope].to_java(Java::double)
|
281
|
+
@multana.getAIC(o, xij.to_java(Java::double[]))
|
282
|
+
end
|
96
283
|
end
|
97
284
|
end
|
98
285
|
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'num4regana.jar'
|
3
|
+
require 'commons-math3-3.6.1.jar'
|
4
|
+
|
5
|
+
java_import 'PoissonHierBayesRegAna'
|
6
|
+
# 階層ベイズモデル
|
7
|
+
module Num4HBMRegAnaLib
|
8
|
+
# 階層ベイズポアソン回帰分析
|
9
|
+
class PoissonHierBayesRegAnaLib
|
10
|
+
def initialize
|
11
|
+
@multana = PoissonHierBayesRegAna.getInstance()
|
12
|
+
end
|
13
|
+
# ポアソン回帰分析
|
14
|
+
#
|
15
|
+
# @overload non_line_reg_ana(yi, xij)
|
16
|
+
# @param [Array] yi yの値(double[])
|
17
|
+
# @param [Array] xij xの値(double[][])
|
18
|
+
# @return [Hash] (intercept:定数項 slope:回帰係数)
|
19
|
+
# @example
|
20
|
+
# glsyi = [4, 10, 7, 14]
|
21
|
+
# glsxij = [
|
22
|
+
# [1],
|
23
|
+
# [2],
|
24
|
+
# [3],
|
25
|
+
# [4],
|
26
|
+
# ]
|
27
|
+
# regana = Num4RegAnaLib::HierBayesPoissonRegAnaLib.new
|
28
|
+
# regana.non_line_reg_ana(glsyi, glsxij)
|
29
|
+
# =>
|
30
|
+
# {
|
31
|
+
# "intercept": 0.477366, # 定数項
|
32
|
+
# "slope": [0.538545], # 回帰係数
|
33
|
+
# }
|
34
|
+
def non_line_reg_ana(yi, xij)
|
35
|
+
multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
|
36
|
+
retRb = {
|
37
|
+
"intercept": multRet.getIntercept(), # 定数項
|
38
|
+
"slope": multRet.getSlope().to_a, # 回帰係数
|
39
|
+
}
|
40
|
+
return retRb
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
data/lib/num4regana.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
require_relative('num4lineregana')
|
2
2
|
require_relative('num4glmregana')
|
3
|
+
require_relative('num4glmmregana')
|
4
|
+
require_relative('num4hbmregana')
|
3
5
|
|
6
|
+
# 回帰分析
|
4
7
|
module Num4RegAnaLib
|
5
8
|
include Num4LineRegAnaLib
|
6
9
|
include Num4GLMRegAnaLib
|
10
|
+
include Num4GLMMRegAnaLib
|
11
|
+
include Num4HBMRegAnaLib
|
7
12
|
end
|
8
13
|
|
9
14
|
|
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.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- siranovel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -61,12 +61,20 @@ files:
|
|
61
61
|
- Gemfile
|
62
62
|
- LICENSE
|
63
63
|
- Rakefile
|
64
|
+
- ext/num4regana/AbstractGLM.java
|
65
|
+
- ext/num4regana/AbstractGLMM.java
|
66
|
+
- ext/num4regana/LogitBayesRegAna.java
|
64
67
|
- ext/num4regana/LogitRegAna.java
|
65
68
|
- ext/num4regana/MultRegAna.java
|
69
|
+
- ext/num4regana/PoissonBayesRegAna.java
|
70
|
+
- ext/num4regana/PoissonHierBayesRegAna.java
|
66
71
|
- ext/num4regana/PoissonRegAna.java
|
72
|
+
- ext/num4regana/ProBitRegAna.java
|
67
73
|
- ext/num4regana/SmplRegAna.java
|
68
74
|
- lib/commons-math3-3.6.1.jar
|
75
|
+
- lib/num4glmmregana.rb
|
69
76
|
- lib/num4glmregana.rb
|
77
|
+
- lib/num4hbmregana.rb
|
70
78
|
- lib/num4lineregana.rb
|
71
79
|
- lib/num4regana.rb
|
72
80
|
homepage: http://github.com/siranovel/num4regana
|