I am a fresher about Stan, especially Rstan. Recently, I have been thinking about a question which puzzled me a lot:

Suppose X\sim f(x) where f(x) is the probability density function of r.v. X, which does not have the usual distributions expressions, such as Normal, Gamma, but a complex expression. And now I want to sample some random values from this distribution. How can I finish this job?

In Rstan, I set the stan code like this:

parameters {
real x;
}
model {
x ~ f(x);
}

But it doesn’t work. Can anyone help me?
I am really appreciate for your kind help!!

Thanks, and I changed my Stan code based on your idea. However, some new problems arose~

You see that Y \sim exp(\lambda) where \lambda=3 in my Stan code.

### Trunk A
functions {
real uf_lpdf(real y) {
return (1/3 * exp(-(1/3) * y));
}
}

So equivalently, results with above code should be the same with the following code:

##Trunk B
functions {
real uf_lpdf(real y) {
return exponential(y | (1/3));
}
}

But, the code with Trunk A has some warnings:

1: There were 5150 divergent transitions after warmup. Increasing adapt_delta above 0.8 may help. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
2: Examine the pairs() plot to diagnose sampling problems

and the outcome is unbelievable:

mean se_mean sd 2.5% 25%
k 9.075119e+307 NaN Inf 5.540203e+306 4.726369e+307
lp__ 7.088200e+02 0.03 0.94 7.063000e+02 7.084500e+02

This is the probability density function, not the log probability density function. The unnormalised log probability density function of the exponential distribution with rate parameter \lambda = 3 is: