-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariogram_fit.pyx
More file actions
311 lines (270 loc) · 7.57 KB
/
Copy pathvariogram_fit.pyx
File metadata and controls
311 lines (270 loc) · 7.57 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
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
# -*- coding: utf-8 -*-
"""
============================
Variogram libraries function
============================
Implemented by Juan Chacon @ UNESCO-IHE
Integrated Water Systems and Governance Department
Hydroinformatics Laboratory
This library contains some semivariogram functions. At this stage the use of \
Matern semivariogram function is still presenting instabilities, and will be \
adjusted in a posterior stage.
* Pre requisites
you will need the following libraries, not coming alongside with the\
Anaconda ditribution (recommended)
* Functions
* exponential_sv: Expnential semivariogram computation.
* gaussian_sv: Gaussian semivariogram computation.
* power_sv: Power semivariogram computation
* spherical_sv: Spherical semivariogram computation
* cubic_sv: Cubic semivariogram computation
* pentaspherical_sv: Pentaspherical semivariogram computation
* sinehole_sv: Sinehole semivariogram computation
* matern_sv: Matérn semivariogram computation
* fit_function: Function to calculate adjustment between observed and \
simulated semivariogram
* Use policy
* You should include the respective citation to the authors
* If you find this tool usefull, you will give the main author a beer next\
time you see him :)
* References
* http://people.ku.edu/~gbohling/cpe940/Variograms.pdf
* http://bit.ly/17z0aDw
"""
import numpy
import scipy.special
from numpy import linalg
## Performance Metrics
def _RMSE(x,y):
'''
Calculates Root Mean Squared Error between two data series. \n
Parameters
----------
**x and y -- Data series which are intercambiable \n
Returns
-------
**RMSE -- Value of the root mean squared error between data series
'''
Erro = numpy.square(numpy.subtract(x,y))
if Erro.any < 0:
return 9999
cdef float F = numpy.sqrt(1.*sum(Erro)/len(x))
return F
## Semivariogram lists
# h = Lag dostance
# S = Partial sill
# R = Range
# N = Nugget Effect
# a = Dimensionless exponent
# v = Matern parameter
def exponential_sv(h,x):
'''
Calculate the value of the theoretical semivariogram
Parameters
----------
**x -- vector of model parameters
**h -- distance (lag) for computation of semivariogram
Returns
-------
**SV -- value of semivariogram at lag x
'''
cdef float S = x[0]
cdef float R = x[1]
cdef float N = x[2]
cdef float SV
if h == 0:
return S
if h/R > 3:
return N+S
try:
SV = S - (-N + S * (1-numpy.exp(-1.*h/R)))
except OverflowError:
SV = N+S
return SV
def gaussian_sv(h,x):
'''
Calculate the value of the theoretical semivariogram
Parameters
----------
**x -- vector of model parameters
**h -- distance (lag) for computation of semivariogram
Returns
-------
**SV -- value of semivariogram at lag x
'''
cdef float S = x[0]
cdef float R = x[1]
cdef float N = x[2]
cdef float SV
if h == 0:
return S
if h/R > 1:
return N
try:
SV = S - (-N + S * (1-numpy.exp(-1.*numpy.square(h)/R)))
except OverflowError:
SV = N+S
return SV
def power_sv(h,x):
'''
Calculate the value of the theoretical semivariogram
Parameters
----------
**x -- vector of model parameters
**h -- distance (lag) for computation of semivariogram
Returns
-------
**SV -- value of semivariogram at lag x
'''
cdef float S = x[0]
cdef float N = x[2]
cdef float a = x[3]
cdef float SV
if h == 0:
return S
if a > 2:
a = 2
try:
SV = S - (-N + S*numpy.power(h,a))
except OverflowError:
SV = N+S
return SV
def spherical_sv(h,x):
'''
Calculate the value of the theoretical semivariogram
Parameters
----------
**x -- vector of model parameters
**h -- distance (lag) for computation of semivariogram
Returns
-------
**SV -- value of semivariogram at lag x
'''
cdef float S = x[0]
cdef float R = x[1]
cdef float N = x[2]
cdef float SV, hr
if h == 0:
return S+N
if h > R:
return 0
hr = 1.*h/R
SV = S - (-N + (S * ((3./2)*hr - ((1./2)*(hr**3.)))))
# if SV < 0:
# SV = 0
return SV
def cubic_sv(h,x):
'''
Calculate the value of the theoretical semivariogram
Parameters
----------
**x -- vector of model parameters
**h -- distance (lag) for computation of semivariogram
Returns
-------
**SV -- value of semivariogram at lag x
'''
cdef float S = x[0]
cdef float R = x[1]
cdef float N = x[2]
cdef float SV
if h == 0:
return S
if h > R:
return N+S
SV = S - (-N + S * (7.0*numpy.power((1.0*h/R),2.0) -
(35.0/4.0)*numpy.power(1.0*h/R,3.0) + (7.0/2.0)*numpy.power(h/R,5.0) -
(3.0/4.0)*numpy.power(h/R,7.0)))
return SV
def pentaspherical_sv(h,x):
'''
Calculate the value of the theoretical semivariogram
Parameters
----------
**x -- vector of model parameters
**h -- distance (lag) for computation of semivariogram
Returns
-------
**SV -- value of semivariogram at lag x
'''
cdef float S = x[0]
cdef float R = x[1]
cdef float N = x[2]
cdef float SV
if h == 0:
return S
if h > R:
return N+S
SV = S - (-N + S * ((15./8)*(1.*h/R)-(5./4)*numpy.power(1.*h/R,3)+
(3./8)*numpy.power(1.*h/R,5)))
return SV
def sinehole_sv(h,x):
'''
Calculate the value of the theoretical semivariogram
Parameters
----------
**x -- vector of model parameters
**h -- distance (lag) for computation of semivariogram
Returns
-------
**SV -- value of semivariogram at lag x
'''
cdef float S = x[0]
cdef float R = x[1]
cdef float N = x[2]
cdef float SV
if h == 0:
return S
if h > R:
h = R
SV = S - (-N + S * (1.-numpy.sin(numpy.pi*1.*h/R)/(numpy.pi*1.*h/R)))
return SV
def matern_sv(h,x):
'''
Calculate the value of the theoretical semivariogram
Parameters
----------
**x -- vector of model parameters
**h -- distance (lag) for computation of semivariogram
Returns
-------
**SV -- value of semivariogram at lag x
'''
cdef float S = x[0]
cdef float R = x[1]
cdef float N = x[2]
cdef float v = x[4]
cdef float SV
if h == 0:
return S
if v < 0:
return 9999
a = 1/(scipy.special.gamma(v)*(2**(v-1)))
b = numpy.sqrt(2*v)*h/R
c = scipy.special.kv(v,b)
SV = (S-N)*a*(b**v)*c
return SV
def fit_function(x, experimental_sv, j, candidate_sv):
'''
Calculate the value of the RMSE between theoretical and experimental \
semivariograms
Parameters
----------
**x -- Vector of model parameters
**SVExp -- Experimental semivariogram vector
**j -- Variogram model in the array
**VarFunArr -- Array of semivariogram models
Returns
-------
**RMSE -- Root mean squared error between observed and theoretical \
semivariogram
'''
temp = []
temp2 = []
cdef float F
cdef int fail = 0
for i in xrange (len(experimental_sv)):
temp.append(candidate_sv[j](experimental_sv[i][0], x))
temp2.append(experimental_sv[i][1])
F = _RMSE(temp, temp2)
return F, [], fail