-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExponential.py
More file actions
59 lines (49 loc) · 1.39 KB
/
Copy pathExponential.py
File metadata and controls
59 lines (49 loc) · 1.39 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from RandomVariable import RandomVariable
import math
from Uniform import Uniform
class Exponential(RandomVariable):
def __init__(self,λ):
super().__init__()
self.min = 0
self.max = float('inf')
self.λ = λ
self.params.append(λ)
self.name = 'exponential'
self.eNegλ = math.exp(-λ)
self.setStrictLower(False)
self.discrete = False
def pdf(self,a):
if a <= 0:
return 0
return self.λ * math.pow(self.eNegλ,a)
def cdf(self, k):
if k > 0:
return 1 - math.pow(self.eNegλ,k)
return 0
def expectedValue(self):
return 1/self.λ
def _expectedValue(self,*params):
λ = params[0]
return 1/λ
def _valid(self,*params):
λ = params[0]
return λ > 0
def variance(self):
return 1/(self.λ ** 2)
def laplace(self):
return lambda s: self.λ/(s+self.λ)
"""
u ~ U(0,1), x = Fx^-1(u)
Fx(x) = 1 - exp(-λx)
u = 1-exp(-λx)
exp(-λx) = 1 - u
-λx = ln(1-u), but 1 - u ~ U(0,1) too
x = -1/λ ln(U(0,1)) [Ross pg. 68]
"""
def genVar(self):
return -(1/self.λ) * math.log(Uniform(0,1).genVar())
def _getMomentRelatedFunction(self):
return self.laplace()
if __name__ == '__main__':
X = Exponential(3)
print(X.moment(10),math.factorial(10)/(3**10))