# Simple fitting of single datasets

Classes used:

Making informed guesses for the initial values of the variable parameters of a model and fit the model to the data is the most straight-forward strategy. Still, different optimisation algorithms can be chosen. The recipe presented here uses `fitpy.analysis.SimpleFit`

with standard values and shows as well how to use the dedicated plotters and reporters.

## Recipe

```
1format:
2 type: ASpecD recipe
3 version: '0.2'
4
5settings:
6 autosave_plots: false
7
8tasks:
9 # Create "dataset" to fit model to
10 - kind: model
11 type: Zeros
12 properties:
13 parameters:
14 shape: 1001
15 range: [-10, 10]
16 result: dummy
17 comment: >
18 Create a dummy model.
19 - kind: model
20 type: Gaussian
21 from_dataset: dummy
22 properties:
23 parameters:
24 position: 2
25 label: Random spectral line
26 comment: >
27 Create a simple Gaussian line.
28 result: dataset
29 - kind: processing
30 type: Noise
31 properties:
32 parameters:
33 amplitude: 0.2
34 apply_to: dataset
35 comment: >
36 Add a bit of noise.
37 - kind: singleplot
38 type: SinglePlotter1D
39 properties:
40 filename: dataset2fit.pdf
41 apply_to: dataset
42 comment: >
43 Just to be on the safe side, plot data of created "dataset"
44
45 # Now for the actual fitting: (i) create model, (ii) fit to data
46 - kind: model
47 type: Gaussian
48 from_dataset: dataset
49 output: model
50 result: gaussian_model
51
52 - kind: fitpy.singleanalysis
53 type: SimpleFit
54 properties:
55 model: gaussian_model
56 parameters:
57 fit:
58 position:
59 start: 1
60 range: [0, 5]
61 algorithm:
62 method: least_squares
63 parameters:
64 ftol: 1e-6
65 xtol: 1e-6
66 result: fitted_gaussian
67 apply_to: dataset
68
69 # Plot result
70 - kind: fitpy.singleplot
71 type: SinglePlotter1D
72 properties:
73 filename: fit_result.pdf
74 parameters:
75 show_legend: true
76 apply_to: fitted_gaussian
77
78 # Create report
79 - kind: fitpy.report
80 type: LaTeXFitReporter
81 properties:
82 template: simplefit.tex
83 filename: report.tex
84 compile: true
85 apply_to: fitted_gaussian
```

## Result

Examples for the two figures created in the recipe are given below. While in the recipe, the output format has been set to PDF, for rendering them here they have been converted to PNG. Due to the noise added to the model having an inherently random component, your data will look slightly different. Therefore, the fit results will be slightly different as well. Nevertheless, overall it should be fairly identical.

## Comments

The purpose of the first block of four tasks is solely to create some data a model can be fitted to. The actual fitting starts only afterwards.

Usually, you will have set another ASpecD-derived package as default package in your recipe for processing and analysing your data. Hence, you need to provide the package name (fitpy) in the

`kind`

property, as shown in the examples.Fitting is always a two-step process: (i) define the model, and (ii) define the fitting task.

To get a quick overview of the fit results, use the dedicated plotter from the FitPy framework:

`fitpy.plotting.SinglePlotter1D`

.For a more detailed overview, particularly in case of several fits with different settings on one and the same dataset or for a series of similar fits on different datasets, use reports, as shown here using

`fitpy.report.LaTeXFitReporter`

. This reporter will automatically create the figure showing both, fitted model and original data.