{"action":"created","issue":{"url":"https://api.github.com/repos/greta-dev/greta/issues/97","repository_url":"https://api.github.com/repos/greta-dev/greta","labels_url":"https://api.github.com/repos/greta-dev/greta/issues/97/labels{/name}","comments_url":"https://api.github.com/repos/greta-dev/greta/issues/97/comments","events_url":"https://api.github.com/repos/greta-dev/greta/issues/97/events","html_url":"https://github.com/greta-dev/greta/issues/97","id":253045738,"number":97,"title":"Dynamic Factor Type Model Won't Compile?","user":{"login":"jwdink","id":6982295,"avatar_url":"https://avatars2.githubusercontent.com/u/6982295?v=4","gravatar_id":"","url":"https://api.github.com/users/jwdink","html_url":"https://github.com/jwdink","followers_url":"https://api.github.com/users/jwdink/followers","following_url":"https://api.github.com/users/jwdink/following{/other_user}","gists_url":"https://api.github.com/users/jwdink/gists{/gist_id}","starred_url":"https://api.github.com/users/jwdink/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwdink/subscriptions","organizations_url":"https://api.github.com/users/jwdink/orgs","repos_url":"https://api.github.com/users/jwdink/repos","events_url":"https://api.github.com/users/jwdink/events{/privacy}","received_events_url":"https://api.github.com/users/jwdink/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":22,"created_at":"2017-08-26T00:18:32Z","updated_at":"2017-08-28T09:14:27Z","closed_at":"2017-08-26T02:19:45Z","body":"Hopefully I'm not making some silly mistake here-- assuming that's not the case...\r\n\r\nIt seems like 'dynamic factor' style models (like this simplified kalman-filter below) won't compile: when I call `model`, R just hangs.\r\n\r\n```\r\nlibrary('greta')\r\ntimes <- 1:5\r\nY <- c(-3.12368290898237, -2.37268902597084, -3.31757075118491, -2.0882385972953, -2.23854863652599)\r\n\r\nsigma_measure <- lognormal(0, 1)\r\nsigma_delta <- lognormal(0, 1);\r\nsigma_position <- lognormal(0, 1);\r\n\r\nlatent_positions <- normal(-2, sigma_position)\r\nlatent_delta <- normal(0, sigma_delta)\r\nfor (t in unique(times)[-1]) {\r\n  # random position-walk:\r\n  latent_positions <- c(latent_positions, normal(latent_positions[t-1] + latent_delta, sigma_position) )\r\n}\r\n# observed position:\r\ndistribution(Y) <- normal(latent_positions, sigma_measure);\r\n\r\ncompiled_model <- model(latent_positions) # hangs\r\n```\r\n\r\nIn contrast, everything seems to go well in stan:\r\n\r\n```\r\ndata {\r\n  int nobs; // number of obs\r\n  matrix[nobs,1] Y; //dataset of generated series\r\n  real initial_y;\r\n}\r\nparameters {\r\n  vector[nobs] latent_position;\r\n  real<lower = 0> sigma_position;\r\n  vector[nobs] latent_delta;\r\n  real<lower = 0> sigma_delta;\r\n\r\n  real<lower = 0> sigma_measure;\r\n}\r\ntransformed parameters {\r\n}\r\nmodel {\r\n  // priors\r\n  latent_position[1] ~ normal(initial_y, 1);\r\n  latent_delta[1] ~ normal(0, 1);\r\n  Y[1,1] ~ normal(latent_position[1],sigma_measure);\r\n\r\n  sigma_position ~ lognormal(0, 1);\r\n  sigma_delta ~ lognormal(0, 1);\r\n  sigma_measure ~ lognormal(0, 1);\r\n\r\n  //\r\n  for(t in 2:nobs) {\r\n    latent_position[t] ~ normal(latent_position[t-1] + latent_delta[t-1], sigma_position);\r\n    latent_delta[t] ~ normal(latent_delta[t-1], sigma_delta);\r\n    Y[t,1] ~ normal(latent_position[t], sigma_measure);\r\n  }\r\n\r\n}\r\n//\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/greta-dev/greta/issues/comments/325303282","html_url":"https://github.com/greta-dev/greta/issues/97#issuecomment-325303282","issue_url":"https://api.github.com/repos/greta-dev/greta/issues/97","id":325303282,"user":{"login":"jwdink","id":6982295,"avatar_url":"https://avatars2.githubusercontent.com/u/6982295?v=4","gravatar_id":"","url":"https://api.github.com/users/jwdink","html_url":"https://github.com/jwdink","followers_url":"https://api.github.com/users/jwdink/followers","following_url":"https://api.github.com/users/jwdink/following{/other_user}","gists_url":"https://api.github.com/users/jwdink/gists{/gist_id}","starred_url":"https://api.github.com/users/jwdink/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwdink/subscriptions","organizations_url":"https://api.github.com/users/jwdink/orgs","repos_url":"https://api.github.com/users/jwdink/repos","events_url":"https://api.github.com/users/jwdink/events{/privacy}","received_events_url":"https://api.github.com/users/jwdink/received_events","type":"User","site_admin":false},"created_at":"2017-08-28T09:14:27Z","updated_at":"2017-08-28T09:14:27Z","body":"I apologize, I'd forgotten I'd posted such a small subset of the data (just to demonstrate the model-compilation issue).\r\n\r\nThe sampling issues come with the full dataset:\r\n\r\n```\r\nlibrary (greta)\r\nlibrary (tensorflow)\r\n\r\nop <- .internals$nodes$constructors$op\r\ncumsum.greta_array <- function (x) \r\n  op(\"cumsum\", x, tf_operation = tf$cumsum)\r\n\r\ntimes <- 1:92\r\nY <- c(-3.12, -2.37, -3.32, -2.09, -2.24, -1.7, -1.84, -2.24, -1.14, \r\n       -1.7, -1.22, -1.17, -1, -1.1, -1.32, -1.59, 0.73, -1.18, -0.58, \r\n       -1.01, 0.01, -1.28, -0.4, -0.15, -0.2, 0.69, -0.26, -1.13, 0.43, \r\n       0.62, 0.88, -0.25, -0.2, 0.61, 0.73, 0.99, -0.46, -1.23, -0.19, \r\n       -0.64, 0.1, -0.03, -0.72, -0.5, -0.2, -0.72, -0.18, -0.3, -0.16, \r\n       -1.17, -0.58, 0.81, -0.29, -0.52, -0.55, -0.25, 1.06, 0.09, 1.13, \r\n       0.72, 0.13, -0.36, 1.02, 0.74, 0.47, 0.6, -0.22, 1.25, 1.56, \r\n       0.01, 0.15, 1.28, -0.03, 0.32, 0.22, 0.86, 1.44, -0.5, 1.36, \r\n       0.77, 1.6, 0.16, 0.52, -0.52, 0.41, 1.77, 1.2, 0.86, 1.67, 1.2, \r\n       0.68, 1.95, 1.55, 1.96, 2.42, 1.83)\r\n#times <- times[1:60]; Y <- Y[1:60] # as ts-length decreases, sampling improves\r\nnobs <- length(Y)\r\n\r\nsigma_measure <- lognormal(0, 1)\r\nsigma_delta <- lognormal(0, 1);\r\nsigma_position <- lognormal(0, 1);\r\n\r\nlatent_delta_sd <- c(ones(1), rep(sigma_delta, nobs - 1))\r\nlatent_delta <- normal(0, latent_delta_sd)\r\nlatent_delta <- cumsum(latent_delta)\r\n# latent_delta <- zeros(nobs) # uncomment to make sampling work\r\n\r\nlatent_positions_sd <- c(ones(1), rep(sigma_position, nobs - 1))\r\nlatent_positions_mean <- c(-2, rep(0, nobs - 1))\r\nlatent_positions <- normal(latent_positions_mean, latent_positions_sd)\r\nlatent_positions <- cumsum(latent_positions + latent_delta)\r\n\r\n# observed position:\r\ndistribution(Y) <- normal(latent_positions, sigma_measure);\r\n\r\ncompiled_model <- model(latent_positions)\r\n\r\ndraws_list <- replicate(3, mcmc(compiled_model, warmup = 250))\r\ndraws_subset <- coda::mcmc.list(purrr::map(draws_list, ~.x[,1:5]))\r\nbayesplot::mcmc_trace(draws_subset)\r\n```\r\n\r\nI noticed that the problems increase as the length of the time-series increases from the original subset of 5 to the full length, with no sharp point where sampling-performance suddenly drops, but instead a gradual degredation.\r\n\r\nI also noticed that removing the 'delta' component makes the problems go away. However, sharpening the prior on sigma_delta does not help.\r\n\r\nI didn't have any luck increasing warmup or specifying manual inits."}}
{"actor":{"display_login":"jwdink"}}