| Larch: Data Analysis Tools for X-ray Spectroscopy

# 13. Fitting and Modeling Data

A key motivation for Larch is to provide easy and robust ways to model data and perform complex fits of data to models. Data modeling and fitting can be messy and challenging tasks, so a major factor in Larch’s design was to make this as simple as possible. This chapter discusses the basic concepts for building models, setting up and performing fits, and inspecting the results.

The concepts presented here focus on modeling and fitting of general spectra and data. Of course, Larch can provides other, specific functions for doing fits, such as the EXAFS procedures `_xafs.autobk()` and `_xafs.feffit()`. Many of these concepts (and the underlying fitting algorithms) are used for those other functions as well.

- 13.1. Fitting Overview
- 13.2. Parameters
  - `param()`
  - `guess()`
  - 13.2.1. setting bounds
  - 13.2.2. using algebraic constraints
  - 13.2.3. `param_group()`: creating a Parameter Group for fitting constraints
  - 13.2.4. fiteval and details about algebraic constraints
  - 13.2.5. working with uncertainties
- 13.3. `minimize()` and objective Functions
  - `minimize()`
- 13.4. Fit Results and Outputs
  - `fit_report()`
- 13.5. Some Builtin Line-shape Functions
  - `gaussian()`
  - `lorentzian()`
  - `voigt()`
  - `pvoigt()`
  - `pearson7()`
  - `students_t()`
  - `breit_wigner()`
  - `lognormal()`
- 13.6. Fit Examples
  - 13.6.1. Example 1: Fitting a Simple Gaussian
  - 13.6.2. Example 2: Fitting XANES Pre-edge Peaks
  - 13.6.3. Example 3: Fitting XANES Spectra as a Linear Combination of Other Spectra
- 13.7. Simplified Peak Fitting with `fit_peak()`
  - `fit_peak()`
  - 13.7.1. Example: Fitting a Gaussian + background with `fit_peak()`
  - 13.7.2. Example: Fitting a Rectangular function with `fit_peak()`
  - 13.7.3. Using lmfit.Model
- 13.8. Advanced Confidence Intervals and Chi-square maps
  - `confidence_intervals()`
  - `confidence_report()`
  - `f_test()`
  - 13.8.1. Confidence interval, Example 1
  - 13.8.2. Confidence interval, Example 2
  - 13.8.3. Chi-square Maps