I am trying to convert the following model code to a multithreaded format using reduce_sum
Original:
data {
int<lower=0> N;//Number of observations
int<lower=1> J;//Number of predictors with random slope
int<lower=1> K;//Number of predictors with non-random slope
int<lower=1> L;//Number of customers/groups
int<lower=0,upper=1> y[N];//Binary response variable
int<lower=1,upper=L> ll[N];//Number of observations in groups
matrix[N,K] x1;
matrix[N,J] x2;
}
parameters {
vector[J] rbeta_mu; //mean of distribution of beta parameters
vector<lower=0>[J] rbeta_sigma; //variance of distribution of beta parameters
vector[J] beta_raw[L]; //group-specific parameters beta
vector[K] beta;
}
transformed parameters {
vector[J] rbeta[L];
for (l in 1:L)
for (j in 1:J)
rbeta[l][j] = rbeta_mu[j] + rbeta_sigma[j] * beta_raw[l][j]; // coefficients on x
}
model {
rbeta_mu ~ normal(0,100);
rbeta_sigma ~ cauchy(0,10);
beta~normal(0,10);
for (l in 1:L)
beta_raw[l] ~ normal(0,1);
for(n in 1:N)
y[n]~bernoulli_logit(x1[n] * beta + x2[n] * rbeta[ll[n]]);
}
Reduce_Sum attempt :
functions {
real partial_sum(int start,
int end,
int[] y_slice,
matrix x1,
matrix x2,
vector[] beta,
matrix rbeta,
int J,
int[] ll) {
return bernoulli_logit_lpmf(y_slice[start:end] | x1[start:end,] * beta + (x2[start:end,:] .* rbeta[ll[start:end],:]) * rep_vector(1,J));
}
}
data {
int<lower=0> N;//Number of observations
int<lower=1> J;//Number of predictors with random slope
int<lower=1> K;//Number of predictors with non-random slope
int<lower=1> L;//Number of customers/groups
int<lower=0,upper=1> y[N];//Binary response variable
int<lower=1,upper=L> ll[N];//Number of observations in groups
matrix[N,K] x1;
matrix[N,J] x2;
}
parameters {
vector[J] rbeta_mu; //mean of distribution of beta parameters
vector<lower=0>[J] rbeta_sigma; //variance of distribution of beta parameters
vector[J] beta_raw[L]; //group-specific parameters beta
vector[K] beta;
}
transformed parameters {
vector[J] rbeta[L];
for (l in 1:L)
for (j in 1:J)
rbeta[l][j] = rbeta_mu[j] + rbeta_sigma[j] * beta_raw[l][j]; // coefficients on x
}
model {
rbeta_mu ~ normal(0,100);
rbeta_sigma ~ cauchy(0,10);
beta~normal(0,10);
for (l in 1:L)
beta_raw[l] ~ normal(0,1);
target+=reduce_sum(partial_sum,1,y,x1,x2,beta,rbeta,J,ll);
}
I am getting the following error:
This is the statement giving the error:
return bernoulli_logit_lpmf(y_slice[start:end] | x1[start:end,] * beta + (x2[start:end,:] .* rbeta[ll[start:end],:]) * rep_vector(1,J));
Any help on how to resolve it is greatly appreciated.