scbi_math 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ === 0.0.1 2011-05-26
2
+
3
+ * Initial release
@@ -0,0 +1,14 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/scbi_math.rb
7
+ lib/scbi_math/array_extras.rb
8
+ lib/scbi_math/float_extras.rb
9
+ lib/scbi_math/scbi_narray.rb
10
+ script/console
11
+ script/destroy
12
+ script/generate
13
+ test/test_helper.rb
14
+ test/test_scbi_math.rb
@@ -0,0 +1,7 @@
1
+
2
+ For more information on scbi_math, see http://scbi_math.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
@@ -0,0 +1,62 @@
1
+ = scbi_math
2
+
3
+ * http://www.scbi.uma.es/downloads
4
+
5
+ == DESCRIPTION:
6
+
7
+ scbi_math is a ruby gem based on narray to do some additional statistics,
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Calculates MAD (Mean absolute deviation) based on mean
12
+ * Calculates MAD (Median absolute deviation) based on median
13
+ * Calculates a FAT mode using a window_size to find the mode that accumulates more frequency on the sample.
14
+
15
+ == SYNOPSIS:
16
+
17
+ # collect an array
18
+ my_array=(1..40).collect.to_a
19
+
20
+ # create a SCBINArray object
21
+ na=ScbiNArray.to_na(my_array)
22
+
23
+ puts na.mad_median
24
+
25
+ puts na.mad_mean
26
+
27
+ puts na.fat_mode
28
+
29
+ # There are also a set of statistics predefined by the narray class
30
+
31
+ == REQUIREMENTS:
32
+
33
+ * narray gem
34
+
35
+ == INSTALL:
36
+
37
+ * sudo gem install scbi_math
38
+
39
+ == LICENSE:
40
+
41
+ (The MIT License)
42
+
43
+ Copyright (c) 2011 Dario Guerrero
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ 'Software'), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
59
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
60
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
61
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
62
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/scbi_math'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'scbi_math' do
14
+ self.developer 'Dario Guerrero', 'dariogf@gmail.com'
15
+ # self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ self.extra_deps = [['narray']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ task :default => [:spec, :features, :redocs]
@@ -0,0 +1,13 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+
5
+ # require 'scbi_math/array_extras'
6
+ require 'scbi_math/scbi_narray'
7
+ # require 'scbi_math/float_extras'
8
+
9
+ # require 'scbi_math/scbi_stats'
10
+
11
+ module ScbiMath
12
+ VERSION = '0.0.1'
13
+ end
@@ -0,0 +1,11 @@
1
+ class Array
2
+
3
+ def sum
4
+ r=0
5
+ each do |e|
6
+ r+=e
7
+ end
8
+ return r
9
+ end
10
+
11
+ end
@@ -0,0 +1,9 @@
1
+ # extend classes with extra methods
2
+ class Float
3
+ def round_to(d = 2)
4
+
5
+ res = (self*10.0*d).round/(10.0*d)
6
+ return res
7
+ end
8
+ end
9
+
@@ -0,0 +1,73 @@
1
+ require 'narray'
2
+
3
+ class ScbiNArray < NArray
4
+
5
+ # mean absolute deviation
6
+ def mad_mean
7
+ me = self.mean
8
+ mad = ((self - me ).abs).mean
9
+
10
+ return [mad,me]
11
+
12
+ end
13
+
14
+ # meadian absolute deviation
15
+ def mad_median
16
+ me = self.median
17
+ mad = ((self - me).abs).median
18
+
19
+ return [mad,me]
20
+ end
21
+
22
+ # returns stddev for length=1
23
+ def stddev
24
+
25
+ if self.length<=1
26
+ stddev=0
27
+ else
28
+ stddev=super()
29
+ end
30
+
31
+ return stddev
32
+ end
33
+
34
+
35
+ # coefficient of variance
36
+ def variance_coefficient
37
+ return ((stddev/mean)*100)
38
+ end
39
+
40
+
41
+ # fat mode calculates mode based on accumulated frequency with a window_size
42
+ def fat_mode(window_size=10)
43
+
44
+ fat_modes=[]
45
+ max_fat=0
46
+
47
+ self.length.times do |i|
48
+ fat=get_window_value(i)
49
+
50
+ fat_modes << fat
51
+
52
+ if fat_modes[max_fat] < fat
53
+ max_fat=i
54
+ end
55
+
56
+ end
57
+ # puts "algo",max_fat
58
+ return [max_fat,fat_modes[max_fat]]
59
+ end
60
+
61
+
62
+ # =======================================
63
+ private
64
+
65
+ def get_window_value(i,window_size=10)
66
+ start_pos=[0,i-window_size].max
67
+
68
+ end_pos=[self.length-1,i+window_size].min
69
+
70
+ return self[start_pos..end_pos].sum
71
+ end
72
+
73
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/scbi_math.rb'}"
9
+ puts "Loading scbi_math gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/scbi_math'
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+
4
+
5
+ class TestScbiMath < Test::Unit::TestCase
6
+
7
+ def setup
8
+ end
9
+
10
+ def test_scbi_narray_class
11
+
12
+ na=ScbiNArray.to_na([2,3,4])
13
+ assert_equal('ScbiNArray',na.class.to_s)
14
+
15
+
16
+
17
+
18
+ # istat=JSON.parse(File.read('initial_stats.json'))
19
+ #
20
+ # x=[]
21
+ # istat['qv'].each do |qv|
22
+ # x<< qv['tot'].to_i
23
+ #
24
+ # end
25
+ # # Usage:
26
+ #
27
+ # s=ScbiStats.new(x)
28
+ #
29
+ # puts s.fat_mode
30
+ end
31
+
32
+ def test_fat_mode
33
+
34
+ na=ScbiNArray.to_na((1..40).collect.to_a)
35
+
36
+ na[10]=1000
37
+
38
+ assert_equal([10,21],na.mad_median)
39
+ assert_equal([47.525,45.225],na.mad_mean)
40
+
41
+
42
+ assert_equal([20,1430], na.fat_mode)
43
+
44
+
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scbi_math
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Dario Guerrero
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-30 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: narray
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: hoe
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.8.0
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: scbi_math is a ruby gem based on narray to do some additional statistics,
38
+ email:
39
+ - dariogf@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - History.txt
46
+ - Manifest.txt
47
+ - PostInstall.txt
48
+ files:
49
+ - History.txt
50
+ - Manifest.txt
51
+ - PostInstall.txt
52
+ - README.rdoc
53
+ - Rakefile
54
+ - lib/scbi_math.rb
55
+ - lib/scbi_math/array_extras.rb
56
+ - lib/scbi_math/float_extras.rb
57
+ - lib/scbi_math/scbi_narray.rb
58
+ - script/console
59
+ - script/destroy
60
+ - script/generate
61
+ - test/test_helper.rb
62
+ - test/test_scbi_math.rb
63
+ homepage: http://www.scbi.uma.es/downloads
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --main
69
+ - README.rdoc
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: scbi_math
87
+ rubygems_version: 1.7.2
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: scbi_math is a ruby gem based on narray to do some additional statistics,
91
+ test_files:
92
+ - test/test_helper.rb
93
+ - test/test_scbi_math.rb