Meteorology & Markov Chains for the HP-41
Overview
-We assume that the weather on day j+1 only depends on the weather on
day j ( Markov property )
-This is perhaps not completely true but it's a good approximation.
-The following routine simply computes the Transition Matrix from observations
of N consecutive days
-The weather is defined by n states, for example 1 = sunny day
, 2 = cloudy ( without rain ) , 3 = rainy day
( do not use 0 )
Program Listing
Data Registers: • R00 = n = number of states ( Registers R00 and Rbb thru Ree are to be initialized before executing "METEO" )
• Rbb = x1 ...................... • Ree = xN bb > n2+n Rn2+1 to Rn2+n: temp
>>>> When the program stops, R01 thru Rn2 contain the elements of the Transition Matrix
Flags: /
Subroutines: /
-Lines 02 and 53-54 are only useful to save bbb.eee
otherwise, they can be deleted
-If you don't have an HP-41CX, replace line 10 by SIGN
CLX LBL 00 STO IND L ISG L GTO 00
01 LBL "METEO"
02 STO M 03 RCL 00 04 X^2 05 LASTX 06 + 07 E3 08 / 09 ISG X 10 CLRGX 11 X<>Y |
12 LBL 01
13 RCL IND X 14 ENTER^ 15 ISG Z 16 X=0? 17 GTO 02 18 RCL 00 19 ST* Y 20 - 21 RCL IND Z 22 + |
23 X<>Y
24 RCL 00 25 X^2 26 + 27 SIGN 28 ST+ IND Y 29 ST+ IND L 30 R^ 31 GTO 01 32 LBL 02 33 RCL 00 |
34 ENTER^
35 X^2 36 .1 37 % 38 + 39 ST+ Y 40 LBL 03 41 RCL 00 42 E3 43 / 44 - |
45 LBL 04
46 RCL IND Z 47 ST/ IND Y 48 RDN 49 DSE X 50 GTO 04 51 DSE Y 52 GTO 03 53 CLX 54 X<> M 55 END |
( 87 bytes )
STACK | INPUT | OUTPUT |
X | bbb.eee | bbb.eee |
with bbb > n2+n
Example: You observe the weather during 21 consecutive days, it yields: 112212333231122322121 ( 1 = Sun , 2 = Clouds without Rain , 3 = Rain )
n = 3 STO 00 and store these 21 integers into registers R13 R14 .............. R33 control number = 13.033
13.033 XEQ "METEO" >>>> 13.035 --- Execution time = 15s ---
And we get:
R01 R04 R07
1/3 1/3 1/5
R02 R05 R08
= 2/3 1/3 2/5 =
Transition Matrix = T
R03 R06 R09
0 1/3 2/5
-What will be tomorrow's weather ?
1- If today is a sunny day, look at the 1st column:
the probability that
tomorrow will be sunny is 1/3
the probability that
tomorrow will be cloudy is 2/3
the probability that
tomorrow will be rainy is 0
2- If today is a cloudy day, look at the 2nd column:
the probability that tomorrow
will be sunny is 1/3
the probability that tomorrow
will be cloudy is 1/3
the probability that tomorrow
will be rainy is 1/3
3- If today is a rainy day, look at the 3rd column:
the probability that tomorrow
will be sunny is 1/5
the probability that tomorrow
will be cloudy is 2/5
the probability that tomorrow
will be rainy is 2/5
Notes:
-The sum of the elements of each column is always 1
-Limit of Tk as k tends to infinity =
0.3 0.3
0.3
0.45 0.45
0.45
0.25 0.25
0.25
-Using one register to store just a one-digit number is of course wasteful.
-With the following variant, you can store the data by groups of up
to 10 states, provided that n < 10
-If you don't have an HP-41CX, replace line 16 by SIGN CLX
LBL 00 STO IND L ISG L GTO 00
01 LBL "METEO"
02 STO N 03 INT 04 DSE X 05 LASTX 06 FRC 07 RCL 00 08 X^2 09 LASTX 10 + 11 E3 12 ST* Z 13 ST/ T 14 / 15 ISG X 16 CLRGX |
17 RDN
18 + 19 STO M 20 RCL IND X 21 STO Y 22 10 23 ST/ Z 24 MOD 25 X<>Y 26 LBL 01 27 INT 28 X=0? 29 GTO 02 30 RCL X 31 10 32 ST/ Z |
33 MOD
34 STO O 35 RCL 00 36 ST* Y 37 - 38 ST+ Z 39 SIGN 40 ST+ IND Z 41 RCL 00 42 X^2 43 RCL O 44 + 45 X<>Y 46 ST+ IND Y 47 LASTX 48 R^ |
49 GTO 01
50 LBL 02 51 DSE M 52 X<0? 53 GTO 02 54 CLX 55 RCL IND M 56 GTO 01 57 LBL 02 58 RCL 00 59 ENTER^ 60 X^2 61 .1 62 % 63 + 64 ST+ Y |
65 LBL 03
66 RCL 00 67 E3 68 / 69 - 70 LBL 04 71 RCL IND Z 72 ST/ IND Y 73 RDN 74 DSE X 75 GTO 04 76 DSE Y 77 GTO 03 78 RCL N 79 CLA 80 END |
( 127 bytes )
STACK | INPUT | OUTPUT |
X | bbb.eee | bbb.eee |
with bbb > n2+n
Example: With the same observations, 112212333231122322121 may be stored as follows:
112212333 STO 13
1122123332 STO 13
231122322 STO 14
or 3112232212 STO
14 and many other ways
121
STO 15
1 STO
15
-In both cases, 13.015 XEQ "METEO"
gives 13.015 ( in 18 seconds ) and the same transition
matrix.
Note:
The weather model may of course be improved, for instance:
1 = Sun without wind
2 = Sun & wind
3 = Cloud ( without rain ) without wind
4 = Cloud & wind ( without rain )
5 = Rain without wind
6 = Rain & wind
... and so on ...