Drawing samples from posterior given constant parameters, product and length functions in Stan

I am trying to translate an MC simulation from Metropolis-Hastings Monte Carlo algorithm into Stan. I have a probability distribution and I just want to draw samples from that distribution given (constant parameters).

My probability distribution is something like (sorry, I don’t know LaTeX):

exp(a1*f(m1,n1)**2) * exp(a2*f(m2,n2)**2) * exp(a3*f(m3,n3)**2) 

My proposal step is based on a random draw from a uniform distribution and my accept/reject decision is based on:

  1. I know that I can extract samples from the S4 stan model object in R, however, I do not know how to just sample using Stan if my parameters are constant. Is this possible without knowing the distribution/variance of the parameters or using uninformed prior? If it is impossible to do it without using uninformed prior, would this compromise my samples as compared to using the Metropolis-Hastings MC algorithm?

  2. Do I need to declare priors in this case eg:

a ~ normal(a, 0)
  1. Since my posterior is essentially a product itself 3 times, is it possible to declare a, n, m as vectors and express the posterior as a product. I’m not sure that for loop would work here, so how would this be possible?

  2. How to get a length of a vector in Stan?

Sorry for many odd questions, I am a beginner with Stan.

Thank you in advance.

Hi, great you are considering Stan for your problem.

I am however not sure I understand your question completely.

So some of the variables in the formula are known and the others you want to sample? (and which are known?) If so, then no “proposals” or similar should be needed. Note that: a) the target in Stan describes the logarithm of the target density and b) Stan doesn’t need proposals and handles accept/reject decisions for you, so (if your density is well behaved) it should sample automatically after you define it. So if I assumed that a1, a2, a3 are real unbounded values you want to sample and the m and n values are known integers, the code would look something like (not actually tested, just a sketch):

functions {
  real f(int m, int n) {
     //your implementation here

data {
  int m1;
  int m2;
  int m3;
  int n1;
  int n2;
  int n3;

transformed data {
  //Since m, n are constant we can compute the f values here 
  // and store them for the full run increasing speed
  real f1_square = square(f(m1, n1));
  real f2_square = square(f(m2, n2));
  real f3_square = square(f(m3, n3));

parameters {
  real a1;
  real a2;
  real a3;

model {
  // the logarithm of the formula you've given (hope I did not mess up)
  target += a1 * f1_sqare + a2 * f2_square + a3 * f3_square;

If the density is well-behaved, you should get samples for your a values by running this Stan program.

If I understood you correctly then no - the a values already have a distribution, and putting another one on them would make no sense.

Yes. But but note that Stan uses .*, for element-wise multiplication while * is reserved for matrix multiplication. So in the model I outlined if you had vector[3] f_square; and vector[3] a; then you could rewrite the model block as target += sum(a .* f_square)

Check out the manual at https://mc-stan.org/docs/2_25/functions-reference/array-size-and-dimension-function.html

Does that answer your question?