-
Notifications
You must be signed in to change notification settings - Fork 55
/
models.py
423 lines (332 loc) · 13.5 KB
/
models.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import math
from functools import wraps
import numpy as np
from scipy import special
from numba import jit
def variogram(func):
@wraps(func)
def wrapper(*args, **kwargs):
if hasattr(args[0], '__iter__'):
new_args = args[1:]
mapping = map(lambda h: func(h, *new_args, **kwargs), args[0])
return np.fromiter(mapping, dtype=float)
else:
return func(*args, **kwargs)
return wrapper
@variogram
@jit(nopython=True)
def spherical(h, r, c0, b=0):
r"""Spherical Variogram function
Implementation of the spherical variogram function. Calculates the
dependent variable for a given lag (h). The nugget (b) defaults to be 0.
Parameters
----------
h : float
Specifies the lag of separating distances that the dependent variable
shall be calculated for. It has to be a positive real number.
r : float
The effective range. Note this is not the range parameter! However,
for the spherical variogram the range and effective range are the same.
c0 : float
The sill of the variogram, where it will flatten out. The function
will not return a value higher than C0 + b.
b : float
The nugget of the variogram. This is the value of independent
variable at the distance of zero. This is usually attributed to
non-spatial variance.
Returns
-------
gamma : numpy.float64
Unlike in most variogram function formulas, which define the function
for :math:`2*\gamma`, this function will return :math:`\gamma` only.
Notes
-----
The implementation follows [6]_:
.. math::
\gamma = b + C_0 * \left({1.5*\frac{h}{a} - 0.5*\frac{h}{a}^3}\right)
if :math:`h < r`, and
.. math::
\gamma = b + C_0
else. r is the effective range, which is in case of the spherical
variogram just a.
References
----------
.. [6] Burgess, T. M., & Webster, R. (1980). Optimal interpolation
and isarithmic mapping of soil properties. I.The semi-variogram and
punctual kriging. Journal of Soil and Science, 31(2), 315–331,
http://doi.org/10.1111/j.1365-2389.1980.tb02084.x
"""
# prepare parameters
a = r / 1.
if h <= r:
return b + c0 * ((1.5 * (h / a)) - (0.5 * ((h / a) ** 3.0)))
else:
return b + c0
@variogram
@jit(nopython=True)
def exponential(h, r, c0, b=0):
r"""Exponential Variogram function
Implementation of the exponential variogram function. Calculates the
dependent variable for a given lag (h). The nugget (b) defaults to be 0.
Parameters
----------
h : float
Specifies the lag of separating distances that the dependent variable
shall be calculated for. It has to be a positive real number.
r : float
The effective range. Note this is not the range parameter! For the
exponential variogram function the range parameter a is defined to be
:math:`a=\frac{r}{3}`. The effective range is the lag where 95% of the
sill are exceeded. This is needed as the sill is only approached
asymptotically by an exponential function.
c0 : float
The sill of the variogram, where it will flatten out. The function
will not return a value higher than C0 + b.
b : float
The nugget of the variogram. This is the value of independent
variable at the distance of zero. This is usually attributed to
non-spatial variance.
Returns
-------
gamma : numpy.float64
Unlike in most variogram function formulas, which define the function
for :math:`2*\gamma`, this function will return :math:`\gamma` only.
Notes
-----
The implementation following [7]_, [9]_ and [8]_ is as:
.. math::
\gamma = b + C_0 * \left({1 - e^{-\frac{h}{a}}}\right)
a is the range parameter, that can be calculated from the
effective range r as: :math:`a = \frac{r}{3}`.
References
----------
.. [7] Cressie, N. (1993): Statistics for spatial data.
Wiley Interscience.
.. [8] Chiles, J.P., Delfiner, P. (1999). Geostatistics. Modeling Spatial
Uncertainty. Wiley Interscience.
.. [9] Journel, A G, and Huijbregts, C J. Mining geostatistics.
United Kingdom: N. p., 1976.
"""
# prepare parameters
a = r / 3.
return b + c0 * (1. - math.exp(-(h / a)))
@variogram
@jit(nopython=True)
def gaussian(h, r, c0, b=0):
r""" Gaussian Variogram function
Implementation of the Gaussian variogram function. Calculates the
dependent variable for a given lag (h). The nugget (b) defaults to be 0.
Parameters
----------
h : float
Specifies the lag of separating distances that the dependent variable
shall be calculated for. It has to be a positive real number.
r : float
The effective range. Note this is not the range parameter! For the
exponential variogram function the range parameter a is defined to be
:math:`a=\frac{r}{3}`. The effetive range is the lag where 95% of the
sill are exceeded. This is needed as the sill is only approached
asymptotically by an exponential function.
c0 : float
The sill of the variogram, where it will flatten out. The function
will not return a value higher than C0 + b.
b : float
The nugget of the variogram. This is the value of independent
variable at the distance of zero. This is usually attributed to
non-spatial variance.
Returns
-------
gamma : numpy.float64
Unlike in most variogram function formulas, which define the function
for :math:`2*\gamma`, this function will return :math:`\gamma` only.
Notes
-----
This implementation follows [10]_ and [11]_:
.. math::
\gamma = b + c_0 * \left({1 - e^{-\frac{h^2}{a^2}}}\right)
a is the range parameter, that can be calculated from the
effective range r as:
.. math::
a = \frac{r}{2}
References
----------
.. [10] Chiles, J.P., Delfiner, P. (1999). Geostatistics. Modeling Spatial
Uncertainty. Wiley Interscience.
.. [11] Journel, A G, and Huijbregts, C J. Mining geostatistics.
United Kingdom: N. p., 1976.
"""
# prepare parameters
a = r / 2.
return b + c0 * (1. - math.exp(- (h ** 2 / a ** 2)))
@variogram
@jit(nopython=True)
def cubic(h, r, c0, b=0):
r"""Cubic Variogram function
Implementation of the Cubic variogram function. Calculates the
dependent variable for a given lag (h). The nugget (b) defaults to be 0.
Parameters
----------
h : float
Specifies the lag of separating distances that the dependent variable
shall be calculated for. It has to be a positive real number.
r : float
The effective range. Note this is not the range parameter! However,
for the cubic variogram the range and effective range are the same.
c0 : float
The sill of the variogram, where it will flatten out. The function
will not return a value higher than C0 + b.
b : float
The nugget of the variogram. This is the value of independent
variable at the distance of zero. This is usually attributed to
non-spatial variance.
Returns
-------
gamma : numpy.float64
Unlike in most variogram function formulas, which define the function
for :math:`2*\gamma`, this function will return :math:`\gamma` only.
Notes
-----
This implementation is taken from [12]_:
.. math::
\gamma = b + C_0 * \left[{7 * \left(\frac{h^2}{a^2}\right) -
\frac{35}{4} * \left(\frac{h^3}{a^3}\right) +
\frac{7}{2} * \left(\frac{h^5}{a^5}\right) -
\frac{3}{4} * \left(\frac{h^7}{a^7}\right)}\right]
a is the range parameter. For the cubic function, the effective range and
range parameter are the same.
References
----------
.. [12] Montero, J.-M., Mateu, J., & others. (2015). Spatial and spatio-temporal
geostatistical modeling and kriging (Vol. 998). John Wiley & Sons.
"""
# prepare parameters
a = r / 1.
if h < r:
return b + c0 * ((7 * (h ** 2 / a ** 2)) -
((35 / 4) * (h ** 3 / a ** 3)) +
((7 / 2) * (h ** 5 / a ** 5)) -
((3 / 4) * (h ** 7 / a ** 7)))
else:
return b + c0
@variogram
@jit(nopython=True)
def stable(h, r, c0, s, b=0):
r"""Stable Variogram function
Implementation of the stable variogram function. Calculates the
dependent variable for a given lag (h). The nugget (b) defaults to be 0.
.. versionchanged:: 0.4.4
Now returns the nugget at lag 0
Parameters
----------
h : float
Specifies the lag of separating distances that the dependent variable
shall be calculated for. It has to be a positive real number.
r : float
The effective range. Note this is not the range parameter! For the
stable variogram function the range parameter a is defined to be
:math:`a = \frac{r}{3^{\frac{1}{s}}}`. The effective range is the lag
where 95% of the sill are exceeded. This is needed as the sill is
only approached asymptotically by the e-function part of the stable
model.
c0 : float
The sill of the variogram, where it will flatten out. The function
will not return a value higher than C0 + b.
s : float
Shape parameter. For s <= 2 the model will be shaped more like a
exponential or spherical model, for s > 2 it will be shaped most like
a Gaussian function.
b : float
The nugget of the variogram. This is the value of independent
variable at the distance of zero. This is usually attributed to
non-spatial variance.
Returns
-------
gamma : numpy.float64
Unlike in most variogram function formulas, which define the function
for :math:`2*\gamma`, this function will return :math:`\gamma` only.
Notes
-----
The implementation is taken from [12]_:
.. math::
\gamma = b + C_0 * \left({1. - e^{- {\frac{h}{a}}^s}}\right)
a is the range parameter and is calculated from the effective range r as:
.. math::
a = \frac{r}{3^{\frac{1}{s}}}
References
----------
.. [12] Montero, J.-M., Mateu, J., & others. (2015). Spatial and spatio-temporal
geostatistical modeling and kriging (Vol. 998). John Wiley & Sons.
"""
# if s gts too small, we run into a zeroDivision error at lag 0
if h == 0:
return b
# prepare parameters
a = r / np.power(3, 1 / s)
# if s > 2:
# s = 2
return b + c0 * (1. - math.exp(- math.pow(h / a, s)))
@variogram
@jit(forceobj=True)
def matern(h, r, c0, s, b=0):
r"""Matérn Variogram function
Implementation of the Matérn variogram function. Calculates the
dependent variable for a given lag (h). The nugget (b) defaults to be 0.
.. versionchanged:: 0.4.4
now returns the nugget instead of NaN for lag 0.
Parameters
----------
h : float
Specifies the lag of separating distances that the dependent variable
shall be calculated for. It has to be a positive real number.
r : float
The effective range. Note this is not the range parameter! For the
Matérn variogram function the range parameter a is defined to be
:math:`a = \frac{r}{2}` and :math:`a = \frac{r}{3}` if s is smaller
than 0.5 or larger than 10. The effective range is the lag
where 95% of the sill are exceeded. This is needed as the sill is
only approached asymptotically by Matérn model.
c0 : float
The sill of the variogram, where it will flatten out. The function
will not return a value higher than C0 + b.
s : float
Smoothness parameter. The smoothness parameter can shape a smooth or
rough variogram function. A value of 0.5 will yield the exponential
function, while a smoothness of +inf is exactly the Gaussian model.
Typically a value of 10 is close enough to Gaussian shape to simulate
its behaviour. Low values are considered to be 'smooth', while larger
values are considered to describe a 'rough' random field.
b : float
The nugget of the variogram. This is the value of independent
variable at the distance of zero. This is usually attributed to
non-spatial variance.
Returns
-------
gamma : numpy.float64
Unlike in most variogram function formulas, which define the function
for :math:`2*\gamma`, this function will return :math:`\gamma` only.
Notes
-----
The implementation is taken from [13]_:
.. math::
\gamma (h) = b + C_0 \left( 1 - \frac{1}{2^{\upsilon - 1}
\Gamma(\upsilon)}\left(\frac{h}{a}\right)^\upsilon K_\upsilon
\left(\frac{h}{a}\right)\right)
a is the range parameter and is calculated from the effective range r as:
.. math::
a = \frac{r}{2}
References
----------
.. [13] Zimmermann, B., Zehe, E., Hartmann, N. K., & Elsenbeer, H. (2008).
Analyzing spatial data: An assessment of assumptions, new methods, and
uncertainty using soil hydraulic data. Water Resources Research,
44(10), 1–18. https://doi.org/10.1029/2007WR006604
"""
if h == 0:
return b
# prepare parameters
a = r / 2.
# calculate
return b + c0 * (1. - (2 / special.gamma(s)) *
np.power((h * np.sqrt(s)) / a, s) *
special.kv(s, 2 * ((h * np.sqrt(s)) / a))
)