Fitting Grating Data
![[CXC Logo]](/ciao/imgs/cxc-logo.gif)
Sherpa Threads (CIAO 4.5 Sherpa v1)
Overview
Synopsis:
This thread provides a general introduction to fitting grating data in Sherpa. Loading and filtering data are covered, as well as defining instrument responses and source models.
Users working with HRC-S/LETG grating data will also find the Fitting Multiple Orders of HRC-S/LETG Data thread helpful for their analysis.
Last Update: 20 Jan 2012 - reviewed for CIAO 4.4 (no changes)
Contents
- Getting Started
- Reading the Spectrum Files
- Loading the Instrument Responses
- Filtering the Data
- Defining the Source and Background Models
- Examining Method & Statistic Settings
- Fitting
- Examining Fit Results
- Saving and Quitting the Session
- Scripting It
- History
- Images
Getting Started
The files used in this example were created by following several of the CIAO Grating threads:
Here is a list of all the necessary files:
spectra: 459_heg_m1_bin10.pha 459_heg_p1_bin10.pha 459_meg_m1_bin10.pha 459_meg_p1_bin10.pha gARFs: 459_heg_m1.garf 459_heg_p1.garf 459_meg_m1.garf 459_meg_p1.garf
The spectrum that will be used in this session has been binned by a factor of 10.
Users may also choose to run the ACIS Grating RMFs thread. Creating observation-specific gRMFs is optional, and is discussed further in the Loading the Instrument Responses section.
The data files are available in sherpa.tar.gz, as explained in the Sherpa Getting Started thread.
Reading the Spectrum Files
The source data are input to Sherpa with the load_pha command, where any associated background or response files will automatically be read in if the corresponding filenames are recorded in the header of the source data files:
sherpa> load_pha(1, "459_heg_m1_bin10.pha") WARNING: systematic errors were not found in file '459_heg_m1_bin10.pha' statistical errors were found in file '459_heg_m1_bin10.pha' but not used; to use them, re-read with use_errors=True read background_up into a dataset from file 459_heg_m1_bin10.pha read background_down into a dataset from file 459_heg_m1_bin10.pha sherpa> load_pha(2, "459_heg_p1_bin10.pha") WARNING: systematic errors were not found in file '459_heg_p1_bin10.pha' statistical errors were found in file '459_heg_p1_bin10.pha' but not used; to use them, re-read with use_errors=True read background_up into a dataset from file 459_heg_p1_bin10.pha read background_down into a dataset from file 459_heg_p1_bin10.pha sherpa> load_pha(3, "459_meg_m1_bin10.pha") WARNING: systematic errors were not found in file '459_meg_m1_bin10.pha' statistical errors were found in file '459_meg_m1_bin10.pha' but not used; to use them, re-read with use_errors=True read background_up into a dataset from file 459_meg_m1_bin10.pha read background_down into a dataset from file 459_meg_m1_bin10.pha sherpa> load_pha(4, "459_meg_p1_bin10.pha") WARNING: systematic errors were not found in file '459_meg_p1_bin10.pha' statistical errors were found in file '459_meg_p1_bin10.pha' but not used; to use them, re-read with use_errors=True read background_up into a dataset from file 459_meg_p1_bin10.pha read background_down into a dataset from file 459_meg_p1_bin10.pha
Sherpa now refers to the spectra as follows:
- HEG, -1 order = dataset 1
- HEG, +1 order = dataset 2
- MEG, -1 order = dataset 3
- MEG, +1 order = dataset 4
Note that there are two background data sets associated with each Chandra source grating data set, 'background up' and 'background down', which Sherpa assigns to background IDs 1 and 2. These contain data extracted from background regions adjacent to the source region. The two background data sets are added and scaled to the source data when being subtracted with the Sherpa subtract command, or modeled with set_bkg_model.
In order to work with only one of the two background data sets associated with a source, the get_data command may be used to remove the unneeded background, as shown below:
sherpa> print get_data(1).background_ids
[1,2]
sherpa> get_data(1).background_ids = [1] # set background 1 as the only
# background to fit.
Loading the Instrument Responses
The instrument response is established when the appropriate response files (ARF, RMF) are input to the Sherpa session. If the names of the ARF and RMF response files are recorded in the header of the PHA file, Sherpa will load them automatically when the PHA file is read with load_pha; if not, they need to be loaded manually with the load_arf and load_rmf commands.
Since we are working with grating data in this example, we load only the ARF files corresponding to each of the four orders and the associated backgrounds:
sherpa> load_arf(1, "459_heg_m1.arf") sherpa> load_arf(2, "459_heg_p1.arf") sherpa> load_arf(3, "459_meg_m1.arf") sherpa> load_arf(4, "459_meg_p1.arf") sherpa> load_arf(1, "459_heg_m1.arf", bkg_id=1) sherpa> load_arf(1, "459_heg_m1.arf", bkg_id=2) sherpa> load_arf(2, "459_heg_p1.arf", bkg_id=1) sherpa> load_arf(2, "459_heg_p1.arf", bkg_id=2) sherpa> load_arf(3, "459_meg_m1.arf", bkg_id=1) sherpa> load_arf(3, "459_meg_m1.arf", bkg_id=2) sherpa> load_arf(4, "459_meg_p1.arf", bkg_id=1) sherpa> load_arf(4, "459_meg_p1.arf", bkg_id=2)
The current definition of the instrument response, along with information about the loaded data sets, may be examined using the commands show_data and show_bkg:
sherpa> show_data() Data Set: 1 Filter: 0.5775-12.3676 Energy (keV) Noticed Channels: 1-8192 name = 459_heg_m1_bin10.pha channel = Float64[8192] counts = Float64[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 1.0 areascal = 1.0 grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [1, 2] ARF Data Set: 1:1 name = 459_heg_m1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38565.2847986 Data Set: 2 Filter: 0.5775-12.3676 Energy (keV) Noticed Channels: 1-8192 name = 459_heg_p1_bin10.pha channel = Float64[8192] counts = Float64[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 1.0 areascal = 1.0 grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [1, 2] ARF Data Set: 2:1 name = 459_heg_p1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38563.0133422 Data Set: 3 Filter: 0.2957-12.3370 Energy (keV) Noticed Channels: 1-8192 name = 459_meg_m1_bin10.pha channel = Float64[8192] counts = Float64[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 1.0 areascal = 1.0 grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [1, 2] ARF Data Set: 3:1 name = 459_meg_m1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38565.2855739 Data Set: 4 Filter: 0.2957-12.3370 Energy (keV) Noticed Channels: 1-8192 name = 459_meg_p1_bin10.pha channel = Float64[8192] counts = Float64[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 1.0 areascal = 1.0 grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [1, 2] ARF Data Set: 4:1 name = 459_meg_p1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38563.0141169 sherpa> show_bkg() Background Data Set: 1:1 Filter: 0.5775-12.3676 Energy (keV) Noticed Channels: 1-8192 name = 459_heg_m1_bin10.pha channel = Float64[8192] counts = Int16[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 4.0188284 areascal = None grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [] Background ARF Data Set: 1:1 name = 459_heg_m1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38565.2847986 Background Data Set: 1:2 Filter: 0.5775-12.3676 Energy (keV) Noticed Channels: 1-8192 name = 459_heg_m1_bin10.pha channel = Float64[8192] counts = Int16[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 4.0188284 areascal = None grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [] Background ARF Data Set: 1:2 name = 459_heg_m1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38565.2847986 Background Data Set: 2:2 Filter: 0.5775-12.3676 Energy (keV) Noticed Channels: 1-8192 name = 459_heg_p1_bin10.pha channel = Float64[8192] counts = Int16[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 4.0188284 areascal = None grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [] Background ARF Data Set: 2:2 name = 459_heg_p1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38563.0133422 Background Data Set: 3:2 Filter: 0.2957-12.3370 Energy (keV) Noticed Channels: 1-8192 name = 459_meg_m1_bin10.pha channel = Float64[8192] counts = Int16[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 4.0188284 areascal = None grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [] Background ARF Data Set: 3:2 name = 459_meg_m1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38565.2855739 Background Data Set: 4:2 Filter: 0.2957-12.3370 Energy (keV) Noticed Channels: 1-8192 name = 459_meg_p1_bin10.pha channel = Float64[8192] counts = Int16[8192] staterror = None syserror = None bin_lo = Float64[8192] bin_hi = Float64[8192] grouping = Int16[8192] quality = Int16[8192] exposure = 38564.6089269 backscal = 4.0188284 areascal = None grouped = True subtracted = False units = energy rate = True plot_fac = 0 response_ids = [1] background_ids = [] Background ARF Data Set: 4:2 name = 459_meg_p1.arf energ_lo = Float64[8192] energ_hi = Float64[8192] specresp = Float64[8192] bin_lo = Float64[8192] bin_hi = Float64[8192] exposure = 38563.0141169
Before plotting the data with the plot command, ensure that the units field of each data set is set to "wavelength" with the set_analysis command, as in the following example:
sherpa> show_filter() Data Set Filter: 1 0.5775-12.3676 Energy (keV) Data Set Filter: 2 0.5775-12.3676 Energy (keV) Data Set Filter: 3 0.2957-12.3370 Energy (keV) Data Set Filter: 4 0.2957-12.3370 Energy (keV) sherpa> set_analysis("wave") sherpa> show_filter() Data Set Filter: 1 1.0025-21.4675 Wavelength (Angstrom) Data Set Filter: 2 1.0025-21.4675 Wavelength (Angstrom) Data Set Filter: 3 1.0050-41.9350 Wavelength (Angstrom) Data Set Filter: 4 1.0050-41.9350 Wavelength (Angstrom)
The data may now be plotted:
sherpa> plot("data", 1, "data", 2, "data", 3, "data", 4)
Figure 1 shows the resulting plot.
Filtering the Data
We choose to filter the data to focus on an area of interest:
sherpa> ignore() sherpa> notice(1., 15.)
The ignore command is used to ignore all the data in every data set, then notice is used to select the desired data range in all data sets. You may wish to adjust the limits to exclude more or less of your data. To apply ignore() or notice() data constraints to a specific data set, use the ignore_id and notice_id commands, respectively.
Each filtered data set may then be plotted:
sherpa> plot("data", 1, "data", 2, "data", 3, "data", 4)
Notice that the plot now includes only the data in the specified wavelength range. Figure 2 shows the resulting plot.
![[Plot of ACIS HEG and MEG +/- 1 orders of 3C 273, restricted to the wavelength range 1.0 - 15.0 angstroms]](2.png)
![[Print media version: Plot of ACIS HEG and MEG +/- 1 orders of 3C 273, restricted to the wavelength range 1.0 - 15.0 angstroms]](2.png)
Figure 2: Filtering the data sets
Plot of ACIS HEG and MEG +/- 1 orders of 3C 273, restricted to the wavelength range 1.0 - 15.0 angstroms.
Defining the Source and Background Models
We plan on simultaneously fitting the background data with the source data (rather than subtracting it), so we need to create a model expression for both the source and background. When modeling (or subtracting) the grating background, Sherpa adds the two background up/down counts arrays and scales the result to the associated source data.
We model this source with a broken power law (bpl1d) absorbed by the interstellar medium (ISM) (atten). The background will be modeled by a one-dimensional power law (powlaw1d), also absorbed by the ISM (the same atten model).
First, we set up the model components with create_model_component, and set some initial parameter values. The absorption model is referred to as "abs1", the broken power law is "bpow1", and the 1-D power law is "pow1d":
sherpa> create_model_component("atten", "abs1")
sherpa> abs1.integrate = "false"
sherpa> abs1.hcol = 1e+20
sherpa> abs1.heiRatio = 0.1
sherpa> abs1.heiiRatio = 0.01
sherpa> create_model_component("bpl1d", "bpow1")
sherpa> bpow1.gamma1 = 0
sherpa> bpow1.gamma2 = 0
sherpa> bpow1.eb = 7.99625
sherpa> freeze(bpow1.ref)
sherpa> bpow1.ampl = 0.001
sherpa> create_model_component("powlaw1d","pow1d")
sherpa> pow1d.gamma = 1
sherpa> freeze(pow1d.ref)
sherpa> pow1d.ampl = 1e-5
We freeze the normalization parameters for bpow1 and pow1d (bpow1.ref and pow1d.ref) without changing the default values. For the bpow1 and pow1d parameters for which we did set initial values, we could have used the Sherpa guess() function to estimate reasonable starting values, based on the data input to the Sherpa session. To have Sherpa automatically query for initial parameter values when a model is established, set 'paramprompt(True)' (it is 'False' by default).
The model parameter values can be listed with print() command (note that show_model/show_source is appropriate once the full model expression has been assigned to the data with set_source:
sherpa> print(abs1) atten.abs1 Param Type Value Min Max Units ----- ---- ----- --- --- ----- abs1.hcol thawed 1e+20 1e+17 1e+24 abs1.heiRatio thawed 0.1 0 1 abs1.heiiRatio thawed 0.01 0 1 sherpa> print(bpow1) bpl1d.bpow1 Param Type Value Min Max Units ----- ---- ----- --- --- ----- bpow1.gamma1 thawed 0 -10 10 bpow1.gamma2 thawed 0 -10 10 bpow1.eb thawed 7.99625 0 1e+03 bpow1.ref frozen 1 -3.40282e+38 3.40282e+38 bpow1.ampl thawed 0.001 1e-20 3.40282e+38 sherpa> print(pow1d) powlaw1d.pow1d Param Type Value Min Max Units ----- ---- ----- --- --- ----- pow1d.gamma thawed 1 -10 10 pow1d.ref frozen 1 -3.40282e+38 3.40282e+38 pow1d.ampl thawed 1e-05 0 3.40282e+38
Next we modify the initial parameter value for abs1.hcol:
sherpa> abs1.hcol = 1.81e20 sherpa> freeze(abs1)
The hydrogen column density (hcol) is set to the Galactic value. All the abs1 parameters are then frozen, which means they will not be allowed to vary during the fit.
Now that the model components have been established, the product of abs1 and bpow1 is assigned as the source model for all data sets :
sherpa> set_source(1, abs1*bpow1) sherpa> set_source(2, abs1*bpow1) sherpa> set_source(3, abs1*bpow1) sherpa> set_source(4, abs1*bpow1)
while the background model is set as the product of abs1 and pow1d:
sherpa> set_bkg_model(1, abs1*pow1d, 1) sherpa> set_bkg_model(1, abs1*pow1d, 2) sherpa> set_bkg_model(2, abs1*pow1d, 1) sherpa> set_bkg_model(2, abs1*pow1d, 2) sherpa> set_bkg_model(3, abs1*pow1d, 1) sherpa> set_bkg_model(3, abs1*pow1d, 2) sherpa> set_bkg_model(4, abs1*pow1d, 1) sherpa> set_bkg_model(4, abs1*pow1d, 2)
The source and background model definitions can be listed with show_model and show_bkg_model:
sherpa> show_model()
Model: 1
apply_arf((38564.6089269 * ((atten.abs1 * bpl1d.bpow1) + scale_factor * ((atten.abs1 * powlaw1d.pow1d) + (atten.abs1 * powlaw1d.pow1d)))))
Param Type Value Min Max Units
----- ---- ----- --- --- -----
abs1.hcol frozen 1.81e+20 1e+17 1e+24
abs1.heiRatio frozen 0.1 0 1
abs1.heiiRatio frozen 0.01 0 1
bpow1.gamma1 thawed 0 -10 10
bpow1.gamma2 thawed 0 -10 10
bpow1.eb thawed 7.99625 0 1000
bpow1.ref frozen 1 -3.40282e+38 3.40282e+38
bpow1.ampl thawed 0.001 1e-20 3.40282e+38
pow1d.gamma thawed 1 -10 10
pow1d.ref frozen 1 -3.40282e+38 3.40282e+38
pow1d.ampl thawed 1e-05 0 3.40282e+38
Model: 2
{same as above}
Model: 3
{same as above}
Model: 4
{same as above}
sherpa> show_bkg_model()
Background Model: 1:1
apply_arf((38564.6089269 * (atten.abs1 * powlaw1d.pow1d)))
Param Type Value Min Max Units
----- ---- ----- --- --- -----
abs1.hcol frozen 1.81e+20 1e+17 1e+24
abs1.heiRatio frozen 0.1 0 1
abs1.heiiRatio frozen 0.01 0 1
pow1d.gamma thawed 1 -10 10
pow1d.ref frozen 1 -3.40282e+38 3.40282e+38
pow1d.ampl thawed 1e-05 0 3.40282e+38
Background Model: 1:2
{same as above}
Background Model: 2:1
{same as above}
Background Model: 2:2
{same as above}
Background Model: 3:1
{same as above}
Background Model: 3:2
{same as above}
Background Model: 4:1
{same as above}
Background Model: 4:2
{same as above}
Examining Method & Statistic Settings
Next we check the current method and statistics settings:
sherpa> show_method() Optimization Method: LevMar name = levmar ftol = 1.19209289551e-07 xtol = 1.19209289551e-07 gtol = 1.19209289551e-07 maxfev = None epsfcn = 1.19209289551e-07 factor = 100.0 verbose = 0 sherpa> show_stat() Statistic: Chi2Gehrels Chi Squared with Gehrels variance
The Sherpa default fitting statistic and optimization method are Chi2Gehrels and Levenberg-Marquardt, respectively. For this fit, we will use the Nelder-Mead Simplex method and the CStat statistic; this is because Chi2Gehrels could bias the fit results and yield an artificially low reduced statistic for this data, and the CStat (and Cash) statistic is appropriate for simultaneously fitting source and background data. For a list of all the available methods and statistic settings, see the Sherpa Statistics and Optimization Methods pages.
To change the current method and statistic, we use set_method and set_stat.
sherpa> set_method("neldermead")
sherpa> set_stat("cstat")
Fitting
The data sets are now fit:
sherpa> fit() Datasets = 1, 2, 3, 4 Method = neldermead Statistic = cstat Initial fit statistic = 100816 Final fit statistic = 5976.06 at function evaluation 1527 Data points = 5052 Degrees of freedom = 5046 Probability [Q-value] = 1.06549e-18 Reduced statistic = 1.18432 Change in statistic = 94840 bpow1.gamma1 0.424711 bpow1.gamma2 0.0496577 bpow1.eb 10.62 bpow1.ampl 0.00548927 pow1d.gamma 0.331504 pow1d.ampl 8.87632e-05
To plot the fits:
sherpa> plot("fit", 1, "fit", 2, "fit", 3, "fit", 4)
WARNING: unable to calculate errors using current statistic: cstat
WARNING: unable to calculate errors using current statistic: cstat
WARNING: unable to calculate errors using current statistic: cstat
WARNING: unable to calculate errors using current statistic: cstat
sherpa> current_plot("all")
sherpa> set_plot_title("3C 273 (ObsID 459)")
sherpa> current_plot("plot1")
sherpa> add_label(10, 0.1, "HEG -1")
sherpa> set_label(["color","green"])
sherpa> current_plot("plot2")
sherpa> add_label(10, 0.1, "HEG +1")
sherpa> set_label(["color","green"])
sherpa> current_plot("plot3")
sherpa> add_label(10, 0.15, "MEG -1")
sherpa> set_label(["color","green"])
sherpa> current_plot("plot4")
sherpa> add_label(10, 0.15, "MEG +1")
sherpa> set_label(["color","green"])
The ChIPS commands are used to add a title and labels to the drawing area. The plot is shown in Figure 3.
![[Labeled plots of the simultaneous fit on ACIS HEG and MEG +/- 1 orders of 3C 273]](3.png)
![[Print media version: Labeled plots of the simultaneous fit on ACIS HEG and MEG +/- 1 orders of 3C 273]](3.png)
Figure 3: Results of Simultaneous Fit
Labeled plots of the simultaneous fit on ACIS HEG and MEG +/- 1 orders of 3C 273.
Note that the cstat statistic does not calculate errors for the data points. Since it is useful to do so, we change the fit statistic to something suitable for calculating errors, and view the residuals of the fit with the plot_fit_delchi command:
sherpa> set_stat('chi2xspecvar')
sherpa> plot_fit_delchi()
This plot is shown in Figure 4.
After creating a plot, it may be saved as a PostScript file; in this example, "all.ps" is returned:
sherpa> print_window("all")
Examining Fit Results
In CIAO 3.4, the GOODNESS command was used to get the chi-squared goodness-of-fit. This information is now reported with the best-fit values after a fit; it is no longer necessary to run a separate command. The commands get_fit_results and show_fit allow access to this information after the fit has been performed:
sherpa> show_fit() Optimization Method: NelderMead name = simplex ftol = 1.19209289551e-07 maxfev = None initsimplex = 0 finalsimplex = 9 step = None iquad = 1 verbose = 0 Statistic: Chi2XspecVar Chi Squared with data variance (XSPEC style) Fit:Datasets = 1, 2, 3, 4 Method = neldermead Statistic = cstat Initial fit statistic = 100816 Final fit statistic = 5976.06 at function evaluation 1527 Data points = 5052 Degrees of freedom = 5046 Probability [Q-value] = 1.06549e-18 Reduced statistic = 1.18432 Change in statistic = 94840 bpow1.gamma1 0.424711 bpow1.gamma2 0.0496577 bpow1.eb 10.62 bpow1.ampl 0.00548927 maxfev = None initsimplex = 0 finalsimplex = 9 step = None iquad = 1 verbose = 0 Statistic: Chi2XspecVar Chi Squared with data variance (XSPEC style) Fit:Datasets = 1, 2, 3, 4 Method = neldermead Statistic = cstat Initial fit statistic = 100816 Final fit statistic = 5976.06 at function evaluation 1527 Data points = 5052 Degrees of freedom = 5046 Probability [Q-value] = 1.06549e-18 Reduced statistic = 1.18432 Change in statistic = 94840 bpow1.gamma1 0.424711 bpow1.gamma2 0.0496577 bpow1.eb 10.62 bpow1.ampl 0.00548927 pow1d.gamma 0.331504 pow1d.ampl 8.87632e-05
The number of bins in the fit (Data points), the number of degrees of freedom (i.e. the number of bins minus the number of free parameters), and the final fit statistic value are reported. If the chosen statistic is one of the chi-square statistics, as in this example, the reduced statistic, i.e. the statistic value divided by the number of degrees of freedom, and the probability (Q-value) are included as well.
The calc_chisqr command calculates the statistic contribution per bin; in this example, the results for data set 1 are returned:
sherpa> calc_chisqr() array([ 0., 0., 0., 0., ... , 1.17970306e-06, 9.99029138e-01, 8.38311915e-08, 2.42387626e-08, 3.06732525e-09, 1.39226106e-09, 1.18170110e-10, 4.34535088e-11, 1.37691508e-12])
The confidence (conf), covariance (covar) and projection (proj) commands can be used to estimate confidence intervals for the thawed parameters:
sherpa> set_stat("cstat")
sherpa> conf()
bpow1.ampl -: WARNING: The confidence level lies within (5.395765e-03, 5.442
519e-03)
bpow1.ampl lower bound: -7.01316e-05
pow1d.ampl lower bound: -5.50067e-06
bpow1.gamma1 -: WARNING: The confidence level lies within (4.157534e-01, 4.2
02320e-01)
bpow1.gamma1 lower bound: -0.00671795
bpow1.gamma2 lower bound: -0.063624
bpow1.eb lower bound: -0.318358
pow1d.gamma lower bound: -0.0324928
bpow1.eb upper bound: 0.281198
bpow1.gamma2 upper bound: 0.0683777
pow1d.gamma upper bound: 0.0323259
pow1d.ampl upper bound: 5.84175e-06
bpow1.ampl upper bound: 7.30538e-05
bpow1.gamma1 upper bound: 0.00706784
Datasets = 1, 2, 3, 4
Confidence Method = confidence
Fitting Method = neldermead
Statistic = cstat
confidence 1-sigma (68.2689%) bounds:
Param Best-Fit Lower Bound Upper Bound
----- -------- ----------- -----------
bpow1.gamma1 0.424711 -0.00671795 0.00706784
bpow1.gamma2 0.0496577 -0.063624 0.0683777
bpow1.eb 10.62 -0.318358 0.281198
bpow1.ampl 0.00548927 -7.01316e-05 7.30538e-05
pow1d.gamma 0.331504 -0.0324928 0.0323259
pow1d.ampl 8.87632e-05 -5.50067e-06 5.84175e-06
Saving and Quitting the Session
Before exiting Sherpa, you may wish to save the session in order to return to the analysis at a later point:
sherpa> save("459_fitting_session.save") sherpa> save("459_fitting_session.ascii")
The save function records all the information about the current session to the binary file 459_fitting_session.save, and the save_all function records the session settings to an editable ASCII file.
To restore the session that was saved to the binary file 459_fitting_session.save or ASCII file 459_fitting_session.ascii:
sherpa> restore("session1.save") sherpa> execfile("session1.ascii")
Finally, quit the session:
sherpa> quit
Scripting It
The file fit.py is a Python script which performs the primary commands used above; it can be executed by typing execfile("fit.py") on the Sherpa command line.
The Sherpa script command may be used to save everything typed on the command line in a Sherpa session:
sherpa> script(filename="sherpa.log", clobber=False)
(Note that restoring a Sherpa session from such a file could be problematic since it may include syntax errors, unwanted fitting trials, etcetera.)
The CXC is committed to helping Sherpa users transition to new syntax as smoothly as possible. If you have existing Sherpa scripts or save files, submit them to us via the CXC Helpdesk and we will provide the CIAO/Sherpa 4.5 syntax to you.
History
| 18 Jul 2008 | updated for CIAO 4.1 |
| 04 Dec 2008 | set_analysis(), show_data(), and show_fit() are available in Sherpa 4.1 |
| 12 Dec 2008 | create_model_component is available in Sherpa 4.1 |
| 28 Apr 2009 | replaced use of atten model with Sherpa user model "atten_wave"; new script command is available with CIAO 4.1.2 |
| 08 Jan 2010 | updated for CIAO 4.2 |
| 13 Jul 2010 | updated for CIAO 4.2 Sherpa v2: removal of S-Lang version of thread. |
| 19 Aug 2011 | updated the Reading the Spectrum Files section with information on the two background up/down data sets included in Chandra grating PHA files |
| 20 Jan 2012 | reviewed for CIAO 4.4 (no changes) |

![[Plot of ACIS HEG and MEG +/- 1 orders for 3C 273]](1.png)
![[Plot of the fit and residuals for the HEG -1 order spectrum]](4.png)