6

I have data whic would fit to an exponential function with a constant.

$$y=a\cdot\exp(b\cdot t) + c.$$

Now I can solve an exponential without a constant using least square by taking log of y and making the whole equation linear. Is it possible to use least square to solve it with a constant too ( i can't seem to convert the above to linear form, maybe i am missing something here) or do I have to use a non linear fitting function like nlm in R ?

M. Winter
  • 29,928
silencer
  • 163

6 Answers6

10

A direct method of fitting (no guessed initial values required, no iterative process) is shown below. For the theory, see the paper (pp.16-17) : https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

enter image description here

JJacquelin
  • 66,221
  • 3
  • 37
  • 87
  • Fantastic. Unfortunately, I don't read french. Can you point me to the page on which the integral equation is stated that is used to perform the transformation? Would love to learn more. – muaddib Jun 24 '15 at 14:17
  • 1
    The integral equation is (6) page 16 : $$y-y_1=-ac(x-x_1)+c\int_{x_1}^x y(u)du$$ Presently, there is no available translation for the whole paper. – JJacquelin Jun 24 '15 at 15:28
  • I am always impressed by your approaches ! – Claude Leibovici Jun 25 '15 at 13:34
  • 1
    Here's a numpy implementation of the above. Behaves a bit weird sometimes; no guarantees that it is accurate. https://hastebin.com/yuxoxotipo.py – Mingwei Samuel Jul 24 '17 at 22:56
  • 1
    @JJacquelin This is very nice. I've started using the approach you provide in your paper(s) in a few places in my work. I've opened up https://github.com/scipy/scipy/pull/9158. I hope the content (and citation) is to your liking. – Mad Physicist Aug 20 '18 at 19:24
  • @ Mad Physicist Thank you for your interest in this method. By the way, there in a very important point at the beginning of the numerical calculus : The data must be ranked in increasing order of the $x_k$. Sometimes I receive complaints that the calculus failed and this was due to not correctly ranked data. – JJacquelin Aug 20 '18 at 20:18
  • @JJacquelin. The PR was closed, but I was advised to create my own package instead, which I'm working on. The ranking was not an issue. I am currently working on details like getting the solution to work along a single dimension of a multi-dimensional array and coming up with a reasonably rigorous testing procedure. By the way, the spaces between the @ and within my name prevented the message from coming to my inbox. – Mad Physicist Sep 20 '18 at 04:16
  • One of the end goals will be to use these routines to supply initial guesses to lmfit. My reading French is fairly decent, especially since most math vocabulary is shared anyway, but my spoken and written French are both atrocious at best. Do you mind if I translate your paper into English and add it to the documentation of my package (with full and unambiguous attribution of course?) I will show you anything I write before publishing it if you are OK with it. – Mad Physicist Sep 20 '18 at 04:17
  • @Mad Physicist : Of course, it would be great that a translation be made. I didn't take the time to do it. Of course, I agree you proposal. Thanks you very much. – JJacquelin Sep 20 '18 at 07:18
  • @JJacquelin. There are some errata that I am compiling as I translate the paper and implement it in code. I feel like I should discuss them with you before publishing anything. I am not sure what the proper mode of contact for something like that would be. Do you have any ideas? I'd be happy to share my email with you if you believe that to be warranted. – Mad Physicist Sep 27 '18 at 19:56
  • @Mad Physicist. I agree that we have to share our emails. But I don't known how to send an email without making it public. I will think about this. I am about to leave to a trip. I will not be available during about two weeks. – JJacquelin Sep 28 '18 at 07:07
  • @Mad Physicist. Hi! I cannot find the mailmap file in githut. – JJacquelin Oct 18 '18 at 09:14
  • Now I have an account on GitHub, with user name JJacquelin. Can you contact me through personal messaging service ? – JJacquelin Oct 18 '18 at 09:35
  • I wonder if this solution can be constrained in a way such that a = -b and b < 0? My data is an offsetted exponential decay, like y=a*(1-e^ct). I was hoping to estimate the limit for t->inf by fitting the exponential curve with a few early values, but with a bit of noise overlayed (white + granular) the results are swamped by errors which result in exponential growth (b > 0)...Otherwise thank you for providing this Information. I hope it's not already mentioned in the paper, unfortunately my french is not fluent enough to unterstand it. – antipattern Mar 04 '22 at 10:37
  • Also @Mad Physicist is there any link to the translated paper, I've tried the github link you provided but was not able to pinpoint it there. – antipattern Mar 04 '22 at 10:42
  • 1
    @antipattern. You can check https://scikit-guess.readthedocs.io/en/latest/appendices/reei/translation.html for an incomplete translation. I'm still working on it. The scikit also has a bunch of sample implementations of the equations in the paper. – Mad Physicist Mar 04 '22 at 15:47
  • @Mad Physicist : I take the opportunity to warmly greet Mad Physicist who did an excellent job of translation. – JJacquelin Mar 04 '22 at 18:50
  • @antipattern : See below the algorithm slightly modified and addapted to the case y=a*(1-e^ct). – JJacquelin Mar 04 '22 at 18:52
  • @JJacquelin. Thank you. And my apologies for falling off the map for so long. If you have some time, I'd like to email you a question about one of the datasets in the paper that I got stuck on – Mad Physicist Mar 05 '22 at 01:21
  • @Mad Physicist. Hi! how are you ? Of course I have always time to read questions. Answering is something else. Hoping that I could. – JJacquelin Mar 05 '22 at 08:58
4

The problem is intrinsically nonlinear since you want to minimize $$SSQ(a,b,c)=\sum_{i=1}^N\big(ae^{bt_i}+c-y_i\big)^2$$ and the nonlinear regression will require good (or at least reasonable and consistent) estimates for the three parameters.

But, suppose that you assign a value to $b$; then defining $z_i=e^{bt_i}$ the problem turns to be linear $(y=az+c)$ and a linear regression will give the value of $a,c$ for the given $b$ as well as the sum of the squares. Try a few values of $b$ untill you see a minimum of $SSQ(b)$. For this approximate value of $b$, you had from the linear regression the corresponding $a$ and $c$ and you are ready to go with the nonlinear regression.

Another approach could be : assume a value of $c$ and rewrite the model as $$y-c=a e^{bt}$$ $$\log(y-c)=\alpha + bt$$ which means that defining $w_i=\log(y_i-c)$, the model is just $z=\alpha + bt$ and a linear regression will give $\alpha$ and $b$. From these, recompute $y_i^*=c+e^{ \alpha + bt_i}$ and the corresponding some of squares $SSQ(c)$. Again, trying different values of $c$ will show a minimum and for the best value of $c$, you know the corresponding $b$ and $a=e^{\alpha}$ and you are ready to go with the nonlinear regression.

It is sure that there is one phase with trial and error but it is very fast.

Edit

You can even have an immediate estimate of parameter $b$ if you take three equally spaced points $t_1$, $t_2$, $t_3$ such that $t_2=\frac{t_1+t_3}2$ and, the corresponding $y_i$'s. After simplification $$\frac{ y_3-y_2}{ y_3-y_1}=\frac{1}{1+e^{\frac{1}{2} b (t_1-t_3)}}$$ from which you get the estimate of $b$ $$b=\frac{2}{t_1-t_3} \log \left(\frac{y_1-y_2}{y_2-y_3}\right)$$ from which $$a=\frac{y_1-y_3}{e^{b t_1}-e^{b t_3}}$$ $$c=y_1-ae^{bt_1}$$ Using the data given in page 18 of JJacquelin's book, let us take (approximate values for the $x$'s) the three points $(-1,0.418)$, $(0,0.911)$, $(1,3.544)$. This immediately gives $b\approx 1.675$, $a\approx 0.606$, $c\approx 0.304$ which are extremely close to the end results by JJacquelin in his book for this specific example.

Having these estimates, just run the nonlinear regression.

This was a trick proposed by Yves Daoust here

1

This isn't another answer to silencer's question. This is a comment which cannot be edited in the comments section.

In fact this is an answer to antipattern's comment and question : I wonder if this solution can be constrained in a way such that a = -b and b < 0? My data is an offsetted exponential decay, like y=a*(1-e^ct).

The end of the calculus is slightly simplified :

enter image description here

A numerical example :

enter image description here

Note : If a specific criteria of fitting (MSE or MSAE or MSRE or etc.) is specified one have to use a non-linear regression method (iterative). Then the approximate values of parameters obtained with the very simple above method can be used as good starting values instead of "guessed" values.

JJacquelin
  • 66,221
  • 3
  • 37
  • 87
0

You can solve the problem by regressing over the derivate (difference) of the data. If you formulate the problem as having to solve for $a, b, c$ in

$$y=b\cdot e^{ax} + c$$

By taking the derivative you get:

\begin{align} \frac{dy}{dx} &= ab\cdot e^{ax} \\ \log\left(\frac{dy}{dx}\right) &= \log(ab\cdot e^{ax}) \\ &= \log(ab) + \log(e^{ax}) \\ &= \log(ab) + ax \end{align}

You can then fit a linear model of the form $u = s x + t$ where $u=\log\left(\frac{dy}{dx}\right)$, $s=a$, and $t=\log(ab)$. After solving you can obtain $a = s$ and $b = \frac{e^t}{a}$. And finally, you can obtain c by subtracting the reconstructed model from the original data.

Note: this whole exercise will require you to sort your data set by $x$ values.

Here is python code to accomplish the task:

def regress_exponential_with_offset(x, y):
    # sort values
    ind = np.argsort(x)
    x = x[ind]
    y = y[ind]

    # decaying exponentials need special treatment
    # since we can't take the log of negative numbers.
    neg = -1 if y[0] > y[-1] else 1
    dx = np.diff(x)
    dy = np.diff(y)
    dy_dx = dy / dx

    # filter any remaining negative numbers.
    v = x[:-1]
    u = neg * dy_dx
    ind = np.where(u > 0)[0]
    v = v[ind]
    u = u[ind]

    # perform regression
    u = np.log(u)
    s, t = np.polyfit(v, u, 1)
    a = s
    b = neg * np.exp(t) / a
    yy = np.exp(a * x) * b
    c = np.median(y - yy)
    return a, b, c
gilsho
  • 9
  • 1
    I am sorry but this is extremely dangerous to do (except if the data are strictly perfect). – Claude Leibovici Jan 15 '19 at 10:48
  • @ClaudeLeibovici Can you please elaborate? I've used this technique successfully on pretty noisy data. – gilsho Jan 16 '19 at 13:53
  • I did not want to answer as soon as I saw your comment and, thinking, I shall not answer NOW but I am ready to have as long discussions as required on this topic with you (chat room, e-mails, ...) if you accept to first ask the question "Is this correct or not ? at Cross Validated Stack Exchange. Cheers. – Claude Leibovici Jan 17 '19 at 02:15
  • 1
    @glisho. The idea to use the derivative of a non linear function to transform a non linear into a linear regression is not new. This is a good idea but with important drawbacks in practice, due to hazard in numerical calculus . Such method was described in the paper referenced below. But this is an very uncertain method if the data are scattered and/or noisy as shown in this paper. I fully agree with the comment of Claude Leibovici. In fact it is much better to use antiderivative than derivative because the numerical integration is much accurate and stable than numerical differentiation. – JJacquelin Jan 17 '19 at 05:30
  • For example, the use of integral equation (but not differential equation) in case of $y=a \exp(bx)+c\quad$ is described pages 15-18 in the paper : https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales – JJacquelin Jan 17 '19 at 06:56
  • @JJacquelin Thank you for detailed response! Is there a formal argument as to why antiderivative methods are more stable than numerical ones? I'd be curious to know. Unfortunately my french is a little rusty so I wasn't able to determine if that is mentioned in your paper. – gilsho Feb 03 '19 at 19:29
  • @glisho. If we compare regressions using antiderivative to regression using derivative, the numerical integration "smooths" the scattered data while the numerical differentiation increases the scatter. If we compare to usual methods requiring iterative calculus starting from initial guessed values of the parameters, the regression with antiderivative doesn't require initial values. As a consequence it is more robust in cases of difficulty to guess the initial values. The regression with antiderivative can be used to obtain good initial values to start a classical iterative regression process. – JJacquelin Feb 03 '19 at 23:26
  • @JJacquelin Thank you for clarifying – gilsho Feb 06 '19 at 07:32
0

Why not create a new variable z = y - c/ Then fit for z = a.e ^bt

If "b" is the problem, define T = bt and then z'= a.e^T. Then if a is the problem, z'' = z'/a and z'' = e^T.

Fit z'' and then unwind the changes.

Rob
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. – Community Dec 14 '21 at 20:41
-1

I tried to deduce the formula for 'b' in case of equdistant ti-s, but I have got a different one. There is a reciprocal under the logarithm in my result. (Please check it. Maybe I am not right.)

Sorry. I see ... The t3-t1 minus sign makes the log content right.