In the Stan manual, it is stated that you can specify truncated distributions by using T[min, max] after the sampling statement.
For example, y ~ normal(0, 1) T[-1, 1]
However, if my code is not in the form as above, but rather in the form of target += normal_lpdf(y| 0,1) - how do I specify the truncated distribution then?
In for a penny, in for a pound, as they say. If you’re incrementing target manually then you have to do the appropriate increment for truncations. But I would only do that where they matter. In this case,
y ~ normal(0, 1) T[-1, 1];
because the parameters to the normal distribution are constant here, the truncations have no effect assuming y is appropriately constrained to (-1, 1). As far as sampling goes, the above is equivalent to the following.
y ~ normal(0, 1);
Now if you have normal(mu, sigma) and mu and/or sigma are parameters, then the truncation is no longer constant but depends on the non-constant parameters. In that case, you need to add the effect of truncation manually, which just standardizes by a difference in cdfs.
where \text{normal}^\text{pdf} is the probability density function for the normal and \text{normal}^\text{cdf} is the cumulative distribution function. We’re just working above on the log scale and trying to keep things stable, where
\text{logDiffExp}(a, b) = \log \left(\exp(a) - \exp(b)\right).
If you want the normalization constants in the normal, then replace _lupdf with _lpdf.