Skip to content

Instantly share code, notes, and snippets.

@DexGroves
Created August 19, 2016 17:11
Show Gist options
  • Save DexGroves/6975ba4a21555a56c83cec3bb12756db to your computer and use it in GitHub Desktop.
Save DexGroves/6975ba4a21555a56c83cec3bb12756db to your computer and use it in GitHub Desktop.
Lowest unique common integer code
generate_fks <- function(N) {
fki <- log(N + 1)
fks <- c(fki)
for (i in seq(N)) {
fki <- log(exp(fki) - fki)
fks <- c(fks, fki)
}
fks
}
normalise_vec <- function(v) {
v / sum(v)
}
#' Probability that there is no one on k
mu <- function(k, P, N) {
(1 - P[k]) ^ (N - 1)
}
#' Probability that there is no one unique BELOW j given that no one is on k
nu <- function(j, k, P, N) {
if (j == 1) {
return(1)
}
lhs <- nu(j-1, k, P, N)
rhs <- 1 - ((N-1) * (P[j-1] / sum(P[-k])) * sum(P[-c(j-1, k)])^(N-2))
lhs * rhs
}
#' EXPECTATION of picking i if a population of N-1 play according to P
E <- function(i, P, N) {
mu(i, P, N) * nu(i, i, P, N)
}
N <- 100
P <- generate_fks(N) %>% normalise_vec
sapply(seq(N), E, P=P, N=N) %>% plot
sapply(seq(N), function(x) nu(x, x, P, N)) %>% plot
nu(50, 50, P, N)
j <- 50
k <- 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment