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
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 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 | |
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
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 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 | |
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
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 1272 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 | |
_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
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 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 | |
_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
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 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 | |
_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 |