num4regana 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: d2c06b0a7caeac79ec2e1d451dccabd387449af22574e60520125374bd4296d7
4
- data.tar.gz: 5744160fa6bec668de85a0ae0fd4b71625b6f4976a9b01e831ca71b4d654ec8c
3
+ metadata.gz: 07475023e86feba9a9a2013d572cdc433753da1cb570aa60fb3eb3662b09f3eb
4
+ data.tar.gz: 24a67fb47fe74071f39ae3d9d078071c1de5fce5b15afb74625354b34777b6be
5
5
  SHA512:
6
- metadata.gz: 7030429dc48f2807211cf07446cd6fbcee4094fe55410d3d54ef01c9151ead39123ef869da446e6497db1f7e95ea59810281111a0f2a05922cdd3396fc9da6ba
7
- data.tar.gz: 3967a10a750896e1b1a8f6bba7de42ae6a88862db18af456ba49f0018fc9e9e9d931aa792aceaa3e37d1c281dabfb52e461de484b0f349e59570e8e8ae73f3fe
6
+ metadata.gz: '058141be9257a86bc72b745402956142d015041e8812886a83ce2c49ac64993bc9fb1c864e2c7a093d0e42ba69f4dc45305f1eabb4fcffc4c0bc32d73333afb8'
7
+ data.tar.gz: db0acabd519684c4db318051df5467f50b0879aabdff44c426ff2c6e96559f762d286d1f8f7642052ff2dffa75847dc6f315610cad6c1d8fe268e14a251e6d22
data/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.6] - 2024-10-04
6
+
7
+ ### chg
8
+ - chg Hierarchical Bayesian models at EM algorithm
9
+
10
+ ## [0.0.5] - 2024-09-27
11
+
12
+ ### add
13
+ - add Bayesian models
14
+ - add Hierarchical Bayesian models
15
+
5
16
  ## [0.0.4] - 2024-09-13
6
17
 
7
18
  ### add
@@ -1,6 +1,6 @@
1
1
  import java.util.Arrays;
2
2
 
3
- abstract class AbstratGLM {
3
+ abstract class AbstractGLM {
4
4
  private final double eta = 0.005;
5
5
  abstract double regression(double[] b, double[] xi);
6
6
  abstract double linkFunc(double q);
@@ -18,23 +18,21 @@ abstract class AbstratGLM {
18
18
  // AIC
19
19
  protected double calcAIC(double[] b, double[][] xij) {
20
20
  // 尤度計算
21
- double maxL = calcL(b,xij);
21
+ double maxL = calcLogL(b,xij);
22
22
  int k = 1 + xij[0].length;
23
23
 
24
24
  return -2 * (maxL - k);
25
25
  }
26
26
  // 交差エントロピー計算
27
27
  private double[] calcE(double[] yi, double[] b, double[][] xij) {
28
- double[] xi = new double[1 + xij[0].length];
29
- double[] ei = new double[1 + xij[0].length];
28
+ double[] xi = new double[b.length];
29
+ double[] ei = new double[b.length];
30
30
 
31
31
  Arrays.fill(ei, 0.0);
32
32
  for(int i = 0; i < yi.length; i++) {
33
33
  xi[0] = 1.0;
34
34
  System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
35
-
36
- double q = regression(b, xi);
37
- double p = linkFunc(q);
35
+ double p = linkFunc(regression(b, xi));
38
36
 
39
37
  for(int j = 0; j < xi.length; j++) {
40
38
  ei[j] += (p - yi[i]) * xi[j];
@@ -43,20 +41,20 @@ abstract class AbstratGLM {
43
41
 
44
42
  return ei;
45
43
  }
46
- // 尤度計算(パラメータ)
47
- private double calcL(double[] b, double[][] xij) {
44
+ // 対数尤度計算(パラメータ)
45
+ private double calcLogL(double[] b, double[][] xij) {
48
46
  double l = 0.0;
49
- double[] xi = new double[1 + xij[0].length];
47
+ double[] xi = new double[b.length];
50
48
 
51
49
  for(int i = 0; i < xij.length; i++) {
52
50
  xi[0] = 1.0;
53
51
  System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
54
- double q = regression(b, xi);
55
- double p = linkFunc(q);
52
+
53
+ double p = linkFunc(regression(b, xi));
56
54
 
57
55
  l += Math.log(p);
58
56
  }
59
57
  return l;
60
- }
58
+ }
61
59
  }
62
60
 
@@ -0,0 +1,200 @@
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
+ double[] newB = new double[b.length];
14
+
15
+ for(int i = 0; i < b.length; i++) {
16
+ newB = Arrays.copyOf(b, b.length);
17
+ newB[i] = beDist.sample();
18
+
19
+ b[i] = mcmcSample(
20
+ calcLx(b,xij), // oldL
21
+ calcLx(newB,xij), // newL
22
+ new double[] {b[i], newB[i]} // bTbl: [0]=> oldB, [1]=> newB
23
+ );
24
+ }
25
+ return b;
26
+ }
27
+ // BIC
28
+ protected double calcBIC(double[] b, double[][] xij) {
29
+ // 尤度計算
30
+ double maxL = calcLogLx(b,xij);
31
+ int k = 1 + xij[0].length;
32
+ int n = xij.length;
33
+
34
+ return -2 * maxL + k * Math.log(n);
35
+ }
36
+ // EMアルゴリズム
37
+ protected double[] mcmcEM(double[] yi, double[] b, double[][] xij) {
38
+ double[] newB = new double[b.length];
39
+ double[][] bE = calcEStep(yi, b, xij);
40
+ double[] bM = calcMStep(yi, bE, xij);
41
+
42
+ for(int i = 0; i < newB.length; i++) {
43
+ newB[i] = bM[i];
44
+ }
45
+ return newB;
46
+ }
47
+ /*********************************/
48
+ /* interface define */
49
+ /*********************************/
50
+ /*********************************/
51
+ /* class define */
52
+ /*********************************/
53
+ private static class ArraysFillEx {
54
+ public static void fill(Object array, Object value) {
55
+ // 第一引数が配列か判定
56
+ Class<?> type = array.getClass();
57
+ if (!type.isArray()) {
58
+ throw new IllegalArgumentException("not array");
59
+ }
60
+
61
+ // クラスの型を判定
62
+ String arrayClassName = array.getClass().getSimpleName()
63
+ .replace("[]", "")
64
+ .toLowerCase();
65
+ String valueClassName = value.getClass().getSimpleName()
66
+ .toLowerCase()
67
+ .replace("character", "char")
68
+ .replace("integer", "int");
69
+ if (!arrayClassName.equals(valueClassName)) {
70
+ throw new IllegalArgumentException("does not matc");
71
+ }
72
+
73
+ // 処理
74
+ if (type.getComponentType().isArray()) {
75
+ for(Object o: (Object[])array) {
76
+ fill(o, value);
77
+ }
78
+ }
79
+ else if (array instanceof boolean[]) {
80
+ Arrays.fill((boolean[])array, (boolean)value);
81
+ }
82
+ else if (array instanceof char[]) {
83
+ Arrays.fill((char[])array, (char)value);
84
+ }
85
+ else if (array instanceof byte[]) {
86
+ Arrays.fill((byte[])array, (byte)value);
87
+ }
88
+ else if (array instanceof short[]) {
89
+ Arrays.fill((short[])array, (short)value);
90
+ }
91
+ else if (array instanceof int[]) {
92
+ Arrays.fill((int[])array, (int)value);
93
+ }
94
+ else if (array instanceof long[]) {
95
+ Arrays.fill((long[])array, (long)value);
96
+ }
97
+ else if (array instanceof float[]) {
98
+ Arrays.fill((float[])array, (float)value);
99
+ }
100
+ else if (array instanceof double[]) {
101
+ Arrays.fill((double[])array, (double)value);
102
+ }
103
+ else {
104
+ Arrays.fill((Object[])array, value);
105
+ }
106
+ }
107
+ }
108
+ /* ------------------------------------------------------------------ */
109
+ private double mcmcSample(double oldL, double newL, double[] bTbl) {
110
+ double r = newL / oldL;
111
+ BetaDistribution beDist2 = new BetaDistribution(1, 1); // 確率用
112
+ double b;
113
+
114
+ b = bTbl[0];
115
+ if (r > 1.0) {
116
+ b = bTbl[1];
117
+ }
118
+ else {
119
+ double r2 = beDist2.sample();
120
+
121
+ if (r2 < (1.0 - r)) {
122
+ b = bTbl[1];
123
+ }
124
+ }
125
+ return b;
126
+ }
127
+ // 尤度計算(パラメータ)
128
+ private double calcLx(double[] b, double[][] xij) {
129
+ double l = 1.0;
130
+ double[] xi = new double[b.length];
131
+
132
+ for(int i = 0; i < xij.length; i++) {
133
+ xi[0] = 1.0;
134
+ System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
135
+ double q = linkFunc(
136
+ regression(b, xi, nDist.sample())
137
+ );
138
+
139
+ l *= q;
140
+ }
141
+ return l;
142
+ }
143
+ // 対数尤度計算(パラメータ)
144
+ private double calcLogLx(double[] b, double[][] xij) {
145
+ double l = 0.0;
146
+ double[] xi = new double[b.length];
147
+
148
+ for(int i = 0; i < xij.length; i++) {
149
+ xi[0] = 1.0;
150
+ System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
151
+ double q = linkFunc(
152
+ regression(b, xi, nDist.sample())
153
+ );
154
+
155
+ l += Math.log(q);
156
+ }
157
+ return l;
158
+ }
159
+ // E-Step
160
+ // (Expetation:自己エントロピー)
161
+ private double[][] calcEStep(double[] yi, double[] b, double[][] xij) {
162
+ double[][] bh = new double[yi.length][b.length];
163
+ double[] xi = new double[b.length];
164
+
165
+ ArraysFillEx.fill(bh, 0.0);
166
+ for(int i = 0; i < yi.length; i++) {
167
+ xi[0] = 1.0;
168
+ System.arraycopy(xij[i], 0, xi, 1, xij[i].length);
169
+ double p = yi[i];
170
+ double q = linkFunc(regression(b, xi, nDist.sample()));
171
+
172
+ for(int j = 0; j < b.length; j++) {
173
+ bh[i][j] =
174
+ Math.log(p * xi[j]) - q * (Math.log(q) - Math.log(p * xi[j]));
175
+ }
176
+ }
177
+ return bh;
178
+ }
179
+ // M-Step
180
+ // (Maximiation:KLダイバージェンス)
181
+ private double[] calcMStep(double[] yi, double[][] q, double[][] xij) {
182
+ double[] xi = new double[1 + xij[0].length];
183
+ double[] ei = new double[1 + xij[0].length];
184
+
185
+ Arrays.fill(ei, 0.0);
186
+ for(int j = 0; j < xi.length; j++) {
187
+ for(int i = 0; i < xij.length; i++) {
188
+ xi[0] = 1.0;
189
+ System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
190
+
191
+ double p = yi[i];
192
+
193
+ ei[j] += q[i][j] * (Math.log(p * xi[j]) - Math.log(q[i][j]));
194
+ }
195
+ ei[j] = -1 * ei[j];
196
+ }
197
+ return ei;
198
+ }
199
+ }
200
+
@@ -0,0 +1,86 @@
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 + r
49
+ // (ランダム切片モデル)
50
+ double regression(double[] b, double[] xi, double r) {
51
+ double ret = 0.0;
52
+
53
+ for(int i = 0; i < xi.length; i++) {
54
+ ret += b[i] * xi[i];
55
+ }
56
+ return ret + r;
57
+ }
58
+ // p = 1 / (1 + exp( -q))
59
+ double linkFunc(double q) {
60
+ return 1.0 / (1.0 + Math.exp(-1.0 * q));
61
+ }
62
+ /*********************************/
63
+ /* interface define */
64
+ /*********************************/
65
+ /*********************************/
66
+ /* class define */
67
+ /*********************************/
68
+ public class LineReg {
69
+ private double a = 0.0;
70
+ private double[] b = null;
71
+ public LineReg(double[] b) {
72
+ this.a = b[0];
73
+ this.b = new double[b.length - 1];
74
+ for (int i = 0; i < this.b.length; i++) {
75
+ this.b[i] = b[i + 1];
76
+ }
77
+ }
78
+ public double getIntercept() {
79
+ return a;
80
+ }
81
+ public double[] getSlope() {
82
+ return b;
83
+ }
84
+ }
85
+ }
86
+
@@ -1,7 +1,7 @@
1
1
  import java.util.Arrays;
2
2
  import java.util.Map;
3
3
 
4
- public class LogitRegAna extends AbstratGLM {
4
+ public class LogitRegAna extends AbstractGLM {
5
5
  private final int NUM = 1000;
6
6
  private static LogitRegAna regana = new LogitRegAna();
7
7
  public static LogitRegAna getInstance() {
@@ -0,0 +1,75 @@
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 + r
37
+ // (ランダム切片モデル)
38
+ double regression(double[] b, double[] xi, double r) {
39
+ double ret = 0.0;
40
+
41
+ for(int i = 0; i < xi.length; i++) {
42
+ ret += b[i] * xi[i];
43
+ }
44
+ return ret + r;
45
+ }
46
+ // p = exp(q)
47
+ double linkFunc(double q) {
48
+ return Math.exp(q);
49
+ }
50
+ /*********************************/
51
+ /* interface define */
52
+ /*********************************/
53
+ /*********************************/
54
+ /* class define */
55
+ /*********************************/
56
+ public class LineReg {
57
+ private double a = 0.0;
58
+ private double[] b = null;
59
+ public LineReg(double[] b) {
60
+ this.a = b[0];
61
+ this.b = new double[b.length - 1];
62
+ for (int i = 0; i < this.b.length; i++) {
63
+ this.b[i] = b[i + 1];
64
+ }
65
+ }
66
+ public double getIntercept() {
67
+ return a;
68
+ }
69
+ public double[] getSlope() {
70
+ return b;
71
+ }
72
+ }
73
+
74
+ }
75
+
@@ -0,0 +1,61 @@
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 = mcmcEM(yi, b, xij);
16
+ }
17
+ return new LineReg(b);
18
+ }
19
+ private double[] initB(int xsie) {
20
+ double[] b = new double[1 + xsie];
21
+
22
+ Arrays.fill(b, 0.0);
23
+ return b;
24
+ }
25
+ // q = b0 + b1 * x0
26
+ double regression(double[] b, double[] xi, double r) {
27
+ double ret = 0.0;
28
+
29
+ for(int i = 0; i < xi.length; i++) {
30
+ ret += b[i] * xi[i];
31
+ }
32
+ return ret;
33
+ }
34
+ // p = exp(q)
35
+ double linkFunc(double q) {
36
+ return Math.exp(q);
37
+ }
38
+ /*********************************/
39
+ /* interface define */
40
+ /*********************************/
41
+ /*********************************/
42
+ /* class define */
43
+ /*********************************/
44
+ public class LineReg {
45
+ private double a = 0.0;
46
+ private double[] b = null;
47
+ public LineReg(double[] b) {
48
+ this.a = b[0];
49
+ this.b = new double[b.length - 1];
50
+ for (int i = 0; i < this.b.length; i++) {
51
+ this.b[i] = b[i + 1];
52
+ }
53
+ }
54
+ public double getIntercept() {
55
+ return a;
56
+ }
57
+ public double[] getSlope() {
58
+ return b;
59
+ }
60
+ }
61
+ }
@@ -1,7 +1,7 @@
1
1
  import java.util.Arrays;
2
2
  import java.util.Map;
3
3
 
4
- public class PoissonRegAna extends AbstratGLM {
4
+ public class PoissonRegAna extends AbstractGLM {
5
5
  private final int NUM = 1000;
6
6
  private static PoissonRegAna regana = new PoissonRegAna();
7
7
  public static PoissonRegAna getInstance() {
@@ -2,7 +2,7 @@ import java.util.Arrays;
2
2
  import org.apache.commons.math3.distribution.NormalDistribution;
3
3
  import java.util.Map;
4
4
 
5
- public class ProBitRegAna extends AbstratGLM {
5
+ public class ProBitRegAna extends AbstractGLM {
6
6
  private final int NUM = 1000;
7
7
  private static ProBitRegAna regana = new ProBitRegAna();
8
8
  private NormalDistribution ndist = new NormalDistribution(0, 1);
@@ -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 BIC値
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 BIC値
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
+
@@ -0,0 +1,45 @@
1
+ require 'java'
2
+ require 'num4regana.jar'
3
+ require 'commons-math3-3.6.1.jar'
4
+
5
+ java_import 'PoissonHierBayesRegAna'
6
+ # 階層ベイズモデル
7
+ # (Apache commoms math3使用)
8
+ module Num4HBMRegAnaLib
9
+ # 階層ベイズポアソン回帰分析
10
+ class PoissonHierBayesRegAnaLib
11
+ def initialize
12
+ @multana = PoissonHierBayesRegAna.getInstance()
13
+ end
14
+ # ポアソン回帰分析
15
+ #
16
+ # @overload non_line_reg_ana(yi, xij)
17
+ # @param [Array] yi yの値(double[])
18
+ # @param [Array] xij xの値(double[][])
19
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
20
+ # @example
21
+ # glsyi = [4, 10, 7, 14]
22
+ # glsxij = [
23
+ # [1],
24
+ # [2],
25
+ # [3],
26
+ # [4],
27
+ # ]
28
+ # regana = Num4RegAnaLib::HierBayesPoissonRegAnaLib.new
29
+ # regana.non_line_reg_ana(glsyi, glsxij)
30
+ # =>
31
+ # {
32
+ # "intercept": 0.477366, # 定数項
33
+ # "slope": [0.538545], # 回帰係数
34
+ # }
35
+ def non_line_reg_ana(yi, xij)
36
+ multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
37
+ retRb = {
38
+ "intercept": multRet.getIntercept(), # 定数項
39
+ "slope": multRet.getSlope().to_a, # 回帰係数
40
+ }
41
+ return retRb
42
+ end
43
+ end
44
+ end
45
+
data/lib/num4regana.rb CHANGED
@@ -1,10 +1,14 @@
1
1
  require_relative('num4lineregana')
2
2
  require_relative('num4glmregana')
3
+ require_relative('num4glmmregana')
4
+ require_relative('num4hbmregana')
3
5
 
4
6
  # 回帰分析
5
7
  module Num4RegAnaLib
6
8
  include Num4LineRegAnaLib
7
9
  include Num4GLMRegAnaLib
10
+ include Num4GLMMRegAnaLib
11
+ include Num4HBMRegAnaLib
8
12
  end
9
13
 
10
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
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: 2024-09-13 00:00:00.000000000 Z
11
+ date: 2024-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -61,15 +61,20 @@ files:
61
61
  - Gemfile
62
62
  - LICENSE
63
63
  - Rakefile
64
- - ext/num4regana/AbstratGLM.java
65
- - ext/num4regana/AbstratGLMM.java
64
+ - ext/num4regana/AbstractGLM.java
65
+ - ext/num4regana/AbstractGLMM.java
66
+ - ext/num4regana/LogitBayesRegAna.java
66
67
  - ext/num4regana/LogitRegAna.java
67
68
  - ext/num4regana/MultRegAna.java
69
+ - ext/num4regana/PoissonBayesRegAna.java
70
+ - ext/num4regana/PoissonHierBayesRegAna.java
68
71
  - ext/num4regana/PoissonRegAna.java
69
72
  - ext/num4regana/ProBitRegAna.java
70
73
  - ext/num4regana/SmplRegAna.java
71
74
  - lib/commons-math3-3.6.1.jar
75
+ - lib/num4glmmregana.rb
72
76
  - lib/num4glmregana.rb
77
+ - lib/num4hbmregana.rb
73
78
  - lib/num4lineregana.rb
74
79
  - lib/num4regana.rb
75
80
  homepage: http://github.com/siranovel/num4regana
@@ -1,18 +0,0 @@
1
- import java.util.Arrays;
2
- import org.apache.commons.math3.distribution.BetaDistribution;
3
-
4
- abstract class AbstratGLMM {
5
- abstract double rereion(double[] b, double[] xi, double r);
6
- abstract double linkFunc(double q);
7
- protected double[] mcmc(double[] yi, double[] b, double[][] xij) {
8
- double[] bnew = new double[1 + xij[0].length];
9
- BetaDistribution beDist = new BetaDistribution(1,1);
10
-
11
- for(int i= 0; i < bnew.length; i++) {
12
- System.out.printf("%f ", beDist.sample());
13
- }
14
- System.out.println();
15
- return null;
16
- }
17
- }
18
-