cfg_loader
Model_xfer
dataclass
Bases: Model
Source code in witch/containers/model.py
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 | |
model_grad
cached
property
The evaluated model and its gradient, see core.model_grad for details.
Note that this is cached, but is automatically reset whenever
update is called or cur_round changes. Currently computing
grad for models with transfer function is not supported.
Returns:
| Type | Description |
|---|---|
None
|
|
deep_merge(a, b)
Based on https://gist.github.com/angstwad/bf22d1822c38a92ec0a9?permalink_comment_id=3517209
Source code in witch/fitter.py
688 689 690 691 692 693 694 695 696 697 698 699 | |
joint_objective(metamodel, do_loglike=True, do_grad=True, do_curve=True)
Compute the objective for multiple datasets in an MPI aware way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metamodel
|
MetaModel
|
The |
required |
do_loglike
|
bool
|
If True then we will compute the log-likelihood between the model and the data. |
True
|
do_grad
|
bool
|
If True then compute the gradient of chi-squared with respect to the model parameters. |
True
|
do_curve
|
bool
|
If True than compute the curvature of chi-squared with respect to the model parameters. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
loglike |
Array
|
The log-likelihood between the model and data.
If |
grad |
Array
|
The gradient of the parameters at there current values.
If |
curve |
Array
|
The curvature of the parameter space at the current values.
If |
Source code in witch/objective.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 | |
load_cfg(cfg, args=None)
Set up datasets and the metamodel from a loaded config, exactly as the fitter does before fitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
dict
|
Config as returned by |
required |
args
|
Namespace
|
Parsed command line arguments. If given, |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datasets |
tuple[DataSet]
|
Processed datasets living on this MPI task. |
outdir |
str
|
Output directory. |
dset_names |
list[str]
|
Names of all datasets in the config. |
metamodel |
MetaModel
|
The metamodel. The (possibly split) MPI communicator is stored in
|
Source code in witch/cfg_loader.py
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 | |
load_config(start_cfg, cfg_path)
We want to load a config and if it has the key "base", load that as well and merge them. We only want to take things from base that are not in the original config so we merge the original into the newly loaded one.
Source code in witch/fitter.py
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 | |
para_to_non_para(model, n_rounds, to_copy, rmax, struct_num, sig_params, default, mm_cfg)
Function which approximately converts cluster profiles into a non-parametric form. Note this is only approximate and should be fit afterwords.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The parametric model to start from. |
required |
n_rounds
|
Optional[int]
|
Number of rounds to fit for output model. If none, copy from self |
required |
to_copy
|
list[str]
|
List of structures, by name, to copy. |
required |
rmax
|
float
|
Maximum radius of the rbins |
required |
struct_num
|
int
|
Structure within model to calculate rbins on |
required |
sig_params
|
list[int]
|
Parameters to consider for computing significance. Only first match will be used. |
required |
default
|
tuple[int, ...]
|
Default rbins to be returned if generation fails. |
required |
mm_cfg
|
dict
|
MetaModel config, parameter_map will be modified if need be. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Model |
Model
|
Model with a non-parametric representation of input model |
Raises:
| Type | Description |
|---|---|
ValueError
|
If there are no models to copy |
Source code in witch/nonparametric.py
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 | |
print_once(*args)
Helper function to print only once when running with MPI. Only the rank 0 process will print.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Unpack[tuple[Any, ...]]
|
Arguments to pass to print. |
()
|
Source code in witch/fitter.py
36 37 38 39 40 41 42 43 44 45 46 47 48 | |
run_lmfit(metamodel, maxiter=10, chitol=1e-05)
Fit a set of models to datasets jointly. This uses a modified Levenberg–Marquardt fitter with flat priors. This function is MPI aware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metamodel
|
MetaModel
|
The MetaModel that describes the models and datasets. |
required |
maxiter
|
int
|
The maximum number of iterations to fit. |
10
|
chitol
|
float
|
The delta chisq to use as the convergence criteria. |
1e-5
|
Returns:
| Name | Type | Description |
|---|---|---|
models |
tuple[Model, ...]
|
Model with the final set of fit parameters, errors, and chisq. |
final_iter |
int
|
The number of iterations the fitter ran for. |
delta_chisq |
float
|
The final delta chisq. |
curve |
Array
|
The final covariance matrix. |
Source code in witch/fitting.py
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 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 | |
run_mcmc(metamodel, num_steps=5000, num_leaps=10, step_size=jnp.array(0.02), sample_which=-1, burn_in=0.1, max_tries=20)
Run Hamilonian Monte Carlo to estimate the posterior for our model.
Currently this function only support flat priors, but more will be supported
down the line. In order to ensure accuracy of the noise model used, it is
reccomended that you run at least one round of fit_tods followed by noise
reestimation before this function.
This is MPI aware. Eventually this will be replaced with something more jaxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metamodel
|
MetaModel
|
The MetaModel that describes the models and datasets. |
required |
num_steps
|
int
|
The number of steps to run MCMC for. |
5000
|
num_leaps
|
int
|
The number of leapfrog steps to take at each sample. |
10
|
step_size
|
Array
|
The step size to use in the leapfrog algorithm. This should be tuned to get an acceptance fraction of ~.65. |
.02
|
sample_which
|
int
|
Sets which parameters to sample.
If this is >= 0 then we will sample which ever parameters were
fit in that round of fitting.
If this is -1 then we will sample which ever parameters were fit
in the last round of fitting.
If this is -2 then any parameters that were ever fit will be sampled.
If this is <= -3 or >= |
-1,
|
burn_in
|
float
|
Fractional burn-in period of samples to discard |
0.1
|
max_tries
|
int
|
Number of tries to tune step size that will be attemted. If 0 no tuning will be run. |
10
|
Returns:
| Name | Type | Description |
|---|---|---|
metamodel |
MetaModel
|
The metamodel with MCMC estimated parameters and errors. The parameters are estimated as the mean of the samples. The errors are estimated as the standard deviation. This also has the chi-squared of the estimated parameters. |
flat_samples |
Array
|
Array of samples from running MCMC. |
Source code in witch/fitting.py
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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 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 823 824 825 826 | |
run_mh(metamodel, num_samples=10000, num_chains=4, burn_in=0.1, bound=2, seed=0, prior_type='uniform', prior_err_scale=1.0, joint=True, dataset_ind=0, progress=True, proposal='cov')
Run Metropolis-Hastings chains and summarize them into the metamodel.
The chains start at the current metamodel.parameters (e.g. the
Levenberg-Marquardt best fit) and only parameters with to_fit set are
varied. Mirrors the interface of witch.fitting.run_mcmc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metamodel
|
MetaModel
|
Model to sample. |
required |
num_samples
|
int
|
Samples per chain. |
10000
|
num_chains
|
int
|
Number of chains. |
4
|
burn_in
|
float
|
Fraction of each chain discarded. |
0.1
|
bound
|
float
|
Scale for unbounded uniform proposals. |
2
|
seed
|
int
|
Base seed; chain |
0
|
prior_type
|
(uniform, normal)
|
Prior and proposal type. |
"uniform"
|
prior_err_scale
|
float
|
Multiplier on |
1.0
|
joint
|
bool
|
Use the MPI-aware joint likelihood (chains run serially). If False chains run in threads on a single dataset. |
True
|
dataset_ind
|
int
|
Dataset used when |
0
|
progress
|
bool
|
Show progress bars. |
True
|
proposal
|
(cov, diag)
|
|
"cov"
|
Returns:
| Name | Type | Description |
|---|---|---|
metamodel |
MetaModel
|
Metamodel with median parameters, std errors, covariance and chisq. |
flat_samples |
ndarray
|
Post burn-in samples of the fitted parameters, |
chains |
ndarray
|
All chains, |
acceptance_rates |
ndarray
|
Acceptance rate of each chain. |
Source code in witch/metropolis_hastings.py
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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | |