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 values

  • observables (Mapping[str, tuple[TensorVar, TensorVar]] | None) – Dictionary mapping observable names to (lower, upper) bound tuples

  • views (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 via context[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 key in 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 key on 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

auxiliaries

Get read-only view of auxiliaries.

observables

Observable names mapped to (lower, upper) bound PyTensor expressions.

parameters

Get read-only view of parameters.

Context.add_log_density(key, log_value)[source]

Register the log-space tensor of the distribution key in place.

The model builder records each built distribution’s log-space expression here alongside its probability-space value (context[key], which is pt.exp of it), so that composite distributions can combine their components in log space via log_density().

Parameters:
  • key (str) – Distribution name.

  • log_value (TensorVar) – Log-space tensor of that distribution, with the same shape as context[key].

Return type:

None

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:
  • key (str) – Parameter name to insert.

  • value (TensorVar) – Resolved PyTensor variable for the parameter.

Raises:

ValueError – If key already exists among the auxiliaries.

Return type:

None

Context.add_view(key, view)[source]

Register a broadcasting view for key on this context in place.

Mirrors the views argument 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 via context[key].

Parameters:
  • key (str) – Parameter name the view belongs to.

  • view (TensorVar) – ExpandDims view tensor (observable leaves are (N, 1), non-observable vector overrides are (1, N)).

Return type:

None

Context.copy()[source]

Create a copy of this context.

Return type:

Context

Context.items()[source]

Get all parameter name-value pairs (both parameters and auxiliaries).

Return type:

ItemsView[str, TensorVar]

Context.keys()[source]

Get all parameter names (both parameters and auxiliaries).

Return type:

KeysView[str]

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) gives pt.log(context[key]), which is the same value wherever context[key] does not underflow to 0.0.

Parameters:

key (str) – Distribution name.

Returns:

The log-space tensor.

Return type:

TensorVar

Raises:

KeyError – If key is neither registered nor in the context.

Context.values()[source]

Get all parameter values (both parameters and auxiliaries).

Return type:

ValuesView[TensorVar]

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.