harv.stats package¶
Statistical utilities.
- class harv.stats.MarginalizedLinear¶
Bases:
Distribution- __init__(design_matrix, prior_distribution, data_distribution, *, validate_args=None)¶
- arg_constraints: dict[str, Any] = {'design_matrix': Real()}¶
- property batch_shape: tuple[int, ...]¶
Returns the shape over which the distribution parameters are batched.
- cdf(value)¶
The cumulative distribution function of this distribution.
- conditional(value=None)¶
- property covariance_matrix¶
- entropy()¶
Returns the entropy of the distribution.
- enumerate_support(expand=True)¶
Returns an array with shape len(support) x batch_shape containing all values in the support.
- property event_shape: tuple[int, ...]¶
Returns the shape of a single sample from the distribution without batching.
- expand(batch_shape)¶
Returns a new
ExpandedDistributioninstance with batch dimensions expanded to batch_shape.
- expand_by(sample_shape)¶
Expands a distribution by adding
sample_shapeto the left side of itsbatch_shape. To expand internal dims ofself.batch_shapefrom 1 to something larger, useexpand()instead.
- has_enumerate_support: bool = False¶
- icdf(q)¶
The inverse cumulative distribution function of this distribution.
- classmethod infer_shapes(*args, **kwargs)¶
Infers
batch_shapeandevent_shapegiven shapes of args to__init__().Note
This assumes distribution shape depends only on the shapes of tensor inputs, not in the data contained in those inputs.
- Parameters:
- Returns:
A pair
(batch_shape, event_shape)of the shapes of a distribution that would be created with input args of the given shapes.- Return type:
- log_prob(value)¶
Evaluates the log probability density for a batch of samples given by value.
- Parameters:
value – A batch of samples from the distribution.
- Returns:
an array with shape value.shape[:-self.event_shape]
- Return type:
ArrayLike
- mask(mask)¶
Masks a distribution by a boolean or boolean-valued array that is broadcastable to the distributions
Distribution.batch_shape.- Parameters:
mask (bool or jnp.ndarray) – A boolean or boolean valued array (True includes a site, False excludes a site).
- Returns:
A masked copy of this distribution.
- Return type:
MaskedDistribution
Example:
- property mean¶
Mean of the distribution.
- pytree_aux_fields: tuple[str, ...] = ('_batch_shape', '_event_shape')¶
- pytree_data_fields: tuple[str, ...] = ()¶
- reparametrized_params: list[str] = ['design_matrix']¶
- rsample(key, sample_shape=())¶
- sample(key, sample_shape=())¶
Returns a sample from the distribution having shape given by sample_shape + batch_shape + event_shape. Note that when sample_shape is non-empty, leading dimensions (of size sample_shape) of the returned sample will be filled with iid draws from the distribution instance.
- sample_with_intermediates(key, sample_shape=())¶
Same as
sampleexcept that any intermediate computations are returned (useful for TransformedDistribution).
- shape(sample_shape=())¶
The tensor shape of samples from this distribution.
Samples are of shape:
d.shape(sample_shape) == sample_shape + d.batch_shape + d.event_shape
- support = RealVector(Real(), 1)¶
- to_event(reinterpreted_batch_ndims=None)¶
Interpret the rightmost reinterpreted_batch_ndims batch dimensions as dependent event dimensions.
- tree_flatten()¶
- classmethod tree_unflatten(aux_data, params)¶
- validate_args(strict=True)¶
Validate the arguments of the distribution.
Submodules¶
harv.stats.linear_op module¶
Vendored linear_op, formerly from numpyro-ext.
- class harv.stats.linear_op.LinearOp¶
Bases:
NamedTupleLinearOp(loc, covariance, inverse, solve_tril, half_log_det)
- count(value, /)¶
Return number of occurrences of value.
- index(value, start=0, stop=9223372036854775807, /)¶
Return first index of value.
Raises ValueError if the value is not present.
harv.stats.numpyro_ext module¶
Vendored numpyro extensions, formerly from numpyro-ext.
- class harv.stats.numpyro_ext.UnitDisk¶
Bases:
DistributionTwo dimensional parameters constrained to live within the unit disk
- support = UnitDiskConstraint()¶
- __init__(*, validate_args=None)¶
- sample(key, sample_shape=())¶
Returns a sample from the distribution having shape given by sample_shape + batch_shape + event_shape. Note that when sample_shape is non-empty, leading dimensions (of size sample_shape) of the returned sample will be filled with iid draws from the distribution instance.
- log_prob(value)¶
Evaluates the log probability density for a batch of samples given by value.
- Parameters:
value – A batch of samples from the distribution.
- Returns:
an array with shape value.shape[:-self.event_shape]
- Return type:
ArrayLike
- property mean¶
Mean of the distribution.
- property variance¶
Variance of the distribution.
- arg_constraints: dict[str, Any] = {}¶
- property batch_shape: tuple[int, ...]¶
Returns the shape over which the distribution parameters are batched.
- cdf(value)¶
The cumulative distribution function of this distribution.
- entropy()¶
Returns the entropy of the distribution.
- enumerate_support(expand=True)¶
Returns an array with shape len(support) x batch_shape containing all values in the support.
- property event_shape: tuple[int, ...]¶
Returns the shape of a single sample from the distribution without batching.
- expand(batch_shape)¶
Returns a new
ExpandedDistributioninstance with batch dimensions expanded to batch_shape.
- expand_by(sample_shape)¶
Expands a distribution by adding
sample_shapeto the left side of itsbatch_shape. To expand internal dims ofself.batch_shapefrom 1 to something larger, useexpand()instead.
- has_enumerate_support: bool = False¶
- icdf(q)¶
The inverse cumulative distribution function of this distribution.
- classmethod infer_shapes(*args, **kwargs)¶
Infers
batch_shapeandevent_shapegiven shapes of args to__init__().Note
This assumes distribution shape depends only on the shapes of tensor inputs, not in the data contained in those inputs.
- Parameters:
- Returns:
A pair
(batch_shape, event_shape)of the shapes of a distribution that would be created with input args of the given shapes.- Return type:
- mask(mask)¶
Masks a distribution by a boolean or boolean-valued array that is broadcastable to the distributions
Distribution.batch_shape.- Parameters:
mask (bool or jnp.ndarray) – A boolean or boolean valued array (True includes a site, False excludes a site).
- Returns:
A masked copy of this distribution.
- Return type:
MaskedDistribution
Example:
- pytree_aux_fields: tuple[str, ...] = ('_batch_shape', '_event_shape')¶
- pytree_data_fields: tuple[str, ...] = ()¶
- reparametrized_params: list[str] = []¶
- rsample(key, sample_shape=())¶
- sample_with_intermediates(key, sample_shape=())¶
Same as
sampleexcept that any intermediate computations are returned (useful for TransformedDistribution).
- shape(sample_shape=())¶
The tensor shape of samples from this distribution.
Samples are of shape:
d.shape(sample_shape) == sample_shape + d.batch_shape + d.event_shape
- to_event(reinterpreted_batch_ndims=None)¶
Interpret the rightmost reinterpreted_batch_ndims batch dimensions as dependent event dimensions.
- classmethod tree_unflatten(aux_data, params)¶
- class harv.stats.numpyro_ext.Angle¶
Bases:
DistributionAn angle constrained to be in the range -pi to pi
The actual sampling is performed in the two dimensional vector space proportional to
(sin(theta), cos(theta))so that the sampler doesn’t see a discontinuity at pi.The
regularizedparameter can be used to improve sampling performance when the value of the angle is well constrained. It removes prior mass near the origin in the sampling space, which can lead to bad geometry when the angle is poorly constrained, but better performance when it is. The default value of10.0is a good starting point.- __init__(*, regularized=10.0, validate_args=None)¶
- property support¶
The support of this distribution. Subclasses can override this as a class attribute or as a property.
- sample(key, sample_shape=())¶
Returns a sample from the distribution having shape given by sample_shape + batch_shape + event_shape. Note that when sample_shape is non-empty, leading dimensions (of size sample_shape) of the returned sample will be filled with iid draws from the distribution instance.
- log_prob(value)¶
Evaluates the log probability density for a batch of samples given by value.
- Parameters:
value – A batch of samples from the distribution.
- Returns:
an array with shape value.shape[:-self.event_shape]
- Return type:
ArrayLike
- property mean¶
Mean of the distribution.
- property variance¶
Variance of the distribution.
- cdf(value)¶
The cumulative distribution function of this distribution.
- Parameters:
value – samples from this distribution.
- Returns:
output of the cumulative distribution function evaluated at value.
- icdf(value)¶
The inverse cumulative distribution function of this distribution.
- Parameters:
q – quantile values, should belong to [0, 1].
- Returns:
the samples whose cdf values equals to q.
- arg_constraints: dict[str, Any] = {}¶
- property batch_shape: tuple[int, ...]¶
Returns the shape over which the distribution parameters are batched.
- entropy()¶
Returns the entropy of the distribution.
- enumerate_support(expand=True)¶
Returns an array with shape len(support) x batch_shape containing all values in the support.
- property event_shape: tuple[int, ...]¶
Returns the shape of a single sample from the distribution without batching.
- expand(batch_shape)¶
Returns a new
ExpandedDistributioninstance with batch dimensions expanded to batch_shape.
- expand_by(sample_shape)¶
Expands a distribution by adding
sample_shapeto the left side of itsbatch_shape. To expand internal dims ofself.batch_shapefrom 1 to something larger, useexpand()instead.
- has_enumerate_support: bool = False¶
- classmethod infer_shapes(*args, **kwargs)¶
Infers
batch_shapeandevent_shapegiven shapes of args to__init__().Note
This assumes distribution shape depends only on the shapes of tensor inputs, not in the data contained in those inputs.
- Parameters:
- Returns:
A pair
(batch_shape, event_shape)of the shapes of a distribution that would be created with input args of the given shapes.- Return type:
- mask(mask)¶
Masks a distribution by a boolean or boolean-valued array that is broadcastable to the distributions
Distribution.batch_shape.- Parameters:
mask (bool or jnp.ndarray) – A boolean or boolean valued array (True includes a site, False excludes a site).
- Returns:
A masked copy of this distribution.
- Return type:
MaskedDistribution
Example:
- pytree_aux_fields: tuple[str, ...] = ('_batch_shape', '_event_shape')¶
- pytree_data_fields: tuple[str, ...] = ()¶
- reparametrized_params: list[str] = []¶
- rsample(key, sample_shape=())¶
- sample_with_intermediates(key, sample_shape=())¶
Same as
sampleexcept that any intermediate computations are returned (useful for TransformedDistribution).
- shape(sample_shape=())¶
The tensor shape of samples from this distribution.
Samples are of shape:
d.shape(sample_shape) == sample_shape + d.batch_shape + d.event_shape
- to_event(reinterpreted_batch_ndims=None)¶
Interpret the rightmost reinterpreted_batch_ndims batch dimensions as dependent event dimensions.
- classmethod tree_unflatten(aux_data, params)¶
- class harv.stats.numpyro_ext.MarginalizedLinear¶
Bases:
Distribution- arg_constraints: dict[str, Any] = {'design_matrix': Real()}¶
- support = RealVector(Real(), 1)¶
- reparametrized_params: list[str] = ['design_matrix']¶
- __init__(design_matrix, prior_distribution, data_distribution, *, validate_args=None)¶
- sample_with_intermediates(key, sample_shape=())¶
Same as
sampleexcept that any intermediate computations are returned (useful for TransformedDistribution).
- sample(key, sample_shape=())¶
Returns a sample from the distribution having shape given by sample_shape + batch_shape + event_shape. Note that when sample_shape is non-empty, leading dimensions (of size sample_shape) of the returned sample will be filled with iid draws from the distribution instance.
- log_prob(value)¶
Evaluates the log probability density for a batch of samples given by value.
- Parameters:
value – A batch of samples from the distribution.
- Returns:
an array with shape value.shape[:-self.event_shape]
- Return type:
ArrayLike
- conditional(value=None)¶
- tree_flatten()¶
- classmethod tree_unflatten(aux_data, params)¶
- property batch_shape: tuple[int, ...]¶
Returns the shape over which the distribution parameters are batched.
- cdf(value)¶
The cumulative distribution function of this distribution.
- entropy()¶
Returns the entropy of the distribution.
- enumerate_support(expand=True)¶
Returns an array with shape len(support) x batch_shape containing all values in the support.
- property event_shape: tuple[int, ...]¶
Returns the shape of a single sample from the distribution without batching.
- expand(batch_shape)¶
Returns a new
ExpandedDistributioninstance with batch dimensions expanded to batch_shape.
- expand_by(sample_shape)¶
Expands a distribution by adding
sample_shapeto the left side of itsbatch_shape. To expand internal dims ofself.batch_shapefrom 1 to something larger, useexpand()instead.
- has_enumerate_support: bool = False¶
- icdf(q)¶
The inverse cumulative distribution function of this distribution.
- classmethod infer_shapes(*args, **kwargs)¶
Infers
batch_shapeandevent_shapegiven shapes of args to__init__().Note
This assumes distribution shape depends only on the shapes of tensor inputs, not in the data contained in those inputs.
- Parameters:
- Returns:
A pair
(batch_shape, event_shape)of the shapes of a distribution that would be created with input args of the given shapes.- Return type:
- mask(mask)¶
Masks a distribution by a boolean or boolean-valued array that is broadcastable to the distributions
Distribution.batch_shape.- Parameters:
mask (bool or jnp.ndarray) – A boolean or boolean valued array (True includes a site, False excludes a site).
- Returns:
A masked copy of this distribution.
- Return type:
MaskedDistribution
Example:
- property mean¶
Mean of the distribution.
- pytree_aux_fields: tuple[str, ...] = ('_batch_shape', '_event_shape')¶
- pytree_data_fields: tuple[str, ...] = ()¶
- rsample(key, sample_shape=())¶
- shape(sample_shape=())¶
The tensor shape of samples from this distribution.
Samples are of shape:
d.shape(sample_shape) == sample_shape + d.batch_shape + d.event_shape
- to_event(reinterpreted_batch_ndims=None)¶
Interpret the rightmost reinterpreted_batch_ndims batch dimensions as dependent event dimensions.
- validate_args(strict=True)¶
Validate the arguments of the distribution.
- property variance: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray¶
Variance of the distribution.
- property covariance_matrix¶