pyhs3.context.Context¶
- class pyhs3.context.Context(parameters, auxiliaries=None, observables=None, views=None)[source]¶
Context for parameter resolution in HS3 expressions.
Provides access to both user-provided parameters and model-computed values (such as auxiliary parameters used by constraints). Distinguishes between parameters and computed values while providing a unified access interface.
- Parameters:
- __init__(parameters, auxiliaries=None, observables=None, views=None)[source]¶
Initialize context with parameter data.
- Parameters:
parameters (
Mapping[str,TensorVar]) – Dictionary of user-provided parameters (1-D leaves for vectors)auxiliaries (
Mapping[str,TensorVar] |None) – Dictionary of model-computed auxiliary valuesobservables (
Mapping[str,tuple[TensorVar,TensorVar]] |None) – Dictionary mapping observable names to (lower, upper) bound tuplesviews (
Mapping[str,TensorVar] |None) – ExpandDims views for broadcasting — observable leaves are (N, 1), non-observable vector overrides are (1, N). When present, looking up an observable name viacontext[name]returns the view so that distribution expressions get the right broadcast shape.context.parameters[name]always returns the raw leaf.
- Raises:
ValueError – If there’s any overlap between parameter and auxiliary names
Methods
__init__(parameters[, auxiliaries, ...])Initialize context with parameter data.
add_log_density(key, log_value)Register the log-space tensor of the distribution
keyin place.add_parameter(key, value)Insert a single resolved parameter tensor into the context in place.
add_view(key, view)Register a broadcasting view for
keyon this context in place.copy()Create a copy of this context.
items()Get all parameter name-value pairs (both parameters and auxiliaries).
keys()Get all parameter names (both parameters and auxiliaries).
log_density(key)Log-space tensor of the distribution
key.values()Get all parameter values (both parameters and auxiliaries).
Attributes
Get read-only view of auxiliaries.
Observable names mapped to (lower, upper) bound PyTensor expressions.
Get read-only view of parameters.
- Context.add_log_density(key, log_value)[source]¶
Register the log-space tensor of the distribution
keyin place.The model builder records each built distribution’s log-space expression here alongside its probability-space value (
context[key], which ispt.expof it), so that composite distributions can combine their components in log space vialog_density().
- Context.add_parameter(key, value)[source]¶
Insert a single resolved parameter tensor into the context in place.
Used by the model builder to grow one long-lived context incrementally as nodes are built in topological order, rather than reconstructing a fresh context per node. Preserves the parameter/auxiliary overlap invariant enforced in
__init__().- Parameters:
- Raises:
ValueError – If
keyalready exists among the auxiliaries.- Return type:
- Context.add_view(key, view)[source]¶
Register a broadcasting view for
keyon this context in place.Mirrors the
viewsargument of__init__()but lets the model builder add the ExpandDims view of a vector parameter at the moment that parameter node is built, so subsequent nodes see the correct broadcast shape viacontext[key].
- Context.log_density(key)[source]¶
Log-space tensor of the distribution
key.Returns the tensor registered with
add_log_density(). A name without one (e.g. a context assembled by hand rather than by the model builder) givespt.log(context[key]), which is the same value wherevercontext[key]does not underflow to 0.0.
- Context.auxiliaries¶
Get read-only view of auxiliaries.
- Context.observables¶
Observable names mapped to (lower, upper) bound PyTensor expressions.
- Context.parameters¶
Get read-only view of parameters.