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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +30 -0
  3. data/README.md +1 -1
  4. data/lib/daru_lite/data_frame/convertible.rb +0 -33
  5. data/lib/daru_lite/data_frame/fetchable.rb +1 -1
  6. data/lib/daru_lite/data_frame/i_o_able.rb +0 -102
  7. data/lib/daru_lite/data_frame/indexable.rb +24 -0
  8. data/lib/daru_lite/data_frame/relabelable.rb +125 -0
  9. data/lib/daru_lite/data_frame/sortable.rb +27 -1
  10. data/lib/daru_lite/dataframe.rb +3 -4
  11. data/lib/daru_lite/index/categorical_index.rb +8 -0
  12. data/lib/daru_lite/index/index.rb +0 -1
  13. data/lib/daru_lite/index/multi_index.rb +8 -5
  14. data/lib/daru_lite/io/io.rb +0 -195
  15. data/lib/daru_lite/maths/arithmetic/vector.rb +31 -0
  16. data/lib/daru_lite/maths/statistics/vector/descriptive.rb +134 -0
  17. data/lib/daru_lite/maths/statistics/vector/dispersion.rb +120 -0
  18. data/lib/daru_lite/maths/statistics/vector/extrema.rb +217 -0
  19. data/lib/daru_lite/maths/statistics/vector/normalization.rb +86 -0
  20. data/lib/daru_lite/maths/statistics/vector/percentile.rb +64 -0
  21. data/lib/daru_lite/maths/statistics/vector/sampling.rb +56 -0
  22. data/lib/daru_lite/maths/statistics/vector/time_series.rb +350 -0
  23. data/lib/daru_lite/maths/statistics/vector.rb +16 -994
  24. data/lib/daru_lite/vector/convertible.rb +0 -32
  25. data/lib/daru_lite/vector.rb +0 -9
  26. data/lib/daru_lite/version.rb +1 -1
  27. data/lib/daru_lite.rb +0 -30
  28. metadata +9 -75
  29. data/lib/daru_lite/date_time/index.rb +0 -570
  30. data/lib/daru_lite/date_time/offsets.rb +0 -397
  31. data/lib/daru_lite/extensions/which_dsl.rb +0 -53
  32. data/lib/daru_lite/io/sql_data_source.rb +0 -80
  33. data/lib/daru_lite/iruby/helpers.rb +0 -38
  34. data/lib/daru_lite/iruby/templates/dataframe.html.erb +0 -5
  35. data/lib/daru_lite/iruby/templates/dataframe_mi.html.erb +0 -5
  36. data/lib/daru_lite/iruby/templates/dataframe_mi_tbody.html.erb +0 -35
  37. data/lib/daru_lite/iruby/templates/dataframe_mi_thead.html.erb +0 -21
  38. data/lib/daru_lite/iruby/templates/dataframe_tbody.html.erb +0 -28
  39. data/lib/daru_lite/iruby/templates/dataframe_thead.html.erb +0 -21
  40. data/lib/daru_lite/iruby/templates/multi_index.html.erb +0 -12
  41. data/lib/daru_lite/iruby/templates/vector.html.erb +0 -5
  42. data/lib/daru_lite/iruby/templates/vector_mi.html.erb +0 -5
  43. data/lib/daru_lite/iruby/templates/vector_mi_tbody.html.erb +0 -26
  44. data/lib/daru_lite/iruby/templates/vector_mi_thead.html.erb +0 -8
  45. data/lib/daru_lite/iruby/templates/vector_tbody.html.erb +0 -17
  46. data/lib/daru_lite/iruby/templates/vector_thead.html.erb +0 -8
@@ -0,0 +1,217 @@
1
+ module DaruLite
2
+ module Maths
3
+ module Statistics
4
+ module Vector
5
+ # Largest and smallest values, and where they are.
6
+ module Extrema
7
+ # Returns the maximum value(s) present in the vector, with an optional comparator block.
8
+ #
9
+ # @param size [Integer] Number of maximum values to return. Defaults to nil.
10
+ #
11
+ # @example
12
+ #
13
+ # dv = DaruLite::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]),
14
+ # index: DaruLite::Index.new([:t, :d, :j])
15
+ # #=>
16
+ # # #<DaruLite::Vector(3)>
17
+ # # t Tyrion
18
+ # # d Daenerys
19
+ # # j Jon Starkgaryen
20
+ #
21
+ # dv.max
22
+ # #=> "Tyrion"
23
+ #
24
+ # dv.max(2) { |a,b| a.size <=> b.size }
25
+ # #=> ["Jon Starkgaryen","Daenerys"]
26
+ def max(size = nil, &)
27
+ reject_values(*DaruLite::MISSING_VALUES).to_a.max(size, &)
28
+ end
29
+
30
+ # Returns the maximum value(s) present in the vector, with a compulsory object block.
31
+ #
32
+ # @param size [Integer] Number of maximum values to return. Defaults to nil.
33
+ #
34
+ # @example
35
+ #
36
+ # dv = DaruLite::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]),
37
+ # index: DaruLite::Index.new([:t, :d, :j])
38
+ # #=>
39
+ # # #<DaruLite::Vector(3)>
40
+ # # t Tyrion
41
+ # # d Daenerys
42
+ # # j Jon Starkgaryen
43
+ #
44
+ # dv.max_by(2) { |i| i.size }
45
+ # #=> ["Jon Starkgaryen","Daenerys"]
46
+ def max_by(size = nil, &block)
47
+ raise ArgumentError, 'Expected compulsory object block in max_by method' unless block
48
+
49
+ reject_values(*DaruLite::MISSING_VALUES).to_a.max_by(size, &block)
50
+ end
51
+
52
+ # Returns the minimum value(s) present in the vector, with an optional comparator block.
53
+ #
54
+ # @param size [Integer] Number of minimum values to return. Defaults to nil.
55
+ #
56
+ # @example
57
+ #
58
+ # dv = DaruLite::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]),
59
+ # index: DaruLite::Index.new([:t, :d, :j])
60
+ # #=>
61
+ # # #<DaruLite::Vector(3)>
62
+ # # t Tyrion
63
+ # # d Daenerys
64
+ # # j Jon Starkgaryen
65
+ #
66
+ # dv.min
67
+ # #=> "Daenerys"
68
+ #
69
+ # dv.min(2) { |a,b| a.size <=> b.size }
70
+ # #=> ["Tyrion","Daenerys"]
71
+ def min(size = nil, &)
72
+ reject_values(*DaruLite::MISSING_VALUES).to_a.min(size, &)
73
+ end
74
+
75
+ # Returns the minimum value(s) present in the vector, with a compulsory object block.
76
+ #
77
+ # @param size [Integer] Number of minimum values to return. Defaults to nil.
78
+ #
79
+ # @example
80
+ #
81
+ # dv = DaruLite::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]),
82
+ # index: DaruLite::Index.new([:t, :d, :j])
83
+ # #=>
84
+ # # #<DaruLite::Vector(3)>
85
+ # # t Tyrion
86
+ # # d Daenerys
87
+ # # j Jon Starkgaryen
88
+ #
89
+ # dv.min_by(2) { |i| i.size }
90
+ # #=> ["Tyrion","Daenerys"]
91
+ def min_by(size = nil, &block)
92
+ raise ArgumentError, 'Expected compulsory object block in min_by method' unless block
93
+
94
+ reject_values(*DaruLite::MISSING_VALUES).to_a.min_by(size, &block)
95
+ end
96
+
97
+ # Returns the index of the maximum value(s) present in the vector, with an optional
98
+ # comparator block.
99
+ #
100
+ # @param size [Integer] Number of maximum indices to return. Defaults to nil.
101
+ #
102
+ # @example
103
+ #
104
+ # dv = DaruLite::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]),
105
+ # index: DaruLite::Index.new([:t, :d, :j])
106
+ # #=>
107
+ # # #<DaruLite::Vector(3)>
108
+ # # t Tyrion
109
+ # # d Daenerys
110
+ # # j Jon Starkgaryen
111
+ #
112
+ # dv.index_of_max
113
+ # #=> :t
114
+ #
115
+ # dv.index_of_max(2) { |a,b| a.size <=> b.size }
116
+ # #=> [:j, :d]
117
+ def index_of_max(size = nil, &)
118
+ vals = max(size, &)
119
+ indexes_of_values(vals)
120
+ end
121
+
122
+ # Returns the index of the maximum value(s) present in the vector, with a compulsory
123
+ # object block.
124
+ #
125
+ # @param size [Integer] Number of maximum indices to return. Defaults to nil.
126
+ #
127
+ # @example
128
+ #
129
+ # dv = DaruLite::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]),
130
+ # index: DaruLite::Index.new([:t, :d, :j])
131
+ # #=>
132
+ # # #<DaruLite::Vector(3)>
133
+ # # t Tyrion
134
+ # # d Daenerys
135
+ # # j Jon Starkgaryen
136
+ #
137
+ # dv.index_of_max_by(2) { |i| i.size }
138
+ # #=> [:j, :d]
139
+ def index_of_max_by(size = nil, &)
140
+ vals = max_by(size, &)
141
+ indexes_of_values(vals)
142
+ end
143
+
144
+ # Returns the index of the minimum value(s) present in the vector, with an optional
145
+ # comparator block.
146
+ #
147
+ # @param size [Integer] Number of minimum indices to return. Defaults to nil.
148
+ #
149
+ # @example
150
+ #
151
+ # dv = DaruLite::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]),
152
+ # index: DaruLite::Index.new([:t, :d, :j])
153
+ # #=>
154
+ # # #<DaruLite::Vector(3)>
155
+ # # t Tyrion
156
+ # # d Daenerys
157
+ # # j Jon Starkgaryen
158
+ #
159
+ # dv.index_of_min
160
+ # #=> :d
161
+ #
162
+ # dv.index_of_min(2) { |a,b| a.size <=> b.size }
163
+ # #=> [:t, :d]
164
+ def index_of_min(size = nil, &)
165
+ vals = min(size, &)
166
+ indexes_of_values(vals)
167
+ end
168
+
169
+ # Returns the index of the minimum value(s) present in the vector, with a compulsory
170
+ # object block.
171
+ #
172
+ # @param size [Integer] Number of minimum indices to return. Defaults to nil.
173
+ #
174
+ # @example
175
+ #
176
+ # dv = DaruLite::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]),
177
+ # index: DaruLite::Index.new([:t, :d, :j])
178
+ # #=>
179
+ # # #<DaruLite::Vector(3)>
180
+ # # t Tyrion
181
+ # # d Daenerys
182
+ # # j Jon Starkgaryen
183
+ #
184
+ # dv.index_of_min(2) { |i| i.size }
185
+ # #=> [:t, :d]
186
+ def index_of_min_by(size = nil, &)
187
+ vals = min_by(size, &)
188
+ indexes_of_values(vals)
189
+ end
190
+
191
+ # Return the maximum element present in the Vector, as a Vector.
192
+ # @return [DaruLite::Vector]
193
+ def max_index
194
+ max_value = @data.max
195
+ DaruLite::Vector.new({ index_of(max_value) => max_value }, name: @name, dtype: @dtype)
196
+ end
197
+
198
+ private
199
+
200
+ # Map each value to the index of a distinct non-missing element, so tied
201
+ # values get different indexes. An element of the same class is preferred,
202
+ # so that 1 and 1.0 each get their own index.
203
+ def indexes_of_values(vals)
204
+ dv = reject_values(*DaruLite::MISSING_VALUES)
205
+ return dv.index_of(vals) unless vals.is_a?(Array)
206
+
207
+ remaining = dv.index.to_a.zip(dv.to_a)
208
+ vals.map do |val|
209
+ position = remaining.index { |_, v| v.eql?(val) } || remaining.index { |_, v| v == val }
210
+ remaining.delete_at(position).first
211
+ end
212
+ end
213
+ end
214
+ end
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,86 @@
1
+ module DaruLite
2
+ module Maths
3
+ module Statistics
4
+ module Vector
5
+ # Centering, standardizing and other value transformations.
6
+ module Normalization
7
+ # Dichotomize the vector with 0 and 1, based on lowest value.
8
+ # If parameter is defined, this value and lower will be 0
9
+ # and higher, 1.
10
+ def dichotomize(low = nil)
11
+ low ||= factors.min
12
+
13
+ recode do |x|
14
+ if x.nil?
15
+ nil
16
+ elsif x > low
17
+ 1
18
+ else
19
+ 0
20
+ end
21
+ end
22
+ end
23
+
24
+ # Center data by subtracting the mean from each non-nil value.
25
+ def center
26
+ self - mean
27
+ end
28
+
29
+ # Standardize data.
30
+ #
31
+ # == Arguments
32
+ #
33
+ # * use_population - Pass as *true* if you want to use population
34
+ # standard deviation instead of sample standard deviation.
35
+ def standardize(use_population = false)
36
+ m ||= mean
37
+ sd = use_population ? sdp : sds
38
+ return DaruLite::Vector.new([nil] * size) if m.nil? || sd.nil? || sd.zero?
39
+
40
+ vector_standardized_compute m, sd
41
+ end
42
+
43
+ # simplecov:disable
44
+ def box_cox_transformation(lambda) # :nodoc:
45
+ must_be_numeric!
46
+
47
+ recode do |x|
48
+ unless x.nil?
49
+ if lambda.zero?
50
+ Math.log(x)
51
+ else
52
+ ((x**lambda) - 1).quo(lambda)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ # simplecov:enable
58
+
59
+ # Replace each non-nil value in the vector with its percentile.
60
+ def vector_percentile
61
+ c = size - indexes(*DaruLite::MISSING_VALUES).size
62
+ ranked.recode! { |i| i.nil? ? nil : (i.quo(c) * 100).to_f }
63
+ end
64
+
65
+ def vector_standardized_compute(m, sd)
66
+ if @data.respond_to? :vector_standardized_compute
67
+ @data.vector_standardized_compute(m, sd)
68
+ else
69
+ DaruLite::Vector.new @data.collect { |x| x.nil? ? nil : (x.to_f - m).quo(sd) },
70
+ index: index, name: name, dtype: dtype
71
+ end
72
+ end
73
+
74
+ def vector_centered_compute(m)
75
+ if @data.respond_to? :vector_centered_compute
76
+ @data.vector_centered_compute(m)
77
+ else
78
+ DaruLite::Vector.new @data.collect { |x| x.nil? ? nil : x.to_f - m },
79
+ index: index, name: name, dtype: dtype
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,64 @@
1
+ module DaruLite
2
+ module Maths
3
+ module Statistics
4
+ module Vector
5
+ # Percentiles and their interpolation strategies.
6
+ module Percentile
7
+ # Returns the value of the percentile q
8
+ #
9
+ # Accepts an optional second argument specifying the strategy to interpolate
10
+ # when the requested percentile lies between two data points a and b
11
+ # Valid strategies are:
12
+ # * :midpoint (Default): (a + b) / 2
13
+ # * :linear : a + (b - a) * d where d is the decimal part of the index between a and b.
14
+ # == References
15
+ #
16
+ # This is the NIST recommended method (http://en.wikipedia.org/wiki/Percentile#NIST_method)
17
+ def percentile(q, strategy = :midpoint)
18
+ case strategy
19
+ when :midpoint
20
+ midpoint_percentile(q)
21
+ when :linear
22
+ linear_percentile(q)
23
+ else
24
+ raise ArgumentError, "Unknown strategy #{strategy}"
25
+ end
26
+ end
27
+
28
+ alias percentil percentile
29
+
30
+ private
31
+
32
+ def midpoint_percentile(q)
33
+ raise ArgumentError, "q must be between 0 and 100, got #{q}" unless q.between?(0, 100)
34
+
35
+ sorted = reject_values(*DaruLite::MISSING_VALUES).to_a.sort
36
+
37
+ v = ((size - count_values(*DaruLite::MISSING_VALUES)) * q).quo(100)
38
+ if v.to_i == v
39
+ (sorted[(v - 0.5).to_i].to_f + sorted[[(v + 0.5).to_i, sorted.size - 1].min]).quo(2)
40
+ else
41
+ sorted[v.to_i]
42
+ end
43
+ end
44
+
45
+ def linear_percentile(q)
46
+ sorted = reject_values(*DaruLite::MISSING_VALUES).to_a.sort
47
+ index = (q / 100.0) * ((size - count_values(*DaruLite::MISSING_VALUES)) + 1)
48
+
49
+ k = index.truncate
50
+ d = index % 1
51
+
52
+ if k.zero?
53
+ sorted[0]
54
+ elsif k >= sorted.size
55
+ sorted[-1]
56
+ else
57
+ sorted[k - 1] + (d * (sorted[k] - sorted[k - 1]))
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,56 @@
1
+ module DaruLite
2
+ module Maths
3
+ module Statistics
4
+ module Vector
5
+ # Random samples of the non-missing values.
6
+ module Sampling
7
+ # Returns an random sample of size n, with replacement,
8
+ # only with non-nil data.
9
+ #
10
+ # In all the trails, every item have the same probability
11
+ # of been selected.
12
+ def sample_with_replacement(sample = 1)
13
+ if @data.respond_to? :sample_with_replacement
14
+ @data.sample_with_replacement sample
15
+ else
16
+ valid = indexes(*DaruLite::MISSING_VALUES).empty? ? self : reject_values(*DaruLite::MISSING_VALUES)
17
+ vds = valid.size
18
+ (0...sample).collect { valid[rand(vds)] }
19
+ end
20
+ end
21
+
22
+ # Returns an random sample of size n, without replacement,
23
+ # only with valid data.
24
+ #
25
+ # Every element could only be selected once.
26
+ #
27
+ # A sample of the same size of the vector is the vector itself.
28
+ def sample_without_replacement(sample = 1)
29
+ if @data.respond_to? :sample_without_replacement
30
+ @data.sample_without_replacement sample
31
+ else
32
+ raw_sample_without_replacement(sample)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def raw_sample_without_replacement(sample)
39
+ valid = indexes(*DaruLite::MISSING_VALUES).empty? ? self : reject_values(*DaruLite::MISSING_VALUES)
40
+ raise ArgumentError, "Sample size couldn't be greater than n" if
41
+ sample > valid.size
42
+
43
+ out = []
44
+ size = valid.size
45
+ while out.size < sample
46
+ value = rand(size)
47
+ out.push(value) unless out.include?(value)
48
+ end
49
+
50
+ out.collect { |i| valid[i] }
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end