ポアソン分布

###########################
# ポアソン分布の計算
#      coded by Y.Suganuma
###########################

#############
# 階乗の計算
#############

def fact(n)
	x = 1.0
	for i1 in 2 ... n+1
		x *= i1
	end
	return x
end

##########################################
# ポアソン分布の計算(P(X = x), P(X < x))
#      x : データ
#      ram : パラメータ
#      pr : P(X = x)
#      return : P(X < x)
#      coded by Y.Suganuma
##########################################

def Poisson(x, ram, pr)

	f     = 0.0
	pr[0] = (ram ** x) * Math.exp(-ram) / fact(x)
	f     = pr[0]
	for i1 in 0 ... x
		f += (ram ** i1) * Math.exp(-ram) / fact(i1)
	end

	return f
end

print("λ は? ")
ram = Float(gets())
print("グラフ出力?(=1: yes,  =0: no) ")
sw = Integer(gets())
pr = Array.new(1)
			# 密度関数と分布関数の値
if sw == 0
	print("   データは? ")
	x = Integer(gets())
	f = Poisson(x, ram, pr)
	print("P(X = " + String(x) + ") = " + String(pr[0]) + ",  P( X < " + String(x) + ") = " + String(f) + " (λ = " + String(ram) + ")\n")
			# グラフ出力
else
	print("   密度関数のファイル名は? ")
	file1 = gets().strip()
	print("   分布関数のファイル名は? ")
	file2 = gets().strip()
	print("   データの上限は? ")
	up   = Integer(gets())
	out1 = open(file1, "w")
	out2 = open(file2, "w")
	for x in 0 ... up+1
		f = Poisson(x, ram, pr)
		out1.print(String(x) + " " + String(pr[0]) + "\n")
		out2.print(String(x) + " " + String(f) + "\n")
	end
	out1.close()
	out2.close()
end