Monday, November 21, 2011

HP 15C Programming Tutorial - Part 10: Statistics in Programming

Statistics on the HP 15C

The HP 15C performs one and two variable statistics. In statistical calculation, data is entered into Y and X registers. When the key [ ∑+ ] is pressed the following variables are updated:

R2 = n = number of data points
R3 = ∑x = sum of x data
R4 = ∑x^2 = sum of x-squared data
R5 = ∑y = sum of y data
R6 = ∑y^2 = sum of y-squared data
R7 = ∑xy = sum of the products of x-data and y-data

You can recall these values.

Additional statistical functions are available:

Arithmetic Mean: [ g ] [ 0 ]. The arithmetic mean of x-data is displayed. Press [x<>y] to display the arithmetic mean of y-data is displayed.

Standard Deviation: [ g ] [ . ]. The standard deviation of x-data is displayed. Press [x<>y] to display the standard deviation of y-data is displayed.

Clearing the Statistics Registers

[ f ] [GSB] (CLEAR ∑) clears memory registers R2, R3, R4, R5, R6, and R7 to 0.

Linear Regression

The HP 15C includes linear regression functions to fit data to the line:

y = a x + b

where a = slope and b = y-intercept with r = correlation coefficient. If r is near 1 or -1, the data has a good fit to the line. If r is near 0, the data does not have a good fit to a line.

To find a, b, and r:

b: [ f ] [∑+] (L.R.)
a: [ f ] [∑+] (L.R.) [x<>y]
r: a valid number [ f ] [ . ] (y-hat, r) [x<>y]

Entering Data

One Variable Data:
1. Enter x
2. Press [∑+]

Two Variable Data:
1. Enter y
2. Press [ENTER]
3. Enter x
4. Press [∑+]

The key sequence [ g ] [∑+] (∑-) can be used to remove data.

A Example of Linear Regression

Find a linear fit to the data:

x	y
1 3
2 6
4 11
8 15
16 36


Caution: In two variable data, enter the y data first.

Key press:

[ f ] [GSB] (CLEAR ∑)
3 [ENTER] 1 [∑+]
6 [ENTER] 2 [∑+]
11 [ENTER] 4 [∑+]
15 [ENTER] 8 [∑+]
36 [ENTER] 16 [∑+]

First the slope and y-intercept:

[ f ] [∑+] (L.R.) (** 1.0833 is displayed)
[x<>y] (** 2.1156 is displayed)
1 [ f ] [ . ] (y-hat, r) [x<>y] (** 0.9905 is displayed)

Results: The linear fit is approximately is y ≈ 2.1156 + 1.0833x with r ≈ 0.9905 (a very good fit!)

In the next program, we will use the statistical features to fit data to a logarithmic fit.

Logarithmic Regression

This program fits the data to the equation:

y = b + a ln x

Note that this is similar to the linear equation y = b + a x, except that we are working with "ln x" instead of "x".

This program adjusts the x-data to ln(x-data).

Labels used:
Label A: Initialize data; clear registers R2 - R7
Label B: Adjusts the data and enters the data
Label C: Calculates a, b, and r.

Program Listing

Key Codes		Key
001 42 21 22 LBL A * Initialization
002 42 32 CLR ∑
003 43 32 RTN
004 42 21 12 LBL B * Data Entry
005 43 12 LN
006 49 ∑+
007 43 32 RTN
008 42 21 13 LBL C * Calculate b, a, r
009 42 49 L.R.
010 31 R/S * Display b
011 34 x<>y
012 31 R/S * Display a
013 1 1
014 42 48 y-hat, r
015 34 x<>y * Display r
016 43 32 RTN


Example

Data is presented regarding the growth of a human male:

X = Age (in years)	Y = Height (in feet)
1 0.88
2 1.44
3 2.06
4 2.18
5 2.45
10 5.02
15 5.76
20 6.09


Use the program to find a logarithmic fit.

Key Presses:

[ f ] [ √ ] (A)
0.88 [ENTER] 1 [ f ] [e^x] (B)
1.44 [ENTER] 2 [ f ] [e^x] (B)
2.06 [ENTER] 3 [ f ] [e^x] (B)
2.18 [ENTER] 4 [ f ] [e^x] (B)
2.45 [ENTER] 5 [ f ] [e^x] (B)
5.02 [ENTER] 10 [ f ] [e^x] (B)
5.76 [ENTER] 15 [ f ] [e^x] (B)
6.09 [ENTER] 20 [ f ] [e^x] (B)
[ f ] [10^x] (C) (* Display: 0.1363)
[R/S] (* Display: 1.9376)
[R/S] (* Display: 0.9656)

Results:

y ≈ 0.1363 + 1.9376 ln x with r ≈ 0.9656 (very good fit!)

Regressions

The following table shows various fits that can be used using L.R. and y-hat,r functions. Use the necessary adjustments to get the correct data.

For example, for logarithmic fit, take the natural logarithm of x before pressing [∑+]. For exponential fit, take the natural logarithm of y before pressing [∑+]; when pressing [L.R.], take the exponential of both a and b to get the true slope and y-intercept.

Fit		Equation	x	y		a	b
Linear y = ax+b x y a b
Logarithmic y = b + a ln x y a b
Power y = b * x^a ln x ln y a b
Exponential y = b * a^x x ln y e^a e^b
Inverse y = b + a/x 1/x y a b
Logistic y = L/(1+ae^(bx)) x ln(L/y - 1) e^a b
L > 2y_max
Sine y = b + a sin(px) sin(px) y a b
p = (x_max - x_min)/(2π)


Hope you find this table useful.

Until then, happy programming,
Eddie


This tutorial is property of Edward Shore. © 2011

3 comments:

  1. Did you got the linear fit wrong?
    y ≈ 2.1156 + 1.0833x
    I think it should be
    y ≈ 1.0833 + 2.1156x

    ReplyDelete
    Replies
    1. I checked it again and got the first result.

      Eddie

      Delete
    2. when I actually plot the line to the data, y ≈ 1.0833 + 2.1156x seems to be a better fit?

      Delete

Next Week... and Plans for October 2017

I'm so excited, can't want for next week's HHC 2017 calculator conference in Nashville!  It is my annual calculator conference ...