Re: [stan-dev] student-t

1,054 views
Skip to first unread message

Daniel Lee

unread,
Aug 30, 2012, 10:24:48 PM8/30/12
to stan-...@googlegroups.com
What's the error that you get? It should work fine, but we currently
don't have a vectorized version of t distribution.

On Thu, Aug 30, 2012 at 10:20 PM, Andrew Gelman
<gel...@stat.columbia.edu> wrote:
> On an unrelated note, how do we do the t distribution? I tried replacing
> eta ~ normal (0, 1);
> with
> eta ~ student_t (4, 0, 1);
> to do the t with 4 d.f., but it didn't work, I got a parser error.
> I also tried
> t (4, 0, 1);
> but that also failed.
>
> A
>



PS. Discussion moved to stan-users.

Daniel Lee

unread,
Aug 30, 2012, 10:44:46 PM8/30/12
to stan-...@googlegroups.com
The issue with the model is the use of "sigma" as opposed to
"sigma[j]". The integer parameter is not the source of the parsing
error. The correct model should be:

data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real theta[J];
real mu;
real<lower=0> tau;
}
model {
theta ~ normal (mu, tau);
for (j in 1:J)
y[j] ~ student_t (4,theta[j], sigma[j]);
}




Daniel


On Thu, Aug 30, 2012 at 10:36 PM, Andrew Gelman
<gel...@stat.columbia.edu> wrote:
> Here are 4 models. Two work, two don't.
>
> (1) This works:
>
> schools_code <- '
> data {
> int<lower=0> J; // number of schools
> real y[J]; // estimated treatment effects
> real<lower=0> sigma[J]; // s.e. of effect estimates
> }
> parameters {
> real theta[J];
> real mu;
> real<lower=0> tau;
> }
> model {
> theta ~ normal (mu, tau);
> for (j in 1:J)
> y[j] ~ normal (theta[j], sigma);
> }
> '
>
> (2) This works too:
>
> schools_code <- '
> data {
> int<lower=0> J; // number of schools
> real y[J]; // estimated treatment effects
> real<lower=0> sigma[J]; // s.e. of effect estimates
> }
> parameters {
> real theta[J];
> real mu;
> real<lower=0> tau;
> }
> model {
> for (j in 1:J){
> theta[j] ~ student_t (4, mu, tau);
> y[j] ~ normal (theta[j], sigma);
> }
> }
> '
>
>
> (3) This doesn't:
>
> schools_code <- '
> data {
> int<lower=0> J; // number of schools
> real y[J]; // estimated treatment effects
> real<lower=0> sigma[J]; // s.e. of effect estimates
> }
> parameters {
> real theta[J];
> real mu;
> real<lower=0> tau;
> }
> model {
> theta ~ normal (mu, tau);
> for (j in 1:J)
> y[j] ~ student_t(4,theta[j], sigma);
> }
> '
>
> It gives an error:
>
> TRANSLATING MODEL 'anon_model' FROM Stan CODE TO C++ CODE NOW.
> Error in stanc(file = file, model_code = model_code, model_name = model_name, :
> failed to parse Stan model 'anon_model' and error message provided as:
> LOCATION: file=input; line=15, column=7
>
> y[j] ~ student_t (4,theta[j], sigma);
> ^-- here
>
> DIAGNOSTIC(S) FROM PARSER:
>
> no matches for function name="student_t_log"
> arg 0 type=real
> arg 1 type=int
> arg 2 type=real
> arg 3 type=real[1]
> unknown distribution=student_t
> Parser expecting: <statement>
>
>
> (4) Aaahhh, so I thought it was a problem with the integer "4". But it also failed with the real "4.0":
>
> schools_code <- '
> data {
> int<lower=0> J; // number of schools
> real y[J]; // estimated treatment effects
> real<lower=0> sigma[J]; // s.e. of effect estimates
> }
> parameters {
> real theta[J];
> real mu;
> real<lower=0> tau;
> }
> model {
> theta ~ normal (mu, tau);
> for (j in 1:J)
> y[j] ~ student_t (4.0,theta[j], sigma);
> }
> '
>
> TRANSLATING MODEL 'anon_model' FROM Stan CODE TO C++ CODE NOW.
> Error in stanc(file = file, model_code = model_code, model_name = model_name, :
> failed to parse Stan model 'anon_model' and error message provided as:
> LOCATION: file=input; line=15, column=7
>
> y[j] ~ student_t (4.0,theta[j], sigma);
> ^-- here
>
> DIAGNOSTIC(S) FROM PARSER:
>
> no matches for function name="student_t_log"
> arg 0 type=real
> arg 1 type=real
> arg 2 type=real
> arg 3 type=real[1]
> unknown distribution=student_t
> Parser expecting: <statement>
>
>
> (Summary) The problem arises only with the model for y, not with theta.
> --
>
>

Andrew Gelman

unread,
Aug 30, 2012, 10:45:36 PM8/30/12
to stan-...@googlegroups.com
Fuck. I'm such an idiot. I thought of everything but that.

Ben Goodrich

unread,
Aug 30, 2012, 10:53:59 PM8/30/12
to stan-...@googlegroups.com
We should have a table of distributions somewhere indicating which are currently vectorizeable on which of their arguments.

Ben

Daniel Lee

unread,
Aug 30, 2012, 11:23:13 PM8/30/12
to stan-...@googlegroups.com


On Aug 30, 2012 10:54 PM, "Ben Goodrich" <goodri...@gmail.com> wrote:
>
> We should have a table of distributions somewhere indicating which are currently vectorizeable on which of their arguments.
>
> Ben

Definitely. We will get that into the manual soon.

Daniel

Danilo Assis Pereira

unread,
Jun 3, 2016, 1:23:40 PM6/3/16
to Stan users mailing list
I found the following syntax to obtain the posterior central tendency (mu) and dispersion (sigma) paramenters using a normal distribution. I tried to change it to obtain these parameters using t-distribution. I couldn't do that following the posts above. Could someone help me with this?

Thank you very much!

Danilo

model <- "
// Inferring the Mean and Standard Deviation of a t-distribution
  data { 
    int<lower=0> J;
real y[J]; // estimated treatment effects 
real<lower=0> sigma[J]; // s.e. of effect estimates 
parameters { 
real theta[J]; 
real mu; 
real<lower=0> tau; 
model { 
theta ~ normal (mu, tau); 
for (j in 1:J) 
y[j] ~ student_t (4,theta[j], sigma[j]); 
}#x <- c(1.1, 1.9, 2.3, 1.8)
x <- sample(dados$A, 100)
n <- length(x)

data <- list(x=x, n=n) # to be passed on to Stan
myinits <- list(
  list(mu=0, sigma=1))

# parameters to be monitored: 
parameters <- c("mu", "sigma")

# The following command calls Stan with specific options.
# For a detailed description type "?rstan".
samples <- stan(model_code=model,   
                data=data, 
                init=myinits,  # If not specified, gives random inits
                pars=parameters,
                iter=10000, 
                chains=1, 
                thin=1,
                # warmup = 100,  # Stands for burn-in; Default = iter/2
                # seed = 123  # Setting seed; Default is random seed
                )
# Now the values for the monitored parameters are in the "samples" object, 
# ready for inspection.

mu <- extract(samples)$mu
sigma <- extract(samples)$sigma 
summary(samples)

Daniel Lee

unread,
Jun 3, 2016, 2:15:59 PM6/3/16
to stan-...@googlegroups.com
Hi Danilo,

It's really hard to guess what is wrong without your output. 

One mistake is that you're missing a double quote (").


Daniel
--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages