openscm_calibration.emcee_plotting#
Support for plotting emcee during run and afterwards
Functions:
| Name | Description |
|---|---|
get_neg_log_likelihood_ylim_default |
Get the y-limits for the negative log likelihood axes |
plot_chains |
Plot chains in MCMC run |
plot_corner |
Plot corner plot of distribution from MCMC run |
plot_dist |
Plot distributions from MCMC run |
plot_emcee_progress |
Plot MCMC progress |
plot_parameter_chains |
Plot chains for a single parameter in an MCMC run |
plot_tau |
Plot the autocorrelation time, tau |
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_PLOT_CORNER_LABEL_KWARGS |
dict[str, Any]
|
Default value for |
DEFAULT_PLOT_CORNER_TITLE_KWARGS |
dict[str, Any]
|
Default value for |
DEFAULT_PROGRESS_KWARGS |
Default arguments to use for displaying progress bars |
DEFAULT_PLOT_CORNER_LABEL_KWARGS
module-attribute
#
Default value for label_kwargs used by plot_corner
Provided to give the user an easy to modify these defaults if they wish or a starting point
DEFAULT_PLOT_CORNER_TITLE_KWARGS
module-attribute
#
Default value for title_kwargs used by plot_corner
Provided to give the user an easy to modify these defaults if they wish or a starting point
DEFAULT_PROGRESS_KWARGS
module-attribute
#
Default arguments to use for displaying progress bars
get_neg_log_likelihood_ylim_default #
get_neg_log_likelihood_ylim_default(
neg_ll_values: NDArray[floating[Any] | integer[Any]],
median_scaling: float = 1.5,
max_scaling: float = 2.0,
) -> tuple[float, float]
Get the y-limits for the negative log likelihood axes
This is the default algorithm
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neg_ll_values
|
NDArray[floating[Any] | integer[Any]]
|
Negative log likelihood values being plotted |
required |
median_scaling
|
float
|
Scaling to apply to the median value |
1.5
|
max_scaling
|
float
|
Scaling to apply to the maximum value |
2.0
|
Returns:
| Type | Description |
|---|---|
y-limits
|
|
Notes
The algorithm is
.. math::
\text{median_scaled} = \text{median_scaling} \times \text{med}(\text{neg_ll_values}) \\
\text{max_scaled} = \text{max_scaling} \times \max(\text{neg_ll_values}) \\
\text{ymin} = \min(0, \text{median_scaled}, \text{max_scaled}) \\
\text{ymax} = \max(0, \text{median_scaled}, \text{max_scaled})
Source code in src/openscm_calibration/emcee_plotting.py
plot_chains #
plot_chains(
inp: Backend | EnsembleSampler,
burnin: int,
parameter_order: tuple[str, ...],
neg_log_likelihood_name: str,
axes_d: dict[str, Axes],
get_neg_log_likelihood_ylim: Callable[
[NDArray[floating[Any] | integer[Any]]],
tuple[float, float],
]
| None = None,
**kwargs: Any,
) -> None
Plot chains in MCMC run
The y-limits of the log likelihood axis are set dynamically
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
Backend | EnsembleSampler
|
Object from which to plot the state |
required |
burnin
|
int
|
Number of iterations to treat as burn in |
required |
parameter_order
|
tuple[str, ...]
|
Order of model parameters. This must match the order used by |
required |
neg_log_likelihood_name
|
str
|
Label when plotting negative log likelihood. Must match the expected
name in |
required |
axes_d
|
dict[str, Axes]
|
Axes on which to plot the chains. Must have a key for each name in
|
required |
get_neg_log_likelihood_ylim
|
Callable[[NDArray[floating[Any] | integer[Any]]], tuple[float, float]] | None
|
Function which gets the y-limits for the negative log likelihood plot
based on the costs. If not provided,
:func: |
None
|
**kwargs
|
Any
|
Passed to :func: |
{}
|
Source code in src/openscm_calibration/emcee_plotting.py
plot_corner #
plot_corner(
inp: Backend | EnsembleSampler,
burnin: int,
thin: int,
parameter_order: tuple[str, ...],
fig: Figure,
bins: int = 30,
plot_contours: bool = True,
smooth: bool = True,
quantiles: Sequence[float] = (
0.05,
0.17,
0.5,
0.83,
0.95,
),
show_titles: bool = True,
title_quantiles: Sequence[float] = (0.05, 0.5, 0.95),
title_kwargs: dict[str, Any] | None = None,
title_fmt: str = ".3f",
label_kwargs: dict[str, Any] | None = None,
**kwargs: Any,
) -> None
Plot corner plot of distribution from MCMC run
This is a thin wrapper around :func:corner.corner which uses some
sensible defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
Backend | EnsembleSampler
|
Object from which to plot the state |
required |
burnin
|
int
|
Number of iterations to treat as burn in |
required |
thin
|
int
|
Thinning to use when sampling the chains |
required |
parameter_order
|
tuple[str, ...]
|
Order of model parameters |
required |
fig
|
Figure
|
Figure to use for plotting, should be empty i.e. have been cleared before being passed here |
required |
bins
|
int
|
Number of bins to use in histograms. See docstring of
:func: |
30
|
plot_contours
|
bool
|
Whether to plot contours on the 2D distribution plots. See docstring
of :func: |
True
|
smooth
|
bool
|
Whether to smooth the contours on the 2D distribution plots. See
docstring of :func: |
True
|
quantiles
|
Sequence[float]
|
Quantiles at which to draw vertical lines in the histogram plots. See
docstring of :func: |
(0.05, 0.17, 0.5, 0.83, 0.95)
|
show_titles
|
bool
|
Whether to show titles on the histogram plots. See docstring of
:func: |
True
|
title_quantiles
|
Sequence[float]
|
Quantiles to put in the titles of the histogram plots. See docstring
of :func: |
(0.05, 0.5, 0.95)
|
title_kwargs
|
dict[str, Any] | None
|
Keyword arguments to use when making the titles on the histogram
plots. If not supplied, we use
|
None
|
title_fmt
|
str
|
Format string to use when creating the titles on the histogram plots.
If not supplied, our own internal defaults are used (see source code
for values). See docstring of :func: |
'.3f'
|
label_kwargs
|
dict[str, Any] | None
|
Keyword arguments to use when creating the labels for the plot. If not
supplied, we use |
None
|
**kwargs
|
Any
|
Passed to :func: |
{}
|
Source code in src/openscm_calibration/emcee_plotting.py
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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
plot_dist #
plot_dist(
inp: Backend | EnsembleSampler,
burnin: int,
thin: int,
parameter_order: tuple[str, ...],
axes_d: dict[str, Axes],
common_norm: bool = False,
fill: bool = True,
legend: bool = False,
**kwargs: Any,
) -> None
Plot distributions from MCMC run
This is a thin wrapper around :func:sns.kdeplot that sets helpful
defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
Backend | EnsembleSampler
|
Object from which to plot the state |
required |
burnin
|
int
|
Number of iterations to treat as burn in |
required |
thin
|
int
|
Thinning to use when sampling the chains |
required |
parameter_order
|
tuple[str, ...]
|
Order of model parameters |
required |
axes_d
|
dict[str, Axes]
|
Axes on which to plot the distributions |
required |
common_norm
|
bool
|
Should all the distributions use the same normalisation? We generally
set this to |
False
|
fill
|
bool
|
Should the KDE plots be filled? See docstring of :func: |
True
|
legend
|
bool
|
Should a legend be added to the plots? The legend is pretty
meaningless (it is just the chain numbers) so we generally set this to
|
False
|
**kwargs
|
Any
|
Passed to :func: |
{}
|
Source code in src/openscm_calibration/emcee_plotting.py
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 | |
plot_emcee_progress #
plot_emcee_progress(
sampler: EnsembleSampler,
iterations: int,
burnin: int,
thin: int,
plot_every: int,
parameter_order: tuple[str, ...],
neg_log_likelihood_name: str,
holder_chain: DisplayHandle,
figure_chain: Figure,
axes_chain: dict[str, Axes],
holder_dist: DisplayHandle,
figure_dist: Figure,
axes_dist: dict[str, Axes],
holder_corner: DisplayHandle,
figure_corner: Figure,
holder_tau: DisplayHandle,
figure_tau: Figure,
ax_tau: Axes,
start: NDArray[float64] | None = None,
sample_kwargs: dict[str, Any] | None = None,
progress: str | bool = "notebook",
progress_kwargs: dict[str, Any] | None = None,
min_samples_before_plot: int = 2,
corner_kwargs: dict[str, Any] | None = None,
convergence_ratio: float = 50,
) -> Iterator[ChainProgressInfo]
Plot MCMC progress
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampler
|
EnsembleSampler
|
Sampler used for sampling the posterior distribution |
required |
iterations
|
int
|
Number of iterations to perform |
required |
burnin
|
int
|
Burn-in to apply to the chains |
required |
thin
|
int
|
Thinning to apply to the chains |
required |
plot_every
|
int
|
How many steps to wait before updating the plots |
required |
parameter_order
|
tuple[str, ...]
|
Order of the parameters. This is used for ensuring that the plots are labelled correctly. |
required |
neg_log_likelihood_name
|
str
|
Name to use for the negative log likelihood in plots |
required |
holder_chain
|
DisplayHandle
|
Holder of the figure that displays the chains |
required |
figure_chain
|
Figure
|
Figure that displays the chains |
required |
axes_chain
|
dict[str, Axes]
|
Axes on which to plot the chains. Each parameter in |
required |
holder_dist
|
DisplayHandle
|
Holder of the figure that displays the parameter distributions |
required |
figure_dist
|
Figure
|
Figure that displays the parameter distributions |
required |
axes_dist
|
dict[str, Axes]
|
Axes on which to plot the parameter distributions. Each parameter in |
required |
holder_corner
|
DisplayHandle
|
Holder of the figure that displays the corner plot |
required |
figure_corner
|
Figure
|
Figure that displays the corner plot |
required |
holder_tau
|
DisplayHandle
|
Holder of the figure that displays the autocorrelation |
required |
figure_tau
|
Figure
|
Figure that displays the autocorrelation |
required |
ax_tau
|
Axes
|
Axes on which to plot the autocorrelation |
required |
start
|
NDArray[float64] | None
|
Starting point for the sampling. Only required if the sampler has not performed iterations already. |
None
|
sample_kwargs
|
dict[str, Any] | None
|
Arguments to pass to |
None
|
progress
|
str | bool
|
Whether to show a progress bar or not. If this is a string, it must match the values supported by |
'notebook'
|
progress_kwargs
|
dict[str, Any] | None
|
Arguments to pass to the progress bar, if used. |
None
|
min_samples_before_plot
|
int
|
Minimum number of samples that must be taken before any plot can be made. |
2
|
corner_kwargs
|
dict[str, Any] | None
|
Passed to |
None
|
convergence_ratio
|
float
|
Ratio to use to check whether the chains have converged or not. Passed to
|
50
|
Yields:
| Type | Description |
|---|---|
ChainProgressInfo
|
Information about the chain's progress. The yield occurs each time the plots are updated, so can also be used as a way to perform other actions. |
Source code in src/openscm_calibration/emcee_plotting.py
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 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 | |
plot_parameter_chains #
plot_parameter_chains(
ax: Axes,
chain_values: NDArray[number[Any]],
burnin: int,
alpha_chain: float = 0.3,
linewidth: float = 0.5,
color: str = "0.2",
alpha_vspan: float = 0.3,
kwargs_chain: dict[str, Any] | None = None,
kwargs_vspan: dict[str, Any] | None = None,
) -> Axes
Plot chains for a single parameter in an MCMC run
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
Axes on which to plot |
required |
chain_values
|
NDArray[number[Any]]
|
Chain values to plot (should be 2D) |
required |
burnin
|
int
|
Number of iterations to treat as burn in |
required |
alpha_chain
|
float
|
Alpha to use for the chains |
0.3
|
linewidth
|
float
|
Linewidth to use for the chains |
0.5
|
color
|
str
|
Colour to use for the chains |
'0.2'
|
alpha_vspan
|
float
|
Alpha to use for the vertical span (which shows the burnin period) |
0.3
|
kwargs_chain
|
dict[str, Any] | None
|
Pass to :meth: |
None
|
kwargs_vspan
|
dict[str, Any] | None
|
Pass to :meth: |
None
|
Source code in src/openscm_calibration/emcee_plotting.py
plot_tau #
plot_tau(
ax: Axes,
autocorr: NDArray[float64],
steps: NDArray[int64],
parameter_order: tuple[str, ...],
convergence_ratio: float,
convergence_ratio_line_kwargs: dict[str, Any]
| None = None,
legend_loc: str | None = "lower right",
xlabel: str = "Number steps (post burnin)",
ylabel: str = "Autocorrelation time, tau",
) -> None
Plot the autocorrelation time, tau
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
Axes on which to plot |
required |
autocorr
|
NDArray[float64]
|
Autocorrelation information. This should be multi-dimensional, with each column being the autocorrelation time for a different parameter. |
required |
steps
|
NDArray[int64]
|
The number of steps taken before each row in In other words, the x-axis for the plot. |
required |
parameter_order
|
tuple[str, ...]
|
The order of the parameters in |
required |
convergence_ratio
|
float
|
Convergence ratio (used to show the convergence line on the plot) |
required |
convergence_ratio_line_kwargs
|
dict[str, Any] | None
|
Keyword arguments to pass to If not supplied, we use |
None
|
legend_loc
|
str | None
|
Location of the legend. If |
'lower right'
|
xlabel
|
str
|
x-label to apply to the plot. |
'Number steps (post burnin)'
|
ylabel
|
str
|
y-label to apply to the plot. |
'Autocorrelation time, tau'
|