반응형
지수(Exponential)분포 난수 생성 방법
random.expovariate(lambda) 함수 사용
위키피디아 지수분포:
https://en.wikipedia.org/wiki/Exponential_distribution
지수분포 난수 생성 및 시각화 :
import random as r
import matplotlib.pyplot as plt
# 지수분포 (Exponential Distribution) 난수 생성
fig, axs = plt.subplots(nrows=2, ncols=2)
# 1. lambda = 0.5 인 경우
l = 0.5
rand = [r.expovariate(lambd=l) for i in range(100000)]
plt.subplot(221)
plt.hist(rand, bins=200)
ax = plt.gca()
ax.set_xlabel("random floating point numbers")
ax.set_ylabel("frequency")
ax.set_ylim(0,6000)
ax.set_xlim(0,20)
plt.title("expovariate("+str(l)+")")
ax.text(x=0.4, y=1500, s="lambda="+str(l))
# 2. lambda = 1 인 경우
l = 1.0
rand = [r.expovariate(lambd=l) for i in range(100000)]
plt.subplot(222)
plt.hist(rand, bins=200)
ax = plt.gca()
ax.set_xlabel("random floating point numbers")
ax.set_ylabel("frequency")
ax.set_ylim(0,6000)
ax.set_xlim(0,20)
plt.title("expovariate("+str(l)+")")
ax.text(x=0.4, y=1500, s="lambda="+str(l))
# 3. lambda = 1.5 인 경우
l = 1.5
rand = [r.expovariate(lambd=l) for i in range(100000)]
plt.subplot(223)
plt.hist(rand, bins=200)
ax = plt.gca()
ax.set_xlabel("random floating point numbers")
ax.set_ylabel("frequency")
ax.set_ylim(0,6000)
ax.set_xlim(0,20)
plt.title("expovariate("+str(l)+")")
ax.text(x=0.4, y=1500, s="lambda="+str(l))
# 4. lambda = 2.0 인 경우
l = 2.0
rand = [r.expovariate(lambd=l) for i in range(100000)]
plt.subplot(224)
plt.hist(rand, bins=200)
ax = plt.gca()
ax.set_xlabel("random floating point numbers")
ax.set_ylabel("frequency")
ax.set_ylim(0,6000)
ax.set_xlim(0,20)
plt.title("expovariate("+str(l)+")")
ax.text(x=0.4, y=1500, s="lambda="+str(l))
fig.suptitle("Exponential Distribution", fontsize=15)
plt.tight_layout()
감마(Gamma)분포 난수 생성 방법
random.gammavariate(a,b) 함수 사용
위키피디아 감마 분포 :
https://en.wikipedia.org/wiki/Gamma_distribution
감마분포 난수 생성 및 시각화 :
import random as r
import matplotlib.pyplot as plt
# 감마분포 (Gamma Distribution) 난수 생성
fig, axs = plt.subplots(nrows=2, ncols=2)
# 1. alpha = 1, beta = 2 인 경우
a = 1
b = 2
rand = [r.gammavariate(alpha=a, beta=b) for i in range(100000)]
plt.subplot(221)
plt.hist(rand, bins=200)
ax = plt.gca()
ax.set_xlabel("random floating point numbers")
ax.set_ylabel("frequency")
ax.set_ylim(0,6000)
ax.set_xlim(0,30)
plt.title("gammavariate("+str(a)+","+str(b)+")")
ax.text(x=0.4, y=1500, s="alpha="+str(a)+"\n"+"beta="+str(b))
# 1. alpha = 2, beta = 2 인 경우
a = 2
b = 2
rand = [r.gammavariate(alpha=a, beta=b) for i in range(100000)]
plt.subplot(222)
plt.hist(rand, bins=200)
ax = plt.gca()
ax.set_xlabel("random floating point numbers")
ax.set_ylabel("frequency")
ax.set_ylim(0,6000)
ax.set_xlim(0,30)
plt.title("gammavariate("+str(a)+","+str(b)+")")
ax.text(x=0.4, y=1500, s="alpha="+str(a)+"\n"+"beta="+str(b))
# 1. alpha = 1, beta = 2 인 경우
a = 3
b = 2
rand = [r.gammavariate(alpha=a, beta=b) for i in range(100000)]
plt.subplot(223)
plt.hist(rand, bins=200)
ax = plt.gca()
ax.set_xlabel("random floating point numbers")
ax.set_ylabel("frequency")
ax.set_ylim(0,6000)
ax.set_xlim(0,30)
plt.title("gammavariate("+str(a)+","+str(b)+")")
ax.text(x=0.4, y=1500, s="alpha="+str(a)+"\n"+"beta="+str(b))
# 1. alpha = 1, beta = 2 인 경우
a = 9
b = 0.5
rand = [r.gammavariate(alpha=a, beta=b) for i in range(100000)]
plt.subplot(224)
plt.hist(rand, bins=200)
ax = plt.gca()
ax.set_xlabel("random floating point numbers")
ax.set_ylabel("frequency")
ax.set_ylim(0,6000)
ax.set_xlim(0,30)
plt.title("gammavariate("+str(a)+","+str(b)+")")
ax.text(x=0.4, y=1500, s="alpha="+str(a)+"\n"+"beta="+str(b))
fig.suptitle("Gamma Distribution", fontsize=15)
plt.tight_layout()
반응형
'파이썬 > 난수 생성' 카테고리의 다른 글
파이썬 랜덤(Random) 뽑기 총정리 - 균등분포, 정규분포 등 다양한 통계 분포로 뽑는법 (2) | 2023.12.31 |
---|---|
파이썬 랜덤(Random) 뽑기 - 로그노멀분포, 파레토분포, 웨이블분포 (0) | 2023.12.30 |
파이썬 랜덤(Random) 뽑기 - 정규분포, 베타분포, 삼각분포 (2) | 2023.12.29 |
파이썬 랜덤(Random) 뽑기 - 균등분포 (0) | 2023.12.29 |