Hi, everyone!

I met a problem in my final task. And now I want to describe it for all of you. Can someone help me to solve it? Thanks!

**Problem** Suppose there is a bivariate distribution with pdf f(x,y;\alpha_1,\alpha_2), where \alpha_1 and \alpha_2 are parameters to be estimated. Now I have some data (x_i,y_i) (i=1,2,\dots,N) distributed from f, I would like to use M.L.E method to estimate \alpha_1 and \alpha_2. The following is stan code:

```
functions{
real fun_log(vector x,vector y, real alpha1, real alpha2){
vector [num_elements(x)] prob;
real lprob;
for (i in 1:num_elements(x)){
prob[i] <- f(x[i],y[i],alpha1,alpha2]);
}
lprob <- sum(log(prob));
return lprob;
}
}
data{
int N;
vector[N] x;
vector[N] y;
}
parameters{
real alpha1;
real alpha2;
}
model{
x ~ fun(alpha1,alpha2);
y ~ fun(alpha1,alpha2);
}
```

But it seems that the stan code is obviously wrong and error appears when simulating. Can someone help me to fix the bug?

I think that code has syntax errors and would not compile.

The sampling statement stuff can seem a little weird (since there are hidden arguments and such). Maybe start with:

```
functions{
real fun(vector x,vector y, real alpha1, real alpha2){
vector [num_elements(x)] prob;
real lprob;
for (i in 1:num_elements(x)){
prob[i] <- f(x[i],y[i],alpha1,alpha2]);
}
lprob <- sum(log(prob));
return lprob;
}
}
data{
int N;
vector[N] x;
vector[N] y;
}
parameters{
real alpha1;
real alpha2;
}
model{
target += fun(x, y, alpha1,alpha2);
}
```

You can increment the special target variable and achieve all the same things a sampling statement can do (without the syntax which can be a bit weird).

What the model block does is evaluate the log density and store that in target (and that’s how what the MCMC algorithm uses to do its sampling). I probably didn’t say that clearly. Have a look here: https://mc-stan.org/docs/2_19/reference-manual/increment-log-prob-section.html and here: https://mc-stan.org/docs/2_19/reference-manual/sampling-statements-section.html

That make sense?

2 Likes