-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnownModels.py
95 lines (67 loc) · 3.59 KB
/
KnownModels.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Author: Matteo L. BEDINI
# Date: April 2016
import math
###########################################################################
## GENERIC RECOGNIZED PDF ##
###########################################################################
class RecognizedPDF:
def __init__(self, location, scale):
assert (isinstance(location,int) or isinstance(location, float)) and location>0, "location must be a positive number"
assert (isinstance(scale,int) or isinstance(scale, float)) and scale>0, "scale must be a positive number"
self.location = location
self.scale = scale
def pdf(self, x):
pass
###########################################################################
## GAMMA PDF ##
###########################################################################
class GammaPDF(RecognizedPDF):
def pdf(self, x):
""" Gamma probability density function evaluated at x.
(see, e.g., https://en.wikipedia.org/wiki/Gamma_distribution)
Input arguments:
+ x: a positive number.
+ location: a positive number representing the location parameter of the gamma pdf.
+ scale: a positive number representing the scale parameter of the gamma pdf
Output:
+ The Gamma PDF evaluated at x.
"""
assert 0<=x, "x must be positive"
return pow(x,self.location-1)*math.exp(-x/self.scale)/(math.gamma(self.location)*pow(self.scale, self.location))
###########################################################################
## LOGNORMAL PDF ##
###########################################################################
class LogNormalPDF(RecognizedPDF):
x_min = 1.e-5
x_tol = 1.e-5
def pdf(self, x):
""" Lognormal probability density function evaluated at x.
(see, e.g., https://en.wikipedia.org/wiki/Log-normal_distribution)
Input arguments:
+ x: a positive number.
+ location: a positive number representing the location parameter of the lognormal pdf.
+ scale: a positive number representing the scale parameter of the lognormal pdf
Output:
+ The Lognormal PDF evaluated at x.
"""
assert 0<=x, "x must be positive"
y = x_min if abs(x-LogNormalPDF.x_min)<=LogNormalPDF.x_tol else x # if we are too close to 0 round to x_min
return math.exp(-pow(math.log(y)-self.location,2)/(2*self.scale**2)) / (y*self.scale*math.sqrt(2*math.pi))
###########################################################################
## UNIFORM PDF ##
###########################################################################
class UniformPDF(RecognizedPDF):
def pdf(self, x):
""" Uniform probability density function evaluated at x.
(see, e.g., https://en.wikipedia.org/wiki/Uniform_distribution_(continuous))
Input arguments:
+ x: a positive number.
+ location: a positive number representing the location parameter of the gamma pdf.
+ scale: a positive number representing the scale parameter of the gamma pdf
Output:
+ The Uniform PDF evaluated at x.
"""
assert 0<=x, "Invalid input value for x"
a = self.location-math.sqrt(3*self.scale)
b = self.location+math.sqrt(3*self.scale)
return 1 / (b -a) if a<= x <=b else 0.0