Skip to content

Instantly share code, notes, and snippets.

@jakob-r
Last active January 26, 2018 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakob-r/eeae893e8a2a2391fa09900185bbf61b to your computer and use it in GitHub Desktop.
Save jakob-r/eeae893e8a2a2391fa09900185bbf61b to your computer and use it in GitHub Desktop.
How to sample weights uniformly
n = 5
x = runif(1000 * n)
mat = matrix(x, ncol = n)
mat2 = apply(mat, 1, function(x) {
ox = order(x)
rox = match(x, x[ox])
dx = diff(c(0,x[ox]))
dx[rox]
})
mat2 = t(mat2)
mat2 = cbind(mat2, 1 - rowSums(mat2))
for (i in seq_len(ncol(mat2))) {
plot.ecdf(mat2[,i], add = i!=1, col = i)
}
# EXPERIMENTAL
# now we use this ecdf to transform 0,1 values to weights
myecdf = ecdf(mat2[,1])
n = 5
x = runif(1000 * n)
mat3 = matrix(x, ncol = n)
mat4 = apply(mat3, 1, function(x) {
quantile(myecdf, x)
})
mat4 = t(mat4)
mat4 = cbind(mat4, 1 - rowSums(mat4))
for (i in seq_len(ncol(mat4))) {
plot.ecdf(mat4[,i], add = i!=1, col = i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment