Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created July 8, 2024 15:48
Show Gist options
  • Save abikoushi/3422e7c424c15275312e895d74768712 to your computer and use it in GitHub Desktop.
Save abikoushi/3422e7c424c15275312e895d74768712 to your computer and use it in GitHub Desktop.
p-values of fisher exact test (and likelihood)
using Distributions
using Plots
function Fishertest(X, phi)
cs = sum(X, dims=1)
rs = sum(X, dims=2)
f(x, phi) = pdf(FisherNoncentralHypergeometric(cs[1],cs[2],rs[1], exp(phi)), x)
prob = f.(0:cs[1], phi)
return sum(prob[prob .<= prob[X[1,1]+1]])
end
X = [12 6; 5 12]
pfun(phi) = Fishertest(X, phi)
plot(x->pfun(x), label="p-value")
cs = sum(X, dims=1)
rs = sum(X, dims=2)
f(phi) = pdf(FisherNoncentralHypergeometric(cs[1],cs[2],rs[1], exp(phi)),X[1,1])
plot!(x->f(x), label="likelihood")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment