class Statistics::Distribution::Normal
Attributes
mean[RW]
mode[RW]
standard_deviation[RW]
variance[RW]
Public Class Methods
new(avg, std)
click to toggle source
# File lib/statistics/distribution/normal.rb, line 7 def initialize(avg, std) self.mean = avg.to_r self.standard_deviation = std.to_r self.variance = std.to_r**2 end
Public Instance Methods
cumulative_function(value)
click to toggle source
# File lib/statistics/distribution/normal.rb, line 13 def cumulative_function(value) (1/2.0) * (1.0 + Math.erf((value - mean)/(standard_deviation * Math.sqrt(2.0)))) end
density_function(value)
click to toggle source
# File lib/statistics/distribution/normal.rb, line 17 def density_function(value) return 0 if standard_deviation <= 0 up_right = (value - mean)**2.0 down_right = 2.0 * variance right = Math.exp(-(up_right/down_right)) left_down = Math.sqrt(2.0 * Math::PI * variance) left_up = 1.0 (left_up/(left_down) * right) end
random(elements: 1, seed: Random.new_seed)
click to toggle source
Marsaglia polar method implementation for random gaussian (normal) number generation.
References: en.wikipedia.org/wiki/Marsaglia_polar_method math.stackexchange.com/questions/69245/transform-uniform-distribution-to-normal-distribution-using-lindeberg-l%C3%A9vy-clt www.projectrhea.org/rhea/index.php/The_principles_for_how_to_generate_random_samples_from_a_Gaussian_distribution
# File lib/statistics/distribution/normal.rb, line 35 def random(elements: 1, seed: Random.new_seed) results = [] # Setup seed srand(seed) # Number of random numbers to be generated. elements.times do x, y, r = 0.0, 0.0, 0.0 # Find an (x, y) point in the x^2 + y^2 < 1 circumference. loop do x = 2.0 * rand - 1.0 y = 2.0 * rand - 1.0 r = (x ** 2) + (y ** 2) break unless r >= 1.0 || r == 0 end # Project the random point to the required random distance r = Math.sqrt(-2.0 * Math.log(r) / r) # Transform the random distance to a gaussian value and append it to the results array results << mean + x * r * standard_deviation end if elements == 1 results.first else results end end