demo/JCGM106.R

## propagate demo: examples of JCGM 106:2012 "Evaluation of measurement data - The role of measurement
## uncertainty in conformity assessment", with conformity()
## demo("JCGM106", package = "propagate")
library(propagate)
library(crayon)                 ## green(), red(), blue() in gauge()
M     <- 1E6                    ## Monte Carlo trials

## comparison of a computed value with a published (gauge) value; the published value is given as a
## character string, so that its decimals define the rounding tolerance; 'se' adds 4 Monte Carlo
## standard errors (of both calculations) for Monte Carlo results; 'extra' adds the propagated rounding
## of published intermediate values; note = TRUE marks a documented discrepancy (NOTE instead of DIFF)
gauge <- function(item, value, published, se = 0, note = FALSE, extra = 0) {
  d <- if (grepl(".", published, fixed = TRUE)) nchar(sub("^[^.]*\\.", "", published)) else 0
  pub <- as.numeric(published); value <- unname(value); tol <- 0.5 * 10^-d + 4 * se + extra
  ok <- abs(value - pub) <= tol * (1 + 1E-9)                         ## exact half-units of rounding pass
  cat(sprintf("  %-46s published %12s  propagate %14.*f  |diff| %8.2g  tol %8.2g  %s\n",
              item, published, d + 2, value, abs(value - pub), tol, if (ok) green("PASS") else if (note) red("NOTE") else blue("DIFF")))
  invisible(ok)
}
set.seed(106)

######## 7.3 Table 1: z for one-sided tolerance intervals and normal PDFs ########
## z = (T_U - y)/u for which a measured value has conformance probability pc; equivalently the guard band
## (in units of u) of an acceptance limit A_U = T_U - z u with pc = p at A_U
cat("Comparison with JCGM 106, Table 1:\n")
for (p in c("0.80", "0.90", "0.95", "0.99", "0.999")) {
  C <- conformity(c(0, 1), upper = 0, prob = as.numeric(p))
  gauge(paste0("z for pc = ", p), -C$accept["AU"], c("0.80" = "0.84", "0.90" = "1.28", "0.95" = "1.64", "0.99" = "2.33", "0.999" = "3.09")[p])
}

######## 7.3 EXAMPLE 1: breakdown voltage of a Zener diode, V_b <= -5.40 V ########
C1 <- conformity(c(-5.47, 0.05), upper = -5.40)
C1
cat("Comparison with JCGM 106, 7.3, Example 1:\n")
gauge("z", C1$z["upper"], "1.40"); gauge("pc", C1$pc, "0.92")

######## 7.3 EXAMPLE 2: bursting strength of a metal container, B >= 490 kPa ########
C2 <- conformity(c(509.7, 8.6), lower = 490)
C2
cat("Comparison with JCGM 106, 7.3, Example 2:\n")
## JCGM 106 uses the rounded z = 2.3: extra tolerance = dPhi(z) * 0.05
gauge("z", C2$z["lower"], "2.3"); gauge("pc", C2$pc, "0.99", extra = dnorm(2.3) * 0.05)

######## 7.4 EXAMPLE: kinematic viscosity of SAE Grade 40 motor oil, 12.5 <= Y <= 16.3 mm^2/s ########
C3 <- conformity(c(13.6, 1.8), lower = 12.5, upper = 16.3)
C3
cat("Comparison with JCGM 106, 7.4, Example:\n")
gauge("(T_U - y)/u", C3$z["upper"], "1.5"); gauge("(T_L - y)/u", -C3$z["lower"], "-0.6")
gauge("pc (published from rounded z: 0.93 - 0.27)", C3$pc, "0.66")

######## 8.3.3 EXAMPLE 1: speed limit enforcement, v0 = 100 km/h, u(v) = 0.02 v ########
## acceptance limit v_max with P(V >= v0) = 99.9 % for a measured v >= v_max; u depends on v
vmax <- uniroot(function(v) conformity(c(v, 0.02 * v), lower = 100)$pc - 0.999, c(100, 150), tol = 1E-10)$root
cat("Comparison with JCGM 106, 8.3.3, Example 1:\n")
gauge("v_max [km/h]", vmax, "107"); gauge("v_max = v0/(1 - 0.02 z) [km/h]", 100/(1 - 0.02 * 3.09), "107")

######## 8.3.3 EXAMPLE 2: nandrolone, threshold 2.00 ug/L, t-distribution with nu = 9 ########
C5 <- conformity(c(2.00, 0.20, 9), lower = 2.00, prob = 0.95)
C5
cat("Comparison with JCGM 106, 8.3.3, Example 2:\n")
gauge("t(0.95; 9)", qt(0.95, 9), "1.83"); gauge("guard band g [ug/L]", C5$accept["AL"] - 2, "0.37")
gauge("decision limit A [ug/L]", C5$accept["AL"], "2.37")

######## 9.5.3 EXAMPLE: manufacture of precision resistors (global risks) ########
## process N(1500, 0.12^2) Ohm, tolerance [1499.8, 1500.2] Ohm, ohmmeter u_m = 0.04 Ohm, acceptance [1499.82, 1500.18] Ohm
R1 <- conformity(function(x) dnorm(x, 1500, 0.12), lower = 1499.8, upper = 1500.2, um = 0.04, accept = c(1499.82, 1500.18))
R1
cat("Comparison with JCGM 106, 9.5.3, Example:\n")
gauge("conforming fraction of the process", R1$pc, "0.90"); gauge("consumer's risk R_C", R1$RC, "0.01")
gauge("producer's risk R_P", R1$RP, "0.07"); ## JCGM 106 adds rounded counts per 100 (83 + 1): extra tolerance +/- 0.5 for each of the two counts
gauge("accepted resistors per 100 (83 + 1)", 100 * R1$accepted, "84", extra = 2 * 0.5)
gauge("conforming among accepted", 1 - R1$RC.accepted, "0.99")
## JCGM 106 computes 7/16 from rounded counts per 100: extra tolerance from +/- 0.5 in both counts
gauge("conforming among rejected (7/16)", R1$RP/(1 - R1$accepted), "0.44", extra = 0.5/16 + 7 * 0.5/16^2)
## the same with a Monte Carlo sample of the process instead of the density
R1s <- conformity(rnorm(M, 1500, 0.12), lower = 1499.8, upper = 1500.2, um = 0.04, accept = c(1499.82, 1500.18))
gauge("R_C, Monte Carlo process sample", R1s$RC, "0.01", se = sqrt(R1s$RC/M))

######## 9.5.4 EXAMPLE: ball bearing production (acceptance limit for a target consumer's risk) ########
## process gamma(alpha = 4, lambda = 4), mean 1 um, sd 0.5 um; tolerance Y <= 2 um; u_m = 0.25 um; R_C = 0.1 %
R2 <- conformity(function(x) dgamma(x, shape = 4, rate = 4), upper = 2, um = 0.25, risk = 0.001, support = c(0, Inf))
R2
cat("Comparison with JCGM 106, 9.5.4, Example:\n")
gauge("non-conforming fraction of the process", R2$pnc, "0.042")
gauge("acceptance limit A [um]", R2$accept["AU"], "1.7")
## r is read from the graph of R_C(r) (Figure 15): reading tolerance 0.01
gauge("guard band multiplier r (Figure 15)", R2$r, "0.65", extra = 0.01)
gauge("producer's risk R_P [%]", 100 * R2$RP, "7.5")
R2b <- conformity(function(x) dgamma(x, 4, 4), upper = 2, um = 0.25, accept = c(-Inf, 2 - 2 * 0.65 * 0.25), support = c(0, Inf))
gauge("R_C [%] at r = 0.65 exactly", 100 * R2b$RC, "0.1")

######## Illustration (no published values): conformity of a propagate() result ########
## GUM H.1, end gauge: deviation l - 50 mm with a hypothetical tolerance of +/- 1000 nm.
## With a propagate object, conformity() uses the Monte Carlo sample ("MC") or the GUF ("GUF").
data(H.1)
H1 <- propagate(expression(Ls + d - Ls * (da * theta + as * dt) - 50000000), H.1, type = "stat", nsim = M, check = FALSE)
conformity(H1, lower = -1000, upper = 1000)                       ## Monte Carlo
conformity(H1, lower = -1000, upper = 1000, method = "GUF")       ## GUM uncertainty framework
conformity(H1, lower = -1000, upper = 1000, prob = 0.95)          ## acceptance limits for pc >= 95 %

######## Decision rules with the expanded uncertainty (ISO 14253-1, ASME B89.7.3.1, VDA 5) ########
## Not part of JCGM 106 (no published values): the six combinations of stringent, simple and relaxed
## acceptance/rejection, for a single item (decision) and for a production process (accepted, rejected
## and undecided fractions, consumer's and producer's risks). The checks below are identities with the
## JCGM 106 risks: simple acceptance = acceptance limits at the tolerance limits, stringent acceptance =
## guarded acceptance with guard band w = U, relaxed acceptance = guarded rejection with w = -U.
same <- function(item, a, b) cat(sprintf("  %-62s exact %14.8g  propagate %14.8g  %s\n", item, b, a,
                                         if (isTRUE(all.equal(a, b, tolerance = 1E-8))) green("PASS") else blue("DIFF")))

## 7.3, Example 1: Zener diode (one-sided tolerance), U = 1.96 u = 0.098 V
D1 <- conformity(c(-5.47, 0.05), upper = -5.40, rule = "all")
D1
plot(D1)
cat("Decisions for y = -5.47 V, U = 0.098 V, T_U = -5.40 V:\n")
same("stringent/stringent: no decision (y + U > T_U > y - U)", D1$rules$decision[1] == "no decision", TRUE)
same("simple/simple: accept (y <= T_U)", D1$rules$decision[2] == "accept", TRUE)
same("stringent/relaxed: reject (y > T_U - U)", D1$rules$decision[3] == "reject", TRUE)

## 9.5.3: precision resistors, U = 1.96 um
D2 <- conformity(function(x) dnorm(x, 1500, 0.12), lower = 1499.8, upper = 1500.2, um = 0.04, rule = "all")
D2
plot(D2)
U2 <- D2$U
S2 <- conformity(function(x) dnorm(x, 1500, 0.12), lower = 1499.8, upper = 1500.2, um = 0.04)
G2 <- conformity(function(x) dnorm(x, 1500, 0.12), lower = 1499.8, upper = 1500.2, um = 0.04, accept = c(1499.8 + U2, 1500.2 - U2))
R2r <- conformity(function(x) dnorm(x, 1500, 0.12), lower = 1499.8, upper = 1500.2, um = 0.04, accept = c(1499.8 - U2, 1500.2 + U2))
cat("Consistency with the JCGM 106 risks (rule vs. conformity(..., accept = )):\n")
same("simple/simple R_C = simple acceptance R_C", D2$rules$RC[2], S2$RC)
same("simple/simple R_P = simple acceptance R_P", D2$rules$RP[2], S2$RP)
same("stringent acceptance R_C = guarded acceptance (w = U) R_C", D2$rules$RC[1], G2$RC)
same("stringent/relaxed R_P = guarded acceptance (w = U) R_P", D2$rules$RP[3], G2$RP)
same("relaxed/stringent R_C = guarded rejection (w = -U) R_C", D2$rules$RC[4], R2r$RC)
same("accepted + rejected + undecided = 1 (stringent/stringent)", sum(D2$rules[1, c("accepted", "rejected", "undecided")]), 1)
## the same with a Monte Carlo sample of the process
D2s <- conformity(rnorm(M, 1500, 0.12), lower = 1499.8, upper = 1500.2, um = 0.04, rule = "stringent")
gauge("R_C stringent/stringent, Monte Carlo process sample", D2s$rules$RC, sprintf("%.5f", D2$rules$RC[1]), se = sqrt(D2$rules$RC[1]/M))

## 9.5.4: ball bearings (one-sided, gamma process), U = 1.96 um
D3 <- conformity(function(x) dgamma(x, shape = 4, rate = 4), upper = 2, um = 0.25, rule = "all", support = c(0, Inf))
D3
plot(D3)

Try the propagate package in your browser

Any scripts or data that you put into this service are public.

propagate documentation built on Nov. 10, 2026, 9:07 a.m.