# Model for Hardy-Weinberg data in which we have genotypes corresponding to k alleles-
# we have k parameters, k-1 distinct allele probabilities q and an inbreeding coefficient f.
# We have a dirichlet prior on q. f is constrained to be > fmin =  -qmin/(1-qmin) where qmin
# is the minimum of q[1],...,q[k].
#

model
{
# genotype frequencies, q's are allele frequencies, f is the common inbreeding coefficient
  for (i in 1:k)
  {
      for (j in 1:i)
      {
         p[i*(i-1)/2+j] <- equals(i,j)*(q[i]*q[i] + f*q[i]*(1-q[i])) + (1-equals(i,j))*2*q[i]*q[j]*(1-f)
      }
  }
# likelihood
  n[1:ncell] ~ dmulti(p[],N)
# priors
  for (i in 1:k)
  {
      q[i]  <- delta[i]/sum(delta[])
      delta[i] ~ dgamma(1,1)
  }
  qmin <- sort(q[])
  fmin <- -qmin[1]/(1-qmin[1])
  lambda ~ dnorm(lambdamu,lambdasd)
  f <- (exp(lambda)+fmin)/(exp(lambda)+1)
  for (i in 1:(k-1))
  {
      theta[i] <- log(q[i]/q[k])
  }
  theta[k] <- log((f-fmin)/(1-f))
}
