I've read the paper Least square fitting of a Gaussian function to a histogram by Leo Zhou on how to perform a Least Square Fitting of a gaussian function to a histogram.
The Gaussian function used to fit the data is: $$f(y)=A\exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)$$
However, the method described in the paper doesn't work if the dataset has a vertical offset (i.e. all the points are shifted on the Y-axis by some amount $K$).
I was wondering how to perform LSF (or any other kind of fitting) to estimate the parameters $A$, $\mu$, $\sigma$ and $K$ of the function
$$f(y)=A\exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)+K$$
(without prior knowledge of the parameter $K$, of course).
The usual methods of non-linear regression involve iterative process starting from guessed values of the parameters.
There is a straight forward method (not iterative, no need for guessed values) which general principle is explain in the paper : https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales (in French, presently no translation available).
The application to the "y-shifted gaussian" case is shown below :
$$y=h+c\: e^{-\frac{(x-a)^2}{b}}$$ Rank the data in increasing order of $x_k$
$(x_1\:,\:y_1)$ , $(x_2\:,\:y_2)$ , … , $(x_k\:,\:y_k)$ , … , $(x_n\:,\:y_n)$
$S_1=0$
$S_k=S_{k-1}+\frac{1}{2}(y_k+y_{k-1})(x_k-x_{k-1})$
$T_1=0$
$T_k=T_{k-1}+\frac{1}{2}(x_k y_k+x_{k-1} y_{k-1})(x_k-x_{k-1})$
Compute matrix $(M)=$ $$(M)= \begin{pmatrix} \displaystyle\sum _{k=1}^n S_k^2& \displaystyle\sum _{k=1}^n S_kT_k & \displaystyle\sum _{k=1}^n S_k(x_k^2-x_1^2)& \displaystyle \sum _{k=1}^n S_k(x_k-x_1) \\ \displaystyle\sum _{k=1}^n S_kT_k& \displaystyle\sum _{k=1}^n T_k^2 & \displaystyle\sum _{k=1}^n T_k(x_k^2-x_1^2)& \displaystyle\sum _{k=1}^n T_k (x_k-x_1)\\ \displaystyle\sum _{k=1}^n S_k(x_k^2-x_1^2) &\displaystyle\sum _{k=1}^n T_k(x_k^2-x_1^2) &\displaystyle\sum _{k=1}^n (x_k^2-x_1^2)^2& \displaystyle\sum _{k=1}^n (x_k^2-x_1^2)(x_k-x_1)\\ \displaystyle\sum _{k=1}^n S_k(x_k-x_1) &\displaystyle\sum _{k=1}^n T_k(x_k-x_1) & \displaystyle\sum _{k=1}^n (x_k^2-x_1^2)(x_k-x_1) &\displaystyle\sum _{k=1}^n (x_k-x_1)^2&\\ \end{pmatrix}$$ and vector $(V)=$ $$(V)=\begin{pmatrix} \displaystyle\sum _{k=1}^n S_k(y_k-y_1) \\ \displaystyle\sum _{k=1}^n T_k(y_k-y_1) \\ \displaystyle\sum _{k=1}^n (x_k^2-x_1^2)(y_k-y_1) \\ \displaystyle\sum _{k=1}^n(x_k-x_1)(y_k-y_1) \displaystyle\end{pmatrix} $$ Then, compute $A,B$ from : $$ \begin{pmatrix} A\\ B\\ C\\ D\\ \end{pmatrix}=(M)^{-1}(V)$$
The approximates of $a,b$ are :
$$a=-\frac{A}{B}$$ $$b=-\frac{2}{B}$$
Then, compute $t_k$ : $$t_k=e^{-\frac{(x_k-a)^2}{b}}$$ and compute the approximates of $c$ and $h$ : $$\begin{pmatrix} c\\ h \end{pmatrix}= \begin{pmatrix} \displaystyle\sum _{k=1}^n t_k^2&\displaystyle \sum _{k=1}^n t_k\\ \displaystyle\sum _{k=1}^n t_k& n \end{pmatrix}^{-1} \begin{pmatrix} \displaystyle\sum _{k=1}^n t_k y_k \\ \displaystyle\sum _{k=1}^n y_k \end{pmatrix} $$ The approximate of $y_k$ is $Y_k=h+c\:e^{-\frac{(x_k-a)^2}{b}}$
A numerical example is detailed below: