On 11/24/13, 11:55 PM, KK Sasa wrote:
> In previous example, I have put in the data, but didn't use it.
> This time, I simply drew samples from multivariate normal distribution and the R code is like
>
> library(rstan)
> model <- '
> data{
> matrix[6,6] sigma1;
> }
> transformed data {
> matrix[6,6] L;
> vector[6] mu1;
> mu1[1] <- 0; mu1[2] <- 0; mu1[3] <- 0;
> mu1[4] <- 0; mu1[5] <- 0; mu1[6] <- 0;
You can replace these six assignments with:
mu1 <- rep_vector(0.0, 6);
> L <- cholesky_decompose(sigma1);
> }
> parameters{
> vector[6] randnum;
> }
> transformed parameters {
> vector[6] y;
> y <- mu1 + L * randnum;
If the mu1 are all 0, you could just drop mu1
altogether for this model.
> }
> model {
> randnum ~ normal(0,1);
> }
> '
> sigma1 <- diag(pair_num) * 2
> sigma1[2,1] <- 1; sigma1[1,2] <- sigma1[2,1];
> sigma1[3,1] <- 1; sigma1[1,3] <- sigma1[3,1];
> sigma1[4,1] <- -1; sigma1[1,4] <- sigma1[4,1];
> sigma1[5,1] <- -1; sigma1[1,5] <- sigma1[5,1];
> sigma1[6,1] <- 0; sigma1[1,6] <- sigma1[6,1];
> sigma1[2,3] <- 1; sigma1[3,2] <- sigma1[2,3];
> sigma1[2,4] <- 1; sigma1[4,2] <- sigma1[2,4];
> sigma1[2,5] <- 0; sigma1[5,2] <- sigma1[2,5];
> sigma1[2,6] <- -1; sigma1[6,2] <- sigma1[2,6];
> sigma1[3,4] <- 0; sigma1[4,3] <- sigma1[3,4];
> sigma1[3,5] <- 1; sigma1[5,3] <- sigma1[3,5];
> sigma1[3,6] <- 1; sigma1[6,3] <- sigma1[3,6];
> sigma1[4,5] <- 1; sigma1[5,4] <- sigma1[4,5];
> sigma1[4,6] <- -1; sigma1[6,4] <- sigma1[4,6];
> sigma1[5,6] <- 1; sigma1[6,5] <- sigma1[5,6];
This kind of code is very error prone. I'd try
to keep the LHS ordered so that for sigma1[i,j]
definitions in the first column you have
i > j. Then you can make it symmetric with:
# 1 <= i < j <= 6
for (i in 1:5)
for (j in (i+1):6)
sigma1[i,j] <- sigma1[j,i];
Now that I look at it, this kind of code is very
error prone, too!
> data <- list(sigma1=sigma1)
> fit1 <- stan(model_code=model,data=data,
> iter=100000,chains=1,thin=1)
>
> # extract samples
> fit_ss <- extract(fit1, permuted = TRUE)
> yy <- fit_ss$y
>
What's the effective sample size and R-hat values?
I'd suggest running way fewer iterations and using
more chains.
> This output goes like
>
>> cov(yy)
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1.775847970 0.8776978664 0.871982398 -0.898150104 -0.9177753396 0.002816607
> [2,] 0.877697866 1.7783345347 0.877739161 0.900636668 0.0007111106 -0.910376544
> [3,] 0.871982398 0.8777391610 1.743159420 0.005756763 0.8589437393 0.873488914
> [4,] -0.898150104 0.9006366683 0.005756763 1.798786772 0.9184864501 -0.913193151
> [5,] -0.917775340 0.0007111106 0.858943739 0.918486450 5.3908461961 2.630386314
> [6,] 0.002816607 -0.9103765442 0.873488914 -0.913193151 2.6303863139 6.247356763
>
>
> It seems there is problem in convergence.
What makes you think that?
- Bob
> Ben Goodrich嚙踝蕭 2013嚙羯11嚙踝蕭25嚙踝蕭P嚙踝蕭嚙瑾UTC+8嚙磕嚙踝蕭11嚙踝蕭36嚙踝蕭49嚙踝蕭g嚙瘩嚙瘦
>
> Are there data involved? If so, the posterior distribution of something could easily be different from the prior
> distribution. That or it is possible that it didn't converge yet.
>
> Ben
>
> On Sunday, November 24, 2013 8:57:09 PM UTC-5, KK Sasa wrote:
>
> Sorry for unclear description. yy is distributed from multivariate normal distribution with mean 0 and sigma1.
> I used the method as Ben suggested (p. 158) and run it in Stan.
>
>
> Bob Carpenter嚙踝蕭 2013嚙羯11嚙踝蕭25嚙踝蕭P嚙踝蕭嚙瑾UTC+8嚙磕嚙踝蕭9嚙踝蕭13嚙踝蕭01嚙踝蕭g嚙瘩嚙瘦
>
> I'm not sure what you're asking here. How are sigma1
> and cov(yy) related? Is cov(yy) an estimate of something
> else based on a parameter in Stan?
>
> - Bob
>
>