-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrvg.py
More file actions
35 lines (29 loc) · 1.17 KB
/
rvg.py
File metadata and controls
35 lines (29 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Random Variate Generator for exponential distribution."""
import math
import random
import matplotlib.pyplot as plt
def rvg_exp(lam : float = 1) -> float:
"""Return a random variate from an exponential distribution with
parameter lam.
:param lam: Lambda parameter of the exponential distribution.
:return: A random variate from the exponential distribution.
"""
return -math.log(1 - random.random())/lam
def plot_rvg_exp(n : int, lam : float = 1, bins : int = 100) -> None:
"""Plot a histogram of n random variates from an exponential
distribution with parameter lam.
:param n: Number of random variates to generate.
:param lam: Lambda parameter of the exponential distribution.
:param bins: Number of bins in the histogram.
"""
data = [rvg_exp(lam) for i in range(n)]
plt.hist(data, bins=bins)
plt.ylabel('Sample Count')
plt.xlabel('Random Variate')
plt.text(0.95, 0.95, f'λ = {lam}\n samples = {n}', horizontalalignment='right', verticalalignment='top', transform=plt.gca().transAxes)
plt.show()
if __name__ == "__main__":
plot_rvg_exp(10)
plot_rvg_exp(100)
plot_rvg_exp(1000)
plot_rvg_exp(100000)