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
keithdalby
Posts: 564
Joined: Mon Apr 24, 2017 8:38 pm

Linear regression analysis program for DM41(L)

Post by keithdalby »

Had a little fun this morning bashing out a program for the DM41 that I can use in my lab.

EDIT: CODE REMOVED BECAUSE BETTER CODE IS POSTED BELOW.

Edited to add:

I've just written a more sophisticated program for the error estimates. It estimates far lower errors, which might be a good thing by some measures but in a high-school laboratory I actually prefer the errors this program produces, so I'm sticking with it. Yes, it overestimates errors in a rather clunky way, but they are close to estimates made manually from a graph.
Last edited by keithdalby on Thu Feb 01, 2018 12:22 pm, edited 1 time in total.
keithdalby
Posts: 564
Joined: Mon Apr 24, 2017 8:38 pm

Re: Linear regression analysis program for DM41(L)

Post by keithdalby »

A couple of lab programs for your DM41(L), if you're interested. These all use the default stats register numbers. Some of these programs use registers 01-04, but do not clear the registers after their use.

GRD finds the gradient of the X and Y data stored in the stats registers

Code: Select all

LBL "GRD" 
RCL 16
RCL 15
*
RCL 11
RCL 13
*
-
RCL 16
RCL 12
*
RCL 11
X^2
-
/
RTN
INTRCPT finds the y-intercept of the X and Y data stored in the stats registers

Code: Select all

LBL "INTRCPT"
RCL 13
RCL 12
*
RCL 11
RCL 15
*
-
RCL 16
RCL 12
*
RCL 11
X^2
-
/
RTN
PMCC finds the Pearson Product Moment Correlation Coefficient of the X and Y data stored in the stats registers

Code: Select all

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
STERROR estimates the standard error of the X and Y data stored in the stats registers using the PMCC

Code: Select all

LBL "STERROR"
XEQ "PMCC"
X^2
CHS
1
+
RCL 16
2
-
/
SQRT
RTN
STATS just executes the programs above in order to display the gradient, standard error, error in gradient, intercept, standard error (again) and error in intercept sequentially, requiring a R/S press betwixt each. Leaves the gradient on the X register, the intercept on the Y register and the standard error of the regression on the Z register

Code: Select all

LBL "STATS"
"GRADIENT="
AVIEW
XEQ "GRD"
STO 01
ARCL X
AVIEW
STOP
"%ERROR="
AVIEW
XEQ "STERROR"
STO 03
100
*
ARCL X
AVIEW
STOP
"ERROR="
AVIEW
RCL 03
RCL 01
*
ARCL X
AVIEW
STOP
"INTERCEPT="
AVIEW
XEQ "INTRCPT"
STO 02
ARCL X
AVIEW
STOP
"%ERROR="
AVIEW
RCL 03
100
*
ARCL X
AVIEW
STOP
"ERROR="
AVIEW
RCL 03
RCL 02
*
ARCL X
AVIEW
STOP
RCL 03
RCL 02
RCL 01
"END OF PROG"
AVIEW
CLA
END
BEEPER plays a tone every X seconds, so that measurements can be taken at regular times without having to keep an eye on a stopwatch or timer. Accuracy is iffy, but it is good enough for a high school lab. Can accept decimal seconds, doesn't need integer values

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
OSCILLS asks how many oscillations to time for, then each press of the R/S key increases the counter until that number is met. At that point, a tone sounds (so you don't have to watch the screen) to indicate you have counted the appropriate number of oscillations. After this, presses of R/S display the total time expired and the time period of the oscillations. With time periods less than a second it can be iffy. Leaves the time period on the X register and the total time on the Y register

Code: Select all

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
STDEV estimates the standard deviation in X and Y from the stat registers, then leaves the standard deviation of X on the X register and of Y on the Y register

Code: Select all

LBL "STDEV"
RCL 12
RCL 16
1
-
/
SQRT
STO 01
RCL 14
RCL 16
1
-
/
SQRT
RCL 01
RTN
I also think it's handy to ASN the SW to a key too, as well as the above programs, for use in the lab. Hope you find them useful!

EDIT TO ADD: Tweaked some programs
Last edited by keithdalby on Thu Feb 01, 2018 11:42 am, edited 2 times in total.
User avatar
akaTB
Posts: 794
Joined: Tue May 02, 2017 1:56 pm
Location: Milan, Italy

Re: Linear regression analysis program for DM41(L)

Post by akaTB »

keithdalby wrote:
Thu Feb 01, 2018 8:45 am
A couple of lab programs for your DM41(L), if you're interested. These all use the default stats register numbers. Some of these programs use registers 01-04, but do not clear the registers after their use.

GRAD finds the gradient of the X and Y data stored in the stats registers
Maybe you could choose a different name for the label, in order to differentiate it from the internal GRAD[ians] command?
Greetings,
    Massimo
ajcaton
-+×÷ left is right and right is wrong :twisted: Casted in gold
keithdalby
Posts: 564
Joined: Mon Apr 24, 2017 8:38 pm

Re: Linear regression analysis program for DM41(L)

Post by keithdalby »

akaTB wrote:
Thu Feb 01, 2018 9:27 am
keithdalby wrote:
Thu Feb 01, 2018 8:45 am
A couple of lab programs for your DM41(L), if you're interested. These all use the default stats register numbers. Some of these programs use registers 01-04, but do not clear the registers after their use.

GRAD finds the gradient of the X and Y data stored in the stats registers
Maybe you could choose a different name for the label, in order to differentiate it from the internal GRAD[ians] command?
Good point. I never use gradians so it doesn't affect me, but feel free to change it if you wish to use it for your own purposes :)

EDITED TO ADD: I've gone through and changes it from GRAD to GRD. Thanks for the advice.
toml_12953
Posts: 795
Joined: Wed May 03, 2017 7:46 pm
Location: Malone, NY USA

Re: Linear regression analysis program for DM41(L)

Post by toml_12953 »

I tried using the DM41 Programming Tool to convert the listing to a file I could send to the 41. I get an error in line 1, LBL GRAD. I tried changing it to LBL GRD but it didn't help. Does anyone know what's going on?

http://dm41.swissmicros.com/
Tom L

Some people call me inept but I'm as ept as anybody!
DM10L SN: 059/100
DM41X SN: 00023 (Beta)
DM41X SN: 00506 (Shipping)
DM42 SN: 00025 (Beta)
DM42 SN: 00221 (Shipping)
WP43 SN: 00025 (Prototype)
User avatar
akaTB
Posts: 794
Joined: Tue May 02, 2017 1:56 pm
Location: Milan, Italy

Re: Linear regression analysis program for DM41(L)

Post by akaTB »

keithdalby wrote:
Thu Feb 01, 2018 11:05 am

EDITED TO ADD: I've gone through and changes it from GRAD to GRD. Thanks for the advice.
;)
Thank you for your programs.
Greetings,
    Massimo
ajcaton
-+×÷ left is right and right is wrong :twisted: Casted in gold
User avatar
akaTB
Posts: 794
Joined: Tue May 02, 2017 1:56 pm
Location: Milan, Italy

Re: Linear regression analysis program for DM41(L)

Post by akaTB »

toml_12953 wrote:
Thu Feb 01, 2018 11:55 am
I tried using the DM41 Programming Tool to convert the listing to a file I could send to the 41. I get an error in line 1, LBL GRAD. I tried changing it to LBL GRD but it didn't help. Does anyone know what's going on?

http://dm41.swissmicros.com/
Just tried with the updated version and it didn't give me any error.
Greetings,
    Massimo
ajcaton
-+×÷ left is right and right is wrong :twisted: Casted in gold
toml_12953
Posts: 795
Joined: Wed May 03, 2017 7:46 pm
Location: Malone, NY USA

Re: Linear regression analysis program for DM41(L)

Post by toml_12953 »

akaTB wrote:
Thu Feb 01, 2018 11:59 am
toml_12953 wrote:
Thu Feb 01, 2018 11:55 am
I tried using the DM41 Programming Tool to convert the listing to a file I could send to the 41. I get an error in line 1, LBL GRAD. I tried changing it to LBL GRD but it didn't help. Does anyone know what's going on?

http://dm41.swissmicros.com/
Just tried with the updated version and it didn't give me any error.
Thanks for responding so quickly! I found the problem. The converter needs quotes around labels. I didn't add them so all lines referring to labels were in error. Also the listing here uses x for multiply. The converter uses *.

Here's the listing in a format the converter can use:

Code: Select all

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 "STERROR"
XEQ "PMCC"
X^2
CHS
1
+
RCL 16
2
-
/
SQRT
RTN
LBL "STATS"
CLA
"GRADIENT="
AVIEW
XEQ "GRD"
STOP
CLA
"%ERROR="
AVIEW
XEQ "STERROR"
100
*
STOP
CLA
"ERROR="
AVIEW
XEQ "GRD"
STO 01
XEQ "STERROR"
RCL 01
*
STOP
CLA
"INTERCEPT="
AVIEW
XEQ "INTRCPT"
STOP
CLA
"%ERROR="
AVIEW
XEQ "STERROR"
100
*
STOP
CLA
"ERROR="
AVIEW
XEQ "INTRCPT"
STO 02
XEQ "STERROR"
RCL 02
*
STOP
RCL 02
RCL 01
CLA
"END OF PROG"
AVIEW
CLA
RTN
END
Tom L

Some people call me inept but I'm as ept as anybody!
DM10L SN: 059/100
DM41X SN: 00023 (Beta)
DM41X SN: 00506 (Shipping)
DM42 SN: 00025 (Beta)
DM42 SN: 00221 (Shipping)
WP43 SN: 00025 (Prototype)
keithdalby
Posts: 564
Joined: Mon Apr 24, 2017 8:38 pm

Re: Linear regression analysis program for DM41(L)

Post by keithdalby »

toml_12953 wrote:
Thu Feb 01, 2018 12:03 pm
Thanks for responding so quickly! I found the problem. The converter needs quotes around labels.
Ah, I see. The first code from the original post was typed by hand. The rest of the code was spat out by the decoder, so the labels are correctly given.
akaTB wrote:
Thu Feb 01, 2018 11:56 am
Thank you for your programs.
You're very welcome. I'm sorry they are so crudely coded.
keithdalby
Posts: 564
Joined: Mon Apr 24, 2017 8:38 pm

Re: Linear regression analysis program for DM41(L)

Post by keithdalby »

Right, after a bit of revising statistics and digging deeper, I've conjured up the following two equations to give standard deviations of slopes and intercepts for straight lines:
IMG_20180202_110833~2.jpg
IMG_20180202_110833~2.jpg (94.74 KiB) Viewed 7071 times
And RPNed it:
IMG_20180202_113135~2.jpg
IMG_20180202_113135~2.jpg (72.7 KiB) Viewed 7071 times
So now I have to throw it at the DM41 and see if it works. I've written a spreadsheet that uses a crude Monte Carlo method to estimate errors in gradients and interprets, and I'm really hoping this will agree. I'll let you know when I've had a chance to key it in.

(Notation, m=gradient - stored in register 01, c=intercept - stored in register 02. Statistics registers have their default numbers)

(Credit to here for the statistics help: http://pages.mtu.edu/~fmorriso/cm3215/U ... resFit.pdf)

EDIT TO ADD: ARRRRGGG, about half way through the second line of the \sigma m function theres a rcl 16 that pushes a number over the top of the four stacks. Need to re-write it to avoid stack overflow. Sorry for getting your hopes up (he says, shouting into the abyss because no-body really cares)
Last edited by keithdalby on Fri Feb 02, 2018 12:50 pm, edited 1 time in total.
Post Reply