random_walk
Classes to simlate random walks on static, temporal, and higher-order networks.
HigherOrderRandomWalk
¶
Bases: pathpyG.processes.random_walk.RandomWalk
Class that implements a biased random walk process in a higher-order network.
Instances of this class can be used to simulate random walk processes in higher-order networks for arbitrary orders k. The random walk process can include weighted edges as well as a restart probability, i.e. a per-step probability to teleport to a randomly chosen higher-order node.
Different from the class RandomWalk, instances of class HigherOrderRandomWalk automatically project states to the corresponding first-order network, i.e. paths and visualisations are given in terms of the nodes in the first-order network, while the dynamics of the random walk is governed by the underlying higher-order network.
The implementation follows the general concept to simulate discrete-time (stochastic) processes
as implemented in the base class BaseProcess. Hence, the user can either use the iterator interface
to iterate through the steps of a single random walk process, or use the run_experiment
function
to simulate multiple runs of a random walk with different start nodes (i.e. seeds).
The run_experiment
function returns a pandas DataFrame object that contains all node state changes
during the process' evolution. This data frame can be converted to Path and PathCollection objects
and it can be visualized using the plot function.
Examples:
Generate and visualize a single random walk with 10 steps on a higher-order network
>>> import pathpy as pp
>>> g = pp.Graph.from_edge_list([['a','b'], ['b','c'], ['c','a'], ['c','d'], ['d','a']])
>>> paths = pp.WalkData(g3.mapping)
>>> paths.add_walk_seq(['a','b','c'],freq=1)
>>> paths.add_walk_seq(['b','c','a'],freq=1)
>>> paths.add_walk_seq(['b','c','d'],freq=0.2)
>>> paths.add_walk_seq(['c','a','b'],freq=1)
>>> paths.add_walk_seq(['c','d','a'],freq=0.2)
>>> paths.add_walk_seq(['d','a','b'],freq=1)
>>> g_ho = pp.HigherOrderGraph(paths, order =2)
>>> rw = pp.processes.HigherOrderRandomWalk(g_ho, weight=True)
>>> data = rw.run_experiment(steps=10, runs=[('b','c')])
>>> rw.plot(data)
[interactive visualization in first-order network]
Use plot
function of base class to visualize random walk in second-order network
Generate a single random walk with 10 steps starting from node 'b-c' and return a first-order path
>>> p = rw.get_path(rw.run_experiment(steps=10, runs=['b-c']))
>>> pprint([v.uid for v in p.nodes ])
[ 'a', 'b', 'c', 'a', 'a', 'b', 'c', 'd', 'a', 'b']
Use get_path
function of base class to return path with second-order nodes
Generate one random walk with 10 steps starting from each node and return a WalkData instance with first-order paths
>>> paths = rw.get_paths(rw.run_experiment(steps=10, runs=g_ho.nodes))
>>> pprint([v.uid for v in p.nodes ])
[ 'a', 'b', 'c', 'a', 'a', 'b', 'c', 'd', 'a', 'b']
[ 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'a', 'b', 'c' ]
[ 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'a', 'b', 'c' ]
[ 'b', 'c', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'a', 'b' ]
Simulate a random walk using the iterator interface, which provides full access to the state after each simulation step
>>> for time, _ in rw2.simulation_run(steps=50, seed='b-c'):
>>> print('Current node = {0}'.format(rw2.first_order_node(rw2.current_node)))
>>> print(rw2._first_order_visitation_frequencies)
Current node = b
[0.33333333 0.33333333 0.33333333 0. ]
Current node = c
[0.32142857 0.32142857 0.35714286 0. ]
Current node = a
[0.34482759 0.31034483 0.34482759 0. ]
Current node = b
[0.33333333 0.33333333 0.33333333 0. ]
Current node = c
[0.32258065 0.32258065 0.35483871 0. ]
Current node = a
Source code in src/pathpyG/processes/random_walk.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
|
first_order_total_variation_distance
property
¶
Returns the total variation distance between stationary visitation probabilities and the current visitation frequencies, projected to nodes in the first_order_network.
Computes the total variation distance between the current (first-order) node visitation probabilities and the (first-order) stationary node visitation probabilities. This quantity converges to zero for HigherOrderRandomWalk.time -> np.infty and its magnitude indicates the current relaxation of the higher-order random walk process.
first_order_visitation_frequencies
property
¶
Returns current normalized visitation frequencies of first-order nodes based on the history of the higher-order random walk. Initially, all visitation probabilities are zero except for the last node of the higher-order seed node.
__init__
¶
Creates a biased random walk process in a network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
higher_order_network
|
pathpyG.Graph
|
The higher-order network instance on which to perform the random walk process. |
required |
first_order_network
|
The first-order network instance to be used for mapping the process to first-order nodes |
required | |
weight
|
typing.Optional[pathpyG.processes.random_walk.Weight]
|
If specified, the given numerical edge attribute will be used to bias the random walk transition probabilities. |
None
|
restart_probability
|
The per-step probability that a random walker restarts in a random (higher-order) node |
required |
Source code in src/pathpyG/processes/random_walk.py
first_order_node
¶
Maps a given uid of a node in the higher-order network to the uid of the corresponding first-order node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
higher_order_node
|
tuple
|
Tuple that represents the higher-order node |
required |
Returns:
Type | Description |
---|---|
str
|
String of the corresponding first-order node |
Source code in src/pathpyG/processes/random_walk.py
first_order_stationary_state
¶
Returns current normalized visitation frequencies of first-order nodes based on the history of the higher-order random walk. Initially, all visitation probabilities are zero except for the last node of the higher-order seed node.
Source code in src/pathpyG/processes/random_walk.py
get_paths
¶
Returns paths that represent the sequences of (first-order) nodes traversed by random walks with given run ids.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
pandas.DataFrame
|
Pandas data frame containing the trajectory of one or more (higher-order) random walks, generated by a call of |
required |
run_uid
|
Uid of the random walk simulations to be returned as WalkData (default: 0). |
required |
Returns:
Type | Description |
---|---|
pathpyG.PathData
|
WalkData object containing the sequences of nodes traversed by the random walks |
Source code in src/pathpyG/processes/random_walk.py
step
¶
Function that will be called for each step of the random walk. This function returns a tuple, where the first entry is the uids of the currently visited higher-order node and the second entry is the uid of the previously visited higher-order node.
Use the first_order_node
function to map those nodes to nodes in the first-order network
Source code in src/pathpyG/processes/random_walk.py
RandomWalk
¶
Bases: pathpyG.processes.process.BaseProcess
Class that implements a biased random walk process in a network.
Instances of this class can be used to simulate random walk processes in any instance of the class Graph. The random walk process can include weighted edges as well as a restart probability, i.e. a per-step probability to teleport to a randomly chosen node.
Since any instance of HigherOrderGraph is also an instance of Graph, this class can be directly be applied to simulate random walks in higher-order networks. However, the state space of such a random walk is given by the higher-order nodes. If you wish to simulate a higher-order random walk while projecting states to the corresponding first-order network, you should use the class HigherOrderRandomWalk instead.
The implementation follows the general concept to simulate discrete-time (stochastic) processes
as implemented in the base class BaseProcess. Hence, the user can either use the iterator interface
to iterate through the steps of a single random walk process, or use the run_experiment
function
to simulate multiple runs of a random walk with different start nodes (i.e. seeds).
The run_experiment
function returns a pandas DataFrame object that contains all node state changes
during the process' evolution. This data frame can be converted to Path and PathCollection objects
and it can be visualized using the plot function.
Examples:
Generate and visualize a single biased random walk with 10 steps on a network
>>> import pathpyG as pp
>>> g = pp.Graph.from_edge_list([['a','b'], ['b','c'], ['c','a'], ['c','d'], ['d','a']])
>>> rw = pp.processes.RandomWalk(g, weight='edge_weight')
>>> data = rw.run_experiment(steps=10, seed='a')
>>> rw.plot(data)
[interactive visualization]
Generate a single random walk with 10 steps starting from node 'a' and return a WalkData instance
Generate one random walk with 10 steps starting from each node and return a PathCollection instance
>>> pc = rw.get_paths(rw.run_experiment(steps=10, runs=g.nodes))
[ 'a', 'b', 'c', 'a', 'a', 'b', 'c', 'd', 'a', 'b']
[ 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'a', 'b', 'c' ]
[ 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'd', 'a', 'b', 'c' ]
[ 'b', 'c', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'a', 'b' ]
Simulate a random walk using the iterator interface, which provides full access to the state after each simulation step
>>> for time, _ in rw.simulation_run(steps=5, seed='a'):
>>> print('Current node = {0}'.format(rw.current_node))
>>> print(rw.visitation_frequencies)
Current node = b
[0.5 0.5 0. 0. ]
Current node = c
[0.33333333 0.33333333 0.33333333 0. ]
Current node = d
[0.25 0.25 0.25 0.25]
Current node = a
[0.4 0.2 0.2 0.2]
Current node = b
[0.33333333 0.33333333 0.16666667 0.16666667]
Current node = a
[0.42857143 0.28571429 0.14285714 0.14285714]
Current node = c
[0.375 0.25 0.25 0.125]
Current node = a
[0.44444444 0.22222222 0.22222222 0.11111111]
Current node = b
[0.4 0.3 0.2 0.1]
Current node = a
[0.45454545 0.27272727 0.18181818 0.09090909]
Source code in src/pathpyG/processes/random_walk.py
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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
|
time
property
¶
The current time of the random walk process, i.e. the number of steps taken since the start node.
total_variation_distance
property
¶
Returns the total variation distance between stationary visitation probabilities and the current visitation frequencies
Computes the total variation distance between the current visitation probabilities and the stationary probabilities. This quantity converges to zero for RandomWalk.t -> np.infty and its magnitude indicates the current relaxation of the random walk process.
transition_matrix
property
¶
Returns the transition matrix of the random walk
visitation_frequencies
property
¶
Returns current normalized visitation frequencies of nodes based on the history of the random walk. Initially, all visitation probabilities are zero except for the start node.
TVD
staticmethod
¶
Calculates the total variation distance between two probability vectors
__init__
¶
Creates a biased random walk process in a network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
network
|
pathpyG.Graph
|
The network instance on which to perform the random walk process. Can also be an instance of HigherOrderNetwork. |
required |
weight
|
typing.Optional[pathpyG.processes.random_walk.Weight]
|
If specified, the given numerical edge attribute will be used to bias the random walk transition probabilities. |
None
|
restart_probability
|
The per-step probability that a random walker restarts in a random node |
required |
Source code in src/pathpyG/processes/random_walk.py
compute_transition_matrix
staticmethod
¶
Returns the transition matrix of a (biased) random walk in the given network.
Returns a transition matrix that describes a random walk process in the given network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
network
|
pathpyG.Graph
|
The network for which the transition matrix will be created. |
required |
weight
|
typing.Optional[pathpyG.processes.random_walk.Weight]
|
If specified, the numerical edge attribute that shall be used in the biased transition probabilities of the random walk. |
None
|
Source code in src/pathpyG/processes/random_walk.py
get_path
¶
Returns a path that represents the sequence of (first-order) nodes traversed by a single random walk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
pandas.DataFrame
|
Pandas |
required |
run_uid
|
Uid of the random walk simulation to be returns as Path (default: 0). |
required |
Returns:
Type | Description |
---|---|
pathpyG.PathData
|
Path object containing the sequence of nodes traversed by the random walk |
Source code in src/pathpyG/processes/random_walk.py
get_paths
¶
Return a PathData object where each path is one random walk trajectory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
pandas.DataFrame
|
Pandas |
required |
run_ids
|
typing.Optional[typing.Iterable]
|
UIDs of random walk simulation runs to be included in the |
None
|
Source code in src/pathpyG/processes/random_walk.py
init
¶
Initializes the random walk state with a given seed/source node
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
str
|
Id of node in which the random walk will start |
required |
Source code in src/pathpyG/processes/random_walk.py
node_state
¶
Returns a boolean variable indicating whether the walker is currently visiting (first-order) node v
Source code in src/pathpyG/processes/random_walk.py
random_seed
¶
Returns a random node from the network, chosen uniformly at random
state_to_color
¶
Maps the current (visitation) state of nodes to colors for visualization. The state is True for the currently visited node and False for all other nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
bool
|
Current visitation state |
required |
Source code in src/pathpyG/processes/random_walk.py
stationary_state
¶
Compute stationary visitation probabilities of random walk.
Computes stationary visitation probabilities of nodes based on the leading eigenvector of the transition matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs
|
typing.Any
|
Arbitrary key-value pairs to bee passed to the |
{}
|
Source code in src/pathpyG/processes/random_walk.py
step
¶
Function that will be called for each step of the random walk. This function returns a tuple, where the first entry is the id of the currently visited node and the second entry is the id of the previously visited node.
Source code in src/pathpyG/processes/random_walk.py
transition_matrix_pd
¶
Returns the transition matrix as pandas DataFrame with proper row/column labels.
Source code in src/pathpyG/processes/random_walk.py
transition_probabilities
¶
Returns a vector that contains transition probabilities.
Returns a vector that contains transition probabilities from a given node to all other nodes in the network.
Source code in src/pathpyG/processes/random_walk.py
visitation_probabilities
¶
Calculates visitation probabilities of nodes after t steps for a given start node
Initially, all visitation probabilities are zero except for the start node.