Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Last active October 22, 2025 09:54
Show Gist options
  • Select an option

  • Save abikoushi/248459f017f2649298eea2757242bde6 to your computer and use it in GitHub Desktop.

Select an option

Save abikoushi/248459f017f2649298eea2757242bde6 to your computer and use it in GitHub Desktop.
Check probability function of a multivariate Fisher's noncentral hypergeometric distribution
library(BiasedUrn)
softmax <- function(x){
mx = max(x)
ex = exp(x-mx)
ex/sum(ex)
}
MVNCH_Fisher <- function(K,M,OR){
CB = combn(K,M)
nr = ncol(CB)
X = matrix(0, nr, K)
for(i in 1:nr){
X[i, CB[,i]]=1
}
prob = softmax(X%*%log(OR))
list(X=X, prob = prob)
}
K=10
set.seed(123); OR = rexp(K)
M=3
dist_F = MVNCH_Fisher(K,M,OR)
theo_F = numeric(nrow(dist_F$X))
for(i in 1:nrow(dist_F$X)){
theo_F[i] = dMFNCHypergeo(x = dist_F$X[i,], m = rep(1, K), n=M, odds = OR)
}
plot(dist_F$prob, theo_F)
abline(0, 1, lty=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment