This tutorial shows some basic Sherpa features.

Workflow:

- create synthetic data: a parabola with noise and error bars
- load data in Sherpa
- plot data using matplotlib
- set, inspect, edit a model to fit the data
- fit the data
- compute the confidence intervals for the parameters
- explore the parameter space

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.

In [1]:

```
%matplotlib inline
```

The following commands just avoid some unnecessary logging duplication when using Sherpa:

In [2]:

```
import logging
logging.getLogger('sherpa').propagate = 0
```

Now, let's create a simple synthetic dataset, using numpy: a parabola between x=-5 and x=5, with some randomly generated noise

In [3]:

```
import numpy as np
x = np.arange(-5, 5.1)
y = x*x + 23.2 + np.random.normal(size=x.size)
e = np.ones(x.size)
```

Let's import Sherpa:

In [4]:

```
from sherpa.astro import ui as sherpa
```

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 `pyfits`

package is installed).

In [5]:

```
sherpa.load_arrays("mydata", x, y, e)
sherpa.plot_data("mydata")
```

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.

In [6]:

```
sherpa.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.

In [7]:

```
print(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.

In [8]:

```
sherpa.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.

In [9]:

```
sherpa.fit("mydata")
```

Notice that Sherpa used a Levemberg Marquadt minimization strategy, and the $\chi^2$ error function. The best fit values of the parameters $c_0=22.9, c_1=-0.1, c_2=1.1$ are close to the ones defined when we generated the dataset, i.e. $23.2, 0, 1$

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.

In [10]:

```
sherpa.plot_fit_resid("mydata")
```

We can now compute the confidence intervals for the free parameters in the fit:

In [11]:

```
sherpa.conf("mydata")
```

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$

In [12]:

```
sherpa.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.

In [13]:

```
sherpa.int_proj(poly.c0)
```