HistState#

class graph_tool.inference.HistState(x, weights=None, bins=None, alpha=2, pcount=1.0, bounded=None, discrete=None, conditional=None, categorical=None, **kwargs)[git][source]#

Bases: object

Bayesian nonparametric histogram of multivariate data.

Parameters:
xnumpy.ndarray of shape (N, D)

Data points, with one row for each of the \(N\) points, and one column for each of the \(D\) dimensions.

weightsiterable of int (optional, default: None)

Integer weights (multiplicities) of the data points. If not given, every point has weight one.

binslist of int or numpy.ndarray (optional, default: None)

Initial bins, with one entry for each dimension. An integer entry gives the number of bins of equal width that span the range of the data in that dimension, and an array gives the bin edges. If not given, a single bin is used for every dimension.

alphafloat (optional, default: 2)

Hyperparameter of the prior distribution of the bin edges.

pcountfloat (optional, default: 1)

Pseudocount of every bin, i.e. the concentration parameter of the Dirichlet prior of the bin probabilities.

boundedlist of pairs of bool (optional, default: None)

For each dimension, a pair (lower, upper) that specifies whether the lowest and the highest bin edges are fixed. If not given, the edges are not fixed.

discretelist of bool (optional, default: None)

For each dimension, whether its values are integers. If all dimensions are discrete, the values are stored as integers.

conditionalint (optional, default: None)

If given, only the first conditional dimensions are modelled, as a distribution conditioned on the values of the remaining ones. If not given, the joint distribution of all dimensions is modelled.

categoricallist of bool (optional, default: None)

For each dimension, whether its values are categories, in which case each distinct value has its own bin, and no bin edges are inferred. The categories must be non-negative integers. They are the distinct values in the data, unless an array of bin edges is given for that dimension, whose values, except the last one, are then the categories (which must include every value in the data).

Notes

The data are modelled by a histogram with piecewise constant density inside each (multidimensional) bin. The probabilities of the bins have a Dirichlet prior with concentration parameter pcount, and the bin edges (and hence their number) are latent variables, which are sampled with mcmc_sweep(), according to their posterior distribution. The value returned by entropy() is the negative joint log probability of the data and the bin edges, i.e. the description length of the data.

Examples

>>> x = np.random.normal(size=(1000, 1))
>>> state = gt.HistState(x, bins=[20])
>>> for i in range(100):
...     ret = state.mcmc_sweep()
>>> print(state.get_lpdf([0.]) > state.get_lpdf([2.]))
True

Methods

add_point(pos, val[, w])

Adds a data point with values val and weight w at position pos.

copy(**kwargs)

Copies the state.

entropy(**kwargs)

Returns the description length of the data and the bin edges, i.e. the negative logarithm of their joint probability.

get_cond_mean(x, j[, mle])

Returns the mean of dimension j, conditioned on the values of the other dimensions given by x (the value x[j] is ignored).

get_lpdf(x[, mle])

Returns the logarithm of the probability density at point x (conditioned on the remaining dimensions if conditional was given).

get_weights()

Returns the weights of the data points.

get_x()

Returns the data points as an array of shape (N, D).

mcmc_sweep([beta, niter, verbose])

Performs niter sweeps of a Metropolis-Hastings MCMC to sample the bin edges, at inverse temperature beta.

predictive_sample([x, n])

Returns n points sampled from the posterior predictive distribution, as an array of shape (n, conditional).

remove_point(pos)

Removes the data point at position pos.

replace_point(pos, x[, w])

Replaces the data point at position pos with values x and weight w.

trim_points()

Compacts the stored data by discarding the points that have been removed, which changes the positions of the remaining points.

virtual_replace_point_dS(pos, x)

Returns the change in description length if the data point at position pos were replaced with values x, without performing the replacement.

add_point(pos, val, w=1)[git][source]#

Adds a data point with values val and weight w at position pos.

copy(**kwargs)[git][source]#

Copies the state. The parameters override the state properties, and have the same meaning as in the constructor.

entropy(**kwargs)[git][source]#

Returns the description length of the data and the bin edges, i.e. the negative logarithm of their joint probability.

get_cond_mean(x, j, mle=True)[git][source]#

Returns the mean of dimension j, conditioned on the values of the other dimensions given by x (the value x[j] is ignored). The parameter mle has the same meaning as in get_lpdf().

get_lpdf(x, mle=True)[git][source]#

Returns the logarithm of the probability density at point x (conditioned on the remaining dimensions if conditional was given). If mle == True, the posterior mode of the bin probabilities is used (which is their maximum likelihood estimate for pcount == 1), otherwise their posterior mean.

get_weights()[git][source]#

Returns the weights of the data points.

get_x()[git][source]#

Returns the data points as an array of shape (N, D).

mcmc_sweep(beta=1.0, niter=1, verbose=False, **kwargs)[git][source]#

Performs niter sweeps of a Metropolis-Hastings MCMC to sample the bin edges, at inverse temperature beta. It returns a tuple (dS, nattempts, nmoves) with the change in description length, and the number of attempted and accepted moves.

predictive_sample(x=None, n=1)[git][source]#

Returns n points sampled from the posterior predictive distribution, as an array of shape (n, conditional). If conditional was given, x must contain the values of the conditioning dimensions.

remove_point(pos)[git][source]#

Removes the data point at position pos.

replace_point(pos, x, w=1)[git][source]#

Replaces the data point at position pos with values x and weight w.

trim_points()[git][source]#

Compacts the stored data by discarding the points that have been removed, which changes the positions of the remaining points.

virtual_replace_point_dS(pos, x)[git][source]#

Returns the change in description length if the data point at position pos were replaced with values x, without performing the replacement.