## Linear regression analysis program for DM41(L)

This is the place to discuss the usage of DM1x/DM41 machines and to share any tips & tricks you may have stumbled across.

If discussing a specific model, please ensure that you prefix topic subjects with the model number this way:

[DM41] How to change the number of program steps available
Walter
Posts: 1000
Joined: Tue May 02, 2017 9:13 am
Location: Close to FRA, Germany

### Re: Linear regression analysis program for DM41(L)

For comparison, the following equations apply:
errors_LR_a.gif (3.54 KiB) Viewed 1688 times
HTH
DM42 SN: 00041 --- Follower of Platon.

HP-35, HP-45, ..., HP-50, WP 34S, WP 31S, DM16L

keithdalby
Posts: 539
Joined: Mon Apr 24, 2017 6:38 pm

### Re: Linear regression analysis program for DM41(L)

Walter wrote:
Fri Feb 02, 2018 11:49 am
For comparison, the following equations apply
I recall we've been here before with the DM42 code I was working on. I think last time we'd concluded that the shorter equation was the same as the mess I've scribbled on the whiteboard, but it was so long ago, my memory is failing me. Thank you for resharing though.

My intention is to write these statistics programs for the DM41 so that I don't have to dig into statistics ever again. It's a nuisance to have to look up if Sx means standard error in x or standard deviation of x or some other exotic statistical quantity of x each time I want to analyse data. I'm not a clever man, so exerting my brain once to get something that works is enough for me. I've no ambitions to master statistics. Or algebra. Or mathematics in general. I'm just not clever enough.

I've solved the stack overflow problem, by the way. Pretty easy to fix. Need to find time to key it into the encoder on the website so I can flash it to the DM41 and see if it works.

keithdalby
Posts: 539
Joined: Mon Apr 24, 2017 6:38 pm

### Re: Linear regression analysis program for DM41(L)

It's all in here, typed by not tested:

Code: Select all

``````LBL "BEEPER"
"INTERVAL?="
PROMPT
STO 01
TIME  ;XROM 26,28
HR
3600
*
STO 02
0
STO 03
LBL 01
RCL 01
RCL 03
*
RCL 02
+
TIME  ;XROM 26,28
HR
3600
*
X<=Y?
GTO 01
TONE 06
"N="
ARCL 03
AVIEW
1
ST+ 03
GTO 01
END
LBL "OSCILLS"
"OSCILLATIONS?="
PROMPT
STO 01
0
STO 02
TIME  ;XROM 26,28
HR
3600
*
STO 03
LBL 01
"N="
RCL 02
ARCL 02
AVIEW
STOP
1
ST+ 02
RCL 02
RCL 01
X>Y?
GTO 01
TIME  ;XROM 26,28
HR
3600
*
RCL 03
-
STO 04
TONE 05
"TOTAL TIME="
ARCL 04
AVIEW
STOP
"PERIOD="
AVIEW
RCL 04
RCL 01
/
ARCL X
AVIEW
STOP
RCL 04
RCL 04
RCL 01
/
END
LBL "GRD"
RCL 16
RCL 15
*
RCL 11
RCL 13
*
-
RCL 16
RCL 12
*
RCL 11
X^2
-
/
RTN
LBL "INTRCPT"
RCL 13
RCL 12
*
RCL 11
RCL 15
*
-
RCL 16
RCL 12
*
RCL 11
X^2
-
/
RTN
LBL "PMCC"
RCL 16
RCL 15
*
RCL 11
RCL 13
*
-
RCL 16
RCL 12
*
RCL 11
X^2
-
RCL 16
RCL 14
*
RCL 13
X^2
-
*
SQRT
/
RTN
LBL "STDEV"
RCL 12
RCL 16
1
-
/
SQRT
STO 01
RCL 14
RCL 16
1
-
/
SQRT
RCL 01
RTN
LBL "STDVGRD"
XEQ "GRD"
STO 01
XEQ "INTRCPT"
STO 02
RCL 14
2
RCL 01
RCL 15
*
*
-
2
RCL 02
RCL 13
*
*
+
RCL 01
X^2
RCL 12
*
+
2
RCL 01
RCL 02
*
*
RCL 11
*
-
RCL 01
X^2
+
RCL 16
2
-
/
X^2
SQRT
RCL 13
RCL 16
/
X^2
RCL 14
RCL 13
RCL 16
/
RCL 2
*
RCL 13
*
-
/
SQRT
RTN
LBL "STDVITR"
XEQ "GRD"
STO 01
XEQ "INTRCPT"
STO 02
RCL 14
2
RCL 01
RCL 15
*
*
-
2
RCL 02
RCL 13
*
*
+
RCL 01
X^2
RCL 12
*
+
2
RCL 01
RCL 02
*
*
RCL 11
*
-
RCL 02
X^2
+
RCL 16
2
-
/
X^2
SQRT
RCL 12
RCL 16
/
RCL 13
RCL 16
/
X^2
RCL 14
+
RCL 13
RCL 16
/
2
*
RCL 13
*
-
/
SQRT
RTN
LBL "STATS"
AVIEW
XEQ "GRD"
STO 01
ARCL X
AVIEW
STOP
"ERROR="
AVIEW
XEQ "STDVGRD"
RCL 16
SQRT
/
ARCL X
AVIEW
STOP
"INTERCEPT="
AVIEW
XEQ "INTRCPT"
STO 02
ARCL X
AVIEW
STOP
"ERROR="
AVIEW
XEQ "STDVITR"
RCL 16
SQRT
/
ARCL X
AVIEW
STOP
RCL 02
RCL 01
"END OF PROG"
AVIEW
CLA
END``````
Encoded version to flash:

Code: Select all

``````DM41
08  4b000000000000  00000000000000  00000000000000  00000000000000
0c  1000000000019c  1a70016919c149  0000002c048000  00000000000000
148  00000000000000  87000000cc112f  462050524f477e  21fb454e44204f
14c  52439b737e8422  44564954529010  523d7e1ef75354  7e84f64552524f
150  52435054329b73  3d7e1ef7494e54  54455243455054  9b737e84fa494e
154  47524490105243  7e1ef753544456  f64552524f523d  5244319b737e84
158  4e543d7e1ef347  f9475241444945  f6005354415453  4241435285c40c
15c  2d90104312422d  2d901043512e40  4351522c901043  22514090101241
160  212242422b4241  4021512c424012  424112222d4242  54322e12212f42
164  f7494e54524350  1ef3475244311e  53544456495452  435285c80bf800
168  104322422d4241  901043512e2d90  1012414351522d  2b424121514090
16c  42401221224242  2d42424021512c  212f4242411222  52435054322e12
170  44311ef7494e54  4752441ef34752  03f80053544456  114143522185ca
174  414352312e9010  4445562c901011  85cc04f6005354  422d5141425243
178  422b514190102e  2b2d424190102c  4d434390102f42  4385cc03f50050
17c  90102c422b5141  2d2c422b2f4241  494e5452435054  414385c603f800
180  4190102c422b51  90102f422b2d42  c600f400475244  24242143c00f09
184  2421439b737e84  4552494f443d7e  3d9b047e84f750  414c2054494d45
188  349f05fb544f54  13161010422341  2145b200a69c6d  027e8411920222
18c  3302f24e3d229b  9c6d1316101042  3f3d8e311032a6  4c4154494f4e53
190  53fe4f5343494c  004f5343494c4c  00c20909c600f8  9b037e119203b2
194  b2009f06f24e3d  6d131610104246  2123422240a69c  10104232103302
198  8e31a69c6d1316  455256414c3f3d  504552fa494e54  c000f700424545
A: 00000000c00020  B: 0000002c0480fd  C: 0000002c0480fd
M: 00011cd5ff73cb  N: 000000000000c0  G: 00``````
I will never understand how so many lines of code can be compressed to so little memory. I'll test this tonight when I get home. Presented here untested. Operates very similarly to how the previously posted code does, with obvious changes in the STATS program and the guff that is the "STERROR" program has been removed.

Labels are as follows:
BEEPER plays a tone every X seconds.

OSCILLS starts a timer, then counts how many times R/S is pressed up to the pre-defined limit. Returns the time taken for all iterations and the time period of each iteration.

GRD returns the gradient of the linear regression data from the statistical registers.

INTRCPT returns the intercept of the linear regression data from the statistical registers.

PMCC returns the PMCC of the linear regression data in the statistical registers.

STDEV returns the standard deviation of X and Y on the stack levels X and Y respectively, using the data in the statistical registers.

STDVGRD returns the standard deviation of the gradient of the linear regression data in the statistical registers (I hope).

STDVITR returns the standard deviation of the intercept of the linear regression data in the statistical registers (I hope).

STATS gives the gradient, the standard error in the gradient, the intercept and the standard error of the intercept sequentially with R/S presses (I hope).

toml_12953
Posts: 507
Joined: Wed May 03, 2017 5:46 pm
Location: Malone, NY USA

### Re: Linear regression analysis program for DM41(L)

keithdalby wrote:
Fri Feb 02, 2018 11:38 am
Sorry for getting your hopes up (he says, shouting into the abyss because no-body really cares)
Huh? We all care! I am, however, old. I can neither get my hopes, nor anything else, up without difficulty.
Tom L

Nerd Emeritus

DM42 SN: 00025 (Beta)
SN: 00221 (Shipping)

keithdalby
Posts: 539
Joined: Mon Apr 24, 2017 6:38 pm

### Re: Linear regression analysis program for DM41(L)

keithdalby wrote:
Fri Feb 02, 2018 12:30 pm
I'll test this tonight when I get home. Presented here untested.
Tested, and doesn't work at all. Errors much bigger than expected by several orders of magnitude. Back to the drawing board.

I appear to have transcribed the equations to RPN incorrectly. Just threw them on Excel and used the same data. Got the same gradient but different errors. No reason why that should happen. Haven't tested intercept yet. Doesn't matter, if it's an RPN transcription mistake, I can easily fix that.

EDIT TO ADD: nope, noticed another mistake too. I think it'll be straightforward for me to unpick, but it'll need more than a few quick minutes so I'll put it away for tonight.

keithdalby
Posts: 539
Joined: Mon Apr 24, 2017 6:38 pm

### Re: Linear regression analysis program for DM41(L)

Found what I need: http://www.namirshammas.com/HP41/LinRegAndAnova41.pdf

I'll implement this into my own code later today and cross check it with Excel. No time now.

keithdalby
Posts: 539
Joined: Mon Apr 24, 2017 6:38 pm

### Re: Linear regression analysis program for DM41(L)

Finally done it, and it works. It actually works. With real data and everything.

The code for the lab toys:

Code: Select all

``````LBL "BEEPER"
"INTERVAL?="
PROMPT
STO 01
TIME  ;XROM 26,28
HR
3600
*
STO 02
0
STO 03
LBL 01
RCL 01
RCL 03
*
RCL 02
+
TIME  ;XROM 26,28
HR
3600
*
X<=Y?
GTO 01
TONE 06
"N="
ARCL 03
AVIEW
1
ST+ 03
GTO 01
END
LBL "OSCILLS"
"OSCILLATIONS?="
PROMPT
STO 01
0
STO 02
TIME  ;XROM 26,28
HR
3600
*
STO 03
LBL 01
"N="
RCL 02
ARCL 02
AVIEW
STOP
1
ST+ 02
RCL 02
RCL 01
X>Y?
GTO 01
TIME  ;XROM 26,28
HR
3600
*
RCL 03
-
STO 04
TONE 05
"TOTAL TIME="
ARCL 04
AVIEW
STOP
"PERIOD="
AVIEW
RCL 04
RCL 01
/
ARCL X
AVIEW
STOP
RCL 04
RCL 04
RCL 01
/
END
LBL "GRD"
RCL 16
RCL 15
*
RCL 11
RCL 13
*
-
RCL 16
RCL 12
*
RCL 11
X^2
-
/
RTN
LBL "INTRCPT"
RCL 13
RCL 12
*
RCL 11
RCL 15
*
-
RCL 16
RCL 12
*
RCL 11
X^2
-
/
RTN
LBL "PMCC"
RCL 16
RCL 15
*
RCL 11
RCL 13
*
-
RCL 16
RCL 12
*
RCL 11
X^2
-
RCL 16
RCL 14
*
RCL 13
X^2
-
*
SQRT
/
RTN
LBL "STDEV"
RCL 12
RCL 16
1
-
/
SQRT
STO 01
RCL 14
RCL 16
1
-
/
SQRT
RCL 01
RTN
LBL "STERGRD"
XEQ "GRD"
STO 01
RCL 14
RCL 13
X^2
RCL 16
/
-
RCL 15
RCL 11
RCL 13
*
RCL 16
/
-
RCL 01
*
-
RCL 16
2
-
/
X^2
SQRT
RCL 16
/
RCL 12
RCL 11
X^2
RCL 16
/
-
/
SQRT
RTN
LBL "STERITR"
XEQ "GRD"
STO 01
RCL 14
RCL 13
X^2
RCL 16
/
-
RCL 15
RCL 11
RCL 13
*
RCL 16
/
-
RCL 01
*
-
RCL 16
2
-
/
X^2
SQRT
RCL 11
RCL 16
/
X^2
RCL 11
X^2
RCL 16
/
CHS
RCL 12
+
/
RCL 16
1/X
+
RCL 16
/
*
SQRT
RTN
LBL "STATS"
AVIEW
XEQ "GRD"
STO 01
ARCL X
AVIEW
STOP
"ERROR="
AVIEW
XEQ "STERGRD"
ARCL X
AVIEW
STOP
"INTERCEPT="
AVIEW
XEQ "INTRCPT"
STO 02
ARCL X
AVIEW
STOP
"ERROR="
AVIEW
XEQ "STERITR"
ARCL X
AVIEW
STOP
RCL 02
RCL 01
"END OF PROG"
AVIEW
CLA
END``````
The calculator dump for the above code:

Code: Select all

``````DM41
08  4b000000000000  00000000000000  00000000000000  00000000000000
0c  1000000000019c  1a70016919c151  0000002c048000  00000000000000
150  00000000000000  477e8700c6102f  204f462050524f  842221fb454e44
154  524954529b737e  3d7e1ef7535445  84f64552524f52  435054329b737e
158  7e1ef7494e5452  4552434550543d  737e84fa494e54  5445524752449b
15c  4f523d7e1ef753  737e84f6455252  1ef3475244319b  4449454e543d7e
160  415453f9475241  85c609f6005354  60409010434252  43542c40439010
164  1043512b519010  12414351522b90  43412142419010  412f2b2d429010
168  312e2d51901043  54521ef3475244  f8005354455249  4341435285cc07
16c  10432c2b519010  10124143515290  10434121424190  43412f2b2d4290
170  44312e2d519010  4752441ef34752  03f80053544552  114143522185ca
174  414352312e9010  4445562c901011  85cc04f6005354  422d5141425243
178  422b514190102e  2b2d424190102c  4d434390102f42  4385cc03f50050
17c  90102c422b5141  2d2c422b2f4241  494e5452435054  414385c603f800
180  4190102c422b51  90102f422b2d42  c600f400475244  24242143c00f09
184  2421439b737e84  4552494f443d7e  3d9b047e84f750  414c2054494d45
188  349f05fb544f54  13161010422341  2145b200a69c6d  027e8411920222
18c  3302f24e3d229b  9c6d1316101042  3f3d8e311032a6  4c4154494f4e53
190  53fe4f5343494c  004f5343494c4c  00c20909c600f8  9b037e119203b2
194  b2009f06f24e3d  6d131610104246  2123422240a69c  10104232103302
198  8e31a69c6d1316  455256414c3f3d  504552fa494e54  c000f700424545
A: 00000000c00020  B: 0000002c0480fd  C: 0000002c0480fd
M: 00011cd5ff73cb  N: 000000000000c0  G: 00``````
Labels as follows:
BEEPER plays a tone every X seconds.

OSCILLS starts a timer, then counts how many times R/S is pressed up to the pre-defined limit. Returns the time taken for all iterations and the time period of each iteration.

GRD returns the gradient of the linear regression data from the statistical registers.

INTRCPT returns the intercept of the linear regression data from the statistical registers.

PMCC returns the PMCC of the linear regression data in the statistical registers.

STDEV returns the standard deviation of X and Y on the stack levels X and Y respectively, using the data in the statistical registers.

STERGRD returns the standard error of the gradient of the linear regression data in the statistical registers.

STERITR returns the standard error of the intercept of the linear regression data in the statistical registers.

STATS gives the gradient, the standard error in the gradient, the intercept and the standard error of the intercept sequentially with R/S presses.

And did I mention that it works?

Thanks to http://pages.mtu.edu/~fmorriso/cm3215/U ... resFit.pdf and http://www.namirshammas.com/HP41/LinRegAndAnova41.pdf for the help (but still had to do quite a bit of trudging to get it to work). I intend to tweak this for the DM42 lab toys I've got at a later date.