votes_distrib.stan 781 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. functions {
  2. real model_lpmf(array[] int counts,
  3. int start, int end,
  4. array[,] int votes,
  5. vector alphas
  6. ) {
  7. real ll = 0;
  8. for (k in start:end) {
  9. ll += dirichlet_multinomial_lpmf(votes[k] | alphas);
  10. }
  11. return ll;
  12. }
  13. }
  14. data {
  15. int<lower=1> N;
  16. int<lower=1> C;
  17. array[N,C] int<lower=0> votes;
  18. }
  19. transformed data {
  20. array[N] int counts;
  21. for (i in 1:N) {
  22. counts[i] = sum(votes[i]);
  23. }
  24. }
  25. parameters {
  26. vector<lower=0>[C] alphas;
  27. //array[N] simplex[C] p;
  28. }
  29. model {
  30. target += reduce_sum(
  31. model_lpmf, counts, 1,
  32. votes, alphas
  33. );
  34. alphas ~ exponential(1);
  35. }
  36. generated quantities {
  37. simplex[C] p = dirichlet_rng(alphas);
  38. }