> print(fit, digits = 3)
Inference for Stan model: ch7_2.
4 chains: each with iter=21000; warmup=1000; thin=1; 21000 iterations saved.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
alpha1 104.930 2.214 17.274 104.533 105.139 105.224 105.309 151.083 61 1.001
alpha2 57.667 24.276 34.332 -1.946 57.299 77.431 77.546 77.738 2 308.550
sigma1 6.446 4.400 16.274 2.619 2.722 2.781 2.839 51.303 14 1.279
sigma2 21.894 23.489 33.242 2.548 2.676 2.758 19.808 83.114 2 25.105
dif -15.449 25.262 38.869 -80.330 -17.096 0.014 0.137 48.565 2 3.143
lp__ -1978.780 375.129 668.558 -3276.437 -2866.694 -1521.770 -1520.283 -1519.298 3 2.412
On 10/16/12 3:32 PM, Shige Song wrote:
> Thanks for the help, Ben. Dropping the seed did lead to closer estimates of the sigma.
There's always a seed. If you don't specify one, Stan
generates it randomly given the time.
> I did not expect setting random
> number seed can have such an effect.
It comes down to the initialization. If you do
your own initial points, it won't matter nearly
as much for sampling. But random inits, especially
with weak constraints, can lead to very bad initial
points that take a lot of time to adapt out of.
If you run long enough, adaptation should find a
better place and adapt to that before sampling.
- Bob
--
But I did not have problem with it in rstan as of commit a6ca1191aeac.Actually, if I comment out line 500 ofRStan should have exactly the same result as Stan for the same seed.
On 10/18/12 12:17 PM, Ben Goodrich wrote:
> On Thursday, October 18, 2012 11:30:20 AM UTC-4, Jiqiang Guo wrote:
>
> But I did not have problem with it in rstan as of commit a6ca1191aeac.
> Actually, if I comment out line 500 of
> https://code.google.com/p/stan/source/browse/rstan/rstan/inst/include/rstan/stan_fit.hpp
> <https://code.google.com/p/stan/source/browse/rstan/rstan/inst/include/rstan/stan_fit.hpp>,
> RStan should have exactly the same result as Stan for the same seed.
>
>
> I think RStan should give exactly the same result as Stan for the same seed.
How could it not? Is the seed somehow not getting passed?
// reset the seed
base_rng.seed(random_seed);
Does it have something to do with the difference
in the way n_eff is calculated? Coda uses some
spectral thing and RStan uses the split R-hat var+
estimator.
Can you see if the samples themselves are the same?
goodrich@CYBERPOWERPC:/tmp/SU/Shige$ ./ch7_2 --seed=12345 --chain_id=1 --refresh=-1 --data=Shige.dumpgoodrich@CYBERPOWERPC:/tmp/SU/Shige$ mv samples.csv samples_1.csv goodrich@CYBERPOWERPC:/tmp/SU/Shige$ ./ch7_2 --chain_id=2 --refresh=-1 --data=Shige.dumpgoodrich@CYBERPOWERPC:/tmp/SU/Shige$ mv samples.csv samples_2.csvgoodrich@CYBERPOWERPC:/tmp/SU/Shige$ ./ch7_2 --chain_id=3 --refresh=-1 --data=Shige.dumpgoodrich@CYBERPOWERPC:/tmp/SU/Shige$ mv samples.csv samples_3.csvgoodrich@CYBERPOWERPC:/tmp/SU/Shige$ ./ch7_2 --chain_id=4 --refresh=-1 --data=Shige.dumpgoodrich@CYBERPOWERPC:/tmp/SU/Shige$ mv samples.csv samples_4.csv> CLS <- lapply(1:4, FUN = function(chain) read.csv(file=paste0("samples_", chain, ".csv"), comment.char="#"))> source("Shige.R") # produces the fit object
> options(scipen = 5)> sapply(CLS, FUN = function(x) colMeans(x[,c(4:8,1)])) [,1] [,2] [,3] [,4]alpha1 105.2152530 105.221594 -0.1216296 105.22197200alpha2 -0.2451326 71.438486 77.4862292 77.51356160sigma1 2.7697423 2.776654 105.0612260 2.77333586sigma2 77.7610272 8.987968 2.7197145 2.68328607dif -74.9912841 -6.211314 102.3414980 0.09004965lp__ -2859.8060600 -1711.659570 -3700.3719800 -1520.92927000> t(apply(extract(fit, inc_warmup=FALSE), 2:3, mean)) [,1] [,2] [,3] [,4]alpha1 105.217908 105.21897243 105.20592224 105.218899alpha2 59.345505 77.49685872 77.48935035 73.223678sigma1 2.772667 2.77103205 2.77128175 2.772860sigma2 19.801809 2.79918901 2.73861566 6.785567dif -17.029143 -0.02815696 0.03266609 -4.012706lp__ -1917.642846 -1524.23329053 -1522.07697366 -1628.017967
On 10/18/12 4:05 PM, Ben Goodrich wrote:
> On Thursday, October 18, 2012 1:18:43 PM UTC-4, Bob Carpenter wrote:
>
> On 10/18/12 12:17 PM, Ben Goodrich wrote:
> > On Thursday, October 18, 2012 11:30:20 AM UTC-4, Jiqiang Guo wrote:
...
> > I think RStan should give exactly the same result as Stan for the same seed.
>
> How could it not? Is the seed somehow not getting passed?
>
>
> I think the issue is that rstan resets with a new seed (from R) in lines 499 -- 500
> |
> // reset the seed
> base_rng.seed(random_seed);
That would certainly do it.
I believe that's what Jiqiang said he'd fixed.
Assuming the lp__ is correct, the "good" samples are
command-line 4 and RStan 2 and 3? Are those params
sensible?
set.seed(12345)
n1 <- 600 # Number of femalesn2 <- 400 # Number of malesalpha1 <- 105 # Population mean of femalesalpha2 <- 77.5 # Population mean of malessigma <- 2.75 # Average population SD of both
n <- n1+n2 # Total sample sizey1 <- rnorm(n1, alpha1, sigma) # Data for femalesy2 <- rnorm(n2, alpha2, sigma) # Date for males> sapply(CLS, FUN = function(x) colMeans(x[,c(4:8,1)])) [,1] [,2] [,3] [,4]alpha1 105.2170710 3.878109 -0.6265813 102.266241alpha2 -0.4422086 77.491623 1.2363986 77.484879sigma1 2.7687514 99.062619 105.9107940 5.813496sigma2 78.1094912 2.718003 76.3153247 2.721343dif -75.3407397 96.344619 29.5954890 3.092153lp__ -2860.8249300 -3678.091020 -5034.9440600 -1638.677820> t(apply(extract(fit, inc_warmup=FALSE), 2:3, mean)) [,1] [,2] [,3] [,4]alpha1 105.217908 105.21897243 105.20592224 105.218899alpha2 59.345505 77.49685872 77.48935035 73.223678sigma1 2.772667 2.77103205 2.77128175 2.772860sigma2 19.801809 2.79918901 2.73861566 6.785567dif -17.029143 -0.02815696 0.03266609 -4.012706lp__ -1917.642846 -1524.23329053 -1522.07697366 -1628.017967Is it getting stuck in modes or just not warming up
fast enough?
Ben--
As I said, if the line of resetting the seed is commented out, RStan and Stan would be the same.I have that line of for the purpose it would be reproducible if we take the inits from a previous run and feed the same seed.So I would propose to add that line in Stan so that we would have the same results in Stan and RStan. Of course, I can be convinced to get rid of that line.
--
Does the R code calculate the seed for subsequent chains
the same way as the command line? It should be, as that's
the L'Ecuyer "parallel" seeding.
--
<Shige2.txt>
stan_rdump(c("n1", "n2", "y1", "y2"), file = "Shige.dump")source("Shige.dump")ch7_2_data <- list(n1=n1, n2=n2, x=x, y1=y1, y2=y2)--
--
goodrich@CYBERPOWERPC:/tmp/SU/Shige$ ./ch7_2 --data=Shige.dump --seed=-1STAN SAMPLING COMMANDdata = Shige.dumpinit = random initializationinit tries = 1samples = samples.csvappend_samples = 0save_warmup = 0seed = 4294967295 (user specified)
goodrich@CYBERPOWERPC:/tmp/SU/Shige$ ./ch7_2 --data=Shige.dump --seed=-1 --refresh=-1STAN SAMPLING COMMANDdata = Shige.dumpinit = random initializationinit tries = 1samples = samples.csvappend_samples = 0save_warmup = 0seed = 4294967295 (user specified)chain_id = 1 (default)iter = 2000warmup = 1000thin = 1 (default)equal_step_sizes = 0leapfrog_steps = -1max_treedepth = 10epsilon = -1epsilon_pm = 0delta = 0.5gamma = 0.05
Iteration: 1 / 2000 [ 0%] (Adapting)Iteration: 2 / 2000 [ 0%] (Adapting)
It's just treated as an int. Nothing in the doc
says it should do anyhting for -1. We should
be throwing an error, I think.
What do you think it should do?
I like the idea of having 0 turn it off,
but haven't implemented it yet.
--
On 10/18/12 7:48 PM, Ben Goodrich wrote:
> I propose that the default seed for Stan be -1 (which would indicate to generate a seed from the clock time and
> chain_id) and the default seed for stan() should also be -1. However, I was surprised to find that specifying --seed=-1
> works currently
I think the default behavior should be to choose
a random seed, not use -1.
Seeds are unsigned int in Stan (because they're
uint32_t in Boost).
I changed the original 'Shige2.txt' a bit so all the results should be exactly the same in RStan and Stan.The final results for running the script is because samples printed out by Stan have less digits after decimal points.
SAMPLING FOR MODEL 'salm' NOW (CHAIN 4).
Error in sampler$call_sampler(args_list[[i]]) :
No acceptably small step size could be found. Perhaps the posterior is not continuous?
error occurred during calling the sampler; sampling not done
Without forward simulation in generated quantities (not
implemented yet), we're bounded by:
(dimensions + 1) * iterations * chains
(I won't be able to read the paywalled ref until I
get back to Columbia.)
On Friday, October 19, 2012 10:38:56 PM UTC-4, Jiqiang Guo wrote:I changed the original 'Shige2.txt' a bit so all the results should be exactly the same in RStan and Stan.The final results for running the script is because samples printed out by Stan have less digits after decimal points.
Great! Now we are down to the problem that sigma2 in the first chain is equally terrible in both cases.
On Saturday, October 20, 2012 4:44:55 PM UTC-4, Bob Carpenter wrote:
It's very configurable as long as we can implement
the Boost concept of a base random number generator.
The question I see is whether we need an RNG for
which it's efficient to advance the position in the
sequence. This is what the one we're using lets you
do and I thought you (Ben) thought this was important.
Because I don't understand this stuff well, I would feel better if we used something from
http://trng.berlios.de/
as the PRNG. It is BSD, it is small, it is designed to be like Boost, it formed the basis for a proposal to get it into the C++11 standard, and the designers have explicitly thought about parallelism. So, to me, it is worth another dependency.