Best Regression Fit
|
11-04-2017, 02:08 PM
Post: #1
|
|||
|
|||
Best Regression Fit
The program BESTFIT compares a set of regressions to determine a best fit. This simulates a feature presented on the Hewlett Packard HP 48S, HP 48G, HP 49G, and HP 50g. BESTFIT compares the correlations of the following four regression models:
1. Linear: y = a * x + b 2. Logarithmic: y = a * ln x + b 3. Exponential: y = b * a^x (y = b * e^(ln a * x)) 4. Power: y = b * x^a The output is a two element list: a string of the best fit equation and its corresponding correlation. HP Prime Program: BESTFIT Code:
BESTFIT2: An Extended Version Version 2 adds the following regressions: 5. Inverse: y = b + a/x 6. Simple Logistic: y = 1/(b + a*e^(-x)) 7. Simple Quadratic: y = b + a*x^2 8. Square Root: y = √(a*x + b) HP Prime Program: BESTFIT2 Code:
Link: http://edspi31415.blogspot.com/2017/11/h...n-fit.html |
|||
11-04-2017, 03:51 PM
Post: #2
|
|||
|
|||
RE: Best Regression Fit
thanks Eddie,
good program. It works better in Home: I tried in CAS with the second list with a series of LN() and it return "Error: Bad argument type", the same input is good in Home, however. Please, control it. Salvo ∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib |
|||
12-17-2017, 04:17 PM
Post: #3
|
|||
|
|||
RE: Best Regression Fit
Very interesting program. I missed that feature on the HP Prime, till now.
I´ve done a little modification for better input data. Code: EXPORT BESTFIT2() |
|||
12-27-2017, 03:31 PM
(This post was last modified: 12-29-2017 03:52 AM by Namir.)
Post: #4
|
|||
|
|||
RE: Best Regression Fit
Here is my version of BESTFIT. It iterates over many powers for Y and X, including powers with fractions.
Code: EXPORT BESTFIT(Mat, IdxX, IdxY) The function requires the name of a data matrix and the indices of the columns that select X and Y data. The function returns: 1) Best Rsqr value. 2) Best power for Y. Please note that a 0 means ln(x). 3) Best power for X. Please note that a 0 means ln(x). 5) Slope for best curve. 6) Intercept of best curve. The fitted models are in the general form: Y^ypwr = intercept + slope*X^xpwr When ypwr or xpwr is 0, the term represents a natural logarithm. You can alter the range for the loops, the scales for X and Y as you see fit. Just make sure you don't try to raise negative values to negative powers or powers with fractions. If you have date the best thing to do is to scale their values to be in the range of 1 to 2, using: x(i) = 1 + (x(i) - min(x))/(max(x) - min(x)) y(i) = 1 + (y(i) - min(y))/(max(y) - min(y)) Such a scaling ensures that all transformations will not generate a run time errors. Enjoy Namir |
|||
12-27-2017, 09:14 PM
(This post was last modified: 12-29-2017 03:53 AM by Namir.)
Post: #5
|
|||
|
|||
RE: Best Regression Fit version 2
The next version of the best fit, BESTFIT2, tests the power model between X and Y first, before testing all other transformations. If you have negative values in either X or Y the do not use this function:
Code: EXPORT BESTFIT2(Mat, IdxX, IdxY) I wrote this modified version because while testing Y=X^2, I was getting 1/Y^2 = 1/X^4 as the best model. While the answer is theoretically correct, it is not in the desired form. Namir |
|||
12-28-2017, 12:54 PM
(This post was last modified: 12-29-2017 03:53 AM by Namir.)
Post: #6
|
|||
|
|||
RE: Best Regression Fit
Here is a third version, BESTFIT3, that maps the X and Y data to values in the range of (1, 2), making all transformations possible:
The function returns: 1) Best Rsqr value. 2) Power of best Y transformation (0 means ln(y)). 3) Power of best X transformation (0 means ln(x)). 4) Best slope. 5) Best intercept. 6) Minimum X value. 7) Maximum X value. 8) Minimum Y value. 9) Maximum Y value. Make sure you use the powers, slope, intercept, minima, and maxima in estimating Yhat values: Yhat' = (slope * ((X-xmin)/(xmax-xmin)+1)^xpwr + intercept)^(1/ypwr) Yhat = ymin + (Yhat'-1)*(ymax-ymin) if xpwr=0 and ywpr!=0 use: Yhat' = (slope * ln((X-xmin)/(xmax-xmin)+1) + intercept)^(1/ypwr) Yhat = ymin + (Yhat'-1)*(ymax-ymin) if xpwr!=0 and ywpr=0 use: Yhat' = exp(slope * ((X-xmin)/(xmax-xmin)+1)^xpwr + intercept) Yhat = ymin + (Yhat'-1)*(ymax-ymin) if both xpwr and ypwr are zero, use: Yhat' = exp(slope * ln((X-xmin)/(xmax-xmin)+1) + intercept) Yhat = ymin + (Yhat'-1)*(ymax-ymin) Use ln() functions when xpwr s 0 and use exp() when the ypwr is 0. Code: EXPORT BESTFIT3(Mat, IdxX, IdxY) |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)