labqc.uncertainty¶
Measurement uncertainty budgets following the GUM (JCGM 100:2008) and its Supplement 1.
A budget is a list of input components. Each has a standard uncertainty u_i, a sensitivity coefficient c_i and degrees of freedom v_i (infinite for most Type B estimates). The combined standard uncertainty is
u_c^2 = sum((c_i * u_i)^2) + sum_{i != j} c_i * c_j * u_i * u_j * r_ij
where r_ij is the correlation coefficient between inputs i and j (GUM 5.2.2); the second sum is zero for uncorrelated inputs. The effective degrees of freedom come from the Welch-Satterthwaite formula, and the coverage factor k from Student's t.
For non-linear models, monte_carlo propagates the input distributions
themselves (JCGM 101:2008, GUM Supplement 1) instead of their first-order
sensitivities.
Correlation
module-attribute
¶
Correlation = Mapping[tuple[str, str], float]
Correlation coefficients r_ij keyed by a pair of component names, e.g. {("m1", "m2"): 0.9}. One order of each pair is enough; pairs not listed are uncorrelated.
Component
dataclass
¶
One input to the budget.
the quoted uncertainty. For "normal" this is a standard
uncertainty, or an expanded one if k is given. For the other
distributions it is the half-width of the interval.
Source code in src/labqc/uncertainty.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
Result
dataclass
¶
Outcome of combine or propagate.
combined: u_c. expanded: U = k * u_c. covariance: the part of u_c^2 that comes from correlations (zero for uncorrelated inputs). value: the measurement result y, when known; it enables the relative uncertainties.
Source code in src/labqc/uncertainty.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
summary ¶
summary(relative: bool = False) -> str
One-line summary; with relative=True also u_c/|y| and U/|y| in percent.
Source code in src/labqc/uncertainty.py
185 186 187 188 189 190 191 192 193 194 195 196 | |
MonteCarloResult
dataclass
¶
Outcome of monte_carlo (JCGM 101:2008).
mean and standard_uncertainty are the estimate of y and u(y); [low, high]
is the shortest interval holding coverage of the samples (JCGM 101 7.7.2).
Source code in src/labqc/uncertainty.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
Agreement
dataclass
¶
Outcome of compare_with_monte_carlo (JCGM 101:2008 clause 8).
gum_low, gum_high: y -/+ U. mc_low, mc_high: the Monte Carlo interval. d_low, d_high: the absolute differences of the endpoints. delta: the numerical tolerance. agrees: both differences are at most delta.
Source code in src/labqc/uncertainty.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
components_from_table ¶
components_from_table(table: DataFrame) -> list[Component]
Build components from a table, e.g. an uploaded CSV or an edited grid.
Columns: name and value are required; distribution (default "normal"), k (1), sensitivity (1), dof (blank or "inf" = infinite) and unit ("") are optional. Fully empty rows are skipped. Raises ValueError with the row number (counting from 1) and a plain explanation for bad input.
Source code in src/labqc/uncertainty.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
type_a ¶
type_a(readings: Sequence[float], name: str = 'Repeatability') -> Component
Type A component from repeated readings: standard deviation of the mean.
Source code in src/labqc/uncertainty.py
141 142 143 144 145 146 147 | |
welch_satterthwaite ¶
welch_satterthwaite(components: Sequence[Component]) -> float
Effective degrees of freedom (GUM G.4.1), for independent inputs.
Source code in src/labqc/uncertainty.py
202 203 204 205 206 | |
correlation_matrix ¶
correlation_matrix(names: Sequence[str], correlation: Correlation | None) -> np.ndarray
Build and check the correlation matrix R for components in names order.
Raises ValueError if a coefficient is outside [-1, 1], a name is unknown, a pair is given twice with different values, a component's correlation with itself is not 1, or R is not positive semi-definite.
Source code in src/labqc/uncertainty.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
combine ¶
combine(components: Sequence[Component], coverage: float = 0.95, k: float | None = None, correlation: Correlation | None = None, value: float | None = None) -> Result
Combine components into u_c, k and U.
Pass k to fix the coverage factor (k = 2 is common in calibration
certificates). Otherwise k comes from the effective degrees of freedom.
correlation maps pairs of component names to r_ij and adds the
covariance terms 2 * c_i * c_j * u_i * u_j * r_ij to u_c^2; the sign of
each sensitivity coefficient matters here. With correlations, the
share_of_variance column no longer sums to 1: the rest is in
Result.covariance. Welch-Satterthwaite is used as for independent inputs.
value is the measurement result y, needed only for relative uncertainties.
Source code in src/labqc/uncertainty.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
propagate ¶
propagate(func: Callable[..., float], values: Mapping[str, float], inputs: Mapping[str, Component], extra: Sequence[Component] = (), coverage: float = 0.95, k: float | None = None, correlation: Correlation | None = None) -> tuple[float, Result]
Propagate input uncertainties through a measurement model y = func(**values).
Sensitivity coefficients are partial derivatives found by central
differences, so the model can be any Python function. inputs maps an
argument name to its uncertainty component (the component's own
sensitivity is replaced). extra adds components already expressed in
units of the result, such as the repeatability of y. correlation keys
may use argument names or component names.
Source code in src/labqc/uncertainty.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
monte_carlo ¶
monte_carlo(func: Callable[..., float], values: Mapping[str, float], inputs: Mapping[str, Component], extra: Sequence[Component] = (), n: int = 200000, seed: int | None = None, coverage: float = 0.95, correlation: Correlation | None = None) -> MonteCarloResult
Propagate distributions through y = func(**values) by Monte Carlo (GUM Supplement 1).
Arguments mirror propagate. Each input in inputs is sampled around
its value in values from its own distribution: normal (Student t when
dof is finite), rectangular, triangular or u-shaped. Components in
extra are sampled around zero, multiplied by their sensitivity and added
to y. Arguments not in inputs are held fixed.
func is called once with numpy arrays when it supports them (plain
arithmetic does); otherwise it is called once per sample, which is slower.
Correlated inputs are sampled from a joint normal distribution, so every component named in a non-zero correlation must be normal with infinite dof.
seed makes the result repeatable. JCGM 101 7.2.2 suggests n of at least
10^4 / (1 - coverage), i.e. 200 000 for 95 %.
Source code in src/labqc/uncertainty.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
monte_carlo_budget ¶
monte_carlo_budget(components: Sequence[Component], value: float = 0.0, correlation: Correlation | None = None, n: int = 200000, seed: int | None = None, coverage: float = 0.95) -> MonteCarloResult
Monte Carlo (JCGM 101) for a budget given as a list of components.
The model is the linear one a budget table stands for:
y = value + sum(c_i * delta_i), where each delta_i is drawn around zero
from component i's distribution (Student t for a normal component with
finite dof). Use it to check a combine result: the GUM method replaces
each t-distributed input by a normal one and puts the extra width into k,
so with few degrees of freedom the two can differ.
Source code in src/labqc/uncertainty.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
numerical_tolerance ¶
numerical_tolerance(u: float, n_dig: int = 2) -> float
Numerical tolerance delta of a standard uncertainty (JCGM 101:2008 7.9.2).
Write u with n_dig significant digits as c x 10^l; then delta = 10^l / 2. Example: u = 0.0191 with n_dig = 2 is 19 x 10^-3, so delta = 0.0005.
Source code in src/labqc/uncertainty.py
554 555 556 557 558 559 560 561 562 563 564 565 566 | |
compare_with_monte_carlo ¶
compare_with_monte_carlo(y: float, result: Result, mc: MonteCarloResult, n_dig: int = 2) -> Agreement
Check a GUM interval y +/- U against a Monte Carlo interval (JCGM 101:2008 8.2).
d_low = |y - U - y_low| and d_high = |y + U - y_high|. The GUM result is
confirmed when both are at most delta, the numerical tolerance of the
Monte Carlo u(y) to n_dig significant digits (numerical_tolerance).
Both intervals must have the same coverage probability.
Source code in src/labqc/uncertainty.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |