sciruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/sciruby.rb +31 -0
  2. data/lib/sciruby/recommend.rb +66 -0
  3. metadata +63 -0
@@ -0,0 +1,31 @@
1
+ # = sci_ruby.rb -
2
+ # SciRuby - Ruby scientific visualization and computation.
3
+ #
4
+ # Copyright (C) 2011 SciRuby Development Team
5
+ # * John O. Woods
6
+ # * John T. Prince
7
+ # * Claudio Bustos
8
+ # * others
9
+ #
10
+ # This program is free software; you can redistribute it and/or
11
+ # modify it under the terms of the GNU General Public License
12
+ # as published by the Free Software Foundation; either version 2
13
+ # of the License, or (at your option) any later version.
14
+ #
15
+ # This program is distributed in the hope that it will be useful,
16
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ # GNU General Public License for more details.
19
+ #
20
+ # You should have received a copy of the GNU General Public License
21
+ # along with this program; if not, write to the Free Software
22
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
+ #
24
+ # Specific notices will be placed where they are appropriate.
25
+ #
26
+
27
+ module SciRuby
28
+ VERSION = '0.1.0'
29
+
30
+ autoload(:Recommend, 'sciruby/recommend')
31
+ end
@@ -0,0 +1,66 @@
1
+ require 'set'
2
+ require 'distribution' # for Hypergeometric
3
+
4
+ unless defined?(SortedSet) # Ruby 1.8
5
+ class SortedSet < Set
6
+ end
7
+ end
8
+
9
+ # Added by John O. Woods.
10
+ #
11
+ # Makes use of Distribution gem, formerly part of Statsample, by Claudio Bustos.
12
+
13
+ module SciRuby
14
+ # Methods and classes for expert recommendation systems.
15
+ module Recommend
16
+ # Set Distance functions: determine distances between sets.
17
+ #
18
+ # These functions may be useful for k-nearest neighbors searches and expert recommendation systems.
19
+ #
20
+ # Sets are used for systems where vectors are binary. For example, if you have a matrix of customers and products, a
21
+ # zero means the customer has not bought the product, and a one means the customer has. There is no concept of
22
+ # degree.
23
+ #
24
+ # Pearson is probably the function most people will want to use.
25
+ class SetDistance
26
+
27
+ # Create a new recommendation-by-set-distance object. This requires as arguments two sets (+a+ and +b+) and a
28
+ # +total+ size which indicates the number of items from which the sets are drawn.
29
+ #
30
+ # It also takes an optional +distance_function+ (e.g., :hypergeometric or :pearson). If this is given, the distance
31
+ # will be calculated immediately. Otherwise, you can use the various distance functions to calculate distance by
32
+ # a variety of metrics (e.g., distance_hypergeometric, distance_pearson, and so on).
33
+ def initialize a, b, total, distance_function = nil
34
+ @a = a.is_a?(Set) ? a : SortedSet.new(a)
35
+ @b = b.is_a?(Set) ? b : SortedSet.new(b)
36
+ @total = total
37
+
38
+ unless distance_function.nil? # Calculate immediately if a distance function is given.
39
+ @distance_function = "distance_#{distance_function.to_s}".to_sym
40
+ @distance = self.send @distance_function
41
+ end
42
+ end
43
+
44
+ attr_reader :a, :b, :total, :distance
45
+
46
+ def m; a.size; end
47
+ def n; b.size; end
48
+ def ab; @ab ||= a.intersection(b); end
49
+ def k; ab.size; end
50
+ alias :a_dot_b :k
51
+
52
+ # Calculate distance as the hypergeometric probability of seeing an intersection of +k+ or greater between two sets
53
+ # +a+ and +b+. This is basically the complement of cdf(+k-1+, +m+, +n+, +total+).
54
+ def distance_hypergeometric
55
+ @distance_hypergeometric ||= 1.0 - Distribution::Hypergeometric.cdf(k-1, m, n, total)
56
+ end
57
+
58
+ # The generalization of Pearson correlation coefficient using binary vectors.
59
+ def distance_pearson
60
+ @distance_pearson ||= 1.0 - (total * a_dot_b - m*n).abs / Math.sqrt( (total - m)*(total - n)*m*n )
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sciruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - SciRuby
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-01 00:00:00.000000000 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: ! 'Ruby has for some time had no equivalent to the beautifully constructed
16
+ numpy, scipy, and matplotlib libraries for Python. We believe that the time for
17
+ a Ruby science and visualization package has come and gone. Sometimes when a solution
18
+ of sugar and water becomes super-saturated, from it precipitates a pure, delicious,
19
+ and diabetes-inducing crystal of sweetness, induced by no more than the tap of a
20
+ finger. So it is, we believe, with the need for numeric and visualization libraries
21
+ in Ruby.
22
+
23
+
24
+ We are not the first with this idea, but we are trying to bring it to life.
25
+
26
+
27
+ At this point, SciRuby has not much to offer. But if you install this gem, you''ll
28
+ get as dependencies all of the libraries that we plan to incorporate into SciRuby.
29
+
30
+ '
31
+ email: john.woods@marcottelab.org
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/sciruby.rb
37
+ - lib/sciruby/recommend.rb
38
+ has_rdoc: true
39
+ homepage: http://www.github.com/SciRuby/sciruby
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project: sciruby
59
+ rubygems_version: 1.6.2
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Tools for scientific computation in Ruby
63
+ test_files: []