daru 0.1.6 → 0.2.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/.github/ISSUE_TEMPLATE.md +18 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +5 -0
- data/History.md +28 -0
- data/README.md +6 -0
- data/ReleasePolicy.md +20 -0
- data/daru.gemspec +4 -0
- data/lib/daru.rb +1 -2
- data/lib/daru/category.rb +15 -10
- data/lib/daru/core/group_by.rb +51 -8
- data/lib/daru/dataframe.rb +267 -28
- data/lib/daru/date_time/index.rb +1 -1
- data/lib/daru/date_time/offsets.rb +1 -1
- data/lib/daru/extensions/which_dsl.rb +55 -0
- data/lib/daru/index/categorical_index.rb +4 -4
- data/lib/daru/index/index.rb +5 -5
- data/lib/daru/index/multi_index.rb +11 -2
- data/lib/daru/io/io.rb +1 -1
- data/lib/daru/maths/arithmetic/vector.rb +38 -2
- data/lib/daru/maths/statistics/dataframe.rb +19 -19
- data/lib/daru/maths/statistics/vector.rb +225 -78
- data/lib/daru/plotting/nyaplot/dataframe.rb +11 -0
- data/lib/daru/vector.rb +55 -13
- data/lib/daru/version.rb +1 -1
- data/profile/vector_new.rb +9 -0
- data/spec/category_spec.rb +5 -1
- data/spec/core/group_by_spec.rb +128 -0
- data/spec/dataframe_spec.rb +125 -10
- data/spec/extensions/which_dsl_spec.rb +38 -0
- data/spec/fixtures/duplicates.csv +32 -0
- data/spec/io/io_spec.rb +2 -2
- data/spec/maths/arithmetic/vector_spec.rb +18 -0
- data/spec/maths/statistics/vector_spec.rb +54 -38
- data/spec/plotting/nyaplot/dataframe_spec.rb +23 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/vector_spec.rb +39 -0
- metadata +25 -3
data/lib/daru/date_time/index.rb
CHANGED
@@ -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
|
-
#
|
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>]
|
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>]
|
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>]
|
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]
|
data/lib/daru/index/index.rb
CHANGED
@@ -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>]
|
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>]
|
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]
|
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
|
-
# @
|
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>]
|
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 "\
|
data/lib/daru/io/io.rb
CHANGED
@@ -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
|
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]
|
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]
|
50
|
-
# @param [TrueClass, FalseClass, NilClass]
|
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]
|
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]
|
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]
|
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]
|
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]
|
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]
|
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]
|
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
|
-
|
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.
|
81
|
-
# #=>
|
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.
|
87
|
-
# #=> [
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
|
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.
|
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
|
122
|
-
|
123
|
-
|
124
|
-
vals
|
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.
|
140
|
-
# #=>
|
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.
|
146
|
-
# #=> [
|
147
|
-
def
|
148
|
-
|
149
|
-
|
150
|
-
|
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
|
181
|
-
|
182
|
-
|
183
|
-
vals
|
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
|
-
# @
|
703
|
+
# @yieldparam [Integer] n (10) Loopback length
|
553
704
|
# @!method rolling_median
|
554
705
|
# Calculate rolling median
|
555
|
-
# @
|
706
|
+
# @yieldparam [Integer] n (10) Loopback length
|
556
707
|
# @!method rolling_count
|
557
708
|
# Calculate rolling non-missing count
|
558
|
-
# @
|
709
|
+
# @yieldparam [Integer] n (10) Loopback length
|
559
710
|
# @!method rolling_max
|
560
711
|
# Calculate rolling max value
|
561
|
-
# @
|
712
|
+
# @yieldparam [Integer] n (10) Loopback length
|
562
713
|
# @!method rolling_min
|
563
714
|
# Calculate rolling min value
|
564
|
-
# @
|
715
|
+
# @yieldparam [Integer] n (10) Loopback length
|
565
716
|
# @!method rolling_sum
|
566
717
|
# Calculate rolling sum
|
567
|
-
# @
|
718
|
+
# @yieldparam [Integer] n (10) Loopback length
|
568
719
|
# @!method rolling_std
|
569
720
|
# Calculate rolling standard deviation
|
570
|
-
# @
|
721
|
+
# @yieldparam [Integer] n (10) Loopback length
|
571
722
|
# @!method rolling_variance
|
572
723
|
# Calculate rolling variance
|
573
|
-
# @
|
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
|
|