astro py:docs

- Index
- Modules

### Navigation

- next »
- « previous |
- Astropy v5.2.1 »
- Models and Fitting (`astropy.modeling`)

# Models and Fitting (`astropy.modeling`)

## Introduction

`astropy.modeling` provides a framework for representing models and performing model evaluation and fitting. A number of predefined 1-D and 2-D models are provided and the capability for custom, user defined models is supported. Different fitting algorithms can be used with any model. For those fitters with the capabilities fitting can be done using uncertainties, parameters with bounds, and priors.

## Using Modeling

- Models
  - Basics
  - Model Sets
  - Model Serialization (Writing a Model to a File)
- Compound Models
  - Basics
  - A comprehensive description
  - Other Topics
- Model Parameters
  - Basics
  - Parameter constraints
  - Parameter examples
- Fitting
  - Notes on non-linear fitting
  - Simple 1-D model fitting
  - Simple 2-D model fitting
- Using Units with Models and Fitting
  - Setting parameters to quantities
  - Evaluating models with quantities
  - Fitting models with units to data
  - Support for units in otherwise unitless models

## A Simple Example

This simple example illustrates defining a model, calculating values based on input x values, and using fitting data with a model.

>     import numpy as np
>     import matplotlib.pyplot as plt
>     from astropy.modeling import models, fitting

>     # define a model for a line
>     line_orig = models.Linear1D(slope=1.0, intercept=0.5)

>     # generate x, y data non-uniformly spaced in x
>     # add noise to y measurements
>     npts = 30
>     rng = np.random.default_rng(10)
>     x = rng.uniform(0.0, 10.0, npts)
>     y = line_orig(x)
>     y += rng.normal(0.0, 1.5, npts)

>     # initialize a linear fitter
>     fit = fitting.LinearLSQFitter()

>     # initialize a linear model
>     line_init = models.Linear1D()

>     # fit the data with the fitter
>     fitted_line = fit(line_init, x, y)

>     # plot the model
>     plt.figure()
>     plt.plot(x, y, 'ko', label='Data')
>     plt.plot(x, fitted_line(x), 'k-', label='Fitted Model')
>     plt.xlabel('x')
>     plt.ylabel('y')
>     plt.legend()

> (png, svg, pdf)

## Advanced Topics

- Performance Tips
- Extending Models
  - Basic custom models
  - A step by step definition of a 1-D Gaussian model
  - A full example of a LineModel
- Extending Fitters
  - Defining a Plugin Fitter
  - Using a Custom Statistic Function
- Adding support for units to models
  - Evaluation
  - Fitting
- JointFitter
- Example: Spectral Line

## Pre-Defined Models

Some of the pre-defined models are listed and illustrated.

- 1D Models
  - Operations
  - Shapes
  - Profiles
- 2D Models
  - Operations
  - Shapes
  - Profiles
- Physical Models
  - BlackBody
  - Drude1D
  - NFW
  - Cosmologies
- Polynomial Models
  - Notes regarding usage of domain and window
  - 1D Polynomials
  - 2D Polynomials
- Powerlaw Models
- Spline Models

## Examples

- Fitting a line
  - Simple Fit
  - Fit using uncertainties
  - Iterative fitting using sigma clipping
- Fitting with constraints
  - Fixed Parameter Constraint
  - Bounded Constraints
  - Tied Constraints
- Fitting Model Sets

## Reference/API

- Reference/API