F.Davis
F.Davis

Reputation: 41

Fit a Gaussian which must use the provided mean in python

Is there anyway to fit a Gaussian where I'm not just providing a suggestion or best guess for the mean, but it MUST take it and adjust the other parameters for this to work? I know this won't won't give me the best fit for the data, but that's not essential.

Upvotes: 1

Views: 775

Answers (2)

M Newville
M Newville

Reputation: 7862

You might consider using lmfit for this (disclosure: I am lead author), as it supports fixing or adding bounds to any parameter. Depending on your data, doing such a fit might be as simple as:

import numpy as np
import matplotlib.pyplot as plt
from lmfit.models import GaussianModel

# get x, y data from some source
data = np.loadtxt('somedatafile.dat')
xdata = data[:, 0]
ydata = data[:, 1]

# create Gaussian, set initial parameter values
model = GaussianModel()
parameters = model.make_params(amplitude=10, center=12, sigma=3)

# tell the center parameter to not vary in the fit 
parameters['center'].vary = False

# run fit
result = model.fit(ydata, parameters, x=xdata)

# print fit statistics, parameter values and uncertainties 
print(result.fit_report())

# make a simple plot of data + fit, with residual
result.plot()
plt.show()

There are options for other peak shapes and controlling parameter values and ranges.

Upvotes: 0

James Phillips
James Phillips

Reputation: 4657

@BenedictWilkinsAI suggestion is the simplest way, write the equation with the fixed value replacing the mean. If however you would like to use a programmatic solution, here is a graphical Python fitter which allows both normal (pun intended) and fixed-mean Gaussian peak equation fitting. When a fixed mean parameter value of 9.0 is used, the fit is visibly worse - as expected. Also, curve_fit() gives a warning that it cannot calculate the covariance matrix, since the mean parameter cannot vary.

plot1

plot2

import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

xData = numpy.array([5.357, 5.797, 5.936, 6.161, 6.697, 6.731, 6.775, 8.442, 9.861])
yData = numpy.array([0.376, 0.874, 1.049, 1.327, 2.054, 2.077, 2.138, 4.744, 7.104])

# normally fitted mean is 10.67571675
# set this value to None to fit normally, else
# set to the value of the fixed mean
fixedMean = 9.0

def func(x, a, b, c): # Gaussian peak equation
    if fixedMean:
        b = fixedMean
    return a * numpy.exp(-0.5 * numpy.power((x-b) / c, 2.0))


# these are the same as the scipy defaults except for the fixed mean
if fixedMean:
    initialParameters = numpy.array([1.0, fixedMean, 1.0])
else:
    initialParameters = numpy.array([1.0, 1.0, 1.0])

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, p0=initialParameters)

modelPredictions = func(xData, *fittedParameters) 

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    # first the raw data as a scatter plot
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = func(xModel, *fittedParameters)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)

Upvotes: 1

Related Questions