-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel.py
More file actions
129 lines (117 loc) · 3.56 KB
/
model.py
File metadata and controls
129 lines (117 loc) · 3.56 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
import numpy as np
import variograms
from numba import jit
import numba
def opt( fct, x, y, c, parameterRange=None, meshSize=1000 ):
'''
Optimize parameters for a model of the semivariogram
'''
if parameterRange == None:
parameterRange = [ x[1], x[-1] ]
mse = np.zeros( meshSize )
a = np.linspace( parameterRange[0], parameterRange[1], meshSize )
for i in range( meshSize ):
mse[i] = np.mean( ( y - fct( x, a[i], c ) )**2.0 )
return a[ mse.argmin() ]
def typetest( h, a, lta, gta ):
'''
Input: (h) scalar or NumPy ndarray
(a) scalar representing the range parameter
(lta) function to perfrom for values less than (a)
(gta) function to perform for values greater than (a)
Output: scalar or array, depending on (h)
'''
# if (h) is a numpy ndarray, then..
try:
# apply lta() to elements less than a
lt = lta( h[ np.where( h <= a ) ] )
# apply gta() to elements greater than a
gt = gta( h[ np.where( h > a ) ] )
return np.hstack((lt,gt))
# otherwise, if (h) is a scalar..
except TypeError:
if h <= a:
return lta( h )
else:
return gta( h )
def nugget( h, a, c ):
'''
Nugget model of the semivariogram
'''
c = float(c)
lta = lambda x: 0+x*0
gta = lambda x: c+x*0
return typetest( h, 0, lta, gta )
def linear( h, a, c ):
'''
Linear model of the semivariogram
'''
a, c = float(a), float(c)
lta = lambda x: (c/a)*x
gta = lambda x: c+x*0
return typetest( h, a, lta, gta )
def spherical( h, a, c ):
'''
Spherical model of the semivariogram
'''
a, c = float(a), float(c)
lta = lambda x: c*( 1.5*(x/a) - 0.5*(x/a)**3.0 )
gta = lambda x: c+x*0
return typetest( h, a, lta, gta )
def exponential( h, a, c ):
'''
Exponential model of the semivariogram
'''
a, c = float( a ), float( c )
return c*( 1.0 - np.exp( -3.0*h/a ) )
def gaussian( h, a, c ):
'''
Gaussian model of the semivariogram
'''
a, c = float( a ), float( c )
return c*( 1.0 - np.exp( -3.0*h**2.0/a**2.0 ) )
def power( h, w, c ):
'''
Power model of the semivariogram
'''
return c*h**w
def semivariance( fct, param ):
'''
Input: (fct) function that takes data and parameters
(param) list or tuple of parameters
Output: (inner) function that only takes data as input
parameters are set internally
'''
def inner( h ):
return fct(h,*param)
return inner
def covariance( fct, param ):
'''
Input: (fct) function that takes data and parameters
(param) list or tuple of parameters
Output: (inner) function that only takes data as input
parameters are set internally
'''
def inner( h ):
return param[-1] - fct(h,*param)
return inner
def fitmodel( data, fct, lags, tol ):
'''
Input: (P) ndarray, data
(model) modeling function
- spherical
- exponential
- gaussian
(lags) lag distances
(tol) tolerance
Output: (covfct) function modeling the covariance
'''
# calculate the semivariogram
sv = variograms.semivariogram( data, lags, tol )
# calculate the sill
c = np.var( data[:,2] )
# calculate the optimal parameters
a = opt( fct, sv[0], sv[1], c )
# return a covariance function
covfct = covariance( fct, ( a, c ) )
return covfct