Fitting, Uncertainties and Residual Diagnostics
Scientific Python in GX-TXT · Module 10 · Lesson 58
We estimate the amplitude and decay constant of the two synthetic series, using the declared uncertainties. We compare estimates with generation parameters, known because it is a simulation.
Objectives
- Record a callback of the exponential model.
- Use the
sigmauncertainties in the fit. - Check convergence before posting the result.
- Read parameters, standard errors and residuals with the correct units.
Execution
After the generation of lesson 56, import final_fit.py, save it and execute it in the corso_python Session.

import numpy as np
from gxtxt import gxtxt_api_call, gxtxt_api_require, gxtxt_callback_register, gxtxt_callback_release
for api in ("NUMERIC_API_v1@1:math.numeric+script_callback_functions", "CANONICAL_DATASET_API_v2@2", "DATASET_PUBLICATION_API_v2@2"):
gxtxt_api_require(api)
def model(parameters, times):
p = np.asarray(parameters).reshape(-1)
return p[0] * np.exp(-p[1] * np.asarray(times).reshape(-1))
handle = gxtxt_callback_register(model)
try:
for display_name, expected in (("a", 0.5), ("b", 0.8)):
source = "corso_finale_" + display_name
data_values = gxtxt_api_call("CANONICAL_DATASET_API_v2", "read_range", [source, ["tempo", "segnale", "sigma"], 0, 21])
t, y, sigma = [np.asarray(data_values[v]["values"]).reshape(-1) for v in ("tempo", "segnale", "sigma")]
fit = gxtxt_api_call("NUMERIC_API_v1", "nonlinear_fit", [t, y, handle, [2.5, 0.6], {"sigma": sigma}])
if not fit["converged"]:
raise RuntimeError("The fit does not converge: " + source)
p = np.asarray(fit["parameters"]).reshape(-1)
predicted = model(p, t)
residuals = y - predicted
print(source, "convergenza:", fit["converged"])
print("Ampiezza (mV), k (1/s):", p, "k generation:", expected)
print("Standard errors:", np.asarray(fit["standard_errors"]).reshape(-1))
print("RMS residues (mV):", np.sqrt(np.mean(residuals**2)))
request = {"dataset_id": "corso_finale_fit_" + display_name, "contract": "table.generic", "storage": "csv", "replace_owned": True,
"x_values": t, "y_values": predicted,
"x_variable": {"variable_id": "tempo", "name": "tempo", "unit": "s"},
"y_variable": {"variable_id": "fit", "name": "fit", "unit": "mV"},
"source_dataset_ids": [source], "provenance": "Exponential Fit of Synthetic Data"}
gxtxt_api_call("DATASET_PUBLICATION_API_v2", "publish_series", [request])
finally:
gxtxt_callback_release(handle)
The callback returns a prediction for each time. For each condition we read the 21 points and uncertainties, we require the fit with sigma and we control converged. If the solver does not converge, the script raises an exception instead of presenting the result as successful. The finally releases the callback. The expected curves are published separately as corso_finale_fit_a and corso_finale_fit_b, with reference to synthetic sources.

Both fits converge in this run. For A we get about \(A=2{.}984\,\mathrm{mV}\), \(k=0{.}498\,\mathrm{s}^{-1}\); for B about \(A=3{.}015\,\mathrm{mV}\), \(k=0{.}793\,\mathrm{s}^{-1}\). The generation values were 0,5 and 0.8 s⁻¹ respectively. Synthetic noise explains why estimates do not coincide exactly with the parameters set.
The standard errors reported by the provider depend on the model, weights and calculation assumptions; they are not automatically confidence intervals. The RMS of residuals is about 0.035 mV for A and 0.043 mV for B. Examine also the distribution and performance of the residuals is useful: a single metric does not guarantee that the model adequately describes the data.
Exercise
- Which parameters were estimated, and what are their units?
- Why should estimates not coincide exactly with the values of generation?
- What does the script do if
convergedis fake?
Worked solution
- Amplitude in mV and decay constant in s−1.
- Synthetic noise was added to the ideal data.
- It raises an exception and does not publish that fit as a successful result.