# Optimal Binning Tutorial

## Overview

When performing spectral analysis, the binning of your spectrum matters. There are several tools now available to help you obtain optimal (or nearly optimal) binning.

Read this thread if you want to: What is Optimal Binning and How to Obtain It

Last update: 2021-10-13

## Introduction

When performing spectral analysis, the binning of your spectrum matters. The nature of the spectral binning affects the statistical properties of your spectral fit. Therefore, it is worth obtaining the best possible binning for your spectrum before starting to perform spectral fitting.

Spectral binning may affect your spectrum in several different ways.

In the low counts limit, too few counts may lead to a bias in the spectral fitting. Most spectral fitting applications, including XSPEC, assume Gaussian statistics (or nearly Gaussian), which effectively implies that there are enough counts in each spectral bin of your fit. If that is not the case, then the fitting process may drive the parameters to a biased value, far from its "true" value. Goodness of fit (chi-square) and confidence intervals may also be biased.

The temptation is present to widen spectral bins, and therefore increase the number of counts. If this process is done naively, there is a risk of averaging over sharp or narrow spectral or detector features.

XSPEC does have a feature known as "setplot rebin", but this feature only governs the display of the spectrum, and does not affect how the spectral fit or goodness of fit are reported.

NICER spectral bins are naturally 10 eV wide, and it is not easy (or recommended) to attempt to change this bin width. However, HEASoft supports the notion of "grouping" or combining adjacent bins into larger effective bins. This can be done with additional metadata and not disturbing the original counts in the spectrum.

Tools such as "grppha" and "ftgrouppha" are available that aid the analyst in performing grouping/rebinning. This thread discusses how to use "ftgrouppha" with "optimal" binning.

## What is Optimal Binning?

Although grouping/rebinning may be performed in a coarse way by simply combining set numbers of spectral bins - for example, rebinning by a factor of two - this may not always be desirable.

The best grouping strategy should take into account where the counts are the greatest and where the sharpest features are present in the spectrum or response (requiring less rebinning), but also where counts are least (requiring more rebinning).

Kaastra & Bleeker (2016) introduced a concept of optimal binning of spectra. This technique attempts to find the best binning that preserves the most information.

The Kaastra & Bleeker technique is implemented in the tool 'ftgrouppha'.

In addition, the 'ftgrouppha' tool implements a variation of the technique, which we will highlight here, which ensures a minimum number of counts per spectral bin, which - as mentioned - minimizes the statistical bias for many spectral fitting applications.

## Prerequisites

Here we assume:

• Properly installed HEASoft
• A NICER spectrum myspectrum.pha
• Know the minimum number of counts you desire

The minimum number of counts is typically 10-25. A value of 10 is the bare minimum for approaching Gaussian statistics (~3 sigma), while a value of 25 leaves a more comfortable margin. However, this value may need to be adjusted based upon feedback or experience.
Figure 1. Example ungrouped spectrum.

Figure 1 is the example spectrum we will work with, before any grouping has been applied. This spectrum has the standard NICER default of 10 eV for each and every bin width.

## Apply Grouping (with Minimum Counts)

You will use the 'ftgrouppha' command to apply a specific grouping. `ftgrouppha myspectrum.pha myspectrum_grp25.pha grouptype=optmin groupscale=25 respfile=myresponse.rmf`

where

• myspectrum.pha is the name of the spectrum before grouping
• myspectrum_grp25.pha is the name of the output grouped spectrum
• grouptype is the type of grouping ("optmin" is the Kaastra & Bleeker optimal binning with minimum counts)
• groupscale is the minimum number of counts (here 25)
• respfile is the name of your RMF file (here myresponse.rmf)

The result is a grouped spectrum (myspectrum_grp25.pha in this example), which can be loaded into XSPEC.
Figure 2. Grouped spectrum after applying optimal binning with 25 count minimum.

Figure 2 shows an example spectrum that has been groupd in this way.

## Apply Grouping (no Minimum Counts)

Here we will use ftgrouppha with optimal binning and no minimum number of counts. `ftgrouppha myspectrum.pha myspectrum_grpopt.pha grouptype=opt respfile=myresponse.rmf`

where

• myspectrum.pha is the name of the spectrum before grouping
• myspectrum_grpopt.pha is the name of the output grouped spectrum
• grouptype is the type of grouping ("opt" is the Kaastra & Bleeker optimal binning with no minimum counts)
• groupscale is not present because it is not needed for this grouping type
• respfile is the name of your RMF file (here myresponse.rmf)

Figure 3. Grouped spectrum after applying optimal binning with no count minimum.

Figure 3 shows an example spectrum with pure optimal binning (and non minimum counts).

## What's Next: How to Use Grouped Spectra?

You may use a spectrum that has been rebinned by grouping (such as with ftgrouppha), in XSPEC as you would with any NICER spectrum. There is no need to rebin the response files. Plots of spectra will automatically be rebinned as necessary to comply with the requested rebinning.

## Acknowledgement

Portions contributed by Ethan Partington.

## References

Kaastra, J. S. & Bleeker, J. A. M. 2016, Astronomy & Astrophysics, 587, A151 DOI 10.1051/0004-6361/201527395

## Modifications

• 2021-07-23 - initial draft
• 2021-10-13 - correct typos and change ftgrouppha command to add respfile keyword (thanks T. Strohmayer & N. S. Cowboy)