statsample-glm 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +51 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +2 -9
  5. data/Gemfile +2 -20
  6. data/LICENSE.txt +1 -1
  7. data/README.rdoc +14 -11
  8. data/Rakefile +16 -24
  9. data/lib/statsample-glm.rb +1 -11
  10. data/lib/statsample-glm/{regression.rb → glm.rb} +13 -46
  11. data/lib/statsample-glm/glm/base.rb +99 -0
  12. data/lib/statsample-glm/glm/irls/base.rb +54 -0
  13. data/lib/statsample-glm/glm/irls/logistic.rb +46 -0
  14. data/lib/statsample-glm/glm/irls/poisson.rb +48 -0
  15. data/lib/statsample-glm/glm/logistic.rb +16 -0
  16. data/lib/statsample-glm/glm/mle/base.rb +157 -0
  17. data/lib/statsample-glm/glm/mle/logistic.rb +113 -0
  18. data/lib/statsample-glm/glm/mle/normal.rb +94 -0
  19. data/lib/statsample-glm/glm/mle/probit.rb +100 -0
  20. data/lib/statsample-glm/glm/normal.rb +17 -0
  21. data/lib/statsample-glm/glm/poisson.rb +17 -0
  22. data/lib/statsample-glm/glm/probit.rb +16 -0
  23. data/lib/statsample-glm/version.rb +5 -0
  24. data/spec/data/logistic.csv +51 -0
  25. data/spec/data/logistic_mle.csv +201 -0
  26. data/spec/data/normal.csv +30 -0
  27. data/spec/logistic_spec.rb +37 -0
  28. data/spec/normal_spec.rb +15 -0
  29. data/spec/poisson_spec.rb +32 -0
  30. data/spec/probit_spec.rb +19 -0
  31. data/spec/spec_helper.rb +50 -0
  32. data/statsample-glm.gemspec +35 -0
  33. metadata +71 -145
  34. data/VERSION +0 -1
  35. data/features/bio-statsample-glm.feature +0 -9
  36. data/features/step_definitions/bio-statsample-glm_steps.rb +0 -0
  37. data/features/support/env.rb +0 -15
  38. data/lib/statsample-glm/regression/logistic.rb +0 -108
  39. data/lib/statsample-glm/regression/poisson.rb +0 -90
  40. data/test/helper.rb +0 -87
  41. data/test/test_glm.rb +0 -4
  42. data/test/test_glm_logistic.rb +0 -23
  43. data/test/test_glm_poisson.rb +0 -25
@@ -0,0 +1,15 @@
1
+ describe Statsample::GLM::Normal do
2
+ context "MLE algorithm", focus: true do
3
+ before do
4
+ # Below data set taken from http://dl.dropbox.com/u/10246536/Web/RTutorialSeries/dataset_multipleRegression.csv
5
+ @ds = Statsample::CSV.read "spec/data/normal.csv"
6
+ end
7
+
8
+ it "reports correct values as an array" do
9
+ @glm = Statsample::GLM.compute @ds, 'roll', :normal, {algorithm: :mle}
10
+
11
+ expect_similar_vector @glm.coefficients, [450.12450365911894,
12
+ 0.4064837278023981, 4.27485769721736,-9153.254462671905]
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ describe Statsample::GLM::Poisson do
2
+ context "IRLS algorithm" do
3
+ before :each do
4
+ x1 = Statsample::Vector.new([0.537322309644812,-0.717124209978434,-0.519166718891331,0.434970973986765,-0.761822002215759,1.51170030921189,0.883854199811195,-0.908689798854196,1.70331977539793,-0.246971150634099,-1.59077593922623,-0.721548040910253,0.467025703920194,-0.510132788447137,0.430106510266798,-0.144353683251536,-1.54943800728303,0.849307651309298,-0.640304240933579,1.31462478279425,-0.399783455165345,0.0453055645017902,-2.58212161987746,-1.16484414309359,-1.08829266466281,-0.243893919684792,-1.96655661929441,0.301335373291024,-0.665832694463588,-0.0120650855753837,1.5116066367604,0.557300353673344,1.12829931872045,0.234443748015922,-2.03486690662651,0.275544751380246,-0.231465849558696,-0.356880153225012,-0.57746647541923,1.35758352580655,1.23971669378224,-0.662466275100489,0.313263561921793,-1.08783223256362,1.41964722846899,1.29325100940785,0.72153880625103,0.440580131022748,0.0351917814720056, -0.142353224879252],:scale)
5
+ x2 = Statsample::Vector.new([-0.866655707911859,-0.367820249977585,0.361486610435,0.857332626245179,0.133438466268095,0.716104533073575,1.77206093023382,-0.10136697295802,-0.777086491435508,-0.204573554913706,0.963353531412233,-1.10103024900542,-0.404372761837392,-0.230226345183469,0.0363730246866971,-0.838265540390497,1.12543549657924,-0.57929175648001,-0.747060244805248,0.58946979365152,-0.531952663697324,1.53338594419818,0.521992029051441,1.41631763288724,0.611402316795129,-0.518355638373296,-0.515192557101107,-0.672697937866108,1.84347042325327,-0.21195540664804,-0.269869371631611,0.296155694010096,-2.18097898069634,-1.21314663927206,1.49193669881581,1.38969280369493,-0.400680808117106,-1.87282814976479,1.82394870451051,0.637864732838274,-0.141155946382493,0.0699950644281617,1.32568550595165,-0.412599258349398,0.14436832227506,-1.16507785388489,-2.16782049922428,0.24318371493798,0.258954871320764,-0.151966534521183],:scale)
6
+ @y_pois = Statsample::Vector.new([1,2,1,3,3,1,10,1,1,2,15,0,0,2,1,2,18,2,1,1,1,8,18,13,7,1,1,0,26,0,2,2,0,0,25,7,0,0,21,0,0,1,5,0,3,0,0,1,0,0],:scale)
7
+
8
+ @df = Statsample::Dataset.new({"x1"=>x1,"x2"=>x2, "y" => @y_pois})
9
+
10
+ end
11
+
12
+ it "reports return values as arrays for IRLS" do
13
+ @glm = Statsample::GLM.compute @df,:y,:poisson,
14
+ {algorithm: :irls, constant: 1}
15
+
16
+ expect_similar_vector(@glm.coefficients,[-0.586359358356708,
17
+ 1.28511323439258,0.32993246633711])
18
+ end
19
+
20
+ it "reports return values as a hash for IRLS" do
21
+ @glm = Statsample::GLM.compute @df,:y,:poisson,
22
+ {algorithm: :irls, constant: 1}
23
+
24
+ expect_similar_hash(@glm.coefficients(:hash), {:constant => 0.32993246633711,
25
+ :x1 => -0.586359358356708, :x2 => 1.28511323439258})
26
+ end
27
+ end
28
+
29
+ context "MLE algorithm" do
30
+ # TODO: Implement MLE for poisson
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ describe Statsample::GLM::Probit do
2
+ context "IRLS algorithm" do
3
+ # TODO : Implement this!
4
+ end
5
+
6
+ context "MLE algorithm" do
7
+ before do
8
+ @data_set = Statsample::CSV.read 'spec/data/logistic_mle.csv'
9
+ @glm = Statsample::GLM.compute @data_set, :y, :probit,
10
+ {algorithm: :mle, constant: 1}
11
+ end
12
+
13
+ it "reports correct values as an array" do
14
+ expect_similar_vector(@glm.coefficients,[0.1763,0.4483,-0.2240,-3.0670],0.001)
15
+
16
+ expect(@glm.log_likelihood).to be_within(0.0001).of(-38.31559)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'rspec'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'statsample-glm'
16
+
17
+ def expect_similar_vector(exp, obs, delta=1e-10,msg=nil)
18
+ expect(exp.size).to eq(obs.size)
19
+
20
+ exp.data_with_nils.each_with_index do |v,i|
21
+ expect(v).to be_within(delta).of(obs[i])
22
+ end
23
+ end
24
+
25
+ def expect_similar_hash(exp, obs, delta=1e-10,msg=nil)
26
+ expect(exp.size).to eq(obs.size)
27
+
28
+ exp.each_key do |k|
29
+ expect(exp[k]).to be_within(delta).of(obs[k])
30
+ end
31
+ end
32
+
33
+ def expect_equal_vector(exp,obs,delta=1e-10,msg=nil)
34
+ expect(exp.size).to eq(obs.size)
35
+
36
+ exp.size.times do |i|
37
+ expect(exp[i]).to be_within(delta).of(obs[i])
38
+ end
39
+ end
40
+
41
+ def expect_equal_matrix(exp,obs,delta=1e-10,msg=nil)
42
+ expect(exp.row_size).to eq(obs.row_size)
43
+ expect(exp.column_size).to eq(obs.column_size)
44
+
45
+ exp.row_size.times do |i|
46
+ exp.column_size.times do |j|
47
+ expect(exp[i,j]).to be_within(delta).of(obs[i,j])
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ $:.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ require 'statsample-glm/version'
5
+
6
+ DESCRIPTION = <<MSG
7
+ Statsample-GLM is an extension to Statsample, an advance statistics suite in
8
+ Ruby. This gem includes modules for Regression techniques such as Poisson
9
+ and Logistic Regression using the IRLS algorithm and Logistic,
10
+ Probit and Normal Regression using the Newton Raphson algorithm.
11
+ MSG
12
+
13
+ Gem::Specification.new do |spec|
14
+ spec.name = 'statsample-glm'
15
+ spec.version = Statsample::GLM::VERSION
16
+ spec.authors = ['Ankur Goel', 'Sameer Deshmukh']
17
+ spec.email = ['sameer.deshmukh93@gmail.com']
18
+ spec.summary = %q{Generalized Linear Models for Statsample}
19
+ spec.description = DESCRIPTION
20
+ spec.homepage = "http://github.com/sciruby/statsample-glm"
21
+ spec.license = 'BSD-2'
22
+
23
+ spec.files = `git ls-files -z`.split("\x0")
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_runtime_dependency 'statsample', '~> 1.2'
29
+
30
+ spec.add_development_dependency 'bundler'
31
+ spec.add_development_dependency 'rake'
32
+ spec.add_development_dependency 'rspec'
33
+ spec.add_development_dependency 'awesome_print'
34
+ spec.add_development_dependency 'rdoc', '~> 3.12'
35
+ end
metadata CHANGED
@@ -1,164 +1,89 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsample-glm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ankur Goel
8
+ - Sameer Deshmukh
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-25 00:00:00.000000000 Z
12
+ date: 2014-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: statsample
16
16
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
17
  requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: 1.2.0
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: 1.2.0
30
- - !ruby/object:Gem::Dependency
31
- name: activesupport
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - '='
18
+ - - ~>
36
19
  - !ruby/object:Gem::Version
37
- version: 3.2.10
20
+ version: '1.2'
38
21
  type: :runtime
39
22
  prerelease: false
40
23
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
24
  requirements:
43
- - - '='
25
+ - - ~>
44
26
  - !ruby/object:Gem::Version
45
- version: 3.2.10
27
+ version: '1.2'
46
28
  - !ruby/object:Gem::Dependency
47
- name: shoulda
29
+ name: bundler
48
30
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
31
  requirements:
51
- - - ! '>='
32
+ - - '>='
52
33
  - !ruby/object:Gem::Version
53
34
  version: '0'
54
35
  type: :development
55
36
  prerelease: false
56
37
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
38
  requirements:
59
- - - ! '>='
39
+ - - '>='
60
40
  - !ruby/object:Gem::Version
61
41
  version: '0'
62
42
  - !ruby/object:Gem::Dependency
63
- name: rdoc
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ~>
68
- - !ruby/object:Gem::Version
69
- version: '3.12'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: '3.12'
78
- - !ruby/object:Gem::Dependency
79
- name: minitest
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ~>
84
- - !ruby/object:Gem::Version
85
- version: 4.7.5
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ~>
92
- - !ruby/object:Gem::Version
93
- version: 4.7.5
94
- - !ruby/object:Gem::Dependency
95
- name: cucumber
43
+ name: rake
96
44
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
45
  requirements:
99
- - - ! '>='
46
+ - - '>='
100
47
  - !ruby/object:Gem::Version
101
48
  version: '0'
102
49
  type: :development
103
50
  prerelease: false
104
51
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
52
  requirements:
107
- - - ! '>='
53
+ - - '>='
108
54
  - !ruby/object:Gem::Version
109
55
  version: '0'
110
56
  - !ruby/object:Gem::Dependency
111
- name: bundler
57
+ name: rspec
112
58
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
59
  requirements:
115
- - - ~>
60
+ - - '>='
116
61
  - !ruby/object:Gem::Version
117
- version: 1.3.5
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ~>
124
- - !ruby/object:Gem::Version
125
- version: 1.3.5
126
- - !ruby/object:Gem::Dependency
127
- name: jeweler
128
- requirement: !ruby/object:Gem::Requirement
129
- none: false
130
- requirements:
131
- - - ~>
132
- - !ruby/object:Gem::Version
133
- version: 1.8.4
62
+ version: '0'
134
63
  type: :development
135
64
  prerelease: false
136
65
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
66
  requirements:
139
- - - ~>
67
+ - - '>='
140
68
  - !ruby/object:Gem::Version
141
- version: 1.8.4
69
+ version: '0'
142
70
  - !ruby/object:Gem::Dependency
143
- name: bio
71
+ name: awesome_print
144
72
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
73
  requirements:
147
- - - ! '>='
74
+ - - '>='
148
75
  - !ruby/object:Gem::Version
149
- version: 1.4.2
76
+ version: '0'
150
77
  type: :development
151
78
  prerelease: false
152
79
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
80
  requirements:
155
- - - ! '>='
81
+ - - '>='
156
82
  - !ruby/object:Gem::Version
157
- version: 1.4.2
83
+ version: '0'
158
84
  - !ruby/object:Gem::Dependency
159
85
  name: rdoc
160
86
  requirement: !ruby/object:Gem::Requirement
161
- none: false
162
87
  requirements:
163
88
  - - ~>
164
89
  - !ruby/object:Gem::Version
@@ -166,81 +91,82 @@ dependencies:
166
91
  type: :development
167
92
  prerelease: false
168
93
  version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
94
  requirements:
171
95
  - - ~>
172
96
  - !ruby/object:Gem::Version
173
97
  version: '3.12'
174
- - !ruby/object:Gem::Dependency
175
- name: mocha
176
- requirement: !ruby/object:Gem::Requirement
177
- none: false
178
- requirements:
179
- - - ~>
180
- - !ruby/object:Gem::Version
181
- version: 0.14.0
182
- type: :development
183
- prerelease: false
184
- version_requirements: !ruby/object:Gem::Requirement
185
- none: false
186
- requirements:
187
- - - ~>
188
- - !ruby/object:Gem::Version
189
- version: 0.14.0
190
- description: Statsample-GLM is an extension to Statsample, an advance statistics suite
191
- in Ruby. This gem includes modules for Regression techniques such as Poisson Regression,
192
- Logistic Regression and Exponential Regression
193
- email: ankurgel@gmail.com
98
+ description: "Statsample-GLM is an extension to Statsample, an advance statistics
99
+ suite in\nRuby. This gem includes modules for Regression techniques such as Poisson
100
+ \nand Logistic Regression using the IRLS algorithm and Logistic, \nProbit and Normal
101
+ Regression using the Newton Raphson algorithm.\n"
102
+ email:
103
+ - sameer.deshmukh93@gmail.com
194
104
  executables: []
195
105
  extensions: []
196
- extra_rdoc_files:
197
- - LICENSE.txt
198
- - README.rdoc
106
+ extra_rdoc_files: []
199
107
  files:
200
108
  - .document
109
+ - .gitignore
110
+ - .rspec
201
111
  - .travis.yml
202
112
  - Gemfile
203
113
  - LICENSE.txt
204
114
  - README.rdoc
205
115
  - Rakefile
206
- - VERSION
207
- - features/bio-statsample-glm.feature
208
- - features/step_definitions/bio-statsample-glm_steps.rb
209
- - features/support/env.rb
210
116
  - lib/statsample-glm.rb
211
- - lib/statsample-glm/regression.rb
212
- - lib/statsample-glm/regression/logistic.rb
213
- - lib/statsample-glm/regression/poisson.rb
214
- - test/helper.rb
215
- - test/test_glm.rb
216
- - test/test_glm_logistic.rb
217
- - test/test_glm_poisson.rb
218
- homepage: http://github.com/AnkurGel/statsample-glm
117
+ - lib/statsample-glm/glm.rb
118
+ - lib/statsample-glm/glm/base.rb
119
+ - lib/statsample-glm/glm/irls/base.rb
120
+ - lib/statsample-glm/glm/irls/logistic.rb
121
+ - lib/statsample-glm/glm/irls/poisson.rb
122
+ - lib/statsample-glm/glm/logistic.rb
123
+ - lib/statsample-glm/glm/mle/base.rb
124
+ - lib/statsample-glm/glm/mle/logistic.rb
125
+ - lib/statsample-glm/glm/mle/normal.rb
126
+ - lib/statsample-glm/glm/mle/probit.rb
127
+ - lib/statsample-glm/glm/normal.rb
128
+ - lib/statsample-glm/glm/poisson.rb
129
+ - lib/statsample-glm/glm/probit.rb
130
+ - lib/statsample-glm/version.rb
131
+ - spec/data/logistic.csv
132
+ - spec/data/logistic_mle.csv
133
+ - spec/data/normal.csv
134
+ - spec/logistic_spec.rb
135
+ - spec/normal_spec.rb
136
+ - spec/poisson_spec.rb
137
+ - spec/probit_spec.rb
138
+ - spec/spec_helper.rb
139
+ - statsample-glm.gemspec
140
+ homepage: http://github.com/sciruby/statsample-glm
219
141
  licenses:
220
- - MIT
142
+ - BSD-2
143
+ metadata: {}
221
144
  post_install_message:
222
145
  rdoc_options: []
223
146
  require_paths:
224
147
  - lib
225
148
  required_ruby_version: !ruby/object:Gem::Requirement
226
- none: false
227
149
  requirements:
228
- - - ! '>='
150
+ - - '>='
229
151
  - !ruby/object:Gem::Version
230
152
  version: '0'
231
- segments:
232
- - 0
233
- hash: 891920669
234
153
  required_rubygems_version: !ruby/object:Gem::Requirement
235
- none: false
236
154
  requirements:
237
- - - ! '>='
155
+ - - '>='
238
156
  - !ruby/object:Gem::Version
239
157
  version: '0'
240
158
  requirements: []
241
159
  rubyforge_project:
242
- rubygems_version: 1.8.25
160
+ rubygems_version: 2.2.2
243
161
  signing_key:
244
- specification_version: 3
162
+ specification_version: 4
245
163
  summary: Generalized Linear Models for Statsample
246
- test_files: []
164
+ test_files:
165
+ - spec/data/logistic.csv
166
+ - spec/data/logistic_mle.csv
167
+ - spec/data/normal.csv
168
+ - spec/logistic_spec.rb
169
+ - spec/normal_spec.rb
170
+ - spec/poisson_spec.rb
171
+ - spec/probit_spec.rb
172
+ - spec/spec_helper.rb