Estimate anything from a result

telescopic.sample() and telescopic.CoupledSampler.run() return a telescopic.Result. It stores each replicate’s signed measure, meeting time and cost, and the settings of the run, so every estimate is computed after the run.

The examples use a correlated Gaussian in two dimensions, whose moments are known (telescopic.examples.gaussian()), and 1000 replicates:

import numpy as np
from scipy import stats

import telescopic
from telescopic import examples

gaussian = examples.gaussian(2)  # N(0, [[1, 0.9], [0.9, 1]]), started from N(0, 4I)
result = telescopic.sample(
    gaussian.log_prob, gaussian.initial, 1000, vectorize=True, rng=11
)
result
Result(R=1000, d=2, k=37, m=370, lag=37, censored=0, interrupted=False)

A result prints as one line of its settings: \(R\), the dimension \(d\), k, m, lag, the number of censored pairs and whether the run was interrupted.

A few of the pairs met after k + lag, so a few replicates carry bias corrections.

Expectations

telescopic.Result.estimate() estimates \(\pi(h)\) for a function h that maps points of shape (n, d), a read-only array, to values of shape (n,) or (n, p); any other shape raises ValueError. It returns an telescopic.Estimate: the mean of the replicates’ estimates (value), its standard_error, a confidence interval at level (0.95 by default), an array of shape (2,) or (2, p) with the lower ends first, and the replicates’ estimates (replicates). The interval is the value plus or minus the standard error times the quantile of Student’s t with \(R - 1\) degrees of freedom (Theory). It needs at least 2 replicates; with 1, estimate raises ValueError. Here \(E[x_0 x_1]\) is the covariance, 0.9:

product = result.estimate(lambda x: x[:, 0] * x[:, 1])
product, product.interval
(Estimate(0.898137 ± 0.0056, R=1000), array([0.88712758, 0.90914603]))

Values of shape (n, p) give p estimates at once, and a boolean function gives probabilities. The exact \(P(x_i > 1)\) is \(1 - \Phi(1)\):

result.estimate(lambda x: x > 1), stats.norm.sf(1)
(Estimate([0.160075 ± 0.0015, 0.158934 ± 0.0015], R=1000),
 np.float64(0.15865525393145707))

telescopic.Result.mean() is estimate(lambda x: x). The value is unbiased for a number of replicates fixed in advance (How many replicates), and the interval is valid as \(R\) grows when the variance is finite (Theory).

Covariances and variances

telescopic.Result.cov() and telescopic.Result.var() are unbiased estimates of the covariance matrix and of the variances (Theory). A variance estimate can be negative, and the covariance matrix need not be positive semi-definite; a start far wider than the target can make them so (Initial distributions):

result.cov(), gaussian.cov
(array([[0.99796285, 0.89815531],
        [0.89815531, 0.99905393]]),
 array([[1. , 0.9],
        [0.9, 1. ]]))

telescopic.Result.std() returns \(\sqrt{\max(0, \widehat{\mathrm{Var}})}\) for each coordinate, which is consistent but not unbiased:

result.std()
array([0.9989809 , 0.99952685])

Quantiles and credible intervals

telescopic.Result.quantile() returns quantiles of each coordinate under the pooled signed measures of the replicates, shape (d,) for one level and (len(q), d) for several. telescopic.Result.credible_interval() returns the central credible interval that holds a given mass of each marginal (0.68 by default): the quantiles at \((1 - \mathrm{mass})/2\) and \((1 + \mathrm{mass})/2\), shape (2, d). It describes the spread of the target, where the interval of an estimate is a confidence interval for an expectation. Quantiles and credible intervals are consistent as \(R\) grows, not unbiased, and come without standard errors; the method is on the theory page.

result.quantile([0.05, 0.5, 0.95]), stats.norm.ppf([0.05, 0.5, 0.95])
(array([[-1.64251240e+00, -1.65370955e+00],
        [ 1.24855446e-04,  1.15797416e-04],
        [ 1.64434705e+00,  1.64403778e+00]]),
 array([-1.64485363,  0.        ,  1.64485363]))
result.credible_interval(0.9)
array([[-1.6425124 , -1.65370955],
       [ 1.64434705,  1.64403778]])

Both take a function h, as estimate does, for the quantiles of a quantity derived from the coordinates: those of its values at the atoms, with each atom’s weight divided by \(R\) (the quantiles of telescopic.estimators.weighted_quantile()). They have shape () or (p,) for one level and (len(q),) or (len(q), p) for several, and the credible interval has shape (2,) or (2, p). The sum \(x_0 + x_1\) is Gaussian, with a variance equal to the sum of the entries of the covariance matrix:

levels = [0.05, 0.5, 0.95]
(
    result.quantile(levels, lambda x: x[:, 0] + x[:, 1]),
    stats.norm.ppf(levels, scale=np.sqrt(gaussian.cov.sum())),
)
(array([-3.21409906, -0.00405315,  3.20097927]),
 array([-3.20641001,  0.        ,  3.20641001]))

Histograms

telescopic.Result.histogram() returns the mass of each bin, its standard error and the edges of the bins. The mass of a bin is the average over replicates of their signed mass in it. The bins are half-open, \([a, b)\), except the last, which is closed, as in numpy.histogram, so an atom on the upper edge of range is counted. It is unbiased for a range fixed before the run, and it can be negative where few atoms fall and the negative weights of the bias corrections outweigh the others. By default the bins span the atoms, so the masses sum to 1, and since the atoms set the edges, every bin is biased, the two outer ones by a term of order \(1/R\):

masses, errors, edges = result.histogram(0, bins=10)
masses, errors, bool(np.isclose(masses.sum(), 1))
(array([-2.99401198e-06,  8.98203593e-06,  1.07485030e-03,  2.04880240e-02,
         1.45685629e-01,  3.71568862e-01,  3.37482036e-01,  1.10745509e-01,
         1.24790419e-02,  4.70059880e-04]),
 array([2.99401198e-06, 8.98203593e-06, 1.24164490e-04, 5.61099512e-04,
        1.38969949e-03, 1.92539586e-03, 1.92555630e-03, 1.24462230e-03,
        4.16944190e-04, 7.68598511e-05]),
 True)

A sequence of coordinates gives a joint histogram of shape (bins,) * len(dims), whose entry [i, j] is bin i of the first coordinate and bin j of the second. range sets the outer edges, a pair per coordinate; atoms outside them fall in no bin:

joint, joint_errors, (edges_0, edges_1) = result.histogram(
    [0, 1], bins=8, range=[(-3, 3), (-3, 3)]
)
joint.shape, joint.sum()
((8, 8), np.float64(0.9959550898203597))

The masses are not densities: divide them by the widths of the bins, as plot_marginals does.

Two sets of replicates

A run with keep (Keeping a subset) stores every replicate’s moments but the signed measures of the replicates of index below keep only. mean(), var(), std() and, up to 100 dimensions, cov() come from all \(R\) replicates that met. estimate(), the quantiles, the credible intervals, the histograms and their plots, measure, chains() and to_datatree() come from the n_kept kept replicates that met, and so does cov above 100 dimensions, whose diagonal can then differ from var. Each set gives unbiased estimates, or consistent ones for quantiles, with error bars from its own replicates, and the summary names the replicates behind each column. The two sets are not independent: an estimate from one combined with one from the other has no error bar of its own.

Signed measures

result.measure is a telescopic.SignedMeasure: the atoms, of shape (N, d), their weights, and for each atom the position, from 0 to \(R - 1\), of the replicate it belongs to, in nondecreasing order, so that each replicate’s atoms are consecutive. These attributes are read-only views, without a copy, of the arrays the measure was built from; those arrays, such as the ones of the replicates that telescopic.engine.run() returns, stay writable, and telescopic never writes to them. Each replicate’s weights sum to 1. Its bias corrections add pairs of atoms with weights of opposite signs (Theory), so an estimate can fall outside the range of \(h\):

measure = result.measure
negative = measure.weights < 0
{
    "atoms": measure.atoms.shape,
    "weights sum to 1": bool(np.allclose(np.bincount(measure.replicate, measure.weights), 1)),
    "negative weights": int(negative.sum()),
    "replicates with negative weights": len(np.unique(measure.replicate[negative])),
}
{'atoms': (334190, 2),
 'weights sum to 1': True,
 'negative weights': 95,
 'replicates with negative weights': 13}

telescopic.SignedMeasure.per_replicate() returns each replicate’s estimate \(\hat\pi_r(h)\), shape (R,) or (R, p), for statistics of your own; an Estimate holds the same values in replicates:

second = measure.per_replicate(lambda x: x[:, 0] ** 2)
second.shape, np.array_equal(second, result.estimate(lambda x: x[:, 0] ** 2).replicates)
((1000,), True)