I am building the following model for logistic regression in Stan (pystan):
Predictor: $\eta_t = \beta_1 x_{1,t} + \beta_2 x_{2,t} + \beta_3 x_{3,t} + \beta_4 x_{4,t} + \sigma \epsilon_t$
Outcome: $y_t \sim \text{Bernoulli}(f(n_t))$
where $\beta_i$ and $\sigma$ are the coefficients of regression, $x_{i,t}$ are the values of regressors for trial $t$ and $\epsilon_t \sim N(0,1)$ is the noise in the predictor $\eta_t$. Finally, $f$ is the sigmoid link function for linking the predictor $\eta_t$ to the Bernoulli outcome $y_t$ on each trial $t$.
Without the noise term in the predictor $\sigma \epsilon_t$, the following code for the model runs fine:
choice_code_bernoulliLogit = """
data {
int<lower=1> N; // number of training samples
int<lower=0> K; // number of predictors
matrix[N, K] x; // matrix of predictors
int<lower=0, upper=1> y[N]; // observed/training choice accuracy
}
parameters {
vector[K] beta;
}
model {
// faster implementation
vector[N] theta;
theta = x * beta;
// priors
beta ~ normal(2,2);
// model
y ~ bernoulli_logit(theta);
}
generated quantities {
vector[K] choiceAccuracy;
choiceAccuracy = inv_logit(beta);
}
"""
# defining the choice data
# choice_data = {...}
# fit, extract parameters, and print summary
posterior = stan.build(choice_code_bernoulliLogit, data=choice_data)
fit = posterior.sample(num_chains=4, num_samples=1000)
However, I cannot figure out how/where to add noise to the model. I keep getting Semantic errors while building the code in stan; I'm not sure where I'm going wrong, and cannot seem to find any examples on the stan documentation pages nor anywhere else. The following code is an example of what I tried to do:
choice_code_bernoulliLogit = """
data {} // same as before
parameters {
vector[K] beta;
real<lower=0> sigma;
}
model {
// faster implementation
vector[N] theta;
theta = x * beta;
// priors
beta ~ normal(2,2);
// model
y ~ bernoulli_logit(normal(theta,sigma));
}
generated quantities {} // same as before
"""
I have also tried removing the vectorization with explicit for loops, and tried using the normal_rng() in a transformed parameters block to make the predictor noisy, but none of that doesn't seem to work. Any help would be greatly appreciated! Thanks :)
R
code, but the comments should help you understand the idea, even if you don't knowR
. $\endgroup$