# Examples¶

To start things off, import a few basic tools, including bayspar:

In [1]: import numpy as np

In [2]: import matplotlib.pyplot as plt

In [3]: import bayspar as bsr


There are a few options when it comes to prediction with bayspar. Below, we use example data - included with the package - to walk through each type of prediction.

## Standard prediction¶

We can access the example data with get_example_data() and use the returned stream with pandas.read_csv() or numpy.genfromtxt():

In [4]: example_file = bsr.get_example_data('castaneda2010.csv')

In [5]: d = np.genfromtxt(example_file, delimiter=',', names=True)


This dataset (from Castañeda et al. 2010) has two columns giving sediment age (calendar years BP) and TEX86.

In [6]: d['age'][:5]
Out[6]: array([142., 284., 375., 466., 558.])

In [7]: d['tex86'][:5]
Out[7]: array([0.662, 0.662, 0.652, 0.66 , 0.664])


We can make a “standard” prediction of sea-surface temperature (SST) with predict_seatemp():

In [8]: prediction = bsr.predict_seatemp(d['tex86'], lon=34.0733, lat=31.6517,
...:                                  prior_std=6, temptype='sst')
...:


The TEX86 data and site position are passed to the prediction function. We also give standard deviation for the prior SST distribution and specify that SST are the target variable. A Prediction instance is returned, which we can poked and prod with direct queries or using a few built-in functions:

In [9]: bsr.predictplot(prediction)
Out[9]: <matplotlib.axes._subplots.AxesSubplot at 0x7fd719569518>


You might have noticed that predictplot() returns an matplotlib.Axes instance. This means we can catch it and then modify the plot as needed:

In [10]: ax = bsr.predictplot(prediction, x=d['age'], xlabel='Age',
....:                      ylabel='SST (°C)')
....:

In [11]: ax.grid()

In [12]: ax.legend()
Out[12]: <matplotlib.legend.Legend at 0x7fd7195b8be0>


In much the same way, we can view the distribution of the prediction prior and posterior with densityplot():

In [13]: ax = bsr.densityplot(prediction,
....:                      x=np.arange(1, 40, 0.1),
....:                      xlabel='SST (°C)')
....:

In [14]: ax.grid(axis='x')

In [15]: ax.legend()
Out[15]: <matplotlib.legend.Legend at 0x7fd7194cbdd8>


We can access the full prediction ensemble with:

In [16]: prediction.ensemble
Out[16]:
array([[26.14268637, 24.05085581, 22.0933663 , ..., 25.87273837,
19.96231357, 27.17017743],
[27.72214668, 23.72567287, 29.2786996 , ..., 22.81598118,
21.95563902, 23.22496556],
[25.14554614, 22.89305935, 24.39608412, ..., 24.36928151,
23.57776533, 24.51826952],
...,
[17.83780007, 15.03072967, 16.62197699, ..., 18.75554239,
15.3823915 , 13.29656579],
[14.15815527, 17.25041827, 20.89633592, ..., 16.8674538 ,
20.41007379, 21.52076741],
[15.46175838, 16.36634982, 17.25499769, ..., 14.60678779,
15.22239733, 12.91596066]])


A useful summary is available using:

In [17]: prediction.percentile()[:10]  # Showing only the first few values.
Out[17]:
array([[20.10738678, 24.43225064, 29.57087897],
[20.12145933, 24.49372798, 29.43615394],
[19.59792307, 23.90746365, 29.03168556],
[20.06126209, 24.42656315, 29.40874874],
[20.2022063 , 24.52854808, 29.68123154],
[20.1365698 , 24.40575849, 29.382667  ],
[19.39770939, 23.77733892, 28.56942883],
[20.44493634, 24.7930831 , 29.93487748],
[19.40955099, 23.61412975, 28.51957921],
[19.83603002, 24.1845369 , 29.24279939]])


See the percentile() method for more options.

## Analog prediction¶

In [18]: example_file = bsr.get_example_data('wilsonlake.csv')

In [19]: d = np.genfromtxt(example_file, delimiter=',', names=True)


This dataset is a TEX86 record from record from Wilson Lake, New Jersey (Zachos et al. 2006). The file has two columns giving depth (m) and TEX86:

In [20]: d['depth'][:5]
Out[20]: array([91.51, 92.95, 93.83, 95.96, 96.94])

In [21]: d['tex86'][:5]
Out[21]: array([0.79, 0.74, 0.77, 0.7 , 0.87])


We can run the analog prediction of SST with predict_seatemp_analog():

In [22]: search_tolerance = np.std(d['tex86'], ddof=1) * 2

In [23]: prediction = bsr.predict_seatemp_analog(d['tex86'], temptype='sst',
....:                                         prior_mean=30, prior_std=20,
....:                                         search_tol=search_tolerance,
....:                                         nens=500)
....:


A Prediction instance is returned from the function. The arguments that we pass to the function indicate that the calibration is for SST. We also indicate a prior mean and standard deviation for the sea temperature inference. Note that we pass a search tolerance used to find analogous conditions across the global TEX86 dataset. The nens argument is to reduce the size of the model parameter ensemble used for the inference - we’re using this option because otherwise this page of documentation would take too long to compile. This argument isn’t required if you’re following along with these examples on your own machine. By default, a progress bar is printed to the screen. This is an optional feature that can be switched off, for example, if you are processing many cores in batch.

Note

Analog predictions can be slow to calculate if many analog are selected. Be careful not to select too large a value for search_tol.

Specifically, for analog predictions, we can map the information used for the inference with analogmap():

In [24]: paleo_location = (38.2, -56.7)

In [25]: ax = bsr.analogmap(prediction, latlon=paleo_location)

In [26]: ax.legend()
Out[26]: <matplotlib.legend.Legend at 0x7fd71943b198>


This maps the location of all available TEX86 observations. We passed an optional paleo-location of the site to the mapping function. The map also indicates the grids that were identified as analogs for the prediction.

We can also examine our prediction as though it were a standard prediction. For example, we can plot a time series of the prediction:

In [27]: bsr.predictplot(prediction, x=d['depth'], xlabel='Depth (m)',
....:                 ylabel='SST (°C)')
....:
Out[27]: <matplotlib.axes._subplots.AxesSubplot at 0x7fd7196a3cc0>


## Forward prediction¶

For this example, we make inferences about TEX86 from SST data using a forward-model prediction. We start by creating a SST series spanning from 0 - 40 °C:

In [28]: sst = np.arange(0, 41)


And now plug the SST data into predict_tex() along with additional information. In this case we’re using the same site location as in Standard prediction:

In [29]: prediction = bsr.predict_tex(sst, lon=34.0733, lat=31.6517, temptype='sst')


As might be expected, we can use the output of the forward prediction to parse and plot:

In [30]: ax = bsr.predictplot(prediction, x=sst,
....:                      xlabel='SST (°C)',
....:                      ylabel=r'TEX$_{86}$')
....:

In [31]: ax.grid()

In [32]: ax.legend()
Out[32]: <matplotlib.legend.Legend at 0x7fd718a8a278>