num4regana 0.0.1-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 616a5ce4aa14748f31750e7b1c2e69ca9af26295e7a173a8454ff9b3bc83cbfe
4
+ data.tar.gz: 74b2370bb501a69ef6546840d3ed0b61315e13ef35f584d5dbe3f2f38725cbb2
5
+ SHA512:
6
+ metadata.gz: 7133ab852f61abbcb0e93bbc1be555bcfe16dfab0751ff1026be21dc8a46d829b3d03ba94313a7a196a0f385b7dbe01a6cb16d4678337bb063662e3faac88b43
7
+ data.tar.gz: a9851b753e8bf686a1a5bccc5dc392dc229822e51e47a3b1b82c8f1f76dbaf8629c04ecc825679aee250ff1225a7cf9ab6ed2f774f61e927f32fcb58ba83e7ee
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ ## Unreleased
4
+
5
+ ## [0.0.1] - 2024-06-27
6
+
7
+ ### Fixed
8
+ - fix first fixed.
9
+
10
+
11
+
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+ platforms :jruby do
5
+ gem "rake-compiler", ">= 1.2.5"
6
+ end
7
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 siranovel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake/javaextensiontask'
2
+
3
+ jars = Dir.glob("lib/*.jar")
4
+ Rake::JavaExtensionTask.new(name='num4regana') do | ext |
5
+ ext.release = '11'
6
+ ext.classpath = jars.map { |x| File.expand_path x }.join ":"
7
+ end
8
+ task :default => [:compile]
9
+
10
+
@@ -0,0 +1,75 @@
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
+
@@ -0,0 +1,77 @@
1
+ import org.apache.commons.math3.stat.regression.SimpleRegression;
2
+
3
+ public class SmplRegAna {
4
+ private static SmplRegAna regana = new SmplRegAna();
5
+ public static SmplRegAna getInstance() {
6
+ return regana;
7
+ }
8
+ public LineReg lineRegAna(double[] yi, double xi[]) {
9
+ LineRegAna line = new LineRegAna();
10
+
11
+ return line.lineRegAna(yi, xi);
12
+ }
13
+ public double getR2(double[] yi, double xi[]) {
14
+ LineRegAna line = new LineRegAna();
15
+
16
+ return line.getR2(yi, xi);
17
+ }
18
+ public double getR(double[] yi, double xi[]) {
19
+ LineRegAna line = new LineRegAna();
20
+
21
+ return line.getR(yi, xi);
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 = 0.0;
32
+ public LineReg(double a, double b) {
33
+ this.a = a;
34
+ this.b = b;
35
+ }
36
+ public double getIntercept() {
37
+ return a;
38
+ }
39
+ public double getSlope() {
40
+ return b;
41
+ }
42
+ }
43
+ private class LineRegAna {
44
+ // 単回帰の分析
45
+ public LineReg lineRegAna(double[] yi, double xi[]) {
46
+ SimpleRegression simpleReg = new SimpleRegression(true);
47
+
48
+ for(int i = 0; i < yi.length; i++) {
49
+ simpleReg.addData(xi[i], yi[i]);
50
+ }
51
+ LineReg ret = new LineReg(simpleReg.getIntercept(), simpleReg.getSlope());
52
+
53
+ return ret;
54
+ }
55
+ // 決定係数取得
56
+ public double getR2(double[] yi, double xi[]) {
57
+ SimpleRegression simpleReg = new SimpleRegression(true);
58
+
59
+ for(int i = 0; i < yi.length; i++) {
60
+ simpleReg.addData(xi[i], yi[i]);
61
+ }
62
+
63
+ return simpleReg.getRSquare();
64
+ }
65
+ // 相関係数取得
66
+ public double getR(double[] yi, double xi[]) {
67
+ SimpleRegression simpleReg = new SimpleRegression(true);
68
+
69
+ for(int i = 0; i < yi.length; i++) {
70
+ simpleReg.addData(xi[i], yi[i]);
71
+ }
72
+
73
+ return simpleReg.getR();
74
+ }
75
+ }
76
+ }
77
+
Binary file
data/lib/num4regana.rb ADDED
@@ -0,0 +1,165 @@
1
+ require 'java'
2
+ require 'num4regana.jar'
3
+ require 'commons-math3-3.6.1.jar'
4
+
5
+ java_import 'SmplRegAna'
6
+ java_import 'OLSMultRegAna'
7
+ # 回帰分析
8
+ # (Apache commoms math3使用)
9
+ module Num4RegAnaLib
10
+ # 単回帰分析
11
+ class SmplRegAnaLib
12
+ def initialize
13
+ @regana = SmplRegAna.getInstance()
14
+ end
15
+ # 単回帰分析
16
+ #
17
+ # @overload line_reg_ana(yi, xi)
18
+ # @param [Array] yi yの値(double[])
19
+ # @param [Array] xi xの値(double[])
20
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
21
+ # @example
22
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
23
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
24
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
25
+ # regana.line_reg_ana(yi, xi)
26
+ # =>
27
+ # {
28
+ # "intercept": 99.075, # 定数項
29
+ # "slope": 2.145, # 回帰係数
30
+ # }
31
+ def line_reg_ana(yi, xi)
32
+ ret = @regana.lineRegAna(yi.to_java(Java::double), xi.to_java(Java::double))
33
+ retRb = {
34
+ "intercept": ret.getIntercept(), # 定数項
35
+ "slope": ret.getSlope(), # 回帰係数
36
+ }
37
+ return retRb
38
+ end
39
+ # 決定係数
40
+ #
41
+ # @overload getr2(yi, xi)
42
+ # @param [Array] yi yの値(double[])
43
+ # @param [Array] xi xの値(double[])
44
+ # @return [double] 決定係数
45
+ # @example
46
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
47
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
48
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
49
+ # regana.getr2(yi, xi)
50
+ # => 0.893
51
+ def getr2(yi, xi)
52
+ return @regana.getR2(yi.to_java(Java::double), xi.to_java(Java::double))
53
+ end
54
+ # 相関係数
55
+ #
56
+ # @overload getr(yi, xi)
57
+ # @param [Array] yi yの値(double[])
58
+ # @param [Array] xi xの値(double[])
59
+ # @return [double] 決定係数
60
+ # @example
61
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
62
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
63
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
64
+ # regana.getr(yi, xi)
65
+ # => 0.945
66
+ def getr(yi, xi)
67
+ return @regana.getR(yi.to_java(Java::double), xi.to_java(Java::double))
68
+ end
69
+ end
70
+ # 重回帰分析(最小2乗法)
71
+ class OLSMultRegAnaLib
72
+ def initialize
73
+ @regana = OLSMultRegAna.getInstance()
74
+ end
75
+ # 重回帰分析
76
+ #
77
+ # @overload line_reg_ana(yi, xij)
78
+ # @param [Array] yi yの値(double[])
79
+ # @param [Array] xij xの値(double[][])
80
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
81
+ # @example
82
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
83
+ # olsxij = [
84
+ # [17.5, 30],
85
+ # [17.0, 25],
86
+ # [18.5, 20],
87
+ # [16.0, 30],
88
+ # [19.0, 45],
89
+ # [19.5, 35],
90
+ # [16.0, 25],
91
+ # [18.0, 35],
92
+ # [19.0, 35],
93
+ # [19.5, 40],
94
+ # ]
95
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
96
+ # regana.line_reg_ana(olsyi, olsxij)
97
+ # =>
98
+ # {
99
+ # "intercept": -34.71, # 定数項
100
+ # "slope": [3.47, 0.53], # 回帰係数
101
+ # }
102
+ def line_reg_ana(yi, xij)
103
+ ret = @regana.lineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
104
+ retRb = {
105
+ "intercept": ret.getIntercept(), # 定数項
106
+ "slope": ret.getSlope().to_a, # 回帰係数
107
+ }
108
+ return retRb
109
+ end
110
+ # 決定係数
111
+ #
112
+ # @overload getr2(yi, xij)
113
+ # @param [Array] yi yの値(double[])
114
+ # @param [Array] xij xの値(double[][])
115
+ # @return [double] 決定係数
116
+ # @example
117
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
118
+ # olsxij = [
119
+ # [17.5, 30],
120
+ # [17.0, 25],
121
+ # [18.5, 20],
122
+ # [16.0, 30],
123
+ # [19.0, 45],
124
+ # [19.5, 35],
125
+ # [16.0, 25],
126
+ # [18.0, 35],
127
+ # [19.0, 35],
128
+ # [19.5, 40],
129
+ # ]
130
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
131
+ # regana.getr2(yi, xi)
132
+ # => 0.858
133
+ def getr2(yi, xij)
134
+ return @regana.getR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
135
+ end
136
+ # 自由度調整済み決定係数
137
+ #
138
+ # @overload getadjr2(yi, xij)
139
+ # @param [Array] yi yの値(double[])
140
+ # @param [Array] xij xの値(double[][])
141
+ # @return [double] 決定係数
142
+ # @example
143
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
144
+ # olsxij = [
145
+ # [17.5, 30],
146
+ # [17.0, 25],
147
+ # [18.5, 20],
148
+ # [16.0, 30],
149
+ # [19.0, 45],
150
+ # [19.5, 35],
151
+ # [16.0, 25],
152
+ # [18.0, 35],
153
+ # [19.0, 35],
154
+ # [19.5, 40],
155
+ # ]
156
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
157
+ # regana.getadjr2(yi, xij)
158
+ # => 0.8176
159
+ def getadjr2(yi, xij)
160
+ return @regana.getAdjR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
161
+ end
162
+
163
+ end
164
+ end
165
+
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: num4regana
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: java
6
+ authors:
7
+ - siranovel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 12.3.3
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '12.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 12.3.3
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake-compiler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.2'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.2.5
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.2'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.2.5
53
+ description: numerical solution for regression analysis.
54
+ email: siranovel@gmail.com
55
+ executables: []
56
+ extensions:
57
+ - Rakefile
58
+ extra_rdoc_files: []
59
+ files:
60
+ - CHANGELOG.md
61
+ - Gemfile
62
+ - LICENSE
63
+ - Rakefile
64
+ - ext/num4regana/OLSMultRegAna.java
65
+ - ext/num4regana/SmplRegAna.java
66
+ - lib/commons-math3-3.6.1.jar
67
+ - lib/num4regana.rb
68
+ homepage: http://github.com/siranovel/num4regana
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.3.7
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: num for regression analysis
91
+ test_files: []