curve_fit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
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 "rspec", "~> 2.4.0"
10
+ gem "yard", "~> 0.6.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.5.2"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,152 @@
1
+ = curve_fit
2
+
3
+ A wrapper around fityk (http://fityk.nieto.pl/) to handle fitting a curve to X+Y data, creating confidence intervals, and projecting up to a ceiling.
4
+
5
+ You must have a working installation of cfityk in your path for this library to work.
6
+
7
+ == Examples
8
+
9
+ The primary use of this library is to take a data array of X+Y points and return to you a trend line, confidence intervals, a best-guess shape and an r-squared value.
10
+
11
+ require 'curve_fit'
12
+
13
+ cf = CurveFit.new
14
+ cf.fit(
15
+ [
16
+ [ 0, 1000.0 ],
17
+ [ 1, 2003.0 ],
18
+ [ 2, 3010.0 ],
19
+ [ 3, 4084.0 ],
20
+ [ 4, 5012.0 ],
21
+ [ 5, 6075.0 ]
22
+ ]
23
+ )
24
+
25
+ Returns:
26
+
27
+ {
28
+ :data => [
29
+ [0, 1000.0],
30
+ [1, 2003.0],
31
+ [2, 3010.0],
32
+ [3, 4084.0],
33
+ [4, 5012.0],
34
+ [5, 6075.0]
35
+ ],
36
+ :trend => [
37
+ [0, 997.27],
38
+ [1, 2010.57],
39
+ [2, 3023.87],
40
+ [3, 4037.1699999999996],
41
+ [4, 5050.469999999999],
42
+ [5, 6063.77]
43
+ ],
44
+ :top_confidence => [
45
+ [0, 1010.5636999999999],
46
+ [1, 2030.03089],
47
+ [2, 3049.49808],
48
+ [3, 4068.9652699999997],
49
+ [4, 5088.43246],
50
+ [5, 6107.899649999999]
51
+ ],
52
+ :bottom_confidence => [
53
+ [0, 983.9763],
54
+ [1, 1991.1091099999999],
55
+ [2, 2998.24192],
56
+ [3, 4005.3747299999995],
57
+ [4, 5012.50754],
58
+ [5, 6019.64035]
59
+ ],
60
+ :ceiling=>[],
61
+ :r_squared=>0.999774,
62
+ :guess=>"Linear"
63
+ }
64
+
65
+ You can pass a second argument, which will be the ceiling to project up to. In
66
+ that case, the trend, top and bottom confidence data will be extended out until
67
+ the Y value is >= the ceiling. Additionally, the ceiling line will be
68
+ populated with the ceiling value for each value of X.
69
+
70
+ cf.fit(
71
+ [
72
+ [ 0, 1000.0 ],
73
+ [ 1, 2003.0 ],
74
+ [ 2, 3010.0 ],
75
+ [ 3, 4084.0 ],
76
+ [ 4, 5012.0 ],
77
+ [ 5, 6075.0 ]
78
+ ],
79
+ 10000
80
+ )
81
+
82
+ Would extrapolate up to 10000.
83
+
84
+ Currently, this library only supports linear and quadratic fits. By default it
85
+ tries both, in that order. You can specify the fits to guess manually, if you
86
+ want to narrow things down:
87
+
88
+ cf.fit(
89
+ [
90
+ [ 0, 1000.0 ],
91
+ [ 1, 2003.0 ],
92
+ [ 2, 3010.0 ],
93
+ [ 3, 4084.0 ],
94
+ [ 4, 5012.0 ],
95
+ [ 5, 6075.0 ]
96
+ ],
97
+ 10000,
98
+ [ "Quadratic", "Linear" ]
99
+ )
100
+
101
+ Finally, you can pass a block to manipulate the value of X in the result set. This is often used if your data is a time series - under the hood, we convert the values of X to the position in the array (0..n). This block lets you get the original data back out, and support generating new correct values as you extrapolate to a ceiling.
102
+
103
+ For example, lets assume we're daily from 2010/1/4.
104
+
105
+ cf.fit(
106
+ [
107
+ [ Time.utc("2010", "1", "4").to_i, 1000.0 ],
108
+ [ Time.utc("2010", "1", "5").to_i, 2003.0 ],
109
+ [ Time.utc("2010", "1", "6").to_i, 3010.0 ],
110
+ [ Time.utc("2010", "1", "7").to_i, 4084.0 ],
111
+ [ Time.utc("2010", "1", "8").to_i, 5012.0 ],
112
+ [ Time.utc("2010", "1", "9").to_i, 6075.0 ]
113
+ ],
114
+ 10000
115
+ ) do |x|
116
+ if x == 0
117
+ Time.utc("2011", "1", "4").to_i
118
+ else
119
+ (Time.utc("2011", "1", "4") + ( 60 * 60 * 24 * 1 ))
120
+ end
121
+ end
122
+
123
+ Will result in the data set, and extrapolated data, having seconds since the epoch for X values.
124
+
125
+ == Contributing to curve_fit
126
+
127
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
128
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
129
+ * Fork the project
130
+ * Start a feature/bugfix branch
131
+ * Commit and push until you are happy with your contribution
132
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
133
+ * 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.
134
+
135
+ == Copyright and License
136
+
137
+ Copyright 2010 Opscode, Inc.
138
+
139
+ Licensed under the Apache License, Version 2.0 (the "License");
140
+ you may not use this file except in compliance with the License.
141
+ You may obtain a copy of the License at
142
+
143
+ http://www.apache.org/licenses/LICENSE-2.0
144
+
145
+ Unless required by applicable law or agreed to in writing, software
146
+ distributed under the License is distributed on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
148
+ See the License for the specific language governing permissions and
149
+ limitations under the License.
150
+
151
+ See LICENSE file for complete license details.
152
+
@@ -0,0 +1,43 @@
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 = "curve_fit"
16
+ gem.homepage = "http://github.com/adamhjk/curve_fit"
17
+ gem.license = "Apache 2.0"
18
+ gem.summary = %Q{Wrapper for curve fitting and confidence around cfityk}
19
+ gem.description = %Q{Creates curve fitting and confidence intervals from cfityk}
20
+ gem.email = "adam@opscode.com"
21
+ gem.authors = ["Adam Jacob"]
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 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'yard'
43
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,254 @@
1
+ #
2
+ # Copyright 2010 Opscode, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require 'tempfile'
17
+
18
+ # A wrapper around cfityk (http://fityk.nieto.pl/) to handle fitting a curve to X+Y data, creating confidence intervals, and projecting up to a ceiling.
19
+ #
20
+ # Also supports basic manipulation of X+Y data files.
21
+ class CurveFit
22
+
23
+ attr_accessor :debug
24
+
25
+ def initialize(debug=false)
26
+ @debug = debug
27
+ end
28
+
29
+ # Loads an x+y style data file as an array of arrays, suitable for passing to the fit method.
30
+ #
31
+ # @param [String] filename
32
+ # The filename to load.
33
+ # @return [Array] data
34
+ # An X+Y array:
35
+ # [ [ X, Y ], [ X, Y ] ]
36
+ def load_xy_file(filename)
37
+ xy_data = Array.new
38
+ File.open(filename, "r") do |xy_file|
39
+ xy_file.each_line do |line|
40
+ x, y = line.split(' ')
41
+ xy_data << [ string_to_number(x), string_to_number(y) ]
42
+ end
43
+ end
44
+ xy_data
45
+ end
46
+
47
+ # Takes a string of digits and converts it to an integer or a float,
48
+ # depending on whether it rocks the dot. Returns the raw string if nothing
49
+ # matches.
50
+ #
51
+ # @param [String] string
52
+ # @return [Integer,Float,String] transformed_string
53
+ def string_to_number(string)
54
+ case string
55
+ when /^\d+$/
56
+ string.to_i
57
+ when /^\d+.\d$/
58
+ string.to_f
59
+ else
60
+ string
61
+ end
62
+ end
63
+
64
+ # Adds an entry to an x+y style data file.
65
+ #
66
+ # @param [String] filename
67
+ # The filename to append to
68
+ # @param [String] x
69
+ # The X value
70
+ # @param [String] y
71
+ # The Y value
72
+ # @return [True]
73
+ def append_xy_file(filename, x, y)
74
+ File.open(filename, 'a') do |xy_file|
75
+ xy_file.puts "#{x} #{y}"
76
+ end
77
+ true
78
+ end
79
+
80
+ # Writes a data set out to an X+Y file
81
+ #
82
+ # @param [String] filename
83
+ # If given, the filename to write. Otherwise, creates a tempfile.
84
+ # @param [Array] data
85
+ # An X+Y array:
86
+ # [ [ X, Y ], [ X, Y ] ]
87
+ # @return [IO] data_file Returns the closed file IO object.
88
+ def write_xy_file(filename=nil, data)
89
+ data_file = nil
90
+ if filename
91
+ data_file = File.open(filename, "w")
92
+ else
93
+ data_file = Tempfile.new("curvefit")
94
+ filename = data_file.path
95
+ end
96
+
97
+ x_pos = 0
98
+ data.each do |point|
99
+ data_file.puts("#{x_pos} #{point[1]}")
100
+ x_pos += 1
101
+ end
102
+
103
+ data_file.close
104
+
105
+ data_file
106
+ end
107
+
108
+ # Given an aray of X,Y data points, guesses the most correct curve (as measured by R-Squared) and
109
+ # generates a trend line, top and bottom confidence intervals, and optionally projects the trend
110
+ # to an artifical ceiling.
111
+ #
112
+ # @param [Array] data
113
+ # An array of arrays [[X, Y]..] representing the data set you want to curve fit.
114
+ #
115
+ # @param [Number, nil] ceiling
116
+ # The integer/float Y value to project the curve up to, the 'ceiling'. Nil
117
+ # means no projection past last X value. Default is nil.
118
+ #
119
+ # @param [Array] guess_list
120
+ # A list of acceptable guesses to use in cfityk. As many of
121
+ # the following as desired: Linear, Quadratic. Default is all of the above.
122
+ #
123
+ # @param [Block] x_transform
124
+ # A block that will be passed a value for X from the original
125
+ # data set as an integer (1,2,3, etc), and should return a value to replace
126
+ # it with that matches the original data set.
127
+ #
128
+ # @return [Hash] A hash with the data, trend, top_confidence, and bottom_confidence as arrays of [X, Y], r_square and the guessed curve.
129
+ #
130
+ # {
131
+ # :data => [ [ X, Y ], [ X, Y ] ... ],
132
+ # :trend => [ [ X, Y ], [ X, Y ] ... ],
133
+ # :top_confidence => [ [ X, Y ], [X, Y] ...],
134
+ # :bottom_confidence => [ [ X, Y ], [X, Y] ...],
135
+ # :ceiling => [ [ X, Y ], [ X, Y ] ],
136
+ # :r_squared => 99.9764,
137
+ # :guess => "Quadratic"
138
+ # }
139
+ #
140
+ def fit(data, ceiling=nil, guess_list=["Linear", "Quadratic"], &x_transform)
141
+ data_file = Tempfile.new("curvefit")
142
+ x_pos = 0
143
+ data.each do |point|
144
+ data_file.puts("#{x_pos} #{point[1]}")
145
+ x_pos += 1
146
+ end
147
+ data_file.close
148
+
149
+ guess_data = Hash.new
150
+
151
+ guess_list.each do |shape|
152
+ guess_data[shape] = Hash.new
153
+ puts "Guessing #{shape} fit..." if @debug
154
+ IO.popen("cfityk -I -q -c '@0 < '#{data_file.path}'; guess #{shape}; fit; info+ formula in @0; info fit in @0; info errors in @0;'") do |fityk_output|
155
+ fityk_output.each_line do |line|
156
+ puts "#{shape}: #{line}" if @debug
157
+ case line
158
+ when /R-squared = (.+)/
159
+ guess_data[shape][:r_squared] = $1.to_f
160
+ when /(.+) \+ (.+) \* \(x\)/ # 692.1 + 30.633 * (x), linear fit formula
161
+ first = $1.to_f
162
+ second = $2.to_f
163
+ guess_data[shape][:curve_formula] = lambda { |x| first + second * x.to_f }
164
+ when /(.+) \+ (.+)\*\(x\) \+ (.+)\*\(x\)\^2/ # 1019.43 + 9.543*(x) + 0.202086*(x)^2, quadratic/polynomial fit formula
165
+ first = $1.to_f
166
+ second = $2.to_f
167
+ third = $3.to_f
168
+ guess_data[shape][:curve_forumla_args] = {
169
+ 1 => first,
170
+ 2 => second,
171
+ 3 => third
172
+ }
173
+ guess_data[shape][:curve_formula] = lambda { |x| first + second * x.to_f + third * x.to_f**2 }
174
+ when /\$_(\d) = (\d+\.\d+) \+\- (\d+\.\d+)/ # $_1 = 692.1 +- 32.0558
175
+ guess_data[shape][:curve_error_args] ||= Hash.new
176
+ guess_data[shape][:curve_error_args][$1.to_i] = [ $2.to_f, $3.to_f ]
177
+ end
178
+ end
179
+ end
180
+
181
+ if $?.exitstatus != 0
182
+ raise "cfityk returned status #{$?.exitstatus} when guessing #{shape}, bailing"
183
+ end
184
+
185
+ if guess_data[shape][:r_squared] == 1
186
+ guess_data[shape][:top_confidence_formula] = guess_data[shape][:curve_formula]
187
+ guess_data[shape][:bottom_confidence_formula] = guess_data[shape][:curve_formula]
188
+ else
189
+ case shape
190
+ when "Quadratic"
191
+ curve_error_args = guess_data[shape][:curve_error_args]
192
+ guess_data[shape][:top_confidence_formula] = lambda { |x| (curve_error_args[1][0] + curve_error_args[1][1]) + (curve_error_args[2][0] + curve_error_args[2][1]) * x.to_f + (curve_error_args[3][0] + + curve_error_args[3][1]) * x.to_f**2 }
193
+ guess_data[shape][:bottom_confidence_formula] = lambda { |x| (curve_error_args[1][0] - curve_error_args[1][1]) + (curve_error_args[2][0] - curve_error_args[2][1]) * x.to_f + (curve_error_args[3][0] + - curve_error_args[3][1]) * x.to_f**2 }
194
+ when "Linear"
195
+ curve_error_args = guess_data[shape][:curve_error_args]
196
+ guess_data[shape][:top_confidence_formula] = lambda { |x| (curve_error_args[1][0] + curve_error_args[1][1]) + (curve_error_args[2][0] + curve_error_args[2][1]) * x.to_f }
197
+ guess_data[shape][:bottom_confidence_formula] = lambda { |x| (curve_error_args[1][0] - curve_error_args[1][1]) + (curve_error_args[2][0] - curve_error_args[2][1]) * x.to_f }
198
+ end
199
+ end
200
+ end
201
+
202
+ best_fit_name = nil
203
+ best_fit = nil
204
+ guess_data.each do |shape, shape_guess|
205
+ best_fit_name ||= shape
206
+ best_fit ||= shape_guess
207
+ if shape_guess[:r_squared] > best_fit[:r_squared]
208
+ best_fit = shape_guess
209
+ best_fit_name = shape
210
+ end
211
+ end
212
+
213
+ trend_line = []
214
+ top_confidence_line = []
215
+ bottom_confidence_line = []
216
+ ceiling_line = []
217
+
218
+ x = 0
219
+ y = 0
220
+
221
+ no_ceiling = ceiling.nil?
222
+
223
+ while(no_ceiling ? x < data.length : ceiling >= y)
224
+ y = best_fit[:curve_formula].call(x)
225
+ y_top_confidence = best_fit[:top_confidence_formula].call(x)
226
+ y_bottom_confidence = best_fit[:bottom_confidence_formula].call(x)
227
+
228
+ if x_transform
229
+ trend_line << [ x_transform.call(x), y ]
230
+ top_confidence_line << [ x_transform.call(x), y_top_confidence ]
231
+ bottom_confidence_line << [ x_transform.call(x), y_bottom_confidence ]
232
+ ceiling_line << [ x_transform.call(x), ceiling ] unless no_ceiling
233
+ else
234
+ trend_line << [ x, y ]
235
+ top_confidence_line << [ x, y_top_confidence ]
236
+ bottom_confidence_line << [ x, y_bottom_confidence ]
237
+ ceiling_line << [ x, ceiling ] unless no_ceiling
238
+ end
239
+
240
+ x += 1
241
+ end
242
+
243
+ {
244
+ :data => data,
245
+ :trend => trend_line,
246
+ :top_confidence => top_confidence_line,
247
+ :bottom_confidence => bottom_confidence_line,
248
+ :ceiling => ceiling_line,
249
+ :r_squared => best_fit[:r_squared],
250
+ :guess => best_fit_name
251
+ }
252
+ end
253
+
254
+ end
@@ -0,0 +1,131 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'tempfile'
3
+
4
+ describe "CurveFit" do
5
+ PERFECTLY_LINEAR_DATA = [
6
+ [ 0, 1000.0 ],
7
+ [ 1, 2000.0 ],
8
+ [ 2, 3000.0 ],
9
+ [ 3, 4000.0 ],
10
+ [ 4, 5000.0 ],
11
+ [ 5, 6000.0 ]
12
+ ]
13
+
14
+ LINEAR_DATA = [
15
+ [ 0, 1000.0 ],
16
+ [ 1, 2003.0 ],
17
+ [ 2, 3010.0 ],
18
+ [ 3, 4084.0 ],
19
+ [ 4, 5012.0 ],
20
+ [ 5, 6075.0 ]
21
+ ]
22
+
23
+ QUADRATIC_DATA = [[0, 1024.0], [1, 1038.0], [2, 1054.0], [3, 1062.0], [4, 1070.0], [5, 1074.0], [6, 1077.0], [7, 1085.0], [8, 1107.0], [9, 1146.0], [10, 1133.0], [11, 1145.0], [12, 1142.0], [13, 1161.0], [14, 1183.0], [15, 1172.0], [16, 1194.0], [17, 1219.0], [18, 1280.0], [19, 1301.0], [20, 1303.0], [21, 1332.0], [22, 1391.0], [23, 1338.0], [24, 1368.0], [25, 1383.0], [26, 1414.0], [27, 1417.0], [28, 1476.0], [29, 1489.0], [30, 1518.0], [31, 1557.0], [32, 1555.0], [33, 1472.0], [34, 1481.0], [35, 1432.0], [36, 1451.0], [37, 1661.0], [38, 1741.0], [39, 1769.0], [40, 1797.0], [41, 1802.0], [42, 1803.0], [43, 1854.0], [44, 1869.0], [45, 1886.0], [46, 1947.0], [47, 2002.0], [48, 1950.0], [49, 2021.0], [50, 2022.0], [51, 2049.0], [52, 2060.0], [53, 2096.0], [54, 2106.0], [55, 2140.0], [56, 2159.0], [57, 2181.0], [58, 2158.0], [59, 2215.0], [60, 2292.0], [61, 2302.0], [62, 2311.0], [63, 2387.0], [64, 2480.0], [65, 2491.0], [66, 2497.0], [67, 2515.0], [68, 2534.0], [69, 2561.0], [70, 2636.0], [71, 2697.0], [72, 2818.0], [73, 2831.0], [74, 2855.0], [75, 2900.0], [76, 2973.0], [77, 2996.0], [78, 3053.0], [79, 3093.0], [80, 3104.0], [81, 3033.0], [82, 3066.0], [83, 3094.0], [84, 3165.0], [85, 3263.0], [86, 3306.0], [87, 3334.0], [88, 3457.0], [89, 3501.0], [90, 3588.0], [91, 3739.0], [92, 3778.0], [93, 3829.0], [94, 3829.0], [95, 3812.0], [96, 3846.0], [97, 3933.0], [98, 3989.0], [99, 4030.0], [100, 4041.0], [101, 4054.0], [102, 4069.0], [103, 4101.0], [104, 4130.0], [105, 4292.0], [106, 4361.0], [107, 4373.0], [108, 4405.0], [109, 4433.0], [110, 4487.0], [111, 4524.0], [112, 4605.0], [113, 4645.0], [114, 4645.0], [115, 4668.0], [116, 4752.0], [117, 4782.0]]
24
+
25
+
26
+ before :each do
27
+ @cf = CurveFit.new(false)
28
+ end
29
+
30
+ describe "write_xy_file" do
31
+ it "should write to a tempfile" do
32
+ data_file = @cf.write_xy_file(nil, PERFECTLY_LINEAR_DATA)
33
+ File.exists?(data_file.path).should == true
34
+ end
35
+
36
+ it "should write to a file" do
37
+ tf = Tempfile.new("curve-fit-funtimes")
38
+ tf.close
39
+ filename = tf.path
40
+ tf.unlink
41
+ data_file = @cf.write_xy_file(filename, PERFECTLY_LINEAR_DATA)
42
+ File.exists?(filename).should == true
43
+ File.unlink(filename)
44
+ end
45
+ end
46
+
47
+ describe "load_xy_file" do
48
+ it "should read from an XY file" do
49
+ data_file = @cf.write_xy_file(nil, PERFECTLY_LINEAR_DATA)
50
+ read_data = @cf.load_xy_file(data_file)
51
+ read_data.should == PERFECTLY_LINEAR_DATA
52
+ end
53
+ end
54
+
55
+ describe "append_xy_file" do
56
+ it "should add a new value to an XY file" do
57
+ tf = Tempfile.new("curve-fit-funtimes")
58
+ tf.close
59
+ filename = tf.path
60
+ tf.unlink
61
+ data_file = @cf.write_xy_file(filename, PERFECTLY_LINEAR_DATA)
62
+ @cf.append_xy_file(filename, 6, 7000.0)
63
+ appended_data = @cf.load_xy_file(filename)
64
+ appended_data[6].should == [ 6, 7000.0 ]
65
+ end
66
+ end
67
+
68
+ describe "fit" do
69
+ describe "for linear data" do
70
+ it "should guess linear" do
71
+ @cf.fit(PERFECTLY_LINEAR_DATA)[:guess].should == "Linear"
72
+ end
73
+
74
+ end
75
+
76
+ describe "for quadratic data" do
77
+ it "should guess quadratic" do
78
+ @cf.fit(QUADRATIC_DATA)[:guess].should == "Quadratic"
79
+ end
80
+ end
81
+
82
+ describe "ceilings" do
83
+ describe "when given" do
84
+ before :each do
85
+ @fit = @cf.fit(PERFECTLY_LINEAR_DATA, 10000)
86
+ end
87
+
88
+ it "should be projected to" do
89
+ @fit[:data].length.should == 6
90
+ @fit[:trend].length.should == 11
91
+ @fit[:top_confidence].length.should == 11
92
+ @fit[:bottom_confidence].length.should == 11
93
+ end
94
+
95
+ it "should generate a ceiling line as long as the trend line" do
96
+ @fit[:ceiling].length.should == @fit[:trend].length
97
+ end
98
+
99
+ it "should always have the y value of the ceiling" do
100
+ @fit[:ceiling].each { |tuple| tuple[1].should == 10000 }
101
+ end
102
+ end
103
+
104
+ describe "when nil" do
105
+ it "should have an empty ceiling line" do
106
+ fit = @cf.fit(PERFECTLY_LINEAR_DATA)
107
+ fit[:ceiling].should == []
108
+ end
109
+ end
110
+ end
111
+
112
+ describe "when r-squared is 1.0" do
113
+ it "should re-use the fit formula for the trend and confidence intervals" do
114
+ linear_fit = @cf.fit(PERFECTLY_LINEAR_DATA)
115
+ linear_fit[:data].should == linear_fit[:trend]
116
+ linear_fit[:data].should == linear_fit[:top_confidence]
117
+ linear_fit[:data].should == linear_fit[:bottom_confidence]
118
+ end
119
+ end
120
+
121
+ describe "when r-squared is not 1.0" do
122
+ it "should generate different trend and confidence intervals" do
123
+ linear_fit = @cf.fit(LINEAR_DATA)
124
+ linear_fit[:data].should_not == linear_fit[:trend]
125
+ linear_fit[:data].should_not == linear_fit[:top_confidence]
126
+ linear_fit[:data].should_not == linear_fit[:bottom_confidence]
127
+ end
128
+ end
129
+ end
130
+
131
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'curve_fit'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: curve_fit
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Adam Jacob
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-04 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :development
23
+ prerelease: false
24
+ name: rspec
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 31
31
+ segments:
32
+ - 2
33
+ - 4
34
+ - 0
35
+ version: 2.4.0
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :development
39
+ prerelease: false
40
+ name: yard
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ hash: 7
47
+ segments:
48
+ - 0
49
+ - 6
50
+ - 0
51
+ version: 0.6.0
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ type: :development
55
+ prerelease: false
56
+ name: bundler
57
+ version_requirements: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 23
63
+ segments:
64
+ - 1
65
+ - 0
66
+ - 0
67
+ version: 1.0.0
68
+ requirement: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ type: :development
71
+ prerelease: false
72
+ name: jeweler
73
+ version_requirements: &id004 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ hash: 7
79
+ segments:
80
+ - 1
81
+ - 5
82
+ - 2
83
+ version: 1.5.2
84
+ requirement: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ type: :development
87
+ prerelease: false
88
+ name: rcov
89
+ version_requirements: &id005 !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirement: *id005
99
+ description: Creates curve fitting and confidence intervals from cfityk
100
+ email: adam@opscode.com
101
+ executables: []
102
+
103
+ extensions: []
104
+
105
+ extra_rdoc_files:
106
+ - LICENSE.txt
107
+ - README.rdoc
108
+ files:
109
+ - .document
110
+ - .rspec
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.rdoc
114
+ - Rakefile
115
+ - VERSION
116
+ - lib/curve_fit.rb
117
+ - spec/curve_fit_spec.rb
118
+ - spec/spec_helper.rb
119
+ has_rdoc: true
120
+ homepage: http://github.com/adamhjk/curve_fit
121
+ licenses:
122
+ - Apache 2.0
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project:
149
+ rubygems_version: 1.3.7
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Wrapper for curve fitting and confidence around cfityk
153
+ test_files:
154
+ - spec/curve_fit_spec.rb
155
+ - spec/spec_helper.rb