Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,15 @@
|
|||
cumsd <- function(x) {
|
||||
n <- seq_along(x)
|
||||
sqrt(cumsum(x^2) / n - (cumsum(x) / n)^2)
|
||||
}
|
||||
|
||||
set.seed(12345L)
|
||||
x <- rnorm(10)
|
||||
|
||||
cumsd(x)
|
||||
# [1] 0.0000000 0.3380816 0.8752973 1.1783628 1.2345538 1.3757142 1.2867220 1.2229056 1.1665168 1.1096814
|
||||
|
||||
# Compare to the naive implementation, i.e. compute sd on each sublist:
|
||||
Vectorize(function(k) sd(x[1:k]) * sqrt((k - 1) / k))(seq_along(x))
|
||||
# [1] NA 0.3380816 0.8752973 1.1783628 1.2345538 1.3757142 1.2867220 1.2229056 1.1665168 1.1096814
|
||||
# Note that the first is NA because sd is unbiased formula, hence there is a division by n-1, which is 0 for n=1.
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
accumsd <- function() {
|
||||
n <- 0
|
||||
m <- 0
|
||||
s <- 0
|
||||
|
||||
function(x) {
|
||||
n <<- n + 1
|
||||
m <<- m + x
|
||||
s <<- s + x * x
|
||||
sqrt(s / n - (m / n)^2)
|
||||
}
|
||||
}
|
||||
|
||||
f <- accumsd()
|
||||
sapply(x, f)
|
||||
# [1] 0.0000000 0.3380816 0.8752973 1.1783628 1.2345538 1.3757142 1.2867220 1.2229056 1.1665168 1.1096814
|
||||
Loading…
Add table
Add a link
Reference in a new issue