detach_attach
attach_move(key, kernel_state, kernel_prior, verbosity=0)
Perform attach move. In this move, a subtree is detached from the tree expression (at idx a), a scaffold is attached to the tree expression (at idx a), and the subtree originally at idx a is reattached to the scaffold (at idx b).
Also returns the probabilities associated with the proposals q_A (going from the original tree to the new tree using the attach move) and q_D (going from the new tree to the original tree using a detach move).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKeyArray
|
The random key. |
required |
kernel_state
|
State
|
The original kernel state, must have attributes: - parameters - leaf_level_map - node_sizes - tree_expression - is_operator |
required |
kernel_prior
|
KernelPrior
|
The prior to sample the new subtree from. |
required |
verbosity
|
int
|
The verbosity level, by default 0. Debug information is printed if verbosity > 2. |
0
|
Returns:
Type | Description |
---|---|
State
|
The updated kernel state after the move. |
ScalarFloat
|
The probability associated with the proposal q_D (b|a, k', theta'), i.e. the transition probability from the new tree to the original tree using a detach move. |
ScalarFloat
|
The probability associated with the proposal q_A (b|a, k, theta), i.e. going from the original tree to the new tree using the attach move. |
Source code in gallifrey/moves/detach_attach.py
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 |
|
detach_attach_move(key, kernel_state, kernel_prior, detach_prob=0.5, verbosity=0)
Perform detach-attach move. In this move, a subtree is detached from the tree expression (at idx a), and a scaffold is attached to the tree expression (at idx a). The subtree is then reattached to the scaffold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKeyArray
|
The random key. |
required |
kernel_state
|
State
|
The original kernel state, must have attributes: - parameters - leaf_level_map - node_sizes - tree_expression - is_operator |
required |
kernel_prior
|
KernelPrior
|
The kernel prior object that contains the kernel structure prior. |
required |
detach_prob
|
ScalarFloat
|
The probability of performing the detach move, by default 0.5. |
0.5
|
verbosity
|
int
|
The verbosity level, by default 0. Debug information is printed if verbosity > 2. |
0
|
Returns:
Type | Description |
---|---|
State
|
The updated kernel state after the move. |
ScalarFloat
|
The log acceptance ratio contribution of the move, needs to be added to the log acceptance ratio of the subtree-replace move. |
Source code in gallifrey/moves/detach_attach.py
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 |
|
detach_move(key, kernel_state, kernel_prior, verbosity=0)
Perform detach move. In this move, a scaffold is detached from the tree expression (at idx a), and a subtree from the scaffold (at idx b) is detached and then reattached to the root (idx a) of the scaffold.
Also returns the probabilities associated with the proposals q_D (going from the original tree to the new tree using the detach move) and q_A (going from the new tree to the original tree using an attach move).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKeyArray
|
The random key. |
required |
kernel_state
|
State
|
The original kernel state, must have attributes: - parameters - leaf_level_map - node_sizes - tree_expression - is_operator |
required |
kernel_prior
|
KernelPrior
|
The prior to sample the new subtree from. |
required |
verbosity
|
int
|
The verbosity level, by default 0. Debug information is printed if verbosity > 2. |
0
|
Returns:
Type | Description |
---|---|
State
|
The updated kernel state after the move. |
ScalarFloat
|
The probability associated with the proposal q_D (b|a, k, theta), i.e. going from the original tree to the new tree using the detach move. |
ScalarFloat
|
The probability associated with the proposal q_A (b|a, k', theta'), i.e. the transition probability from the new tree to the original tree using an attach move. |
Source code in gallifrey/moves/detach_attach.py
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 381 382 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 |
|
detach_subtree(tree_expression, subtree_root_idx, fill_value=-1)
Cut a subtree from a tree expression, starting at the subtree root node.
Returns the tree_expression with the subtree removed, the subtree expression, and the indices of the nodes in the subtree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tree_expression
|
Int[ndarray, ' D']
|
The tree expression, given in level order notation. |
required |
subtree_root_idx
|
ScalarInt
|
The index of node where the subtree is rooted. |
required |
fill_value
|
ScalarInt
|
The value to fill the hole with in the subtree, by default -1. (Must be negative.) |
-1
|
Returns:
Type | Description |
---|---|
Int[ndarray, ' D']
|
The (level order) tree expression with the subtree removed. |
Int[ndarray, ' D']
|
The (level order) tree expression of the subtree, same length as the input tree expression. |
Int[ndarray, ' D']
|
The indices of the nodes in the subtree. |
Source code in gallifrey/moves/detach_attach.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 |
|
scaffold_proposal(key, max_depth, path_to_hole, hole_idx, probs, is_operator, empty_value=-1)
Propose the tree structure for the scaffold in the attach move. Also returns the log-probability of the proposal.
The scaffold is a tree structure proposal very similar to the samples created from the kernel prior (see gallifrey.kernels.prior.TreeStructurePrior). The difference is that this prior is conditioned by the path to the hole, meaning some branches are fixed to be operator nodes, so that the hole can be reached. The root of the scaffold is assumed to be first index in the path_to_hole.
The hole itself is fixed to be a leaf node, and empty (filled by the empty_value) in the scaffold. NOTE: This means the returned scaffold is not a valid kernel structure in itself, the hole must be filled by a valid subtree.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKeyArray
|
Random key for sampling. |
required |
max_depth
|
ScalarInt
|
The maximum depth of the nested kernel structure tree. |
required |
path_to_hole
|
Int[jnp.ndarray, " D"]
|
The path to the hole in the tree. A list of indices that describe the path from the root to the hole (level order indices). |
required |
hole_idx
|
ScalarInt
|
The index of the hole in the tree (level order index). |
required |
probs
|
Float[jnp.ndarray, " D"]
|
The probabilities of sampling each kernel in the library. |
required |
is_operator
|
Bool[ndarray, ' D']
|
An array that indicates whether each kernel in the library is an operator. |
required |
empty_value
|
ScalarInt
|
The value to fill the hole with in the scaffold, by default -1. |
-1
|
Returns:
Type | Description |
---|---|
Int[jnp.ndarray, " D"]
|
An array that describes the scaffold structure. |
ScalarFloat
|
The log probability associated with the scaffold. |
Source code in gallifrey/moves/detach_attach.py
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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
|
structure_attach_move(key, tree_expression, post_level_map, node_heights, kernel_prior, verbosity=0)
Perform attach move on the kernel structure array. In this move, a subtree is detached from the tree expression (at idx a), a scaffold is attached to the tree expression (at idx a), and the subtree originally at idx a is reattached to the scaffold (at idx b).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKeyArray
|
The random key. |
required |
tree_expression
|
Int[ndarray, ' D']
|
The tree expression, given in level order notation. |
required |
post_level_map
|
Int[ndarray, ' D']
|
The array that maps the post order index to the level order index. (This is useful, since the post_level_map contains all the indices of the nodes in the tree, which we can sample). |
required |
node_heights
|
Int[ndarray, ' D']
|
The heights of each node in the tree. (See gallifrey.kernels.tree.TreeKernel for more information.) |
required |
kernel_prior
|
KernelPrior
|
The kernel prior object that contains the kernel structure prior. |
required |
verbosity
|
int
|
The verbosity level, by default 0. Debug information is printed if verbosity > 2. |
0
|
Returns:
Type | Description |
---|---|
Int[ndarray, ' D']
|
The new tree expression with the scaffold attached and the subtree reattached. |
Int[ndarray, ' D']
|
The changes in the tree structure, a list of indices that differ between the original tree and the new tree. |
Int[ndarray, '2 D']
|
The mapping between the old and new indices of the nodes in the subtree. The first column contains the old indices, and the second column contains the new indices. |
ScalarFloat
|
The log probability associated with the path to the hole in the scaffold. |
ScalarFloat
|
The log probability associated with the scaffold. |
ScalarInt
|
The index of the root of the subtree that was detached/scaffold attached (idx a). |
ScalarInt
|
The index where the subtree was reattached (idx b). |
Source code in gallifrey/moves/detach_attach.py
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 |
|
structure_detach_move(key, tree_expression, post_level_map, empty_node_value=-1, verbosity=0)
Perform detach move on the kernel structure array. In this move, a scaffold is detached from the tree expression (at idx a), and a subtree from the scaffold (at idx b) is detached and then reattached to the root (idx a) of the scaffold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKeyArray
|
The random key for sampling. |
required |
tree_expression
|
Int[ndarray, ' D']
|
The tree expression, given in level order notation. |
required |
post_level_map
|
Int[ndarray, ' D']
|
The array that maps the post order index to the level order index. (This is useful, since the post_level_map contains all the indices of the nodes in the tree, which we can sample). |
required |
empty_node_value
|
ScalarInt
|
The value to fill the hole with in the subtree, by default -1. (Must be negative.) |
-1
|
verbosity
|
int
|
The verbosity level, by default 0. Debug information is printed if verbosity > 2. |
0
|
Returns:
Type | Description |
---|---|
Int[ndarray, ' D']
|
The new tree expression with the scaffold removed and the subtree reattached. |
Int[ndarray, '2 D']
|
The mapping between the old and new indices of the nodes in the subtree. The first column contains the old indices, and the second column contains the new indices. |
ScalarInt
|
The index of the root of the scaffold (idx a). |
ScalarInt
|
The index of the root of the subtree that was detached from the scaffold (idx b). |
Source code in gallifrey/moves/detach_attach.py
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 |
|
transplant_subtree(donor_tree_expression, recipient_tree_expression, donor_root_idx, recipient_root_idx, max_nodes)
Transplant a subtree from one tree to another. The subtree is rooted at the donor root index and re-rooted at the recipient root index. The recipient tree must have an empty node at the recipient root index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
donor_tree_expression
|
Int[ndarray, ' D']
|
The tree expression of the donor tree. |
required |
recipient_tree_expression
|
Int[ndarray, ' D']
|
The tree expression of the recipient tree. |
required |
donor_root_idx
|
ScalarInt
|
The index of the root of the subtree to be moved. |
required |
recipient_root_idx
|
ScalarInt
|
The index of the root of the subtree to be moved to. |
required |
max_nodes
|
int
|
The maximum number of nodes in the tree. |
required |
Returns:
Type | Description |
---|---|
Int[ndarray, ' D']
|
The tree expression of the recipient tree with the attached subtree. |
Int[ndarray, '2 D']
|
The indices of the moved nodes. Two rows, the first row contains the indices of the subtree in the donor tree, the second row contains the indices of the subtree in the recipient tree. |
Source code in gallifrey/moves/detach_attach.py
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
|