Model Checking¶
API Reference¶
Base Class¶
masa.common.pctl.BoundedPCTLModelChecker ¶
BoundedPCTLModelChecker(formula: BoundedPCTLFormula, label_fn: LabelFn, atomic_predicates: List[str], transition_matrix: Optional[ndarray] = None, successor_states: Optional[ndarray] = None, probabilities: Optional[ndarray] = None)
Shared base for bounded PCTL model checkers.
This class stores:
- A bounded PCTL formula (
formula). - A labeling function (
label_fn) and a precomputed vectorized labeling matrix (vec_label_fn). - A transition representation in one of two forms:
Dense MDP kernel (mode="full")
transition_matrix with shape (n_states, n_states, n_actions).
The convention used throughout this module is (next_state, state, action).
Compact successor kernel (mode="compact")
successor_states with shape (K, n_states) and
probabilities with shape (K, n_states, n_actions).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
BoundedPCTLFormula
|
The bounded PCTL formula to evaluate. |
required |
label_fn
|
LabelFn
|
A |
required |
atomic_predicates
|
List[str]
|
List of atom names (strings). These define the row
ordering of |
required |
transition_matrix
|
Optional[ndarray]
|
Optional dense MDP kernel of shape
|
None
|
successor_states
|
Optional[ndarray]
|
Optional compact successor ids of shape |
None
|
probabilities
|
Optional[ndarray]
|
Optional compact probabilities of shape
|
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
formula |
Stored formula. |
|
label_fn |
Stored labeling function. |
|
atomic_predicates |
Atom vocabulary used to build |
|
atom_dict |
Mapping from atom name to row index in |
|
mode |
Either |
|
n_states |
Number of states. |
|
n_actions |
Number of actions. |
|
vec_label_fn |
Float64 matrix of shape |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If kernel shapes are inconsistent. |
Source code in masa/common/pctl.py
atom_dict
instance-attribute
¶
_build_vec_label_fn ¶
Build vec_label_fn from label_fn.
Returns:
| Type | Description |
|---|---|
ndarray
|
Float64 array |
ndarray
|
|
Source code in masa/common/pctl.py
update_kernel ¶
update_kernel(transition_matrix: Optional[ndarray] = None, successor_states: Optional[ndarray] = None, probabilities: Optional[ndarray] = None)
Update the stored transition representation in-place.
Exactly one update mode should be used:
- Dense update: provide
transition_matrixwith the same shape as the original. - Compact update: provide both
successor_statesandprobabilitieswith shapes consistent with the original compact representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transition_matrix
|
Optional[ndarray]
|
New dense MDP kernel |
None
|
successor_states
|
Optional[ndarray]
|
New successor ids |
None
|
probabilities
|
Optional[ndarray]
|
New successor probabilities |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If shapes do not match the originally configured representation. |
Source code in masa/common/pctl.py
Exact Model Checking¶
masa.common.pctl.ExactModelChecker ¶
ExactModelChecker(formula: BoundedPCTLFormula, label_fn: LabelFn, atomic_predicates: List[str], transition_matrix: Optional[ndarray] = None, successor_states: Optional[ndarray] = None, probabilities: Optional[ndarray] = None)
Bases: BoundedPCTLModelChecker
Exact model checker for bounded PCTL under a fixed policy.
The exact checker collapses an MDP into a Markov chain by applying a stochastic policy, then evaluates the bounded formula on the resulting Markov chain using the formula's internal recurrences.
check_statereturns per-state satisfaction for the policy-induced chain.check_state_actionreturns a state-action value-like array derived from the formula probability sequence (using the vector at horizonB-1).
See Also
StatisticalModelChecker: Sampling-based estimation of satisfaction.
Source code in masa/common/pctl.py
check_state ¶
Evaluate the stored formula on the policy-induced Markov chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
Unused PRNG key (kept for API symmetry with
|
required |
policy
|
array
|
Stochastic policy probabilities, either:
- shape |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Float64 array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in masa/common/pctl.py
check_state_action ¶
Compute a state-action satisfaction value for the stored formula.
This method:
1) Builds the policy-induced Markov chain kernel,
2) Computes the formula sequence up to bound B = formula.bound,
3) Uses the vector at time max(B-1, 0) as a value function,
4) Computes one-step expectations under each action to produce Q.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
Unused PRNG key (kept for API symmetry with
|
required |
policy
|
ndarray
|
Stochastic policy probabilities, either |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Float64 array |
ndarray
|
|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in masa/common/pctl.py
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 | |
Statistical Model Checking¶
masa.common.pctl.StatisticalModelChecker ¶
StatisticalModelChecker(formula: BoundedPCTLFormula, label_fn: LabelFn, atomic_predicates: List[str], transition_matrix: Optional[ndarray] = None, successor_states: Optional[ndarray] = None, probabilities: Optional[ndarray] = None)
Bases: BoundedPCTLModelChecker
Statistical model checker (SMC) for bounded PCTL formulas.
The SMC estimates satisfaction probabilities by Monte Carlo sampling of trajectories under a policy and comparing the estimated probability \(\hat{p}\) to the formula’s threshold.
Notes
- Pure state formulas (
Truth,Atom,Neg,And,Or, andImpliesif present) are evaluated exactly without sampling. - Nested probabilistic operators inside state formulas are not supported.
Attributes:
| Name | Type | Description |
|---|---|---|
vec_label_fn_jax |
JAX copy of |
Source code in masa/common/pctl.py
vec_label_fn_jax
instance-attribute
¶
check_state ¶
Estimate whether state satisfies the formula under policy.
For probabilistic temporal formulas, this samples num_samples paths of
length max_steps = max(1, formula.bound) and estimates
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
PRNG key used for sampling. |
required |
policy
|
array
|
Stochastic policy probabilities, either |
required |
state
|
int
|
Start state index. |
required |
num_samples
|
int
|
Number of trajectories to sample. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Scalar float64 JAX array equal to |
ndarray
|
|
Source code in masa/common/pctl.py
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 | |
check_state_action ¶
check_state_action(key: Array, policy: ndarray, state: int, action: int, num_samples: int) -> np.ndarray
Estimate satisfaction when forcing the first action, then following policy.
The first transition uses the forced action action and subsequent
steps follow the policy-induced kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
PRNG key used for sampling. |
required |
policy
|
ndarray
|
Stochastic policy probabilities, either |
required |
state
|
int
|
Start state index. |
required |
action
|
int
|
Forced first action index. |
required |
num_samples
|
int
|
Number of trajectories to sample. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Scalar float64 JAX array equal to |
ndarray
|
meets the formula's threshold, else |
Source code in masa/common/pctl.py
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 | |
_is_probabilistic_formula ¶
Return True if formula is a probabilistic path operator.
_get_formula_prob ¶
Extract probability threshold from a probabilistic formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
BoundedPCTLFormula
|
A probabilistic formula ( |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability threshold. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in masa/common/pctl.py
_eval_state_formula_python ¶
Evaluate a pure state formula in Python.
This supports only boolean (non-probabilistic) formulas. Nested probabilistic operators are explicitly rejected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
BoundedPCTLFormula
|
Formula to evaluate. |
required |
state
|
int
|
State index. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Boolean satisfaction. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If nested probabilistic operators are encountered. |
TypeError
|
If an unsupported formula type is encountered. |
Source code in masa/common/pctl.py
_prepare_policy_probs_jax
staticmethod
¶
Normalize policy shape for JAX computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_states
|
int
|
Number of states. |
required |
n_actions
|
int
|
Number of actions. |
required |
policy
|
ndarray
|
Policy probabilities as either |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Policy as a |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in masa/common/pctl.py
_eval_state_formula_jax
staticmethod
¶
_eval_state_formula_jax(state_idx: ndarray, formula: BoundedPCTLFormula, vec_labels: ndarray, atom_dict: Dict[str, int]) -> jnp.ndarray
Evaluate a pure state formula in JAX.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_idx
|
ndarray
|
Scalar state index. |
required |
formula
|
BoundedPCTLFormula
|
State formula (must not contain probabilistic operators). |
required |
vec_labels
|
ndarray
|
Vectorized labels |
required |
atom_dict
|
Dict[str, int]
|
Mapping from atom name to row index in |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
JAX boolean scalar indicating satisfaction. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If nested probabilistic operators are encountered. |
TypeError
|
If an unsupported formula type is encountered. |
Source code in masa/common/pctl.py
_path_satisfies_single
staticmethod
¶
_path_satisfies_single(states_1d: ndarray, formula: BoundedPCTLFormula, vec_labels: ndarray, atom_dict: Dict[str, int]) -> jnp.ndarray
Evaluate whether a single sampled path satisfies the formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states_1d
|
ndarray
|
A single trajectory of state indices with shape |
required |
formula
|
BoundedPCTLFormula
|
The (bounded) formula to check. |
required |
vec_labels
|
ndarray
|
Vectorized labels |
required |
atom_dict
|
Dict[str, int]
|
Mapping from atom name to row index. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
JAX boolean scalar: True iff the trajectory satisfies |
Source code in masa/common/pctl.py
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 | |
_estimate_prob_dense
staticmethod
¶
_estimate_prob_dense(key: Array, start_state: int, num_samples: int, max_steps: int, m_first: ndarray, m_rest: ndarray, formula: BoundedPCTLFormula, vec_labels: ndarray, atom_dict: Dict[str, int]) -> jnp.ndarray
Estimate satisfaction probability by sampling from a dense kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
PRNG key. |
required |
start_state
|
int
|
Initial state index. |
required |
num_samples
|
int
|
Number of trajectories to sample. |
required |
max_steps
|
int
|
Trajectory horizon (number of transitions). |
required |
m_first
|
ndarray
|
Transition matrix for the first step |
required |
m_rest
|
ndarray
|
Transition matrix for subsequent steps |
required |
formula
|
BoundedPCTLFormula
|
Formula to check along sampled paths. |
required |
vec_labels
|
ndarray
|
Vectorized labels |
required |
atom_dict
|
Dict[str, int]
|
Atom-to-index mapping. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Scalar float64 JAX array: estimated probability |
Source code in masa/common/pctl.py
_estimate_prob_compact
staticmethod
¶
_estimate_prob_compact(key: Array, start_state: int, num_samples: int, max_steps: int, succ: ndarray, p_first: ndarray, p_rest: ndarray, formula: BoundedPCTLFormula, vec_labels: ndarray, atom_dict: Dict[str, int]) -> jnp.ndarray
Estimate satisfaction probability by sampling from a compact kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
PRNG key. |
required |
start_state
|
int
|
Initial state index. |
required |
num_samples
|
int
|
Number of trajectories to sample. |
required |
max_steps
|
int
|
Trajectory horizon (number of transitions). |
required |
succ
|
ndarray
|
Successor matrix |
required |
p_first
|
ndarray
|
Probabilities for the first step |
required |
p_rest
|
ndarray
|
Probabilities for subsequent steps |
required |
formula
|
BoundedPCTLFormula
|
Formula to check along sampled paths. |
required |
vec_labels
|
ndarray
|
Vectorized labels |
required |
atom_dict
|
Dict[str, int]
|
Atom-to-index mapping. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Scalar float64 JAX array: estimated probability |
Source code in masa/common/pctl.py
_sample_trajectories_dense_jax
staticmethod
¶
_sample_trajectories_dense_jax(key: Array, m_first: ndarray, m_rest: ndarray, start_state: ndarray, max_steps: int, num_samples: int) -> jnp.ndarray
Sample a batch of trajectories from a dense transition kernel.
The returned tensor is shaped (num_samples, max_steps + 1).
Special handling
- Rows with zero outgoing probability mass fall back to a self-loop (the agent stays in the same state).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
PRNG key. |
required |
m_first
|
ndarray
|
Transition matrix for the first step |
required |
m_rest
|
ndarray
|
Transition matrix for subsequent steps |
required |
start_state
|
ndarray
|
Scalar start state (integer-valued, stored as array). |
required |
max_steps
|
int
|
Number of transitions to sample. |
required |
num_samples
|
int
|
Number of independent trajectories. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Integer JAX array of shape |
Source code in masa/common/pctl.py
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 | |
_sample_trajectories_compact_jax
staticmethod
¶
_sample_trajectories_compact_jax(key: Array, succ: ndarray, p_first: ndarray, p_rest: ndarray, start_state: ndarray, max_steps: int, num_samples: int) -> jnp.ndarray
Sample a batch of trajectories from a compact successor kernel.
The returned tensor is shaped (num_samples, max_steps + 1).
Special handling
- Rows with zero outgoing probability mass fall back to a self-loop (the agent stays in the same state).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
PRNG key. |
required |
succ
|
ndarray
|
Successor matrix |
required |
p_first
|
ndarray
|
Probabilities for first step |
required |
p_rest
|
ndarray
|
Probabilities for subsequent steps |
required |
start_state
|
ndarray
|
Scalar start state (integer-valued, stored as array). |
required |
max_steps
|
int
|
Number of transitions to sample. |
required |
num_samples
|
int
|
Number of independent trajectories. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Integer JAX array of shape |
Source code in masa/common/pctl.py
Helpers¶
masa.common.pctl.kernel_n_states ¶
Return the number of states induced by a transition kernel.
MASA evaluates bounded PCTL formulas on a Markov-chain transition kernel that can be represented in either a dense or compact form.
Dense kernel
A 2D transition matrix m with shape (n_states, n_states) where
column s encodes the distribution over next states from state s.
(So m[:, s] is a categorical distribution when state s has outgoing
probability mass.)
Compact kernel
A tuple (succ, p) where:
succhas shape(K, n_states)and stores up toKsuccessor state ids per state.phas shape(K, n_states)and stores aligned successor probabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kernel
|
Kernel
|
The transition kernel, either a dense matrix or a |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of states |