Syntax error?

data {
int<lower=0> N; // number of observations
vector[N] x; // predictor variables
vector[N] y; // response variable
}

parameters {
real mu; // intercept
real<lower=0> sigma; // residual standard deviation
vector[N] f; // function values at observed x
}

model {
f ~ bart_custom_lpdf(0, 0, 0.5, 2, x); // BART prior
y ~ normal(f, sigma); // likelihood
sigma ~ gamma(0.5, 0.5); // gamma prior on sigma
}

generated quantities {
vector[N] y_pred; // predicted values
for (i in 1:N) {
y_pred[i] = normal_rng(f[i], sigma);
}
}

functions {
real bart_custom_lpdf(vector f, real a, real b, real tau, vector x) {
// Define custom probability function for bart_custom
// f: function values at observed x
// a: alpha
// b: beta
// tau: tau
// x: predictor variables
int num_trees = 50; // number of trees
int N = size(x);
matrix[N, num_trees] trees;
vector[num_trees] weights;
vector[N] mu;
real lp = 0;

for (j in 1:num_trees) {
  for (i in 1:N) {
    trees[i, j] = normal_rng(0, 1);
  }
  weights[j] = normal_rng(0, sqrt(tau));
}

mu = x * trees * weights;

lp += normal_lpdf(f | mu, a);
lp += normal_lpdf(weights | 0, sqrt(b));
lp += normal_lpdf(trees | 0, 1);

return lp;

}
}

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:

vector ~ bart_custom(int, int, real, int, vector)

Distribution bart_custom not found. Require function with _lpdf or _lpmf or _log suffix
Real return type required for probability function.
error in ‘modelb2b47f9297ad_BART_function’ at line 14, column 40

12: 
13: model {
14:   f ~ bart_custom_lpdf(0, 0, 0.5, 2, x); // BART prior
                                           ^
15:   y ~ normal(f, sigma);       // likelihood

Error in stanc(filename, allow_undefined = TRUE) :
failed to parse Stan model ‘BART function’ due to the above error.

Your functions block should be at the top of your Stan file (before the data block).

Additionally, you don’t need the _lpdf extension when using the sampling statement ~. So in the model block you should just call:

f ~ bart_custom(...)

Thank you very much. But I still get an error: SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:

size(vector)

Available argument signatures for size:

size(int)
size(real)
size(vector)
size(row_vector)
size(matrix)
size(int[ , ])
size(real[ , ])
size(vector[ , ])
size(row_vector[ , ])
size(matrix[ , ])
size(int[ , , ])
size(real[ , , ])
size(vector[ , , ])
size(row_vector[ , , ])
size(matrix[ , , ])
size(int[ , , , ])
size(real[ , , , ])
size(vector[ , , , ])
size(row_vector[ , , , ])
size(matrix[ , , , ])
size(int[ , , , , ])
size(real[ , , , , ])
size(vector[ , , , , ])
size(row_vector[ , , , , ])
size(matrix[ , , , , ])
size(int[ , , , , , ])
size(real[ , , , , , ])
size(vector[ , , , , , ])
size(row_vector[ , , , , , ])
size(matrix[ , , , , , ])
size(int[ , , , , , , ])
size(real[ , , , , , , ])
size(vector[ , , , , , , ])
size(row_vector[ , , , , , , ])
size(matrix[ , , , , , , ])

error in ‘modelb2b43f2f648_BART_function’ at line 11, column 19

 9:     // x: predictor variables
10:     int num_trees = 50; // number of trees
11:     int N = size(x);
                      ^
12:     matrix[N, num_trees] trees;

Error in stanc(filename, allow_undefined = TRUE) :
failed to parse Stan model ‘BART function’ due to the above error.

rng functions can only be called in transformed data and generated quantities blocks. Also, any function that has a call to an rng function must itself be declared with as an rng function. The way this pdf is written cannot be estimated in Stan because of the above restrictions.

@spinkney I do not understand exactly where should I change. Can you please the lines?