Nothing
## propagate demo: correlated input quantities with corDist() (Gaussian copula), gauged against
## JCGM 101:2008, 9.4.3 (Table 9), Possolo (2010), Metrologia 47, 262-271 (sections 2.4 and 3.1,
## Table 3) and exact results for non-Gaussian margins
## demo("COPULA", 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(" %-52s published %10s propagate %12.*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)
}
## exact (analytical) values instead of published ones: labelled "exact", counted as PASS/DIFF by doGauge()
same <- function(item, value, exact, tol) cat(sprintf(" %-52s exact %14.6g propagate %12.6g |diff| %8.2g tol %8.2g %s\n",
item, exact, value, abs(value - exact), tol,
if (abs(value - exact) <= tol) green("PASS") else blue("DIFF")))
set.seed(101)
######## JCGM 101:2008, 9.4.3: comparison loss, correlated Gaussian inputs, r(x1, x2) = 0.9 (Table 9) ########
## dY = X1^2 + X2^2, u(x1) = u(x2) = 0.005, x2 = 0; MCM shortest 95 % coverage interval; values / 1E-6
R9 <- matrix(c(1, 0.9, 0.9, 1), 2)
TAB9 <- list("0.000" = c(y = "50", u = "67", lo = "0", hi = "185"),
"0.010" = c(y = "150", u = "121", lo = "13", hi = "398"),
"0.050" = c(y = "2551", u = "504", lo = "1628", hi = "3555"))
for (x1 in names(TAB9)) {
X <- corDist(mean = c(X1 = as.numeric(x1), X2 = 0), u = 0.005, dist = "normal", cor = R9, nsim = M)
RES <- suppressMessages(propagate(expression(1E6 * (X1^2 + X2^2)), X, type = "sim", cov = "full", check = FALSE))
cat("Comparison with JCGM 101, Table 9 (MCM), x1 =", x1, ":\n")
P <- TAB9[[x1]]; SE <- RES$sim.se
gauge("estimate dy [1E-6]", RES$sim["mean.MC"], P["y"], se = SE["mean.MC"])
gauge("u(dy) [1E-6]", RES$sim["u.MC"], P["u"], se = SE["u.MC"])
gauge("shortest 95 % interval, low [1E-6]", RES$ci.short[1], P["lo"], se = SE["short.low"])
gauge("shortest 95 % interval, high [1E-6]", RES$ci.short[2], P["hi"], se = SE["short.high"])
## analytical moments: E = x1^2 + 2u^2, Var = 4 x1^2 u^2 + 2 u^4 (2 + 2 r^2) -- exact for bivariate Gaussian
u <- 0.005; xx <- as.numeric(x1)
same("analytical u(dy) [1E-6] (JCGM 101, Table 9)", RES$sim["u.MC"], 1E6 * sqrt(4 * xx^2 * u^2 + 4 * u^4 * (1 + 0.9^2)), 4 * SE["u.MC"] + 1E-9)
}
######## Possolo (2010), 3.1, Table 3: GUM H.2 with a Gaussian copula and Gaussian margins ########
## R = V/I cos(phi), X = V/I sin(phi), Z = V/I; inputs from the five sets of indications (Table H.2)
v <- c(5.007, 4.994, 5.005, 4.990, 4.999)
i <- c(19.663, 19.639, 19.640, 19.685, 19.678)/1000
phi <- c(1.0456, 1.0438, 1.0468, 1.0428, 1.0433)
IN <- cbind(V = v, I = i, phi = phi)
XH2 <- corDist(mean = colMeans(IN), u = apply(IN, 2, sd)/sqrt(5), dist = "normal", cor = cor(IN), nsim = M)
cat("Comparison with Possolo (2010), Table 3, Gaussian copula with Gaussian margins (m = 5E6 in the paper):\n")
for (out in c("R", "X", "Z")) {
EX <- switch(out, R = expression(V/I * cos(phi)), X = expression(V/I * sin(phi)), Z = expression(V/I))
RES <- suppressMessages(propagate(EX, XH2, type = "sim", cov = "full", check = FALSE))
y <- RES$resSIM; Q <- quantile(y, c(0.005, 0.995)); SE <- RES$sim.se
P <- list(R = c("127.7", "0.0711", "0.18"), X = c("219.8", "0.296", "0.76"), Z = c("254.3", "0.236", "0.61"))[[out]]
gauge(paste0("AVE(", out, ") [Ohm]"), RES$sim["mean.MC"], P[1], se = SE["mean.MC"])
gauge(paste0("u(", out, ") [Ohm]"), RES$sim["u.MC"], P[2], se = SE["u.MC"])
gauge(paste0("U99(", out, ") = half-width of 99 % interval [Ohm]"), diff(Q)/2, P[3], se = 2 * max(SE[c("sym.low", "sym.high")]))
}
######## Possolo (2010), 2.4: viscosity, Gaussian and non-central chi-square margins, copula rho = -0.78 ########
## K = X/(a Y), X ~ N(7.63, 0.36^2) cm^3/g, Y ~ non-central chi^2 (1 DOF, ncp 3.41E3), a = 1.84E-3;
## the copula parameter is -0.78 (as in the R code of the paper), i.e. cor.type = "copula"
XV <- corDist(mean = c(X = 7.63, Y = NA), u = c(0.36, NA), dist = list("normal", function(n) rchisq(n, df = 1, ncp = 3410)),
cor = matrix(c(1, -0.78, -0.78, 1), 2), cor.type = "copula", nsim = M)
RV <- suppressMessages(propagate(expression(X/(1.84E-3 * Y)), XV, type = "sim", cov = "full", check = FALSE))
cat("Comparison with Possolo (2010), 2.4, viscosity:\n")
gauge("mean of Y (published 3410)", mean(XV[, "Y"]), "3410", se = sd(XV[, "Y"])/sqrt(M), extra = 1) ## E(Y) = 1 + ncp
gauge("sd of Y (published 117)", sd(XV[, "Y"]), "117", se = sd(XV[, "Y"])/sqrt(2 * M))
gauge("estimate K [g/cm^3]", RV$sim["mean.MC"], "1.22", se = RV$sim.se["mean.MC"])
gauge("u(K) [g/cm^3]", RV$sim["u.MC"], "0.094", se = RV$sim.se["u.MC"])
gauge("95 % interval, low [g/cm^3]", RV$sim[5], "1.04", se = RV$sim.se["sym.low"])
gauge("95 % interval, high [g/cm^3]", RV$sim[6], "1.41", se = RV$sim.se["sym.high"])
######## Exact results for non-Gaussian margins (no published values) ########
cat("Exact properties of corDist() for non-Gaussian margins:\n")
## (a) Pearson matching: two rectangular inputs, r = 0.8, Y = X1 + X2: u(y) = sqrt(1 + 1 + 2 * 0.8)
XR <- corDist(mean = c(X1 = 10, X2 = 5), u = c(1, 1), dist = "rect", cor = matrix(c(1, 0.8, 0.8, 1), 2), nsim = M)
RR <- suppressMessages(propagate(expression(X1 + X2), XR, type = "sim", cov = "full", check = FALSE))
same("Pearson correlation of the samples (target 0.8)", cor(XR)[1, 2], 0.8, 4/sqrt(M))
same("u(X1 + X2) MCM = sqrt(3.6)", RR$sim["u.MC"], sqrt(3.6), 4 * RR$sim.se["u.MC"])
same("u(X1 + X2) GUF = sqrt(3.6)", RR$prop["u.1"], sqrt(3.6), 0.002)
## (b) rectangular margins: copula correlation for Pearson r is 2 sin(pi r/6) (Kruskal 1958)
same("copula correlation, rect/rect, r = 0.8", attr(XR, "corMat.copula")[1, 2], 2 * sin(pi * 0.8/6), 1E-6)
## (c) copula mode: Spearman rank correlation = 6/pi asin(rho/2) for ANY margins (here arcsine/gamma sampler)
XS <- corDist(mean = c(A = 0, B = NA), u = c(1, NA), dist = list("arcsine", function(n) rgamma(n, 2, 1)),
cor = matrix(c(1, 0.6, 0.6, 1), 2), cor.type = "copula", nsim = M)
same("Spearman correlation, arcsine/gamma, rho = 0.6", cor(XS[, 1], XS[, 2], method = "spearman"), 6/pi * asin(0.3), 4/sqrt(M))
## (d) margins are preserved: moments of the arcsine margin (u = 1) and of the gamma sampler (mean 2, sd sqrt(2))
same("sd of the arcsine margin", sd(XS[, "A"]), 1, 4 * sqrt(0.5/M))
same("mean of the gamma margin", mean(XS[, "B"]), 2, 4 * sqrt(2/M))
same("sd of the gamma margin", sd(XS[, "B"]), sqrt(2), 4 * sqrt(4/M))
## (e) curvilinear trapezoid (JCGM 101, 6.4.3): u^2 = a^2/3 + d^2/9 with a = 1, d = 0.2
uct <- sqrt(1/3 + 0.04/9)
XC <- corDist(mean = c(C = 0), u = uct, dist = "ctrap", d = 0.2, nsim = M)
same("sd of the curvilinear trapezoidal margin", sd(XC[, 1]), uct, 4 * uct/sqrt(2 * M))
same("support half-width a + d = 1.2", max(abs(XC[, 1])), 1.2, 0.002)
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.