Sunday, March 31, 2024

TI-30Xa Algorithms: Linear Regression

 TI-30Xa Algorithms: Linear Regression


Welcome to the March 2024 installment of TI-30Xa Algorithms.



Linear Regression with a TI-30Xa?


Today’s task is to fit bi-variate data to the line:


y = m * x + b


with the TI-30Xa. What? This calculator does not have a linear regression mode. Yes, with it is possible. We are going to use the technique shown in by the TI-36 Solar Guidebook from 1985 (see Source below). The TI-36 Solar was part of Texas Instruments’ line in the late 1980s and the early 1990s.


You can read my review on the TI-36 Solar from September 26, 2020 here:

https://edspi31415.blogspot.com/2020/09/retro-review-ti-36-solar.html


Where the TI-36 Solar only had one memory register, the TI-30Xa has three memory registers, and they are going to come in handy here.



Procedure


Caution: Be sure the calculator is on the entire time. Turning off the calculator will clear out statistics mode.


1. Enter the y data. Store the mean (y-bar) into memory register 2. Store the population deviation (σy) into memory register 3.


Keystrokes:

Mean of y-data: [ 2nd ] [ x^2 ] {x-bar} [ STO ] [ 2 ]

Population of y-data: [ 2nd ] [ ÷ ] { σxn } [ STO ] [ 3 ]


2. Clear the stat registers and enter the x data. As we are entering the x data, use memory register 1 to calculate Σxy.


Keystrokes:


Clear Stat Registers: [ 2nd ] [ 7 ] {CSR}


1st Point:

x1 [ × ] y1 [ = ] [ STO ] [ 1 ]

x1 [ Σ+ ]


Every point there after:

x_i [ × ] y_i [ = ] [ 2nd ] [ RCL ] {SUM} [ 1 ]

x_i [ Σ+ ]



3. Calculate the slope, and replace Σxy with the slope.


m = ( y-bar * Σx – Σxy) / ( x-bar * Σx - Σx^2)


Keystrokes:

[ ( ] [ RCL ] [ 2 ] [ × ] [ 2nd ] [ ( ] [ - ] [ RCL ] [ 1 ] [ ) ]

[ ÷ ] [ ( ] [ 2nd ] [ x^2 ] [ × ] [ 2nd ] [ ( ] [ - ] [ 2nd ] [ ) ] [ = ] [ STO ] [ 1 ]


4. Calculate the y-intercept, replace y-bar with the y-intercept.


b = -m * x-bar + y-bar


Keystrokes:

[ RCL ] [ 1 ] [ +/- ] [ × ] [ 2nd ] [ x^2 ] [ + ] [ RCL ] [ 2 ] [ = ] [ STO ] [ 2 ]


5. Calculate the correlation. If the correlation is close to -1 or +1, the linear fit will be excellent.


r = m * σx / σy


Keystrokes:

[ RCL ] [ 1 ] [ × ] [ 2nd ] [ ÷ ] [ ÷ ] [ RCL ] [ 3 ] [ = ]


6. Use slope (m) and intercept (b) to predict x and y values:


y’ = m * x0 + b

Keystrokes: [ RCL ] [ 1 ] [ × ] x0 [ + ] [ RCL ] [ 2 ] [ = ]


x’ = (y0 – b) / m

Keystrokes: [ ( ] y0 [ - ] [ RCL ] [ 2 ] [ ) ] [ ÷ ] [ RCL ] [ 1 ] [ = ]


Key Map


[ RCL ] [ 1 ]: first Σxy, then m

[ RCL ] [ 2 ]: first y-bar, then b

[ RCL ] [ 3 ]: σy

[ 2nd ] [ x^2 ]: x-bar

[ 2nd ] [ ÷ ]: σxn



Example


Fit a line to the data:


X

Y

12

100

13

98.7

14

97.1

16

94.9

18

92.6

20

90



1. Enter the y data. Store the mean (y-bar) into memory register 2. Store the population deviation (σy) into memory register 3.


100 [ Σ+ ]

98.7 [ Σ+ ]

97.1 [ Σ+ ]

94.9 [ Σ+ ]

92.6 [ Σ+ ]

90 [ Σ+ ] (display n = 6)



Mean of y-data: [ 2nd ] [ x^2 ] [ STO ] [ 2 ] (y-bar = 95.55)


Population of y-data: [ 2nd ] [ ÷ ] [ STO ] [ 3 ] (σy = 3.465424457)



2. Clear the stat registers and enter the x data. As we are entering the x data, use memory register 1 to calculate Σxy.


Keystrokes:


Clear Stat Registers: [ 2nd ] [ 7 ] {CSR}


12 [ × ] 100 [ = ] [ STO ] [ 1 ]

12 [ Σ+ ]


13 [ × ] 98.7 [ = ] [ 2nd ] [ RCL ] [ 1 ] (SUM 1)

13 [ Σ+ ]


14 [ × ] 97.1 [ = ] [ 2nd ] [ RCL ] [ 1 ] (SUM 1)

14 [ Σ+ ]


16 [ × ] 94.9 [ = ] [ 2nd ] [ RCL ] [ 1 ] (SUM 1)

16 [ Σ+ ]


18 [ × ] 92.6 [ = ] [ 2nd ] [ RCL ] [ 1 ] (SUM 1)

18 [ Σ+ ]


20 [ × ] 90 [ = ] [ 2nd ] [ RCL ] [ 1 ] (SUM 1)

20 [ Σ+ ]


RCL 1: Σxy = 8827.7

[ 2nd ] [ x^2 ]: x-bar = 15.5



3. Calculate the slope, and replace Σxy with the slope.


[ ( ] [ RCL ] [ 2 ] [ × ] [ 2nd ] [ ( ] [ - ] [ RCL ] [ 1 ] [ ) ]

[ ÷ ] [ ( ] [ 2nd ] [ x^2 ] [ × ] [ 2nd ] [ ( ] [ - ] [ 2nd ] [ ) ] [ = ] [ STO ] [ 1 ]


Slope: m = -1.230526316


4. Calculate the y-intercept, replace y-bar with the y-intercept.


[ RCL ] [ 1 ] [ +/- ] [ × ] [ 2nd ] [ x^2 ] [ + ] [ RCL ] [ 2 ] [ = ] [ STO ] [ 2 ]


Intercept: b = 114.6231579


5. Calculate the correlation.


[ RCL ] [ 1 ] [ × ] [ 2nd ] [ ÷ ] [ ÷ ] [ RCL ] [ 3 ] [ = ]


Correlation: r = -0.999092386


The line is:


y = -1.230526316 * x + 114.6231579



6. Predict values.


If x = 15, predict the y value (y’):


[ RCL ] [ 1 ] [ × ] x0 [ + ] [ RCL ] [ 2 ] [ = ]

y’ = 96.16526316


If y = 95, predict the x value (x’):


[ ( ] y0 [ - ] [ RCL ] [ 2 ] [ ) ] [ ÷ ] [ RCL ] [ 1 ] [ = ]

x’ = 15.94696322



Source


Alley, Chris M., Brenda M. Cornitius, et al. TI-36 Solar Guidebook Texas Instruments Incorporated. Dallas, TX. 1985, 1986, 1987. pp. 4.6 – 4.13



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 30, 2024

HP Prime and Casio fx-CG 50: Dedekind Sums

 HP Prime and Casio fx-CG 50: Dedekind Sums


Definition


The Dedekind Sum is defined as follows:


Let P and Q be relatively prime integers, that is GCD(P, Q) = 1.


Then S is the Dedekind sum as:


S = Σ( ((I ÷ Q)) × ((P × I ÷ Q)), for I=1 to Q)


The double parenthesis around the terms I ÷ Q and P × I ÷ Q signify a custom function:


(( X )) =

0, if X is an integer

X – FLOOR(X) – 1/2, if X is not an integer


If X is positive, X – INTG(X) – 1/2


HP Prime: DEDEKIND

EXPORT DEDEKIND(p,q)

BEGIN

// 2024-02-21 EWS

LOCAL s,i,a,b;



// Calculation

IF CAS.gcd(p,q)==1 THEN

s:=0;



FOR i FROM 1 TO q DO



a:=i/q;

IF FP(a)==0 THEN

a:=0;

ELSE

a:=a-FLOOR(a)-0.5;

END;



b:=p*i/q;

IF FP(b)==0 THEN

b:=0;

ELSE

b:=b-FLOOR(b)-0.5;

END;

s:=s+a*b;

END;

RETURN s;

ELSE

RETURN "p and q are not relatively prime.";

END;

END;


Casio fx-CG 50: DEDEKIND

244 bytes


Code:

 “DEDEKIND SUM: S(P,Q)”

P”? → P

Q”? → Q


If GCD(P,Q)≠1

Then

P AND Q ARE NOT RELATIVELY PRIME”

Stop


For 1→ I To Q

I÷Q → A

Frac A=0 ⇒ 0 → A

Frac A≠0 ⇒ A – Intg A – 0.5 → A

P × I ÷ Q → B

Frac B=0 ⇒ 0 → B

Frac B≠0 ⇒ B – Intg B – 0.5 → B
S + A × B → S

Next

S


Note: The are 6 spaces between NOT and RELATIVELY to align the text.


Examples


P

Q

Results (fraction)

Results (decimal)

2

17

8/17

0.4705882353

1

21

95/63

1.50793650794

9

43

27/86

0.3139534884

8

67

53/134

0.3955223881

4

75

649/450

1.442222222

14

57

-140/171

-0.8187134503


Sources

Shipp, R. Dale. “Table of Dedekind Sums” Journal of Research of the National Bureau of Standards-B. Mathematics and Mathematical Physics Vol. 69B, No 4, October-December 1965 https://nvlpubs.nist.gov/nistpubs/jres/69B/jresv69Bn4p259_A1b.pdf

Retrieved February 21, 2024


Weisstein, Eric W. "Dedekind Sum." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/DedekindSum.html

Retrieved February 18, 2024


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.

Sunday, March 24, 2024

 

Casio fx-7000G vs Casio fx-CG 50: A Comparison of Generating Statistical Graphs



Today’s blog entry is a comparison of how a histogram, normal distribution graphs based on statistical data, scatter plots, and linear regression plot are generated on Casio’s first graphing calculator, the fx-7000G (1985) and the most recent (as of this blog post), the fx-CG 50 (2016). For the curious, the SD2 and LR2 modes are the statistical plot modes of the fx-7000G.


The procedures for the fx-7000G are the same for the fx-6500G, fx-7500G, fx-8000G (and equivalents) and fx-6300G.


The procedures for the fx-CG 50 are the same for the fx-CG 10/20, the fx-9860G series, and fx-9750G series.





Single Variable: Histogram Graphs and Normal Distribution Graphs


The screen shots for this section uses the example data:


Rank #

Rank (List 1)

Frequency (List 2)

1

10

11

2

20

19

3

30

36

4

40

39

5

50

33

6

60

13


Histogram: fx-7000G


1. Enter SD2 mode by pressing [ SHIFT ] [ MODE ] [ × ]. Execute Cls to clear the graph screen and Scl to clear the statistical data registers.

2. There is no automatic zoom adjustment for statistical data on the fx-7000G. Observe the data and set the range accordingly.

3. Count the number of ranks. We have to set aside additional memory registers for the bars. Do this by pressing [ SHIFT ] [ MODE ] [ . ] (Defm mode) and entering the number of ranks. For the data set above, we will need 6 additional registers (Defm 6).

4. Enter the data by using the [x^y] key, which acts as the data entry (DT) key for this mode. The format for each point is: rank ; frequency.

5. Draw the bar graphing by pressing [Graph] [ EXE ]. In other words, run the Graph Y= command without any other arguments. The bar graph can be traced.


(Note: The range I used is: Xmin: 10, Xmax: 70, Xscl: 10, Ymin: 0, Ymin: 40, Yscl: 10)





Normal Distribution Graph: fx-7000G


1. Clear the graph screen by executing Cls.

2. Redo the range. The y-values will have a probability of 1 or less. I like to set Ymin to a very small negative value so that the graph won’t be at the bottom of the screen.

3. Draw the normal graph by pressing [Graph] [SHIFT] [ ↑ ] {Line} 1 [ EXE ]. The command line is Graph Y=Line 1. The normal distribution curve can be traced.


Don’t forget to reset the memory to Defm 0 (or the setting you had) when you are done. Allocating for extra memory registers reduces the amount of program steps available.





Histogram: fx-CG 50


On the fx-CG 50, everything is done through the Statistics Mode. We can use any list from List 1 to List 26, generate multiple data sets and graphs, and on the fx-CG 10/20 and fx-CG 50, the graphs are in color.


1. For this graph, listed the ranks in List 1 and frequencies in List 2. From the main menu, press [ F1 ] {GRAPH}, [ F6 ] { SET }, and either [ F1 ], [ F2 ], or [ F3 ] to choose the graph slot Graph1, Graph2, or Graph3, respectively. Set the Graph Type to Hist.

2. Exit the setup by pressing [ EXIT ], and next press the corresponding graph slot. We will be prompted for the start value (Xmin) and the width (size of the rank). Press [ EXE ]. On the fx-CG 50, we do not have to worry about the window ranges because the Statistics mode automatically zooms the window to fit the data. Nice! The histogram can be traced.





Normal Distribution Graph: fx-CG 50


1. From the main menu, press [ F1 ] {GRAPH}, [ F6 ] { SET }. Change the Graph Type to N-Dist.

2. Exit the set up and press the graph slot. Again, the window zooms to fit the curve. The normal curve can be traced.




Linear Regression: Scatter Plot and Linear Regression Plot


X: (List 1)

Y: (List 2)

-8

-10

-4

-6

-1

0

2

3

5

6

8

8


Scatter Plot: fx-7000G


1. Enter LR2 mode by pressing [ SHIFT ] [ MODE ] [ ÷ ]. Execute Cls to clear the graph screen and Scl to clear the statistical data registers.

2. Adjust the Range (viewing window) to fit the data.

3. Enter the data by using the [x^y] key, which acts as the data entry (DT) key for this mode. The format for each point is: x, y.


If the point occurs more than once, use the format x, y; frequency.


The scatter plot dynamically updates and is shown each time a point is added.





Linear Regression Plot: fx-7000G


The line fits to the equation y = A + Bx (A is the y-intercept, B is the slope).


1. Execute the command Graph Y=Line 1 by pressing by pressing [Graph] [SHIFT] [ ↑ ] {Line} 1 [ EXE ]. The line is then plotted and can be traced. Easy as that.





Scatter Plot: fx-CG 50


Execute the statistics mode and enter the data. Like the histogram and normal distribution plots, the fx-CG 50 automatically adjusts the window to fit the statistical data for the scatter plot and linear regression line.


1. For this graph, List 1 has the x data and List 2 has the y data. From the main menu, press [ F1 ] {GRAPH}, [ F6 ] { SET }, and either [ F1 ], [ F2 ], or [ F3 ] to choose the graph slot Graph1, Graph2, or Graph3, respectively. Set the Graph Type to Scatter. You have an option of selecting the shape of the markers of the point.

2. Exit the setup by pressing [ EXIT ], and next press the corresponding graph slot. The scatter plot will be shown and you can trace the points.





Linear Regression Plot: fx-CG 50


1. While we are on the scatter plot screen, press [ F1 ] {CALC}, then [ F2 ] { X }. We have a choice of two forms: ax+b (default) or a+bx. I chose a+bx to match the fx-7000G.

2. The regression equation, along with the intercept, slope, and correlation are shown. When ready, press [ F6 ] {DRAW}.

3. The regression line is drawn, and the line can be traced.





I hope you enjoyed the comparisons between the classic fx-7000G and modern fx-CG 50.


Until next time,


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 23, 2024

Measuring a Distance with an Object in the Way

 Measuring a Distance with an Object in the Way




A Surveying Problem





Refer to the picture above. Here’s the situation: We are given a line and we are to determine a distance, X, from the Observer to the Survey Point. There is one problem though, there is a tree in the way, and the distance we are to measure goes directly through the tree.


Perhaps we can estimate the distance. Pick a point where the line of sight is not blocked. Let this new point be the Auxiliary Point and let C be the distance between the Observer and the Auxiliary Point.


We can also measure the distance between the Auxiliary Point and Survey Point, name this distance H. We can use the “right triangle” created to find the original distance X.



By the Pythagorean Theorem, we have:


X^2 + H^2 = C^2

X^2 = C^2 – H^2

X = √(C^2 – H^2) (only positive roots make geometric sense)


What if the calculator device does not have the square root function? Or if you are working by hand? In the 1938 book, The Principles and Practice of Surveying, Volume I (see the Source section), we can estimate the length of X without the need of taking a square root. Here is the derivation:


X^2 + H^2 = C^2

H^2 = C^2 – X^2

Observe that for any t, s: t^2 – s^2 = (t – s) × (t + s), and:


H^2 = (C – X) × (C + X)


If H is small, then C is close to X. Assume that H is small. As a result, C ≈ X. This derivation applies this approximation only to the term C + X:


H^2 ≈ (C – X) × (C + C)


Solve for X:


H^2 ≈ (C – X) × 2 × C

H^2 ÷ (2 × C) ≈ C – X

X ≈ C - H^2 ÷ (2 × C)


How good is the estimated formula?




Testing the Approximation


I used the HP 15C Collector’s Edition for testing the approximation formula. The code used to compare the approximate to the actual result:


Line #; Key Code; Key


Stack: y: hypotenuse (C), x: side length (H); (y > x)


01; 42, 21, 11; LBL A // approximate calculation

02; __, 44, _1; STO 1

03; __, __, 34; x<>y

04; __, 44, _2; STO 2

05; __, 45, _2; RCL 2

06; __, 45, _1; RCL 1

07; __, 43, 11; x^2

08; __, __, _2; 2

09; __, __, 10; ÷

10; __, 45, _2; RCL 2

11; __, __, 10; ÷

12; __, __, 30; -

13; __, 44, _3; STO 3

14; __, __, 31; R/S

15; __, 45, _2; RCL 2 // actual calculation

16; __, 43, 11; x^2

17; __, 45, _1; RCL 1

18; __, 43, 11; x^2

19; __, __, 30; -

20; __, __, 11; √

21; __, 44, _4; STO 4

22; __, __, 31; R/S

23; __, 45, _3; RCL 3 // absolute error

24; __, 45, _4; RCL 4

25; __, __, 30; -

26; __, 43, 16; ABS

27; __, 43, 32; RTN


(27 steps, 34 bytes)


Sample Data (rounded to 7 digits)


C

H

Approx.

Actual

Abs. Error

5

1

4.9

4.8989795

0.0010205

6

1

5.9166667

5.9160798

0.0005869

7

1

6.2985174

6.9282032

0.0003682

8

1

7.9375

7.9372539

0.0002461

9

1

8.9444444

8.9442719

0.0001725

10

1

9.95

9.9498744

0.0001256

5

2

4.6

4.5825757

0.0174243

6

2

5.6666667

5.6556842

0.0098124

7

2

6.7142857

6.7082039

0.0060818

8

2

7.75

7.7459667

0.0040333

9

2

8.7777778

8.7749644

0.0028134

10

2

9.8

9.7979590

0.0020410

5

3

4.1

4

0.1

6

3

5.25

5.1961524

0.0538476

7

3

6.3571429

6.3245553

0.0325875

8

3

7.4375

7.4161985

0.0213015

9

3

8.5

8.4852814

0.0147186

10

3

9.55

9.5393920

0.0106080

20

19

10.975

6.2449980

4.7300020

20

1

19.975

19.9749844

0.0000156

50

49

25.99

9.9498744

16.0401256

50

1

49.99

49.9899990

0.0000010




From the data above, it seems that the greater the difference between C and H, in general, the better the approximation.



Source


Breed, Charles B. and George L. Hosmer. The Principles and Practice of Surveying: Volume I. Elementary Surveying. 7th Edition. John Wiley & sons, Inc. London, Chapman & Hall, Limited. 1938. pg. 14


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.

Casio fx-CG50 and TI-84 Plus CE: Multiple Linear Regression

Casio fx-CG50 and TI-84 Plus CE: Multiple Linear Regression We have arrived at the last month of 2024. What a crazy year. I hope for...