linear_regression_trend 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +8 -0
- data/.gitignore +1 -0
- data/README.md +17 -4
- data/doc/Linear regression playground.ipynb +173 -16
- data/lib/linear_regression_trend.rb +7 -3
- data/lib/linear_regression_trend/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0886ac85ef56b725b1deaa5d4ca3331d4a981ee8
|
4
|
+
data.tar.gz: f3e224e216a4cc89524c2fd27b44a28e139f9b8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 733341279c21804fc37579789fd52f56bcf69d606e6dc2f117321f6dff3eb5673f3a500a0578c325388e700ad9a96ac59095685576e6e24ab66bef66a44512e9
|
7
|
+
data.tar.gz: 4ecc8929a43d05fa0c67e46ce45d1ce2ee2690e7ffdf0d9a75e098e80ed5136caa67b108da641ca4b7fbdfabd4e9653d49291ae2bf300ffeb2feeac093c49707
|
data/.codeclimate.yml
ADDED
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Linear Regression Trend Calculator [![Build Status](https://travis-ci.org/zero-one-software/linear_regression_trend.svg)](https://travis-ci.org/zero-one-software/linear_regression_trend)
|
1
|
+
# Linear Regression Trend Calculator [![Build Status](https://travis-ci.org/zero-one-software/linear_regression_trend.svg)](https://travis-ci.org/zero-one-software/linear_regression_trend) [![Code Climate](https://codeclimate.com/github/zero-one-software/linear_regression_trend/badges/gpa.svg)](https://codeclimate.com/github/zero-one-software/linear_regression_trend)
|
2
2
|
|
3
3
|
This is a simple gem to help calculate linear regression trendlines for a given list of samples.
|
4
4
|
|
@@ -22,7 +22,7 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
$ gem install linear_regression_trend
|
24
24
|
|
25
|
-
## Usage
|
25
|
+
## Usage: Standard usage
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
my_array_of_samples = [10, 14, 18, 22, 26]
|
@@ -33,9 +33,22 @@ trend_slope = trender.slope # => 4
|
|
33
33
|
trend_intercept = trender.intercept # => 6
|
34
34
|
```
|
35
35
|
|
36
|
-
##
|
36
|
+
## Usage: non-negative trending
|
37
37
|
|
38
|
-
|
38
|
+
Sometimes you want a trend, and that trend happens to be a descending one. Normally this is fine. However there are instances where that trend calculation can go into the negative. Again, sometimes this is fine, but sometimes this doesnt make sense, e.g. packet loss, or latency on a network can never be less than zero (would be cool though). In cases like this, you'd wanna tell the library to stop trending past zero. Observe:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
my_array_of_samples = [1,2,3,0,10,18,18,18,1,1,1,1,0.5,0.5,0.5,0.5,05,0.25,0.25,0.1,0.1,0,0,0,0]
|
42
|
+
trender = LinearRegressionTrend::Calculator.new(my_array_of_samples, non_negative: true)
|
43
|
+
|
44
|
+
trended_samples = trender.trend # => [array of trend points]
|
45
|
+
trend_slope = trender.slope # => a negative number, indicating a descending trend
|
46
|
+
trended_samples.last == 0 # => true, would've be -0.8927 otherwise
|
47
|
+
```
|
48
|
+
|
49
|
+
## Development and extra documentation
|
50
|
+
|
51
|
+
Check out the tests in the /spec dir, and also install iRuby notebook, and **take a look at the live documentation in /doc.**
|
39
52
|
|
40
53
|
## Contributing
|
41
54
|
|
@@ -11,18 +11,60 @@
|
|
11
11
|
},
|
12
12
|
{
|
13
13
|
"cell_type": "code",
|
14
|
-
"execution_count":
|
14
|
+
"execution_count": 1,
|
15
15
|
"metadata": {
|
16
16
|
"collapsed": false
|
17
17
|
},
|
18
18
|
"outputs": [
|
19
|
+
{
|
20
|
+
"data": {
|
21
|
+
"application/javascript": [
|
22
|
+
"if(window['d3'] === undefined ||\n",
|
23
|
+
" window['Nyaplot'] === undefined){\n",
|
24
|
+
" var path = {\"d3\":\"http://d3js.org/d3.v3.min\",\"downloadable\":\"http://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n",
|
25
|
+
"\n",
|
26
|
+
"\n",
|
27
|
+
"\n",
|
28
|
+
" var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n",
|
29
|
+
"\n",
|
30
|
+
" require.config({paths: path, shim:shim});\n",
|
31
|
+
"\n",
|
32
|
+
"\n",
|
33
|
+
"require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n",
|
34
|
+
"\n",
|
35
|
+
"\tvar script = d3.select(\"head\")\n",
|
36
|
+
"\t .append(\"script\")\n",
|
37
|
+
"\t .attr(\"src\", \"http://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n",
|
38
|
+
"\t .attr(\"async\", true);\n",
|
39
|
+
"\n",
|
40
|
+
"\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n",
|
41
|
+
"\n",
|
42
|
+
"\n",
|
43
|
+
"\t var event = document.createEvent(\"HTMLEvents\");\n",
|
44
|
+
"\t event.initEvent(\"load_nyaplot\",false,false);\n",
|
45
|
+
"\t window.dispatchEvent(event);\n",
|
46
|
+
"\t console.log('Finished loading Nyaplotjs');\n",
|
47
|
+
"\n",
|
48
|
+
"\t};\n",
|
49
|
+
"\n",
|
50
|
+
"\n",
|
51
|
+
"});});\n",
|
52
|
+
"}\n"
|
53
|
+
],
|
54
|
+
"text/plain": [
|
55
|
+
"\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"http://d3js.org/d3.v3.min\\\",\\\"downloadable\\\":\\\"http://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"http://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\""
|
56
|
+
]
|
57
|
+
},
|
58
|
+
"metadata": {},
|
59
|
+
"output_type": "display_data"
|
60
|
+
},
|
19
61
|
{
|
20
62
|
"data": {
|
21
63
|
"text/plain": [
|
22
|
-
"
|
64
|
+
"true"
|
23
65
|
]
|
24
66
|
},
|
25
|
-
"execution_count":
|
67
|
+
"execution_count": 1,
|
26
68
|
"metadata": {},
|
27
69
|
"output_type": "execute_result"
|
28
70
|
}
|
@@ -56,7 +98,7 @@
|
|
56
98
|
},
|
57
99
|
{
|
58
100
|
"cell_type": "code",
|
59
|
-
"execution_count":
|
101
|
+
"execution_count": 2,
|
60
102
|
"metadata": {
|
61
103
|
"collapsed": false,
|
62
104
|
"scrolled": true
|
@@ -93,7 +135,7 @@
|
|
93
135
|
},
|
94
136
|
{
|
95
137
|
"cell_type": "code",
|
96
|
-
"execution_count":
|
138
|
+
"execution_count": 3,
|
97
139
|
"metadata": {
|
98
140
|
"collapsed": false,
|
99
141
|
"scrolled": true
|
@@ -102,12 +144,12 @@
|
|
102
144
|
{
|
103
145
|
"data": {
|
104
146
|
"text/html": [
|
105
|
-
"<div id='vis-
|
147
|
+
"<div id='vis-15c391e9-f571-411f-83c9-8da90be28ce1'></div>\n",
|
106
148
|
"<script>\n",
|
107
149
|
"(function(){\n",
|
108
150
|
" var render = function(){\n",
|
109
|
-
" var model = {\"panes\":[{\"diagrams\":[{\"type\":\"bar\",\"options\":{\"x\":\"data0\",\"y\":\"data1\"},\"data\":\"
|
110
|
-
" var id_name = '#vis-
|
151
|
+
" var model = {\"panes\":[{\"diagrams\":[{\"type\":\"bar\",\"options\":{\"x\":\"data0\",\"y\":\"data1\"},\"data\":\"f4ab127e-0b11-41e9-9e1f-7ef326bd0021\"}],\"options\":{\"width\":700,\"xrange\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\",\"13\",\"14\",\"15\",\"16\",\"17\",\"18\",\"19\",\"20\",\"21\",\"22\",\"23\",\"24\",\"25\"],\"yrange\":[0,18]}}],\"data\":{\"f4ab127e-0b11-41e9-9e1f-7ef326bd0021\":[{\"data0\":\"1\",\"data1\":1},{\"data0\":\"2\",\"data1\":2},{\"data0\":\"3\",\"data1\":3},{\"data0\":\"4\",\"data1\":6},{\"data0\":\"5\",\"data1\":10},{\"data0\":\"6\",\"data1\":11},{\"data0\":\"7\",\"data1\":12},{\"data0\":\"8\",\"data1\":12},{\"data0\":\"9\",\"data1\":12},{\"data0\":\"10\",\"data1\":12},{\"data0\":\"11\",\"data1\":15},{\"data0\":\"12\",\"data1\":18},{\"data0\":\"13\",\"data1\":18},{\"data0\":\"14\",\"data1\":16},{\"data0\":\"15\",\"data1\":16},{\"data0\":\"16\",\"data1\":15},{\"data0\":\"17\",\"data1\":14},{\"data0\":\"18\",\"data1\":11},{\"data0\":\"19\",\"data1\":9},{\"data0\":\"20\",\"data1\":9},{\"data0\":\"21\",\"data1\":7},{\"data0\":\"22\",\"data1\":4},{\"data0\":\"23\",\"data1\":2},{\"data0\":\"24\",\"data1\":1},{\"data0\":\"25\",\"data1\":1}]},\"extension\":[]}\n",
|
152
|
+
" var id_name = '#vis-15c391e9-f571-411f-83c9-8da90be28ce1';\n",
|
111
153
|
" Nyaplot.core.parse(model, id_name);\n",
|
112
154
|
"\n",
|
113
155
|
" require(['downloadable'], function(downloadable){\n",
|
@@ -126,7 +168,7 @@
|
|
126
168
|
"</script>\n"
|
127
169
|
],
|
128
170
|
"text/plain": [
|
129
|
-
"#<Nyaplot::Frame:
|
171
|
+
"#<Nyaplot::Frame:0x007ff6544286f8 @properties={:panes=>[#<Nyaplot::Plot:0x007ff65442aca0 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007ff654428ba8 @properties={:type=>:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"f4ab127e-0b11-41e9-9e1f-7ef326bd0021\"}, @xrange=[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\"], @yrange=[0, 18]>], :options=>{:width=>700, :xrange=>[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\"], :yrange=>[0, 18]}}>], :data=>{\"f4ab127e-0b11-41e9-9e1f-7ef326bd0021\"=>#<Nyaplot::DataFrame:0x007ff65442a778 @name=\"f4ab127e-0b11-41e9-9e1f-7ef326bd0021\", @rows=[{:data0=>\"1\", :data1=>1}, {:data0=>\"2\", :data1=>2}, {:data0=>\"3\", :data1=>3}, {:data0=>\"4\", :data1=>6}, {:data0=>\"5\", :data1=>10}, {:data0=>\"6\", :data1=>11}, {:data0=>\"7\", :data1=>12}, {:data0=>\"8\", :data1=>12}, {:data0=>\"9\", :data1=>12}, {:data0=>\"10\", :data1=>12}, {:data0=>\"11\", :data1=>15}, {:data0=>\"12\", :data1=>18}, {:data0=>\"13\", :data1=>18}, {:data0=>\"14\", :data1=>16}, {:data0=>\"15\", :data1=>16}, {:data0=>\"16\", :data1=>15}, {:data0=>\"17\", :data1=>14}, {:data0=>\"18\", :data1=>11}, {:data0=>\"19\", :data1=>9}, {:data0=>\"20\", :data1=>9}, {:data0=>\"21\", :data1=>7}, {:data0=>\"22\", :data1=>4}, {:data0=>\"23\", :data1=>2}, {:data0=>\"24\", :data1=>1}, {:data0=>\"25\", :data1=>1}]>}, :extension=>[]}>"
|
130
172
|
]
|
131
173
|
},
|
132
174
|
"metadata": {},
|
@@ -148,7 +190,62 @@
|
|
148
190
|
},
|
149
191
|
{
|
150
192
|
"cell_type": "code",
|
151
|
-
"execution_count":
|
193
|
+
"execution_count": 4,
|
194
|
+
"metadata": {
|
195
|
+
"collapsed": false,
|
196
|
+
"scrolled": true
|
197
|
+
},
|
198
|
+
"outputs": [
|
199
|
+
{
|
200
|
+
"data": {
|
201
|
+
"text/html": [
|
202
|
+
"<div id='vis-77606c3f-45ec-4d9d-83f4-d387935bb1a5'></div>\n",
|
203
|
+
"<script>\n",
|
204
|
+
"(function(){\n",
|
205
|
+
" var render = function(){\n",
|
206
|
+
" var model = {\"panes\":[{\"diagrams\":[{\"type\":\"bar\",\"options\":{\"x\":\"data0\",\"y\":\"data1\"},\"data\":\"ea7a4bcf-0823-4c0b-b7af-e05a762e8b60\"}],\"options\":{\"width\":700,\"xrange\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\",\"13\",\"14\",\"15\",\"16\",\"17\",\"18\",\"19\",\"20\",\"21\",\"22\",\"23\",\"24\",\"25\"],\"yrange\":[0,10.246153846153847]}}],\"data\":{\"ea7a4bcf-0823-4c0b-b7af-e05a762e8b60\":[{\"data0\":\"1\",\"data1\":10.246153846153847},{\"data0\":\"2\",\"data1\":10.182307692307694},{\"data0\":\"3\",\"data1\":10.118461538461538},{\"data0\":\"4\",\"data1\":10.054615384615385},{\"data0\":\"5\",\"data1\":9.990769230769232},{\"data0\":\"6\",\"data1\":9.926923076923078},{\"data0\":\"7\",\"data1\":9.863076923076923},{\"data0\":\"8\",\"data1\":9.79923076923077},{\"data0\":\"9\",\"data1\":9.735384615384616},{\"data0\":\"10\",\"data1\":9.671538461538463},{\"data0\":\"11\",\"data1\":9.607692307692307},{\"data0\":\"12\",\"data1\":9.543846153846154},{\"data0\":\"13\",\"data1\":9.48},{\"data0\":\"14\",\"data1\":9.416153846153847},{\"data0\":\"15\",\"data1\":9.352307692307694},{\"data0\":\"16\",\"data1\":9.288461538461538},{\"data0\":\"17\",\"data1\":9.224615384615385},{\"data0\":\"18\",\"data1\":9.160769230769231},{\"data0\":\"19\",\"data1\":9.096923076923078},{\"data0\":\"20\",\"data1\":9.033076923076923},{\"data0\":\"21\",\"data1\":8.96923076923077},{\"data0\":\"22\",\"data1\":8.905384615384616},{\"data0\":\"23\",\"data1\":8.841538461538462},{\"data0\":\"24\",\"data1\":8.777692307692309},{\"data0\":\"25\",\"data1\":8.713846153846154}]},\"extension\":[]}\n",
|
207
|
+
" var id_name = '#vis-77606c3f-45ec-4d9d-83f4-d387935bb1a5';\n",
|
208
|
+
" Nyaplot.core.parse(model, id_name);\n",
|
209
|
+
"\n",
|
210
|
+
" require(['downloadable'], function(downloadable){\n",
|
211
|
+
" var svg = d3.select(id_name).select(\"svg\");\n",
|
212
|
+
"\t if(!svg.empty())\n",
|
213
|
+
"\t svg.call(downloadable().filename('fig'));\n",
|
214
|
+
"\t});\n",
|
215
|
+
" };\n",
|
216
|
+
" if(window['Nyaplot']==undefined){\n",
|
217
|
+
" window.addEventListener('load_nyaplot', render, false);\n",
|
218
|
+
"\treturn;\n",
|
219
|
+
" } else {\n",
|
220
|
+
" render();\n",
|
221
|
+
" }\n",
|
222
|
+
"})();\n",
|
223
|
+
"</script>\n"
|
224
|
+
],
|
225
|
+
"text/plain": [
|
226
|
+
"#<Nyaplot::Frame:0x007ff653a55c48 @properties={:panes=>[#<Nyaplot::Plot:0x007ff652928790 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007ff653a56080 @properties={:type=>:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"ea7a4bcf-0823-4c0b-b7af-e05a762e8b60\"}, @xrange=[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\"], @yrange=[0, 10.246153846153847]>], :options=>{:width=>700, :xrange=>[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\"], :yrange=>[0, 10.246153846153847]}}>], :data=>{\"ea7a4bcf-0823-4c0b-b7af-e05a762e8b60\"=>#<Nyaplot::DataFrame:0x007ff653a57bb0 @name=\"ea7a4bcf-0823-4c0b-b7af-e05a762e8b60\", @rows=[{:data0=>\"1\", :data1=>10.246153846153847}, {:data0=>\"2\", :data1=>10.182307692307694}, {:data0=>\"3\", :data1=>10.118461538461538}, {:data0=>\"4\", :data1=>10.054615384615385}, {:data0=>\"5\", :data1=>9.990769230769232}, {:data0=>\"6\", :data1=>9.926923076923078}, {:data0=>\"7\", :data1=>9.863076923076923}, {:data0=>\"8\", :data1=>9.79923076923077}, {:data0=>\"9\", :data1=>9.735384615384616}, {:data0=>\"10\", :data1=>9.671538461538463}, {:data0=>\"11\", :data1=>9.607692307692307}, {:data0=>\"12\", :data1=>9.543846153846154}, {:data0=>\"13\", :data1=>9.48}, {:data0=>\"14\", :data1=>9.416153846153847}, {:data0=>\"15\", :data1=>9.352307692307694}, {:data0=>\"16\", :data1=>9.288461538461538}, {:data0=>\"17\", :data1=>9.224615384615385}, {:data0=>\"18\", :data1=>9.160769230769231}, {:data0=>\"19\", :data1=>9.096923076923078}, {:data0=>\"20\", :data1=>9.033076923076923}, {:data0=>\"21\", :data1=>8.96923076923077}, {:data0=>\"22\", :data1=>8.905384615384616}, {:data0=>\"23\", :data1=>8.841538461538462}, {:data0=>\"24\", :data1=>8.777692307692309}, {:data0=>\"25\", :data1=>8.713846153846154}]>}, :extension=>[]}>"
|
227
|
+
]
|
228
|
+
},
|
229
|
+
"metadata": {},
|
230
|
+
"output_type": "display_data"
|
231
|
+
}
|
232
|
+
],
|
233
|
+
"source": [
|
234
|
+
"chart = Nyaplot::Plot.new \n",
|
235
|
+
"chart.add(:bar, x, t.trend)\n",
|
236
|
+
"chart.show"
|
237
|
+
]
|
238
|
+
},
|
239
|
+
{
|
240
|
+
"cell_type": "markdown",
|
241
|
+
"metadata": {},
|
242
|
+
"source": [
|
243
|
+
"# Non-negative descending trends"
|
244
|
+
]
|
245
|
+
},
|
246
|
+
{
|
247
|
+
"cell_type": "code",
|
248
|
+
"execution_count": 13,
|
152
249
|
"metadata": {
|
153
250
|
"collapsed": false
|
154
251
|
},
|
@@ -156,12 +253,41 @@
|
|
156
253
|
{
|
157
254
|
"data": {
|
158
255
|
"text/html": [
|
159
|
-
"<
|
256
|
+
"<table><tr><td>Slope</td><td>Intercept</td><td>Conclusion</td></tr><tr><td>-0.3467307692307691</td><td>7.775499999999997</td><td>0</td></tr></table>"
|
257
|
+
],
|
258
|
+
"text/plain": [
|
259
|
+
"\"<table><tr><td>Slope</td><td>Intercept</td><td>Conclusion</td></tr><tr><td>-0.3467307692307691</td><td>7.775499999999997</td><td>0</td></tr></table>\""
|
260
|
+
]
|
261
|
+
},
|
262
|
+
"metadata": {},
|
263
|
+
"output_type": "display_data"
|
264
|
+
}
|
265
|
+
],
|
266
|
+
"source": [
|
267
|
+
"y = [1,2,3,0,10,18,18,18,1,1,1,1,0.5,0.5,0.5,0.5,05,0.25,0.25,0.1,0.1,0,0,0,0]\n",
|
268
|
+
"x = (1..y.size).to_a.map { |n| n.to_s }\n",
|
269
|
+
"\n",
|
270
|
+
"t = LinearRegressionTrend::Calculator.new(y, non_negative: true)\n",
|
271
|
+
"\n",
|
272
|
+
"IRuby.display IRuby.table([['Slope', 'Intercept', 'Conclusion'], [t.slope, t.intercept, t.trend.last]])"
|
273
|
+
]
|
274
|
+
},
|
275
|
+
{
|
276
|
+
"cell_type": "code",
|
277
|
+
"execution_count": 12,
|
278
|
+
"metadata": {
|
279
|
+
"collapsed": false
|
280
|
+
},
|
281
|
+
"outputs": [
|
282
|
+
{
|
283
|
+
"data": {
|
284
|
+
"text/html": [
|
285
|
+
"<div id='vis-e93ae034-724e-40dd-89ff-8e9e06f30d71'></div>\n",
|
160
286
|
"<script>\n",
|
161
287
|
"(function(){\n",
|
162
288
|
" var render = function(){\n",
|
163
|
-
" var model = {\"panes\":[{\"diagrams\":[{\"type\":\"bar\",\"options\":{\"x\":\"data0\",\"y\":\"data1\"},\"data\":\"
|
164
|
-
" var id_name = '#vis-
|
289
|
+
" var model = {\"panes\":[{\"diagrams\":[{\"type\":\"bar\",\"options\":{\"x\":\"data0\",\"y\":\"data1\"},\"data\":\"759c4fe1-01be-4c0f-bf93-49ab6d168710\"}],\"options\":{\"width\":700,\"xrange\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\",\"13\",\"14\",\"15\",\"16\",\"17\",\"18\",\"19\",\"20\",\"21\",\"22\",\"23\",\"24\",\"25\"],\"yrange\":[-0.8927692307692308,7.428769230769229]}}],\"data\":{\"759c4fe1-01be-4c0f-bf93-49ab6d168710\":[{\"data0\":\"1\",\"data1\":7.428769230769229},{\"data0\":\"2\",\"data1\":7.082038461538459},{\"data0\":\"3\",\"data1\":6.73530769230769},{\"data0\":\"4\",\"data1\":6.388576923076921},{\"data0\":\"5\",\"data1\":6.041846153846151},{\"data0\":\"6\",\"data1\":5.695115384615383},{\"data0\":\"7\",\"data1\":5.348384615384614},{\"data0\":\"8\",\"data1\":5.001653846153845},{\"data0\":\"9\",\"data1\":4.654923076923075},{\"data0\":\"10\",\"data1\":4.308192307692306},{\"data0\":\"11\",\"data1\":3.961461538461537},{\"data0\":\"12\",\"data1\":3.6147307692307677},{\"data0\":\"13\",\"data1\":3.267999999999999},{\"data0\":\"14\",\"data1\":2.92126923076923},{\"data0\":\"15\",\"data1\":2.5745384615384603},{\"data0\":\"16\",\"data1\":2.2278076923076915},{\"data0\":\"17\",\"data1\":1.8810769230769226},{\"data0\":\"18\",\"data1\":1.534346153846153},{\"data0\":\"19\",\"data1\":1.187615384615384},{\"data0\":\"20\",\"data1\":0.8408846153846152},{\"data0\":\"21\",\"data1\":0.4941538461538455},{\"data0\":\"22\",\"data1\":0.14742307692307666},{\"data0\":\"23\",\"data1\":-0.19930769230769219},{\"data0\":\"24\",\"data1\":-0.5460384615384619},{\"data0\":\"25\",\"data1\":-0.8927692307692308}]},\"extension\":[]}\n",
|
290
|
+
" var id_name = '#vis-e93ae034-724e-40dd-89ff-8e9e06f30d71';\n",
|
165
291
|
" Nyaplot.core.parse(model, id_name);\n",
|
166
292
|
"\n",
|
167
293
|
" require(['downloadable'], function(downloadable){\n",
|
@@ -180,7 +306,7 @@
|
|
180
306
|
"</script>\n"
|
181
307
|
],
|
182
308
|
"text/plain": [
|
183
|
-
"#<Nyaplot::Frame:
|
309
|
+
"#<Nyaplot::Frame:0x007ff6544894a8 @properties={:panes=>[#<Nyaplot::Plot:0x007ff65448bc30 @properties={:diagrams=>[#<Nyaplot::Diagram:0x007ff654489bb0 @properties={:type=>:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"759c4fe1-01be-4c0f-bf93-49ab6d168710\"}, @xrange=[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\"], @yrange=[-0.8927692307692308, 7.428769230769229]>], :options=>{:width=>700, :xrange=>[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\"], :yrange=>[-0.8927692307692308, 7.428769230769229]}}>], :data=>{\"759c4fe1-01be-4c0f-bf93-49ab6d168710\"=>#<Nyaplot::DataFrame:0x007ff65448b6e0 @name=\"759c4fe1-01be-4c0f-bf93-49ab6d168710\", @rows=[{:data0=>\"1\", :data1=>7.428769230769229}, {:data0=>\"2\", :data1=>7.082038461538459}, {:data0=>\"3\", :data1=>6.73530769230769}, {:data0=>\"4\", :data1=>6.388576923076921}, {:data0=>\"5\", :data1=>6.041846153846151}, {:data0=>\"6\", :data1=>5.695115384615383}, {:data0=>\"7\", :data1=>5.348384615384614}, {:data0=>\"8\", :data1=>5.001653846153845}, {:data0=>\"9\", :data1=>4.654923076923075}, {:data0=>\"10\", :data1=>4.308192307692306}, {:data0=>\"11\", :data1=>3.961461538461537}, {:data0=>\"12\", :data1=>3.6147307692307677}, {:data0=>\"13\", :data1=>3.267999999999999}, {:data0=>\"14\", :data1=>2.92126923076923}, {:data0=>\"15\", :data1=>2.5745384615384603}, {:data0=>\"16\", :data1=>2.2278076923076915}, {:data0=>\"17\", :data1=>1.8810769230769226}, {:data0=>\"18\", :data1=>1.534346153846153}, {:data0=>\"19\", :data1=>1.187615384615384}, {:data0=>\"20\", :data1=>0.8408846153846152}, {:data0=>\"21\", :data1=>0.4941538461538455}, {:data0=>\"22\", :data1=>0.14742307692307666}, {:data0=>\"23\", :data1=>-0.19930769230769219}, {:data0=>\"24\", :data1=>-0.5460384615384619}, {:data0=>\"25\", :data1=>-0.8927692307692308}]>}, :extension=>[]}>"
|
184
310
|
]
|
185
311
|
},
|
186
312
|
"metadata": {},
|
@@ -192,11 +318,42 @@
|
|
192
318
|
"chart.add(:bar, x, t.trend)\n",
|
193
319
|
"chart.show"
|
194
320
|
]
|
321
|
+
},
|
322
|
+
{
|
323
|
+
"cell_type": "markdown",
|
324
|
+
"metadata": {},
|
325
|
+
"source": [
|
326
|
+
"# It's last few values should be 0, instead of negative"
|
327
|
+
]
|
328
|
+
},
|
329
|
+
{
|
330
|
+
"cell_type": "code",
|
331
|
+
"execution_count": 10,
|
332
|
+
"metadata": {
|
333
|
+
"collapsed": false
|
334
|
+
},
|
335
|
+
"outputs": [
|
336
|
+
{
|
337
|
+
"data": {
|
338
|
+
"text/html": [
|
339
|
+
"<table><tr><td>25</td><td>24</td><td>23</td><td>22</td><td>21</td><td>20</td><td>19</td><td rowspan='2'>…</td><td>8</td><td>7</td><td>6</td><td>5</td><td>4</td><td>3</td><td>2</td></tr><tr><td>0</td><td>0</td><td>0</td><td>0.14742307692307666</td><td>0.4941538461538455</td><td>0.8408846153846152</td><td>1.187615384615384</td><td>5.001653846153845</td><td>5.348384615384614</td><td>5.695115384615383</td><td>6.041846153846151</td><td>6.388576923076921</td><td>6.73530769230769</td><td>7.082038461538459</td></tr></table>"
|
340
|
+
],
|
341
|
+
"text/plain": [
|
342
|
+
"\"<table><tr><td>25</td><td>24</td><td>23</td><td>22</td><td>21</td><td>20</td><td>19</td><td rowspan='2'>…</td><td>8</td><td>7</td><td>6</td><td>5</td><td>4</td><td>3</td><td>2</td></tr><tr><td>0</td><td>0</td><td>0</td><td>0.14742307692307666</td><td>0.4941538461538455</td><td>0.8408846153846152</td><td>1.187615384615384</td><td>5.001653846153845</td><td>5.348384615384614</td><td>5.695115384615383</td><td>6.041846153846151</td><td>6.388576923076921</td><td>6.73530769230769</td><td>7.082038461538459</td></tr></table>\""
|
343
|
+
]
|
344
|
+
},
|
345
|
+
"metadata": {},
|
346
|
+
"output_type": "display_data"
|
347
|
+
}
|
348
|
+
],
|
349
|
+
"source": [
|
350
|
+
"IRuby.display IRuby.table([(1..y.size).to_a.reverse, t.trend.reverse])"
|
351
|
+
]
|
195
352
|
}
|
196
353
|
],
|
197
354
|
"metadata": {
|
198
355
|
"kernelspec": {
|
199
|
-
"display_name": "Ruby 2.
|
356
|
+
"display_name": "Ruby 2.2.2",
|
200
357
|
"language": "ruby",
|
201
358
|
"name": "ruby"
|
202
359
|
},
|
@@ -204,7 +361,7 @@
|
|
204
361
|
"file_extension": "rb",
|
205
362
|
"mimetype": "application/x-ruby",
|
206
363
|
"name": "ruby",
|
207
|
-
"version": "2.
|
364
|
+
"version": "2.2.2"
|
208
365
|
}
|
209
366
|
},
|
210
367
|
"nbformat": 4,
|
@@ -11,10 +11,11 @@ module LinearRegressionTrend
|
|
11
11
|
attr_accessor :slope, :intercept
|
12
12
|
|
13
13
|
# Pass in an array of values to get the regression on
|
14
|
-
def initialize
|
14
|
+
def initialize(y_values, non_negative: false)
|
15
15
|
@y_values = y_values
|
16
16
|
@size = @y_values.size
|
17
17
|
@x_values = (1..@size).to_a
|
18
|
+
@no_negs = non_negative
|
18
19
|
|
19
20
|
#initialize everything to 0
|
20
21
|
sum_x = 0
|
@@ -43,8 +44,11 @@ module LinearRegressionTrend
|
|
43
44
|
# Get the Y value for any given X value
|
44
45
|
# from y = mx + b, or
|
45
46
|
# y = slope * x + intercept
|
46
|
-
def predict(
|
47
|
-
@slope * x + @intercept
|
47
|
+
def predict(x)
|
48
|
+
predicted = @slope * x + @intercept
|
49
|
+
|
50
|
+
return 0 if predicted < 0 and @no_negs
|
51
|
+
return predicted
|
48
52
|
end
|
49
53
|
|
50
54
|
# Get the "next" value if the sequence
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linear_regression_trend
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Fortuna
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -130,6 +130,7 @@ executables: []
|
|
130
130
|
extensions: []
|
131
131
|
extra_rdoc_files: []
|
132
132
|
files:
|
133
|
+
- ".codeclimate.yml"
|
133
134
|
- ".gitignore"
|
134
135
|
- ".rspec"
|
135
136
|
- ".travis.yml"
|