analytica 0.0.2 → 0.0.3
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/Manifest.txt +2 -0
- data/lib/analytica.rb +1 -1
- data/lib/analytica_comp.rb +99 -0
- data/lib/analytica_viz.rb +61 -0
- metadata +6 -4
data/Manifest.txt
CHANGED
data/lib/analytica.rb
CHANGED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'typestrict'
|
2
|
+
|
3
|
+
module Computation
|
4
|
+
include Strict
|
5
|
+
|
6
|
+
def sum
|
7
|
+
sum = inject { |sum, x| sum + x }
|
8
|
+
sum ? sum : 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def mean
|
12
|
+
sum.to_f / size
|
13
|
+
end
|
14
|
+
|
15
|
+
def lma(params)
|
16
|
+
linear_moving_average(params)
|
17
|
+
end
|
18
|
+
|
19
|
+
def linear_moving_average(params)
|
20
|
+
enforce_map!({
|
21
|
+
:bias => [:last, :first],
|
22
|
+
:samples => :integer}, params)
|
23
|
+
|
24
|
+
data = []
|
25
|
+
case params[:bias]
|
26
|
+
when :last
|
27
|
+
data = self.reverse
|
28
|
+
when :first
|
29
|
+
data = self
|
30
|
+
else
|
31
|
+
raise "Bias not legally set!"
|
32
|
+
end
|
33
|
+
|
34
|
+
raise "too few samples available to calculate lma at given :samples input" if params[:samples] > size
|
35
|
+
|
36
|
+
n = params[:samples] + 1
|
37
|
+
numerator = 0.0
|
38
|
+
denominator = 0.0
|
39
|
+
|
40
|
+
data.each do |sample|
|
41
|
+
n -= 1
|
42
|
+
n = n > 0 ? n : 0
|
43
|
+
|
44
|
+
numerator += n*sample
|
45
|
+
denominator += n
|
46
|
+
end
|
47
|
+
numerator / denominator
|
48
|
+
end
|
49
|
+
|
50
|
+
def ema(params)
|
51
|
+
exponential_moving_average(params)
|
52
|
+
end
|
53
|
+
|
54
|
+
def exponential_moving_average(params)
|
55
|
+
enforce_map!({
|
56
|
+
:decay => [:exponential, :linear],
|
57
|
+
:decay_bias => [:latest, :oldest],
|
58
|
+
:decay_coefficent => :float}, params)
|
59
|
+
|
60
|
+
if params[:decay_bias] == :latest
|
61
|
+
data = self
|
62
|
+
else
|
63
|
+
data = self.reverse
|
64
|
+
end
|
65
|
+
0.0
|
66
|
+
end
|
67
|
+
|
68
|
+
def dydx(n=1)
|
69
|
+
piecewise_derivative(n)
|
70
|
+
end
|
71
|
+
|
72
|
+
def piecewise_derivative(n=1)
|
73
|
+
enforce!(:natural_number, n)
|
74
|
+
|
75
|
+
d = self
|
76
|
+
n.times do
|
77
|
+
d = d.inject([]) do |result, item|
|
78
|
+
if result.size == 0
|
79
|
+
result << item
|
80
|
+
else
|
81
|
+
d_y = (item - result.last).to_f
|
82
|
+
d_x = 1.0 #account for d_x eventually
|
83
|
+
deriv = d_y/d_x
|
84
|
+
result.pop
|
85
|
+
result << deriv
|
86
|
+
result << item unless (result.size) == (d.size-1)
|
87
|
+
result
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
DataSet.new(d)
|
92
|
+
end
|
93
|
+
|
94
|
+
def savitzky_golay(n=1)
|
95
|
+
enforce!(:natural_number, n)
|
96
|
+
|
97
|
+
raise "savitzy_golay filter not yet implemented!"
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'typestrict'
|
2
|
+
|
3
|
+
require 'gchart'
|
4
|
+
|
5
|
+
module Visualization
|
6
|
+
include Strict
|
7
|
+
|
8
|
+
def set_labels(labels)
|
9
|
+
enforce!(:string_array, labels)
|
10
|
+
|
11
|
+
@labels = labels
|
12
|
+
end
|
13
|
+
|
14
|
+
def datamax
|
15
|
+
(max > 0) ? max : 1
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_linegraph(params)
|
19
|
+
enforce_map!({
|
20
|
+
:width => :natural_number,
|
21
|
+
:height => :natural_number,
|
22
|
+
:background_color => :hex_color,
|
23
|
+
:color => :hex_color}, params)
|
24
|
+
|
25
|
+
GChart.line do |g|
|
26
|
+
g.data = self
|
27
|
+
g.extras = {
|
28
|
+
'chm' => 'N*cUSD0*,000000,0,-1,11',
|
29
|
+
'chbh' => '18,38',
|
30
|
+
'chds' => "0,#{datamax}"
|
31
|
+
}
|
32
|
+
g.size = "#{(params[:width]).to_i}x#{(params[:height]).to_i}"
|
33
|
+
g.entire_background = params[:background_color].to_s
|
34
|
+
g.colors = params[:color].to_s
|
35
|
+
g.axis(:bottom) {|a| a.labels = @labels}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_bargraph(params)
|
40
|
+
enforce_map!({
|
41
|
+
:width => :natural_number,
|
42
|
+
:height => :natural_number,
|
43
|
+
:orientation => [:vertical, :horizontal],
|
44
|
+
:background_color => :hex_color,
|
45
|
+
:color => :hex_color}, params)
|
46
|
+
|
47
|
+
GChart.bar do |g|
|
48
|
+
g.data = self
|
49
|
+
g.extras = {
|
50
|
+
'chm' => 'N*cUSD0*,000000,0,-1,11',
|
51
|
+
'chbh' => '18,38',
|
52
|
+
'chds' => "0,#{datamax}"
|
53
|
+
}
|
54
|
+
g.size = "#{(params[:width]).to_i}x#{(params[:height]).to_i}"
|
55
|
+
g.entire_background = params[:background_color].to_s
|
56
|
+
g.colors = params[:color].to_s
|
57
|
+
g.orientation = params[:orientation]
|
58
|
+
g.axis(:bottom) {|a| a.labels = @labels}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: analytica
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Raeez Lorgat
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-04 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -81,6 +81,8 @@ files:
|
|
81
81
|
- Rakefile
|
82
82
|
- bin/analytica
|
83
83
|
- lib/analytica.rb
|
84
|
+
- lib/analytica_comp.rb
|
85
|
+
- lib/analytica_viz.rb
|
84
86
|
- test/test_analytica.rb
|
85
87
|
has_rdoc: true
|
86
88
|
homepage: http://www.raeez.com/anlaytica
|