The Museum of HP Calculators

# Linear Regression for the HP-25

This program is Copyright © 1975 by Hewlett-Packard and is used here by permission. This program was originally published in "HP-25 Applications Programs" as four separate programs. These were generalized into four versions of one program and reorganized by Lawrence Leinweber.

This program is supplied without representation or warranty of any kind. Hewlett-Packard Company, Mr. Leinweber, and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

## Overview

When investigating the relationship between two variables in the real world, it is a reasonable first step to make experimental observations of the system to gather paired values of the variables, (x, y). The investigator might then ask the question: What mathematical formula best describes the relationship between the variables x and y? His first guess will often be that the relationship is linear, i.e., that the form of the equation is y = a1x + a0, where a1 and a0 are constants. The purpose of this program is to find the constants a1 and a0, which give the closest agreement between the experimental data and the equation y = a1x + a0. The technique used is linear regression by the method of least squares.

The user must input the paired values of data he has gathered, (xi, yi), i = 1, ..., n. When all data pairs have been input, the regression constants a1 and a0 may be calculated. A third value may also be found, the coefficient of determination, r2. The value of r2 will lie between 0 and 1 and will indicate how closely the equation fits the experimental data: the closer r2 is to 1, the better the fit.

### Equations:

y = a1x + a0

All summations below are performed for i = 1, ..., n.

Regression constants:

a1 = (xy - y ÷ n) ÷ (x2 - (x)2 ÷ n)

a0 = y ÷ n - a1x ÷ n

Coefficient of determination:

r2 = [xy - y ÷ n]2 ÷ [x2 - (x)2 ÷ n] ÷ [y2 - (y)2 ÷ n]

## Generalization

Besides the linear curve fit, Hewlett-Packard included three similar programs for exponential, logarithmic, and power curve fits. The minor differences between these versions are noted in the text below.

* The logarithms of x, y and a0 are introduced here as x*, y* and a0*, then the distinction is dropped because it unnecessarily complicates the instructions below. Technically, the equations in the overview section above refer to the ordinary variables, whereas the data references in the sections below refer to the logarithms as described in this section.

### Linear Curve Fit:

This version fits a linear curve

y = a1x + a0

to a set of data points {(xi, yi), i =1, ..., n}.

### Exponential Curve Fit:

Let a0* = ln a0 (a0* > 0) and y* = ln y (y* > 0). Substituting into the linear equation y = a1x + a0 above, gives ln y* = a1x + ln a0* or y* = a0* e(a1x).

This version fits an exponential curve

y = a0 e(a1x)   (a0 > 0)

to a set of data points {(xi, yi), i =1, ..., n}, where yi > 0.

### Logarithmic Curve Fit:

Let x* = ln x (x* > 0). Substituting into the linear equation y = a1x + a0 above, gives y = a1 ln x* + a0.

This version fits a logarithmic curve

y = a0 + a1 ln x

to a set of data points {(xi, yi), i =1, ..., n}, where xi > 0.

### Power Curve Fit:

This is a combination of both the exponential and logarithmic curve fits. Substituting a0*, y* and x* into the linear equation y = a1x + a0 above, gives ln y* = a1ln x* + ln a0* or y* = a0* x*(a1).

This version fits a power curve

y = a0 x(a1)   (a0 > 0)

to a set of data points {(xi, yi), i =1, ..., n}, where xi > 0, yi > 0.

## Instructions

 Step Instructions Input Data/Units Keys Output Data/Units 1 Key in program 2 Initialize f REG f PRGM 3 Perform for i = 1, ..., n: Input x-value and y-value xi ENTER yi R/S i 4 Compute regression constants and coefficient of determination GTO 10 R/S a0 roll dn a1 roll dn r2 5 To calculate a projected y-value, input the x-value x R/S y (estimate) 6 Perform step 5 as many times as desired 7 For a new case, go to step 2

## Examples

### Linear Curve Fit:

An eccentric professor of numerical analysis wakes up one morning and feels feverish. A search through his medicine cabinet reveals one oral thermometer which, unfortunately, is in degrees centigrade, a scale he is not familiar with. As he stares disconsolately out his window, he spies the outdoor thermometer affixed to the windowframe. This thermometer, however, will not fit comfortably into his mouth. Still, with some ingenuity....

The professor suspects that the relationship is F = a1C + a0. If he can get a few data pairs for F and C, he can run a linear regression program to find a1 and a0, then convert any reading in °C to °F through the equations. So tossing both thermometers into a sink of lukewarm water, he reads the following pairs of temperatures as the water cools:

 C 40.5 38.6 37.9 36.2 35.1 34.6 F 104.5 102 100 97.5 95.5 94

If the relationship is indeed F = a1C + a0, what are the values for a1 and a0? What is the coefficient of determination?

#### Solution:

 Keys Output f PRGM f REG 40.5 ENTER 104.5 R/S 1.00 38.6 ENTER 102 R/S 2.00 37.9 ENTER 100 R/S 3.00 36.2 ENTER 97.5 R/S 4.00 35.1 ENTER 95.5 R/S 5.00 34.6 ENTER 94 R/S 6.00 GTO 10 R/S 33.53 roll dn 1.76 roll dn 0.99

Thus, by the data above, F = 1.76 C + 33.53, with r2 = 0.99. (The real equation, of course, is F = 1.8C + 32.)

Suppose the professor puts the centigrade thermometer in his mouth and finds he has a temperature of 37°C. Should he be worried?

 37 R/S 98.65°F

It looks like he is safe.

### Exponential Curve Fit:

 xi 0.72 1.31 1.95 2.58 3.14 yi 2.16 1.61 1.16 0.85 0.5

#### Solution:

a0 = 3.45, a1 = -0.58
y = 3.45 e-0.58x
r2 = 0.98
For x = 1.5, y (est.) = 1.44

### Logarithmic Curve Fit:

 xi 3 4 6 10 12 yi 1.5 9.3 23.4 45.8 60.1

#### Solution:

a0 = -47.02, a1 = 41.39
y = -47.02 + 41.39 ln x
r2 = 0.98
For x = 8, y (est.) = 39.06
For x = 14.5, y (est.) = 63.67

### Power Curve Fit:

 xi 10 12 15 17 20 22 25 27 30 32 35 yi 0.95 1.05 1.25 1.41 1.73 2 2.53 2.98 3.85 4.59 6.02

#### Solution:

a0 = .03, a1 = 1.46
y = .03 x1.46
r2 = 0.94
For x = 18, y (est.) = 1.76
For x = 23, y (est.) = 2.52

## The Program

```LINE    CODE    KEYS            COMMENTS
00
/01\   /15 74\ /g NOP\         /Linear or logarithmic curve fit
\01/   \14 07/ \f ln /         \Exponential or power curve fit
02        31   ENTER           Steps 1-9 for summation
03     15 02   g x2
04  23 51 02   STO + 2         sum y2
05        22   roll dn
06        21   x<>y
/07\   /15 74\ /g NOP\         /Linear or exponential curve fit
\07/   \14 07/ \f ln /         \Logarithmic or power curve fit
08        25   sum+            n, sum y, sum xy, sum x2, sum x
09     13 00   GTO 00
10     24 05   RCL 5
11     14 21   f mean x
12     24 04   RCL 4
13        61   ×
14        41   -               C = sum xy - (sum x sum y / n)
15     24 06   RCL 6           (C goes on a magic carpet ride)
16     24 07   RCL 7
17     14 21   f mean x        (C at top of stack)
18        61   ×               (C is duplicated as stack falls)
19        41   -               D = sum x2 - [(sum x)2 / n]
20        71   ÷               a1 = C / D
21     23 01   STO 1
22        61   ×               a1 * C (handy extra copy of C)
23     24 02   RCL 2
24     24 04   RCL 4
25     15 02   g x2
26     24 03   RCL 3
27        71   ÷
28        41   -               E = sum y2 - [(sum y)2 / n]
29        71   ÷               r2 = a1 C / E
30     24 04   RCL 4
31     24 07   RCL 7
32     24 01   RCL 1
33        61   ×
34        41   -
35     24 03   RCL 3
36        71   ÷               a0 = (sum y - a1 sum x) / n
/37\   /15 74\ /g NOP\         /Linear or logarithmic curve fit
\37/   \15 07/ \g ex /         \Exponential or power curve fit
38     23 00   STO 0
39     24 01   RCL 1
40        21   x<>y
41        74   R/S             Halt to display a0, a1, r2
/42\   /15 74\ /g NOP\         /Linear or exponential curve fit
\42/   \14 07/ \f ln /         \Logarithmic or power curve fit
43     24 01   RCL 1
44        61   ×
45     24 00   RCL 0
/46\   /15 74\ /g NOP\         /Linear or logarithmic curve fit
\46/   \14 07/ \f ln /         \Exponential or power curve fit
47        51   +
/48\   /15 74\ /g NOP\         /Linear or logarithmic curve fit
\48/   \15 07/ \g ex /         \Exponential or power curve fit
49     13 41   GTO 41
```

## Register Use

```R0  a0
R1  a1
R2  sum y2
R3  n
R4  sum y
R5  sum xy
R6  sum x2
R7  sum x

```