Post Reply 
(HP 41) (DM 41X) Stratified Random Sampling
10-10-2020, 12:30 PM
Post: #1
(HP 41) (DM 41X) Stratified Random Sampling
Hi,

This is my first program for the HP 41 since decades. So it may be that there are numerous ways to improve the code.
I have tested it on a DM 41X and a HP 41CL.

What it does:

The program helps to analyze stratified random samples.
You can either put in the weights (i.e. the shares of the strata in universe) and the standard deviations within the strata or you can put in a density function and the weights of the strata and the standard deviations within the strata are computed.
For the allocation of the sample on the strata you can put in numbers or let the program compute sample sizes for proportional or optimal allocation.
Standard deviation of total mean is computed.

Needs command “INTEG” from Advantage Module.

Use of registers:

00 Final result
01 Name of density function global label (Alpha)
02 Number of strata (maximum: 6)
03 Sample size
04 Lower limit of first stratum

Stratum 1 2 3 4 5 6
Upper limit 05 06 07 08 09 10
Weights 11 12 13 14 15 16
Std. dev. 17 18 19 20 21 22

Register 23-30 are used for computations.
Program INTEG from ADVANTAGE module needs another 32 unused program registers.
Usage instructions: See program listing
Example:
You want to analyze a stratified sample with 4 strata and optimum allocation of sample. The variable has a standard normal distribution. Strata limits are -1, 0, 1. Total sample size is n=1000.
For convenience you take as lower limit of standard normal distribution -7.5 and as upper limit 7.5.
First type in routine for normal distribution under global label:
01 LBL “NV”
02 x²
03 2
04 /
05 CHS
06 e^x
07 2
08 PI
09 *
10 SQRT
11 /
12 RTN
XEQ “STRAT”
-> SMPL SZ? – 1000 R/S (sample size input)
-> N STRATA? – 4 R/S (input of number of strata)
-> 0=I 1=DF? – 1 R/S (we use densitiy function, so we type 1)
-> NM DF? – NV R/S (input of label of program with density function)
-> LL S1? – 7.5 CHS R/S (lower limit of first stratum is -7.5)
-> UL S1? – 1 CHS R/S (upper limit of first stratum is -1)
-> UL S2? – 0 R/S
-> UL S3? – 1 R/S
-> UL S4? – 7.5 R/S

After about 15 minutes (HP 41) / 20 seconds (DM 41X with power supply):
-> 0=I 1=P 2=O? – 2 R/S (we want optimum allocation, so we type 2)
-> N1=212 (sample size in stratum 1)
R/S -> N2 = 288 (sample size in stratum 2)
R/S -> N3 = 288
R/S -> N$ = 212
R/S -> 0.0106 (std deviation of stratified sample mean)

Weights and standard deviations can be seen in registers, e.g. weight in stratum 1: RCL 11 -> 0.1587

Code:
01 LBL STRAT
02 „SMPL SZ?”
03 PROMPT            Input of sample size
04 STO 03
05 “N STRATA?”
06 PROMPT            Input of number of strata (maximum: 6)
07 1000
08 /
09 1
10 +
11 STO 02
12 0
13 STO 00
14 STO 27
15 “0=I 1=DF?”            Input whether weights and std. dev. of strata are individually typed
16 PROMPT            (0) or whether they are computed by given density function (1)
17 4
18 +
19 XEQ IND X
20 “0=I 1=P 2=O?”        Input whether allocation of sample on strata should be individually
21 PROMPT            given (0), proportional to weights (1) or optimal (2)
22 STO 30
23 FIX 0
24 10
25 STO 28
26 16
27 STO 29
28 LBL 08            Determination of sample size in strata
29 1
30 ST+ 28
31 ST+ 29
32 RCL IND 28
33 RCL IND 29
34 XEQ IND 30
35 RCL IND 28
36 RCL IND 29
37 *
38 x²
39 x<>y
40 /
41 ST+ 00
42 ISG 02
43 GTO 08
44 FIX 4
45 RCL 00
46 SQRT
47 STO 00            Standard dev. of mean of total sample is shown
48 CF 01
49 RTN
50 LBL 04            Manual Input of weights and std. dev. in strata
51 10
52 STO 28
53 16 
54 STO 29
55 FIX 0
56 LBL 06
57 1 
58 ST+ 28
59 ST+ 29
60 “W”
61 ARCL 02
62 “|-?”
63 PROMPT
64 STO IND 28
65 “S”
66 ARCL 02
67 “|-?
68 PROMPT
69 STO IND 29
70 *
71 ST+ 27
72 ISG 02
73 GTO 06
74 XEQ 09
75 RTN
76 LBL 05            Computation of weights and std. dev. in strata with given
77 AON                function
78 FIX 0
79 “NM DF?”            Asks for name of global program label with density function
80 PROMPT
81 ASTO 01
82 AOFF
83 4
84 STO 28
85 “LL S1?”            Asks for lower limit of stratum 1
86 PROMPT
87 STO 04
88 STO 23
89 LBL 07
90 1
91 ST+ 28
92 “UL S”
93 ARCL 02
94 “|-?”
95 PROMPT            Asks for upper limit of stratum l
96 STO IND 28
97 ISG 02
98 GTO 07
99 FIX 5                Format defines accuracy of integration
100 4
101 STO 28
102 10
103 STO 29
104 XEQ 09
105 LBL 10            Computation of parameters of strata
106 1
107 ST+ 28
108 ST+ 29
109 CLA
110 CF 01
111 ARCL 01
112 RCL 23
113 RCL IND 28
114 STO 24
115 INTEG            Computation of weight of stratum l = integral(f(x))
116 STO IND 29
117 STO 25
118 “STRAX”
119 RCL 23
120 RCL 24
121 INTEG            Computation of integral(x*f(x))
122 RCL 25
123 /
124 STO 26            Mean in stratum
125 SF 01
126 RCL 23
127 RCL 24
128 STO 23
129 INTEG            Computation of integral(x²*f(x))
130 RCL 25
131 /
132 RCL 26
133 x²
134 -
135 SQRT            std. dev. in stratum
136 RCL 29
137 6
138 +
139 x<>y
140 STO IND Y
141 RCL 25
142 *                weight in stratum * std. dev. in stratum
143 ST+ 27
144 ISG 02
145 GTO 10
146 XEQ 09
147 RTN
148 LBL 09            Refreshing loop register 02
149 RCL 02
150 FRC
151 1
152 +
153 STO 02
154 RTN
155 LBL 00            Manual input of sample size in stratum
156 “N”
157 ARCL 02
158 “|-?”
159 PROMPT
160 RTN
161 LBL 01            Computation of proportional sample size in stratum
162 X<>Y            (share of stratum in sample and in universe are equal)
163 RCL 03
164 *
165 GTO 03
166 LBL 02            Computation of optimum sample size in stratum 
167 *                (proportional to weight*std. dev in stratum)
168 RCL 27
169 /
170 RCL 03
171 *
172 LBL 03            Output of sample size in stratum
173 AON
174 “N”
175 ARCL 02
176 “|-=”
177 ARCL X
178 STOP
179 AOFF
180 RTN
181 END
01 LBL “STRAX”            Subroutine for computation of integral x*f(x) and x²*f(x)
02 STO 30            depending of status of flag 01
03 XEQ IND 01
04 RCL 30
05 FS? 01
06 x²
07 *
08 RTN
09 END
Find all posts by this user
Quote this message in a reply
Post Reply 




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