num4tststatistic 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: a8b0c9145ecc7379e5619ad7f8ffb33b61d5560bf92c594e42f53e5c0e288a1b
4
+ data.tar.gz: e8b836c2974645183f78e3e9c6533b105b3d364f2405c10c68ab18372f06e685
5
+ SHA512:
6
+ metadata.gz: 20fa87418cb04465c0c62d33dd64016e67a57ad9f0c0dccddf2572b8f6aa6618ddbc76ff6111c12baa414103e24cc8dc74abcec87dfac38768e28a3ae80233f5
7
+ data.tar.gz: a01a432d8a7530b3be8f568e047faed5a42d9d79dde68f60426bcda8a751c166212e772495bd74e7b4277d3e7045e9443a5993c19a5af36b451306486dce528f
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ ## Unreleased
4
+
5
+ ## [0.0.1] - 2023-11-11
6
+
7
+ ### Fixed
8
+ - fix first fixed.
9
+
10
+
11
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 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,7 @@
1
+ require 'rake/javaextensiontask'
2
+
3
+ Rake::JavaExtensionTask.new(name='num4tststatistic') do | ext |
4
+ ext.release = '11'
5
+ ext.classpath = 'lib/commons-math3-3.6.1.jar'
6
+ end
7
+ task :default => [:compile]
@@ -0,0 +1,135 @@
1
+ import java.util.Arrays;
2
+
3
+ import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
4
+ import org.apache.commons.math3.stat.correlation.PearsonsCorrelation;
5
+
6
+ public class TstStatistic {
7
+ public static double populationMean(double[] xi, double m0) {
8
+ int n = xi.length;
9
+ SummaryStatistics stat = new SummaryStatistics();
10
+
11
+ Arrays.stream(xi).forEach(stat::addValue);
12
+ double m = stat.getMean(); // 平均
13
+ double s2 = stat.getVariance();// 分散
14
+ return (m - m0) / Math.sqrt(s2 / n);
15
+ }
16
+ public static double populationVar(double[] xi, double sig0) {
17
+ int n = xi.length;
18
+ SummaryStatistics stat = new SummaryStatistics();
19
+ Arrays.stream(xi).forEach(stat::addValue);
20
+ double s2 = stat.getVariance();// 分散
21
+
22
+ return (n - 1) * s2 / sig0;
23
+ }
24
+ public static double populationRatio(int m, int n, double p0) {
25
+ double p = (double)m / (double)n;
26
+
27
+ return (p - p0) / Math.sqrt(p0 * (1-p0) / n);
28
+ }
29
+ public static double diffPopulationMean2EquVar(double[] xi1, double[] xi2) {
30
+ int n1 = xi1.length;
31
+ int n2 = xi2.length;
32
+ SummaryStatistics stat1 = new SummaryStatistics();
33
+ SummaryStatistics stat2 = new SummaryStatistics();
34
+ Arrays.stream(xi1).forEach(stat1::addValue);
35
+ Arrays.stream(xi2).forEach(stat2::addValue);
36
+
37
+ double m1 = stat1.getMean();
38
+ double m2 = stat2.getMean();
39
+ double s12 = stat1.getVariance();// 分散
40
+ double s22 = stat2.getVariance();// 分散
41
+ double s2 = ((n1 - 1) * s12 + (n2 - 1) * s22) / (n1 + n2 - 2);
42
+
43
+ return (m1 - m2) / Math.sqrt((1.0 / n1 + 1.0 / n2) * s2);
44
+ }
45
+ public static double diffPopulationMean2UnEquVar(double[] xi1, double[] xi2) {
46
+ int n1 = xi1.length;
47
+ int n2 = xi2.length;
48
+ SummaryStatistics stat1 = new SummaryStatistics();
49
+ SummaryStatistics stat2 = new SummaryStatistics();
50
+ Arrays.stream(xi1).forEach(stat1::addValue);
51
+ Arrays.stream(xi2).forEach(stat2::addValue);
52
+
53
+ double m1 = stat1.getMean();
54
+ double m2 = stat2.getMean();
55
+ double s12 = stat1.getVariance();// 分散
56
+ double s22 = stat2.getVariance();// 分散
57
+
58
+ return (m1 - m2) / Math.sqrt(s12 / n1 + s22 / n2);
59
+ }
60
+ public static int df4welch(double[] xi1, double[] xi2) {
61
+ int n1 = xi1.length;
62
+ int n2 = xi2.length;
63
+ SummaryStatistics stat1 = new SummaryStatistics();
64
+ SummaryStatistics stat2 = new SummaryStatistics();
65
+ Arrays.stream(xi1).forEach(stat1::addValue);
66
+ Arrays.stream(xi2).forEach(stat2::addValue);
67
+
68
+ double s12 = stat1.getVariance();// 分散
69
+ double s22 = stat2.getVariance();// 分散
70
+ double s14 = s12 * s12;
71
+ double s24 = s22 * s22;
72
+ int n12 = n1 * n1;
73
+ int n22 = n2 * n2;
74
+ double ns = (s12 / n1) + (s22 / n2);
75
+
76
+ return (int)
77
+ (
78
+ (ns * ns) /
79
+ (
80
+ s14 / (n12 * (n1 - 1)) + s24 / (n22 * (n2 - 1))
81
+ )
82
+ );
83
+ }
84
+ public static double diffPopulationMean(double[] xi1, double[] xi2) {
85
+ int n = xi1.length;
86
+ SummaryStatistics stat = new SummaryStatistics();
87
+
88
+ for(int i = 0; i < n; i++) {
89
+ stat.addValue(xi1[i] - xi2[i]);
90
+ }
91
+ double m = stat.getMean();
92
+ double s2 = stat.getVariance();// 分散
93
+
94
+ return (m - 0) / Math.sqrt(s2/n);
95
+ }
96
+ public static double diffPopulationVar(double[] xi1, double[] xi2) {
97
+ SummaryStatistics stat1 = new SummaryStatistics();
98
+ SummaryStatistics stat2 = new SummaryStatistics();
99
+ Arrays.stream(xi1).forEach(stat1::addValue);
100
+ Arrays.stream(xi2).forEach(stat2::addValue);
101
+
102
+ double s12 = stat1.getVariance();// 分散
103
+ double s22 = stat2.getVariance();// 分散
104
+ return s12 / s22;
105
+ }
106
+ public static double diffPopulationRatio(int m1, int n1, int m2, int n2) {
107
+ double p1 = (double)m1 / (double)n1;
108
+ double p2 = (double)m2 / (double)n2;
109
+ double p = (double)(m1 + m2) / (double)(n1 + n2);
110
+
111
+ return (p1 - p2) / Math.sqrt(p * (1 - p) * (1.0 / n1 + 1.0 / n2));
112
+ }
113
+ public static double unCorrelation(double[] x, double[] y) {
114
+ int n = x.length;
115
+ PearsonsCorrelation corel = new PearsonsCorrelation();
116
+
117
+ double r = corel.correlation(x, y);
118
+
119
+ return r * Math.sqrt(n - 2.0) / Math.sqrt(1.0 - r * r);
120
+ }
121
+ public static double populationCorre(double[] x, double[] y, double rth0) {
122
+ int n = x.length;
123
+ PearsonsCorrelation corel = new PearsonsCorrelation();
124
+
125
+ double r = corel.correlation(x, y);
126
+
127
+ return Math.sqrt(n-3.0) *
128
+ (
129
+ 0.5 * Math.log((1.0 + r) / (1.0 - r))
130
+ - 0.5 * Math.log((1.0 + rth0) / (1.0 - rth0))
131
+ );
132
+ }
133
+ }
134
+
135
+
Binary file
@@ -0,0 +1,200 @@
1
+ require 'java'
2
+ require 'num4tststatistic.jar'
3
+ require 'commons-math3-3.6.1.jar'
4
+
5
+ java_import 'TstStatistic'
6
+
7
+ # 検定統計量を計算
8
+ # (Apache commoms math3使用)
9
+ module Num4TstStatisticLib
10
+ class << self
11
+ # 正規母集団の母平均の検定量
12
+ #
13
+ # @overload populationMean(xi, m0)
14
+ # @param [Array] xi データ(double[])
15
+ # @param [double] m0 母平均
16
+ # @return [double] 検定統計量
17
+ # @example
18
+ # xi = [15.5, 15.7, 15.4, 15.4, 15.6, 15.4, 15.6, 15.5, 15.4]
19
+ # Num4TstStatisticLib.populationMean(xi, 15.4)
20
+ # => 2.683
21
+ # @note
22
+ # 自由度(N-1)のt分布に従う
23
+ def populationMean(xi, m0)
24
+ return TstStatistic.populationMean(xi.to_java(Java::double), m0)
25
+ end
26
+ # 正規母集団の母分散の検定量
27
+ #
28
+ # @overload populationVar(xi, sig0)
29
+ # @param [Array] xi データ(double[])
30
+ # @param [double] sig0 母分散
31
+ # @return [double] 検定統計量
32
+ # @example
33
+ # xi = xi = [35.2, 34.5, 34.9, 35.2, 34.8, 35.1, 34.9, 35.2, 34.9, 34.8]
34
+ # sd = 0.4
35
+ # Num4TstStatisticLib.populationVar(xi, sd*sd)
36
+ # => 2.906
37
+ # @note
38
+ # 自由度(N-1)の階2乗分布に従う
39
+ def populationVar(xi, sig0)
40
+ return TstStatistic.populationVar(xi.to_java(Java::double), sig0)
41
+ end
42
+ # 母比率の検定量
43
+ # @overload populationRatio(m, n, p0)
44
+ # @param [int] m m値
45
+ # @param [int] n N値
46
+ # @param [double] p0 母比率
47
+ # @return [double] 検定統計量
48
+ # @example
49
+ # Num4TstStatisticLib.populationRatio(29, 346, 0.12)
50
+ # => -2.071
51
+ # @note
52
+ # 標準正規分布 N(0,1*1)に従う
53
+ def populationRatio(m, n, p0)
54
+ return TstStatistic.populationRatio(m, n, p0)
55
+ end
56
+ # 2つの母平均の差の検定量
57
+ # (等分散性を仮定)
58
+ #
59
+ # @overload diffPopulationMean2EquVar(xi1, xi2)
60
+ # @param [Array] xi1 x1のデータ(double[])
61
+ # @param [Array] xi2 x2のデータ)double[])
62
+ # @return [double] 検定統計量
63
+ # @example
64
+ # xi1 = [165, 130, 182, 178, 194, 206, 160, 122, 212, 165, 247, 195]
65
+ # xi2 = [180, 180, 235, 270, 240, 285, 164, 152]
66
+ # Num4TstStatisticLib.diffPopulationMean2EquVar(xi1, xi2)
67
+ # => -1.765
68
+ # @note
69
+ # N1+N2-2のt分布に従う
70
+ def diffPopulationMean2EquVar(xi1, xi2)
71
+ return TstStatistic.diffPopulationMean2EquVar(
72
+ xi1.to_java(Java::double), xi2.to_java(Java::double)
73
+ )
74
+ end
75
+ # 2つの母平均の差の検定量
76
+ # (不等分散性を仮定)
77
+ #
78
+ # @overload diffPopulationMean2UnEquVar(xi1, xi2)
79
+ # @param [Array] xi1 x1のデータ(double[])
80
+ # @param [Array] xi2 x2のデータ)double[])
81
+ # @return [double] 検定統計量
82
+ # @example
83
+ # xi1 = [165, 130, 182, 178, 194, 206, 160, 122, 212, 165, 247, 195]
84
+ # xi2 = [180, 180, 235, 270, 240, 285, 164, 152]
85
+ # Num4TstStatisticLib.diffPopulationMean2UnEquVar(xi1, xi2)
86
+ # => -1.636
87
+ # @note
88
+ # df4welch関数で求めた自由度のt分布に従う
89
+ def diffPopulationMean2UnEquVar(xi1, xi2)
90
+ return TstStatistic.diffPopulationMean2UnEquVar(
91
+ xi1.to_java(Java::double), xi2.to_java(Java::double)
92
+ )
93
+ end
94
+ # ウェルチ検定の為の自由度
95
+ # @overload df4welch(xi1, xi2)
96
+ # @param [Array] xi1 x1のデータ(double[])
97
+ # @param [Array] xi2 x2のデータ)double[])
98
+ # @return [int] 自由度
99
+ # @example
100
+ # xi1 = [165, 130, 182, 178, 194, 206, 160, 122, 212, 165, 247, 195]
101
+ # xi2 = [180, 180, 235, 270, 240, 285, 164, 152]
102
+ # Num4TstStatisticLib.df4welch(xi1, xi2)
103
+ # => 11
104
+ def df4welch(xi1, xi2)
105
+ return TstStatistic.df4welch(
106
+ xi1.to_java(Java::double), xi2.to_java(Java::double)
107
+ )
108
+ end
109
+ # 対応のある2つの母平均の差の検定量
110
+ #
111
+ # @overload diffPopulationMean(xi1, xi2)
112
+ # @param [Array] xi1 x1のデータ(double[])
113
+ # @param [Array] xi2 x2のデータ)double[])
114
+ # @return [double] 検定統計量
115
+ # @example
116
+ # xi1 = [37.1, 36.2, 36.6, 37.4, 36.8, 36.7, 36.9, 37.4, 36.6, 36.7]
117
+ # xi2 = [36.8, 36.6, 36.5, 37.0, 36.0, 36.5, 36.6, 37.1, 36.4, 36.7]
118
+ # Num4TstStatisticLib.diffPopulationMean(xi1, xi2)
119
+ # => 2.283
120
+ # @note
121
+ # 自由度(N-1)のt分布に従う
122
+ def diffPopulationMean(xi1, xi2)
123
+ return TstStatistic.diffPopulationMean(
124
+ xi1.to_java(Java::double), xi2.to_java(Java::double)
125
+ )
126
+ end
127
+ # 2つの母分散の差の検定量
128
+ #
129
+ # @oerload diffPopulationVar(xi1, xi2)
130
+ # @param [Array] xi1 x1のデータ(double[])
131
+ # @param [Array] xi2 x2のデータ)double[])
132
+ # @return [double] 検定統計量
133
+ # @example
134
+ # xi1 = [165, 130, 182, 178, 194, 206, 160, 122, 212, 165, 247, 195]
135
+ # xi2 = [180, 180, 235, 270, 240, 285, 164, 152]
136
+ # Num4TstStatisticLib.diffPopulationVar(xi1, xi2)
137
+ # => 0.4727
138
+ # @note
139
+ # 自由度(N1-1,N2-1)のF分布に従う
140
+ def diffPopulationVar(xi1, xi2)
141
+ return TstStatistic.diffPopulationVar(
142
+ xi1.to_java(Java::double), xi2.to_java(Java::double)
143
+ )
144
+ end
145
+ # 2つの母比率の差の検定量
146
+ #
147
+ # @overload diffPopulationRatio(m1, n1, m2, n2)
148
+ # @param [int] m1 m1値
149
+ # @param [int] n1 N1値
150
+ # @param [int] m2 m2値
151
+ # @param [int] n2 N2値
152
+ # @return [double] 検定統計量
153
+ # @example
154
+ # Num4TstStatisticLib.diffPopulationRatio(469, 1200, 308, 900)
155
+ # => 2.283
156
+ # @note
157
+ # 標準正規分布 N(0,1*1)に従う
158
+ def diffPopulationRatio(m1, n1, m2, n2)
159
+ return TstStatistic.diffPopulationRatio(m1, n1, m2, n2)
160
+ end
161
+ # 無相関の検定量
162
+ #
163
+ # @overload unCorrelation(x, y)
164
+ # @param [Array] x xのデータ(double[])
165
+ # @param [Array] y yのデータ)double[])
166
+ # @return [double] 検定統計量
167
+ # @example
168
+ # x = [113, 64, 16, 45, 28, 19, 30, 82, 76]
169
+ # y = [31, 5, 2, 17, 18, 2, 9, 25, 13]
170
+ # Num4TstStatisticLib.unCorrelation(x, y)
171
+ # => 3.1035
172
+ # @note
173
+ # 自由度(N-2)t分布に従う
174
+ def unCorrelation(x, y)
175
+ return TstStatistic.unCorrelation(
176
+ x.to_java(Java::double), y.to_java(Java::double)
177
+ )
178
+ end
179
+ # 母相関係数の検定量
180
+ #
181
+ # @overload populationCorre(x, y, rth0)
182
+ # @param [Array] x xのデータ(double[])
183
+ # @param [Array] y yのデータ)double[])
184
+ # @param [double] rth0 母相関係数
185
+ # @return [double] 検定統計量
186
+ # @example
187
+ # x = [2750, 2956, 2675, 3198, 1816, 2233, 2375, 2288, 1932, 2036, 2183, 2882]
188
+ # y = [249, 713, 1136, 575, 5654, 2107, 915, 4193, 7225, 3730, 472, 291]
189
+ # Num4TstStatisticLib.populationCorre(x, y, -0.3)
190
+ # => -2.107168
191
+ # @note
192
+ # 標準正規分布 N(0,1*1)に従う
193
+ def populationCorre(x, y, rth0)
194
+ return TstStatistic.populationCorre(
195
+ x.to_java(Java::double), y.to_java(Java::double), rth0
196
+ )
197
+ end
198
+ end
199
+ end
200
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: num4tststatistic
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: 2023-11-11 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 test statistic
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/num4tststatistic/TstStatistic.java
65
+ - lib/commons-math3-3.6.1.jar
66
+ - lib/num4tststatistic.rb
67
+ homepage: http://github.com/siranovel/num4tststatistic
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.3.7
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: num for test statistic!
90
+ test_files: []