I am trying to fit a two-component Gaussian fit:
mu0 = sum(velo_peak * spec_peak) / sum(spec_peak)
sigma = np.sqrt(sum(spec_peak * (velo_peak - mu0)**2) / sum(spec_peak))
def Gauss(velo_peak, a, mu0, sigma):
res = a * np.exp(-(velo_peak - mu0)**2 / (2 * sigma**2))
return res
p0 = [max(spec_peak) - RMS, mu0, sigma] # a = max(spec_peak)
popt,pcov = curve_fit(Gauss, velo_peak, spec_peak, p0,maxfev=10000, bounds=((0, 0, +np.inf, +np.inf), (0, 0, +np.inf, +np.inf)))
#____________________two component gaussian fit_______________________#
def double_gaussian(velo_peak,a1, mu1, sigma1, a2, mu2, sigma2):
res_two = a1 * np.exp(-(velo_peak - mu1)**2/(2 * sigma1**2)) \
+ a2 * np.exp(-(velo_peak - mu2)**2/(2 * sigma2**2))
return res_two
##_____________________Initial guess values__________________________##
sigma1 = 0.7 * sigma
sigma2 = 0.7 * sigma
mu1 = mu0 + sigma
mu2 = mu0 - sigma
a1 = 3
a2 = 1
guess = [a1, mu1, sigma1, a2, mu2, sigma2]
popt_2,pcov_2 = curve_fit(double_gaussian, velo_peak, spec_peak, guess,maxfev=10000, bounds=((0, 0, +np.inf, +np.inf), (0, 0, +np.inf, +np.inf)))
But I am getting a negative part which I want to avoid but I don't know how to implement the bounds correctly as I didn't understand well the documentation.
I am getting an error of:
ValueError: Inconsistent shapes between bounds and `x0`.
Can anyone guide me on how to use the bounds correctly?
It's expecting "2-tuple of array_like, optional"
so that looks like:
((lower_bound0, lower_bound1, ..., lower_boundn), (upper_bound0, upper_bound1, ..., upper_boundn))
Seems to me if you want to avoid negative values then in the double gaussian you'd want to constrain a1
and a2
to be positive.
Following your guess
:
[a1, mu1, sigma1, a2, mu2, sigma2]
That would be:
... bounds=[(0, -np.inf, -np.inf, 0, -np.inf, -np.inf), (np.inf, np.inf, np.inf, np.inf, np.inf, np.inf)], ...
Demo:
import matplotlib.pyplot as plt
def double_gaussian(velo_peak,a1, mu1, sigma1, a2, mu2, sigma2):
res_two = a1 * np.exp(-(velo_peak - mu1)**2/(2 * sigma1**2)) \
+ a2 * np.exp(-(velo_peak - mu2)**2/(2 * sigma2**2))
return res_two
x = np.linspace(0, 10, 1000)
y = double_gaussian(x, 1, 3, 1, 1, 7, 0.5) + 0.4*(np.random.random(x.shape) - 0.5)
popt, _ = curve_fit(double_gaussian, x, y, bounds=[(0, -np.inf, -np.inf, 0, -np.inf, -np.inf), (np.inf, np.inf, np.inf, np.inf, np.inf, np.inf)])
plt.plot(x, y)
plt.plot(x, double_gaussian(x, *popt))