# Stan with the approximated likelihoods

My data is a size classification data in which a human subject classifies whether a stimulus is a “large (1)” or “small(0)” trial to trial. The stimulus size of a trial is randomly selected between large (1), medium (0), and small (-1). 26 consecutive trials are grouped as a run. Data looks like as follows:

``````run    trial      stimulus    choice
1      1          1           1
1      2          0           1
1      3          -1          0
...
1      26         1           1
2      1          -1          1
...
``````

My model has a parameter: the stimulus measurement variability (sm). Critically, I want to implement MCMC by using the approximated likelihood. Here is the Stan code and the annotation:

nR = the number of runs (8)
nT = the number of trials (26)
Stm = the stimuli (1, 0, or -1)
Chc = the choices (1 or 0)
nIter4LhApprox = the number of iteration for approximating the likelihoods (1000)

``````data {
int<lower=1> nR;
int<lower=1> nT;
int<lower=-1, upper=1> Stm[nT,nR];
int<lower=0, upper=1> Chc[nT,nR];
int<lower=1> nIter4LhApprox;
}

parameters {
real<lower=0, upper=3>  sm; // measurement noise
}

model {
sm  ~ uniform(0,3);
for (iR in 1:nR) {
for (iT in 1:nT) {
real im;
real ic;
real id;
ic = 1;
for (iIter in 1:nIter4LhApprox) {
im ~ normal(Stm[iT,iR],sm);
id = (im>0)*1.0;
ic = ic + id;
}
Chc[iT,iR] ~ bernoulli(ic*1.0/(nIter4LhApprox+1));
}
}
}
``````

However, when I ran the code, I encountered an error message:

``````Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: normal_lpdf: Random variable is nan, but must not be nan!  (in 'model6d4d601cfd3a_KalmanClassifier_Reduced_NoHierarchical2' at line 23)

Chain 1:
Chain 1: Initialization between (-2, 2) failed after 100 attempts.
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler\$call_sampler(args_list[[i]]) : Initialization failed."
``````

How can I resolve the problem?

It seems that your model is

``````model {
sm  ~ uniform(0,3);
for (iR in 1:nR) {
for (iT in 1:nT) {
real p = normal_cdf(Stm[iT,iR], 0, sm);
Chc[iT,iR] ~ bernoulli(p);
}
}
}
``````

but you want to approximate the likelihood by random sampling instead of using the exact CDF.
Such approximation is not possible; Stan requires the likelihood function to be deterministic and continuous.

1 Like

Yes, you exactly understand the model. Thanks for the clarification!