daru 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -344,7 +344,7 @@ module Daru
344
344
 
345
345
  # Retreive a slice or a an individual index number from the index.
346
346
  #
347
- # @param [String, DateTime] Specify a date partially (as a String) or
347
+ # @param key [String, DateTime] Specify a date partially (as a String) or
348
348
  # completely to retrieve.
349
349
  def [] *key
350
350
  return slice(*key) if key.size != 1
@@ -86,7 +86,7 @@ module Daru
86
86
 
87
87
  module Offsets
88
88
  class DateOffsetType < DateOffset
89
- # @method initialize
89
+ # @!method initialize(n)
90
90
  # Initialize one of the subclasses of DateOffsetType with the number of the times
91
91
  # the offset should be applied, which is the supplied as the argument.
92
92
  #
@@ -0,0 +1,55 @@
1
+ # Support for a simple query DSL for accessing where(), inspired by gem "squeel"
2
+
3
+ module Daru
4
+ class DataFrame
5
+ # a simple query DSL for accessing where(), inspired by gem "squeel"
6
+ # e.g.:
7
+ # df.which{ `FamilySize` == `FamilySize`.max }
8
+ # equals
9
+ # df.where( df['FamilySize'].eq( df['FamilySize'].max ) )
10
+ #
11
+ # e.g.:
12
+ # df.which{ (`NameTitle` == 'Dr') & (`Sex` == 'female') }
13
+ # equals
14
+ # df.where( df['NameTitle'].eq('Dr') & df['Sex'].eq('female') )
15
+ def which(&block)
16
+ WhichQuery.new(self, &block).exec
17
+ end
18
+ end
19
+
20
+ class WhichQuery
21
+ def initialize(df, &condition)
22
+ @df = df
23
+ @condition = condition
24
+ end
25
+
26
+ # executes a block of DSL code
27
+ def exec
28
+ query = instance_eval(&@condition)
29
+ @df.where(query)
30
+ end
31
+
32
+ def `(vector_name)
33
+ if !@df.has_vector?(vector_name) && @df.has_vector?(vector_name.to_sym)
34
+ vector_name = vector_name.to_sym
35
+ end
36
+ VectorWrapper.new(@df[vector_name])
37
+ end
38
+
39
+ class VectorWrapper < SimpleDelegator
40
+ {
41
+ :== => :eq,
42
+ :!= => :not_eq,
43
+ :< => :lt,
44
+ :<= => :lteq,
45
+ :> => :mt,
46
+ :>= => :mteq,
47
+ :=~ => :in
48
+ }.each do |opt, method|
49
+ define_method opt do |*args|
50
+ send(method, *args)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -45,7 +45,7 @@ module Daru
45
45
  # Returns positions given categories or positions
46
46
  # @note If the argument does not a valid category it treats it as position
47
47
  # value and return it as it is.
48
- # @param [Array<object>] *indexes categories or positions
48
+ # @param indexes [Array<object>] categories or positions
49
49
  # @example
50
50
  # x = Daru::CategoricalIndex.new [:a, 1, :a, 1, :c]
51
51
  # x.pos :a, 1
@@ -145,7 +145,7 @@ module Daru
145
145
  end
146
146
 
147
147
  # Return subset given categories or positions
148
- # @param [Array<object>] *indexes categories or positions
148
+ # @param indexes [Array<object>] categories or positions
149
149
  # @return [Daru::CategoricalIndex] subset of the self containing the
150
150
  # mentioned categories or positions
151
151
  # @example
@@ -161,7 +161,7 @@ module Daru
161
161
 
162
162
  # Takes positional values and returns subset of the self
163
163
  # capturing the categories at mentioned positions
164
- # @param [Array<Integer>] positional values
164
+ # @param positions [Array<Integer>] positional values
165
165
  # @return [object] index object
166
166
  # @example
167
167
  # idx = Daru::CategoricalIndex.new [:a, :b, :a, :b, :c]
@@ -178,7 +178,7 @@ module Daru
178
178
  end
179
179
 
180
180
  # Add specified index values to the index object
181
- # @param [Array<object>] *indexes index values to add
181
+ # @param indexes [Array<object>] index values to add
182
182
  # @return [Daru::CategoricalIndex] index object with added values
183
183
  # @example
184
184
  # idx = Daru::CategoricalIndex.new [:a, :b, :a, :b, :c]
@@ -90,7 +90,7 @@ module Daru
90
90
  end
91
91
 
92
92
  # Returns true if all arguments are either a valid category or position
93
- # @param [Array<object>] *indexes categories or positions
93
+ # @param indexes [Array<object>] categories or positions
94
94
  # @return [true, false]
95
95
  # @example
96
96
  # idx.valid? :a, 2
@@ -104,7 +104,7 @@ module Daru
104
104
  # Returns positions given indexes or positions
105
105
  # @note If the arugent is both a valid index and a valid position,
106
106
  # it will treated as valid index
107
- # @param [Array<object>] *indexes indexes or positions
107
+ # @param indexes [Array<object>] indexes or positions
108
108
  # @example
109
109
  # x = Daru::Index.new [:a, :b, :c]
110
110
  # x.pos :a, 1
@@ -136,7 +136,7 @@ module Daru
136
136
 
137
137
  # Takes positional values and returns subset of the self
138
138
  # capturing the indexes at mentioned positions
139
- # @param [Array<Integer>] positional values
139
+ # @param positions [Array<Integer>] positional values
140
140
  # @return [object] index object
141
141
  # @example
142
142
  # idx = Daru::Index.new [:a, :b, :c]
@@ -218,7 +218,7 @@ module Daru
218
218
  # Return vector of booleans with value at ith position is either
219
219
  # true or false depending upon whether index value at position i is equal to
220
220
  # any of the values passed in the argument or not
221
- # @param [Array] *indexes values to equate with
221
+ # @param indexes [Array] values to equate with
222
222
  # @return [Daru::Vector] vector of boolean values
223
223
  # @example
224
224
  # dv = Daru::Index.new [1, 2, 3, :one, 'one']
@@ -258,7 +258,7 @@ module Daru
258
258
 
259
259
  # Provide an Index for sub vector produced
260
260
  #
261
- # @param input_indexes [Array] the input by user to index the vector
261
+ # @option * [Array] the input by user to index the vector
262
262
  # @return [Object] the Index object for sub vector produced
263
263
  def conform(*)
264
264
  self
@@ -152,7 +152,7 @@ module Daru
152
152
  # Returns positions given indexes or positions
153
153
  # @note If the arugent is both a valid index and a valid position,
154
154
  # it will treated as valid index
155
- # @param [Array<object>] *indexes indexes or positions
155
+ # @param indexes [Array<object>] indexes or positions
156
156
  # @example
157
157
  # idx = Daru::MultiIndex.from_tuples [[:a, :one], [:a, :two], [:b, :one], [:b, :two]]
158
158
  # idx.pos :a
@@ -177,7 +177,7 @@ module Daru
177
177
 
178
178
  # Takes positional values and returns subset of the self
179
179
  # capturing the indexes at mentioned positions
180
- # @param [Array<Integer>] positional values
180
+ # @param positions [Array<Integer>] positional values
181
181
  # @return [object] index object
182
182
  # @example
183
183
  # idx = Daru::MultiIndex.from_tuples [[:a, :one], [:a, :two], [:b, :one], [:b, :two]]
@@ -239,6 +239,15 @@ module Daru
239
239
  end
240
240
  end
241
241
 
242
+ def remove_layer layer_index
243
+ @levels.delete_at(layer_index)
244
+ @labels.delete_at(layer_index)
245
+ @name.delete_at(layer_index) unless @name.nil?
246
+
247
+ # CategoricalIndex is used , to allow duplicate indexes.
248
+ @levels.size == 1 ? Daru::CategoricalIndex.new(to_a.flatten) : self
249
+ end
250
+
242
251
  # Array `name` must have same length as levels and labels.
243
252
  def validate_name names, levels
244
253
  error_msg = "'names' and 'levels' should be of same size. Size of the "\
@@ -110,7 +110,7 @@ module Daru
110
110
 
111
111
  # Execute a query and create a data frame from the result
112
112
  #
113
- # @param dbh [DBI::DatabaseHandle, String] A DBI connection OR Path to a SQlite3 database.
113
+ # @param db [DBI::DatabaseHandle, String] A DBI connection OR Path to a SQlite3 database.
114
114
  # @param query [String] The query to be executed
115
115
  #
116
116
  # @return A dataframe containing the data resulting from the query
@@ -42,6 +42,34 @@ module Daru
42
42
  recode { |e| e.round(precision) unless e.nil? }
43
43
  end
44
44
 
45
+ # Add specified vector.
46
+ #
47
+ # @param other [Daru::Vector] The vector thats added to this.
48
+ # @param opts [Boolean] :skipnil if true treats nils as 0.
49
+ #
50
+ # @example
51
+ #
52
+ # v0 = Daru::Vector.new [1, 2, nil, nil]
53
+ # v1 = Daru::Vector.new [2, 1, 3, nil]
54
+ #
55
+ # irb> v0.add v1
56
+ # => #<Daru::Vector(4)>
57
+ # 0 3
58
+ # 1 3
59
+ # 2 nil
60
+ # 3 nil
61
+ #
62
+ # irb> v0.add v1, skipnil: true
63
+ # => #<Daru::Vector(4)>
64
+ # 0 3
65
+ # 1 3
66
+ # 2 3
67
+ # 3 0
68
+ #
69
+ def add other, opts={}
70
+ v2v_binary :+, other, skipnil: opts.fetch(:skipnil, false)
71
+ end
72
+
45
73
  private
46
74
 
47
75
  def math_unary_op operation
@@ -62,19 +90,27 @@ module Daru
62
90
  name: @name, index: @index
63
91
  end
64
92
 
65
- def v2v_binary operation, other
93
+ def v2v_binary operation, other, opts={}
66
94
  # FIXME: why the sorting?.. - zverok, 2016-05-18
67
95
  index = (@index.to_a | other.index.to_a).sort
68
96
 
69
97
  elements = index.map do |idx|
70
98
  this = self.index.include?(idx) ? self[idx] : nil
71
99
  that = other.index.include?(idx) ? other[idx] : nil
72
-
100
+ this, that = zero_nil_args(this, that, opts.fetch(:skipnil, false))
73
101
  this && that ? this.send(operation, that) : nil
74
102
  end
75
103
 
76
104
  Daru::Vector.new(elements, name: @name, index: index)
77
105
  end
106
+
107
+ def zero_nil_args(this, that, skipnil)
108
+ if skipnil
109
+ this = 0 if this.nil?
110
+ that = 0 if that.nil?
111
+ end
112
+ [this, that]
113
+ end
78
114
  end
79
115
  end
80
116
  end
@@ -41,35 +41,35 @@ 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.
72
+ # @param n [Integer] (10) Loopback length. Default to 10.
73
73
  %i[
74
74
  cumsum standardize acf ema rolling_mean rolling_median rolling_max
75
75
  rolling_min rolling_count rolling_std rolling_variance rolling_sum
@@ -66,7 +66,185 @@ module Daru
66
66
  reject_values(*Daru::MISSING_VALUES).uniq.reset_index!
67
67
  end
68
68
 
69
- # Returns the maximum value present in the vector.
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)]
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.
70
248
  #
71
249
  # @example
72
250
  #
@@ -77,29 +255,21 @@ module Daru
77
255
  # # d Daenerys
78
256
  # # j Jon Starkgaryen
79
257
  #
80
- # dv.max
81
- # #=> "Tyrion"
82
- #
83
- # dv.max(2) { |a,b| a.size <=> b.size }
84
- # #=> ["Jon Starkgaryen","Daenerys"]
258
+ # dv.index_of_max
259
+ # #=> :t
85
260
  #
86
- # dv.max(2) { |i| i.size }
87
- # #=> ["Jon Starkgaryen","Daenerys"]
88
- def max(size=nil, &block)
89
- data = @data.data.to_a
90
- data = if block_given?
91
- if block.parameters.count == 1 # Object block like { |x| x.size }
92
- data.sort_by(&block)
93
- else # Comparative block like { |a,b| a.size <=> b.size }
94
- data.sort(&block)
95
- end
96
- else
97
- data.sort
98
- end
99
- size.nil? ? data.last : data[data.count-size..-1].reverse
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)
100
267
  end
101
268
 
102
- # Returns the index of the maximum value present in the vector.
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.
103
273
  #
104
274
  # @example
105
275
  #
@@ -110,22 +280,18 @@ module Daru
110
280
  # # d Daenerys
111
281
  # # j Jon Starkgaryen
112
282
  #
113
- # dv.index_of_max
114
- # #=> :t
115
- #
116
- # dv.index_of_max(2) { |a,b| a.size <=> b.size }
117
- # #=> [:j, :d]
118
- #
119
- # dv.max(2) { |i| i.size }
283
+ # dv.index_of_max_by(2) { |i| i.size }
120
284
  # #=> [:j, :d]
121
- def index_of_max(size=nil,&block)
122
- data = @data.data.to_a
123
- indx = @index.to_a
124
- vals = max(size,&block)
125
- vals.is_a?(Array) ? (vals.map { |x| indx[data.index(x)] }) : indx[data.index(vals)]
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)
126
289
  end
127
290
 
128
- # Returns the minimum value present in the vector.
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.
129
295
  #
130
296
  # @example
131
297
  #
@@ -136,29 +302,21 @@ module Daru
136
302
  # # d Daenerys
137
303
  # # j Jon Starkgaryen
138
304
  #
139
- # dv.min
140
- # #=> "Daenerys"
141
- #
142
- # dv.min(2) { |a,b| a.size <=> b.size }
143
- # #=> ["Tyrion","Daenerys"]
305
+ # dv.index_of_min
306
+ # #=> :d
144
307
  #
145
- # dv.min(2) { |i| i.size }
146
- # #=> ["Tyrion","Daenerys"]
147
- def min(size=nil, &block)
148
- data = @data.data.to_a
149
- data = if block_given?
150
- if block.parameters.count == 1 # Object block like { |x| x.size }
151
- data.sort_by(&block)
152
- else # Comparative block like { |a,b| a.size <=> b.size }
153
- data.sort(&block)
154
- end
155
- else
156
- data.sort
157
- end
158
- size.nil? ? data.first : data[0..size-1]
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)
159
314
  end
160
315
 
161
- # Returns the index of the minimum value present in the vector.
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.
162
320
  #
163
321
  # @example
164
322
  #
@@ -169,19 +327,12 @@ module Daru
169
327
  # # d Daenerys
170
328
  # # j Jon Starkgaryen
171
329
  #
172
- # dv.index_of_min
173
- # #=> :d
174
- #
175
- # dv.index_of_min(2) { |a,b| a.size <=> b.size }
176
- # #=> [:t, :d]
177
- #
178
330
  # dv.index_of_min(2) { |i| i.size }
179
331
  # #=> [:t, :d]
180
- def index_of_min(size=nil,&block)
181
- data = @data.data.to_a
182
- indx = @index.to_a
183
- vals = min(size,&block)
184
- vals.is_a?(Array) ? (vals.map { |x| indx[data.index(x)] }) : indx[data.index(vals)]
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)
185
336
  end
186
337
 
187
338
  # Return the maximum element present in the Vector, as a Vector.
@@ -205,7 +356,7 @@ module Daru
205
356
  def proportions
206
357
  len = size - count_values(*Daru::MISSING_VALUES)
207
358
  frequencies.to_h.each_with_object({}) do |(el, count), hash|
208
- hash[el] = count / len
359
+ hash[el] = count / len.to_f
209
360
  end
210
361
  end
211
362
 
@@ -549,28 +700,28 @@ module Daru
549
700
 
550
701
  # @!method rolling_mean
551
702
  # Calculate rolling average
552
- # @param [Integer] n (10) Loopback length
703
+ # @yieldparam [Integer] n (10) Loopback length
553
704
  # @!method rolling_median
554
705
  # Calculate rolling median
555
- # @param [Integer] n (10) Loopback length
706
+ # @yieldparam [Integer] n (10) Loopback length
556
707
  # @!method rolling_count
557
708
  # Calculate rolling non-missing count
558
- # @param [Integer] n (10) Loopback length
709
+ # @yieldparam [Integer] n (10) Loopback length
559
710
  # @!method rolling_max
560
711
  # Calculate rolling max value
561
- # @param [Integer] n (10) Loopback length
712
+ # @yieldparam [Integer] n (10) Loopback length
562
713
  # @!method rolling_min
563
714
  # Calculate rolling min value
564
- # @param [Integer] n (10) Loopback length
715
+ # @yieldparam [Integer] n (10) Loopback length
565
716
  # @!method rolling_sum
566
717
  # Calculate rolling sum
567
- # @param [Integer] n (10) Loopback length
718
+ # @yieldparam [Integer] n (10) Loopback length
568
719
  # @!method rolling_std
569
720
  # Calculate rolling standard deviation
570
- # @param [Integer] n (10) Loopback length
721
+ # @yieldparam [Integer] n (10) Loopback length
571
722
  # @!method rolling_variance
572
723
  # Calculate rolling variance
573
- # @param [Integer] n (10) Loopback length
724
+ # @yieldparam [Integer] n (10) Loopback length
574
725
  %i[count mean median max min sum std variance].each do |meth|
575
726
  define_method("rolling_#{meth}".to_sym) do |n=10|
576
727
  rolling(meth, n)
@@ -791,10 +942,6 @@ module Daru
791
942
  alias :ss :sum_of_squares
792
943
  alias :percentil :percentile
793
944
  alias :se :standard_error
794
- alias :max_by :max
795
- alias :min_by :min
796
- alias :index_of_max_by :index_of_max
797
- alias :index_of_min_by :index_of_min
798
945
 
799
946
  private
800
947