Skip to content

Commit 09424cc

Browse files
committed
Updated MLE.md
1 parent 0cc7aae commit 09424cc

File tree

8 files changed

+343
-11
lines changed

8 files changed

+343
-11
lines changed

code/gmm/distributions.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
'''
2+
------------------------------------------------------------------------
3+
This module contains the functions for probability density functions of
4+
continuous PDF's.
5+
6+
This Python module defines the following function(s):
7+
GA_pdf()
8+
GG_pdf()
9+
GB2_pdf()
10+
------------------------------------------------------------------------
11+
'''
12+
# Import packages
13+
import numpy as np
14+
import scipy.special as spc
15+
16+
17+
'''
18+
------------------------------------------------------------------------
19+
Functions
20+
------------------------------------------------------------------------
21+
'''
22+
23+
24+
def LN_pdf(xvals, mu, sigma):
25+
'''
26+
--------------------------------------------------------------------
27+
This function gives the PDF of the lognormal distribution for xvals
28+
given mu and sigma
29+
30+
(LN): f(x; mu, sigma) = (1 / (x * sigma * sqrt(2 * pi))) *
31+
exp((-1 / 2) * (((log(x) - mu) / sigma) ** 2))
32+
x in [0, infty), mu in (-infty, infty), sigma > 0
33+
--------------------------------------------------------------------
34+
INPUTS:
35+
xvals = (N,) vector, data
36+
mu = scalar, mean of the ln(x)
37+
sigma = scalar > 0, standard deviation of ln(x)
38+
39+
OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION: None
40+
41+
OBJECTS CREATED WITHIN FUNCTION:
42+
pdf_vals = (N,) vector, probability of each observation given
43+
the parameter values
44+
45+
FILES CREATED BY THIS FUNCTION: None
46+
47+
RETURNS: pdf_vals
48+
--------------------------------------------------------------------
49+
'''
50+
pdf_vals = np.float64(((1 / (np.sqrt(2 * np.pi) * sigma * xvals)) *
51+
np.exp((-1.0 / 2.0) *
52+
(((np.log(xvals) - mu) / sigma) ** 2))))
53+
54+
return pdf_vals
55+
56+
57+
def GA_pdf(xvals, alpha, beta):
58+
'''
59+
--------------------------------------------------------------------
60+
Returns the PDF values from the two-parameter gamma (GA)
61+
distribution. See McDonald and Xu (1995).
62+
63+
(GA): f(x; alpha, beta) = (1 / ((beta ** alpha) *
64+
spc.gamma(alpha))) * (x ** (alpha - 1)) * (e ** (-x / beta))
65+
x in [0, infty), alpha, beta > 0
66+
--------------------------------------------------------------------
67+
INPUTS:
68+
xvals = (N,) vector, values in the support of gamma distribution
69+
alpha = scalar > 0, gamma distribution parameter
70+
beta = scalar > 0, gamma distribution parameter
71+
72+
OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:
73+
spc.gamma()
74+
75+
OBJECTS CREATED WITHIN FUNCTION:
76+
pdf_vals = (N,) vector, pdf values from gamma distribution
77+
corresponding to xvals given parameters alpha and beta
78+
79+
FILES CREATED BY THIS FUNCTION: None
80+
81+
RETURNS: pdf_vals
82+
--------------------------------------------------------------------
83+
'''
84+
pdf_vals = \
85+
np.float64((1 / ((beta ** alpha) * spc.gamma(alpha))) *
86+
(xvals ** (alpha - 1)) * np.exp(-xvals / beta))
87+
88+
return pdf_vals
89+
90+
91+
def GG_pdf(xvals, alpha, beta, mm):
92+
'''
93+
--------------------------------------------------------------------
94+
Returns the PDF values from the three-parameter generalized gamma
95+
(GG) distribution. See McDonald and Xu (1995).
96+
97+
(GG): f(x; alpha, beta, m) =
98+
(m / ((beta ** alpha) * spc.gamma(alpha/m))) *
99+
(x ** (alpha - 1)) * (e ** -((x / beta) ** m))
100+
x in [0, infty), alpha, beta, m > 0
101+
--------------------------------------------------------------------
102+
INPUTS:
103+
xvals = (N,) vector, values in the support of generalized gamma (GG)
104+
distribution
105+
alpha = scalar > 0, generalized gamma (GG) distribution parameter
106+
beta = scalar > 0, generalized gamma (GG) distribution parameter
107+
mm = scalar > 0, generalized gamma (GG) distribution parameter
108+
109+
OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:
110+
spc.gamma()
111+
112+
OBJECTS CREATED WITHIN FUNCTION:
113+
pdf_vals = (N,) vector, pdf values from generalized gamma
114+
distribution corresponding to xvals given parameters
115+
alpha, beta, and mm
116+
117+
FILES CREATED BY THIS FUNCTION: None
118+
119+
RETURNS: pdf_vals
120+
--------------------------------------------------------------------
121+
'''
122+
pdf_vals = \
123+
np.float64((mm / ((beta ** alpha) * spc.gamma(alpha / mm))) *
124+
(xvals ** (alpha - 1)) *
125+
np.exp(-((xvals / beta) ** mm)))
126+
127+
return pdf_vals
128+
129+
130+
def GB2_pdf(xvals, aa, bb, pp, qq):
131+
'''
132+
--------------------------------------------------------------------
133+
Returns the PDF values from the four-parameter generalized beta 2
134+
(GB2) distribution. See McDonald and Xu (1995).
135+
136+
(GB2): f(x; a, b, p, q) = (a * (x ** ((a*p) - 1))) /
137+
((b ** (a * p)) * spc.beta(p, q) *
138+
((1 + ((x / b) ** a)) ** (p + q)))
139+
x in [0, infty), alpha, beta, m > 0
140+
--------------------------------------------------------------------
141+
INPUTS:
142+
xvals = (N,) vector, values in the support of generalized beta 2
143+
(GB2) distribution
144+
aa = scalar > 0, generalized beta 2 (GB2) distribution parameter
145+
bb = scalar > 0, generalized beta 2 (GB2) distribution parameter
146+
pp = scalar > 0, generalized beta 2 (GB2) distribution parameter
147+
qq = scalar > 0, generalized beta 2 (GB2) distribution parameter
148+
149+
OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:
150+
spc.beta()
151+
152+
OBJECTS CREATED WITHIN FUNCTION:
153+
pdf_vals = (N,) vector, pdf values from generalized beta 2 (GB2)
154+
distribution corresponding to xvals given parameters aa,
155+
bb, pp, and qq
156+
157+
FILES CREATED BY THIS FUNCTION: None
158+
159+
RETURNS: pdf_vals
160+
--------------------------------------------------------------------
161+
'''
162+
pdf_vals = \
163+
np.float64((aa * (xvals ** (aa * pp - 1))) / ((bb ** (aa * pp)) *
164+
spc.beta(pp, qq) *
165+
((1 + ((xvals / bb) ** aa)) ** (pp + qq))))
166+
167+
return pdf_vals

code/mle/distributions.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
'''
2+
------------------------------------------------------------------------
3+
This module contains the functions for probability density functions of
4+
continuous PDF's.
5+
6+
This Python module defines the following function(s):
7+
GA_pdf()
8+
GG_pdf()
9+
GB2_pdf()
10+
------------------------------------------------------------------------
11+
'''
12+
# Import packages
13+
import numpy as np
14+
import scipy.special as spc
15+
16+
17+
'''
18+
------------------------------------------------------------------------
19+
Functions
20+
------------------------------------------------------------------------
21+
'''
22+
23+
24+
def LN_pdf(xvals, mu, sigma):
25+
'''
26+
--------------------------------------------------------------------
27+
This function gives the PDF of the lognormal distribution for xvals
28+
given mu and sigma
29+
30+
(LN): f(x; mu, sigma) = (1 / (x * sigma * sqrt(2 * pi))) *
31+
exp((-1 / 2) * (((log(x) - mu) / sigma) ** 2))
32+
x in [0, infty), mu in (-infty, infty), sigma > 0
33+
--------------------------------------------------------------------
34+
INPUTS:
35+
xvals = (N,) vector, data
36+
mu = scalar, mean of the ln(x)
37+
sigma = scalar > 0, standard deviation of ln(x)
38+
39+
OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION: None
40+
41+
OBJECTS CREATED WITHIN FUNCTION:
42+
pdf_vals = (N,) vector, probability of each observation given
43+
the parameter values
44+
45+
FILES CREATED BY THIS FUNCTION: None
46+
47+
RETURNS: pdf_vals
48+
--------------------------------------------------------------------
49+
'''
50+
pdf_vals = np.float64(((1 / (np.sqrt(2 * np.pi) * sigma * xvals)) *
51+
np.exp((-1.0 / 2.0) *
52+
(((np.log(xvals) - mu) / sigma) ** 2))))
53+
54+
return pdf_vals
55+
56+
57+
def GA_pdf(xvals, alpha, beta):
58+
'''
59+
--------------------------------------------------------------------
60+
Returns the PDF values from the two-parameter gamma (GA)
61+
distribution. See McDonald and Xu (1995).
62+
63+
(GA): f(x; alpha, beta) = (1 / ((beta ** alpha) *
64+
spc.gamma(alpha))) * (x ** (alpha - 1)) * (e ** (-x / beta))
65+
x in [0, infty), alpha, beta > 0
66+
--------------------------------------------------------------------
67+
INPUTS:
68+
xvals = (N,) vector, values in the support of gamma distribution
69+
alpha = scalar > 0, gamma distribution parameter
70+
beta = scalar > 0, gamma distribution parameter
71+
72+
OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:
73+
spc.gamma()
74+
75+
OBJECTS CREATED WITHIN FUNCTION:
76+
pdf_vals = (N,) vector, pdf values from gamma distribution
77+
corresponding to xvals given parameters alpha and beta
78+
79+
FILES CREATED BY THIS FUNCTION: None
80+
81+
RETURNS: pdf_vals
82+
--------------------------------------------------------------------
83+
'''
84+
pdf_vals = \
85+
np.float64((1 / ((beta ** alpha) * spc.gamma(alpha))) *
86+
(xvals ** (alpha - 1)) * np.exp(-xvals / beta))
87+
88+
return pdf_vals
89+
90+
91+
def GG_pdf(xvals, alpha, beta, mm):
92+
'''
93+
--------------------------------------------------------------------
94+
Returns the PDF values from the three-parameter generalized gamma
95+
(GG) distribution. See McDonald and Xu (1995).
96+
97+
(GG): f(x; alpha, beta, m) =
98+
(m / ((beta ** alpha) * spc.gamma(alpha/m))) *
99+
(x ** (alpha - 1)) * (e ** -((x / beta) ** m))
100+
x in [0, infty), alpha, beta, m > 0
101+
--------------------------------------------------------------------
102+
INPUTS:
103+
xvals = (N,) vector, values in the support of generalized gamma (GG)
104+
distribution
105+
alpha = scalar > 0, generalized gamma (GG) distribution parameter
106+
beta = scalar > 0, generalized gamma (GG) distribution parameter
107+
mm = scalar > 0, generalized gamma (GG) distribution parameter
108+
109+
OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:
110+
spc.gamma()
111+
112+
OBJECTS CREATED WITHIN FUNCTION:
113+
pdf_vals = (N,) vector, pdf values from generalized gamma
114+
distribution corresponding to xvals given parameters
115+
alpha, beta, and mm
116+
117+
FILES CREATED BY THIS FUNCTION: None
118+
119+
RETURNS: pdf_vals
120+
--------------------------------------------------------------------
121+
'''
122+
pdf_vals = \
123+
np.float64((mm / ((beta ** alpha) * spc.gamma(alpha / mm))) *
124+
(xvals ** (alpha - 1)) *
125+
np.exp(-((xvals / beta) ** mm)))
126+
127+
return pdf_vals
128+
129+
130+
def GB2_pdf(xvals, aa, bb, pp, qq):
131+
'''
132+
--------------------------------------------------------------------
133+
Returns the PDF values from the four-parameter generalized beta 2
134+
(GB2) distribution. See McDonald and Xu (1995).
135+
136+
(GB2): f(x; a, b, p, q) = (a * (x ** ((a*p) - 1))) /
137+
((b ** (a * p)) * spc.beta(p, q) *
138+
((1 + ((x / b) ** a)) ** (p + q)))
139+
x in [0, infty), alpha, beta, m > 0
140+
--------------------------------------------------------------------
141+
INPUTS:
142+
xvals = (N,) vector, values in the support of generalized beta 2
143+
(GB2) distribution
144+
aa = scalar > 0, generalized beta 2 (GB2) distribution parameter
145+
bb = scalar > 0, generalized beta 2 (GB2) distribution parameter
146+
pp = scalar > 0, generalized beta 2 (GB2) distribution parameter
147+
qq = scalar > 0, generalized beta 2 (GB2) distribution parameter
148+
149+
OTHER FUNCTIONS AND FILES CALLED BY THIS FUNCTION:
150+
spc.beta()
151+
152+
OBJECTS CREATED WITHIN FUNCTION:
153+
pdf_vals = (N,) vector, pdf values from generalized beta 2 (GB2)
154+
distribution corresponding to xvals given parameters aa,
155+
bb, pp, and qq
156+
157+
FILES CREATED BY THIS FUNCTION: None
158+
159+
RETURNS: pdf_vals
160+
--------------------------------------------------------------------
161+
'''
162+
pdf_vals = \
163+
np.float64((aa * (xvals ** (aa * pp - 1))) / ((bb ** (aa * pp)) *
164+
spc.beta(pp, qq) *
165+
((1 + ((xvals / bb) ** aa)) ** (pp + qq))))
166+
167+
return pdf_vals

0 commit comments

Comments
 (0)