dev_siblings_effect_sil.stan 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. 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. real beta_sib_och; // effect of n of siblings on OCH speech
  199. real beta_sib_adu; // effect of n of siblings on ADU speech
  200. real<lower=0,upper=1> p_sib; // prob of having siblings
  201. vector [n_speech_rate_children] child_dev_speech_age;
  202. // average effect of age
  203. real alpha_dev;
  204. real<lower=0> sigma_dev;
  205. // effect of excess ADU input
  206. real beta_dev;
  207. }
  208. model {
  209. //actual model
  210. // inverse confusion model
  211. target += reduce_sum(
  212. inverse_model_lpmf, children, 1,
  213. n_recs, n_classes, recs_duration,
  214. vocs, age,
  215. truth_vocs, actual_confusion_baseline, actual_fp_rate, mus, alphas, mus_fp, alphas_fp
  216. );
  217. // priors on actual speech
  218. target += reduce_sum(
  219. recs_priors_lpmf, children, 1,
  220. n_recs, n_classes, recs_duration, age,
  221. truth_vocs,
  222. mu_pop_level, mu_child_level, alpha_child_level,
  223. child_dev_age, beta_dev
  224. );
  225. vector [2] ll;
  226. for (c in 1:n_children) {
  227. // if there is sibling data
  228. if (child_siblings[c]>=0) {
  229. mu_child_level[c,1] ~ gamma(
  230. alpha_corpus_level[1,corpus[c]],
  231. (alpha_corpus_level[1,corpus[c]]/(mu_corpus_level[1,corpus[c]]*exp(
  232. child_siblings[c]>0?beta_sib_och:0
  233. )))
  234. );
  235. mu_child_level[c,2:] ~ gamma(
  236. alpha_corpus_level[2:,corpus[c]],
  237. (alpha_corpus_level[2:,corpus[c]]./mu_corpus_level[2:,corpus[c]]*exp(
  238. child_siblings[c]>0?beta_sib_adu:0
  239. ))
  240. );
  241. }
  242. // otherwise
  243. else {
  244. ll[1] = log(p_sib)+gamma_lpdf(
  245. mu_child_level[c,1] | alpha_corpus_level[1,corpus[c]], alpha_corpus_level[1,corpus[c]]/(mu_corpus_level[1,corpus[c]]*exp(beta_sib_och))
  246. );
  247. ll[1] += gamma_lpdf(
  248. mu_child_level[c,2] | alpha_corpus_level[2,corpus[c]], alpha_corpus_level[2,corpus[c]]/(mu_corpus_level[2,corpus[c]]*exp(beta_sib_adu))
  249. );
  250. ll[1] += gamma_lpdf(
  251. mu_child_level[c,3] | alpha_corpus_level[3,corpus[c]], alpha_corpus_level[3,corpus[c]]/(mu_corpus_level[3,corpus[c]]*exp(beta_sib_adu))
  252. );
  253. ll[2] = log(1-p_sib)+gamma_lpdf(
  254. mu_child_level[c,1] | alpha_corpus_level[1,corpus[c]], alpha_corpus_level[1,corpus[c]]/(mu_corpus_level[1,corpus[c]])
  255. );
  256. ll[2] += gamma_lpdf(
  257. mu_child_level[c,2] | alpha_corpus_level[2,corpus[c]], alpha_corpus_level[2,corpus[c]]/(mu_corpus_level[2,corpus[c]])
  258. );
  259. ll[2] += gamma_lpdf(
  260. mu_child_level[c,3] | alpha_corpus_level[3,corpus[c]], alpha_corpus_level[3,corpus[c]]/(mu_corpus_level[3,corpus[c]])
  261. );
  262. target += log_sum_exp(ll);
  263. }
  264. }
  265. alpha_child_level ~ gamma(2,1);
  266. target += reduce_sum(
  267. confusion_model_lpmf, group, n_clips%/%(threads*4),
  268. n_classes,
  269. algo_total, truth_total, clip_duration, clip_age,
  270. lambda, lambda_fp
  271. );
  272. mus_fp ~ exponential(1);
  273. alphas_fp ~ exponential(1);
  274. for (i in 1:n_classes) {
  275. lambda_fp[:,i] ~ gamma(alphas_fp[i], alphas_fp[i]/mus_fp[i]);
  276. for (j in 1:n_classes) {
  277. // mus[i,j] ~ exponential(i==j?2:8);
  278. // alphas[i,j] ~ gamma(2,1);
  279. mus[i,j] ~ exponential(1);
  280. alphas[i,j] ~ exponential(1);
  281. for (c in 1:n_groups) {
  282. lambda[c,i,j] ~ gamma(alphas[i,j], alphas[i,j]/mus[i,j]);
  283. }
  284. }
  285. }
  286. // speech rates
  287. mu_pop_level ~ exponential(4); // 250 vocs/hour
  288. alpha_pop_level ~ gamma(8, 4); // sd = 0.35 x \mu
  289. alpha_pop ~ gamma(10, 10);
  290. for (i in 1:n_classes-1) {
  291. alpha_corpus_level[i,:] ~ gamma(4, 4/alpha_pop[i]);
  292. mu_corpus_level[i,:] ~ gamma(alpha_pop_level[i],alpha_pop_level[i]/mu_pop_level[i+1]);
  293. }
  294. for (g in 1:n_rates) {
  295. real chi_mu = mu_pop_level[1]*exp(
  296. 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
  297. );
  298. speech_rate[1,g] ~ gamma(
  299. alpha_child_level[1],
  300. alpha_child_level[1]/chi_mu
  301. );
  302. speech_rate[2:,g] ~ gamma(
  303. alpha_child_level[2:],
  304. (alpha_child_level[2:]./(speech_rate_child_level[speech_rate_child[g],:]')) //'
  305. );
  306. speech_rates[g,:] ~ poisson(speech_rate[:,g]*durations[g]*1000);
  307. }
  308. for (c in 1:n_speech_rate_children) {
  309. speech_rate_child_level[c,1] ~ gamma(
  310. alpha_corpus_level[1,speech_rate_child_corpus[c]],
  311. (alpha_corpus_level[1,speech_rate_child_corpus[c]]/(mu_corpus_level[1,speech_rate_child_corpus[c]]*exp(
  312. speech_rate_child_siblings[c]>0?beta_sib_och:0
  313. )))
  314. );
  315. speech_rate_child_level[c,2:] ~ gamma(
  316. alpha_corpus_level[2:,speech_rate_child_corpus[c]],
  317. (alpha_corpus_level[2:,speech_rate_child_corpus[c]]./(mu_corpus_level[2:,speech_rate_child_corpus[c]]*exp(
  318. speech_rate_child_siblings[c]>0?beta_sib_adu:0
  319. )))
  320. );
  321. }
  322. child_dev_age ~ normal(alpha_dev, sigma_dev);
  323. child_dev_speech_age ~ normal(alpha_dev, sigma_dev);
  324. has_siblings ~ binomial(has_siblings+no_siblings, p_sib);
  325. p_sib ~ uniform(0, 1);
  326. beta_sib_och ~ normal(0, 1);
  327. beta_sib_adu ~ normal(0, 1);
  328. alpha_dev ~ normal(0, 1);
  329. sigma_dev ~ exponential(1);
  330. beta_dev ~ normal(0, 1);
  331. }