moving_avg 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +46 -0
- data/README.md +84 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/moving_avg/base.rb +50 -0
- data/lib/moving_avg/evaluator.rb +26 -0
- data/lib/moving_avg/helper.rb +36 -0
- data/lib/moving_avg/version.rb +3 -0
- data/lib/moving_avg.rb +7 -0
- data/moving_avg.gemspec +27 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9ca3a31aa83c9ee4734e42d2b1473e4454a37d7a
|
4
|
+
data.tar.gz: 28ddfa0b6d617b230ecc2b48ef3971b3bead7bcf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 19bd75386f757b260aa9ea4a4b841aae58e84afb7e0c674301b9630766715caa1e3bf3db8bcffedd1095a03e0c93319d76117e07e74cbb335345554bff5fb7a7
|
7
|
+
data.tar.gz: 4b5812e266961e95e9a47988807b317f2c1f7447faf02b395aad6aa6a572ece5e6c4d477142be37f94d96878e0a26be2d346687b168dfa7dfab5a81264e45c5a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
moving_avg (0.2.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
byebug (9.1.0)
|
10
|
+
coderay (1.1.2)
|
11
|
+
diff-lcs (1.3)
|
12
|
+
method_source (0.9.0)
|
13
|
+
pry (0.11.3)
|
14
|
+
coderay (~> 1.1.0)
|
15
|
+
method_source (~> 0.9.0)
|
16
|
+
pry-byebug (3.5.1)
|
17
|
+
byebug (~> 9.1)
|
18
|
+
pry (~> 0.10)
|
19
|
+
rake (10.5.0)
|
20
|
+
rspec (3.6.0)
|
21
|
+
rspec-core (~> 3.6.0)
|
22
|
+
rspec-expectations (~> 3.6.0)
|
23
|
+
rspec-mocks (~> 3.6.0)
|
24
|
+
rspec-core (3.6.0)
|
25
|
+
rspec-support (~> 3.6.0)
|
26
|
+
rspec-expectations (3.6.0)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.6.0)
|
29
|
+
rspec-mocks (3.6.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.6.0)
|
32
|
+
rspec-support (3.6.0)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
bundler (~> 1.16.a)
|
39
|
+
moving_avg!
|
40
|
+
pry
|
41
|
+
pry-byebug
|
42
|
+
rake (~> 10.0)
|
43
|
+
rspec (~> 3.0)
|
44
|
+
|
45
|
+
BUNDLED WITH
|
46
|
+
1.16.0.pre.3
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# MovingAvg
|
2
|
+
|
3
|
+
Calculate Moving Average in Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'moving_avg'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install moving_avg
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
data = [100, 200, 200, 500]
|
25
|
+
p MovingAvg::Base.sma(data) #=> 250.0
|
26
|
+
p MovingAvg::Base.wma(data) #=> 190.0
|
27
|
+
p MovingAvg::Base.ewma(data) #=> 298.4
|
28
|
+
p MovingAvg::Base.mma(data) #=> 232.8125
|
29
|
+
```
|
30
|
+
|
31
|
+
## `MovingAvg::Base`
|
32
|
+
|
33
|
+
### Simple Moving Average
|
34
|
+
- aliases
|
35
|
+
- `MovingAvg::Base.simple_moving_average()`
|
36
|
+
- `MovingAvg::Base.sma()`
|
37
|
+
|
38
|
+
### Weighted Moving Average
|
39
|
+
- a.k.a. Linear Weighted Moving Average
|
40
|
+
- aliases
|
41
|
+
- `MovingAvg::Base.weighted_moving_average()`
|
42
|
+
- `MovingAvg::Base.wma()`
|
43
|
+
- `MovingAvg::Base.lwma()`
|
44
|
+
|
45
|
+
### Exponentially Weighted Moving Average
|
46
|
+
- aliases
|
47
|
+
- `MovingAvg::Base.exponentially_weighted_moving_average()`
|
48
|
+
- `MovingAvg::Base.ewma()`
|
49
|
+
|
50
|
+
### Modified Moving Average
|
51
|
+
- aliases
|
52
|
+
- `MovingAvg::Base.modified_moving_average()`
|
53
|
+
- `MovingAvg::Base.mma()`
|
54
|
+
|
55
|
+
|
56
|
+
## `MovingAvg::Evaluator`
|
57
|
+
|
58
|
+
### `.error_sum`
|
59
|
+
|
60
|
+
Helper method for calculating the sum of errors.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
training_items = [100, 300, 350, 280, 500]
|
64
|
+
teacher_items = [110.0, 200.0, 300.0, 400.0]
|
65
|
+
%i(sma wma ewma mma).each { |strategy|
|
66
|
+
error = MovingAvg::Evaluator.error_sum(
|
67
|
+
training_items: training_items,
|
68
|
+
teacher_items: teacher_items,
|
69
|
+
window_size: 3,
|
70
|
+
strategy: strategy,
|
71
|
+
)
|
72
|
+
puts "#{strategy.to_s.ljust(4).rjust(10)} = #{error}"
|
73
|
+
}
|
74
|
+
```
|
75
|
+
|
76
|
+
## Development
|
77
|
+
|
78
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
79
|
+
|
80
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kenju/moving_avg.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "moving_avg"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "moving_avg/version"
|
2
|
+
|
3
|
+
module MovingAvg
|
4
|
+
class Base
|
5
|
+
class << self
|
6
|
+
# SMA - Simple Moving Average
|
7
|
+
def simple_moving_average(u)
|
8
|
+
sum_vector(u).to_f / u.size
|
9
|
+
end
|
10
|
+
alias_method :sma, :simple_moving_average
|
11
|
+
|
12
|
+
# WMA - Weighted Moving Average
|
13
|
+
def weighted_moving_average(u)
|
14
|
+
weights = (1..u.size).to_a.reverse # [n, n-1, n-2 ... 2, 1]
|
15
|
+
weighted = weights.zip(u).map { |w, x| w * x }
|
16
|
+
sum_vector(weighted).to_f / sum_vector(weights)
|
17
|
+
end
|
18
|
+
alias_method :wma, :weighted_moving_average
|
19
|
+
alias_method :lwma, :weighted_moving_average
|
20
|
+
|
21
|
+
# EWMA - Exponentially Weighted Moving Average
|
22
|
+
def exponentially_weighted_moving_average(u)
|
23
|
+
factor = 2.0 / (u.size + 1) # 2 / (N + 1) a.k.a. smoothing factor
|
24
|
+
weighted = exponentially_weighting(u, factor)
|
25
|
+
weighted.last
|
26
|
+
end
|
27
|
+
alias_method :ewma, :exponentially_weighted_moving_average
|
28
|
+
|
29
|
+
# MMA - Modified Moving Average
|
30
|
+
def modified_moving_average(u)
|
31
|
+
factor = 1.0 / u.size # 1 / N
|
32
|
+
weighted = exponentially_weighting(u, factor)
|
33
|
+
weighted.last
|
34
|
+
end
|
35
|
+
alias_method :mma, :modified_moving_average
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def exponentially_weighting(u, factor)
|
40
|
+
u.inject([]) { |acc, x|
|
41
|
+
acc << factor * x + (1 - factor) * (acc.last || x)
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def sum_vector(u)
|
46
|
+
u.reduce(0) { |x, y| x + y }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module MovingAvg
|
2
|
+
class Evaluator
|
3
|
+
class << self
|
4
|
+
def error_sum(training_items:, teacher_items:, window_size:, strategy:)
|
5
|
+
errors = self.errors(
|
6
|
+
training_items: training_items,
|
7
|
+
teacher_items: teacher_items,
|
8
|
+
window_size: window_size,
|
9
|
+
strategy: strategy,
|
10
|
+
)
|
11
|
+
errors.reduce(0) { |x, y| x + y }
|
12
|
+
end
|
13
|
+
|
14
|
+
def errors(training_items:, teacher_items:, window_size:, strategy:)
|
15
|
+
trained = MovingAvg::Helper.build_with_sliding(
|
16
|
+
items: training_items,
|
17
|
+
window_size: window_size,
|
18
|
+
strategy: strategy,
|
19
|
+
)
|
20
|
+
trained.zip(teacher_items).map { |trained, teacher|
|
21
|
+
(trained - teacher).abs
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module MovingAvg
|
2
|
+
class Helper
|
3
|
+
class << self
|
4
|
+
# repeatedly calculate moving average
|
5
|
+
# separating items by window size.
|
6
|
+
# the first N-1 items should be padded
|
7
|
+
# or calculated from inconvinient data
|
8
|
+
def build_with_sliding(items:, window_size:, strategy:)
|
9
|
+
map_with_sliding(
|
10
|
+
items: items,
|
11
|
+
window_size: window_size,
|
12
|
+
).map { |data|
|
13
|
+
MovingAvg::Base.public_send(strategy, data)
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def map_with_sliding(items:, window_size:)
|
18
|
+
items.each_with_object([]) { |val, acc|
|
19
|
+
if acc.last.nil?
|
20
|
+
# the first time
|
21
|
+
acc << [val]
|
22
|
+
elsif acc.last.size < window_size
|
23
|
+
# the first window
|
24
|
+
acc.last << val
|
25
|
+
else
|
26
|
+
# the following windows
|
27
|
+
next_window = acc.last.dup
|
28
|
+
next_window.shift
|
29
|
+
next_window << val
|
30
|
+
acc << next_window
|
31
|
+
end
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/moving_avg.rb
ADDED
data/moving_avg.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "moving_avg/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "moving_avg"
|
8
|
+
spec.version = MovingAvg::VERSION
|
9
|
+
spec.authors = ["kenju"]
|
10
|
+
spec.email = ["ken901waga@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Moving Average in Ruby.}
|
13
|
+
spec.homepage = "https://github.com/kenju/moving_avg-ruby"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.16.a"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
spec.add_development_dependency "pry-byebug"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moving_avg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kenju
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.16.a
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.16.a
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- ken901waga@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/moving_avg.rb
|
100
|
+
- lib/moving_avg/base.rb
|
101
|
+
- lib/moving_avg/evaluator.rb
|
102
|
+
- lib/moving_avg/helper.rb
|
103
|
+
- lib/moving_avg/version.rb
|
104
|
+
- moving_avg.gemspec
|
105
|
+
homepage: https://github.com/kenju/moving_avg-ruby
|
106
|
+
licenses: []
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.6.13
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Moving Average in Ruby.
|
128
|
+
test_files: []
|