Created
November 18, 2010 02:43
-
-
Save noqisofon/704552 to your computer and use it in GitHub Desktop.
正規分布を求めるクラス。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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