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:
objectBayesian nonparametric histogram of multivariate data.
- Parameters:
- x
numpy.ndarrayof 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
intornumpy.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.
- alpha
float(optional, default:2) Hyperparameter of the prior distribution of the bin edges.
- pcount
float(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.
- conditional
int(optional, default:None) If given, only the first
conditionaldimensions 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).
- x
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 withmcmc_sweep(), according to their posterior distribution. The value returned byentropy()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
valand weightwat positionpos.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 byx(the valuex[j]is ignored).get_lpdf(x[, mle])Returns the logarithm of the probability density at point
x(conditioned on the remaining dimensions ifconditionalwas given).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
nitersweeps of a Metropolis-Hastings MCMC to sample the bin edges, at inverse temperaturebeta.predictive_sample([x, n])Returns
npoints 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
poswith valuesxand weightw.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
poswere replaced with valuesx, without performing the replacement.- add_point(pos, val, w=1)[git][source]#
Adds a data point with values
valand weightwat positionpos.
- 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 byx(the valuex[j]is ignored). The parametermlehas the same meaning as inget_lpdf().
- get_lpdf(x, mle=True)[git][source]#
Returns the logarithm of the probability density at point
x(conditioned on the remaining dimensions ifconditionalwas given). Ifmle == True, the posterior mode of the bin probabilities is used (which is their maximum likelihood estimate forpcount == 1), otherwise their posterior mean.
- mcmc_sweep(beta=1.0, niter=1, verbose=False, **kwargs)[git][source]#
Performs
nitersweeps of a Metropolis-Hastings MCMC to sample the bin edges, at inverse temperaturebeta. 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
npoints sampled from the posterior predictive distribution, as an array of shape(n, conditional). Ifconditionalwas given,xmust contain the values of the conditioning dimensions.
- replace_point(pos, x, w=1)[git][source]#
Replaces the data point at position
poswith valuesxand weightw.