pydfnWorks
python wrapper for dfnWorks
distributions.py
Go to the documentation of this file.
1 """
2  :filename: distributions.py
3  :synopsis: Analytic expressions for fracture radii distributions
4  :version: 1.0
5  :maintainer: Jeffrey Hyman
6  :moduleauthor: Jeffrey Hyman <jhyman@lanl.gov>
7 """
8 
9 from scipy import stats, special
10 import numpy as np
11 
12 
13 
14 def tpl_cdf(xmin, alpha, x):
15  """ Returns the analytical values of the power law CDF with exponent a
16 
17  Parameters
18  --------------
19  xmin : double
20  The lower bound of the truncated power law distribution.
21  alpha : double
22  The alpha parameter in the power law distribution.
23  x : numpy array
24  x values
25 
26  Returns
27  ----------
28  cdf : numpy array
29  Analytical values of the power law CDF
30  """
31  cdf = 1 - ((xmin / x)**alpha)
32  return cdf
33 
34 
35 def tpl_pdf(norm_const, xmin, alpha, x):
36  """ Returns the analytical power laws PDF values.
37 
38  Parameters
39  --------------
40  norm_const : double
41  The normalization constant for the PDF
42  xmin : double
43  The lower bound of the truncated power law distribution.
44  alpha : double
45  The alpha parameter (decay rate / exponent) in the power law distribution.
46  x : numpy array
47  x-values of the function
48  Returns
49  --------
50  pdf : numpy array
51  Analytical values of the power law PDF
52 
53  """
54  pdf = norm_const * ((alpha * (xmin**alpha)) / x**(alpha + 1))
55  return pdf
56 
57 
58 def tpl(alpha, xmin, xmax):
59  """ Returns the PDF and CDF of a truncated Power-law distribution with exponent alpha over the range [xmin,xmax].
60 
61  Parameters
62  -----------
63  alpha : double
64  The alpha parameter (decay rate / exponent) in the power law distribution. (alpha > 0)
65  xmin : double
66  Minimum x-value
67  xmax : double
68  Maximum x-value
69 
70  Returns
71  ---------
72  x : numpy array
73  x-values of the function
74  pdf : numpy array
75  pdf values of the truncated powerlaw
76  cdf : numpy array
77  cdf values of truncated powerlaw distribution
78 
79  Notes
80  -------
81  dfnWorks uses the convention of pdf(x) = C x^{-(alpha +1)}, rather than pdf(x) = C x^{-alpha} for a powerlaw.
82 
83  """
84 
85  x = np.linspace(xmin, xmax, 1000)
86  norm_const = 1.0 / (tpl_cdf(xmin, alpha, xmax) -
87  tpl_cdf(xmin, alpha, xmin))
88  pdf = tpl_pdf(norm_const, xmin, alpha, x)
89  cdf = tpl_cdf(xmin, alpha, x)
90  return x, pdf, cdf
91 
92 
93 
94 def exp_pdf(norm_const, eLambda, x):
95  """ Returns the analytical values of the PDF of the exponential distribution with exponent eLambda for values of x.
96 
97  Parameters
98  -------------
99  norm_const : double
100  The normalization constant for the PDF
101  eLambda : double
102  The exponent of the exponential distribution
103  x : numpy array
104  x-values of the function
105  Returns
106  --------
107  pdf : numpy array
108  Analytical values of the power law PDF
109 
110  Notes
111  ---------
112  None
113  """
114  pdf = norm_const * eLambda * np.e**(-eLambda * x)
115  return pdf
116 
117 
118 def exp_cdf(eLambda, x):
119  """ Returns the analytical values of the CDF of the exponential distribution with exponent eLambda for values of x.
120 
121  Parameters
122  -------------
123  eLambda : double
124  The exponent of the exponential distribution
125  x : numpy array
126  x-values of the function
127  Returns
128  --------
129  cdf : numpy array
130  Analytical values of the exponential CDF
131 
132  Notes
133  ---------
134  None
135  """
136  xmin = min(x)
137  cdf = 1 - (np.e**(-eLambda * (x - xmin)))
138  return cdf
139 
140 
141 def exponential(eLambda, xmin, xmax):
142  """ Returns the PDF and CDF of an exponential distribution with exponent eLambda over the range [xmin,xmax].
143 
144  Parameters
145  -----------
146  eLambda : double
147  The exponent of the exponential distribution
148  xmin : double
149  Minimum x-value
150  xmax : double
151  Maximum x-value
152 
153  Returns
154  ---------
155  x : numpy array
156  x-values of the function
157  pdf : numpy array
158  pdf values of the exponential distribution
159  cdf : numpy array
160  cdf values of exponential distribution
161 
162  Notes
163  -------
164  None
165 
166  """
167  x = np.linspace(xmin, xmax, 1000)
168  const = 1.0 / (exp_cdf(eLambda, xmax) - exp_cdf(eLambda, xmin))
169  pdf = exp_pdf(const, eLambda, x)
170  cdf = exp_cdf(eLambda, x)
171  return x, pdf, cdf
172 
173 
174 
175 def lognormal_cdf(x, mu, sigma):
176  """ Returns the analytical values of the CDF of the lognormal distribution with parameters mu and sigma
177 
178  Parameters
179  -------------
180  x : numpy array
181  x-values of the function
182  mu : double
183  Lognormal distribution parameter #1
184  sigma : double
185  Lognormal distribution parameter #1 (sigma > 0)
186  Returns
187  --------
188  cdf : numpy array
189  Analytical values of the CDF for the Log-Normal distribution
190 
191  Notes
192  ---------
193  None
194  """
195  cdf = 0.5 + (0.5 * special.erf((np.log(x) - mu) / (np.sqrt(2) * sigma)))
196  return cdf
197 
198 
199 def lognormal_pdf(x, mu, sigma):
200  """ Returns the analytical values of the CDF of the lognormal distribution with parameters mu and sigma
201 
202  Parameters
203  -------------
204  x : numpy array
205  x-values of the function
206  mu : double
207  Lognormal distribution parameter #1
208  sigma : double
209  Lognormal distribution parameter #1 (sigma > 0)
210  Returns
211  --------
212  pdf : numpy array
213  Analytical values of the PDF for the Log-Normal distribution
214 
215  Notes
216  ---------
217  None
218  """
219  constant = 1 / (x * sigma * np.sqrt(2 * np.pi))
220  exp_term = (-1.0 * (np.log(x) - mu)**2 / (2 * sigma**2))
221  pdf = constant * np.exp(exp_term)
222  return pdf
223 
224 
225 def lognormal(mu, sigma, xmin, xmax):
226  """ Returns the PDF and CDF of a LogNormal distribution with parameters mu and sigma over the range [xmin,xmax].
227 
228  Parameters
229  -----------
230  mu : double
231  Lognormal distribution parameter #1
232  sigma : double
233  Lognormal distribution parameter #1 (sigma > 0)
234  xmin : double
235  Minimum x-value
236  xmax : double
237  Maximum x-value
238 
239  Returns
240  ---------
241  x : numpy array
242  x-values of the function
243  pdf : numpy array
244  pdf values of the lognormal distribution
245  cdf : numpy array
246  cdf values of lognormal distribution
247 
248  Notes
249  -------
250  dfnGen uses the mean and standard deviation of the underlying normal distribution that creates the lognormal distribution.
251 
252  In order to produce a LogNormal distribution with a desired mean (m) and variance (s) one uses
253 
254  mu = ln [ m^2 / sqrt(m^2 + s^2)]
255 
256  and
257 
258  sigma = ln ( 1 + m^2 / s^2)
259 
260  For more details see https://en.wikipedia.org/wiki/Log-normal_distribution
261 """
262 
263  x = np.linspace(xmin, xmax, 1000)
264  const = 1.0 / (lognormal_cdf(xmax, mu, sigma) -
265  lognormal_cdf(xmin, mu, sigma))
266  pdf = lognormal_pdf(x, mu, sigma)
267  cdf = lognormal_cdf(x, mu, sigma)
268  return x, pdf, cdf
269 
270 
271 def create_ecdf(vals):
272  """ Returns the Empirical Cumulative Density function of provided values
273 
274  Parameters
275  ----------
276  vals : array
277  array of values to be binned
278 
279  Returns
280  -------
281  x : numpy array
282  sorted input values
283  cdf : numpy array
284  values of the cdf, normalized so cumulative sum = 1
285 
286  Notes
287  ------
288  None
289 
290  """
291 
292  vals.sort()
293  cdf = np.ones(len(vals))
294  cdf = np.cumsum(cdf) / sum(cdf)
295  return vals, cdf
def lognormal_cdf(x, mu, sigma)
Log-Normal Distribution Functions ########.
def exp_pdf(norm_const, eLambda, x)
Exponential Distribution Functions ########.
def tpl_cdf(xmin, alpha, x)
Truncated Power Law Distribution Functions ########.