Note
Go to the end to download the full example code.
Constrained fit#
Gaussian fit to histogram data where the prefactor is frozen based on the histogram normalization.

Gaussian
χ²: 98.28 / 84 dof
amplitude: 10101 (frozen)
mu: -0.0029±0.0034
sigma: 1.0026±0.0024 (min=0.0)
<matplotlib.legend.Legend object at 0x7f9d37edecc0>
import numpy as np
from aptapy.hist import Histogram1d
from aptapy.models import Gaussian
from aptapy.plotting import plt
hist = Histogram1d(np.linspace(-5., 5., 100), label="Random data", xlabel="z")
hist.fill(np.random.default_rng().normal(size=100000))
hist.plot()
model = Gaussian()
# Fix the amplitude. This is a generally useful technique, as you should
# never fit the normalization of a histogram.
model.amplitude.freeze(hist.area() / model.integral(-5., 5.))
model.fit(hist)
print(model)
model.plot(fit_output=True)
plt.legend()
Total running time of the script: (0 minutes 0.295 seconds)