This tutorial shows some basic Sherpa features. More information on Sherpa can be found in the Sherpa documentation.
Workflow:
First of all, let's activate the inline matplotlib mode. Sherpa seamlessly uses matplotlib to provide immediate visual feedback to the user. Support for matplotlib in Sherpa requires the matplotlib package to be installed.
%matplotlib inline
Now, let's create a simple synthetic dataset, using numpy: a parabola between x=-5 and x=5, with some randomly generated noise (the form for y
is chosen to match the model selected in step 6 below to fit the data, and the random seed used by NumPy is set to make this notebook repeatable):
import numpy as np
np.random.seed(824842)
x = np.arange(-5, 5.1)
c0_true = 23.2
c1_true = 0
c2_true = 1
y = c2_true * x*x + c1_true * x + c0_true + np.random.normal(size=x.size)
e = np.ones(x.size)
Let's import Sherpa:
from sherpa.astro import ui
Depending on how you installed Sherpa, certain special features may be enabled or disabled. Sherpa prints some warning messages when it cannot find some of its modules, as shown above. These warnings are benign. You can refer to the Sherpa documentation to find out what additional features you can enable in Sherpa and how to enable them.
Let's load and plot the data we just created. Notice we are assigning the ID mydata
to the dataset we are loading. We will use this ID to refer to the same dataset in the rest of the tutorial. Sherpa can deal with multiple datasets, fit them simultaneously with the same model, and even link parameters between models. Sherpa can read ASCII table and FITS files (provided the astropy
package is installed).
ui.load_arrays("mydata", x, y, e)
ui.plot_data("mydata")
The data can be retrieved with the get_data
routine:
d = ui.get_data("mydata")
print(d)
The object - in this case a Data1D object - has support for Jupyter notebooks and will display a summary of the data (in this case a plot, but other objects will display differently, as we will see below):
d
We can set the model we want to fit to the data using the set_model
call. There are different ways to instantiate the models: in this case, we just use the string polynom1d
to refer to a 1D polynomial. The name of the model will be poly
, and will be accessible as a Python variable. One can use more object oriented patterns to access and instantiate built-in models. Also, new models can be added by the user as Python functions or from tabular data.
ui.set_model("mydata", "polynom1d.poly")
Several Sherpa commands can be used to inspect the model. In this case we just use a simple print
to get a summary of the model and its components.
print(poly)
We can also display it directly in a Jupyter notebook:
poly
By default, only the first component (the intercept) is thawed, i.e. is free to vary in the fit. This corresponds to a constant function. In order to fit a parabola, we need to thaw the coefficients of the first two orders in the polynomial, as shown below.
ui.thaw(poly.c1, poly.c2)
We are going to fit the dataset using the default settings. However, Sherpa has a number of optimization algorithms, each configurable by the user, and a number of statistics that can be used to take into account the error and other characteristics of data being fitted.
ui.fit("mydata")
Notice that Sherpa used a Levenberg-Marquadt minimization strategy (levmar
), and the $\chi^2$ error function (chi2
). These options can be changed with the set_method
and set_stat
functions. Note that when using the levmar
optimiser the fit results include approximate error estimates, but you should use the conf
routine (shown below) to get accurate error estimates.
The best fit values of the parameters are close to the ones defined when we generated the dataset:
Parameter | true value | best-fit value |
---|---|---|
$c_0$ | 23.2 | 23.1367 |
$c_1$ | 0 | 0.0538 |
$c_2$ | 1 | 1.0460 |
The get_fit_results
function will return the information on the last fit:
fitres = ui.get_fit_results()
print(fitres)
Perhaps unsurprisingly by now, it too can be displayed automatically in a Jupyter notebook:
fitres
The model object has also been updated by the fit:
poly
In order to get immediate feedback of the fit results, we can plot the fit and the residuals. Again, Sherpa facilitates the creation of the plots, but users can harvest the power of matplotlib directly if they want to.
ui.plot_fit_resid("mydata")
We can now compute the confidence intervals for the free parameters in the fit:
ui.conf("mydata")
conf = ui.get_conf_results()
print(conf)
or, if you want a fancy display, you can get the Jupter notebook to display it:
conf
Sherpa allows to inspect the parameter space. In the cell below we ask sherpa to show us the projection of the confidence regions for the c0
and c1
parameters. The contours are configurable by the user: by default they show the confidence curves at 1, 2, and 3 $\sigma$
ui.reg_proj(poly.c0, poly.c1)
We can also directlty inspect the parameter space. For instance, in the plot below Sherpa displays the Interval Projection of the c0
parameter, i.e. a plot of the error for each value of the parameter, around the minimum found by the optimization method during the fit.
ui.int_proj(poly.c0)