Module: Daru::Maths::Statistics::DataFrame
- Included in:
- DataFrame
- Defined in:
- lib/daru/maths/statistics/dataframe.rb
Instance Method Summary collapse
-
#acf ⇒ Object
Calculate Autocorrelation coefficient.
-
#correlation ⇒ Object
(also: #corr)
Calculate the correlation between the numeric vectors.
-
#count ⇒ Object
Count the number of non-nil values in each vector.
-
#covariance ⇒ Object
(also: #cov)
Calculate sample variance-covariance between the numeric vectors.
-
#cumsum ⇒ Object
Calculate cumulative sum of each numeric Vector.
-
#describe(methods = nil) ⇒ Object
Create a summary of mean, standard deviation, count, max and min of each numeric vector in the dataframe in one shot.
-
#ema ⇒ Object
Calculate exponential moving average.
-
#max(opts = {}) ⇒ Object
Calculate the maximum value of each numeric vector.
-
#mean ⇒ Object
Calculate mean of numeric vectors.
-
#min ⇒ Object
Calculate the minimmum value of each numeric vector.
-
#product ⇒ Object
Compute the product of each numeric vector.
-
#rolling_count ⇒ Object
Calculate moving non-missing count.
-
#rolling_max ⇒ Object
Calculate moving max.
-
#rolling_mean ⇒ Object
Calculate moving averages.
-
#rolling_median ⇒ Object
Calculate moving median.
-
#rolling_min ⇒ Object
Calculate moving min.
-
#rolling_std ⇒ Object
Calculate moving standard deviation.
-
#rolling_variance ⇒ Object
Calculate moving variance.
-
#standardize ⇒ Object
Standardize each Vector.
-
#std ⇒ Object
Calculate sample standard deviation of numeric vectors.
-
#sum ⇒ Object
Calculate sum of numeric vectors.
Instance Method Details
#acf ⇒ Object
Calculate Autocorrelation coefficient
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#correlation ⇒ Object Also known as: corr
Calculate the correlation between the numeric vectors.
130 131 132 133 134 135 136 137 138 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 130 def correlation standard_deviation = std.to_matrix corr_arry = (cov .to_matrix .elementwise_division(standard_deviation.transpose * standard_deviation)).to_a Daru::DataFrame.rows(corr_arry, index: numeric_vectors, order: numeric_vectors) end |
#count ⇒ Object
Count the number of non-nil values in each vector.
21 22 23 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 21 def count compute_stats :count end |
#covariance ⇒ Object Also known as: cov
Calculate sample variance-covariance between the numeric vectors.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 104 def covariance cache={} vectors = self.numeric_vectors mat_rows = vectors.collect do |row| vectors.collect do |col| if row == col self[row].variance else if cache[[col,row]].nil? cov = vector_cov(self[row],self[col]) cache[[row,col]] = cov cov else cache[[col,row]] end end end end Daru::DataFrame.rows(mat_rows, index: numeric_vectors, order: numeric_vectors) end |
#cumsum ⇒ Object
Calculate cumulative sum of each numeric Vector
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#describe(methods = nil) ⇒ Object
Create a summary of mean, standard deviation, count, max and min of each numeric vector in the dataframe in one shot.
Arguments
methods
- An array with aggregation methods specified as symbols to be applied to numeric vectors. Default is [:count, :mean, :std, :max, :min]. Methods will be applied in the specified order.
93 94 95 96 97 98 99 100 101 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 93 def describe methods=nil methods ||= [:count, :mean, :std, :min, :max] description_hash = {} numeric_vectors.each do |vec| description_hash[vec] = methods.map { |m| self[vec].send(m) } end Daru::DataFrame.new(description_hash, index: methods) end |
#ema ⇒ Object
Calculate exponential moving average.
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#max(opts = {}) ⇒ Object
Calculate the maximum value of each numeric vector.
26 27 28 29 30 31 32 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 26 def max opts={} if opts[:vector] self.row[*self[opts[:vector]].max_index.index.to_a] else compute_stats :max end end |
#mean ⇒ Object
Calculate mean of numeric vectors.
6 7 8 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 6 def mean compute_stats :mean end |
#min ⇒ Object
Calculate the minimmum value of each numeric vector.
35 36 37 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 35 def min compute_stats :min end |
#product ⇒ Object
Compute the product of each numeric vector.
40 41 42 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 40 def product compute_stats :product end |
#rolling_count ⇒ Object
Calculate moving non-missing count
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#rolling_max ⇒ Object
Calculate moving max
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#rolling_mean ⇒ Object
Calculate moving averages
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#rolling_median ⇒ Object
Calculate moving median
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#rolling_min ⇒ Object
Calculate moving min
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#rolling_std ⇒ Object
Calculate moving standard deviation
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#rolling_variance ⇒ Object
Calculate moving variance
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#standardize ⇒ Object
Standardize each Vector
77 78 79 80 81 82 83 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 77 [:cumsum,:standardize,:acf,:ema,:rolling_mean,:rolling_median,:rolling_max, :rolling_min,:rolling_count,:rolling_std,:rolling_variance, :rolling_sum ].each do |meth| define_method(meth) do |*args| apply_method_to_numerics meth, *args end end |
#std ⇒ Object
Calculate sample standard deviation of numeric vectors.
11 12 13 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 11 def std compute_stats :std end |
#sum ⇒ Object
Calculate sum of numeric vectors
16 17 18 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 16 def sum compute_stats :sum end |