Fit with error analysis

[1]:
import nexus as nx
import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('..\hello_nexus\example_spectrum.txt')

velocity_experiment = data[:,0]
intensity_experiment = data[:,1]

site = nx.Hyperfine(magnetic_field = nx.Var(value = 31, min = 25, max = 35, fit = True, id = "magnetic field"),
                    magnetic_theta = nx.Var(value = 0, min = -15, max = 15, fit = True, id = "magnetic theta"),
                    quadrupole = nx.Var(value = 0.3, min = 0, max = 2, fit = True, id = "quadrupole"),
                    isotropic = True)

mat_Fe = nx.Material.Template(nx.lib.material.Fe)

mat_Fe.hyperfine_sites = [site]

layer_Fe = nx.Layer(id = "Fe",
                    material = mat_Fe,
                    thickness = nx.Var(value = 2900, min = 0, max = 5000, fit = True, id = "thickness"))

sample = nx.Sample(layers = [layer_Fe])

beam = nx.Beam()

beam.Unpolarized()

exp = nx.Experiment(beam = beam,
                    objects = [sample],
                    isotope = nx.lib.moessbauer.Fe57)

spectrum = nx.MoessbauerSpectrum(experiment = exp,
                                 velocity = velocity_experiment,
                                 intensity_data = intensity_experiment,
                                 scaling = "auto")

# calculate the intensity from the assumed model
intensity = spectrum.Calculate()

spectrum.Plot()
../../_images/tutorial_optimization_nb_fit_errors_1_0.png
[2]:
fit = nx.Fit(measurements = [spectrum])

fit.options.file_output = False

fit.Evaluate()

spectrum.Plot()

Run Fit instance with id:

Starting fit with 1 measurement data set(s) and 6 fit parameter(s):

  no. |                           id |       initial value |              min |              max
    0 |                   ES scaling |              2634.1 |            1e-16 |           263410
    1 |                    ES backgr |             256.611 |                0 |          25661.1
    2 |                    thickness |                2900 |                0 |             5000
    3 |               magnetic field |                  31 |               25 |               35
    4 |               magnetic theta |                   0 |              -15 |               15
    5 |                   quadrupole |                 0.3 |                0 |                2

Using 0 equality constraint(s) on parameter(s):

Using 0 inequality constraint(s).


Calling ceres solver with fit method LevMar

Ceres Solver Report: Iterations: 43, Initial cost: 4.629873e+02, Final cost: 2.967388e+01, Termination: CONVERGENCE

Gradient error analysis.


Fit performed with algorithm:
LevMar
Error analysis:
Gradient

Using 6 fit parameter(s):

  no. |                           id |          fit value |   +/- std dev | initial value |          min |          max
    0 |                   ES scaling |            1700.66 |       306.099 |        2634.1 |        1e-16 |       263410
    1 |                    ES backgr |            1256.44 |       331.395 |       256.611 |            0 |      25661.1
    2 |                    thickness |               5000 |       1405.16 |          2900 |            0 |         5000
    3 |               magnetic field |            33.0131 |    0.00972019 |            31 |           25 |           35
    4 |               magnetic theta |                  0 |             0 |             0 |          -15 |           15
    5 |                   quadrupole |       -1.49012e-08 |    0.00265477 |           0.3 |            0 |            2

    Values at boundaries:
      thickness at UPPER boundary: 5000
      quadrupole at LOWER boundary: 0

Correlation matrix:

  no. |        0        1        2        3        4        5
 -----|------------------------------------------------------
    0 |    1.000   -1.000   -0.998   -0.000      nan   -0.000
    1 |   -1.000    1.000    0.999    0.000      nan    0.000
    2 |   -0.998    0.999    1.000    0.000      nan    0.000
    3 |   -0.000    0.000    0.000    1.000      nan    0.000
    4 |      nan      nan      nan      nan      nan      nan
    5 |   -0.000    0.000    0.000    0.000      nan    1.000

Found 1 parameters without influence on the fit model:

  no. |                           id
    4 |               magnetic theta

It is recommended to remove these parameters from the fit or to change the start parameters.

Using 0 equality constraint(s) on parameter(s):

and 0 inequality constraint(s).

Total cost = 2.938e-02

cost for each FitMeasurement is:

  no. |                           id |                cost |                   %
    0 |                              |           2.938e-02 |             100.000

Fit instance with id "" finished.

-------------------------------------------------------------------------------------------
 NEXUS WARNING in Fit
 warning: Fit model does not depend on 1 fit parameters.
-------------------------------------------------------------------------------------------
../../_images/tutorial_optimization_nb_fit_errors_2_1.png
[3]:
inverseHessian = fit.inverse_hessian

covariance_matrix = fit.covariance_matrix

correlation_matrix = fit.correlation_matrix

errors = np.array(fit.fit_parameter_errors)
[4]:
print(correlation_matrix)

print(errors)
[[ 1.00000000e+00 -9.99871493e-01 -9.98462302e-01 -3.68900608e-04
              nan -1.73984066e-04]
 [-9.99871493e-01  1.00000000e+00  9.99220294e-01  3.73622906e-04
              nan  1.79817216e-04]
 [-9.98462302e-01  9.99220294e-01  1.00000000e+00  3.84967085e-04
              nan  1.93413874e-04]
 [-3.68900608e-04  3.73622906e-04  3.84967085e-04  1.00000000e+00
              nan  1.93661073e-04]
 [            nan             nan             nan             nan
              nan             nan]
 [-1.73984066e-04  1.79817216e-04  1.93413874e-04  1.93661073e-04
              nan  1.00000000e+00]]
[[3.06098605e+02]
 [3.31395161e+02]
 [1.40516221e+03]
 [9.72018605e-03]
 [0.00000000e+00]
 [2.65476559e-03]]