Debian Codegraph @

hugo/content/ref/fit/ex/polarized-spinasymmetry-fit.md

raw
1 +++
2 title = "Spin asymmetry fit example"
3 weight = 40
4 +++
5
6 ### Fitting the spin-asymmetry example from NIST
7
8 This example shows how to fit the parameters in the [spin-asymmetry example](/ref/instr/pol/polarized-spinasymmetry).
9
10 For this demonstration, we choose initial parameters that are not too far from the fitting results.
11 In particular, the magnetization is initially set to zero, such that the spin asymmetry identically vanishes.
12
13 With the initial parameters, we obtain the following reflectivity and spin-asymmetry curves:
14
15 {{< galleryscg >}}
16 {{< figscg src="/files/fitted/SpinAsymmetryInitial1.png" width="350px" caption="Reflectivity">}}
17 {{< figscg src="/files/fitted/SpinAsymmetryInitial2.png" width="350px" caption="Spin Asymmetry">}}
18 {{< /galleryscg >}}
19
20
21
22
23 #### Setup of the Fit
24
25 For fitting of reflectometry data covering several orders of magnitude we use the $\chi^2$ metric
26
27 $$\chi^2 = \sum_{i = 1}^N \frac{\left( d_i - s_i \right)^2}{\sigma_i^2}$$
28
29 Here $d_i$ is the $i$-thexperimental data point, $\sigma_i$ is its uncertainty and
30 $s_i$ is the corresponding simulation result.
31
32 This is supported in BornAgain by setting
33
34 ```python
35 fit_objective.setObjectiveMetric("chi2")
36 ```
37
38 Note that in order to obtain good results, one needs to provide the uncertainties
39 of the reflectivity.
40 If no uncertainties are available, using the relative difference `fit_objective.setObjectiveMetric("reldiff")` yields better results.
41 If the relative difference is selected and uncertainties are provided, BornAgain automatically falls back to the above $\chi^2$ metric.
42
43 The fitting of polarized reflectometry data proceeds similar to the lines presented in
44 [the tutorial on multiple datasets](/ref/fit/ex/multiple-datasets).
45 We need to add the reflectivity curves for the up-up and down-down channel
46 to the fit objective:
47
48 ```python
49 fit_objective.addFitPair(SimulationFunctionPlusplus,
50 r_data_pp, 1.0)
51 fit_objective.addFitPair(SimulationFunctionMinusMinus,
52 r_data_mm, 1.0)
53 ```
54
55 `SimulationFunctionPlusplus` and `SimulationFunctionMinusMinus` are
56 two callables that accept a parameter dict and return a
57 `Simulation` object for the up-up and down-down channels, respectively.
58 Experimental uncertainties are provided inside the data `Datafield`
59 objects as error sigmas.
60
61 The fit parameters are defined in the dictionary `startParams`, where they are defined as a triple of values `(start, min, max)`.
62 If no fit is performed the values obtained from our own fit are stored in `fixedParams` and are subsequently used
63 to simulate the system.
64
65 We want to fit the following parameters:
66
67 * `q_res`: Relative $Q$-resolution
68 * `q_offset`: Shift of the $Q$-axis.
69 * `t_Mafo`: The thickness of the layer
70 * `rho_Mafo`: The SLD of the layer
71 * `rhoM_Mafo`: The magnetic SLD of the layer
72 * `r_Mao`: The roughness on top of the substrate
73 * `r_Mafo`: The roughness on top of the magnetic layer
74
75
76 #### Fit Result
77
78 After running the fit using
79
80 ```python
81 python3 PolarizedSpinAsymmetryFit.py
82 ```
83
84 we get the result
85
86 {{< galleryscg >}}
87 {{< figscg src="/files/fitted/SpinAsymmetry1.png" width="350px" caption="Reflectivity">}}
88 {{< figscg src="/img/auto/specular/PolarizedSpinAsymmetry.png" width="350px" caption="Spin Asymmetry">}}
89 {{< /galleryscg >}}
90
91
92 This result was already presented in the [spin-asymmetry](/ref/instr/pol/polarized-spinasymmetry) tutorial and can also be plotted by runnning
93
94 ```python
95 python3 PolarizedSpinAsymmetry.py
96 ```
97
98
99 Here is the complete example:
100
101 {{< show-ex file="fit/specular/PolarizedSpinAsymmetryFit.py" >}}
102
103 Data to be fitted: {{% ref-data-spec MAFO_Saturated_mm.tab %}}, {{% ref-data-spec MAFO_Saturated_pp.tab %}}