Base Sampler Class
Module containing base sampler for Monte Carlo sampling methods containing some basic utility methods
- class monte.base_sampler.BaseSampler[source]
Bases:
object- __init__() None[source]
Base sampler for Monte Carlo sampling methods containing basic utility methods. Initializes the sampler with two main attributes: samples and acceptances which are saved here at each iteration
- acceptance_rate()[source]
Calculates the sampler acceptance rate
- Returns:
Sampler acceptance rate
- Return type:
float
- property acceptances
Gets or sets acceptances property
- Returns:
Acceptances property
- Return type:
ndarray
- credible_interval(ci=0.9)[source]
Calculates
\[100 \times \text{ci} \%\]credible interval for each parameter
- Parameters:
ci (float, optional) – Credible interval in decimal, defaults to 0.9
- Returns:
Parameter values at credible interval
- Return type:
ndarray
- ergodic_mean()[source]
Calculates ergodic mean(s) for each parameter. Ergodic mean refers to the mean value until the current iteration
- Returns:
Ergodic mean
- Return type:
ndarray
- ess()[source]
Calculates effective sample size (for each parameter) as per Kass et al (1998) and Robert and Casella (2004; pg.500)
- Returns:
Effective sample size
- Return type:
ndarray
- geweke(first=0.1, last=0.5)[source]
Calculates Geweke diagnostic for a Markov chain
- Parameters:
first (float, optional) – The fraction of the chain to use for the first segment, defaults to 0.1
last (float, optional) – The fraction of the chain to use for the last segment, defaults to 0.5
- Returns:
Test Z-scores and p-values
- Return type:
float, float
- heidelberger_welch(alpha=0.05)[source]
Calculates Heidelberger-Welch diagnostic for a Markov chain
- Parameters:
alpha (float, optional) – Test significance level, defaults to 0.05
- Returns:
Diagnostic results, including: stationarity test result, halfwidth test result, initial chain mean, updated chain start, chain end, updated chain mean
- Return type:
dict
- property lp
Gets or sets log-probability (lp) property
- Returns:
Log-probability (lp) property
- Return type:
ndarray
- mean()[source]
Estimates the expected value of the posterior by calculating the mean of the samples
- Returns:
Numpy array containing posterior mean for every parameter
- Return type:
ndarray
- parameter_kde(figsize=(12, 8), histogram=True, bins=100, show=True, save=False, **kwargs)[source]
Plots Kernel Density Estimation(s) (KDE) (for each parameter) of posterior. Visually estimates posterior distribution
- Parameters:
figsize (tuple, optional) – Size of the total figure (all plots together), defaults to (12, 8)
histogram (bool, optional) – Whether to overlay histogram over KDE plots, defaults to True
bins (int, optional) – Number of histogram bins, defaults to 100
show (bool, optional) – Whether to show the figure at runtime, defaults to True
save (bool, optional) – Whether to save the figure as .png file, defaults to False
- plot_acf(figsize=(12, 8), show=True, save=False, **kwargs)[source]
Plots autocorrelation function values of the parameter samples
- Parameters:
figsize (tuple, optional) – Size of the total figure (all plots together), defaults to (12, 8)
show (bool, optional) – Whether to show the figure at runtime, defaults to True
save (bool, optional) – Whether to save the figure as .png file, defaults to False
- plot_ergodic_mean(figsize=(12, 8), show=True, save=False, **kwargs)[source]
Plots ergodic mean(s) (for each parameter)
- Parameters:
figsize (tuple, optional) – Size of the total figure (all plots together), defaults to (12, 8)
show (bool, optional) – Whether to show the figure at runtime, defaults to True
save (bool, optional) – Whether to save the figure as .png file, defaults to False
- plot_histogram(figsize=(12, 8), bins=100, show=True, save=False)[source]
Plots histogram(s) (for each parameter) of posterior. Visually estimates posterior distribution
- Parameters:
figsize (tuple, optional) – Size of the total figure (all histograms together), defaults to (12, 8)
bins (int, optional) – Number of histogram bins, defaults to 100
show (bool, optional) – Whether to show the figure at runtime, defaults to True
save (bool, optional) – Whether to save the figure as .png file, defaults to False
- plot_histogram_fold(k=2, style='default', rcParams_update={}, show=True, save=False)[source]
Plots histograms of k-subsets of the samples
- Parameters:
k (int, optional) – Number of subsets taken from the Markov chain for histogram plotting, defaults to 2
style (str, optional) – matplotlib style to be used for plots. User can pass built-in matplotlib style (e.g. classic, fivethirtyeight), or a path to a custom style defined in a .mplstyle document, defaults to “default”
rcParams_update (dict, optional) – matplotlib.rcParams to modify the style defined by style argument, defaults to {} (no modification)
show (bool, optional) – Whether to show the plot, defaults to True
save (bool, optional) – Whether to save the plot, defaults to False
- quantiles(quantiles=array([0.25, 0.5, 0.75]))[source]
Calculates posterior quantiles.
- Parameters:
quantiles (ndarray, optional) – Quantiles, defaults to np.array([0.25, 0.5, 0.75])
- Returns:
Parameter values at quantiles
- Return type:
ndarray
- raftery_lewis(q=0.025, r=0.005, s=0.95)[source]
Calculates Raftery-Lewis diagnostic
- Parameters:
q (float, optional) – The quantile of interest, defaults to 0.025 for a lower tail quantile
r (float, optional) – The accuracy required, defaults to 0.005
s (float, optional) – The probability of achieving the specified accuracy, defaults to 0.95
- Returns:
Diagnostic results and specifications, including: quantile of interest, requires samples, burn-in samples, total samples, accuracy required, probability of reaching that accuracy
- Return type:
dict
- property samples
Gets or sets samples property
- Returns:
Samples property
- Return type:
ndarray
- save_samples(parameter_names=None, path=None)[source]
Exports samples as .csv file.
- Parameters:
path (str, optional) – Path where to save the .csv file. By default, it saves to the working directory, defaults to None
- stationarity_test(test='adf', **kwargs)[source]
Test for the stationarity of Markov chains
- Parameters:
test (str, optional) – Statistical test used. Available are Augmented Dickey-Fuller test (adf) and Kwiatkowski–Phillips–Schmidt–Shin test (kpss), defaults to “adf”
- Returns:
Test statistic, p-value
- Return type:
float, float
- std()[source]
Estimates the standard deviation of the posterior
- Returns:
Numpy array containing posterior standard deviation for every parameter
- Return type:
ndarray
- traceplots(figsize=(12, 8), show=True, save=False, **kwargs)[source]
Plots traceplot(s) (for each parameter)
- Parameters:
figsize (tuple, optional) – Size of the total figure (all plots together), defaults to (12, 8)
show (bool, optional) – Whether to show the figure at runtime, defaults to True
save (bool, optional) – Whether to save the figure as .png file, defaults to False