본문 바로가기

파이썬/난수 생성

파이썬 랜덤(Random) 뽑기 - 지수분포, 감마분포

반응형

 

지수(Exponential)분포 난수 생성 방법

random.expovariate(lambda) 함수 사용

지수분포 난수 생성. 람다 값에 따라 분포가 바뀐다.

 

 

위키피디아 지수분포:

https://en.wikipedia.org/wiki/Exponential_distribution

 

Exponential distribution - Wikipedia

From Wikipedia, the free encyclopedia Probability distribution In probability theory and statistics, the exponential distribution or negative exponential distribution is the probability distribution of the time between events in a Poisson point process, i.

en.wikipedia.org

지수분포 난수 생성 및 시각화 : 

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) 함수 사용

감마분포 확률밀도함수(PDF) 수식
감마분포 난수 생성

 

위키피디아 감마 분포 : 

https://en.wikipedia.org/wiki/Gamma_distribution

 

Gamma distribution - Wikipedia

From Wikipedia, the free encyclopedia Probability distribution Gamma Probability density function Cumulative distribution functionParameters Support x ∈ ( 0 , ∞ ) {\displaystyle x\in (0,\infty )} x ∈ ( 0 , ∞ ) {\displaystyle x\in (0,\infty )} PDF f

en.wikipedia.org

 

감마분포 난수 생성 및 시각화 : 

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()

 

반응형