Post Reply 
(19C/29C) (k-)Permutations, Combinations, Linear Regression and Pseudo-random number
01-15-2020, 01:42 PM (This post was last modified: 01-16-2020 05:20 PM by Archilog.)
Post: #1
(19C/29C) (k-)Permutations, Combinations, Linear Regression and Pseudo-random number
Here is a set of routines in 96 steps to add to your HP-19C, HP-29C or HP-29E.

The three first routines (steps 01 to 37) 1, 2 and 3 compute:

- the factorial of an integer between 0 and 69 - or number of permutations of n elements without repetition(*);
- the number of k-permutations from a set of n elements;
- the number of combinations of k elements from a set of n elements.

In this program factorials are computed into the k-permutations routine therefore the two routines (steps 01 to 25) are needed; for the same reason computing combinations needs the whole set (steps 01 to 37); this program does not suit for large N and small K - see below for other references.
Please note that input values are not checked for validity.

(*): Number of permutations of n elements with repetition is given by n^k.

Code:

01 LBL 1 | Permutations or Factorials
02 1
03 X>Y?
04 RTN   | If x=0, then 0!=1
05 X<->Y
06 ENTER
07 LBL 2 | k-permutation: P(n,k)=n!/(n-k)!
08 FIX 0 | will display a rounded number - for some results, the mantissa may differ
09 1
10 STO 1
11 Rd    | Roll down
12 X<->Y
13 -
14 Lst X
15 +
16 STO-0
17 Lst X
18 LBL 0
19 STO*1
20 1
21 -
22 ISZ
23 GTO 0
24 RCL 1
25 RTN
26 LBL 3 | Combination
27 STO 2
28 GSB 2 | uses P(n,k)=n!/(n-k)!
29 RCL 2
30 X<->Y
31 STO 2
32 X<->Y
33 GSB 1 | computes k!
34 RCL 2
35 X<->Y
36 /     | [n!/(n-k)!]/k!
37 RTN
Registers 0, 1 and 2 are used. It is recommended to clear those registers before operation.

Examples:
[0] [GSB] [1]: 0!=1
[1] [2] [GSB] [1]: 12!=479001600
[8] [ENTER] [3] [GSB] [2]: P(8,3)=336
[7] [8] [ENTER] [6] [GSB] [3]: C(78,6)=256851595


Here is a longer version to compute Factorials, Permutations and Combinations with bigger n and smaller k.
Another interesting way to proceed should be to use common logarithms like in this program for HP-25.

The fourth routine (steps 38 to 88) computes a linear regression (y=a1.x + a0) with a0, a1 and coefficient of determination r² respectively in register 3, 4 and 5 from a set of statistical datas (registers 10 to 15); then the program waits for a new variable x to predict y.

Code:

38 LBL 4 | Linear regression
39 ENG 3
40 RCL.5 | Sum{x*y}
41 RCL.1 | Sum{x}
42 RCL.3 | Sum{y}
43 *
44 RCL.0 | n
45 /
46 -     | Sum{x*y}-Sum{x}*Sum{y}/n, also used to calculate r²
47 STO 3
48 RCL.2 | Sum{x²}
49 RCL.1 | Sum{x}
50 x²
51 RCL.0 | n
52 /
53 -     | Sum{x²}-Sum²{x}/n, also used to calculate r²
54 STO 5
55 /     | a1=[Sum{x*y}-Sum{x}*Sum{y}/n]/[Sum{x²}-Sum²{x}/n]
56 STO 4 |
57 RCL 5 | ending r² calculus
58 RCL.4 | Sum{y²}
59 RCL.3 
60 x²    | Sum²{y}
61 RCL.0 | n
62 /
63 -     | 2nd part of denominator
64 *     | denominator completed
65 RCL 3 | numerator
66 x²
67 X<->Y
68 /
69 STO 5 | r²
70 RCL.3 | a0 calculus 
71 RCL.0 | n
72 /
73 RCL.1
74 RCL.0
75 /
76 RCL 4 | a1
77 *
78 -     | a0
79 STO 3 | R3=a0; R4=a1; R5=r²
80 RCL 5
81 PAUSE | displaying r²
82 RCL 3
83 PAUSE | displaying a0 (y for x=0)
84 RCL 4 | displays a1 and waits for new x
85 R/S
86 *
87 +     | executes y=a1.x+a0
88 RTN
Registers 3, 4 and 5 are used.

Example (from HP25 Applications Programs):
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 = a1 C + 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 equation. So tossing both thermometers into a sink of lukewarm water, he reads the following pairs of temperatures as the water cools:

Code:
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 = a1 C + a0, what are the values for a1 and a0? What is the coefficient of determination?

Solution:
In RUN mode, press [f] [Σ],

104.5 [ENTER] 40.5 [Σ+]
102 [ENTER] 38.6 [Σ+]
100 [ENTER] 37.9 [Σ+]
97.5 [ENTER] 36.2 [Σ+]
95.5 [ENTER] 35.1 [Σ+]
94 [ENTER] 34.6 [Σ+]
[GSB] [4]

The calculator displays in engineering notation:
- (961.3 -03);
- a0 (125.8 00);
- a1 (171.5 00), waiting for a new x.

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

Press 37 [R/S] ---------------------> 98.65°F (98.65 00)

It looks he is safe.

You can compute another value by pressing [GSB] [4], entering a new variable x and pressing [R/S].


Pseudo-random number is useful in statistics, here is a quick code to fill the program zone and generate some; you only need to enter a seed [0 ; +1] in X register and to press [GSB] [5] (steps 89 to 96):

Code:

89 LBL 5
90 FIX 2
91 PI
92 +
93 5
94 y^x
95 FRAC
96 RTN
X register displays a pseudo-random number which can be used as seed to generate others.

Examples:
0.5 in X register will generate sequence 0.41, 0.58, 0.45, 0.51, 0.11, 0.26, etc.


Edition: two useless steps were removed from first routine, two links were added.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(19C/29C) (k-)Permutations, Combinations, Linear Regression and Pseudo-random number - Archilog - 01-15-2020 01:42 PM



User(s) browsing this thread: 1 Guest(s)