Nothing
## propagate demo: all examples of JCGM 102:2011 (GUM Supplement 2), clause 9
## demo("JCGM102", package = "propagate")
## For each example: propagateMV() output (GUM uncertainty framework and Monte Carlo), histograms of
## the Monte Carlo outputs with the best fitting distribution, and a comparison with the published values.
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 }
seR <- function(r) sqrt(2) * (1 - r^2)/sqrt(M) ## standard error of a correlation coefficient
set.seed(102)
######## 9.2 Additive bivariate model Y1 = X1 + X3, Y2 = X2 + X3 (Tables 3 - 5) ########
EX2 <- expression(Y1 = X1 + X3, Y2 = X2 + X3)
PUB <- rbind(c("1.416", "0.500", "2.45", "2.21"), c("1.414", "0.499", "2.38", "2.15"), c("3.161", "0.900", "2.28", "1.87"))
for (cs in 1:3) {
B <- data.frame(name = c("X1", "X2", "X3"), value = 0, u = c(1, 1, if (cs == 1) 1 else NA),
a = c(NA, NA, if (cs == 1) NA else sqrt(3) * c(1, 1, 3)[cs]),
dist = c("normal", "normal", if (cs == 1) "normal" else "rect"))
R <- propagateMV(EX2, makeDat(EX2, data = B, output = "sim", nsim = M), type = "sim")
print(R)
cat("Comparison with JCGM 102, Table", 2 + cs, ":\n")
gauge("GUF u(y1)", R$guf$u[1], if (cs == 3) "3.162" else "1.414"); gauge("GUF r(y1, y2)", R$guf$r[1, 2], if (cs == 3) "0.900" else "0.500")
gauge("GUF k_p", R$guf$kp, "2.45"); gauge("GUF k_q", R$guf$kq, "2.24")
gauge("MCM u(y1)", R$mc$u[1], PUB[cs, 1], seSD(R$resSIM[, 1])); gauge("MCM r(y1, y2)", R$mc$r[1, 2], PUB[cs, 2], seR(R$mc$r[1, 2]))
gauge("MCM k_p", R$mc$kp, PUB[cs, 3], 0.003); gauge("MCM k_q", R$mc$kq, PUB[cs, 4], 0.003)
}
######## 9.3 Cartesian to polar co-ordinates (Tables 6, 7) ########
EX3 <- expression(R = sqrt(X1^2 + X2^2), Theta = atan2(X2, X1))
PUB <- list("0" = rbind(c("0.013", "-0.001", "0.007", "1.744", "0.000"), c("0.015", "0.000", "0.008", "1.117", "0.000"),
c("0.101", "0.000", "0.010", "0.101", "0.000")),
"0.9" = rbind(c("0.012", "-0.556", "0.008", "1.599", "-0.070"), c("0.015", "-0.343", "0.008", "0.903", "0.352"),
c("0.101", "-0.009", "0.010", "0.102", "0.882")))
for (r in c("0", "0.9")) for (j in 1:3) {
x1 <- c(0.001, 0.010, 0.100)[j]; rr <- as.numeric(r)
COV <- matrix(c(1, rr, rr, 1) * 1E-4, 2, dimnames = list(c("X1", "X2"), c("X1", "X2")))
R <- propagateMV(EX3, cbind(X1 = c(x1, 0.010, Inf), X2 = c(0, 0.010, Inf)), type = "stat", cov = COV, nsim = M)
print(R)
P <- PUB[[r]][j, ]; Y <- R$resSIM
cat("Comparison with JCGM 102, Table", if (r == "0") 6 else 7, "(r =", r, ", x1 =", x1, "):\n")
gauge("GUF u(Theta)", R$guf$u[2], formatC(0.010/x1, format = "f", digits = 3))
gauge("MCM R", R$mc$y[1], P[1], seMean(Y[, 1])); gauge("MCM Theta", R$mc$y[2], P[2], seMean(Y[, 2]))
gauge("MCM u(R)", R$mc$u[1], P[3], seSD(Y[, 1])); gauge("MCM u(Theta)", R$mc$u[2], P[4], seSD(Y[, 2]))
gauge("MCM r(R, Theta)", R$mc$r[1, 2], P[5], seR(R$mc$r[1, 2]))
}
######## 9.4 Resistance and reactance, n = 6 (Tables 8 - 11) ########
data(S2.9.4)
EX4 <- expression(R = (V/I) * cos(phi), X = (V/I) * sin(phi), Z = V/I)
R <- propagateMV(EX4, S2.9.4, type = "raw", cov = "full", mc.margins = "mvt", nsim = M)
R
cat("Comparison with JCGM 102, Table 11:\n")
gauge("GUF R [Ohm] (published: truncated phi)", R$guf$y[1], "127.732", note = TRUE)
for (k in 1:3) gauge(paste0("GUF u(", R$names[k], ") [Ohm]"), R$guf$u[k], c("0.058", "0.241", "0.193")[k])
gauge("GUF r(R, X)", R$guf$r[1, 2], "-0.588"); gauge("GUF 1 - r(X, Z) [1e-2]", 100 * (1 - R$guf$r[2, 3]), "0.749")
## nu = 3: the output has no finite fourth moment, Monte Carlo u and r converge slowly (wide tolerance)
for (k in 1:3) gauge(paste0("MCM u(", R$names[k], ") [Ohm]"), R$mc$u[k], c("0.130", "0.536", "0.429")[k], 3 * seSD(R$resSIM[, k]))
for (k in 1:3) gauge(paste0("alternative GUF u(", R$names[k], ") [Ohm]"), sqrt(5) * R$guf$u[k], c("0.130", "0.540", "0.431")[k])
######## 9.5 Celsius temperature with a resistance thermometer (Tables 12 - 16) ########
data(S2.9.5)
IN <- S2.9.5$inputs; RJ <- S2.9.5$ratios
th <- function(r) paste0("(-A + sqrt(A^2 - 4 * B * (1 - ", r, " * RS/R0)))/(2 * B)")
EX5 <- parse(text = th(paste0("r", RJ$j))); names(EX5) <- paste0("t", RJ$j)
DF5 <- cbind(sapply(seq_len(nrow(IN)), function(i) c(IN$value[i], IN$u[i], Inf)),
sapply(seq_len(nrow(RJ)), function(j) c(RJ$r[j], RJ$u[j], Inf)))
colnames(DF5) <- c(IN$name, paste0("r", RJ$j))
COV5 <- diag(DF5[2, ]^2); dimnames(COV5) <- rep(list(colnames(DF5)), 2)
COV5[1:3, 1:3] <- S2.9.5$cor * outer(IN$u[1:3], IN$u[1:3]) ## R0, A, B correlated (Table 13)
R <- propagateMV(EX5, DF5, type = "stat", cov = COV5, nsim = M)
R
cat("Comparison with JCGM 102, Tables 15, 16:\n")
TP <- c("0.0100", "3.8491", "7.6928", "11.5410", "15.3938", "20.0232", "23.1131", "26.9797", "30.8509", "20.0232")
UP <- c("0.0018", "0.0027", "0.0040", "0.0046", "0.0047", "0.0045", "0.0046", "0.0060", "0.0089", "0.0045")
for (k in 1:10) { gauge(paste0("t", k, " [degC]"), R$guf$y[k], TP[k]); gauge(paste0("u(t", k, ") [degC]"), R$guf$u[k], UP[k]) }
gauge("r(t6, t10)", R$guf$r[6, 10], "0.918"); gauge("r(t1, t2)", R$guf$r[1, 2], "0.252")
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.