I’m fitting an experimental spectrum to a theoretical expectation using LeastSq from SciPy. There are of course errors associated with the experimental values. How can I feed these to the LeastSq or do I need a different routine? I find nothing in the documentation.
I’m fitting an experimental spectrum to a theoretical expectation using LeastSq from SciPy. There
Share
The scipy.optimize.leastsq function does not have a built-in way to incorporate weights. However, the scipy.optimize.curve_fit function does have a
sigmaparameter which can be used to indicate the variance of each y-data point.curve_fituses1.0/sigmaas the weight, wheresigmacan be an array of lengthN, (the same length asydata).So somehow you have to surmise the variance of each ydata point based on the size of the error bar and use that to determine
sigma.For example, if you declare that half the length of the error bar represents 1 standard deviation, then the variance (what
curve_fitcallssigma) would be the square of the standard deviation.Reference: