cjgeyer / mcmc

R package mcmc (public version on CRAN)
19 stars 4 forks source link

Sampler becomes stuck, repeating a single proposal indefinitely #2

Open rplzzz opened 5 years ago

rplzzz commented 5 years ago

I am seeing the following odd behavior. When I run the sampler, like so:

out1 <- metrop(logpost, pv0, 10, scale=mcmc_scale, debug=TRUE)

I get a couple of good iterations, but then the sampler starts repeating the same proposal over and over:

out1$proposal
          [,1]      [,2]     [,3]      [,4]     [,5]     [,6]     [,7]     [,8]
 [1,] 3.073006 0.7735492 2.280427 0.2525763 2.127533 285.1648 1.125720 11.24583
 [2,] 3.146012 0.5470985 2.260854 0.2051526 2.255066 285.3296 1.251440 12.49165
 [3,] 3.219018 0.3206477 2.241281 0.1577288 2.382599 285.4944 1.377160 13.73748
 [4,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362
 [5,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362
 [6,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362
 [7,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362
 [8,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362
 [9,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362
[10,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362

You can see that starting with iteration 4, the proposal is the same every time.

Restarting from the point where the last one left off gives me a one more valid iteration, but the sampler quickly falls back into the same repetitive behavior:

out2 <- metrop(out1, pv0, 100, scale=mcmc_scale, debug=TRUE)
out2$proposal
           [,1]      [,2]     [,3]      [,4]     [,5]     [,6]     [,7]     [,8]
  [1,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362
  [2,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
  [3,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
  [4,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
  [5,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
  [6,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
  [7,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
  [8,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
  [9,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
 [10,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
 [11,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
 [12,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
 [13,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
 [14,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620
(...)

The same proposal then gets repeated for all of the remaining iterations.

I tried it one more time, starting from the end of the second run, and this time it was a few iterations before the repetition began:

out3 <- metrop(out2, pv0, 100, scale=mcmc_scale, debug=TRUE)
out3$proposal[1:15,]
          [,1]      [,2]     [,3]        [,4]     [,5]     [,6]     [,7]     [,8]
 [1,] 2.919562 0.5275253 2.213430  0.33268563 2.271547 286.5868 1.500605 12.14620
 [2,] 3.127507 0.5089804 2.380828 -0.01134893 2.182718 286.1311 1.301835 12.62126
 [3,] 3.100912 0.6157917 2.299748  0.24053632 2.345545 285.2590 1.331331 12.22602
 [4,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
 [5,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
 [6,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
 [7,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
 [8,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
 [9,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
[10,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
[11,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
[12,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
[13,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
[14,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
[15,] 3.082407 0.5776736 2.419722  0.02403483 2.273197 286.0605 1.381725 12.35563
(...)

Presumably R's RNG is getting mangled somehow, but nothing in my code touches anything related to the RNG. Do you have any idea what might be causing this behavior?

cjgeyer commented 5 years ago

No and I can't deal with it right now as I am in hospital but will take a look when I get out

On Sun, Oct 28, 2018, 2:49 PM Robert Link notifications@github.com wrote:

I am seeing the following odd behavior. When I run the sampler, like so:

out1 <- metrop(logpost, pv0, 10, scale=mcmc_scale, debug=TRUE)

I get a couple of good iterations, but then the sampler starts repeating the same proposal over and over:

out1$proposal [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 3.073006 0.7735492 2.280427 0.2525763 2.127533 285.1648 1.125720 11.24583 [2,] 3.146012 0.5470985 2.260854 0.2051526 2.255066 285.3296 1.251440 12.49165 [3,] 3.219018 0.3206477 2.241281 0.1577288 2.382599 285.4944 1.377160 13.73748 [4,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362 [5,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362 [6,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362 [7,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362 [8,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362 [9,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362 [10,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362

You can see that starting with iteration 4, the proposal is the same every time.

Restarting from the point where the last one left off gives me a one more valid iteration, but the sampler quickly falls back into the same repetitive behavior:

out2 <- metrop(out1, pv0, 100, scale=mcmc_scale, debug=TRUE) out2$proposal [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 3.079750 0.5285934 2.222736 0.3251265 2.038565 284.6061 1.331585 12.74362 [2,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [3,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [4,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [5,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [6,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [7,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [8,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [9,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [10,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [11,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [12,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [13,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 [14,] 2.919562 0.5275253 2.213430 0.3326856 2.271547 286.5868 1.500605 12.14620 (...)

The same proposal then gets repeated for all of the remaining iterations.

I tried it one more time, starting from the end of the second run, and this time it was a few iterations before the repetition began:

out3 <- metrop(out2, pv0, 100, scale=mcmc_scale, debug=TRUE) out3$proposal[1:15,] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 2.919562 0.5275253 2.213430 0.33268563 2.271547 286.5868 1.500605 12.14620 [2,] 3.127507 0.5089804 2.380828 -0.01134893 2.182718 286.1311 1.301835 12.62126 [3,] 3.100912 0.6157917 2.299748 0.24053632 2.345545 285.2590 1.331331 12.22602 [4,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [5,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [6,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [7,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [8,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [9,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [10,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [11,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [12,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [13,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [14,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 [15,] 3.082407 0.5776736 2.419722 0.02403483 2.273197 286.0605 1.381725 12.35563 (...)

Presumably R's RNG is getting mangled somehow, but nothing in my code touches anything related to the RNG. Do you have any idea what might be causing this behavior?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/cjgeyer/mcmc/issues/2, or mute the thread https://github.com/notifications/unsubscribe-auth/AAcdwPaUUrh80QIJPMDQbUYv781uDhc8ks5upgpDgaJpZM4X-Jfn .

rplzzz commented 5 years ago

@cjgeyer Sorry to hear about that! Hope you're feeling better soon.

I did some additional experiments that I'll post here when I have a chance to organize them, probably later this week.

cjgeyer commented 5 years ago

Sorry I took practically forever to get back to this, but I am doing a new version of the mcmc package at the request of CRAN. I cannot reproduce the behavior you report (see attachments, version 0.9-6 is not on CRAN yet but version 0.9-5 does the same thing).

On Mon, Oct 29, 2018 at 1:59 PM Robert Link notifications@github.com wrote:

@cjgeyer https://github.com/cjgeyer Sorry to hear about that! Hope you're feeling better soon.

I did some additional experiments that I'll post here when I have a chance to organize them, probably later this week.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cjgeyer/mcmc/issues/2#issuecomment-434036907, or mute the thread https://github.com/notifications/unsubscribe-auth/AAcdwMxtj3E3PXaaaYfSqpkE8JWC2rCoks5up1AMgaJpZM4X-Jfn .

-- Charles Geyer Professor, School of Statistics Resident Fellow, Minnesota Center for Philosophy of Science University of Minnesota charlie@stat.umn.edu

cjgeyer commented 5 years ago

woops! clicked "send" inadvertently.

If I am going to be able to do anything with this, I will need a complete working example, like the attachments in the previous email. See also https://www.chiark.greenend.org.uk/~sgtatham/bugs.html

On Tue, Mar 5, 2019 at 3:33 PM Charles Geyer charlie@stat.umn.edu wrote:

Sorry I took practically forever to get back to this, but I am doing a new version of the mcmc package at the request of CRAN. I cannot reproduce the behavior you report (see attachments, version 0.9-6 is not on CRAN yet but version 0.9-5 does the same thing).

On Mon, Oct 29, 2018 at 1:59 PM Robert Link notifications@github.com wrote:

@cjgeyer https://github.com/cjgeyer Sorry to hear about that! Hope you're feeling better soon.

I did some additional experiments that I'll post here when I have a chance to organize them, probably later this week.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cjgeyer/mcmc/issues/2#issuecomment-434036907, or mute the thread https://github.com/notifications/unsubscribe-auth/AAcdwMxtj3E3PXaaaYfSqpkE8JWC2rCoks5up1AMgaJpZM4X-Jfn .

-- Charles Geyer Professor, School of Statistics Resident Fellow, Minnesota Center for Philosophy of Science University of Minnesota charlie@stat.umn.edu

-- Charles Geyer Professor, School of Statistics Resident Fellow, Minnesota Center for Philosophy of Science University of Minnesota charlie@stat.umn.edu

rplzzz commented 5 years ago

I haven't had time to look at it either. When last I looked, I had just about convinced myself that we were seeing some sort of bad interaction between our model (which is written in C++) and the C++ sampler in your package. I never had any problem with models written in R (but of course I can't exactly reimplement our model in R). I had been planning to write a simple C++ posterior to test whether this is the case for all posteriors using C++, or just our model. However, I haven't had the time to get to that.

I will need a complete working example, like the attachments in the previous email.

I don't think that email attachments get posted to GitHub when you use email to respond to an issue. At any rate, I don't see the attachments.

cjgeyer commented 5 years ago

Ack! The attachments are in the devel/bug directory of the mcmc package (branch morph) https://github.com/cjgeyer/mcmc/blob/morph/devel/bug/foo.R https://github.com/cjgeyer/mcmc/blob/morph/devel/bug/foo.Rout https://github.com/cjgeyer/mcmc/blob/morph/devel/bug/foo-old. https://github.com/cjgeyer/mcmc/blob/morph/devel/bug/foo-old.RoutR