Sunday, October 22, 2023

HP 15C: Transition Matrix Test and Taking a Square Matrix to an Integer Power

HP 15C:   Transition Matrix Test and Taking a Square Matrix to an Integer Power



Transition Matrix Test - Markov Chain


Let A be a square matrix of probabilities (entries from 0 to 1).   Is the square matrix suitable to be used in Markov Chain calculations?   It would qualify if for each row, the elements of that row have a sum to 1.  


The program:  


*  Tests whether Matrix A is a square matrix.  Non-square matrices are not transition matrices qualified for Markov Chains.   (lines 005 through 007)

*  Sum the elements of each row.

*  Tests whether sum of each row is 1. (LBL 2)


If (I) and (III) are met, then Matrix A is qualified to be used as a transition matrix for Markov Chain calculations.   This test returns a 1 for yes and 0 for no.


Store the contents and dimensions of Matrix A before running the program. 



HP 15C Program - Transition Matrix Test


Code:


001 : 42,21,11 : f LBL A

002 : 45,16,11 : RCL MATRIX A

003 : 36 : ENTER

004 : 36 : ENTER

005 : 45,23,11 : RCL DIM A

006 : 43,30, 6 :  g TEST 6  (x≠y)

007 : 22, 1 : GTO 1

008 : 44, 2 : STO 2

009 : 43,35 : g CLx

010 : 1 : 1

011 : 42,23,12 :  f DIM B

012 : 44,16,12 : STO MATRIX B

013 : 33 :  R↓

014 : 33 :  R↓

015 : 45,16,12 : RCL MATRIX B

016 : 42,26,13 : f RESULT C

017 : 20 : ×

018 : 42,16, 1 : f MATRIX 1


019 : 42,21, 2 : LBL 2

020 : 45, 2 : RCL 2

021 : 44, 0 : STO 0

022 : 45,13 : RCL C

023 : 1 : 1

024 : 43,30, 6 : g TEST 6 (x≠y)

025 : 22, 1 : GTO 1

026 : 42, 5, 2 : DSE 2

027 : 22, 2 : GTO 2


028 : 43,32 : RTN   (test passes)


029 : 42,21, 1 : f LBL 1  

030 : 43,35 : g CLx

031 : 43,32 : RTN  (test fails)


Notes:


RCL C instead of RCL MATRIX C is used because we want to recall the element, not the entire matrix.  The element that is recalled depends on the row (stored in R0) and column (stored in R1). 


HP 15C:  Square Matrix to a Positive Integer


Using a loop is required.  We are not able to use the x^2 or the y^x function with matrices, an Error 1 condition occurs.  


 A square matrix is stored in Matrix A.   Enter the positive integer power (n > 1) on the X stack before running the program.  


Code:  


032 : 42, 21, 11 : f LBL B

033:  1 :  1

034: 30 : -

035 : 44, 2 : STO 2

036 : 45,16,11 : RCL MATRIX A

037 : 44,16,12 : STO MATRIX B


038 : 42,21, 3 : f LBL 3

039 : 42,16,11 : f RESULT C

040 : 45,16,11 : RCL MATRIX A

041 : 45,16,12 : RCL MATRIX B

042 : 20 : ×

043 : 44,16,12 : STO MATRIX B

044 : 42, 5, 2 : f DSE 2

045: 22, 3 : GTO 3


046 : 45,16,13 : RCL MATRIX C

047: 42,16, 1 : f MATRIX 1

048 :43,32 : g RTN


Notes:  


MATRIX 1:  Set the row and column pointers to 1  (R0 = 1, R1 = 1)



Example



A = [ [ 0.3, 0.7, 0 ] [ 0.3, 0.3, 0.4 ] [ 0.2, 0.5, 0.3 ] ] 


GTO A R/S:   1  (yes, Matrix A is qualified for as a transition matrix)


If we insert a high enough power (as n → ∞), the matrix settles into a steady state, where each column will have the same value.


25 GTO B R/S:   C 3 x 3 

MATRIX 1

RCL C...

[ [ 0.2736, 0.4623, 0.2642 ] [ 0.2736, 0.4623, 0.2642 ] [ 0.2736, 0.4623, 0.2642 ] ]



Eddie



All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...