demo/NIST1900.R

## propagate demo: examples of NIST Technical Note 1900 (Possolo 2015), "Simple Guide for Evaluating and
## Expressing the Uncertainty of NIST Measurement Results", with measurement equations
## demo("NIST1900", package = "propagate")
## Not included: examples based on statistical (observation-equation) models, e.g. random-effects,
## errors-in-variables, Bayesian or time-series models (E6, E7, E10, E12, E14, E16 - E35 except E23 and E26,
## which need such models as well), and E5 / E15, whose input budgets are not fully specified in TN 1900.
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)
}

## Monte Carlo standard errors (both calculations with M trials) of the mean and the standard deviation of y
seMean <- function(y) sqrt(2) * sd(y)/sqrt(length(y))
seSD   <- function(y) { k <- mean((y - mean(y))^4)/var(y)^2; sqrt(2) * sd(y) * sqrt((k - 1)/(4 * length(y))) }
seQ    <- function(y, p) { f <- density(y, from = quantile(y, p), to = quantile(y, p), n = 1)$y; sqrt(2) * sqrt(p * (1 - p)/length(y))/f }
set.seed(1900)

######## E1 Weighing (TN 1900, p. 27): m_P = c_P - c_E - (c_R2 - c_R1) ########
uB <- 0.1E-3; uR <- 0.1E-3; uL <- 0.2E-3; uT <- 0.3E-3              ## balance: readability, repeatability, linearity, eccentricity (g)
uC <- sqrt(0.005^2 + uB^2 + uR^2 + uL^2 + uT^2)                       ## container + balance
uD <- sqrt(2 * (uB^2 + uR^2 + uL^2 + uT^2))                           ## difference of two weighings of the reference
E1 <- propagate(expression(cP - cE - dR), cbind(cP = c(53.768, uC, Inf), cE = c(3.436, uC, Inf), dR = c(3.476 - 3.428, uD, Inf)),
                type = "stat", nsim = M, check = FALSE)
E1
cat("Comparison with TN 1900, E1:\n")
gauge("u(c_P) = u(c_E) [g]", uC, "0.005015"); gauge("u(c_R2 - c_R1) [g]", uD, "0.0005477")
gauge("m_P [g]", E1$prop["mean.1"], "50.284"); gauge("u(m_P) [g]", E1$prop["u.1"], "0.0071")
gauge("95 % interval, low [g]", E1$sim[5], "50.270", seQ(E1$resSIM, 0.025)); gauge("95 % interval, high [g]", E1$sim[6], "50.298", seQ(E1$resSIM, 0.975))

######## E2 Surface temperature (TN 1900, p. 29): mean of 22 daily maxima, Student t with 21 DOF ########
data(TN.E2); TMAX <- TN.E2$t                                          ## 22 daily maxima (Exhibit 2)
E2 <- propagate(expression(t), cbind(t = TMAX), type = "raw", nsim = M, check = FALSE)
E2
cat("Comparison with TN 1900, E2:\n")
gauge("mean [degC]", E2$prop["mean.1"], "25.59"); gauge("s [degC]", sd(TMAX), "4.1"); gauge("u = s/sqrt(m) [degC]", E2$prop["u.1"], "0.872")
gauge("k = t_95(21)", E2$k, "2.08"); gauge("95 % interval, low [degC]", tail(E2$prop, 2)[1], "23.8"); gauge("95 % interval, high [degC]", tail(E2$prop, 2)[2], "27.4")
gauge("MC (S1) 95 % interval, low [degC]", E2$sim[5], "23.8", seQ(E2$resSIM, 0.025))

######## E3 Falling ball viscometer (TN 1900, p. 31) ########
EX3 <- expression(muC * ((rB - rM)/(rB - rC)) * (tM/tC))
E3 <- propagate(EX3, cbind(muC = c(4.63, 0.0463, Inf), rB = c(2217, 0.5, Inf), rC = c(810, 0.5, Inf), rM = c(1180, 0.5, Inf),
                           tC = c(36.6, 0.15 * 36.6, Inf), tM = c(61, 0.10 * 61, Inf)), type = "stat", nsim = M, check = FALSE)
E3
y <- E3$resSIM
cat("Comparison with TN 1900, E3:\n")
gauge("GUF estimate [mPa s]", E3$prop["mean.1"], "5.69"); gauge("GUF u [mPa s] (TN: same as MC)", E3$prop["u.1"], "1.11", note = TRUE)
## NOTE: TN 1900 states that Gauss's formula gives u = 1.11 mPa s, the same as the Monte Carlo method. The first-order
## formula gives 5.69 x sqrt(0.15^2 + 0.10^2 + 0.01^2 + ...) = 1.03 mPa s (relative uncertainties of tC, tM, muC);
## 1.11 mPa s is the standard deviation of the Monte Carlo output only.
gauge("MC mean [mPa s]", E3$sim["mean.MC"], "5.82", seMean(y)); gauge("MC u [mPa s]", E3$sim["u.MC"], "1.11", seSD(y))
gauge("MC 95 % interval, low [mPa s]", E3$sim[5], "4.05", seQ(y, 0.025)); gauge("MC 95 % interval, high [mPa s]", E3$sim[6], "8.39", seQ(y, 0.975))

######## E4 Pitot tube (TN 1900, p. 32): v = sqrt(2 Delta Rs T / p), u = U95/2 ########
DF4 <- cbind(D = c(1993, 25/2, Inf), p = c(101400, 2100/2, Inf), T = c(292.8, 0.11/2, Inf))
E4a <- propagate(expression(sqrt(2 * D * 287.058 * T/p)), DF4, type = "stat", nsim = M, check = FALSE)
cat("Measurement equation as printed in TN 1900, v = sqrt(2 Delta Rs T/p):\n")
gauge("v [m/s] (formula as printed)", E4a$prop["mean.1"], "40.64", note = TRUE)
## NOTE: with the factor 2 of the printed measurement equation, v = 57.48 m/s; the published results are
## reproduced exactly without it, i.e. v = sqrt(Delta Rs T/p) (u then is smaller by sqrt(2)):
E4 <- propagate(expression(sqrt(D * 287.058 * T/p)), DF4, type = "stat", nsim = M, check = FALSE)
E4
y <- E4$resSIM
cat("Comparison with TN 1900, E4:\n")
gauge("v [m/s]", E4$prop["mean.1"], "40.64"); gauge("u(v) [m/s]", E4$prop["u.1"], "0.25")
gauge("v - 2u [m/s]", E4$prop["mean.1"] - 2 * E4$prop["u.1"], "40.15"); gauge("v + 2u [m/s]", E4$prop["mean.1"] + 2 * E4$prop["u.1"], "41.14")
gauge("MC 95 % interval, low [m/s]", E4$sim[5], "40.17", seQ(y, 0.025)); gauge("MC 95 % interval, high [m/s]", E4$sim[6], "41.13", seQ(y, 0.975))

######## E8 Molecular weight of carbon dioxide (TN 1900, p. 41): rectangular atomic weights ########
EX8 <- expression(C + 2 * O)
B8 <- data.frame(name = c("C", "O"), value = c(12.0106, 15.9994), a = c(0.0010, 0.00037), dist = "rect")
E8 <- propagate(EX8, makeDat(EX8, data = B8, output = "sim", nsim = M), type = "sim", check = FALSE)
E8
y <- E8$resSIM
cat("Comparison with TN 1900, E8:\n")
gauge("M_r(CO2)", E8$prop["mean.1"], "44.0094")
## TN 1900 computes u from the rounded u(C) = 0.0006 and u(O) = 0.0002 (exact: 0.000577, 0.000214)
gauge("u(M_r(CO2))", E8$prop["u.1"], "0.000721", extra = (0.0006 + 4 * 0.0002) * 0.00005/0.000721)
gauge("shortest 95 % interval, low (trapezoid)", E8$ci.short[1], "44.0080", 2 * seQ(y, 0.025))
gauge("shortest 95 % interval, high (trapezoid)", E8$ci.short[2], "44.0108", 2 * seQ(y, 0.975))

######## E9 Cadmium calibration standard (TN 1900, p. 42; EURACHEM QUAM A1) ########
E9 <- propagate(expression(1000 * m * P/V), cbind(m = c(100.28, 0.05, Inf), P = c(0.9999, 0.000058, Inf), V = c(100.0, 0.07, Inf)),
                type = "stat", nsim = M, check = FALSE)
E9
cat("Comparison with TN 1900, E9:\n")
gauge("c_Cd [mg/L]", E9$prop["mean.1"], "1002.7"); gauge("u(c_Cd) [mg/L]", E9$prop["u.1"], "0.9")
gauge("MC u(c_Cd) [mg/L]", E9$sim["u.MC"], "0.9", seSD(E9$resSIM))

######## E11 Microwave step attenuator (TN 1900, p. 44; EA-4/02 S7) ########
## TN 1900 rescales the t_3 distribution of L_S to the standard deviation 0.0091 dB (scale 0.0091/sqrt(3));
## arcsine inputs with standard deviation u have half-width a = sqrt(2) u
data(TN.E11)                                                          ## observations of L_S and budget (Exhibit 16)
cat("Comparison with TN 1900, E11, Type A evaluation of L_S:\n")
gauge("mean L_S [dB]", mean(TN.E11$LS), "30.0402"); gauge("u(L_S) = s/sqrt(4) [dB]", sd(TN.E11$LS)/2, "0.0091")
## budget for makeDat(): t3 with scale u/sqrt(3) (standard deviation u), half-widths a = sqrt(3) u (rectangular),
## a = sqrt(2) u (arcsine)
EX11 <- expression(LS + dLS + dLD + dLM + dLK + dLib - dLia + dL0b - dL0a)
B <- TN.E11$budget
B11 <- data.frame(name = B$name, value = B$value,
                  u  = ifelse(B$dist == "t3", B$u/sqrt(3), ifelse(B$dist == "normal", B$u, NA)),
                  df = ifelse(B$dist == "t3", 3, NA),
                  a  = ifelse(B$dist == "rect", sqrt(3) * B$u, ifelse(B$dist == "arcsine", sqrt(2) * B$u, NA)),
                  dist = ifelse(B$dist == "t3", "t", B$dist))
E11 <- propagate(EX11, makeDat(EX11, data = B11, output = "sim", nsim = M), type = "sim", check = FALSE)
E11
y <- E11$resSIM
cat("Comparison with TN 1900, E11 (M = 1e7):\n")
gauge("L_X [dB]", E11$sim["mean.MC"], "30.043", seMean(y)); gauge("u(L_X) [dB]", E11$sim["u.MC"], "0.0224", seSD(y))
gauge("95 % interval, low [dB]", E11$sim[5], "30.006", seQ(y, 0.025)); gauge("95 % interval, high [dB]", E11$sim[6], "30.081", seQ(y, 0.975))
gauge("coverage of L_X +/- 2u [%]", 100 * mean(abs(y - mean(y)) <= 2 * sd(y)), "99", note = TRUE)
## NOTE: the effective coverage of L_X +/- 2u is 98.5 %, which TN 1900 states as 99 %
gauge("coverage of L_X +/- u [%]", 100 * mean(abs(y - mean(y)) <= sd(y)), "61")

######## E13 Thermal expansion coefficient (TN 1900, p. 50), alpha in 1e-5/K ########
EX13 <- expression(1E5 * (L1 - L0)/(L0 * (T1 - T0)))
DF13 <- cbind(L0 = c(1.4999, 0.0001, Inf), L1 = c(1.5021, 0.0002, Inf), T0 = c(288.15, 0.02, Inf), T1 = c(373.10, 0.05, Inf))
E13 <- propagate(EX13, DF13, type = "stat", alpha = 0.05, nsim = M, check = FALSE)
E13
y <- E13$resSIM; q99 <- quantile(y, c(0.005, 0.995))
cat("Comparison with TN 1900, E13, Gaussian inputs (M = 1e7):\n")
gauge("alpha [1e-5/K]", E13$prop["mean.1"], "1.73"); gauge("u(alpha) [1e-5/K]", E13$prop["u.1"], "0.18")
gauge("95 % interval, low [1e-5/K]", E13$sim[5], "1.38", seQ(y, 0.025)); gauge("95 % interval, high [1e-5/K]", E13$sim[6], "2.07", seQ(y, 0.975))
gauge("99 % interval, low [1e-5/K]", q99[1], "1.27", seQ(y, 0.005)); gauge("99 % interval, high [1e-5/K]", q99[2], "2.18", seQ(y, 0.995))
## Student inputs: TN 1900 assigns t_3 distributions rescaled to standard deviation u (scale u/sqrt(3)), not the
## scale u of JCGM 101:2008, 6.4.9 -> budget with u/sqrt(3) as scale
B13 <- data.frame(name = colnames(DF13), value = DF13[1, ], u = DF13[2, ]/sqrt(3), df = 3, dist = "t")
E13t <- propagate(EX13, makeDat(EX13, data = B13, output = "sim", nsim = M), type = "sim", check = FALSE)
E13t
y <- E13t$resSIM; q99 <- quantile(y, c(0.005, 0.995))
cat("Comparison with TN 1900, E13, Student t_3 inputs (variance-matched):\n")
gauge("95 % interval, low [1e-5/K]", E13t$sim[5], "1.40", seQ(y, 0.025)); gauge("95 % interval, high [1e-5/K]", E13t$sim[6], "2.05", seQ(y, 0.975))
gauge("99 % interval, low [1e-5/K]", q99[1], "1.15", seQ(y, 0.005)); gauge("99 % interval, high [1e-5/K]", q99[2], "2.30", seQ(y, 0.995))

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.