Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
Created May 31, 2013 19:39
Show Gist options
  • Save johnmyleswhite/5687454 to your computer and use it in GitHub Desktop.
Save johnmyleswhite/5687454 to your computer and use it in GitHub Desktop.
Generating Random Probability Vectors
# Uniform
a <- runif(10000, 0, 1)
b <- runif(10000, 0, 1)
c <- runif(10000, 0, 1)
df <- cbind(a, b, c)
df <- transform(df, s = a + b + c)
df <- transform(df, a = a / s)
df <- transform(df, b = b / s)
df <- transform(df, c = c / s)
ggplot(df, aes(x = a, y = b)) + geom_point()
# Negative Log Uniform
a <- -log(runif(10000, 0, 1))
b <- -log(runif(10000, 0, 1))
c <- -log(runif(10000, 0, 1))
df <- cbind(a, b, c)
df <- transform(df, s = a + b + c)
df <- transform(df, a = a / s)
df <- transform(df, b = b / s)
df <- transform(df, c = c / s)
ggplot(df, aes(x = a, y = b)) + geom_point()
# Gamma
a <- rgamma(10000, 1, 1)
b <- rgamma(10000, 1, 1)
c <- rgamma(10000, 1, 1)
df <- cbind(a, b, c)
df <- transform(df, s = a + b + c)
df <- transform(df, a = a / s)
df <- transform(df, b = b / s)
df <- transform(df, c = c / s)
ggplot(df, aes(x = a, y = b)) + geom_point()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment