num4regana 0.0.4-java → 0.0.5-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: a35c59c0d504b97992d42739340fb1370e5fd80fc547987bb4d0a138f628b284
4
+ data.tar.gz: fec825eed34dd9c230fed6065accc753e3539bd5af5bb9b8a697c1a3752c06c6
5
5
  SHA512:
6
- metadata.gz: 7030429dc48f2807211cf07446cd6fbcee4094fe55410d3d54ef01c9151ead39123ef869da446e6497db1f7e95ea59810281111a0f2a05922cdd3396fc9da6ba
7
- data.tar.gz: 3967a10a750896e1b1a8f6bba7de42ae6a88862db18af456ba49f0018fc9e9e9d931aa792aceaa3e37d1c281dabfb52e461de484b0f349e59570e8e8ae73f3fe
6
+ metadata.gz: 10a7cbe03f96c05fc5995b696e6946fbc222d6b5a6b0f8b487cd5c50dfccca9d931d9312d5ab3589473b2589f6887885a142e6fcb467aa526499fb6812248368
7
+ data.tar.gz: 97009fb18fcb77dd771b6290eea892092710bf49d647e451cb720d94f2ead2a07826657b139e9842fc14d8c6b3aa3ac3fd938819bea090faa403bfe43384fb8e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.5] - 2024-09-27
6
+
7
+ ### add
8
+ - add Bayesian models
9
+ - add Hierarchical Bayesian models
10
+
5
11
  ## [0.0.4] - 2024-09-13
6
12
 
7
13
  ### 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,15 +18,15 @@ 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++) {
@@ -43,14 +43,15 @@ abstract class AbstratGLM {
43
43
 
44
44
  return ei;
45
45
  }
46
- // 尤度計算(パラメータ)
47
- private double calcL(double[] b, double[][] xij) {
46
+ // 対数尤度計算(パラメータ)
47
+ private double calcLogL(double[] b, double[][] xij) {
48
48
  double l = 0.0;
49
- double[] xi = new double[1 + xij[0].length];
49
+ double[] xi = new double[b.length];
50
50
 
51
51
  for(int i = 0; i < xij.length; i++) {
52
52
  xi[0] = 1.0;
53
53
  System.arraycopy(xij[i], 0, xi, 1, xij[0].length);
54
+
54
55
  double q = regression(b, xi);
55
56
  double p = linkFunc(q);
56
57
 
@@ -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,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,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,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 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
+
@@ -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,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.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-13 00:00:00.000000000 Z
11
+ date: 2024-09-27 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
-