Nothing
## propagate demo: all examples of JCGM 101:2008 (GUM Supplement 1), clause 9
## demo("JCGM101", package = "propagate")
## For each example: propagate() output (GUM uncertainty framework and Monte Carlo), a histogram of
## the Monte Carlo output with the best fitting distribution, and a comparison with the published values
## (Monte Carlo values within rounding + 4 standard errors).
library(propagate)
library(crayon)
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; note = TRUE marks a documented
## discrepancy of the published value (printed as NOTE instead of DIFF)
gauge <- function(item, value, published, se = 0, note = FALSE) {
d <- if (grepl(".", published, fixed = TRUE)) nchar(sub("^[^.]*\\.", "", published)) else 0
pub <- as.numeric(published); tol <- 0.5 * 10^-d + 4 * se; ok <- abs(value - pub) <= tol
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(101)
EX <- expression(X1 + X2 + X3 + X4)
######## 9.2.2 Additive model, Gaussian inputs (Table 2) ########
DF <- cbind(X1 = c(0, 1, Inf), X2 = c(0, 1, Inf), X3 = c(0, 1, Inf), X4 = c(0, 1, Inf))
R1 <- propagate(EX, DF, type = "stat", nsim = M, check = FALSE)
R1
y <- R1$resSIM
cat("Comparison with JCGM 101, Table 2:\n")
gauge("GUF u", R1$prop["u.1"], "2.00"); gauge("GUF interval, high", tail(R1$prop, 2)[2], "3.92")
gauge("MCM u", R1$sim["u.MC"], "2.00", seSD(y)); gauge("MCM interval, low", R1$sim[5], "-3.92", seQ(y, 0.025))
gauge("MCM interval, high", R1$sim[6], "3.92", seQ(y, 0.975))
RA <- propagate(EX, DF, type = "stat", adaptive = "validation", check = FALSE)
cat(" adaptive MCM (delta/5): M =", RA$nsim, "(published 1.23e6, 1.02e6)\n")
######## 9.2.3 Rectangular inputs (Table 3) ########
B <- data.frame(name = paste0("X", 1:4), value = 0, a = sqrt(3), dist = "rect")
R2 <- propagate(EX, makeDat(EX, data = B, output = "sim", nsim = M), type = "sim", check = FALSE)
R2
y <- R2$resSIM; a <- sqrt(3)
irwin <- function(x) sum(sapply(0:floor(x), function(k) (-1)^k * choose(4, k) * (x - k)^4))/24
cat("Comparison with JCGM 101, Table 3:\n")
gauge("MCM u", R2$sim["u.MC"], "2.00", seSD(y)); gauge("MCM interval, high", R2$sim[6], "3.88", seQ(y, 0.975))
gauge("analytical interval, high (Irwin-Hall)", uniroot(function(x) irwin((x + 4 * a)/(2 * a)) - 0.975, c(0, 4 * a))$root, "3.88")
######## 9.2.4 One dominant rectangular input (Table 4) ########
B$a[4] <- 10 * sqrt(3)
R3 <- propagate(EX, makeDat(EX, data = B, output = "sim", nsim = M), type = "sim", check = FALSE)
R3
y <- R3$resSIM
cat("Comparison with JCGM 101, Table 4:\n")
gauge("GUF u", R3$prop["u.1"], "10.1"); gauge("GUF interval, high", tail(R3$prop, 2)[2], "19.9")
gauge("MCM interval, low", R3$sim[5], "-17.0", seQ(y, 0.025)); gauge("MCM interval, high", R3$sim[6], "17.0", seQ(y, 0.975))
######## 9.3 Mass calibration (Tables 5, 6) ########
data(S1.9.3)
EX3 <- expression((mR + dmR) * (1 + (ra - 1.2) * (1/rW - 1/rR)) - 100000)
R4 <- propagate(EX3, makeDat(EX3, data = S1.9.3, output = "sim", nsim = M), type = "sim", check = FALSE)
R4
y <- R4$resSIM; g2 <- R4$prop["mean.1"] + c(-1, 1) * 1.96 * R4$prop["u.2"]
cat("Comparison with JCGM 101, Table 6:\n")
gauge("GUF1 u [mg]", R4$prop["u.1"], "0.0539"); gauge("GUF1 interval, low [mg]", R4$prop["mean.1"] - 1.96 * R4$prop["u.1"], "1.1285")
gauge("GUF2 u [mg]", R4$prop["u.2"], "0.0750")
gauge("GUF2 interval, low [mg] (published: rounded u2)", g2[1], "1.0870", note = TRUE)
gauge("MCM mean [mg]", R4$sim["mean.MC"], "1.2341", seMean(y)); gauge("MCM u [mg]", R4$sim["u.MC"], "0.0754", seSD(y))
gauge("MCM shortest interval, low [mg]", R4$ci.short[1], "1.0834", 2 * seQ(y, 0.025))
gauge("MCM shortest interval, high [mg]", R4$ci.short[2], "1.3825", 2 * seQ(y, 0.975))
######## 9.4 Comparison loss dY = X1^2 + X2^2 (Tables 8, 9; units 1e-6) ########
EX4 <- expression(1E6 * (X1^2 + X2^2))
PUB <- list("0" = rbind(c("50", "50", "0", "150"), c("150", "112", "0", "367"), c("2551", "502", "1590", "3543")),
"0.9" = rbind(c("50", "67", "0", "185"), c("150", "121", "13", "398"), c("2551", "504", "1628", "3555")))
op <- par(mfrow = c(2, 3))
for (r in c("0", "0.9")) for (j in 1:3) {
x1 <- c(0, 0.010, 0.050)[j]; rr <- as.numeric(r)
COV <- matrix(c(1, rr, rr, 1) * 0.005^2, 2, dimnames = list(c("X1", "X2"), c("X1", "X2")))
R5 <- propagate(EX4, cbind(X1 = c(x1, 0.005, Inf), X2 = c(0, 0.005, Inf)), type = "stat", cov = COV, nsim = M, check = FALSE)
print(R5)
y <- R5$resSIM; P <- PUB[[r]][j, ]
cat("Comparison with JCGM 101, Table", if (r == "0") 8 else 9, "(r =", r, ", x1 =", x1, "):\n")
gauge("MCM mean", R5$sim["mean.MC"], P[1], seMean(y)); gauge("MCM u", R5$sim["u.MC"], P[2], seSD(y))
gauge("MCM shortest interval, low", R5$ci.short[1], P[3], if (x1 == 0) 0 else 2 * seQ(y, 0.025))
gauge("MCM shortest interval, high", R5$ci.short[2], P[4], 2 * seQ(y, 0.975))
}
par(op)
######## 9.5 Gauge block calibration (Tables 10, 11) ########
data(S1.9.5)
EX5 <- expression(Ls + D + d1 + d2 - Ls * (da * (th0 + Dl) + as * dth) - 50000000)
R6 <- propagate(EX5, makeDat(EX5, data = S1.9.5, output = "sim", nsim = M), type = "sim", alpha = 0.01, check = FALSE)
R6
y <- R6$resSIM
cat("Comparison with JCGM 101, Table 11:\n")
gauge("GUF dL [nm]", R6$prop["mean.1"], "838"); gauge("GUF u [nm]", R6$prop["u.1"], "32")
gauge("GUF 99 % interval, low [nm] (published: GUM H.1)", tail(R6$prop, 2)[1], "745", note = TRUE)
gauge("MCM dL [nm]", R6$sim["mean.MC"], "838", seMean(y)); gauge("MCM u [nm]", R6$sim["u.MC"], "36", seSD(y))
gauge("MCM shortest 99 % interval, low [nm]", R6$ci.short[1], "745", 2 * seQ(y, 0.005))
gauge("MCM shortest 99 % interval, high [nm]", R6$ci.short[2], "932", 2 * seQ(y, 0.995))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.