Module: Daru::Maths::Statistics::Vector
- Included in:
- Vector
- Defined in:
- lib/daru/maths/statistics/vector.rb
Overview
rubocop:disable Metrics/ModuleLength
Instance Method Summary collapse
-
#acf(max_lags = nil) ⇒ Object
Calculates the autocorrelation coefficients of the series.
-
#acvf(demean = true, unbiased = true) ⇒ Object
Provides autocovariance.
- #average_deviation_population(m = nil) ⇒ Object (also: #adp)
-
#box_cox_transformation(lambda) ⇒ Object
:nocov:.
-
#center ⇒ Object
Center data by subtracting the mean from each non-nil value.
- #coefficient_of_variation ⇒ Object (also: #cov)
-
#count(value = false, &block) ⇒ Object
Retrieves number of cases which comply condition.
-
#covariance_population(other) ⇒ Object
Population covariance with denominator (N).
-
#covariance_sample(other) ⇒ Object
(also: #covariance)
Sample covariance with denominator (N-1).
-
#cumsum ⇒ Object
Calculate cumulative sum of Vector.
-
#describe(methods = nil) ⇒ Object
Create a summary of count, mean, standard deviation, min and max of the vector in one shot.
-
#dichotomize(low = nil) ⇒ Object
Dichotomize the vector with 0 and 1, based on lowest value.
-
#diff(max_lags = 1) ⇒ Daru::Vector
Performs the difference of the series.
-
#ema(n = 10, wilder = false) ⇒ Daru::Vector
Exponential Moving Average.
-
#emsd(n = 10, wilder = false) ⇒ Daru::Vector
Exponential Moving Standard Deviation.
-
#emv(n = 10, wilder = false) ⇒ Daru::Vector
Exponential Moving Variance.
-
#factors ⇒ Object
Retrieve unique values of non-nil data.
- #freqs ⇒ Object
- #frequencies ⇒ Object
- #kurtosis(m = nil) ⇒ Object
-
#macd(fast = 12, slow = 26, signal = 9) ⇒ Object
Moving Average Convergence-Divergence.
-
#max(return_type = :stored_type) ⇒ Object
Maximum element of the vector.
-
#max_index ⇒ Daru::Vector
Return a Vector with the max element and its index.
- #mean ⇒ Object
- #median ⇒ Object
- #median_absolute_deviation ⇒ Object (also: #mad)
- #min ⇒ Object
- #mode ⇒ Object
-
#percent_change(periods = 1) ⇒ Object
The percent_change method computes the percent change over the given number of periods.
-
#percentile(q, strategy = :midpoint) ⇒ Object
(also: #percentil)
Returns the value of the percentile q.
- #product ⇒ Object
- #proportion(value = 1) ⇒ Object
- #proportions ⇒ Object
- #range ⇒ Object
- #ranked ⇒ Object
-
#rolling(function, n = 10) ⇒ Daru::Vector
Calculate the rolling function for a loopback value.
-
#rolling_count ⇒ Object
Calculate rolling non-missing count.
-
#rolling_max ⇒ Object
Calculate rolling max value.
-
#rolling_mean ⇒ Object
Calculate rolling average.
-
#rolling_median ⇒ Object
Calculate rolling median.
-
#rolling_min ⇒ Object
Calculate rolling min value.
-
#rolling_std ⇒ Object
Calculate rolling standard deviation.
-
#rolling_sum ⇒ Object
Calculate rolling sum.
-
#rolling_variance ⇒ Object
Calculate rolling variance.
-
#sample_with_replacement(sample = 1) ⇒ Object
Returns an random sample of size n, with replacement, only with non-nil data.
-
#sample_without_replacement(sample = 1) ⇒ Object
Returns an random sample of size n, without replacement, only with valid data.
-
#skew(m = nil) ⇒ Object
Calculate skewness using (sigma(xi - mean)^3)/((N)*std_dev_sample^3).
- #standard_deviation_population(m = nil) ⇒ Object (also: #sdp)
- #standard_deviation_sample(m = nil) ⇒ Object (also: #sds, #sd)
- #standard_error ⇒ Object (also: #se)
-
#standardize(use_population = false) ⇒ Object
Standardize data.
- #sum ⇒ Object
- #sum_of_squared_deviation ⇒ Object
- #sum_of_squares(m = nil) ⇒ Object (also: #ss)
-
#value_counts ⇒ Object
Count number of occurrences of each value in the Vector.
-
#variance_population(m = nil) ⇒ Object
Population variance with denominator (N).
-
#variance_sample(m = nil) ⇒ Object
(also: #variance)
Sample variance with denominator (N-1).
- #vector_centered_compute(m) ⇒ Object
-
#vector_percentile ⇒ Object
Replace each non-nil value in the vector with its percentile.
- #vector_standardized_compute(m, sd) ⇒ Object
Instance Method Details
#acf(max_lags = nil) ⇒ Object
Calculates the autocorrelation coefficients of the series.
The first element is always 1, since that is the correlation of the series with itself.
612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/daru/maths/statistics/vector.rb', line 612 def acf(max_lags=nil) max_lags ||= (10 * Math.log10(size)).to_i (0..max_lags).map do |i| if i.zero? 1.0 else m = mean # can't use Pearson coefficient since the mean for the lagged series should # be the same as the regular series ((self - m) * (lag(i) - m)).sum / variance_sample / (size - 1) end end end |
#acvf(demean = true, unbiased = true) ⇒ Object
Provides autocovariance.
Options
-
:demean = true; optional. Supply false if series is not to be demeaned
-
:unbiased = true; optional. true/false for unbiased/biased form of autocovariance
Returns
Autocovariance value
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
# File 'lib/daru/maths/statistics/vector.rb', line 637 def acvf(demean=true, unbiased=true) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength opts = { demean: true, unbaised: true }.merge(opts) demean = opts[:demean] unbiased = opts[:unbiased] demeaned_series = demean ? self - mean : self n = (10 * Math.log10(size)).to_i + 1 m = mean d = if unbiased Array.new(size, size) else (1..size).to_a.reverse[0..n] end 0.upto(n - 1).map do |i| (demeaned_series * (lag(i) - m)).sum / d[i] end end |
#average_deviation_population(m = nil) ⇒ Object Also known as: adp
226 227 228 229 230 231 232 |
# File 'lib/daru/maths/statistics/vector.rb', line 226 def average_deviation_population m=nil must_be_numeric! m ||= mean reject_values(*Daru::MISSING_VALUES).data.inject(0) { |memo, val| (val - m).abs + memo }.quo(size - count_values(*Daru::MISSING_VALUES)) end |
#box_cox_transformation(lambda) ⇒ Object
:nocov:
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/daru/maths/statistics/vector.rb', line 292 def box_cox_transformation lambda # :nodoc: must_be_numeric! recode do |x| if !x.nil? if lambda.zero? Math.log(x) else (x ** lambda - 1).quo(lambda) end else nil end end end |
#center ⇒ Object
Center data by subtracting the mean from each non-nil value.
273 274 275 |
# File 'lib/daru/maths/statistics/vector.rb', line 273 def center self - mean end |
#coefficient_of_variation ⇒ Object Also known as: cov
117 118 119 |
# File 'lib/daru/maths/statistics/vector.rb', line 117 def coefficient_of_variation standard_deviation_sample / mean end |
#count(value = false, &block) ⇒ Object
Retrieves number of cases which comply condition. If block given, retrieves number of instances where block returns true. If other values given, retrieves the frequency for this value. If no value given, counts the number of non-nil elements in the Vector.
125 126 127 128 129 130 131 132 133 |
# File 'lib/daru/maths/statistics/vector.rb', line 125 def count value=false, &block if block_given? @data.select(&block).count elsif value count { |val| val == value } else size - indexes(*Daru::MISSING_VALUES).size end end |
#covariance_population(other) ⇒ Object
Population covariance with denominator (N)
175 176 177 178 |
# File 'lib/daru/maths/statistics/vector.rb', line 175 def covariance_population other size == other.size or raise ArgumentError, 'size of both the vectors must be equal' covariance_sum(other) / (size - count_values(*Daru::MISSING_VALUES)) end |
#covariance_sample(other) ⇒ Object Also known as: covariance
Sample covariance with denominator (N-1)
169 170 171 172 |
# File 'lib/daru/maths/statistics/vector.rb', line 169 def covariance_sample other size == other.size or raise ArgumentError, 'size of both the vectors must be equal' covariance_sum(other) / (size - count_values(*Daru::MISSING_VALUES) - 1) end |
#cumsum ⇒ Object
Calculate cumulative sum of Vector
661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/daru/maths/statistics/vector.rb', line 661 def cumsum result = [] acc = 0 @data.each do |d| if include_with_nan? Daru::MISSING_VALUES, d result << nil else acc += d result << acc end end Daru::Vector.new(result, index: @index) end |
#describe(methods = nil) ⇒ Object
Create a summary of count, mean, standard deviation, min and max of the vector in one shot.
Arguments
methods
- An array with aggregation methods specified as symbols to be applied to vectors. Default is [:count, :mean, :std, :max, :min]. Methods will be applied in the specified order.
44 45 46 47 48 |
# File 'lib/daru/maths/statistics/vector.rb', line 44 def describe methods=nil methods ||= [:count, :mean, :std, :min, :max] description = methods.map { |m| send(m) } Daru::Vector.new(description, index: methods, name: :statistics) end |
#dichotomize(low = nil) ⇒ Object
Dichotomize the vector with 0 and 1, based on lowest value. If parameter is defined, this value and lower will be 0 and higher, 1.
258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/daru/maths/statistics/vector.rb', line 258 def dichotomize(low=nil) low ||= factors.min recode do |x| if x.nil? nil elsif x > low 1 else 0 end end end |
#diff(max_lags = 1) ⇒ Daru::Vector
Performs the difference of the series. Note: The first difference of series is X(t) - X(t-1) But, second difference of series is NOT X(t) - X(t-2) It is the first difference of the first difference
> (X(t) - X(t-1)) - (X(t-1) - X(t-2))
Arguments
-
max_lags: integer, (default: 1), number of differences reqd.
414 415 416 417 418 419 420 421 422 |
# File 'lib/daru/maths/statistics/vector.rb', line 414 def diff(max_lags=1) ts = self difference = [] max_lags.times do difference = ts - ts.lag ts = difference end difference end |
#ema(n = 10, wilder = false) ⇒ Daru::Vector
Exponential Moving Average. Calculates an exponential moving average of the series using a specified parameter. If wilder is false (the default) then the EMA uses a smoothing value of 2 / (n + 1), if it is true then it uses the Welles Wilder smoother of 1 / n.
Warning for EMA usage: EMAs are unstable for small series, as they use a lot more than n observations to calculate. The series is stable if the size of the series is >= 3.45 * (n + 1)
498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/daru/maths/statistics/vector.rb', line 498 def ema(n=10, wilder=false) # rubocop:disable Metrics/AbcSize smoother = wilder ? 1.0 / n : 2.0 / (n + 1) # need to start everything from the first non-nil observation start = @data.index { |i| !i.nil? } # first n - 1 observations are nil base = [nil] * (start + n - 1) # nth observation is just a moving average base << @data[start...(start + n)].inject(0.0) { |s, a| a.nil? ? s : s + a } / n (start + n).upto size - 1 do |i| base << self[i] * smoother + (1 - smoother) * base.last end Daru::Vector.new(base, index: @index, name: @name) end |
#emsd(n = 10, wilder = false) ⇒ Daru::Vector
Exponential Moving Standard Deviation. Calculates an exponential moving standard deviation of the series using a specified parameter. If wilder is false (the default) then the EMSD uses a smoothing value of 2 / (n + 1), if it is true then it uses the Welles Wilder smoother of 1 / n.
569 570 571 572 573 574 575 576 |
# File 'lib/daru/maths/statistics/vector.rb', line 569 def emsd(n=10, wilder=false) result = [] emv_return = emv(n, wilder) emv_return.each do |d| result << (d.nil? ? nil : Math.sqrt(d)) end Daru::Vector.new(result, index: @index, name: @name) end |
#emv(n = 10, wilder = false) ⇒ Daru::Vector
Exponential Moving Variance. Calculates an exponential moving variance of the series using a specified parameter. If wilder is false (the default) then the EMV uses a smoothing value of 2 / (n + 1), if it is true then it uses the Welles Wilder smoother of 1 / n.
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/daru/maths/statistics/vector.rb', line 532 def emv(n=10, wilder=false) # rubocop:disable Metrics/AbcSize smoother = wilder ? 1.0 / n : 2.0 / (n + 1) # need to start everything from the first non-nil observation start = @data.index { |i| !i.nil? } # first n - 1 observations are nil var_base = [nil] * (start + n - 1) mean_base = [nil] * (start + n - 1) mean_base << @data[start...(start + n)].inject(0.0) { |s, a| a.nil? ? s : s + a } / n # nth observation is just a moving variance_population var_base << @data[start...(start + n)].inject(0.0) { |s,x| x.nil? ? s : s + (x - mean_base.last)**2 } / n (start + n).upto size - 1 do |i| last = mean_base.last mean_base << self[i] * smoother + (1 - smoother) * last var_base << (1 - smoother) * var_base.last + smoother * (self[i] - last) * (self[i] - mean_base.last) end Daru::Vector.new(var_base, index: @index, name: @name) end |
#factors ⇒ Object
Retrieve unique values of non-nil data
66 67 68 |
# File 'lib/daru/maths/statistics/vector.rb', line 66 def factors reject_values(*Daru::MISSING_VALUES).uniq.reset_index! end |
#freqs ⇒ Object
96 97 98 |
# File 'lib/daru/maths/statistics/vector.rb', line 96 def freqs Daru::Vector.new(frequencies) end |
#frequencies ⇒ Object
90 91 92 93 94 |
# File 'lib/daru/maths/statistics/vector.rb', line 90 def frequencies @data.each_with_object(Hash.new(0)) do |element, hash| hash[element] += 1 unless element.nil? end end |
#kurtosis(m = nil) ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'lib/daru/maths/statistics/vector.rb', line 216 def kurtosis m=nil if @data.respond_to? :kurtosis @data.kurtosis else m ||= mean fo = @data.inject(0) { |a, x| a + ((x - m) ** 4) } fo.quo((size - indexes(*Daru::MISSING_VALUES).size) * standard_deviation_sample(m) ** 4) - 3 end end |
#macd(fast = 12, slow = 26, signal = 9) ⇒ Object
Moving Average Convergence-Divergence. Calculates the MACD (moving average convergence-divergence) of the time series - this is a comparison of a fast EMA with a slow EMA.
Arguments
-
fast: integer, (default = 12) - fast component of MACD
-
slow: integer, (default = 26) - slow component of MACD
-
signal: integer, (default = 9) - signal component of MACD
Usage
ts = Daru::Vector.new((1..100).map { rand })
# => [0.69, 0.23, 0.44, 0.71, ...]
ts.macd(13)
Returns
Array of two Daru::Vectors - comparison of fast EMA with slow and EMA with signal value
597 598 599 600 |
# File 'lib/daru/maths/statistics/vector.rb', line 597 def macd(fast=12, slow=26, signal=9) series = ema(fast) - ema(slow) [series, series.ema(signal)] end |
#max(return_type = :stored_type) ⇒ Object
Maximum element of the vector.
75 76 77 78 79 80 81 82 |
# File 'lib/daru/maths/statistics/vector.rb', line 75 def max return_type=:stored_type max_value = @data.max if return_type == :vector Daru::Vector.new({index_of(max_value) => max_value}, name: @name, dtype: @dtype) else max_value end end |
#max_index ⇒ Daru::Vector
Return a Vector with the max element and its index.
86 87 88 |
# File 'lib/daru/maths/statistics/vector.rb', line 86 def max_index max :vector end |
#mean ⇒ Object
8 9 10 |
# File 'lib/daru/maths/statistics/vector.rb', line 8 def mean @data.mean end |
#median ⇒ Object
28 29 30 |
# File 'lib/daru/maths/statistics/vector.rb', line 28 def median @data.respond_to?(:median) ? @data.median : percentile(50) end |
#median_absolute_deviation ⇒ Object Also known as: mad
50 51 52 53 |
# File 'lib/daru/maths/statistics/vector.rb', line 50 def median_absolute_deviation m = median recode { |val| (val - m).abs }.median end |
#min ⇒ Object
20 21 22 |
# File 'lib/daru/maths/statistics/vector.rb', line 20 def min @data.min end |
#mode ⇒ Object
32 33 34 |
# File 'lib/daru/maths/statistics/vector.rb', line 32 def mode frequencies.max { |a,b| a[1]<=>b[1] }.first end |
#percent_change(periods = 1) ⇒ Object
The percent_change method computes the percent change over the given number of periods.
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/daru/maths/statistics/vector.rb', line 379 def percent_change periods=1 must_be_numeric! prev = nil arr = @data.each_with_index.map do |cur, i| if i < periods || include_with_nan?(Daru::MISSING_VALUES, cur) || include_with_nan?(Daru::MISSING_VALUES, prev) nil else (cur - prev) / prev.to_f end.tap { prev = cur if cur } end Daru::Vector.new(arr, index: @index, name: @name) end |
#percentile(q, strategy = :midpoint) ⇒ Object Also known as: percentil
Returns the value of the percentile q
Accepts an optional second argument specifying the strategy to interpolate when the requested percentile lies between two data points a and b Valid strategies are:
-
:midpoint (Default): (a + b) / 2
-
:linear : a + (b - a) * d where d is the decimal part of the index between a and b.
References
This is the NIST recommended method (en.wikipedia.org/wiki/Percentile#NIST_method)
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/daru/maths/statistics/vector.rb', line 244 def percentile(q, strategy=:midpoint) case strategy when :midpoint midpoint_percentile(q) when :linear linear_percentile(q) else raise ArgumentError, "Unknown strategy #{strategy}" end end |
#product ⇒ Object
16 17 18 |
# File 'lib/daru/maths/statistics/vector.rb', line 16 def product @data.product end |
#proportion(value = 1) ⇒ Object
144 145 146 |
# File 'lib/daru/maths/statistics/vector.rb', line 144 def proportion value=1 frequencies[value].quo(size - count_values(*Daru::MISSING_VALUES)).to_f end |
#proportions ⇒ Object
100 101 102 103 104 105 |
# File 'lib/daru/maths/statistics/vector.rb', line 100 def proportions len = size - count_values(*Daru::MISSING_VALUES) frequencies.each_with_object({}) do |(el, count), hash| hash[el] = count / len end end |
#range ⇒ Object
24 25 26 |
# File 'lib/daru/maths/statistics/vector.rb', line 24 def range max - min end |
#ranked ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/daru/maths/statistics/vector.rb', line 107 def ranked sum = 0 r = frequencies.sort.each_with_object({}) do |(el, count), memo| memo[el] = ((sum + 1) + (sum + count)).quo(2) sum += count end recode { |e| r[e] } end |
#rolling(function, n = 10) ⇒ Daru::Vector
Calculate the rolling function for a loopback value.
436 437 438 439 440 441 442 443 |
# File 'lib/daru/maths/statistics/vector.rb', line 436 def rolling function, n=10 Daru::Vector.new( [nil] * (n - 1) + (0..(size - n)).map do |i| Daru::Vector.new(@data[i...(i + n)]).send(function) end, index: @index ) end |
#rolling_count ⇒ Object
Calculate rolling non-missing count
469 470 471 472 473 |
# File 'lib/daru/maths/statistics/vector.rb', line 469 [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth| define_method("rolling_#{meth}".to_sym) do |n=10| rolling(meth, n) end end |
#rolling_max ⇒ Object
Calculate rolling max value
469 470 471 472 473 |
# File 'lib/daru/maths/statistics/vector.rb', line 469 [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth| define_method("rolling_#{meth}".to_sym) do |n=10| rolling(meth, n) end end |
#rolling_mean ⇒ Object
Calculate rolling average
469 470 471 472 473 |
# File 'lib/daru/maths/statistics/vector.rb', line 469 [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth| define_method("rolling_#{meth}".to_sym) do |n=10| rolling(meth, n) end end |
#rolling_median ⇒ Object
Calculate rolling median
469 470 471 472 473 |
# File 'lib/daru/maths/statistics/vector.rb', line 469 [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth| define_method("rolling_#{meth}".to_sym) do |n=10| rolling(meth, n) end end |
#rolling_min ⇒ Object
Calculate rolling min value
469 470 471 472 473 |
# File 'lib/daru/maths/statistics/vector.rb', line 469 [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth| define_method("rolling_#{meth}".to_sym) do |n=10| rolling(meth, n) end end |
#rolling_std ⇒ Object
Calculate rolling standard deviation
469 470 471 472 473 |
# File 'lib/daru/maths/statistics/vector.rb', line 469 [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth| define_method("rolling_#{meth}".to_sym) do |n=10| rolling(meth, n) end end |
#rolling_sum ⇒ Object
Calculate rolling sum
469 470 471 472 473 |
# File 'lib/daru/maths/statistics/vector.rb', line 469 [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth| define_method("rolling_#{meth}".to_sym) do |n=10| rolling(meth, n) end end |
#rolling_variance ⇒ Object
Calculate rolling variance
469 470 471 472 473 |
# File 'lib/daru/maths/statistics/vector.rb', line 469 [:count, :mean, :median, :max, :min, :sum, :std, :variance].each do |meth| define_method("rolling_#{meth}".to_sym) do |n=10| rolling(meth, n) end end |
#sample_with_replacement(sample = 1) ⇒ Object
Returns an random sample of size n, with replacement, only with non-nil data.
In all the trails, every item have the same probability of been selected.
338 339 340 341 342 343 344 345 346 |
# File 'lib/daru/maths/statistics/vector.rb', line 338 def sample_with_replacement(sample=1) if @data.respond_to? :sample_with_replacement @data.sample_with_replacement sample else valid = indexes(*Daru::MISSING_VALUES).empty? ? self : reject_values(*Daru::MISSING_VALUES) vds = valid.size (0...sample).collect { valid[rand(vds)] } end end |
#sample_without_replacement(sample = 1) ⇒ Object
Returns an random sample of size n, without replacement, only with valid data.
Every element could only be selected once.
A sample of the same size of the vector is the vector itself.
354 355 356 357 358 359 360 |
# File 'lib/daru/maths/statistics/vector.rb', line 354 def sample_without_replacement(sample=1) if @data.respond_to? :sample_without_replacement @data.sample_without_replacement sample else raw_sample_without_replacement(sample) end end |
#skew(m = nil) ⇒ Object
Calculate skewness using (sigma(xi - mean)^3)/((N)*std_dev_sample^3)
206 207 208 209 210 211 212 213 214 |
# File 'lib/daru/maths/statistics/vector.rb', line 206 def skew m=nil if @data.respond_to? :skew @data.skew else m ||= mean th = @data.inject(0) { |memo, val| memo + ((val - m)**3) } th.quo((size - indexes(*Daru::MISSING_VALUES).size) * (standard_deviation_sample(m)**3)) end end |
#standard_deviation_population(m = nil) ⇒ Object Also known as: sdp
187 188 189 190 191 192 193 194 |
# File 'lib/daru/maths/statistics/vector.rb', line 187 def standard_deviation_population m=nil m ||= mean if @data.respond_to? :standard_deviation_population @data.standard_deviation_population(m) else Math.sqrt(variance_population(m)) end end |
#standard_deviation_sample(m = nil) ⇒ Object Also known as: sds, sd
196 197 198 199 200 201 202 203 |
# File 'lib/daru/maths/statistics/vector.rb', line 196 def standard_deviation_sample m=nil m ||= mean if @data.respond_to? :standard_deviation_sample @data.standard_deviation_sample m else Math.sqrt(variance_sample(m)) end end |
#standard_error ⇒ Object Also known as: se
57 58 59 |
# File 'lib/daru/maths/statistics/vector.rb', line 57 def standard_error standard_deviation_sample/Math.sqrt(size - count_values(*Daru::MISSING_VALUES)) end |
#standardize(use_population = false) ⇒ Object
Standardize data.
Arguments
-
use_population - Pass as true if you want to use population
standard deviation instead of sample standard deviation.
283 284 285 286 287 288 289 |
# File 'lib/daru/maths/statistics/vector.rb', line 283 def standardize use_population=false m ||= mean sd = use_population ? sdp : sds return Daru::Vector.new([nil]*size) if m.nil? || sd == 0.0 vector_standardized_compute m, sd end |
#sum ⇒ Object
12 13 14 |
# File 'lib/daru/maths/statistics/vector.rb', line 12 def sum @data.sum end |
#sum_of_squared_deviation ⇒ Object
61 62 63 |
# File 'lib/daru/maths/statistics/vector.rb', line 61 def sum_of_squared_deviation (@data.inject(0) { |a,x| x**2 + a } - (sum**2).quo(size - count_values(*Daru::MISSING_VALUES)).to_f).to_f end |
#sum_of_squares(m = nil) ⇒ Object Also known as: ss
180 181 182 183 184 185 |
# File 'lib/daru/maths/statistics/vector.rb', line 180 def sum_of_squares(m=nil) m ||= mean reject_values(*Daru::MISSING_VALUES).data.inject(0) { |memo, val| memo + (val - m)**2 } end |
#value_counts ⇒ Object
Count number of occurrences of each value in the Vector
136 137 138 139 140 141 142 |
# File 'lib/daru/maths/statistics/vector.rb', line 136 def value_counts values = @data.each_with_object(Hash.new(0)) do |d, memo| memo[d] += 1 end Daru::Vector.new(values) end |
#variance_population(m = nil) ⇒ Object
Population variance with denominator (N)
159 160 161 162 163 164 165 166 |
# File 'lib/daru/maths/statistics/vector.rb', line 159 def variance_population m=nil m ||= mean if @data.respond_to? :variance_population @data.variance_population m else sum_of_squares(m).quo(size - count_values(*Daru::MISSING_VALUES)).to_f end end |
#variance_sample(m = nil) ⇒ Object Also known as: variance
Sample variance with denominator (N-1)
149 150 151 152 153 154 155 156 |
# File 'lib/daru/maths/statistics/vector.rb', line 149 def variance_sample m=nil m ||= mean if @data.respond_to? :variance_sample @data.variance_sample m else sum_of_squares(m).quo(size - count_values(*Daru::MISSING_VALUES) - 1) end end |
#vector_centered_compute(m) ⇒ Object
324 325 326 327 328 329 330 331 |
# File 'lib/daru/maths/statistics/vector.rb', line 324 def vector_centered_compute(m) if @data.respond_to? :vector_centered_compute @data.vector_centered_compute(m) else Daru::Vector.new @data.collect { |x| x.nil? ? nil : x.to_f-m }, index: index, name: name, dtype: dtype end end |
#vector_percentile ⇒ Object
Replace each non-nil value in the vector with its percentile.
310 311 312 313 |
# File 'lib/daru/maths/statistics/vector.rb', line 310 def vector_percentile c = size - indexes(*Daru::MISSING_VALUES).size ranked.recode! { |i| i.nil? ? nil : (i.quo(c)*100).to_f } end |
#vector_standardized_compute(m, sd) ⇒ Object
315 316 317 318 319 320 321 322 |
# File 'lib/daru/maths/statistics/vector.rb', line 315 def vector_standardized_compute(m,sd) if @data.respond_to? :vector_standardized_compute @data.vector_standardized_compute(m,sd) else Daru::Vector.new @data.collect { |x| x.nil? ? nil : (x.to_f - m).quo(sd) }, index: index, name: name, dtype: dtype end end |