daru_lite 0.4.0 → 0.5.0
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.
- checksums.yaml +4 -4
- data/History.md +30 -0
- data/README.md +1 -1
- data/lib/daru_lite/data_frame/convertible.rb +0 -33
- data/lib/daru_lite/data_frame/fetchable.rb +1 -1
- data/lib/daru_lite/data_frame/i_o_able.rb +0 -102
- data/lib/daru_lite/data_frame/indexable.rb +24 -0
- data/lib/daru_lite/data_frame/relabelable.rb +125 -0
- data/lib/daru_lite/data_frame/sortable.rb +27 -1
- data/lib/daru_lite/dataframe.rb +3 -4
- data/lib/daru_lite/index/categorical_index.rb +8 -0
- data/lib/daru_lite/index/index.rb +0 -1
- data/lib/daru_lite/index/multi_index.rb +8 -5
- data/lib/daru_lite/io/io.rb +0 -195
- data/lib/daru_lite/maths/arithmetic/vector.rb +31 -0
- data/lib/daru_lite/maths/statistics/vector/descriptive.rb +134 -0
- data/lib/daru_lite/maths/statistics/vector/dispersion.rb +120 -0
- data/lib/daru_lite/maths/statistics/vector/extrema.rb +217 -0
- data/lib/daru_lite/maths/statistics/vector/normalization.rb +86 -0
- data/lib/daru_lite/maths/statistics/vector/percentile.rb +64 -0
- data/lib/daru_lite/maths/statistics/vector/sampling.rb +56 -0
- data/lib/daru_lite/maths/statistics/vector/time_series.rb +350 -0
- data/lib/daru_lite/maths/statistics/vector.rb +16 -994
- data/lib/daru_lite/vector/convertible.rb +0 -32
- data/lib/daru_lite/vector.rb +0 -9
- data/lib/daru_lite/version.rb +1 -1
- data/lib/daru_lite.rb +0 -30
- metadata +9 -75
- data/lib/daru_lite/date_time/index.rb +0 -570
- data/lib/daru_lite/date_time/offsets.rb +0 -397
- data/lib/daru_lite/extensions/which_dsl.rb +0 -53
- data/lib/daru_lite/io/sql_data_source.rb +0 -80
- data/lib/daru_lite/iruby/helpers.rb +0 -38
- data/lib/daru_lite/iruby/templates/dataframe.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/dataframe_mi.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/dataframe_mi_tbody.html.erb +0 -35
- data/lib/daru_lite/iruby/templates/dataframe_mi_thead.html.erb +0 -21
- data/lib/daru_lite/iruby/templates/dataframe_tbody.html.erb +0 -28
- data/lib/daru_lite/iruby/templates/dataframe_thead.html.erb +0 -21
- data/lib/daru_lite/iruby/templates/multi_index.html.erb +0 -12
- data/lib/daru_lite/iruby/templates/vector.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/vector_mi.html.erb +0 -5
- data/lib/daru_lite/iruby/templates/vector_mi_tbody.html.erb +0 -26
- data/lib/daru_lite/iruby/templates/vector_mi_thead.html.erb +0 -8
- data/lib/daru_lite/iruby/templates/vector_tbody.html.erb +0 -17
- data/lib/daru_lite/iruby/templates/vector_thead.html.erb +0 -8
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
module DaruLite
|
|
2
|
+
module Maths
|
|
3
|
+
module Statistics
|
|
4
|
+
module Vector
|
|
5
|
+
# Differences, rolling and exponential statistics, autocorrelation.
|
|
6
|
+
module TimeSeries
|
|
7
|
+
# The percent_change method computes the percent change over
|
|
8
|
+
# the given number of periods.
|
|
9
|
+
#
|
|
10
|
+
# @param [Integer] periods (1) number of nils to insert at the beginning.
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
#
|
|
14
|
+
# vector = DaruLite::Vector.new([4,6,6,8,10],index: ['a','f','t','i','k'])
|
|
15
|
+
# vector.percent_change
|
|
16
|
+
# #=>
|
|
17
|
+
# # <DaruLite::Vector:28713060 @name = nil size: 5 >
|
|
18
|
+
# # nil
|
|
19
|
+
# # a
|
|
20
|
+
# # f 0.5
|
|
21
|
+
# # t 0.0
|
|
22
|
+
# # i 0.3333333333333333
|
|
23
|
+
# # k 0.25
|
|
24
|
+
def percent_change(periods = 1)
|
|
25
|
+
must_be_numeric!
|
|
26
|
+
|
|
27
|
+
prev = nil
|
|
28
|
+
arr = @data.each_with_index.map do |cur, i|
|
|
29
|
+
if i < periods ||
|
|
30
|
+
include_with_nan?(DaruLite::MISSING_VALUES, cur) ||
|
|
31
|
+
include_with_nan?(DaruLite::MISSING_VALUES, prev)
|
|
32
|
+
nil
|
|
33
|
+
else
|
|
34
|
+
(cur - prev) / prev.to_f
|
|
35
|
+
end.tap { prev = cur if cur }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
DaruLite::Vector.new(arr, index: @index, name: @name)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Performs the difference of the series.
|
|
42
|
+
# Note: The first difference of series is X(t) - X(t-1)
|
|
43
|
+
# But, second difference of series is NOT X(t) - X(t-2)
|
|
44
|
+
# It is the first difference of the first difference
|
|
45
|
+
# => (X(t) - X(t-1)) - (X(t-1) - X(t-2))
|
|
46
|
+
#
|
|
47
|
+
# == Arguments
|
|
48
|
+
#
|
|
49
|
+
# * *max_lags*: integer, (default: 1), number of differences reqd.
|
|
50
|
+
#
|
|
51
|
+
# @example Using #diff
|
|
52
|
+
#
|
|
53
|
+
# ts = DaruLite::Vector.new((1..10).map { rand })
|
|
54
|
+
# # => [0.69, 0.23, 0.44, 0.71, ...]
|
|
55
|
+
#
|
|
56
|
+
# ts.diff # => [nil, -0.46, 0.21, 0.27, ...]
|
|
57
|
+
#
|
|
58
|
+
# @return [DaruLite::Vector]
|
|
59
|
+
def diff(max_lags = 1)
|
|
60
|
+
ts = self
|
|
61
|
+
difference = []
|
|
62
|
+
max_lags.times do
|
|
63
|
+
difference = ts - ts.lag
|
|
64
|
+
ts = difference
|
|
65
|
+
end
|
|
66
|
+
difference
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Calculate the rolling function for a loopback value.
|
|
70
|
+
#
|
|
71
|
+
# @param [Symbol] function The rolling function to be applied. Can be
|
|
72
|
+
# any function applicatble to DaruLite::Vector (:mean, :median, :count,
|
|
73
|
+
# :min, :max, etc.)
|
|
74
|
+
# @param [Integer] n (10) A non-negative value which serves as the loopback length.
|
|
75
|
+
# @return [DaruLite::Vector] Vector containin rolling calculations.
|
|
76
|
+
# @example Using #rolling
|
|
77
|
+
# ts = DaruLite::Vector.new((1..100).map { rand })
|
|
78
|
+
# # => [0.69, 0.23, 0.44, 0.71, ...]
|
|
79
|
+
# # first 9 observations are nil
|
|
80
|
+
# ts.rolling(:mean) # => [ ... nil, 0.484... , 0.445... , 0.513 ... , ... ]
|
|
81
|
+
def rolling(function, n = 10)
|
|
82
|
+
DaruLite::Vector.new(
|
|
83
|
+
([nil] * (n - 1)) +
|
|
84
|
+
(0..(size - n)).map do |i|
|
|
85
|
+
DaruLite::Vector.new(@data[i...(i + n)]).send(function)
|
|
86
|
+
end, index: @index
|
|
87
|
+
)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# @!method rolling_mean
|
|
91
|
+
# Calculate rolling average
|
|
92
|
+
# @yieldparam [Integer] n (10) Loopback length
|
|
93
|
+
# @!method rolling_median
|
|
94
|
+
# Calculate rolling median
|
|
95
|
+
# @yieldparam [Integer] n (10) Loopback length
|
|
96
|
+
# @!method rolling_count
|
|
97
|
+
# Calculate rolling non-missing count
|
|
98
|
+
# @yieldparam [Integer] n (10) Loopback length
|
|
99
|
+
# @!method rolling_max
|
|
100
|
+
# Calculate rolling max value
|
|
101
|
+
# @yieldparam [Integer] n (10) Loopback length
|
|
102
|
+
# @!method rolling_min
|
|
103
|
+
# Calculate rolling min value
|
|
104
|
+
# @yieldparam [Integer] n (10) Loopback length
|
|
105
|
+
# @!method rolling_sum
|
|
106
|
+
# Calculate rolling sum
|
|
107
|
+
# @yieldparam [Integer] n (10) Loopback length
|
|
108
|
+
# @!method rolling_std
|
|
109
|
+
# Calculate rolling standard deviation
|
|
110
|
+
# @yieldparam [Integer] n (10) Loopback length
|
|
111
|
+
# @!method rolling_variance
|
|
112
|
+
# Calculate rolling variance
|
|
113
|
+
# @yieldparam [Integer] n (10) Loopback length
|
|
114
|
+
%i[count mean median max min sum std variance].each do |meth|
|
|
115
|
+
define_method(:"rolling_#{meth}") do |n = 10|
|
|
116
|
+
rolling(meth, n)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Exponential Moving Average.
|
|
121
|
+
# Calculates an exponential moving average of the series using a
|
|
122
|
+
# specified parameter. If wilder is false (the default) then the EMA
|
|
123
|
+
# uses a smoothing value of 2 / (n + 1), if it is true then it uses the
|
|
124
|
+
# Welles Wilder smoother of 1 / n.
|
|
125
|
+
#
|
|
126
|
+
# Warning for EMA usage: EMAs are unstable for small series, as they
|
|
127
|
+
# use a lot more than n observations to calculate. The series is stable
|
|
128
|
+
# if the size of the series is >= 3.45 * (n + 1)
|
|
129
|
+
#
|
|
130
|
+
# @param [Integer] n (10) Loopback length.
|
|
131
|
+
# @param [TrueClass, FalseClass] wilder (false) If true, 1/n value is
|
|
132
|
+
# used for smoothing; if false, uses 2/(n+1) value
|
|
133
|
+
#
|
|
134
|
+
# @example Using ema
|
|
135
|
+
#
|
|
136
|
+
# ts = DaruLite::Vector.new((1..100).map { rand })
|
|
137
|
+
# # => [0.577..., 0.123..., 0.173..., 0.233..., ...]
|
|
138
|
+
#
|
|
139
|
+
# # first 9 observations are nil
|
|
140
|
+
# ts.ema # => [ ... nil, 0.455... , 0.395..., 0.323..., ... ]
|
|
141
|
+
#
|
|
142
|
+
# @return [DaruLite::Vector] Contains EMA
|
|
143
|
+
def ema(n = 10, wilder = false)
|
|
144
|
+
smoother = wilder ? 1.0 / n : 2.0 / (n + 1)
|
|
145
|
+
# need to start everything from the first non-nil observation
|
|
146
|
+
start = @data.index { |i| !i.nil? }
|
|
147
|
+
# not enough observations to seed the average
|
|
148
|
+
return DaruLite::Vector.new([nil] * size, index: @index, name: @name) if start.nil? || start + n > size
|
|
149
|
+
|
|
150
|
+
# nth observation is just a moving average, of the non-nil observations
|
|
151
|
+
seed = @data[start...(start + n)].compact
|
|
152
|
+
last = seed_mean(seed)
|
|
153
|
+
# first n - 1 observations are nil; the nth always carries the seed, even when it is nil itself
|
|
154
|
+
base = ([nil] * (start + n - 1)) << last
|
|
155
|
+
# nil observations are skipped: they yield nil and leave the average as is
|
|
156
|
+
(start + n).upto size - 1 do |i|
|
|
157
|
+
x = @data[i]
|
|
158
|
+
base << (x.nil? ? nil : last = (x * smoother) + ((1 - smoother) * last))
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
DaruLite::Vector.new(base, index: @index, name: @name)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Exponential Moving Variance.
|
|
165
|
+
# Calculates an exponential moving variance of the series using a
|
|
166
|
+
# specified parameter. If wilder is false (the default) then the EMV
|
|
167
|
+
# uses a smoothing value of 2 / (n + 1), if it is true then it uses the
|
|
168
|
+
# Welles Wilder smoother of 1 / n.
|
|
169
|
+
#
|
|
170
|
+
# @param [Integer] n (10) Loopback length.
|
|
171
|
+
# @param [TrueClass, FalseClass] wilder (false) If true, 1/n value is
|
|
172
|
+
# used for smoothing; if false, uses 2/(n+1) value
|
|
173
|
+
#
|
|
174
|
+
# @example Using emv
|
|
175
|
+
#
|
|
176
|
+
# ts = DaruLite::Vector.new((1..100).map { rand })
|
|
177
|
+
# # => [0.047..., 0.23..., 0.836..., 0.845..., ...]
|
|
178
|
+
#
|
|
179
|
+
# # first 9 observations are nil
|
|
180
|
+
# ts.emv # => [ ... nil, 0.073... , 0.082..., 0.080..., ...]
|
|
181
|
+
#
|
|
182
|
+
# @return [DaruLite::Vector] contains EMV
|
|
183
|
+
def emv(n = 10, wilder = false) # rubocop:disable Metrics/AbcSize
|
|
184
|
+
smoother = wilder ? 1.0 / n : 2.0 / (n + 1)
|
|
185
|
+
# need to start everything from the first non-nil observation
|
|
186
|
+
start = @data.index { |i| !i.nil? }
|
|
187
|
+
# not enough observations to seed the average
|
|
188
|
+
return DaruLite::Vector.new([nil] * size, index: @index, name: @name) if start.nil? || start + n > size
|
|
189
|
+
|
|
190
|
+
seed = @data[start...(start + n)].compact
|
|
191
|
+
mean = seed_mean(seed)
|
|
192
|
+
# nth observation is just a moving variance_population, of the non-nil observations
|
|
193
|
+
var = seed.inject(0.0) { |s, x| s + ((x - mean)**2) } / seed.size
|
|
194
|
+
# first n - 1 observations are nil; the nth always carries the seed, even when it is nil itself
|
|
195
|
+
var_base = ([nil] * (start + n - 1)) << var
|
|
196
|
+
# nil observations are skipped: they yield nil and leave mean and variance as they are
|
|
197
|
+
(start + n).upto size - 1 do |i|
|
|
198
|
+
x = @data[i]
|
|
199
|
+
next var_base << nil if x.nil?
|
|
200
|
+
|
|
201
|
+
last = mean
|
|
202
|
+
mean = (x * smoother) + ((1 - smoother) * last)
|
|
203
|
+
var_base << (var = ((1 - smoother) * var) + (smoother * (x - last) * (x - mean)))
|
|
204
|
+
end
|
|
205
|
+
DaruLite::Vector.new(var_base, index: @index, name: @name)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Exponential Moving Standard Deviation.
|
|
209
|
+
# Calculates an exponential moving standard deviation of the series using a
|
|
210
|
+
# specified parameter. If wilder is false (the default) then the EMSD
|
|
211
|
+
# uses a smoothing value of 2 / (n + 1), if it is true then it uses the
|
|
212
|
+
# Welles Wilder smoother of 1 / n.
|
|
213
|
+
#
|
|
214
|
+
# @param [Integer] n (10) Loopback length.
|
|
215
|
+
# @param [TrueClass, FalseClass] wilder (false) If true, 1/n value is
|
|
216
|
+
# used for smoothing; if false, uses 2/(n+1) value
|
|
217
|
+
#
|
|
218
|
+
# @example Using emsd
|
|
219
|
+
#
|
|
220
|
+
# ts = DaruLite::Vector.new((1..100).map { rand })
|
|
221
|
+
# # => [0.400..., 0.727..., 0.862..., 0.013..., ...]
|
|
222
|
+
#
|
|
223
|
+
# # first 9 observations are nil
|
|
224
|
+
# ts.emsd # => [ ... nil, 0.285... , 0.258..., 0.243..., ...]
|
|
225
|
+
#
|
|
226
|
+
# @return [DaruLite::Vector] contains EMSD
|
|
227
|
+
def emsd(n = 10, wilder = false)
|
|
228
|
+
result = []
|
|
229
|
+
emv_return = emv(n, wilder)
|
|
230
|
+
emv_return.each do |d| # rubocop:disable Style/MapIntoArray
|
|
231
|
+
result << (d.nil? ? nil : Math.sqrt(d))
|
|
232
|
+
end
|
|
233
|
+
DaruLite::Vector.new(result, index: @index, name: @name)
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Moving Average Convergence-Divergence.
|
|
237
|
+
# Calculates the MACD (moving average convergence-divergence) of the time
|
|
238
|
+
# series.
|
|
239
|
+
# @see https://en.wikipedia.org/wiki/MACD
|
|
240
|
+
#
|
|
241
|
+
# @param fast [Integer] fast period of MACD (default 12)
|
|
242
|
+
# @param slow [Integer] slow period of MACD (default 26)
|
|
243
|
+
# @param signal [Integer] signal period of MACD (default 9)
|
|
244
|
+
#
|
|
245
|
+
# @example Create a series and calculate MACD values
|
|
246
|
+
# ts = DaruLite::Vector.new((1..100).map { rand })
|
|
247
|
+
# # => [0.69, 0.23, 0.44, 0.71, ...]
|
|
248
|
+
# macdseries, macdsignal, macdhist = ts.macd
|
|
249
|
+
# macdseries, macdsignal, macdhist = ts.macd(13)
|
|
250
|
+
# macdseries, macdsignal, macdhist = ts.macd(signal=5)
|
|
251
|
+
#
|
|
252
|
+
# @return [Array<DaruLite::Vector>] macdseries, macdsignal and macdhist are
|
|
253
|
+
# returned as an array of three DaruLite::Vectors
|
|
254
|
+
#
|
|
255
|
+
def macd(fast = 12, slow = 26, signal = 9)
|
|
256
|
+
macdseries = positional_difference(ema(fast), ema(slow))
|
|
257
|
+
macdsignal = macdseries.ema(signal)
|
|
258
|
+
macdhist = positional_difference(macdseries, macdsignal)
|
|
259
|
+
[macdseries, macdsignal, macdhist]
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# Calculates the autocorrelation coefficients of the series.
|
|
263
|
+
#
|
|
264
|
+
# The first element is always 1, since that is the correlation
|
|
265
|
+
# of the series with itself.
|
|
266
|
+
#
|
|
267
|
+
# @example
|
|
268
|
+
# ts = DaruLite::Vector.new((1..100).map { rand })
|
|
269
|
+
#
|
|
270
|
+
# ts.acf # => array with first 21 autocorrelations
|
|
271
|
+
# ts.acf 3 # => array with first 3 autocorrelations
|
|
272
|
+
def acf(max_lags = nil)
|
|
273
|
+
max_lags ||= (10 * Math.log10(size)).to_i
|
|
274
|
+
|
|
275
|
+
(0..max_lags).map do |i|
|
|
276
|
+
if i.zero?
|
|
277
|
+
1.0
|
|
278
|
+
else
|
|
279
|
+
m = mean
|
|
280
|
+
# can't use Pearson coefficient since the mean for the lagged series should
|
|
281
|
+
# be the same as the regular series
|
|
282
|
+
((self - m) * (lag(i) - m)).sum / variance_sample / (size - 1)
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Provides autocovariance.
|
|
288
|
+
#
|
|
289
|
+
# == Options
|
|
290
|
+
#
|
|
291
|
+
# * *:demean* = true; optional. Supply false if series is not to be demeaned
|
|
292
|
+
# * *:unbiased* = false; optional. By default each lag k is divided by the size n of the
|
|
293
|
+
# series (the biased form). Supply true to divide it by n - k (the unbiased form)
|
|
294
|
+
#
|
|
295
|
+
# == Returns
|
|
296
|
+
#
|
|
297
|
+
# Autocovariance value
|
|
298
|
+
def acvf(demean = true, unbiased = false)
|
|
299
|
+
demeaned_series = demean ? self - mean : self
|
|
300
|
+
|
|
301
|
+
n = [(10 * Math.log10(size)).to_i + 1, size].min
|
|
302
|
+
m = mean
|
|
303
|
+
d = if unbiased
|
|
304
|
+
(1..size).to_a.reverse[0..n]
|
|
305
|
+
else
|
|
306
|
+
Array.new(size, size)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
0.upto(n - 1).map do |i|
|
|
310
|
+
# nil when no pair of observations is left at that lag
|
|
311
|
+
total = (demeaned_series * (demean ? lag(i) - m : lag(i))).sum
|
|
312
|
+
total&.fdiv(d[i])
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# Calculate cumulative sum of Vector
|
|
317
|
+
def cumsum
|
|
318
|
+
result = []
|
|
319
|
+
acc = 0
|
|
320
|
+
@data.each do |d|
|
|
321
|
+
if include_with_nan? DaruLite::MISSING_VALUES, d
|
|
322
|
+
result << nil
|
|
323
|
+
else
|
|
324
|
+
acc += d
|
|
325
|
+
result << acc
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
DaruLite::Vector.new(result, index: @index)
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
private
|
|
333
|
+
|
|
334
|
+
# Vector#- aligns both vectors on their index and sorts it, which would
|
|
335
|
+
# change the order of the observations: subtract by position instead.
|
|
336
|
+
def positional_difference(minuend, subtrahend)
|
|
337
|
+
differences = minuend.to_a.zip(subtrahend.to_a).map { |a, b| a && b && (a - b) }
|
|
338
|
+
DaruLite::Vector.new(differences, index: @index, name: @name)
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
# Plain left-to-right addition: Array#sum compensates rounding errors,
|
|
342
|
+
# which would change the results in the last decimals.
|
|
343
|
+
def seed_mean(seed)
|
|
344
|
+
seed.inject(0.0) { |sum, value| sum + value } / seed.size # rubocop:disable Performance/Sum
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
end
|