daru 0.1.5 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE.md +18 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +21 -7
  5. data/.travis.yml +10 -5
  6. data/CONTRIBUTING.md +15 -10
  7. data/History.md +124 -2
  8. data/README.md +37 -9
  9. data/ReleasePolicy.md +20 -0
  10. data/benchmarks/db_loading.rb +34 -0
  11. data/benchmarks/statistics.rb +6 -6
  12. data/benchmarks/where_clause.rb +1 -1
  13. data/benchmarks/where_vs_filter.rb +1 -1
  14. data/daru.gemspec +17 -41
  15. data/lib/daru.rb +10 -13
  16. data/lib/daru/accessors/gsl_wrapper.rb +1 -1
  17. data/lib/daru/accessors/nmatrix_wrapper.rb +2 -0
  18. data/lib/daru/category.rb +29 -15
  19. data/lib/daru/configuration.rb +34 -0
  20. data/lib/daru/core/group_by.rb +158 -77
  21. data/lib/daru/core/merge.rb +12 -3
  22. data/lib/daru/core/query.rb +20 -4
  23. data/lib/daru/dataframe.rb +692 -118
  24. data/lib/daru/date_time/index.rb +14 -11
  25. data/lib/daru/date_time/offsets.rb +9 -1
  26. data/lib/daru/extensions/which_dsl.rb +55 -0
  27. data/lib/daru/formatters/table.rb +3 -5
  28. data/lib/daru/index/categorical_index.rb +4 -4
  29. data/lib/daru/index/index.rb +131 -42
  30. data/lib/daru/index/multi_index.rb +118 -10
  31. data/lib/daru/io/csv/converters.rb +21 -0
  32. data/lib/daru/io/io.rb +105 -33
  33. data/lib/daru/io/sql_data_source.rb +10 -0
  34. data/lib/daru/iruby/templates/dataframe.html.erb +4 -51
  35. data/lib/daru/iruby/templates/dataframe_mi.html.erb +3 -56
  36. data/lib/daru/iruby/templates/dataframe_mi_tbody.html.erb +35 -0
  37. data/lib/daru/iruby/templates/dataframe_mi_thead.html.erb +21 -0
  38. data/lib/daru/iruby/templates/dataframe_tbody.html.erb +28 -0
  39. data/lib/daru/iruby/templates/dataframe_thead.html.erb +21 -0
  40. data/lib/daru/iruby/templates/vector.html.erb +3 -25
  41. data/lib/daru/iruby/templates/vector_mi.html.erb +3 -34
  42. data/lib/daru/iruby/templates/vector_mi_tbody.html.erb +26 -0
  43. data/lib/daru/iruby/templates/vector_mi_thead.html.erb +8 -0
  44. data/lib/daru/iruby/templates/vector_tbody.html.erb +17 -0
  45. data/lib/daru/iruby/templates/vector_thead.html.erb +8 -0
  46. data/lib/daru/maths/arithmetic/vector.rb +38 -2
  47. data/lib/daru/maths/statistics/dataframe.rb +28 -30
  48. data/lib/daru/maths/statistics/vector.rb +295 -41
  49. data/lib/daru/plotting/gruff/dataframe.rb +13 -15
  50. data/lib/daru/plotting/nyaplot/category.rb +1 -1
  51. data/lib/daru/plotting/nyaplot/dataframe.rb +15 -4
  52. data/lib/daru/plotting/nyaplot/vector.rb +1 -2
  53. data/lib/daru/vector.rb +308 -96
  54. data/lib/daru/version.rb +1 -1
  55. data/profile/vector_new.rb +9 -0
  56. data/spec/accessors/gsl_wrapper_spec.rb +38 -35
  57. data/spec/accessors/nmatrix_wrapper_spec.rb +25 -22
  58. data/spec/category_spec.rb +24 -20
  59. data/spec/core/group_by_spec.rb +238 -4
  60. data/spec/core/merge_spec.rb +1 -1
  61. data/spec/core/query_spec.rb +65 -50
  62. data/spec/daru_spec.rb +22 -0
  63. data/spec/dataframe_spec.rb +473 -16
  64. data/spec/date_time/date_time_index_helper_spec.rb +72 -0
  65. data/spec/date_time/index_spec.rb +34 -16
  66. data/spec/date_time/offsets_spec.rb +14 -0
  67. data/spec/extensions/rserve_spec.rb +1 -1
  68. data/spec/extensions/which_dsl_spec.rb +38 -0
  69. data/spec/fixtures/boolean_converter_test.csv +5 -0
  70. data/spec/fixtures/duplicates.csv +32 -0
  71. data/spec/fixtures/eciresults.html +394 -0
  72. data/spec/fixtures/empty_rows_test.csv +17 -0
  73. data/spec/fixtures/macau.html +3691 -0
  74. data/spec/fixtures/macd_data.csv +150 -0
  75. data/spec/fixtures/matrix_test.csv +55 -55
  76. data/spec/fixtures/moneycontrol.html +6812 -0
  77. data/spec/fixtures/string_converter_test.csv +5 -0
  78. data/spec/fixtures/test_xls.xls +0 -0
  79. data/spec/fixtures/test_xls_2.xls +0 -0
  80. data/spec/fixtures/url_test.txt~ +0 -0
  81. data/spec/fixtures/valid_markup.html +62 -0
  82. data/spec/fixtures/wiki_climate.html +1243 -0
  83. data/spec/fixtures/wiki_table_info.html +631 -0
  84. data/spec/formatters/table_formatter_spec.rb +29 -0
  85. data/spec/index/categorical_index_spec.rb +33 -33
  86. data/spec/index/index_spec.rb +160 -41
  87. data/spec/index/multi_index_spec.rb +143 -33
  88. data/spec/io/io_spec.rb +246 -2
  89. data/spec/io/sql_data_source_spec.rb +31 -41
  90. data/spec/iruby/dataframe_spec.rb +17 -19
  91. data/spec/iruby/vector_spec.rb +26 -28
  92. data/spec/maths/arithmetic/dataframe_spec.rb +1 -1
  93. data/spec/maths/arithmetic/vector_spec.rb +18 -0
  94. data/spec/maths/statistics/vector_spec.rb +153 -15
  95. data/spec/plotting/gruff/category_spec.rb +3 -3
  96. data/spec/plotting/gruff/dataframe_spec.rb +14 -4
  97. data/spec/plotting/gruff/vector_spec.rb +9 -9
  98. data/spec/plotting/nyaplot/category_spec.rb +5 -9
  99. data/spec/plotting/nyaplot/dataframe_spec.rb +95 -47
  100. data/spec/plotting/nyaplot/vector_spec.rb +5 -11
  101. data/spec/shared/vector_display_spec.rb +12 -14
  102. data/spec/spec_helper.rb +30 -7
  103. data/spec/support/matchers.rb +5 -0
  104. data/spec/vector_spec.rb +306 -72
  105. metadata +96 -55
  106. data/spec/fixtures/stock_data.csv +0 -500
@@ -22,7 +22,7 @@ module Daru
22
22
  # Calculate the minimum value of each numeric vector
23
23
  # @!method product
24
24
  # Compute the product of each numeric vector
25
- [:mean, :variance_sample, :range, :median, :mode, :std, :sum, :count, :min, :product].each do |meth|
25
+ %i[mean variance_sample range median mode std sum count min product].each do |meth|
26
26
  define_method(meth) do
27
27
  compute_stats meth
28
28
  end
@@ -41,38 +41,38 @@ module Daru
41
41
  # Calculate cumulative sum of each numeric Vector
42
42
  # @!method standardize
43
43
  # Standardize each Vector
44
- # @!method acf
44
+ # @!method acf(max_lags)
45
45
  # Calculate Autocorrelation coefficient
46
- # @param [Integer] max_lags (nil) Number of initial lags
47
- # @!method ema
46
+ # @param max_lags [Integer] (nil) Number of initial lags
47
+ # @!method ema(n,wilder)
48
48
  # Calculate exponential moving average.
49
- # @param [Integer] n (10) Loopback length.
50
- # @param [TrueClass, FalseClass, NilClass] wilder (false) If true,
49
+ # @param n [Integer] (10) Loopback length.
50
+ # @param wilder [TrueClass, FalseClass, NilClass] (false) If true,
51
51
  # 1/n value is used for smoothing; if false, uses 2/(n+1) value.
52
- # @!method rolling_mean
52
+ # @!method rolling_mean(n)
53
53
  # Calculate moving averages
54
- # @param [Integer] n (10) Loopback length. Default to 10.
55
- # @!method rolling_median
54
+ # @param n [Integer] (10) Loopback length. Default to 10.
55
+ # @!method rolling_median(n)
56
56
  # Calculate moving median
57
- # @param [Integer] n (10) Loopback length. Default to 10.
58
- # @!method rolling_max
57
+ # @param n [Integer] (10) Loopback length. Default to 10.
58
+ # @!method rolling_max(n)
59
59
  # Calculate moving max
60
- # @param [Integer] n (10) Loopback length. Default to 10.
61
- # @!method rolling_min
60
+ # @param n [Integer] (10) Loopback length. Default to 10.
61
+ # @!method rolling_min(n)
62
62
  # Calculate moving min
63
- # @param [Integer] n (10) Loopback length. Default to 10.
64
- # @!method rolling_count
63
+ # @param n [Integer] (10) Loopback length. Default to 10.
64
+ # @!method rolling_count(n)
65
65
  # Calculate moving non-missing count
66
- # @param [Integer] n (10) Loopback length. Default to 10.
67
- # @!method rolling_std
66
+ # @param n [Integer] (10) Loopback length. Default to 10.
67
+ # @!method rolling_std(n)
68
68
  # Calculate moving standard deviation
69
- # @param [Integer] n (10) Loopback length. Default to 10.
70
- # @!method rolling_variance
69
+ # @param n [Integer] (10) Loopback length. Default to 10.
70
+ # @!method rolling_variance(n)
71
71
  # Calculate moving variance
72
- # @param [Integer] n (10) Loopback length. Default to 10.
73
- [
74
- :cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max,
75
- :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum
72
+ # @param n [Integer] (10) Loopback length. Default to 10.
73
+ %i[
74
+ cumsum standardize acf ema rolling_mean rolling_median rolling_max
75
+ rolling_min rolling_count rolling_std rolling_variance rolling_sum
76
76
  ].each do |meth|
77
77
  define_method(meth) do |*args|
78
78
  apply_method_to_numerics meth, *args
@@ -88,7 +88,7 @@ module Daru
88
88
  # be applied to numeric vectors. Default is [:count, :mean, :std, :max,
89
89
  # :min]. Methods will be applied in the specified order.
90
90
  def describe methods=nil
91
- methods ||= [:count, :mean, :std, :min, :max]
91
+ methods ||= %i[count mean std min max]
92
92
 
93
93
  description_hash = {}
94
94
  numeric_vectors.each do |vec|
@@ -133,17 +133,15 @@ module Daru
133
133
  # Calculate sample variance-covariance between the numeric vectors.
134
134
  def covariance
135
135
  cache = Hash.new do |h, (col, row)|
136
- h[[col, row]] = vector_cov(self[row],self[col])
136
+ value = vector_cov(self[row],self[col])
137
+ h[[col, row]] = value
138
+ h[[row, col]] = value
137
139
  end
138
140
  vectors = numeric_vectors
139
141
 
140
142
  mat_rows = vectors.collect do |row|
141
143
  vectors.collect do |col|
142
- if row == col
143
- self[row].variance
144
- else
145
- cache[[col,row]]
146
- end
144
+ row == col ? self[row].variance : cache[[col,row]]
147
145
  end
148
146
  end
149
147
 
@@ -19,10 +19,6 @@ module Daru
19
19
  @data.product
20
20
  end
21
21
 
22
- def min
23
- @data.min
24
- end
25
-
26
22
  def range
27
23
  max - min
28
24
  end
@@ -45,7 +41,7 @@ module Daru
45
41
  # be applied to vectors. Default is [:count, :mean, :std, :max,
46
42
  # :min]. Methods will be applied in the specified order.
47
43
  def describe methods=nil
48
- methods ||= [:count, :mean, :std, :min, :max]
44
+ methods ||= %i[count mean std min max]
49
45
  description = methods.map { |m| send(m) }
50
46
  Daru::Vector.new(description, index: methods, name: :statistics)
51
47
  end
@@ -70,24 +66,280 @@ module Daru
70
66
  reject_values(*Daru::MISSING_VALUES).uniq.reset_index!
71
67
  end
72
68
 
73
- # Maximum element of the vector.
74
- #
75
- # @param return_type [Symbol] Data type of the returned value. Defaults
76
- # to returning only the maximum number but passing *:vector* will return
77
- # a Daru::Vector with the index of the corresponding maximum value.
78
- def max return_type=:stored_type
79
- max_value = @data.max
80
- if return_type == :vector
81
- Daru::Vector.new({index_of(max_value) => max_value}, name: @name, dtype: @dtype)
82
- else
83
- max_value
69
+ if RUBY_VERSION >= '2.2'
70
+ # Returns the maximum value(s) present in the vector, with an optional comparator block.
71
+ #
72
+ # @param size [Integer] Number of maximum values to return. Defaults to nil.
73
+ #
74
+ # @example
75
+ #
76
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
77
+ # #=>
78
+ # # #<Daru::Vector(3)>
79
+ # # t Tyrion
80
+ # # d Daenerys
81
+ # # j Jon Starkgaryen
82
+ #
83
+ # dv.max
84
+ # #=> "Tyrion"
85
+ #
86
+ # dv.max(2) { |a,b| a.size <=> b.size }
87
+ # #=> ["Jon Starkgaryen","Daenerys"]
88
+ def max(size=nil, &block)
89
+ reject_values(*Daru::MISSING_VALUES).to_a.max(size, &block)
90
+ end
91
+
92
+ # Returns the maximum value(s) present in the vector, with a compulsory object block.
93
+ #
94
+ # @param size [Integer] Number of maximum values to return. Defaults to nil.
95
+ #
96
+ # @example
97
+ #
98
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
99
+ # #=>
100
+ # # #<Daru::Vector(3)>
101
+ # # t Tyrion
102
+ # # d Daenerys
103
+ # # j Jon Starkgaryen
104
+ #
105
+ # dv.max_by(2) { |i| i.size }
106
+ # #=> ["Jon Starkgaryen","Daenerys"]
107
+ def max_by(size=nil, &block)
108
+ raise ArgumentError, 'Expected compulsory object block in max_by method' unless block_given?
109
+ reject_values(*Daru::MISSING_VALUES).to_a.max_by(size, &block)
110
+ end
111
+
112
+ # Returns the minimum value(s) present in the vector, with an optional comparator block.
113
+ #
114
+ # @param size [Integer] Number of minimum values to return. Defaults to nil.
115
+ #
116
+ # @example
117
+ #
118
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
119
+ # #=>
120
+ # # #<Daru::Vector(3)>
121
+ # # t Tyrion
122
+ # # d Daenerys
123
+ # # j Jon Starkgaryen
124
+ #
125
+ # dv.min
126
+ # #=> "Daenerys"
127
+ #
128
+ # dv.min(2) { |a,b| a.size <=> b.size }
129
+ # #=> ["Tyrion","Daenerys"]
130
+ def min(size=nil, &block)
131
+ reject_values(*Daru::MISSING_VALUES).to_a.min(size, &block)
132
+ end
133
+
134
+ # Returns the minimum value(s) present in the vector, with a compulsory object block.
135
+ #
136
+ # @param size [Integer] Number of minimum values to return. Defaults to nil.
137
+ #
138
+ # @example
139
+ #
140
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
141
+ # #=>
142
+ # # #<Daru::Vector(3)>
143
+ # # t Tyrion
144
+ # # d Daenerys
145
+ # # j Jon Starkgaryen
146
+ #
147
+ # dv.min_by(2) { |i| i.size }
148
+ # #=> ["Tyrion","Daenerys"]
149
+ def min_by(size=nil, &block)
150
+ raise ArgumentError, 'Expected compulsory object block in min_by method' unless block_given?
151
+ reject_values(*Daru::MISSING_VALUES).to_a.min_by(size, &block)
152
+ end
153
+ else
154
+ # Returns the maximum value(s) present in the vector, with an optional comparator block.
155
+ #
156
+ # @param size [Integer] Number of maximum values to return. Defaults to nil.
157
+ #
158
+ # @example
159
+ #
160
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
161
+ # #=>
162
+ # # #<Daru::Vector(3)>
163
+ # # t Tyrion
164
+ # # d Daenerys
165
+ # # j Jon Starkgaryen
166
+ #
167
+ # dv.max
168
+ # #=> "Tyrion"
169
+ #
170
+ # dv.max(2) { |a,b| a.size <=> b.size }
171
+ # #=> ["Jon Starkgaryen","Daenerys"]
172
+ def max(size=nil, &block)
173
+ range = size.nil? ? 0 : (0..size-1)
174
+ reject_values(*Daru::MISSING_VALUES).to_a.sort(&block).reverse[range]
175
+ end
176
+
177
+ # Returns the maximum value(s) present in the vector, with a compulsory object block.
178
+ #
179
+ # @param size [Integer] Number of maximum values to return. Defaults to nil.
180
+ #
181
+ # @example
182
+ #
183
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
184
+ # #=>
185
+ # # #<Daru::Vector(3)>
186
+ # # t Tyrion
187
+ # # d Daenerys
188
+ # # j Jon Starkgaryen
189
+ #
190
+ # dv.max_by(2) { |i| i.size }
191
+ # #=> ["Jon Starkgaryen","Daenerys"]
192
+ def max_by(size=nil, &block)
193
+ raise ArgumentError, 'Expected compulsory object block in max_by method' unless block_given?
194
+ reject_values(*Daru::MISSING_VALUES).to_a.sort_by(&block).reverse[size.nil? ? 0 : (0..size-1)]
84
195
  end
196
+
197
+ # Returns the minimum value(s) present in the vector, with an optional comparator block.
198
+ #
199
+ # @param size [Integer] Number of minimum values to return. Defaults to nil.
200
+ #
201
+ # @example
202
+ #
203
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
204
+ # #=>
205
+ # # #<Daru::Vector(3)>
206
+ # # t Tyrion
207
+ # # d Daenerys
208
+ # # j Jon Starkgaryen
209
+ #
210
+ # dv.min
211
+ # #=> "Daenerys"
212
+ #
213
+ # dv.min(2) { |a,b| a.size <=> b.size }
214
+ # #=> ["Tyrion","Daenerys"]
215
+ def min(size=nil, &block)
216
+ range = size.nil? ? 0 : (0..size-1)
217
+ reject_values(*Daru::MISSING_VALUES).to_a.sort(&block)[range]
218
+ end
219
+
220
+ # Returns the minimum value(s) present in the vector, with a compulsory object block.
221
+ #
222
+ # @param size [Integer] Number of minimum values to return. Defaults to nil.
223
+ #
224
+ # @example
225
+ #
226
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
227
+ # #=>
228
+ # # #<Daru::Vector(3)>
229
+ # # t Tyrion
230
+ # # d Daenerys
231
+ # # j Jon Starkgaryen
232
+ #
233
+ # dv.min_by
234
+ # #=> "Daenerys"
235
+ #
236
+ # dv.min_by(2) { |i| i.size }
237
+ # #=> ["Tyrion","Daenerys"]
238
+ def min_by(size=nil, &block)
239
+ raise ArgumentError, 'Expected compulsory object block in min_by method' unless block_given?
240
+ reject_values(*Daru::MISSING_VALUES).to_a.sort_by(&block)[size.nil? ? 0 : (0..size-1)]
241
+ end
242
+ end
243
+
244
+ # Returns the index of the maximum value(s) present in the vector, with an optional
245
+ # comparator block.
246
+ #
247
+ # @param size [Integer] Number of maximum indices to return. Defaults to nil.
248
+ #
249
+ # @example
250
+ #
251
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
252
+ # #=>
253
+ # # #<Daru::Vector(3)>
254
+ # # t Tyrion
255
+ # # d Daenerys
256
+ # # j Jon Starkgaryen
257
+ #
258
+ # dv.index_of_max
259
+ # #=> :t
260
+ #
261
+ # dv.index_of_max(2) { |a,b| a.size <=> b.size }
262
+ # #=> [:j, :d]
263
+ def index_of_max(size=nil,&block)
264
+ vals = max(size, &block)
265
+ dv = reject_values(*Daru::MISSING_VALUES)
266
+ vals.is_a?(Array) ? (vals.map { |x| dv.index_of(x) }) : dv.index_of(vals)
267
+ end
268
+
269
+ # Returns the index of the maximum value(s) present in the vector, with a compulsory
270
+ # object block.
271
+ #
272
+ # @param size [Integer] Number of maximum indices to return. Defaults to nil.
273
+ #
274
+ # @example
275
+ #
276
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
277
+ # #=>
278
+ # # #<Daru::Vector(3)>
279
+ # # t Tyrion
280
+ # # d Daenerys
281
+ # # j Jon Starkgaryen
282
+ #
283
+ # dv.index_of_max_by(2) { |i| i.size }
284
+ # #=> [:j, :d]
285
+ def index_of_max_by(size=nil,&block)
286
+ vals = max_by(size, &block)
287
+ dv = reject_values(*Daru::MISSING_VALUES)
288
+ vals.is_a?(Array) ? (vals.map { |x| dv.index_of(x) }) : dv.index_of(vals)
289
+ end
290
+
291
+ # Returns the index of the minimum value(s) present in the vector, with an optional
292
+ # comparator block.
293
+ #
294
+ # @param size [Integer] Number of minimum indices to return. Defaults to nil.
295
+ #
296
+ # @example
297
+ #
298
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
299
+ # #=>
300
+ # # #<Daru::Vector(3)>
301
+ # # t Tyrion
302
+ # # d Daenerys
303
+ # # j Jon Starkgaryen
304
+ #
305
+ # dv.index_of_min
306
+ # #=> :d
307
+ #
308
+ # dv.index_of_min(2) { |a,b| a.size <=> b.size }
309
+ # #=> [:t, :d]
310
+ def index_of_min(size=nil,&block)
311
+ vals = min(size, &block)
312
+ dv = reject_values(*Daru::MISSING_VALUES)
313
+ vals.is_a?(Array) ? (vals.map { |x| dv.index_of(x) }) : dv.index_of(vals)
314
+ end
315
+
316
+ # Returns the index of the minimum value(s) present in the vector, with a compulsory
317
+ # object block.
318
+ #
319
+ # @param size [Integer] Number of minimum indices to return. Defaults to nil.
320
+ #
321
+ # @example
322
+ #
323
+ # dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j])
324
+ # #=>
325
+ # # #<Daru::Vector(3)>
326
+ # # t Tyrion
327
+ # # d Daenerys
328
+ # # j Jon Starkgaryen
329
+ #
330
+ # dv.index_of_min(2) { |i| i.size }
331
+ # #=> [:t, :d]
332
+ def index_of_min_by(size=nil,&block)
333
+ vals = min_by(size, &block)
334
+ dv = reject_values(*Daru::MISSING_VALUES)
335
+ vals.is_a?(Array) ? (vals.map { |x| dv.index_of(x) }) : dv.index_of(vals)
85
336
  end
86
337
 
87
- # Return a Vector with the max element and its index.
338
+ # Return the maximum element present in the Vector, as a Vector.
88
339
  # @return [Daru::Vector]
89
340
  def max_index
90
- max :vector
341
+ max_value = @data.max
342
+ Daru::Vector.new({index_of(max_value) => max_value}, name: @name, dtype: @dtype)
91
343
  end
92
344
 
93
345
  def frequencies
@@ -104,7 +356,7 @@ module Daru
104
356
  def proportions
105
357
  len = size - count_values(*Daru::MISSING_VALUES)
106
358
  frequencies.to_h.each_with_object({}) do |(el, count), hash|
107
- hash[el] = count / len
359
+ hash[el] = count / len.to_f
108
360
  end
109
361
  end
110
362
 
@@ -448,29 +700,29 @@ module Daru
448
700
 
449
701
  # @!method rolling_mean
450
702
  # Calculate rolling average
451
- # @param [Integer] n (10) Loopback length
703
+ # @yieldparam [Integer] n (10) Loopback length
452
704
  # @!method rolling_median
453
705
  # Calculate rolling median
454
- # @param [Integer] n (10) Loopback length
706
+ # @yieldparam [Integer] n (10) Loopback length
455
707
  # @!method rolling_count
456
708
  # Calculate rolling non-missing count
457
- # @param [Integer] n (10) Loopback length
709
+ # @yieldparam [Integer] n (10) Loopback length
458
710
  # @!method rolling_max
459
711
  # Calculate rolling max value
460
- # @param [Integer] n (10) Loopback length
712
+ # @yieldparam [Integer] n (10) Loopback length
461
713
  # @!method rolling_min
462
714
  # Calculate rolling min value
463
- # @param [Integer] n (10) Loopback length
715
+ # @yieldparam [Integer] n (10) Loopback length
464
716
  # @!method rolling_sum
465
717
  # Calculate rolling sum
466
- # @param [Integer] n (10) Loopback length
718
+ # @yieldparam [Integer] n (10) Loopback length
467
719
  # @!method rolling_std
468
720
  # Calculate rolling standard deviation
469
- # @param [Integer] n (10) Loopback length
721
+ # @yieldparam [Integer] n (10) Loopback length
470
722
  # @!method rolling_variance
471
723
  # Calculate rolling variance
472
- # @param [Integer] n (10) Loopback length
473
- [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth|
724
+ # @yieldparam [Integer] n (10) Loopback length
725
+ %i[count mean median max min sum std variance].each do |meth|
474
726
  define_method("rolling_#{meth}".to_sym) do |n=10|
475
727
  rolling(meth, n)
476
728
  end
@@ -581,26 +833,28 @@ module Daru
581
833
 
582
834
  # Moving Average Convergence-Divergence.
583
835
  # Calculates the MACD (moving average convergence-divergence) of the time
584
- # series - this is a comparison of a fast EMA with a slow EMA.
836
+ # series.
837
+ # @see https://en.wikipedia.org/wiki/MACD
585
838
  #
586
- # == Arguments
587
- # * *fast*: integer, (default = 12) - fast component of MACD
588
- # * *slow*: integer, (default = 26) - slow component of MACD
589
- # * *signal*: integer, (default = 9) - signal component of MACD
590
- #
591
- # == Usage
839
+ # @param fast [Integer] fast period of MACD (default 12)
840
+ # @param slow [Integer] slow period of MACD (default 26)
841
+ # @param signal [Integer] signal period of MACD (default 9)
592
842
  #
843
+ # @example Create a series and calculate MACD values
593
844
  # ts = Daru::Vector.new((1..100).map { rand })
594
845
  # # => [0.69, 0.23, 0.44, 0.71, ...]
595
- # ts.macd(13)
846
+ # macdseries, macdsignal, macdhist = ts.macd
847
+ # macdseries, macdsignal, macdhist = ts.macd(13)
848
+ # macdseries, macdsignal, macdhist = ts.macd(signal=5)
596
849
  #
597
- # == Returns
850
+ # @return [Array<Daru::Vector>] macdseries, macdsignal and macdhist are
851
+ # returned as an array of three Daru::Vectors
598
852
  #
599
- # Array of two Daru::Vectors - comparison of fast EMA with slow and EMA with
600
- # signal value
601
853
  def macd(fast=12, slow=26, signal=9)
602
- series = ema(fast) - ema(slow)
603
- [series, series.ema(signal)]
854
+ macdseries = ema(fast) - ema(slow)
855
+ macdsignal = macdseries.ema(signal)
856
+ macdhist = macdseries - macdsignal
857
+ [macdseries, macdsignal, macdhist]
604
858
  end
605
859
 
606
860
  # Calculates the autocorrelation coefficients of the series.