Skip to content

Instantly share code, notes, and snippets.

@noqisofon
Created November 18, 2010 02:43
Show Gist options
  • Save noqisofon/704552 to your computer and use it in GitHub Desktop.
Save noqisofon/704552 to your computer and use it in GitHub Desktop.
正規分布を求めるクラス。
#!/bin/ruby
# -*- encoding: shift_jis -*-
class GaussianDistribution
def initialize(mu = 0, sigma = 1)
@mu = mu.to_f
@sigma = sigma.to_f
end
def average
@mu
end
def distributed
@sigma ** 2
end
def prob(x)
1 / Math.sqrt( 2 * Math::PI * @sigma ) * Math.exp( -( (x - average) ** 2 / (2 * distributed) ) )
end
end
if __FILE__ == "ndist.rb" then
gd = GaussianDistribution.new
( -5.0..5.0 ).step( 0.1 ) do |x|
printf( "%.2f,%0.7f\n", x, gd.prob( x ) )
end
end
# file: ndist.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment