Skip to content

Commit b3a009d

Browse files
committed
formulas double checked
1 parent 2cbba77 commit b3a009d

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

confIntVariance/R/main.R

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,63 @@ productsOfDisjointTuples <- function(s) {
1111
N[, n]
1212
}
1313

14+
standardEx <- c(2,2,4,6,2, 6,4,5,4,6)
15+
16+
lsesv <- function(x) {
17+
v <- var(x)
18+
n <- length(x)
19+
elSym <- productsOfDisjointTuples(x)
20+
# triple-checked formula
21+
(2/n/(n-1) * elSym[2]^2 - 4/n/(n-2) * elSym[1] * elSym[3] + 4/(n-2)/(n-3) * elSym[4])
22+
}
23+
24+
lsesvExp <- function(x) {
25+
stopifnot(length(x)==6)
26+
p1 <- sum(x)
27+
p2 <- sum(x^2)
28+
p3 <- sum(x^3)
29+
p4 <- sum(x^4)
30+
(1/360)*p1^4 +(-1/30)*p1^2*p2 +(7/120)*p2^2 +(1/18)*p1*p3 +(-1/12)*p4
31+
}
32+
1433
varianceOfSampleVariance <- function(x) {
1534
v <- var(x)
1635
n <- length(x)
1736
p1 <- productsOfDisjointTuples(x)
1837
p2 <- productsOfDisjointTuples(x^2)
19-
var(x)^2 - p2[2] / choose(n, 2) + 4/n/(n-1)/(n-2)*(p1[3]*sum(x) - 4 * p1[4]) - p1[4]/choose(n, 4)
38+
sv <- lsesv(x)
39+
# give the same value, up to numerical inaccuracies
40+
k1 <- var(x)^2 - p2[2] / choose(n, 2) + 4/n/(n-1)/(n-2)*(p1[3]*sum(x) - 4 * p1[4]) - p1[4]/choose(n, 4)
41+
k2 <- var(x)^2 - sv
42+
k1
2043
}
2144

45+
46+
x <- (1:6)^3
47+
print(
48+
mean(
49+
apply(
50+
combn(6, 4),
51+
2,
52+
function(col) {
53+
mean(
54+
c(
55+
(x[col[1]] - x[col[2]])^2 * (x[col[3]] - x[col[4]])^2,
56+
(x[col[1]] - x[col[3]])^2 * (x[col[2]] - x[col[4]])^2,
57+
(x[col[1]] - x[col[4]])^2 * (x[col[2]] - x[col[3]])^2
58+
)
59+
)
60+
}
61+
)
62+
)
63+
/4
64+
)
65+
66+
67+
68+
69+
70+
2271
# the confidence interval for the variance
2372
varwci <- function(x, conf.level=0.95) {
2473
if (is.data.frame(x)) {

0 commit comments

Comments
 (0)