一様分布

# -*- coding: UTF-8 -*-
from math import *
import numpy as np

##########################################
# 一様分布の計算(P(X = x), P(X < x))
#      x : データ
#      a,b : 区間
#      pr : P(X = x) 
#      return : P(X < x)
##########################################

def uniform(x, a, b, pr) :

	f = 0.0

	if x < a :
		pr[0] = 0.0
		f     = 0.0
	elif x > b :
		pr[0] = 0.0
		f     = 1.0
	else :
		y     = 1.0 / (b - a)
		pr[0] = y
		f     = y * (x - a)

	return f

----------------------------------

# -*- coding: UTF-8 -*-
import numpy as np
import sys
from math import *
from function import uniform

##########################################
# 一様分布の計算
#      coded by Y.Suganuma
##########################################

s = input("下限(a)は? ")
a = float(s)
s = input("上限(b)は? ")
b = float(s)
print("目的とする結果は? ")
print("     =0 : 確率の計算( P(X = x) 及び P(X < x) の値)")
s  = input("     =1 : p%値( P(X > u) = 0.01p となるuの値) ")
sw = int(s)
pr = np.empty(1, np.float)

if sw == 0 :
	s  = input("グラフ出力?(=1: yes,  =0: no) ")
	sw = int(s)
	if sw == 0 :
			# 密度関数と分布関数の値
		s = input("   データは? ")
		x = float(s)
		f = uniform(x, a, b, pr)
		print("P(X = " + str(x) + ") = " + str(pr[0]) + ",  P( X < " + str(x) + ") = " + str(f) + " (区間 [" + str(a) + ", " + str(b) + "])")
			# グラフ出力
	else :
		file1 = input("   密度関数のファイル名は? ")
		file2 = input("   分布関数のファイル名は? ")
		s     = input("   データの上限は? ")
		up    = int(s)
		s     = input("   刻み幅は? ")
		h     = float(s)
		out1  = open(file1, "w")
		out2  = open(file2, "w")
		x     = a
		while x < b+0.5*h :
			if x > b-0.5*h and x < b+0.5*h :
				x = b
			f = uniform(x, a, b, pr)
			out1.write(str(x) + " " + str(pr[0]) + "\n")
			out2.write(str(x) + " " + str(f) + "\n")
			x += h
		out1.close()
		out2.close()
			# %値
else :
	s = input("%の値は? ")
	x = float(s)
	p = 0.01 * x
	f = b - p * (b - a)
	print(str(x) + "%値 = " + str(f) + " (区間 [" + str(a) + ", " + str(b) + "])")