timeseries 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --drb
3
+ --profile
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 2.0.0" > .rvmrc
9
+ environment_id="ruby-2.0.0-p0@timeseries"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.17.6 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in timeseries.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nick O'Neill
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Timeseries
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'timeseries'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install timeseries
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/timeseries.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "timeseries/version"
2
+ require 'timeseries/series'
3
+ require 'timeseries/data_point'
4
+ module Timeseries
5
+ # Your code goes here...
6
+
7
+ end
@@ -0,0 +1,11 @@
1
+ module Timeseries
2
+ class DataPoint
3
+ attr_accessor :date , :value
4
+
5
+ def initialize date = DateTime.new , value = 0
6
+ raise ArgumentError , "Must use ruby DateTime format for DataPoint dates" unless date.class == DateTime
7
+ @date = date
8
+ @value = value
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,130 @@
1
+ require 'active_support/core_ext'
2
+ require 'timeseries/data_point'
3
+ module Timeseries
4
+ # Series - a time-ordered array of values
5
+ # equations from here: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages
6
+ class Series < Array
7
+ def initialize *args
8
+ super
9
+ each { |item| raise ArgumentError , 'Invalid item added to Series, must be Timeseries::DataPoint' unless item.class == Timeseries::DataPoint }
10
+ end
11
+
12
+ # Returns
13
+ def sum_range offset = 0 , time_period = 7
14
+ self[offset,time_period].inject(0) { |total,dp| total.to_f + dp.value.to_f }
15
+ end
16
+
17
+ def sum
18
+ inject(0) { |total,dp| total.to_f + dp.value.to_f }
19
+ end
20
+
21
+ # Generates time series where the DataPoint object values are moving averages
22
+ #
23
+ # @return [Timeseries::Series] moving average series
24
+ def moving_average time_period = 7
25
+ moving_averages = zero_empty_dates.each_with_index.map do |c,i|
26
+ dp = DataPoint.new
27
+ dp.value = single_day_moving_average( i , time_period )
28
+ dp.date = c.date
29
+ dp
30
+ end
31
+ Timeseries::Series.new( moving_averages )
32
+ end
33
+
34
+ # Gets the moving average for a single day in the series
35
+ #
36
+ # @return [Float] moving average
37
+ def single_day_moving_average offset = 0 , time_period = 7
38
+ self.sum_range( offset , time_period ) / time_period.to_f
39
+ end
40
+
41
+ # Generates time series where the DataPoint object values are exponential moving averages
42
+ #
43
+ # @return [TimeSeries::Series] exponential moving average series
44
+ def exponential_moving_average time_period = 7
45
+ averages = self.each_with_index.map do |dp,i|
46
+ Timeseries::DataPoint.new( dp.date , ema( time_period , i ) )
47
+ end
48
+ Timeseries::Series.new( averages )
49
+ end
50
+
51
+ # Sorting methods
52
+ def method_missing(name , *args)
53
+ # sort DataPoints ascending or descending based on attribute (date or value)
54
+ if /^sort_by_(\w+)_([^!]+)(!?)$/.match(name)
55
+ sort_field = $1.to_sym
56
+ sort_dir = $2
57
+ bang = $3
58
+ return Timeseries::Series.new( self.send("sort#{bang}".to_sym) { |a,b| a.send(sort_field) <=> b.send(sort_field) } ) if sort_dir =~ /^asc/
59
+ return Timeseries::Series.new( self.send("sort#{bang}".to_sym) { |a,b| b.send(sort_field) <=> a.send(sort_field) } ) if sort_dir =~ /^desc/
60
+ elsif /^([^_]+)_day_((?:exponential_)?moving_average)$/.match(name)
61
+ case $1
62
+ when 'seven'
63
+ return send($2.to_sym , 7)
64
+ end
65
+ end
66
+ super
67
+ end
68
+
69
+ # Generates a daily summation of the series
70
+ #
71
+ # @return [Timeseries::Series] a daily version of the current series
72
+ def daily
73
+ grouped = group_by { |dp| dp.date.jd }
74
+ daily_series = grouped.map { |k,v| Timeseries::DataPoint.new( v[0].date , v.inject(0) { |sum,c| sum.to_f + c.value.to_f } ) }
75
+ Timeseries::Series.new( daily_series )
76
+ end
77
+
78
+ # Modifies the existing series to become a daily series
79
+ # KEEP IN MIND: This will overwrite existing values if you have multiple DataPoints for individual days
80
+ #
81
+ # @return [Timeseries::Series] self
82
+ def daily!
83
+ self.replace( daily )
84
+ end
85
+
86
+ # Fills in 0 for any empty dates
87
+ #
88
+ # @return [Timeseries::Series] an 0 filled time series
89
+ def zero_empty_dates
90
+ incomplete_series = daily.sort_by_date_descending!
91
+
92
+ daily_series = Timeseries::Series.new
93
+
94
+ return daily_series if incomplete_series.length < 2
95
+
96
+ ( incomplete_series.last.date.jd .. incomplete_series.first.date.jd ).map do |jd_date|
97
+ match = incomplete_series.find { |dp| dp.date.jd == jd_date }
98
+ value = ( match == nil ) ? 0 : match.value
99
+ date = DateTime.jd( jd_date ).midnight
100
+ daily_series.unshift( Timeseries::DataPoint.new( date , value ) )
101
+ end
102
+
103
+ daily_series
104
+ end
105
+
106
+ private
107
+ def ema time_period = 7 , offset = 0
108
+ # equation:
109
+ # Multiplier: (2 / (Time periods + 1) ) = (2 / (10 + 1) ) = 0.1818 (18.18%)
110
+ # EMA: {value - EMA(previous day)} x multiplier + EMA(previous day).
111
+ return nil if offset > ( self.length - time_period )
112
+
113
+ # Exponential moving average always uses simple moving average as first value
114
+ return single_day_moving_average( offset , time_period ) if offset == ( self.length - time_period )
115
+
116
+ # generate the ema for the given day
117
+ previous_day = offset + 1
118
+ yesterday_ema = ema( time_period , previous_day )
119
+ ( self[offset].value - yesterday_ema ) * exponential_multiplier( time_period ) + yesterday_ema
120
+ end
121
+
122
+ def data_points_for_jd_day day
123
+ find_all { |dp| dp.date.jd == day }
124
+ end
125
+
126
+ def exponential_multiplier time_period = 7
127
+ ( 2 / ( time_period.to_f + 1.0 ) )
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,3 @@
1
+ module Timeseries
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Timeseries::DataPoint do
4
+ before { @point = Timeseries::DataPoint.new( DateTime.new , 123 ) }
5
+
6
+ subject { @point }
7
+
8
+ it { should respond_to(:date) }
9
+ it { should respond_to(:value) }
10
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Timeseries::Series do
4
+ before do
5
+ @datapoints = (0..10).map{ |i| Timeseries::DataPoint.new( DateTime.now - i , i + 1 ) }
6
+ end
7
+
8
+ it "can be initialized with an array of DataPoint objects" do
9
+ expect {
10
+ Timeseries::Series.new( [ Timeseries::DataPoint.new( DateTime.now , 123 ) ] )
11
+ }.to_not raise_error
12
+ end
13
+
14
+ it "raises error for non-DataPoint objects" do
15
+ expect {
16
+ Timeseries::Series.new( [123] )
17
+ }.to raise_error(ArgumentError)
18
+ end
19
+
20
+ context "instance methods" do
21
+ before do
22
+ @series = Timeseries::Series.new( @datapoints )
23
+ end
24
+
25
+ describe "#sum_range" do
26
+ it "sums the range requested" do
27
+ # sums @datapoints[0,7]
28
+ @series.sum_range( 0 , 7 ).should eq 28
29
+ end
30
+ end
31
+
32
+ describe "#sum" do
33
+ it "returns the sum of all the datapoints" do
34
+ @series.sum.should eq 66
35
+ end
36
+ end
37
+
38
+ describe "#moving_average" do
39
+ it "zeros any empty days" do
40
+ Timeseries::Series.any_instance.should_receive(:zero_empty_dates).and_return(Timeseries::Series.new)
41
+ @series.moving_average(7)
42
+ end
43
+
44
+ it "returns a Timeseries::Series" do
45
+ @series.moving_average(2).should be_an_instance_of( Timeseries::Series )
46
+ end
47
+
48
+ it "returns moving average for the given number of days" do
49
+ @series.moving_average(2)[0].value.should eq 1.5
50
+ end
51
+ end
52
+
53
+ describe "#single_day_moving_average" do
54
+ end
55
+
56
+ describe "#exponential_moving_average" do
57
+ end
58
+
59
+ describe "#daily" do
60
+ end
61
+
62
+ describe "#daily!" do
63
+ end
64
+
65
+ describe "#zero_empty_dates" do
66
+ it "fills in empty days as 0" do
67
+ @series.delete_at( 1 )
68
+ @series.zero_empty_dates[1].value.should eq 0
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'timeseries'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :rspec
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'timeseries/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "timeseries"
8
+ spec.version = Timeseries::VERSION
9
+ spec.authors = ["Nick O'Neill"]
10
+ spec.email = ["mr.nick.oneill@gmail.com"]
11
+ spec.description = "A time series gem used by StartupStats apps"
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/Holler/timeseries"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "yard", "~> 0.7.3"
25
+
26
+ spec.add_dependency "activesupport"
27
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timeseries
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick O'Neill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.7.3
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: 0.7.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: activesupport
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A time series gem used by StartupStats apps
95
+ email:
96
+ - mr.nick.oneill@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - .rvmrc
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - lib/timeseries.rb
109
+ - lib/timeseries/data_point.rb
110
+ - lib/timeseries/series.rb
111
+ - lib/timeseries/version.rb
112
+ - spec/data_point_spec.rb
113
+ - spec/series_spec.rb
114
+ - spec/spec_helper.rb
115
+ - timeseries.gemspec
116
+ homepage: https://github.com/Holler/timeseries
117
+ licenses:
118
+ - MIT
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ segments:
130
+ - 0
131
+ hash: -667150900491174156
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ segments:
139
+ - 0
140
+ hash: -667150900491174156
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 1.8.25
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: A time series gem used by StartupStats apps
147
+ test_files:
148
+ - spec/data_point_spec.rb
149
+ - spec/series_spec.rb
150
+ - spec/spec_helper.rb
151
+ has_rdoc: