Module: Analytica::Computation
Defined Under Namespace
Classes: InvalidInputException
Instance Method Summary collapse
- #average_filter(params = {}) ⇒ Object (also: #avg)
- #exponential_mean(params = {}) ⇒ Object
- #exponential_moving_average(params = {}) ⇒ Object (also: #ema)
- #linear_mean(params = {}) ⇒ Object
- #linear_moving_average(params = {}) ⇒ Object (also: #lma)
- #mean ⇒ Object
- #moving_average(params = {}) ⇒ Object
- #piecewise_derivative(n = 1) ⇒ Object (also: #dydx)
- #savitzky_golay(n = 1) ⇒ Object
- #simple_moving_average(params = {}) ⇒ Object (also: #sma)
- #sum ⇒ Object
Instance Method Details
#average_filter(params = {}) ⇒ Object Also known as: avg
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/computation.rb', line 92 def average_filter(params={}) enforce_map!({ :decay => [:simple, :linear, :exponential], :offset => :natural_number, # offset from latest data point :samples => :natural_number}, params) if !(params[:offset] <= size) c = InvalidInputException.new(:simple_average, size, params, [":offset <= :size"]) raise c, c.inspect, caller end if !(params[:offset] >= params[:samples]) c = InvalidInputException.new(:simple_average, size, params, [":offset >= :samples"]) raise c, c.inspect, caller end i = size - params[:offset] j = i + params[:samples]-1 d = DataSet.new(self[i..j]) case params[:decay] when :simple d.mean when :linear d.linear_mean(:bias => :last, :samples => d.size) when :exponential enforce_exists!(:alpha, params) enforce!(:numeric, params[:alpha]) d.exponential_mean(:bias => :first, :samples => d.size, :alpha => params[:alpha]) end end |
#exponential_mean(params = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/computation.rb', line 59 def exponential_mean(params={}) enforce_map!({ :bias => [:last, :first], :alpha => :numeric, :samples => :integer}, params) data = self data.reverse! if params[:bias] == :last params[:samples] = [params[:samples], data.size].min ema = 0 counter = 0 alpha = params[:alpha] data.each do |sample| if counter > params[:samples] break end if counter == 0 ema += sample else ema += sample * (1-alpha)**counter end counter += 1 end alpha*ema end |
#exponential_moving_average(params = {}) ⇒ Object Also known as: ema
169 170 171 172 173 174 175 |
# File 'lib/computation.rb', line 169 def exponential_moving_average(params={}) enforce_map!({ :samples => :integer, :alpha => :float}, params) moving_average(:decay => :exponential, :samples => params[:samples], :alpha => params[:alpha]) end |
#linear_mean(params = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/computation.rb', line 35 def linear_mean(params={}) enforce_map!({ :bias => [:last, :first], :samples => :natural_number}, params) data = self data.reverse! if params[:bias] == :last params[:samples] = [params[:samples], data.size].min n = params[:samples] + 1 numerator = 0.0 denominator = 0.0 data.each do |sample| n -= 1 n = n > 0 ? n : 0 numerator += n*sample denominator += n end numerator / denominator end |
#linear_moving_average(params = {}) ⇒ Object Also known as: lma
161 162 163 164 165 |
# File 'lib/computation.rb', line 161 def linear_moving_average(params={}) enforce_map!({ :samples => :natural_number}, params) moving_average(:decay => :linear, :samples => params[:samples]) end |
#mean ⇒ Object
31 32 33 |
# File 'lib/computation.rb', line 31 def mean sum.to_f / size end |
#moving_average(params = {}) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/computation.rb', line 133 def moving_average(params={}) enforce_map!({ :decay => [:simple, :linear, :exponential], :samples => :natural_number}, params) if !(size >= (2*params[:samples]-1)) c = InvalidInputException.new(:moving_average, size, params, ["size >= (2*:samples-1)"]) raise c, c.inspect, caller end d = DataSet.new (1..params[:samples]).each do |offset| d << average_filter(:decay => params[:decay], :alpha => params[:alpha], :offset => offset+params[:samples]-1, :samples => params[:samples]) end d.reverse end |
#piecewise_derivative(n = 1) ⇒ Object Also known as: dydx
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/computation.rb', line 179 def piecewise_derivative(n=1) enforce!(:natural_number, n) d = self n.times do d = d.inject([]) do |result, item| if result.size == 0 result << item else d_y = (item - result.last).to_f d_x = 1.0 #account for d_x eventually deriv = d_y/d_x result.pop result << deriv result << item unless (result.size) == (d.size-1) result end end end DataSet.new(d) end |
#savitzky_golay(n = 1) ⇒ Object
203 204 205 206 207 |
# File 'lib/computation.rb', line 203 def savitzky_golay(n=1) enforce!(:natural_number, n) raise "savitzy_golay filter not yet implemented!" end |
#simple_moving_average(params = {}) ⇒ Object Also known as: sma
153 154 155 156 157 |
# File 'lib/computation.rb', line 153 def simple_moving_average(params={}) enforce_map!({ :samples => :natural_number}, params) moving_average(:decay => :simple, :samples => params[:samples]) end |
#sum ⇒ Object
26 27 28 29 |
# File 'lib/computation.rb', line 26 def sum sum = inject { |sum, x| sum + x } sum ? sum : 0 end |