dev_siblings_combined.stan 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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=0> age;
  117. array[n_recs] int<lower=-1> siblings;
  118. array[n_recs, n_classes] int<lower=0> vocs_algo1;
  119. array[n_recs, n_classes] int<lower=0> vocs_algo2;
  120. array[n_children] int<lower=1> corpus;
  121. real<lower=0> recs_duration;
  122. // speaker confusion data block
  123. int<lower=1> n_clips; // number of clips
  124. int<lower=1> n_groups; // number of groups
  125. int<lower=1> n_corpora;
  126. array [n_clips] int group;
  127. array [n_clips] int conf_corpus;
  128. array [n_clips,n_classes] int<lower=0> algo1_total; // algo vocs attributed to specific speakers
  129. array [n_clips,n_classes] int<lower=0> algo2_total; // algo vocs attributed to specific speakers
  130. array [n_clips,n_classes] int<lower=0> truth_total;
  131. array [n_clips] real<lower=0> clip_duration;
  132. array [n_clips] real<lower=0> clip_age;
  133. int<lower=0> n_validation;
  134. // actual speech rates
  135. int<lower=1> n_rates;
  136. int<lower=1> n_speech_rate_children;
  137. array [n_rates,n_classes] int<lower=0> speech_rates;
  138. array [n_rates] int group_corpus;
  139. array [n_rates] real<lower=0> durations;
  140. array [n_rates] real<lower=0> speech_rate_age;
  141. array [n_rates] int<lower=-1> speech_rate_siblings;
  142. array [n_rates] int<lower=1,upper=n_speech_rate_children> speech_rate_child;
  143. // parallel processing
  144. int<lower=1> threads;
  145. }
  146. transformed data {
  147. vector<lower=0>[n_groups] recording_age;
  148. array[n_speech_rate_children] int<lower=1> speech_rate_child_corpus;
  149. array[n_children] int<lower=-1> child_siblings;
  150. array[n_speech_rate_children] int<lower=-1> speech_rate_child_siblings;
  151. int no_siblings = 0;
  152. int has_siblings = 0;
  153. for (c in 1:n_clips) {
  154. recording_age[group[c]] = clip_age[c];
  155. }
  156. for (k in 1:n_rates) {
  157. speech_rate_child_corpus[speech_rate_child[k]] = group_corpus[k];
  158. }
  159. for (k in 1:n_recs) {
  160. child_siblings[children[k]] = siblings[k];
  161. }
  162. for (c in 1:n_children) {
  163. if (child_siblings[c] == 0) {
  164. no_siblings += 1;
  165. }
  166. else if (child_siblings[c] > 0) {
  167. has_siblings += 1;
  168. }
  169. }
  170. for (k in 1:n_rates) {
  171. speech_rate_child_siblings[speech_rate_child[k]] = speech_rate_siblings[k];
  172. }
  173. }
  174. parameters {
  175. matrix<lower=0>[n_children,n_classes-1] mu_child_level;
  176. vector [n_children] child_dev_age;
  177. matrix<lower=0> [n_recs, n_classes] truth_vocs;
  178. // nuisance parameters
  179. array [n_recs] matrix<lower=0>[n_classes,n_classes] actual_confusion_baseline_algo1;
  180. array [n_recs] matrix<lower=0>[n_classes,n_classes] actual_confusion_baseline_algo2;
  181. //array [n_recs] vector<lower=0>[n_classes] actual_fp_rate;
  182. // confusion parameters
  183. // confusion matrix
  184. matrix<lower=0>[n_classes,n_classes] alphas_algo1;
  185. matrix<lower=0>[n_classes,n_classes] mus_algo1;
  186. array [n_groups] matrix<lower=0>[n_classes,n_classes] lambda_algo1;
  187. matrix<lower=0>[n_classes,n_classes] alphas_algo2;
  188. matrix<lower=0>[n_classes,n_classes] mus_algo2;
  189. array [n_groups] matrix<lower=0>[n_classes,n_classes] lambda_algo2;
  190. // false positives
  191. //vector<lower=0>[n_classes] alphas_fp;
  192. //vector<lower=0>[n_classes] mus_fp;
  193. //array [n_groups] vector<lower=0>[n_classes] lambda_fp;
  194. // speech rates
  195. vector<lower=0>[n_classes] alpha_child_level; // variance across recordings for a given child
  196. array[2] matrix<lower=0>[n_classes-1,n_corpora] alpha_corpus_level; // variance among children
  197. matrix<lower=0>[n_classes-1,n_corpora] mu_corpus_level; // child-level average
  198. vector<lower=0>[n_classes-1] alpha_pop_level; // variance among corpora
  199. vector<lower=0>[n_classes] mu_pop_level; // population level averages
  200. vector<lower=0>[n_classes-1] alpha_pop;
  201. matrix<lower=0>[n_classes,n_rates] speech_rate; // truth speech rates observed in annotated clips
  202. matrix<lower=0>[n_speech_rate_children,n_classes-1] speech_rate_child_level; // expected speech rate at the child-level
  203. // siblings
  204. real beta_sib_och; // effect of having siblings on OCH speech
  205. real beta_sib_adu; // effect of having siblings on ADU speech
  206. real<lower=0,upper=1> p_sib; // prob of having siblings
  207. vector [n_speech_rate_children] child_dev_speech_age;
  208. // average effect of age
  209. real alpha_dev;
  210. real<lower=0> sigma_dev;
  211. // effect of excess ADU input
  212. real beta_dev;
  213. }
  214. model {
  215. //actual model
  216. // inverse confusion model
  217. target += reduce_sum(
  218. inverse_model_lpmf, children, 1,
  219. n_recs, n_classes, recs_duration,
  220. vocs_algo1, age,
  221. truth_vocs, actual_confusion_baseline_algo1, mus_algo1, alphas_algo1//, mus_fp, alphas_fp
  222. );
  223. target += reduce_sum(
  224. inverse_model_lpmf, children, 1,
  225. n_recs, n_classes, recs_duration,
  226. vocs_algo2, age,
  227. truth_vocs, actual_confusion_baseline_algo2, mus_algo2, alphas_algo2//, mus_fp, alphas_fp
  228. );
  229. // priors on actual speech
  230. target += reduce_sum(
  231. recs_priors_lpmf, children, 1,
  232. n_recs, n_classes, recs_duration, age,
  233. truth_vocs,
  234. mu_pop_level, mu_child_level, alpha_child_level,
  235. child_dev_age, beta_dev
  236. );
  237. vector [2] ll;
  238. int distrib;
  239. for (c in 1:n_children) {
  240. // if there is sibling data
  241. if (child_siblings[c]>=0) {
  242. distrib = child_siblings[c]>0?2:1;
  243. mu_child_level[c,1] ~ gamma(
  244. alpha_corpus_level[distrib,1,corpus[c]],
  245. (alpha_corpus_level[distrib,1,corpus[c]]/(mu_corpus_level[1,corpus[c]]*exp(
  246. child_siblings[c]>0?beta_sib_och:0
  247. )))
  248. );
  249. mu_child_level[c,2:] ~ gamma(
  250. alpha_corpus_level[distrib,2:,corpus[c]],
  251. (alpha_corpus_level[distrib,2:,corpus[c]]./mu_corpus_level[2:,corpus[c]]*exp(
  252. child_siblings[c]>0?beta_sib_adu:0
  253. ))
  254. );
  255. }
  256. // otherwise
  257. else {
  258. // assuming no sibling
  259. ll[1] = log(p_sib)+gamma_lpdf(
  260. 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_och))
  261. );
  262. ll[1] += gamma_lpdf(
  263. 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_adu))
  264. );
  265. ll[1] += gamma_lpdf(
  266. 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_adu))
  267. );
  268. // assuming sibling
  269. ll[2] = log(1-p_sib)+gamma_lpdf(
  270. 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]])
  271. );
  272. ll[2] += gamma_lpdf(
  273. 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]])
  274. );
  275. ll[2] += gamma_lpdf(
  276. 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]])
  277. );
  278. target += log_sum_exp(ll);
  279. }
  280. }
  281. alpha_child_level ~ gamma(2,1);
  282. target += reduce_sum(
  283. confusion_model_lpmf, group, n_clips%/%(threads*4),
  284. n_classes,
  285. algo1_total, truth_total, clip_duration, clip_age,
  286. lambda_algo1//, lambda_fp
  287. );
  288. target += reduce_sum(
  289. confusion_model_lpmf, group, n_clips%/%(threads*4),
  290. n_classes,
  291. algo2_total, truth_total, clip_duration, clip_age,
  292. lambda_algo2//, lambda_fp
  293. );
  294. //mus_fp ~ exponential(1);
  295. //alphas_fp ~ gamma(2, 1);
  296. for (i in 1:n_classes) {
  297. //lambda_fp[:,i] ~ gamma(alphas_fp[i], alphas_fp[i]/mus_fp[i]);
  298. for (j in 1:n_classes) {
  299. mus_algo1[i,j] ~ exponential(i==j?2:8);
  300. mus_algo2[i,j] ~ exponential(i==j?2:8);
  301. alphas_algo1[i,j] ~ gamma(2,1);
  302. alphas_algo2[i,j] ~ gamma(2,1);
  303. // mus[i,j] ~ exponential(1);
  304. // alphas[i,j] ~ exponential(1);
  305. for (c in 1:n_groups) {
  306. lambda_algo1[c,i,j] ~ gamma(alphas_algo1[i,j], alphas_algo1[i,j]/mus_algo1[i,j]);
  307. lambda_algo2[c,i,j] ~ gamma(alphas_algo2[i,j], alphas_algo2[i,j]/mus_algo2[i,j]);
  308. }
  309. }
  310. }
  311. // speech rates
  312. mu_pop_level ~ exponential(4); // 250 vocs/hour
  313. alpha_pop_level ~ gamma(8, 4); // sd = 0.35 x \mu
  314. alpha_pop ~ gamma(10, 10);
  315. for (i in 1:n_classes-1) {
  316. alpha_corpus_level[1,i,:] ~ gamma(4, 4/alpha_pop[i]);
  317. alpha_corpus_level[2,i,:] ~ gamma(4, 4/alpha_pop[i]);
  318. mu_corpus_level[i,:] ~ gamma(alpha_pop_level[i],alpha_pop_level[i]/mu_pop_level[i+1]);
  319. }
  320. for (g in 1:n_rates) {
  321. real chi_mu = mu_pop_level[1]*exp(
  322. 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
  323. );
  324. speech_rate[1,g] ~ gamma(
  325. alpha_child_level[1],
  326. alpha_child_level[1]/chi_mu
  327. );
  328. speech_rate[2:,g] ~ gamma(
  329. alpha_child_level[2:],
  330. (alpha_child_level[2:]./(speech_rate_child_level[speech_rate_child[g],:]')) //'
  331. );
  332. speech_rates[g,:] ~ poisson(speech_rate[:,g]*durations[g]*1000);
  333. }
  334. for (c in 1:n_speech_rate_children) {
  335. distrib = child_siblings[c]>0?2:1;
  336. speech_rate_child_level[c,1] ~ gamma(
  337. alpha_corpus_level[distrib,1,speech_rate_child_corpus[c]],
  338. (alpha_corpus_level[distrib,1,speech_rate_child_corpus[c]]/(mu_corpus_level[1,speech_rate_child_corpus[c]]*exp(
  339. speech_rate_child_siblings[c]>0?beta_sib_och:0
  340. )))
  341. );
  342. speech_rate_child_level[c,2:] ~ gamma(
  343. alpha_corpus_level[distrib,2:,speech_rate_child_corpus[c]],
  344. (alpha_corpus_level[distrib,2:,speech_rate_child_corpus[c]]./(mu_corpus_level[2:,speech_rate_child_corpus[c]]*exp(
  345. speech_rate_child_siblings[c]>0?beta_sib_adu:0
  346. )))
  347. );
  348. }
  349. child_dev_age ~ normal(alpha_dev, sigma_dev);
  350. child_dev_speech_age ~ normal(alpha_dev, sigma_dev);
  351. has_siblings ~ binomial(has_siblings+no_siblings, p_sib);
  352. p_sib ~ uniform(0, 1);
  353. beta_sib_och ~ normal(0, 1);
  354. beta_sib_adu ~ normal(0, 1);
  355. alpha_dev ~ normal(0, 1);
  356. sigma_dev ~ exponential(1);
  357. beta_dev ~ normal(0, 1);
  358. }