state - Sampling history for MCMC

MCMCDraw
load_state
save_state

Sampling history for MCMC.

MCMC keeps track of a number of things during sampling.

The results may be queried as follows:

draws, generation, thinning
sample(condition) returns draws, points, logp
logp()            returns draws, logp
acceptance_rate() returns draws, AR
chains()          returns draws, chains, logp
R_stat()          returns draws, R
CR_weight()       returns draws, CR_weight
best()            returns best_x, best_logp
outliers()        returns outliers
show()/save(file)/load(file)

Data is stored in circular arrays, which keeps the last N generations and throws the rest away.

draws is the total number of draws from the sampler.

generation is the total number of generations.

thinning is the number of generations per stored sample.

draws[i] is the number of draws including those required to produce the information in the corresponding return vector. Note that draw numbers need not be linearly spaced, since techniques like delayed rejection will result in a varying number of samples per generation.

logp[i] is the set of log likelihoods, one for each member of the population. The logp() method returns the complete set, and the sample() method returns a thinned set, with on element of logp[i] for each vector point[i, :].

AR[i] is the acceptance rate at generation i, showing the proportion of proposed points which are accepted into the population.

chains[i, :, :] is the set of points in the differential evolution population at thinned generation i. Ideally, the thinning rate of the MCMC process is chosen so that thinned generations i and i+1 are independent samples from the posterior distribution, though there is a chance that this may not be the case, and indeed, some points in generation i+1 may be identical to those in generation i. Actual generation number is i*thinning.

points[i, :] is the ith point in a returned sample. The i is just a place holder; there is no inherent ordering to the sample once they have been extracted from the chains. Note that the sample may be from a marginal distribution.

R[i] is the Gelman R statistic measuring convergence of the Markov chain.

CR_weight[i] is the set of weights used for selecting between the crossover ratios available to the candidate generation process of differential evolution. These will be fixed early in the sampling, even when adaptive differential evolution is selected.

outliers[i] is a vector containing the thinned generation number at which an outlier chain was removed, the id of the chain that was removed and the id of the chain that replaced it. We leave it to the reader to decide if the cloned samples, point[:generation, :, removed_id], should be included in further analysis.

best_logp is the highest log likelihood observed during the analysis and best_x is the corresponding point at which it was observed.

generation is the last generation number

class bumps.dream.state.MCMCDraw(Ngen, Nthin, Nupdate, Nvar, Npop, Ncr, thinning)[source]

Bases: object

CR_weight()[source]

Return the crossover ratio weights to be used in the next generation.

For example, to see if the adaptive CR is stable use:

draw, weight = state.CR_weight()
plot(draw, weight)

See dream.crossover for details.

Ncr
Ngen
Npop
Nthin
Nupdate
Nvar

Number of parameters in the fit

R_stat()[source]

Return the R-statistics convergence statistic for each variable.

For example, to plot the convergence of all variables over time:

draw, R = state.R_stat()
plot(draw, R)

See dream.gelman and references detailed therein.

acceptance_rate()[source]

Return the iteration number and the acceptance rate for that iteration.

For example, to plot the acceptance rate over time:

draw, AR = state.acceptance_rate()
plot(draw, AR)
best()[source]

Return the best point seen and its log likelihood.

chains()[source]

Returns the observed Markov chains and the corresponding likelihoods.

The return value is a tuple (draws, chains, logp).

draws is the number of samples taken up to and including the samples for the current generation.

chains is a three dimensional array of generations X chains X vars giving the set of points observed for each chain in every generation. Only the thinned samples are returned.

logp is a two dimensional array of generation X population giving the log likelihood of observing the set of variable values given in chains.

derive_vars(fn, labels=None)[source]

Generate derived variables from the current sample, adding columns for the derived variables to each sample of every chain.

The new columns are treated as part of the sample.

fn is a function taking points p[:, k] for k in 0 … samples and returning a set of derived variables pj[k] for each sample k. The variables can be returned as any kind of sequence including an array or a tuple with one entry per variable. The caller uses asarray to convert the returned variables into a vars X samples array. For convenience, a single variable can be returned by itself.

labels are the labels to use for the derived variables.

The following example adds the new variable x+y = P[0] + P[1]:

state.derive_vars(lambda p: p[0]+p[1], labels=["x+y"])
draw(portion=1, vars=None, selection=None)[source]

Return a sample from the posterior distribution.

portion is the portion of each chain to use vars is a list of variables to return for each point selection sets the range for the returned marginal distribution

selection is a dictionary of {variable: (low, high)} to set the range on each variable. Missing variables default to the full range.

To plot the distribution for parameter p1:

draw = state.draw()
hist(draw.points[:, 0])

To plot the interdependence of p1 and p2:

draw = state.sample()
plot(draw.points[:, 0], draw.points[:, 1], '.')
entropy(**kw)[source]

Return entropy estimate and uncertainty from an MCMC draw.

See entropy.entropy() for details.

keep_best()[source]

Place the best point at the end of the chain final good chain.

Good chains are defined by mark_outliers.

Because the Markov chain is designed to wander the parameter space, the best individual seen during the random walk may have been observed during the burn-in period, and may no longer be present in the chain. If this is the case, replace the final point with the best, otherwise swap the positions of the final and the best.

labels
logp(full=False)[source]

Return the iteration number and the log likelihood for each point in the individual sequences in that iteration.

For example, to plot the convergence of each sequence:

draw, logp = state.logp()
plot(draw, logp)

Note that draw[i] represents the total number of samples taken, including those for the samples in logp[i].

If full is True, then return all chains, not just good chains.

mark_outliers(test='IQR', portion=1.0)[source]

Mark some chains as outliers but don’t remove them. This can happen after drawing is complete, so that chains that did not converge are not included in the statistics.

test is ‘IQR’, ‘Mahol’ or ‘none’.

portion indicates what portion of the samples should be included in the outlier test. The default is to include all of them.

outliers()[source]

Return a list of outlier removal operations.

Each outlier operation is a tuple giving the thinned generation in which it occurred, the old chain id and the new chain id.

The chains themselves have already been updated to reflect the removal.

Curiously, it is possible for the maximum likelihood seen so far to be removed by this operation.

remove_outliers(x, logp, test='IQR', portion=0.5)[source]

Replace outlier chains with clones of good ones. This should happen early in the sampling processes so the clones have an opportunity to evolve their own identity.

state contains the chains, with log likelihood for each point.

x, logp are the current population and the corresponding log likelihoods

test is the name of the test to use (one of IQR, Grubbs, Mahal or none).

portion in (0, 1] is the amount of the chain to use

Updates state, x and logp to reflect the changes.

See outliers for details.

resize(Ngen, Nthin, Nupdate, Nvar, Npop, Ncr, thinning)[source]
sample(**kw)[source]

Return a sample from the posterior distribution.

Deprecated use draw() instead.

save(filename)[source]
set_integer_vars(labels)[source]

Indicate tha variables should be considered integer variables when computing statistics.

set_visible_vars(labels)[source]
show(portion=1.0, figfile=None)[source]
title = None
bumps.dream.state.load_state(filename, skip=0, report=0, derived_vars=0)[source]
bumps.dream.state.save_state(state, filename)[source]