HP Forums
Gompertz curve - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Gompertz curve (/thread-3509.html)



Gompertz curve - salvomic - 03-30-2015 03:09 PM

hi all,
using a very useful tutorial in "Prime tutorial" (tnx Eddie!) I wrote a little program to get the Gompertz Curve (see here):

Code:

EXPORT Gompertz_curve()
BEGIN
LOCAL cr, cg, cb, I;
LOCAL control:=1;

REPEAT
INPUT({A, B, C},
"Gompertz ae^(-be^(-ct))", {"asymptote A","displacement B","growth rate C" }, {"Asymptote", "Displacement along x", "Growth rate (y scaling)" }, {0,1,1},
{A, B, C});
CASE
IF (B <= 0) THEN MSGBOX ("B must be > 0"); control:=1; B:=1; END;
IF (C <= 0) THEN MSGBOX ("C must be > 0"); control:=1; C:=1; END;
DEFAULT control:=0;
END; // case
UNTIL control < 1;

// Colors
CHOOSE(I, "Choose a Color",
"Red", "Blue", "Orange", "Green");
cr := {255, 0, 255, 0};
cg := {0, 0, 127, 255};
cb := {0, 255, 0, 0};

STARTAPP("Advanced Graphing");
V1 := "Y=A*e^(-B*e^(-C*X))";
V1(COLOR) := RGB(cr(I), cg(I), cb(I));
CHECK(1);
// Plot View
STARTVIEW(1, 1);
END;

Please, feel free to suggest improvements or criticism Smile


RE: Gompertz curve - salvomic - 03-30-2015 06:36 PM

However, about this curve I need help for the "inverse" problem: to use regression (interpolation) for that formula (Y=A*e^(-B*e^(-C*X))) with Statistic 2var (or otherwise).

I have a series of values for X (t, years) and Y (population):
X: 1,2,3,4,5,6,7,8,9
Y: 146583, 150203, 161475, 169182, 177619, 183054, 189431, 193082, 195687
I would get an estimation for A, B, C. (Then predict future values...)

I set in Symb -> Type: "User Defined" and Fit: "A*e^(-B*e^(-C*X))", but I'm not able to get plot and the values of estimated A, B, C, as I would like to have...
Any hints?


RE: Gompertz curve - Marcio - 03-30-2015 07:44 PM

(03-30-2015 06:36 PM)salvomic Wrote:  However, about this curve I need help for the "inverse" problem: to use regression (interpolation) for that formula (Y=A*e^(-B*e^(-C*X))) with Statistic 2var (or otherwise).

I have a series of values for X (t, years) and Y (population):
X: 1,2,3,4,5,6,7,8,9
Y: 146583, 150203, 161475, 169182, 177619, 183054, 189431, 193082, 195687
I would get an estimation for A, B, C. (Then predict future values...)

I set in Symb -> Type: "User Defined" and Fit: "A*e^(-B*e^(-C*X))", but I'm not able to get plot and the values of estimated A, B, C, as I would like to have...
Any hints?

The Prime can't do that. To use customized functions, you can't put unknowns in there, meaning you have to provide values for A, B and C if you want to see data plotted against an equation. The manual discusses this a little bit.

What you could do is try to adjust the customized function to one of the options the Prime gives you. Try to make them equivalent to each other. I often do that. Quite fun.

Alternatively, if you are in a hurry or REALLY need this, I have a program that will fit data to customized functions. It's not fully integrated to the apps just yet but it will work just fine, meaning it will only work on the terminal environment. Shouldn't take more than 1 second to give the results back, provided you enter good initial values.

BTW, you will have to normalize your data before using any (or most) parameter estimation program or app.

Just like this:
Y:=Y/max(Y). That should only change the value of A, which will have to be multiplied by max(Y) after you get the results back. The values of B and C should and will not be affected by this little trick.

Marcio


RE: Gompertz curve - Han - 03-30-2015 07:50 PM

(03-30-2015 06:36 PM)salvomic Wrote:  However, about this curve I need help for the "inverse" problem: to use regression (interpolation) for that formula (Y=A*e^(-B*e^(-C*X))) with Statistic 2var (or otherwise).

I have a series of values for X (t, years) and Y (population):
X: 1,2,3,4,5,6,7,8,9
Y: 146583, 150203, 161475, 169182, 177619, 183054, 189431, 193082, 195687
I would get an estimation for A, B, C. (Then predict future values...)

I set in Symb -> Type: "User Defined" and Fit: "A*e^(-B*e^(-C*X))", but I'm not able to get plot and the values of estimated A, B, C, as I would like to have...
Any hints?

For this, you could create a system of equations resulting from substituting each pair of X and Y values into the general equation. Then use the generalized Newton's method to determine the least-squares fit. Snorre implemented such a function here:

http://www.hpmuseum.org/forum/thread-455.html#pid24491


RE: Gompertz curve - salvomic - 03-30-2015 07:51 PM

(03-30-2015 07:44 PM)Marcio Wrote:  The Prime can't do that. To use customized functions, you can't put unknowns in there, meaning you have to provide values for A, B and C if you want to see data plotted against an equation. The manual discusses this a little bit.
I thought that, in fact...
I'm trying to put the unknown but no fit, then...
Quote:What you could do is try to adjust the customized function to one of the options the Prime gives you.
the nearest is surely "logistic regression", but Gompertz is a generalized logistic, not the same thing...
Quote:Alternatively, if you are in a hurry, I have a program that fits data to customized functions, but it's not yet fully integrated to the apps, meaning it will only work on the terminal environment.

Marcio

I could try your solution, if you like to share it...
I was founding a solution in the program suite from Namir (MLRX, BestMLR...) but no one seems to be adapt...

thank you


RE: Gompertz curve - salvomic - 03-30-2015 08:08 PM

(03-30-2015 07:50 PM)Han Wrote:  For this, you could create a system of equations resulting from substituting each pair of X and Y values into the general equation. Then use the generalized Newton's method to determine the least-squares fit. Snorre implemented such a function here:

http://www.hpmuseum.org/forum/thread-455.html#pid24491

thank you Han,
I'm reading the post with the Snorre's method...
but Snorre wrote: «Usage: newton( [f1(x1,...,xn), ..., fm(x1,...,xn)], [x1,...,xn], [xinit1,...,xinitn], thf, thd, kmax )
Prints out the current values of |f(x)| and |d|. Returns x=[x1,...,xn].»
Now, in my case, how to put as xinit1..., thf, thd and kmax?

I tried to use also Solve with 3 equations like you suggest, but perhaps isn't the right method...


RE: Gompertz curve - salvomic - 03-30-2015 10:07 PM

(03-30-2015 07:44 PM)Marcio Wrote:  BTW, you will have to normalize your data before using any (or most) parameter estimation program or app.

Just like this:
Y:=Y/max(Y). That should only change the value of A, which will have to be multiplied by max(Y) after you get the results back. The values of B and C should and will not be affected by this little trick.

doing so, I get a logistic as 1.1177/(1+0.6269*e^(-0.1929*X)) and it is very close to the observed data. The asymptote should be about 1.1177 (A)...
This curve seems to fit to my data better than the Gompertz (but a book of mine write that those data are from Gompertz curve...) Smile

manually (empirically) I get for Gompertz A=1.1177, B=0.44, C=.14, but also so, Logistic is a little more close to the data...

Now I can see it in Statistic 2var (also if seems don't fit very well)


RE: Gompertz curve - Marcio - 03-31-2015 01:54 PM

(03-30-2015 10:07 PM)salvomic Wrote:  doing so, I get a logistic as 1.1177/(1+0.6269*e^(-0.1929*X)) and it is very close to the observed data. The asymptote should be about 1.1177 (A)...
This curve seems to fit to my data better than the Gompertz (but a book of mine write that those data are from Gompertz curve...) Smile
manually (empirically) I get for Gompertz A=1.1177, B=0.44, C=.14, but also so, Logistic is a little more close to the data...
Now I can see it in Statistic 2var (also if seems don't fit well, but I now can see who rules A,B,C: my program or ...Statistic? in fact they seem to use the same data, A, B, C ...)

The correct values for A, B and C are 1.151, 0.5191 and 0.1509 respectively. For those, R^2 = 0.9916.

Let me know if you still need the program. I will have to edit it to make it more user-friendly.

And yes, the curves are indeed very similar but only for that region your data covers, outside that area the functions behave differently, that means that the estimates you found for A, B and C using the logistic fit might not be the best for extrapolation.

Marcio


RE: Gompertz curve - salvomic - 03-31-2015 02:04 PM

(03-31-2015 01:54 PM)Marcio Wrote:  The correct values for A, B and C are 1.151, 0.5191 and 0.1509 respectively. For those, R^2 = 0.9916.
thank you. You're right: so the two curves are more close with the range of our data
Quote:Let me know if you still need the program. I will have to edit it to make it more user-friendly.
you're very kind.
Yes, I would like to try it.
We could also make a program with mine and yours functions to treat Gompertz Curve...
Quote:And yes, the curves are indeed very similar but only for that region your data covers, outside of that area the functions behave differently, that means that the estimates you found for A, B and C using the logistic fit might not be the best for extrapolation.

Marcio

yes, indeed.
With those data I can see (in Statistic 2var) they are however enough close also for extrapolation, but generally speaking above the range there is a difference and with Gompertz often the purpose is to predict future values...
In our case Gompertz seems to be below Logistic in the left tail and over it beyond the range of the Y...

However in this case Logistic is always a good approximation.

***
In the image the two curves (orange Gompertz, green Logistic) in Statistics 2var


RE: Gompertz curve - Marcio - 04-01-2015 03:43 AM

SAlvomic

The code was sent to you via PM with basic instructions and everything. I will be publishing the new version of it on the forum as soon as I finish a few details. However, if anyone else is interested in the basic version, I can share it via PM.

Marcio


RE: Gompertz curve - salvomic - 04-01-2015 07:22 AM

(04-01-2015 03:43 AM)Marcio Wrote:  SAlvomic

The code was sent to you via PM with basic instructions and everything. I will be publishing the new version of it on the forum as soon as I finish a few details. However, if anyone else is interested in the basic version, I can share it via PM.

Marcio

very kind of you, thanks!

only a curiosity of mine: why the data to analyze go in C7 and C9?, I mean: do you like 7 and 9 and not 7 and 8? Smile ...


RE: Gompertz curve - Marcio - 04-01-2015 09:55 AM

All the rest was filled up.
You can import data from anywhere, spreadsheet, stat2var app, home variable etc.
Marcio


RE: Gompertz curve - salvomic - 04-01-2015 11:41 AM

(04-01-2015 09:55 AM)Marcio Wrote:  All the rest was filled up.
You can import data from anywhere, spreadsheet, stat2var app, home variable etc.
Marcio

thank you, Marcio,
I customized two or three lines, for the rest the program for me is already almost ok!

I'm looking forward for your general solution, when you'll publish in the Forum.


RE: Gompertz curve - Marcio - 04-01-2015 11:57 AM

(04-01-2015 11:41 AM)salvomic Wrote:  I customized two or three lines, for the rest the program for me is already almost ok!

I'm looking forward for your general solution, when you'll publish in the Forum.

You know, having to enter the equation or function directly in the code does not bother me as much. I will see what I can do about it though.

Truth is that I much prefer dealing with numbers than letters.

Marcio