dev_siblings_effect.stan 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. functions {
  2. real confusion_model_lpmf(array[] int group,
  3. int start, int end,
  4. int n_classes,
  5. array[,] int algo,
  6. array[,] int truth,
  7. array[] real age,
  8. array[] real clip_duration,
  9. array[] matrix lambda//,
  10. //array[] vector lambda_fp,
  11. ) {
  12. real ll = 0;
  13. vector [4] bp;
  14. vector[8192] log_contrib_comb;
  15. int n = size(log_contrib_comb);
  16. for (k in start:end) {
  17. for (i in 1:n_classes) {
  18. log_contrib_comb[:n] = rep_vector(0, n);
  19. n = 1;
  20. for (chi in 0:(truth[k,1]>0?max(truth[k,1], algo[k,i]):0)) {
  21. bp[1] = truth[k,1]==0?0:poisson_lpmf(chi | truth[k,1]*lambda[group[k-start+1],1,i]);
  22. for (och in 0:(truth[k,2]>0?max(truth[k,2], algo[k,i]-chi):0)) {
  23. bp[2] = truth[k,2]==0?0:poisson_lpmf(och | truth[k,2]*lambda[group[k-start+1],2,i]);
  24. for (fem in 0:(truth[k,3]>0?max(truth[k,3], algo[k,i]-chi-och):0)) {
  25. bp[3] = truth[k,3]==0?0:poisson_lpmf(fem | truth[k,3]*lambda[group[k-start+1],3,i]);
  26. for (mal in 0:(truth[k,4]>0?max(truth[k,4], algo[k,i]-chi-och-fem):0)) {
  27. bp[4] = truth[k,4]==0?0:poisson_lpmf(mal | truth[k,4]*lambda[group[k-start+1],4,i]);
  28. int delta = algo[k,i] - (mal+fem+och+chi);
  29. // if (delta >= 0) {
  30. // log_contrib_comb[n] += sum(bp);
  31. // log_contrib_comb[n] += poisson_lpmf(
  32. // delta | lambda_fp[group[k-start+1],i]*clip_duration[k]
  33. // );
  34. // n = n+1;
  35. // }
  36. if (delta==0) {
  37. log_contrib_comb[n] += sum(bp);
  38. n = n+1;
  39. }
  40. }
  41. }
  42. }
  43. }
  44. if (n>1) {
  45. ll += log_sum_exp(log_contrib_comb[1:n-1]);
  46. }
  47. }
  48. }
  49. return ll;
  50. }
  51. real inverse_model_lpmf(array[] int children,
  52. int start, int end,
  53. int n_recs,
  54. int n_classes,
  55. real duration,
  56. array [,] int vocs,
  57. array [] real age,
  58. matrix truth_vocs,
  59. array [] matrix actual_confusion,
  60. //array [] vector actual_fp_rate,
  61. matrix mus,
  62. matrix alphas//,
  63. //vector mus_fp,
  64. //vector alphas_fp
  65. ) {
  66. real ll = 0;
  67. vector [4] expect;
  68. for (k in start:end) {
  69. expect = rep_vector(0, 4);
  70. for (i in 1:n_classes) {
  71. ll += gamma_lpdf(actual_confusion[k,i] | alphas[i,:], alphas[i,:]./mus[i,:]);
  72. //ll += gamma_lpdf(actual_fp_rate[k] | alphas_fp, alphas_fp./mus_fp);
  73. expect[i] = dot_product(truth_vocs[k,:], actual_confusion[k,:,i]);
  74. //expect[i] += actual_fp_rate[k,i] * duration;
  75. }
  76. ll += normal_lpdf(vocs[k,:] | expect, sqrt(expect));
  77. }
  78. return ll;
  79. }
  80. real recs_priors_lpmf(array[] int children,
  81. int start, int end,
  82. int n_recs,
  83. int n_classes,
  84. real recs_duration,
  85. array [] real age,
  86. matrix truth_vocs,
  87. vector mu_pop_level,
  88. matrix mu_child_level,
  89. vector alpha_child_level,
  90. vector child_dev_age,
  91. real beta_dev
  92. ) {
  93. real ll = 0;
  94. for (k in start:end) {
  95. real chi_mu = mu_pop_level[1]*exp(
  96. child_dev_age[children[k-start+1]]*age[k]/12.0/10.0+beta_dev*(mu_child_level[children[k-start+1],2]+mu_child_level[children[k-start+1],3]-mu_pop_level[3]-mu_pop_level[4])*age[k]/12.0/10.0
  97. );
  98. ll += gamma_lpdf(
  99. truth_vocs[k,1]/1000/recs_duration | alpha_child_level[1], alpha_child_level[1]/chi_mu
  100. );
  101. ll += gamma_lpdf(
  102. truth_vocs[k,2:]/1000/recs_duration | alpha_child_level[2:], alpha_child_level[2:]./mu_child_level[children[k-start+1],:]' //'
  103. );
  104. }
  105. return ll;
  106. }
  107. }
  108. // TODO
  109. // use speech rates to set priors on truth_vocs
  110. data {
  111. int<lower=1> n_classes; // number of classes
  112. // analysis data block
  113. int<lower=1> n_recs;
  114. int<lower=1> n_children;
  115. array[n_recs] int<lower=1> children;
  116. array[n_recs] real<lower=1> age;
  117. array[n_recs] int<lower=-1> siblings;
  118. array[n_recs, n_classes] int<lower=0> vocs;
  119. array[n_children] int<lower=1> corpus;
  120. real<lower=0> recs_duration;
  121. // speaker confusion data block
  122. int<lower=1> n_clips; // number of clips
  123. int<lower=1> n_groups; // number of groups
  124. int<lower=1> n_corpora;
  125. array [n_clips] int group;
  126. array [n_clips] int conf_corpus;
  127. array [n_clips,n_classes] int<lower=0> algo_total; // algo vocs attributed to specific speakers
  128. array [n_clips,n_classes] int<lower=0> truth_total;
  129. array [n_clips] real<lower=0> clip_duration;
  130. array [n_clips] real<lower=0> clip_age;
  131. int<lower=0> n_validation;
  132. // actual speech rates
  133. int<lower=1> n_rates;
  134. int<lower=1> n_speech_rate_children;
  135. array [n_rates,n_classes] int<lower=0> speech_rates;
  136. array [n_rates] int group_corpus;
  137. array [n_rates] real<lower=0> durations;
  138. array [n_rates] real<lower=0> speech_rate_age;
  139. array [n_rates] int<lower=-1> speech_rate_siblings;
  140. array [n_rates] int<lower=1,upper=n_speech_rate_children> speech_rate_child;
  141. // parallel processing
  142. int<lower=1> threads;
  143. }
  144. transformed data {
  145. vector<lower=0>[n_groups] recording_age;
  146. array[n_speech_rate_children] int<lower=1> speech_rate_child_corpus;
  147. array[n_children] int<lower=-1> child_siblings;
  148. array[n_speech_rate_children] int<lower=-1> speech_rate_child_siblings;
  149. int no_siblings = 0;
  150. int has_siblings = 0;
  151. for (c in 1:n_clips) {
  152. recording_age[group[c]] = clip_age[c];
  153. }
  154. for (k in 1:n_rates) {
  155. speech_rate_child_corpus[speech_rate_child[k]] = group_corpus[k];
  156. }
  157. for (k in 1:n_recs) {
  158. child_siblings[children[k]] = siblings[k];
  159. }
  160. for (c in 1:n_children) {
  161. if (child_siblings[c] == 0) {
  162. no_siblings += 1;
  163. }
  164. else if (child_siblings[c] > 0) {
  165. has_siblings += 1;
  166. }
  167. }
  168. for (k in 1:n_rates) {
  169. speech_rate_child_siblings[speech_rate_child[k]] = speech_rate_siblings[k];
  170. }
  171. }
  172. parameters {
  173. matrix<lower=0>[n_children,n_classes-1] mu_child_level;
  174. vector [n_children] child_dev_age;
  175. matrix<lower=0> [n_recs, n_classes] truth_vocs;
  176. // nuisance parameters
  177. array [n_recs] matrix<lower=0>[n_classes,n_classes] actual_confusion_baseline;
  178. //array [n_recs] vector<lower=0>[n_classes] actual_fp_rate;
  179. // confusion parameters
  180. // confusion matrix
  181. matrix<lower=0>[n_classes,n_classes] alphas;
  182. matrix<lower=0>[n_classes,n_classes] mus;
  183. array [n_groups] matrix<lower=0>[n_classes,n_classes] lambda;
  184. // false positives
  185. //vector<lower=0>[n_classes] alphas_fp;
  186. //vector<lower=0>[n_classes] mus_fp;
  187. //array [n_groups] vector<lower=0>[n_classes] lambda_fp;
  188. // speech rates
  189. vector<lower=0>[n_classes] alpha_child_level; // variance across recordings for a given child
  190. array[2] matrix<lower=0>[n_classes-1,n_corpora] alpha_corpus_level; // variance among children
  191. matrix<lower=0>[n_classes-1,n_corpora] mu_corpus_level; // child-level average
  192. vector<lower=0>[n_classes-1] alpha_pop_level; // variance among corpora
  193. vector<lower=0>[n_classes] mu_pop_level; // population level averages
  194. vector<lower=0>[n_classes-1] alpha_pop;
  195. matrix<lower=0>[n_classes,n_rates] speech_rate; // truth speech rates observed in annotated clips
  196. matrix<lower=0>[n_speech_rate_children,n_classes-1] speech_rate_child_level; // expected speech rate at the child-level
  197. // siblings
  198. vector[3] beta_sib; // effect of having siblings on OCH, FEM and MAL speech
  199. real<lower=0,upper=1> p_sib; // prob of having siblings
  200. vector [n_speech_rate_children] child_dev_speech_age;
  201. // average effect of age
  202. real alpha_dev;
  203. real<lower=0> sigma_dev;
  204. // effect of excess ADU input
  205. real beta_dev;
  206. }
  207. model {
  208. //actual model
  209. // inverse confusion model
  210. target += reduce_sum(
  211. inverse_model_lpmf, children, 1,
  212. n_recs, n_classes, recs_duration,
  213. vocs, age,
  214. truth_vocs, actual_confusion_baseline, mus, alphas//, mus_fp, alphas_fp
  215. );
  216. // priors on actual speech
  217. target += reduce_sum(
  218. recs_priors_lpmf, children, 1,
  219. n_recs, n_classes, recs_duration, age,
  220. truth_vocs,
  221. mu_pop_level, mu_child_level, alpha_child_level,
  222. child_dev_age, beta_dev
  223. );
  224. vector [2] ll;
  225. int distrib;
  226. for (c in 1:n_children) {
  227. // if there is sibling data
  228. if (child_siblings[c]>=0) {
  229. distrib = child_siblings[c]>0?2:1;
  230. mu_child_level[c,1] ~ gamma(
  231. alpha_corpus_level[distrib,1,corpus[c]],
  232. (alpha_corpus_level[distrib,1,corpus[c]]/(mu_corpus_level[1,corpus[c]]*exp(
  233. child_siblings[c]>0?beta_sib[1]:0
  234. )))
  235. );
  236. mu_child_level[c,2:] ~ gamma(
  237. alpha_corpus_level[distrib,2:,corpus[c]],
  238. (alpha_corpus_level[distrib,2:,corpus[c]]./mu_corpus_level[2:,corpus[c]].*exp(
  239. child_siblings[c]>0?beta_sib[2:]:rep_vector(0,2)
  240. ))
  241. );
  242. }
  243. // otherwise
  244. else {
  245. // assuming no sibling
  246. ll[1] = log(p_sib)+gamma_lpdf(
  247. mu_child_level[c,1] | alpha_corpus_level[2,1,corpus[c]], alpha_corpus_level[2,1,corpus[c]]/(mu_corpus_level[1,corpus[c]]*exp(beta_sib[1]))
  248. );
  249. ll[1] += gamma_lpdf(
  250. mu_child_level[c,2] | alpha_corpus_level[2,2,corpus[c]], alpha_corpus_level[2,2,corpus[c]]/(mu_corpus_level[2,corpus[c]]*exp(beta_sib[2]))
  251. );
  252. ll[1] += gamma_lpdf(
  253. mu_child_level[c,3] | alpha_corpus_level[2,3,corpus[c]], alpha_corpus_level[2,3,corpus[c]]/(mu_corpus_level[3,corpus[c]]*exp(beta_sib[3]))
  254. );
  255. // assuming sibling
  256. ll[2] = log(1-p_sib)+gamma_lpdf(
  257. mu_child_level[c,1] | alpha_corpus_level[1,1,corpus[c]], alpha_corpus_level[1,1,corpus[c]]/(mu_corpus_level[1,corpus[c]])
  258. );
  259. ll[2] += gamma_lpdf(
  260. mu_child_level[c,2] | alpha_corpus_level[1,2,corpus[c]], alpha_corpus_level[1,2,corpus[c]]/(mu_corpus_level[2,corpus[c]])
  261. );
  262. ll[2] += gamma_lpdf(
  263. mu_child_level[c,3] | alpha_corpus_level[1,3,corpus[c]], alpha_corpus_level[1,3,corpus[c]]/(mu_corpus_level[3,corpus[c]])
  264. );
  265. target += log_sum_exp(ll);
  266. }
  267. }
  268. alpha_child_level ~ gamma(4,1);
  269. target += reduce_sum(
  270. confusion_model_lpmf, group, n_clips%/%(threads*4),
  271. n_classes,
  272. algo_total, truth_total, clip_duration, clip_age,
  273. lambda//, lambda_fp
  274. );
  275. //mus_fp ~ exponential(1);
  276. //alphas_fp ~ gamma(2, 1);
  277. for (i in 1:n_classes) {
  278. //lambda_fp[:,i] ~ gamma(alphas_fp[i], alphas_fp[i]/mus_fp[i]);
  279. for (j in 1:n_classes) {
  280. mus[i,j] ~ exponential(1);
  281. alphas[i,j] ~ exponential(0.1);
  282. // mus[i,j] ~ exponential(1);
  283. // alphas[i,j] ~ exponential(1);
  284. for (c in 1:n_groups) {
  285. lambda[c,i,j] ~ gamma(alphas[i,j], alphas[i,j]/mus[i,j]);
  286. }
  287. }
  288. }
  289. // speech rates
  290. mu_pop_level ~ exponential(4); // 250 vocs/hour
  291. alpha_pop_level ~ gamma(16, 4); // dispersion of corpora within population sd = 0.5 x \mu
  292. alpha_pop ~ gamma(16, 4); // dispersion of dispersion of individuals within corpora
  293. for (i in 1:n_classes-1) {
  294. alpha_corpus_level[1,i,:] ~ gamma(8, 8/alpha_pop[i]);
  295. alpha_corpus_level[2,i,:] ~ gamma(8, 8/alpha_pop[i]);
  296. mu_corpus_level[i,:] ~ gamma(alpha_pop_level[i],alpha_pop_level[i]/mu_pop_level[i+1]);
  297. }
  298. for (g in 1:n_rates) {
  299. real chi_mu = mu_pop_level[1]*exp(
  300. child_dev_speech_age[speech_rate_child[g]]*speech_rate_age[g]/12.0/10.0 + beta_dev*(speech_rate_child_level[speech_rate_child[g],2]+speech_rate_child_level[speech_rate_child[g],3]-mu_pop_level[3]-mu_pop_level[4])*speech_rate_age[g]/12.0/10.0
  301. );
  302. speech_rate[1,g] ~ gamma(
  303. alpha_child_level[1],
  304. alpha_child_level[1]/chi_mu
  305. );
  306. speech_rate[2:,g] ~ gamma(
  307. alpha_child_level[2:],
  308. (alpha_child_level[2:]./(speech_rate_child_level[speech_rate_child[g],:]')) //'
  309. );
  310. speech_rates[g,:] ~ poisson(speech_rate[:,g]*durations[g]*1000);
  311. }
  312. for (c in 1:n_speech_rate_children) {
  313. distrib = child_siblings[c]>0?2:1;
  314. speech_rate_child_level[c,1] ~ gamma(
  315. alpha_corpus_level[distrib,1,speech_rate_child_corpus[c]],
  316. (alpha_corpus_level[distrib,1,speech_rate_child_corpus[c]]/(mu_corpus_level[1,speech_rate_child_corpus[c]]*exp(
  317. speech_rate_child_siblings[c]>0?beta_sib[1]:0
  318. )))
  319. );
  320. speech_rate_child_level[c,2:] ~ gamma(
  321. alpha_corpus_level[distrib,2:,speech_rate_child_corpus[c]],
  322. (alpha_corpus_level[distrib,2:,speech_rate_child_corpus[c]]./(mu_corpus_level[2:,speech_rate_child_corpus[c]].*exp(
  323. speech_rate_child_siblings[c]>0?beta_sib[2:]:rep_vector(0,2)
  324. )))
  325. );
  326. }
  327. child_dev_age ~ normal(alpha_dev, sigma_dev);
  328. child_dev_speech_age ~ normal(alpha_dev, sigma_dev);
  329. has_siblings ~ binomial(has_siblings+no_siblings, p_sib);
  330. p_sib ~ uniform(0, 1);
  331. beta_sib ~ normal(0, 1);
  332. alpha_dev ~ normal(0, 1);
  333. sigma_dev ~ exponential(1);
  334. beta_dev ~ normal(0, 1);
  335. }