{"action":"created","comment":{"url":"https://api.github.com/repos/stan-dev/example-models/pulls/comments/55128378","id":55128378,"diff_hunk":"@@ -0,0 +1,228 @@\n+// Integrated population model\n+\n+functions {\n+  /**\n+   * Return log probability of Poisson distribution.\n+   * Outcome n may be a real value; for compatibility with OpenBUGS.\n+   *\n+   * @param n      Outcome\n+   * @theta lambda Mean\n+   *\n+   * @return Log probability\n+   */\n+  real real_poisson_log(real n, real lambda) {\n+    real lp;\n+\n+    if (lambda < 0) {\n+      reject(\"lambda must be non-negative; found lambda=\", lambda);\n+    } else if (n < 0.0) {\n+      reject(\"n must not be negative; found n=\", n);\n+    } else {\n+      return n * log(lambda) - lambda - lgamma(n + 1);\n+    }\n+    return negative_infinity();\n+  }\n+\n+  /**\n+   * Return log probability of binomial distribution.\n+   * Outcome n may be a real value; for compatibility with OpenBUGS.\n+   *\n+   * @param n     Outcome\n+   * @param N     Size\n+   * @theta theta Probability\n+   *\n+   * @return Log probability\n+   */\n+  real real_binomial_log(real n, real N, real theta) {\n+    real lp;\n+\n+    if (N < 0) {\n+      reject(\"N must be non-negative; found N=\", N);\n+    } else if (theta < 0 || theta > 1) {\n+      reject(\"theta must be in [0,1]; found theta=\", theta);\n+    } else if (n < 0 || n > N) {\n+      reject(\"n must be in [0:N]; found n=\", n);\n+    } else {\n+      return binomial_coefficient_log(N, n)\n+        + n * log(theta) + (N - n) * log(1.0 - theta);\n+    }\n+    return negative_infinity();\n+  }\n+}\n+\n+data {\n+  int nyears;                    // Number of years\n+  vector[nyears] y;              // Population counts\n+  int J[nyears-1];               // Total number of nestings recorded\n+  int R[nyears-1];               // Annual number of surveyed broods\n+  int m[2*(nyears-1), nyears];   // m-array\n+  int<lower=1> t_pred;           // Number of years to predict\n+}\n+\n+parameters {\n+  real<lower=0> sigma_y;           // Observation error\n+  vector<lower=0>[nyears] N1;      // Number of 1-year juveniles\n+  vector<lower=0>[nyears] Nad;     // Number of adults\n+  real<lower=0,upper=1> mean_sjuv; // Mean survival prob. juveniles\n+  real<lower=0,upper=1> mean_sad;  // Mean survival prob. adults\n+  real<lower=0,upper=1> mean_p;    // Mean recapture prob.\n+  real<lower=0> mean_fec;          // Mean productivity\n+}\n+\n+transformed parameters {\n+  vector<lower=0,upper=1>[nyears] sjuv;\n+  vector<lower=0,upper=1>[nyears] sad;\n+  vector<lower=0,upper=1>[nyears] p;\n+  vector<lower=0>[nyears] f;\n+  vector<lower=0>[nyears] Ntot;\n+  simplex[nyears] pr[2*(nyears-1)];\n+  vector<lower=0>[nyears-1] rho;\n+\n+  // Survival and recapture probabilities, as well as productivity\n+  for (t in 1:nyears) {\n+    sjuv[t] <- mean_sjuv;\n+    sad[t] <- mean_sad;\n+    p[t] <- mean_p;\n+    f[t] <- mean_fec;\n+  }\n+\n+  // Total number of individuals\n+  for (t in 1:nyears)\n+    Ntot[t] <- Nad[t] + N1[t];\n+\n+  { // m-array\n+      vector[nyears] q;\n+\n+      q <- 1.0 - p;\n+\n+      // m-array cell probabilities for juveniles\n+      for (t in 1:(nyears - 1)) {\n+        // Main diagonal\n+        pr[t, t] <- sjuv[t] * p[t];\n+\n+        // Above main diagonal\n+        for (j in (t + 1):(nyears - 1))\n+          pr[t, j] <- sjuv[t] * prod(sad[(t + 1):j])\n+            * prod(q[t:(j - 1)]) * p[j];\n+\n+        // Below main diagonal\n+        for (j in 1:(t - 1))\n+          pr[t, j] <- 0.0;\n+\n+        // Last column: probability of non-recapture\n+        pr[t,nyears] <- 1.0 - sum(pr[t, 1:(nyears - 1)]);\n+      } //t\n+\n+      // m-array cell probabilities for adults\n+      for (t in 1:(nyears - 1)) {\n+\n+        // Main diagonal\n+        pr[t + nyears - 1, t] <- sad[t] * p[t];\n+        // Above main diagonal\n+        for (j in (t + 1):(nyears - 1))\n+          pr[t + nyears - 1, j] <- prod(sad[t:j])\n+            * prod(q[t:(j - 1)]) * p[j];\n+\n+        // Below main diagonal\n+        for (j in 1:(t - 1))\n+          pr[t + nyears - 1, j] <- 0.0;\n+\n+        // Last column\n+        pr[t + nyears - 1, nyears] <- 1.0\n+          - sum(pr[t + nyears - 1, 1:(nyears - 1)]);\n+      } //t\n+  }\n+\n+  // Productivity\n+  for (t in 1:(nyears - 1))\n+    rho[t] <- R[t] * f[t];\n+}\n+\n+model {\n+  // Priors\n+  // Initial population sizes\n+  // Constraints ensure truncated normal (> 0)\n+  N1[1] ~ normal(100, 100);\n+  Nad[1] ~ normal(100, 100);\n+\n+  mean_sjuv ~ uniform(0, 1);\n+  mean_sad ~ uniform(0, 1);\n+  mean_p ~ uniform(0, 1);\n+  // Improper flat priors are implicitly used on sigma_y and mean_fec.","path":"BPA/Ch.11/ipm_pred.stan","position":null,"original_position":151,"commit_id":"e8b6c3ff0c8d1a1760d42645911e9e14c88483f8","original_commit_id":"a6a9f18719f949fe99263f1c4a2438e5f183e1b1","user":{"login":"ito4303","id":13703977,"avatar_url":"https://avatars.githubusercontent.com/u/13703977?v=3","gravatar_id":"","url":"https://api.github.com/users/ito4303","html_url":"https://github.com/ito4303","followers_url":"https://api.github.com/users/ito4303/followers","following_url":"https://api.github.com/users/ito4303/following{/other_user}","gists_url":"https://api.github.com/users/ito4303/gists{/gist_id}","starred_url":"https://api.github.com/users/ito4303/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ito4303/subscriptions","organizations_url":"https://api.github.com/users/ito4303/orgs","repos_url":"https://api.github.com/users/ito4303/repos","events_url":"https://api.github.com/users/ito4303/events{/privacy}","received_events_url":"https://api.github.com/users/ito4303/received_events","type":"User","site_admin":false},"body":"Thank you for the comments. Implicit prior declarations are now used for each model in this chapter if possible.","created_at":"2016-03-05T21:34:04Z","updated_at":"2016-03-05T21:34:04Z","html_url":"https://github.com/stan-dev/example-models/pull/49#discussion_r55128378","pull_request_url":"https://api.github.com/repos/stan-dev/example-models/pulls/49","_links":{"self":{"href":"https://api.github.com/repos/stan-dev/example-models/pulls/comments/55128378"},"html":{"href":"https://github.com/stan-dev/example-models/pull/49#discussion_r55128378"},"pull_request":{"href":"https://api.github.com/repos/stan-dev/example-models/pulls/49"}}},"pull_request":{"url":"https://api.github.com/repos/stan-dev/example-models/pulls/49","id":61534359,"html_url":"https://github.com/stan-dev/example-models/pull/49","diff_url":"https://github.com/stan-dev/example-models/pull/49.diff","patch_url":"https://github.com/stan-dev/example-models/pull/49.patch","issue_url":"https://api.github.com/repos/stan-dev/example-models/issues/49","number":49,"state":"open","locked":false,"title":"BPA: Add Chapter 11","user":{"login":"ito4303","id":13703977,"avatar_url":"https://avatars.githubusercontent.com/u/13703977?v=3","gravatar_id":"","url":"https://api.github.com/users/ito4303","html_url":"https://github.com/ito4303","followers_url":"https://api.github.com/users/ito4303/followers","following_url":"https://api.github.com/users/ito4303/following{/other_user}","gists_url":"https://api.github.com/users/ito4303/gists{/gist_id}","starred_url":"https://api.github.com/users/ito4303/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ito4303/subscriptions","organizations_url":"https://api.github.com/users/ito4303/orgs","repos_url":"https://api.github.com/users/ito4303/repos","events_url":"https://api.github.com/users/ito4303/events{/privacy}","received_events_url":"https://api.github.com/users/ito4303/received_events","type":"User","site_admin":false},"body":"I translated models in Chapter 11 of BPA. Would you please review the code?","created_at":"2016-03-03T09:55:05Z","updated_at":"2016-03-05T21:34:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4f0fee7f56f638a0a6c0c208db44af5011ba475c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/stan-dev/example-models/pulls/49/commits","review_comments_url":"https://api.github.com/repos/stan-dev/example-models/pulls/49/comments","review_comment_url":"https://api.github.com/repos/stan-dev/example-models/pulls/comments{/number}","comments_url":"https://api.github.com/repos/stan-dev/example-models/issues/49/comments","statuses_url":"https://api.github.com/repos/stan-dev/example-models/statuses/e8b6c3ff0c8d1a1760d42645911e9e14c88483f8","head":{"label":"ito4303:BPA","ref":"BPA","sha":"e8b6c3ff0c8d1a1760d42645911e9e14c88483f8","user":{"login":"ito4303","id":13703977,"avatar_url":"https://avatars.githubusercontent.com/u/13703977?v=3","gravatar_id":"","url":"https://api.github.com/users/ito4303","html_url":"https://github.com/ito4303","followers_url":"https://api.github.com/users/ito4303/followers","following_url":"https://api.github.com/users/ito4303/following{/other_user}","gists_url":"https://api.github.com/users/ito4303/gists{/gist_id}","starred_url":"https://api.github.com/users/ito4303/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ito4303/subscriptions","organizations_url":"https://api.github.com/users/ito4303/orgs","repos_url":"https://api.github.com/users/ito4303/repos","events_url":"https://api.github.com/users/ito4303/events{/privacy}","received_events_url":"https://api.github.com/users/ito4303/received_events","type":"User","site_admin":false},"repo":{"id":47541215,"name":"example-models","full_name":"ito4303/example-models","owner":{"login":"ito4303","id":13703977,"avatar_url":"https://avatars.githubusercontent.com/u/13703977?v=3","gravatar_id":"","url":"https://api.github.com/users/ito4303","html_url":"https://github.com/ito4303","followers_url":"https://api.github.com/users/ito4303/followers","following_url":"https://api.github.com/users/ito4303/following{/other_user}","gists_url":"https://api.github.com/users/ito4303/gists{/gist_id}","starred_url":"https://api.github.com/users/ito4303/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ito4303/subscriptions","organizations_url":"https://api.github.com/users/ito4303/orgs","repos_url":"https://api.github.com/users/ito4303/repos","events_url":"https://api.github.com/users/ito4303/events{/privacy}","received_events_url":"https://api.github.com/users/ito4303/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ito4303/example-models","description":"","fork":true,"url":"https://api.github.com/repos/ito4303/example-models","forks_url":"https://api.github.com/repos/ito4303/example-models/forks","keys_url":"https://api.github.com/repos/ito4303/example-models/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ito4303/example-models/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ito4303/example-models/teams","hooks_url":"https://api.github.com/repos/ito4303/example-models/hooks","issue_events_url":"https://api.github.com/repos/ito4303/example-models/issues/events{/number}","events_url":"https://api.github.com/repos/ito4303/example-models/events","assignees_url":"https://api.github.com/repos/ito4303/example-models/assignees{/user}","branches_url":"https://api.github.com/repos/ito4303/example-models/branches{/branch}","tags_url":"https://api.github.com/repos/ito4303/example-models/tags","blobs_url":"https://api.github.com/repos/ito4303/example-models/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ito4303/example-models/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ito4303/example-models/git/refs{/sha}","trees_url":"https://api.github.com/repos/ito4303/example-models/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ito4303/example-models/statuses/{sha}","languages_url":"https://api.github.com/repos/ito4303/example-models/languages","stargazers_url":"https://api.github.com/repos/ito4303/example-models/stargazers","contributors_url":"https://api.github.com/repos/ito4303/example-models/contributors","subscribers_url":"https://api.github.com/repos/ito4303/example-models/subscribers","subscription_url":"https://api.github.com/repos/ito4303/example-models/subscription","commits_url":"https://api.github.com/repos/ito4303/example-models/commits{/sha}","git_commits_url":"https://api.github.com/repos/ito4303/example-models/git/commits{/sha}","comments_url":"https://api.github.com/repos/ito4303/example-models/comments{/number}","issue_comment_url":"https://api.github.com/repos/ito4303/example-models/issues/comments{/number}","contents_url":"https://api.github.com/repos/ito4303/example-models/contents/{+path}","compare_url":"https://api.github.com/repos/ito4303/example-models/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ito4303/example-models/merges","archive_url":"https://api.github.com/repos/ito4303/example-models/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ito4303/example-models/downloads","issues_url":"https://api.github.com/repos/ito4303/example-models/issues{/number}","pulls_url":"https://api.github.com/repos/ito4303/example-models/pulls{/number}","milestones_url":"https://api.github.com/repos/ito4303/example-models/milestones{/number}","notifications_url":"https://api.github.com/repos/ito4303/example-models/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ito4303/example-models/labels{/name}","releases_url":"https://api.github.com/repos/ito4303/example-models/releases{/id}","deployments_url":"https://api.github.com/repos/ito4303/example-models/deployments","created_at":"2015-12-07T09:06:25Z","updated_at":"2016-01-22T11:09:41Z","pushed_at":"2016-03-05T21:22:40Z","git_url":"git://github.com/ito4303/example-models.git","ssh_url":"git@github.com:ito4303/example-models.git","clone_url":"https://github.com/ito4303/example-models.git","svn_url":"https://github.com/ito4303/example-models","homepage":null,"size":62923,"stargazers_count":0,"watchers_count":0,"language":"R","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"stan-dev:master","ref":"master","sha":"28eabd2de4e548d73546f854c0ec6c1bea0309fa","user":{"login":"stan-dev","id":3374820,"avatar_url":"https://avatars.githubusercontent.com/u/3374820?v=3","gravatar_id":"","url":"https://api.github.com/users/stan-dev","html_url":"https://github.com/stan-dev","followers_url":"https://api.github.com/users/stan-dev/followers","following_url":"https://api.github.com/users/stan-dev/following{/other_user}","gists_url":"https://api.github.com/users/stan-dev/gists{/gist_id}","starred_url":"https://api.github.com/users/stan-dev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stan-dev/subscriptions","organizations_url":"https://api.github.com/users/stan-dev/orgs","repos_url":"https://api.github.com/users/stan-dev/repos","events_url":"https://api.github.com/users/stan-dev/events{/privacy}","received_events_url":"https://api.github.com/users/stan-dev/received_events","type":"Organization","site_admin":false},"repo":{"id":22768481,"name":"example-models","full_name":"stan-dev/example-models","owner":{"login":"stan-dev","id":3374820,"avatar_url":"https://avatars.githubusercontent.com/u/3374820?v=3","gravatar_id":"","url":"https://api.github.com/users/stan-dev","html_url":"https://github.com/stan-dev","followers_url":"https://api.github.com/users/stan-dev/followers","following_url":"https://api.github.com/users/stan-dev/following{/other_user}","gists_url":"https://api.github.com/users/stan-dev/gists{/gist_id}","starred_url":"https://api.github.com/users/stan-dev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stan-dev/subscriptions","organizations_url":"https://api.github.com/users/stan-dev/orgs","repos_url":"https://api.github.com/users/stan-dev/repos","events_url":"https://api.github.com/users/stan-dev/events{/privacy}","received_events_url":"https://api.github.com/users/stan-dev/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/stan-dev/example-models","description":"Example models for Stan","fork":false,"url":"https://api.github.com/repos/stan-dev/example-models","forks_url":"https://api.github.com/repos/stan-dev/example-models/forks","keys_url":"https://api.github.com/repos/stan-dev/example-models/keys{/key_id}","collaborators_url":"https://api.github.com/repos/stan-dev/example-models/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/stan-dev/example-models/teams","hooks_url":"https://api.github.com/repos/stan-dev/example-models/hooks","issue_events_url":"https://api.github.com/repos/stan-dev/example-models/issues/events{/number}","events_url":"https://api.github.com/repos/stan-dev/example-models/events","assignees_url":"https://api.github.com/repos/stan-dev/example-models/assignees{/user}","branches_url":"https://api.github.com/repos/stan-dev/example-models/branches{/branch}","tags_url":"https://api.github.com/repos/stan-dev/example-models/tags","blobs_url":"https://api.github.com/repos/stan-dev/example-models/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/stan-dev/example-models/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/stan-dev/example-models/git/refs{/sha}","trees_url":"https://api.github.com/repos/stan-dev/example-models/git/trees{/sha}","statuses_url":"https://api.github.com/repos/stan-dev/example-models/statuses/{sha}","languages_url":"https://api.github.com/repos/stan-dev/example-models/languages","stargazers_url":"https://api.github.com/repos/stan-dev/example-models/stargazers","contributors_url":"https://api.github.com/repos/stan-dev/example-models/contributors","subscribers_url":"https://api.github.com/repos/stan-dev/example-models/subscribers","subscription_url":"https://api.github.com/repos/stan-dev/example-models/subscription","commits_url":"https://api.github.com/repos/stan-dev/example-models/commits{/sha}","git_commits_url":"https://api.github.com/repos/stan-dev/example-models/git/commits{/sha}","comments_url":"https://api.github.com/repos/stan-dev/example-models/comments{/number}","issue_comment_url":"https://api.github.com/repos/stan-dev/example-models/issues/comments{/number}","contents_url":"https://api.github.com/repos/stan-dev/example-models/contents/{+path}","compare_url":"https://api.github.com/repos/stan-dev/example-models/compare/{base}...{head}","merges_url":"https://api.github.com/repos/stan-dev/example-models/merges","archive_url":"https://api.github.com/repos/stan-dev/example-models/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/stan-dev/example-models/downloads","issues_url":"https://api.github.com/repos/stan-dev/example-models/issues{/number}","pulls_url":"https://api.github.com/repos/stan-dev/example-models/pulls{/number}","milestones_url":"https://api.github.com/repos/stan-dev/example-models/milestones{/number}","notifications_url":"https://api.github.com/repos/stan-dev/example-models/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/stan-dev/example-models/labels{/name}","releases_url":"https://api.github.com/repos/stan-dev/example-models/releases{/id}","deployments_url":"https://api.github.com/repos/stan-dev/example-models/deployments","created_at":"2014-08-08T19:03:11Z","updated_at":"2016-03-04T13:25:26Z","pushed_at":"2016-03-05T21:22:41Z","git_url":"git://github.com/stan-dev/example-models.git","ssh_url":"git@github.com:stan-dev/example-models.git","clone_url":"https://github.com/stan-dev/example-models.git","svn_url":"https://github.com/stan-dev/example-models","homepage":"http://mc-stan.org/","size":65565,"stargazers_count":150,"watchers_count":150,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":95,"mirror_url":null,"open_issues_count":15,"forks":95,"open_issues":15,"watchers":150,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/stan-dev/example-models/pulls/49"},"html":{"href":"https://github.com/stan-dev/example-models/pull/49"},"issue":{"href":"https://api.github.com/repos/stan-dev/example-models/issues/49"},"comments":{"href":"https://api.github.com/repos/stan-dev/example-models/issues/49/comments"},"review_comments":{"href":"https://api.github.com/repos/stan-dev/example-models/pulls/49/comments"},"review_comment":{"href":"https://api.github.com/repos/stan-dev/example-models/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/stan-dev/example-models/pulls/49/commits"},"statuses":{"href":"https://api.github.com/repos/stan-dev/example-models/statuses/e8b6c3ff0c8d1a1760d42645911e9e14c88483f8"}}}}