Setting Jeffreys’s prior on sigma

Hey,
I’d like to build this model (Bayesian One-Sample T-Test (Stan)) in brms. Is this possible?

I can’t figure out how to put the Jeffrey’s prior on sigma2 like this: target += log(1/sigma2); // Jeffreys prior on sigma2.

How can I add the equivalent prior on sigma?
I’ve tried this

priors <- c(prior('cauchy(0, 0.707)', class = "Intercept"),
            prior_string("target += sqrt(log(1/(sigma^sigma)))", check = FALSE))

but it didn’t work:

model$prior
prior class coef group resp dpar nlpar bound
1 cauchy(0, 0.707) Intercept
2 student_t(3, 0, 10) sigma
3 target += sqrt(log(1/(sigma^sigma)))

Sorry if I get something horribly wrong.

brms expects priors to be already defined functions. You might need to use stanvars (check brms docs, if that’s not clear feel free to ask :-) ) to add a piece of Stan code defining a function representing the Jeffrey’s prior. Once a function like this is added to the model:

real jeffreys_lpdf(real x) {
  return log(1 / x);
}

You should be able to write prior('jeffreys()', ...)

Best of luck!

2 Likes

Hey,
Thank you so much! This is what I tried:

# Generate data
df <- data.frame(y = rnorm(100, 0.5, 1))

# Create custom jeffrey prior
jeffreyPrior <- stanvar(block  = 'functions', 
                        name   = 'prior_sigma', 
                        scode  = "real jeffreys_lpdf(real x) {
  return log(1 / x);
}")

# Make Stancode 
t_test_model <- make_stancode(y ~ 1, prior = priors, stanvars = jeffreyPrior, data = df)

However, I got a error message saying PARSER EXPECTED: <probability function argument>. More detail:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in 'model3b6059dd15ab_file3b60e233cd4' at line 25, column 25
  -------------------------------------------------
    23:   // priors including all constants
    24:   target += cauchy_lpdf(Intercept | 0, 0.707);
    25:   target += jeffreys_lpdf(sigma | )
                                ^
    26:     - 1 * jeffreys_lccdf(0 | );
  -------------------------------------------------

PARSER EXPECTED: <probability function argument>
Error in stanc(model_code = paste(program, collapse = "\n"), model_name = model_cppname,  : 
  failed to parse Stan model 'file3b60e233cd4' due to the above error.

What am I doing wrong here?

At this point, the probably easiest solution is probably

set_prior("", "sigma") + 
  set_prior("target += log(1 / sigma)", check = FALSE)
2 Likes