Files
news.gitea.happylizard.me/bin/gems/facets-3.1.0/lib/standard/facets/math/variance.rb
T
pizza2d1 4cee170d66
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s
Add bin and edit workflow
2026-09-16 13:11:16 -06:00

32 lines
666 B
Ruby

require 'facets/math/summed_sqdevs'
module Math
#
def self.variance(array, &block)
sum2 = if block_given?
sum(array){ |i| j = block[i]; j*j }
else
sum(array){ |i| i**2 }
end
sum2/array.size - mean(array, &block)**2
end
# Variance of the sample.
# Variance of 0 or 1 elements is 0.0.
#
# TODO: Same as #variance? Then choose one.
def self.variance2(array)
return 0.0 if array.size < 2
summed_sqdevs(array) / (array.size - 1)
end
# Variance of a population.
# Variance of 0 or 1 elements is 0.0.
def self.pvariance(array)
return 0.0 if array.size < 2
summed_sqdevs(array) / array.size
end
end