A Basic Model

In this example application it is shown how a simple time series model can be developed to simulate groundwater levels. The recharge (calculated as preciptation minus evaporation) is used as the explanatory time series.


In [1]:
# First perform the necessary imports
import pandas as pd
import matplotlib.pyplot as plt
import pastas as ps
%matplotlib inline

1. Importing the dependent time series data

In this codeblock a time series of groundwater levels is imported using the read_csv function of pandas. As pastas expects a pandas Series object, the data is squeezed. To check if you have the correct data type (a pandas Series object), you can use type(oseries) as shown below.

The following characteristics are important when importing and preparing the observed time series:

  • The observed time series are stored as a pandas Series object.
  • The time step can be irregular.

In [2]:
# Import groundwater time seriesm and squeeze to Series object
gwdata = pd.read_csv('../data/head_nb1.csv', parse_dates=['date'],
                     index_col='date', squeeze=True)
print('The data type of the oseries is: %s' % type(gwdata))

# Plot the observed groundwater levels
gwdata.plot(style='.', figsize=(10, 4))
plt.ylabel('Head [m]');
plt.xlabel('Time [years]');


The data type of the oseries is: <class 'pandas.core.series.Series'>

2. Import the independent time series

Two explanatory series are used: the precipitation and the potential evaporation. These need to be pandas Series objects, as for the observed heads.

Important characteristics of these time series are:

  • All series are stored as pandas Series objects.
  • The series may have irregular time intervals, but then it will be converted to regular time intervals when creating the time series model later on.
  • It is preferred to use the same length units as for the observed heads.

In [3]:
# Import observed precipitation series
precip = pd.read_csv('../data/rain_nb1.csv', parse_dates=['date'],
                     index_col='date', squeeze=True)
print('The data type of the precip series is: %s' % type(precip))

# Import observed evaporation series
evap = pd.read_csv('../data/evap_nb1.csv', parse_dates=['date'],
                   index_col='date', squeeze=True)
print('The data type of the evap series is: %s' % type(evap))

# Calculate the recharge to the groundwater
recharge = precip - evap
print('The data type of the recharge series is: %s' % type(recharge))

# Plot the time series of the precipitation and evaporation
plt.figure()
recharge.plot(label='Recharge', figsize=(10, 4))
plt.xlabel('Time [years]')
plt.ylabel('Recharge (m/year)');


The data type of the precip series is: <class 'pandas.core.series.Series'>
The data type of the evap series is: <class 'pandas.core.series.Series'>
The data type of the recharge series is: <class 'pandas.core.series.Series'>

3. Create the time series model

In this code block the actual time series model is created. First, an instance of the Model class is created (named ml here). Second, the different components of the time series model are created and added to the model. The imported time series are automatically checked for missing values and other inconsistencies. The keyword argument fillnan can be used to determine how missing values are handled. If any nan-values are found this will be reported by pastas.


In [4]:
# Create a model object by passing it the observed series
ml = ps.Model(gwdata, name="GWL")
# Add the recharge data as explanatory variable
ts1 = ps.StressModel(recharge, ps.Gamma, name='recharge', settings="evap")
ml.add_stressmodel(ts1)


INFO: Cannot determine frequency of series head
INFO: Inferred frequency from time series None: freq=D 

4. Solve the model

The next step is to compute the optimal model parameters. The default solver uses a non-linear least squares method for the optimization. The python package scipy is used (info on scipy's least_squares solver can be found here). Some standard optimization statistics are reported along with the optimized parameter values and correlations.


In [5]:
ml.solve()


Model Results GWL                   Fit Statistics
==================================================
nfev     12                     EVP          91.36
nobs     644                    R2            0.91
noise    True                   RMSE          0.13
tmin     1985-11-14 00:00:00    AIC           7.15
tmax     2015-06-28 00:00:00    BIC          29.49
freq     D                      ___               
warmup   3650 days 00:00:00     ___               
solver   LeastSquares           ___               

Parameters (5 were optimized)
==================================================
                optimal   stderr     initial  vary
recharge_A   755.633477   ±4.91%  215.674528  True
recharge_n     1.052268   ±1.49%    1.000000  True
recharge_a   136.703979   ±6.68%   10.000000  True
constant_d    27.555653   ±0.07%   27.900078  True
noise_alpha   60.931918  ±12.55%   15.000000  True

Parameter correlations |rho| > 0.5
==================================================
recharge_A recharge_a  0.85
           constant_d -0.76
recharge_n recharge_a -0.71
recharge_a constant_d -0.63

5. Plot the results

The solution can be plotted after a solution has been obtained.


In [6]:
ml.plot()


Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x1c1ba700b8>

6. Advanced plotting

There are many ways to further explore the time series model. pastas has some built-in functionalities that will provide the user with a quick overview of the model. The plots subpackage contains all the options. One of these is the method plots.results which provides a plot with more information.


In [7]:
ml.plots.results(figsize=(10, 6))


Out[7]:
[<matplotlib.axes._subplots.AxesSubplot at 0x1c1b93ada0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1c1b987198>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1c1c3e5eb8>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1c1c4bcef0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1c1cc0def0>]

7. Statistics

The stats subpackage includes a number of statistical functions that may applied to the model. One of them is the summary method, which gives a summary of the main statistics of the model.


In [8]:
ml.stats.summary()


Out[8]:
Value
Statistic
Akaike Information Criterion 7.150780
Average Deviation -0.000827
Bayesian Information Criterion 29.489274
Explained variance percentage 91.363980
Pearson R^2 0.913636
Root mean squared error 0.126314

8. Improvement: estimate evaporation factor

In the previous model, the recharge was estimated as precipitation minus potential evaporation. A better model is to estimate the actual evaporation as a factor (called the evaporation factor here) times the potential evaporation. First, new model is created (called ml2 here so that the original model ml does not get overwritten). Second, the StressModel2 object is created, which combines the precipitation and evaporation series and adds a parameter for the evaporation factor f. The StressModel2 object is added to the model, the model is solved, and the results and statistics are plotted to the screen. Note that the new model gives a better fit (lower root mean squared error and higher explained variance), and that the Akiake information criterion indicates that the addition of the additional parameter improved the model signficantly (the Akaike criterion for model ml2 is higher than for model ml).


In [9]:
# Create a model object by passing it the observed series
ml2 = ps.Model(gwdata)

# Add the recharge data as explanatory variable
ts1 = ps.StressModel2([precip, evap], ps.Gamma, name='rainevap', settings=("prec", "evap"))
ml2.add_stressmodel(ts1)

# Solve the model
ml2.solve()

# Plot the results
ml2.plot()

# Statistics
ml2.stats.summary()


INFO: Cannot determine frequency of series head
INFO: Inferred frequency from time series rain: freq=D 
INFO: Inferred frequency from time series evap: freq=D 
Model Results head                  Fit Statistics
==================================================
nfev     34                     EVP          92.89
nobs     644                    R2            0.93
noise    True                   RMSE          0.11
tmin     1985-11-14 00:00:00    AIC           9.23
tmax     2015-06-28 00:00:00    BIC          36.04
freq     D                      ___               
warmup   3650 days 00:00:00     ___               
solver   LeastSquares           ___               

Parameters (6 were optimized)
==================================================
                optimal   stderr     initial  vary
rainevap_A   691.547124   ±5.19%  215.674528  True
rainevap_n     1.020384   ±1.76%    1.000000  True
rainevap_a   149.624800   ±7.26%   10.000000  True
rainevap_f    -1.253367   ±4.85%   -1.000000  True
constant_d    27.863756   ±0.25%   27.900078  True
noise_alpha   52.571745  ±12.23%   15.000000  True

Parameter correlations |rho| > 0.5
==================================================
rainevap_A rainevap_a  0.60
rainevap_n rainevap_a -0.76
           rainevap_f  0.55
           constant_d -0.53
rainevap_f constant_d -0.99
Out[9]:
Value
Statistic
Akaike Information Criterion 9.230147
Average Deviation -0.001348
Bayesian Information Criterion 36.036340
Explained variance percentage 92.885295
Pearson R^2 0.928843
Root mean squared error 0.114656

Origin of the series

  • The rainfall data is taken from rainfall station Heibloem in The Netherlands.
  • The evaporation data is taken from weather station Maastricht in The Netherlands.
  • The head data is well B58C0698, which was obtained from Dino loket