Showing posts with label logarithmic regression. Show all posts
Showing posts with label logarithmic regression. Show all posts

Saturday, June 22, 2024

Curve Fitting: Adjusting Data Points (feat. HP 14B and HP Prime)

 Curve Fitting: Adjusting Data Points


Graphs are generated with the HP Prime emulator software.



A Curve Fitting Problem


Problem:


We have an investment project, which at the beginning will cost us 50,000 (insert the currency of your choice). Through analysis, the projected cash flows at the beginning of each term are shown here:



Period (beginning of)

Cash Flow

1

-50000

2

-3000

3

9000

4

16000

5

18000

6

21000


What is the projected cash flow at the beginning of period 7?


We could easily use linear regression, but the problem is, the data itself does not easily fit a line. So we can just use another regression model and that’s simple, right? Not quite, since two of the cash flows are negative.


Approach


Most calculators have at least the following curve fitting regressions:


Model

Transformed

X’ =

Y’ =

True B =

Linear

Y = B + M * X

Y = B + M * X

X

Y

B

Exponential

Y = B * exp(M *X)

ln(Y) = ln(B) + M * X

X

ln(Y)

exp(B)

Logarithmic

Y = B + M * ln(X)

Y = B + M * ln(X)

ln(X)

Y

B

Power

Y = B * X^M

ln(Y) = ln(B) + M * ln(X)

ln(X)

ln(Y)

exp(B)


A lot of them rely on a transition of data to use a translated linear regression model. After the parameters slope (M) and y-intercept (B) are calculated, any adjustments are made to find the correct curve fitting parameters.


The statistical calculations operate on real numbers. If we try to fit an exponential, logarithmic, or power fit on data with non-positive numbers, we are going to have a problem because the natural logarithmic function returns non-real numbers in the transition process.


Still the data does not fit a linear regression line as good as the other regression curves. This calls for an adjustment of data. In essence, we are going to subtract out the minimum value of either the x or y data, or both “minus one”. Conceptually:


If min(X) ≤ 0, then:

P = min(X) -1

X_adj = X – P


If min(Y) ≤ 0, then:

Q = min(Y) -1

Y_adj = Y – Q


Why the “minus 1”?


If we only subtract the minimum, the lowest adjust amount would be 0. But ln(0) approaches negative infinity, which most calculators would not work with.


If we subtract the minimum minus one, the lowest adjusted amount would be 1. ln(1) =0. Beautiful!


Example: Adjusting the Raw Data


Raw data:

X

Y

1

-50000

2

-3000

3

9000

4

16000

5

18000

6

21000


min(X) = 1 > 0. No adjustment is needed.

min(Y) = -50000 ≤ 0. An adjustment is needed.


Let Q = min(Y) – 1 = -50000 – 1 = -50001


Adjusted data:


X

Y

Y’ = Y – Q

ln(Y’) (fix 3)

1

-50000

1

0

2

-3000

47001

10.758

3

9000

59001

10.985

4

16000

66001

11.097

5

18000

68001

11.127

6

21000

71001

11.170


Let’s fit the adjusted curve.


Keep in mind that:


Y’ = Y – Q

Y’ = Y - -50001

Y’ = Y + 50001

Y = Y’ – 50001





Fitting the Curve


Determine which one of the four regressions: linear, exponential, logarithmic, or power bets fits the adjusted data points. Predict the actual revenue (Y) for period 7.


Here is the data. We are going to use the data sets X and Y’.



X

Y

Y’ = Y – Q , (Q = -50001)

1

-50000

1

2

-3000

47001

3

9000

59001

4

16000

66001

5

18000

68001

6

21000

71001


Correlation Coefficients:


Model

Correlation (fix 4)

Linear

0.8477

Exponential

0.6772

Logarithmic

0.9520

Power

0.8290


Calculations were made with a HP 14B calculator. The best fit has absolute value of the correlation closest to 1. Correlations near -1 or +1 are better than correlations near 0.


The best fit is the logarithmic fit. Hence the curve fit is:


Y’ ≈ 9618.6378 + 38498.9034 * ln(X)





Remember that Y’ = Y + 50001. Then:


Y + 50001 ≈ 9618.6378 + 38498.9034 * ln(X)

Y ≈ -40382.3262 + 38498.9034 * ln(X)


To predict for X = 7:

Y ≈ -40382.3262 + 38498.9034 * ln(7)

Y ≈ 34533.0807


The beginning of period 7 will have the project cash flow of about $34,533.



Eddie


All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Saturday, March 6, 2021

Fun with the TI-73 Part I: Logarithmic Regression, Percent, Square Root Simplification

 Fun with the TI-73 Part I: Logarithmic Regression, Percent, Square Root Simplification


TI-73 Program:  LNREG - Logarithmic Regression


Introduction:


The program LNREG fits the data to the equation:


y = a * ln x + b


The lists used are:

L_1:  x list  

L_2:  y list


Access L_1 by pressing [ 2nd ] [ STAT ] (LIST), 1.

Access L_2 by pressing [ 2nd ] [ STAT ] (LIST), 2


You can edit lists by pressing [LIST].  


The lower case a and b are from the VARS, Statistics, EQ menu.


Program:


"EWS 2021"

Disp "FIT Y=A LN X +B","USES X=L1,Y=L2:

ln(L_1) → L_1

LinReg(ax+b) L_1,L_2

a → A

b → B

Output(4,1,"A=")

Output(4,5,A)

Output(5,1,"B=")

Output(5,5,B)

Output(6,5,r²)

e^(L_1) → L_1

Pause

Menu("PLOT?","YES",1,"NO",2)

Lbl 1

PlotsOff

FnOff

"A*ln(X)+B"→Y_1

Plot1(xyLine,L_1,L_2,□)

ZoomStat

Lbl 2


Example:


X:  L_1 = {1, 2, 3, 4, 5, 6}

Y:  L_2 = {-1.26, 2.1018, 4.0683, 5.4635, 6.5458, 7.43}


Results:

y = A ln x + B

A = -1.259978557

B = 4.849985251

r^2 ≈ 1


TI-73 Program: PERCENT - Percent Calculations


Introduction:


The program gives the users four options:


1.  Adding tax (plus percent)

2.  Subtracting tax (minus percent)

3.  Percent Change

4.  Percent Total


The percent key divides the amount by 100.  Example:  5% turns to 0.052.


Program:


"EWS 2021"

Menu("PERCENT","TAX+",1,"TAX-",2,"%CHG",3,"%TOT",4)

Lbl 1

Prompt N

Input "X% ",X

N*(1+X%)→Y

Pause Y

Stop

Lbl 2

Prompt N

Input "X% ",X

N*(1-X%)→Y

Pause Y

Stop

Lbl 3

Input "OLD=",O

Input "NEW=",N

(N-O)*100/O→Y

Pause Y

Stop

Lbl 4

Input "PART=",P

Input "WHOLE=",W

100*P/W→Y

Pause Y

Stop


Example:


TAX+:   Add 15% to 19.95

N: 19.95, X%:  15

Result:  22.9425


TAX-:  Subtract 15% from 19.95

N:  19.95, X%:  15

Result:  16.9575


%CHG:  Old (O):  11700, New (N):  15420

Result:  31.79487179


%TOT:  Part (P):  11700, Whole (W):  15420

Result:  75.7548638


TI-73 Program: SQROOT - Simplification Square Root


This program tries to factor √N to A√B.  N, A, and B are integers.


Program:


"EWS 2021"

Disp "√(N)->A√(B)"

Prompt N

iPart(√(N))→X

While X≠0

If fPart(N/X^2)=0

Then

X→A

N/X^2→B

Goto 1

Else

X-1→X

End

End

Lbl 1

Disp "A=",A,"B=",B

Pause


Examples:


N = 88;   √88 = 2√22


N = 1225;  √1225 = 35√1


Notes:


*  There is no ALPHA key for the TI-73 (family).   Text is entered by use of the [ 2nd ] [ MATH ] (TEXT) key sequence.  Letters are selected by using the arrow keys and selecting them with the ENTER key.


*  The underscore character ( _ ) acts like a space.


*  The TEXT has the letters A-Z, _, <, =, >, ≤, ≠, ≥, and the logic functions and, or.  For the question mark character, ?, press [ 2nd ] [ PRGM ] (CATALOG) [ ↑ ] [ ENTER ]. 


* The programming language of the TI-73 is the robust as the TI-83 family (TI-83, TI-83 Plus, TI-84 Plus family, TI-82).  


* The main difference is that results are shown on a separate screen for output.   When a program terminates, the TI-73 returns to the Home screen.  It is necessary to include a Pause at the end to keep the results on the screen.


Stay tuned tomorrow for more programs for the TI-73.  


Eddie


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Tuesday, July 18, 2017

Expanding Linear Regression

Expanding Linear Regression


Introduction – Linear Regression

All scientific calculators that have two-variable dimensions has curve fitting.  The most (and universal) curve fitting mode is linear regression.   Linear regression attempts to model bivariate data ((x,y)) to a line by the equation:

y = a + bx

Where a is the y-intercept (ITC) and b is the slope (SLP).  The correlation coefficient, r, is calculated by:

r = SXY / √(SX2 * SY2) where
SX2 = Σ(x – μx)^2
SY2 = Σ(y – μy)^2
SXY = Σ( (x – μx)*(y – μy) )
μx = mean of x values, μy = mean of y values

Source:  HP 17B II Financial Calculator Owner’s Manual, Corvallis, OR  1989.

The best fits are were |r| = 1, or as close to 1 as we can get.  If r is close to 0, the first is not good.  Note that the formula for correlation does not involve the slope or intercept.  We can use this to our advantage.

“Linearize” the Equation

If you can “linearize” equation, we can use the linear regression mode to fit a variety of curves.  The ultimate goal is of course:

y = a + bx

Now, most calculators use the form for linear equations.  However, some switch the a and b around and use the form, like the TI-30X IIS, 

y = ax + b

Hewlett Packard calculators has the b for the intercept and m for slope. 

For clarity, I will use the form Y = ITC + SLP*X. 

  
Calculator Comparison

To demonstrate how we can use the linear regression to fit a variety of curves, I use two calculators:



Sharp EL-W516T which offers 7 regressions.

TI-30X IIS which offers just linear regression.  I use the TI-30X IIS to use the linearized form.

[screen shot of the calcs] 

Sharp EL-W516T
TI-30X IIS
SLP
B
a
ITC
A
b
Linear Regression Equation
y = a + bx
y = ax + b

Screen shots are generated from a Casio fx-CG50. 


Logarithmic Regression:  y = a + b ln x



Translation to Linear:  This is pretty simple, we already have the form that we need, except we note that we have ln x instead of x.  Set the following variables as such:

X = ln x, Y = y, a = ITC, b = SLP

Example data set:
x
y
X = ln x  (8 decimal places)
1.0
2.00
0
1.3
3.00
0.2623643
1.7
4.15
0.5306283
2.3
5.34
0.8329091
2.8
6.10
1.0296194
3.5
7.00
1.252763

To use the linear regression mode, enter the data as such:  (ln x, y).  Everything else remains the same.

Logarithmic Regression Results:
ITC = a = 1.990614146
SLP = b = 4.003372076
r = 0.99905634

Comparing results between using the Logarithmic regression with the Sharp EL-W516T and the Linear regression with the TI-30X IIS, I get the same results.




Exponential Regression:  y  = a*e^(b*x)



Translation to linear: 

y = a*e^(b*x)
ln y = ln (a*e^(b*x))
ln y = ln a + ln(e^(b*x))
ln y = ln a + b*x

We got our form of Y = ITC + SLP*X where:
X = x, Y = ln y, a = e^ITC, b = SLP

When using the linear regression mode, enter data as (x, ln y).  When calculating a, note that

ITC = ln a
e^ITC = a

Example data set:
X
y
Y = ln y  (8 decimal places)
1.0
109.2
4.693181
1.3
363.0
5.8944028
1.7
1795.6
7.4930945
2.3
19794.3
9.8931493
2.8
146260.8
11.893147
3.5
2405208.5
14.693147

Results:
ITC = 0.693829923;  a = 2.001365951
SLP = b = 3.99773169
r = 0.999999992

Inverse Regression: y = a + b/x



Translation to Linear:  Like logarithmic regression, we pretty much have the equation pretty much set, with the exception of 1/x instead of x.  Hence:

X = 1/x, Y = y, a = ITC, b = SLP


 When using the linear regression mode, enter data as (1/x, y). Everything else remains.

Example data set:
X
y
X = 1/x  (8 decimal places)
1.0
6.00
1
1.3
5.55
0.7692308
1.7
5.16
0.5882353
2.3
4.86
0.4347826
2.8
4.73
0.3571429
3.5
4.58
0.2857143

Results:
a = ITC = 4.005529472
b = SLP = 1.993191341
r = 0.999733184

Power Regression:  y = a*x^b



Translation to Linear:  X = ln x, Y = ln y, A = e^a, b

y = a*x^b
ln y = ln (a*x^b)
ln y = ln a + ln(x^b)
ln y = ln a + b * ln x

So we have the following: X = ln x, Y = ln y, ITC = ln a, SLP = b.

In the linear regression mode, enter data as (ln x, ln y). Also, a = e^ITC. 

Example data set:
x
y
X = ln x
Y = ln y
1.0
3.95
0
1.3737156
1.3
6.76
0.2623643
1.9110229
1.7
11.56
0.5306283
2.4475509
2.3
21.09
0.8329091
3.048799
2.8
31.40
1.0296194
3.4468079
3.5
49.00
1.252763
3.8918203

Results:
ITC = 1.379195052; a = e^ITC = 3.971703326
SLP = b = 2.007158681
CORR = r = 0.999991481
   

Table of Linear Regression Equivalents to Curve Fitting


Regression

X

Y

ITC

SLP

Logarithmic: y = a + b * ln x

ln x

y

a

b

Exponential: y = a*e^(b*x)

x

ln y

e^a

b

Inverse: y = a + b/x

1/x

y

a

b

Power: y = a*x^b

ln x

ln y

e^a

b

General Exponential: y = a * b^x

x

ln y

e^a

e^b

Simple Logistic: y = 1/(a + b*e^(-x))

e^(-x)

1/y

a

b

Square Root Linear: y = √(a + b*x)

x

y^2

a

b

Cosine: y = a + b*cos(ω(x – Ï•))

With

Ï• = the point (x) nearest to zero where the trough or peak begins

ω = (2*π)/period (radians) or

ω = 360°/period (degrees)

cos(ω(x – Ï•))

y

a

b


Eddie


This blog is property of Edward Shore, 2016

HP 67 Collection: October 2026

HP 67 Collection: Oct ober 2026 All examples are done in FIX 4. (DSP 4) Earth's Gravity at a Specific Latitude The true ea...