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

Sunday, July 16, 2017

Retro Review: HP 22S

Retro Review:  HP 22S

HP 22S



Pioneers:  10B, 10bII+ (updated), 14B, 17bII+ (updated), 20S, 21S, 22S, 27S, 32SII, 42S


Essentials

Company:  Hewlett Packard
Years:  1988 - 1991
Type:  Scientific, Formula Programming
Memory:  371
Operating System: Algebraic
Memory Registers: 26 (A-Z)
Number of Built In Equations: 16

Batteries:  3 LR44, 3 A76, 3 357

Features

The HP 22S is an algebraic calculator that shares the similar style with the HP 20S, 21S, 32S, and 42S.  The top row of keys ([ √x ], [ e^x ], [ LN ], [ y^x ], [ 1/x ], [ Σ+ ]) act not only as function keys but soft menu keys. 

The standard variety of functions are present:

* Trigonometric, hyperbolic, exponential, and logarithmic functions
* Conversions:  kg/lb, °C/°F, cm/in, ltr/gal, H/HMS, DEG/RAD
* Probability:  nCr, nPr, n!  (the factorial function only accepts integer)
* Integer part, fractional part, absolute value, round to fix setting
* Statistics:  1 Variable and Linear Regression (y = mx + b)
* Storage arithmetic
* Base conversions: Binary, Decimal, Octal, Hexadecimal.  Decimal mode is floating decimal mode.
* This is really cool:  the rectangular (x-COORD, y-COORD) and polar (RADIUS, ANGLE) conversions get their own keys (well, shifted functions of [ 1 ], [ 2 ], [ 3 ], and [ - ], respectively)
* Percent Change:  old [INPUT] new [(shift)] [1/x] (%CHG)  (nPr and nCr work the same way)

Overall the keys are really easy to work with and the calculator is a pleasure to use.  I like the crisp display.  Plus, if blue is your favorite color like me, the light blue font against the dark background is a plus. 

There are two manuals with the HP 22S:  the User Manual, and the excellent Science Student Applications for the HP 22S. 

Equations

You can either use one of the 14 built-in equations (listed below) or create your own.  While you are creating your own, you can use either the [STO] or [RCL] keys to call up a letter.  All variables are one letter and global. Equations that are longer than the 12 character display can be scrolled by the [ √x ] and [ Σ+ ] keys.

Equations can be evaluated (evaluate the right side of the equation) or solved (a menu will show the variables to be solved for).  Equations are solved in a reasonably amount of time.


The 16 built in equations, which are displayed by the equations themselves (no titles):
(Source: HP 22S manual)

Title
Equation
Length of a Line/Vector
R=SQRT(X^2+Y^2+Z^2)
Roots of a Quadratic Equation
X=(-B+J*SQRT(B^2-4*A*C))÷2÷A
Real roots only, J = -1 or 1
Equation of Motion (Position)
X=S+V*T+.5*A*T^2
X = position, V = velocity, A = acceleration
Equation of Motion (Final Velocity)
F=V+A*T
Kinetic Energy
E=.5*M*V^2
Force Between Two Objects (Physics or Electric Force)
F=K*A*B÷R^2
K = 6.67408E-11 m^3 kg^-1 s^-2 (gravitational force)
K = 1.602176621E-19 J (electron charge)
Joule’s Law
P=I^2*R
Ideal Gas Law
P*V=N*R*T
Gibb’s Free Energy
G=H-T*S
Pressure of a Fluid
P=I+D*G*H
G = 9.80665 m/s^2
Radioactive Decay
-K*T=LN(N÷I)
Thin Lens Equation
O*F+I*F=O*I
Diffraction
A=ASIN(M*L÷D)
Exponential Growth and Decay
Y=F+(I-F)*EXP(K*T)
Root Mean Square
R=SQRT(Σx^2÷n)
Σx^2 and n come from statistical data
Time Value of Money
(End of Period payments/periods assumed)
(P*100÷I-F)*(1+I÷100)^-N-P*100÷I=B
N = number of payments/periods
B = present value
I = periodic interest rate
P = payment
F = future value

Final Verdict

The HP 22S is a great calculator to own.  I love the rectangular/polar conversion set up.  The display is great.

My only criticism is that I wish the 22S had more memory.  371 bytes can store a handful equations, but data points that are used in the statistical calculations and storing values in the variables A-Z eat up the memory as well.    

4 out of 5 stars.

Eddie

This blog is property of Edward Shore, 2017

Coming soon (tentative):  a review of a favorite Texas Instruments calculator, TI-68.


Expanding Linear Regression

Expanding Linear Regression Introduction – Linear Regression All scientific calculators that have two-variable dimensions has cu...