Module: Daru::Maths::Statistics::DataFrame

Included in:
DataFrame
Defined in:
lib/daru/maths/statistics/dataframe.rb

Instance Method Summary collapse

Instance Method Details

#acfObject

Calculate Autocorrelation coefficient

Parameters:

  • max_lags (Integer)

    (nil) Number of initial lags



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

#correlationObject 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

#countObject

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

#covarianceObject 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

#cumsumObject

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

#emaObject

Calculate exponential moving average.

Parameters:

  • n (Integer)

    (10) Loopback length.

  • wilder (TrueClass, FalseClass, NilClass)

    (false) If true, 1/n value is used for smoothing; if false, uses 2/(n+1) value.



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

#meanObject

Calculate mean of numeric vectors.



6
7
8
# File 'lib/daru/maths/statistics/dataframe.rb', line 6

def mean
  compute_stats :mean
end

#minObject

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

#productObject

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_countObject

Calculate moving non-missing count

Parameters:

  • n (Integer)

    (10) Loopback length. Default to 10.



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_maxObject

Calculate moving max

Parameters:

  • n (Integer)

    (10) Loopback length. Default to 10.



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_meanObject

Calculate moving averages

Parameters:

  • n (Integer)

    (10) Loopback length. Default to 10.



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_medianObject

Calculate moving median

Parameters:

  • n (Integer)

    (10) Loopback length. Default to 10.



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_minObject

Calculate moving min

Parameters:

  • n (Integer)

    (10) Loopback length. Default to 10.



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_stdObject

Calculate moving standard deviation

Parameters:

  • n (Integer)

    (10) Loopback length. Default to 10.



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_varianceObject

Calculate moving variance

Parameters:

  • n (Integer)

    (10) Loopback length. Default to 10.



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

#standardizeObject

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

#stdObject

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

#sumObject

Calculate sum of numeric vectors



16
17
18
# File 'lib/daru/maths/statistics/dataframe.rb', line 16

def sum
  compute_stats :sum
end