GeoStat-Framework / GSTools

GSTools - A geostatistical toolbox: random fields, variogram estimation, covariance models, kriging and much more
https://geostat-framework.org
GNU Lesser General Public License v3.0
544 stars 71 forks source link

Defining a CustomVariogram istead of a CustomCovariance #303

Open rkenko opened 1 year ago

rkenko commented 1 year ago

Hi,

Is it a way to define a custom variogram model instead of a custom covariance model in order to perform simulation of the field?

I want to define directly the variogram since the field is not stationnary: there is no sill due to the linear component.

This is an example of what i try to do (the current version of the code is not working):

# importing modules
!pip install gstools
!export GSTOOLS_BUILD_PARALLEL=1
!pip install --no-binary=gstools gstools
!pip install gstools-core
from gstools import Variogram, SRF
import gstools as gs

# defining the variogram parameters
gamma0, gamma1, a1, gamma2, amplitud = 3.50822126e-05, 6.67841391e-04, 2.05546548e-01, 9.60346380e-06, 1.8182820314159756e-05

# defining the variogram model
class CustomVariogramModel():
    def __init__(self, gamma0, gamma1, a1, gamma2, amplitud, **kwargs):
        super().__init__(**kwargs)
        self.gamma0 = gamma0
        self.gamma1 = gamma1
        self.a1 = a1
        self.gamma2 = gamma2
        self.amplitud = amplitud
    def model(self, h):
        gaussian = self.gamma0 + self.gamma1 * (1 - np.exp(-(np.abs(h) ** 2) / (self.a1 ** 2)))
        linear = self.gamma2 * np.abs(h)
        periodic = self.amplitud * np.cos(2 * np.pi / (12 / 10) * h)
        return gaussian + linear + periodic

# attributing the variogram parameters 
my_variogram = CustomVariogramModel(gamma0, gamma1, a1, gamma2, amplitud)

# initializing the variogram model
my_variogram_model = Variogram(my_variogram.model)

# initializing the spatial random field 
srf = SRF(variogram_model=my_variogram_model, seed=0)

# generating the 2D field on a N x N grid
field = srf((100, 100))
MuellerSeb commented 1 year ago

Hey there,

at the moment this is not possible. All models need to be bounded and therefore have a covariance dependent formulation.

Here is an issue tracking the need of a class for unbounded models: https://github.com/GeoStat-Framework/GSTools/issues/192

The main obstacle is the random field generation, since the randomization method depends on the covariance function associated with the variogram.

For now, I would recommend using a truncated model, where there is an upper bound for the variance. Examples are the truncated linear model and the truncated power law models that are already implemented.

Cheers, Sebastian