fazscore 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +13 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +5 -0
- data/README.markdown +51 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/fazscore.gemspec +63 -0
- data/lib/fazscore.rb +33 -0
- data/test/helper.rb +26 -0
- data/test/test_fazscore.rb +13 -0
- metadata +137 -0
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.5.2)
|
6
|
+
bundler (~> 1.0.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.8.7)
|
10
|
+
rcov (0.9.9)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.5.2)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
2
|
+
|
3
|
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
4
|
+
|
5
|
+
You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>
|
data/README.markdown
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# fazscore - Floating Average Z-Score implementation
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
An implementation of a Floating Average Z-Score algorithmic method for use in identifying trends in historical data.
|
6
|
+
|
7
|
+
For further information see - http://stackoverflow.com/questions/787496/what-is-the-best-way-to-compute-trending-topics-or-tags/826509#826509
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
gem install fazscore
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Typically you'll have a set of historical data points that you want to compare to a current data point and see how much the latest data point has deviated from the historical average, to identify if it's trending.
|
16
|
+
|
17
|
+
require 'fazscore'
|
18
|
+
|
19
|
+
# A set of mentions for a set of terms
|
20
|
+
history = { 'England' => [5,1,3,4,4],
|
21
|
+
'Ireland' => [1,0,0,1,2],
|
22
|
+
'Scotland' => [2,4,1,4,5]}
|
23
|
+
|
24
|
+
# The current number of mentions for each term
|
25
|
+
current = { 'England' => 0,
|
26
|
+
'Ireland' => 7,
|
27
|
+
'Scotland' => 6 }
|
28
|
+
|
29
|
+
# Calculate the trending scores
|
30
|
+
scores = {}
|
31
|
+
history.keys.each do |term|
|
32
|
+
score[term] = FAZScore.new(0.5, history[term]).score(current[term])
|
33
|
+
end
|
34
|
+
|
35
|
+
# Display 'most' trending term with trending score
|
36
|
+
puts scores.sort {|a,b| a[1]<=>b[1]}.last
|
37
|
+
|
38
|
+
## Contributing to fazscore
|
39
|
+
|
40
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
41
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
42
|
+
* Fork the project
|
43
|
+
* Start a feature/bugfix branch
|
44
|
+
* Commit and push until you are happy with your contribution
|
45
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
46
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
47
|
+
|
48
|
+
## Copyright
|
49
|
+
|
50
|
+
Copyright (c) 2011 Rob Lee. See LICENSE.txt for further details.
|
51
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
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
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "fazscore"
|
16
|
+
gem.homepage = "http://github.com/rattle/fazscore"
|
17
|
+
gem.license = "GPLv3"
|
18
|
+
gem.summary = %Q{Floating Average Z-Score implementation}
|
19
|
+
gem.description = %Q{TAn implementation of a Floating Average Z-Score algorithmic method for use in identifying trends with historical data.}
|
20
|
+
gem.email = "robl[at]rjlee.net"
|
21
|
+
gem.authors = ["Rob Lee"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "fazscore #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/fazscore.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{fazscore}
|
8
|
+
s.version = "0.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Rob Lee"]
|
12
|
+
s.date = %q{2011-01-06}
|
13
|
+
s.description = %q{TAn implementation of a Floating Average Z-Score algorithmic method for use in identifying trends with historical data.}
|
14
|
+
s.email = %q{robl[at]rjlee.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.markdown",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"fazscore.gemspec",
|
27
|
+
"lib/fazscore.rb",
|
28
|
+
"test/helper.rb",
|
29
|
+
"test/test_fazscore.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/rattle/fazscore}
|
32
|
+
s.licenses = ["GPLv3"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.7}
|
35
|
+
s.summary = %q{Floating Average Z-Score implementation}
|
36
|
+
s.test_files = [
|
37
|
+
"test/helper.rb",
|
38
|
+
"test/test_fazscore.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
49
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
54
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
60
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
data/lib/fazscore.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
class FAZScore
|
2
|
+
|
3
|
+
attr_accessor :decay, :population, :sqr_avg, :avg
|
4
|
+
|
5
|
+
def initialize(decay,population)
|
6
|
+
@sqr_avg = @avg = 0
|
7
|
+
@decay = decay
|
8
|
+
population.each { |p| self.update(p) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def update(value)
|
12
|
+
value = value.to_f
|
13
|
+
if @avg == 0 and @sqr_avg == 0:
|
14
|
+
@avg = value
|
15
|
+
@sqr_avg = value ** 2
|
16
|
+
else
|
17
|
+
@avg = @avg * @decay + value * (1 - @decay)
|
18
|
+
@sqr_avg = @sqr_avg * @decay + (value ** 2) * (1 - @decay)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def std
|
23
|
+
return Math.sqrt(@sqr_avg - @avg ** 2)
|
24
|
+
end
|
25
|
+
|
26
|
+
def score(obs)
|
27
|
+
if self.std() == 0
|
28
|
+
return 0
|
29
|
+
else
|
30
|
+
return (obs - self.avg) / self.std()
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
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
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'fazscore'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
|
19
|
+
HISTORY = { 'England' => [5,1,3,4,4],
|
20
|
+
'Ireland' => [1,0,0,1,2],
|
21
|
+
'Scotland' => [2,4,1,4,5]}
|
22
|
+
CURRENT = { 'England' => 0,
|
23
|
+
'Ireland' => 7,
|
24
|
+
'Scotland' => 6 }
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestFazscore < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "identify the trending term" do
|
6
|
+
scores = {}
|
7
|
+
HISTORY.keys.each do |term|
|
8
|
+
scores[term] = FAZScore.new(0.5, HISTORY[term]).score(CURRENT[term])
|
9
|
+
end
|
10
|
+
assert_equal 'Ireland', scores.sort {|a,b| a[1]<=>b[1]}.last[0]
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fazscore
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 0.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rob Lee
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-06 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
name: shoulda
|
24
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
requirement: *id001
|
34
|
+
type: :development
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
prerelease: false
|
37
|
+
name: bundler
|
38
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 23
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
version: 1.0.0
|
49
|
+
requirement: *id002
|
50
|
+
type: :development
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
prerelease: false
|
53
|
+
name: jeweler
|
54
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 7
|
60
|
+
segments:
|
61
|
+
- 1
|
62
|
+
- 5
|
63
|
+
- 2
|
64
|
+
version: 1.5.2
|
65
|
+
requirement: *id003
|
66
|
+
type: :development
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
name: rcov
|
70
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirement: *id004
|
80
|
+
type: :development
|
81
|
+
description: TAn implementation of a Floating Average Z-Score algorithmic method for use in identifying trends with historical data.
|
82
|
+
email: robl[at]rjlee.net
|
83
|
+
executables: []
|
84
|
+
|
85
|
+
extensions: []
|
86
|
+
|
87
|
+
extra_rdoc_files:
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.markdown
|
90
|
+
files:
|
91
|
+
- Gemfile
|
92
|
+
- Gemfile.lock
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.markdown
|
95
|
+
- Rakefile
|
96
|
+
- VERSION
|
97
|
+
- fazscore.gemspec
|
98
|
+
- lib/fazscore.rb
|
99
|
+
- test/helper.rb
|
100
|
+
- test/test_fazscore.rb
|
101
|
+
has_rdoc: true
|
102
|
+
homepage: http://github.com/rattle/fazscore
|
103
|
+
licenses:
|
104
|
+
- GPLv3
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 1.3.7
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Floating Average Z-Score implementation
|
135
|
+
test_files:
|
136
|
+
- test/helper.rb
|
137
|
+
- test/test_fazscore.rb
|