tbnpy.variable¶
Overview¶
The Variable class stores metadata about a Bayesian-network variable
(name, domain) and provides helper methods for working with composite states (sets of basic
states represented in a compact index form).
Quick start¶
from tbnpy.variable import Variable
# Discrete variable: values define the ordered basic states
v = Variable("damage_state", values=["none", "minor", "major"])
# Example: the composite state {1, 2} (i.e., {"minor","major"})
st = v.get_state({1, 2})
S = v.get_set(st)
Public API¶
Variable¶
- class Variable(name: str, values=None)¶
Manage information about a variable.
Parameters¶
- name
Variable name used as an identifier (hashing, ordering, printing).
- values
Domain specification.
Discrete variable: a
list/numpy.ndarrayof labels (states are indexed as0, 1, 2, ...in that order).Continuous variable: a
tuple(min, max)where bounds may be finite numbers or infinities (e.g.(-torch.inf, torch.inf)).None: allowed; indicates the domain is unspecified.
Attributes¶
- namestr
Variable name.
- valueslist | tuple | None
Domain specification as described above.
Notes¶
Ordering tip: when applicable, prefer an ordering where lower indices represent worse outcomes, because some modules may assume this convention (e.g.,
["failure", "survival"]so that0 < 1).
Properties¶
- Variable.name: str¶
Variable name.
- Variable.values: list | tuple | None¶
Variable domain specification (discrete labels or continuous bounds).
Composite-state helpers¶
The following methods work with composite states. A composite state represents a set of basic (discrete) states. Internally, the module uses a deterministic ordering over all non-empty subsets (e.g., all 1-element subsets, then all 2-element subsets, …, up to the full set).
- Variable.get_state(state_set: set) int¶
Convert a set of basic-state indices to its composite-state index.
Parameters¶
- state_set
A Python
setof integer indices intovalues(e.g.,{0},{1, 2}).
Returns¶
- int
Composite-state index representing the same subset.
Notes¶
state_setmust be a non-empty set.This method assumes a discrete variable (
valuesis list/ndarray).
Examples¶
v = Variable("x", ["low", "med", "high"]) v.get_state({1, 2}) # -> composite index for {"med","high"}
- Variable.get_set(state: int) set¶
Convert a composite-state index back to the corresponding set of basic-state indices.
Parameters¶
- state
Composite-state index (non-negative integer).
Returns¶
- set
A set of basic-state indices (e.g.,
{0, 2}).
- Variable.get_state_from_vector(vector) int¶
Convert a binary indicator vector to a composite-state index.
Parameters¶
- vector
A 1D list/array of length
len(values)with entries in{0,1}. A value of1indicates that the corresponding basic state is included in the subset.
Returns¶
- int
Composite-state index, or
-1if the vector is all zeros (empty set).