KDA
Contains the Kemeny Decomposition Algorithm (KDA) class.
KDA
Kemeny Decomposition Algorithm (KDA) class.
KDA will iteratively cut 'edges' to decompose an original Markov chain (MC). An edge corresponds to a MC transition probability. The notation of Berkhout and Heidergott (2019) is used.
Attributes:
-
MC
(MarkovChain
) –Current Markov chain during KDA.
-
log
(dict
) –Dictionary which logs the edges cut by KDA during the iterations. It also logs the Markov chains after each iteration. Each iteration of the inner-loop is stored by appending the list log['edges cut']. It is initialized with [None] to make the indexing easier. The Markov chains are stored in log['Markov chains'], where the original/initial MC is stored at index 0.
Methods:
-
run
–This will run KDA.
-
cut_edges
–Allows one to cut manual edges in the current Markov chain to create a new Markov chain after normalization
Source code in pykda\KDA.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
__init__(original_MC, CO_A='CO_A_1(1)', CO_B='CO_B_3(0)', symmetric_cut=False, verbose=False, normalizer=standard_row_normalization)
Parameters:
-
original_MC
(MarkovChain
) –Original Markov chain object to which KDA will be applied.
-
CO_A
(str
, default:'CO_A_1(1)'
) –Condition of how often the Kemeny constant derivatives are being recalculated (outer while loop in KDA). The options are:
- 'CO_A_1(i)' = Number of times performed < i - 'CO_A_2(E)' = Number of ergodic classes in current MC is < E - 'CO_A_3(C)' = Number of strongly connected components in current MC is < C
-
CO_B
(str
, default:'CO_B_3(0)'
) –Condition of how many edges are being cut per iteration (inner while loop in KDA). The options are:
- 'CO_B_1(e)' = Number of edges cut is < e - 'CO_B_2(E)' = Number of ergodic classes in MC is < E - 'CO_B_3(q)' = Not all edges with MC.KDer < q are cut
-
symmetric_cut
(bool
, default:False
) –If True, cutting (i, j) will also cut (j, i). If False, only (i, j).
-
verbose
(bool
, default:False
) –If true, information will be printed, else not.
-
normalizer
(normalizer_type
, default:standard_row_normalization
) –Normalizer used to create a stochastic matrix from a matrix.
Source code in pykda\KDA.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
check_for_infinite_loops()
Due to fixes for normalization, it may happen that the same edge is cut over and over again. This check raises an error in that case.
Source code in pykda\KDA.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
condition_A()
Returns whether condition A is True or False.
Source code in pykda\KDA.py
99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
condition_A_translator()
Translates the condition A into something readable.
Source code in pykda\KDA.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
condition_B()
Returns whether condition B is True or False.
Source code in pykda\KDA.py
113 114 115 116 117 118 119 |
|
condition_B_translator()
Translates the condition B into something readable.
Source code in pykda\KDA.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
cut_edges(*args)
Cut given edges in the Markov chain and normalize afterward.
There are different options to specify which edges to cut via args. When self.symmetric_cut = True, also the reversed edges are cut.
Parameters:
-
args
–There are three options for args: 1. One tuple of length 2 indicating which edge to cut. 2. One list of tuples of edges which to cut. 3. Two lists or np.ndarrays indicating which edges to cut.
Source code in pykda\KDA.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
cut_till_condition_B_fails()
Cuts edges till condition B fails.
Source code in pykda\KDA.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
get_num_in_brackets(s)
staticmethod
Gets the number given between brackets in string s.
Source code in pykda\KDA.py
284 285 286 287 288 |
|
get_num_in_str_brackets(s)
staticmethod
Returns the number given between brackets in string s.
Source code in pykda\KDA.py
93 94 95 96 97 |
|
log_edges(row_indexes, col_indexes)
Logs the edges that have been cut.
Parameters:
-
row_indexes
(ndarray | list
) –Row indexes of the edges that are cut.
-
col_indexes
(ndarray | list
) –Columns indexes of the edges that are cut.
Source code in pykda\KDA.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
plot(**kwargs)
Plots the Markov chain after KDA. Refer to MarkovChain.plot() for more details.
Source code in pykda\KDA.py
332 333 334 335 336 |
|
plot_progress(**kwargs)
Plots the Markov chains in the log. The kwargs are passed to the plot method of the Markov chain. Refer to MarkovChain.plot() for more details.
Source code in pykda\KDA.py
324 325 326 327 328 329 330 |
|
report()
Prints a report of KDA.
Source code in pykda\KDA.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
run()
Runs KDA with conditions CO_A and CO_B.
Source code in pykda\KDA.py
121 122 123 124 125 126 127 128 129 130 |
|