Module: Daru::Maths::Statistics::DataFrame
- Included in:
- DataFrame
- Defined in:
- lib/daru/maths/statistics/dataframe.rb
Instance Method Summary collapse
-
#acf(max_lags) ⇒ 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(n, wilder) ⇒ Object
Calculate exponential moving average.
-
#max(opts = {}) ⇒ Object
Calculate the maximum value of each numeric vector.
-
#mean ⇒ Object
Calculate mean of numeric vectors.
-
#median ⇒ Object
Calculate median of numeric vectors.
-
#min ⇒ Object
Calculate the minimum value of each numeric vector.
-
#mode ⇒ Object
Calculate mode of numeric vectors.
-
#percent_change(periods = 1) ⇒ Object
The percent_change method computes the percent change over the given number of periods for numeric vectors.
-
#product ⇒ Object
Compute the product of each numeric vector.
-
#range ⇒ Object
Calculate range of numeric vectors.
-
#rolling_count(n) ⇒ Object
Calculate moving non-missing count.
-
#rolling_max(n) ⇒ Object
Calculate moving max.
-
#rolling_mean(n) ⇒ Object
Calculate moving averages.
-
#rolling_median(n) ⇒ Object
Calculate moving median.
-
#rolling_min(n) ⇒ Object
Calculate moving min.
-
#rolling_std(n) ⇒ Object
Calculate moving standard deviation.
-
#rolling_variance(n) ⇒ Object
Calculate moving variance.
-
#standardize ⇒ Object
Standardize each Vector.
-
#std ⇒ Object
(also: #sds)
Calculate sample standard deviation of numeric vectors.
-
#sum ⇒ Object
Calculate sum of numeric vectors.
-
#variance_sample ⇒ Object
(also: #variance)
Calculate sample variance of numeric vectors.
Instance Method Details
#acf(max_lags) ⇒ Object
Calculate Autocorrelation coefficient
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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.
154 155 156 157 158 159 160 161 162 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 154 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
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#covariance ⇒ Object Also known as: cov
Calculate sample variance-covariance between the numeric vectors.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 134 def covariance cache = Hash.new do |h, (col, row)| value = vector_cov(self[row],self[col]) h[[col, row]] = value h[[row, col]] = value end vectors = numeric_vectors mat_rows = vectors.collect do |row| vectors.collect do |col| row == col ? self[row].variance : cache[[col,row]] end end Daru::DataFrame.rows(mat_rows, index: numeric_vectors, order: numeric_vectors) end |
#cumsum ⇒ Object
Calculate cumulative sum of each numeric Vector
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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.
90 91 92 93 94 95 96 97 98 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 90 def describe methods=nil methods ||= %i[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(n, wilder) ⇒ Object
Calculate exponential moving average.
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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.
32 33 34 35 36 37 38 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 32 def max opts={} if opts[:vector] row[*self[opts[:vector]].max_index.index.to_a] else compute_stats :max end end |
#mean ⇒ Object
Calculate mean of numeric vectors
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#median ⇒ Object
Calculate median of numeric vectors
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#min ⇒ Object
Calculate the minimum value of each numeric vector
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#mode ⇒ Object
Calculate mode of numeric vectors
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#percent_change(periods = 1) ⇒ Object
The percent_change method computes the percent change over the given number of periods for numeric vectors.
124 125 126 127 128 129 130 131 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 124 def percent_change periods=1 df_numeric = only_numerics.vectors.to_a df = Daru::DataFrame.new({}, order: @order, index: @index, name: @name) df_numeric.each do |vec| df[vec] = self[vec].percent_change periods end df end |
#product ⇒ Object
Compute the product of each numeric vector
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#range ⇒ Object
Calculate range of numeric vectors
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#rolling_count(n) ⇒ Object
Calculate moving non-missing count
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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(n) ⇒ Object
Calculate moving max
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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(n) ⇒ Object
Calculate moving averages
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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(n) ⇒ Object
Calculate moving median
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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(n) ⇒ Object
Calculate moving min
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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(n) ⇒ Object
Calculate moving standard deviation
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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(n) ⇒ Object
Calculate moving variance
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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
73 74 75 76 77 78 79 80 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 73 %i[ 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 Also known as: sds
Calculate sample standard deviation of numeric vectors
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#sum ⇒ Object
Calculate sum of numeric vectors
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |
#variance_sample ⇒ Object Also known as: variance
Calculate sample variance of numeric vectors
25 26 27 28 29 |
# File 'lib/daru/maths/statistics/dataframe.rb', line 25 %i[mean variance_sample range median mode std sum count min product].each do |meth| define_method(meth) do compute_stats meth end end |