How to use "target +=" instead of writing a likelihood_log function in stan

Hi all, I am trying to get familiar with RStan. I tried examples from Stan reference guide for “target+=” and it worked. But, in my code I got the error while compiling. The code within model block as follows:

model{
   mu ~ uniform(4.0,4.5);
   sigma2 ~ uniform(0.1,0.5)

for(i in 1:5){
vector[3] V; 
vector[5] t;
t[1]=50;
for(j in 2:5){
     t[j] = t[j-1]+1;
}
V[1] = integrate_1d(fun_integral,t[1],t[2],{mu,sigma2},{0.0}, {0}, 1e-8);
target += pow(V[1],M[i,1]);

for(k in 2:K){
V[k] = integrate_1d(fun_integral,t[k],t[k+1],{mu,sigma2},{0.0}, {0}, 1e-8)
target += pow(V[k],M[i,k]);
}
}
}

Here, M is a matrix and fun_integral is the likelihood. Please kindly tell me why is this code wrong?
Please!

Note that target += requires the logarithm of the likelihood so you should write

target += M[i,k]*log(V[k]);

Other than that, your code looks fine. What error did you get?

That means pow(log(V[k]), M[i,k]) ?? Here, I want to use power.

TRANSLATING MODEL ‘09067cdebe4be8d8dcbb771b890fdfaa’ FROM Stan CODE TO C++ CODE NOW.
PARSER FAILED TO PARSE INPUT COMPLETELY
STOPPED AT LINE 1:

This is my function block
functions{
real fun_integral(real x,
real xc,
real[] theta,
real[] x_r,
int[] x_i){

  real mu = theta[1];
  real sigma2 = theta[2];
  return  0.3*exp(-((log(x)-mu)^2)/(2*sigma2));

}
}

Is there an error in function block?

You are at least missing a couple of semicolons, one after uniform(0.1,0.5) and one after the second integral. Not sure why it it complains about line 1.

Are you familiar with the logarithm identity

\log(x^m) = m\times \log(x)

I ll try and note here, I am familiar with it but I made a mistake here. Thanks a lot.

Error looks like this ;

SAMPLING FOR MODEL ‘843c6ba9cfaa3c4c0a9a5e81d15f0e98’ NOW (CHAIN 1).
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: Error in function tanh_sinh::integrate: The tanh_sinh quadrature evaluated your function at a singular point and got nan. Please narrow the bounds of integration or check your function for singularities. (in ‘model18943bc16ebd_843c6ba9cfaa3c4c0a9a5e81d15f0e98’ at line 85)

Do you have any idea for that?
Thanks

Te likelihood is defined only when x>0 so maybe one t is \leq 0? Actually, your likelihood looks like a log-normal? Stan has a builtin lognormal CDF if that’s what you’re after.
Oh, wait, you do define t[1]=50 and so on. Those aren’t the problem.
However, the uniform prior on mu and sigma is. If they are parameters you need to declare the bounds.

parameters {
  real<lower=4.0,upper=4.5> mu;
  real<lower=0.1,upper=0.5> sigma2;
}

Hello Niko, I have again back to my code for working with. Thank you for the above comments and I have done modifications as you suggested. But, again I am having this error message,

Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: vector[uni] assign range: accessing element out of range. index 0 out of range; expecting index to be between 1 and 3
“error occurred during calling the sampler; sampling not done”

Is this saying the error in vector defined line? or some other else? I would like to have your suggestions?
I will be most grateful for your time and comments here.

Thanks

Stan’s indexing starts at 1 so the first element in vector V is V[1]. The error says you have V[0] somewhere.

Thanks for your suggestions. I found an error in indexing and hope that it’s okay now. But, I am struggling with some other errors so then tried to solve those in various ways, but again I am failed.
Got this error message;
SAMPLING FOR MODEL ‘02bf6148d01d0edcb8aa3daa9e2c762f’ NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.
Chain 1: Stan can’t start sampling from this initial value.

But, because of above issue so then I decided to add “ïnit” with "stan() as follows;

initial_values = list(mu=runif(1,4.0,4.5), sigma2=runif(1,0.01,0.05));
stan(model_code,data,iter = 500, chains = 2, init=initial_values)

Now I want to know that should we parse equal number of “init” values sets as how many chains in our model?

Please kindly leave me some suggestions here.

Thanks

I don’t use R or RStan but the documentation says

inits via list: Set inital values by providing a list equal in length to the number of chains. The elements of this list should themselves be named lists, where each of these named lists has the name of a parameter and is used to specify the initial values for that parameter for the corresponding chain.

You can also simply set init=0.

@Bob_Carpenter @nhuurre I added init values for my stan() as this:
stan(model_code, data, warmup = 250, iter = 500, chains = 2, init = list(list(mu=4.5),list(sigma2=0.05))

But, no luck. It gives me this error message,
SAMPLING FOR MODEL ‘02bf6148d01d0edcb8aa3daa9e2c762f’ NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.
Chain 1: Stan can’t start sampling from this initial value.

I don’t know what is the reason for it, Do I need to define a function to generate initial values that can
be fed to function stan’s argument init as the example mentioned under this link “https://mc-stan.org/rstan/reference/stan.html#examples”.

Your responses are highly appreciated.

Thanks