I am trying draw samples from a userdefined pdf with HMC algorithm, but there is something wrong in my code

Hello, I am new to the stan, I trying to draw samples from the userdefined log pdf. But the fit I got is really nonesense. I am wondering what are mistakes I made in my code. Could any one help me with this?

functions {

real DSPwithWL_lpdf(vector beta, matrix cmatrix, vector y) {

return -sum(beta+exp(y-beta)) + 0.5*(beta)'*cmatrix*beta;   //  the log  wittle likelihood with DSP



data {

int<lower=0> n; // the length of the vector

matrix[n,n] cmatrix; // x matrix n by n matrix

vector[n] y; // data y vector


parameters {

vector[n] beta; // coefficients


model {

target += DSPwithWL_lpdf(beta | cmatrix, y);


In what way is the fit β€œnonsense”?
The PDF you have defined diverges for large positive beta. Perhaps the term

+ 0.5*(beta)'*cmatrix*beta

should have a negative sign? Your model looks a lot like

model {
   beta ~ multi_normal(rep_vector(0.0, n), cmatrix);
   exp(y) ~ exponential(inv(beta));
1 Like

Hello, thank you so much for your reply.
Yes you are right! I made a mistake at + 0.5*(beta)’ cmatrix beta. Here the β€œ+” should be β€œ-”.

What I am doing is to combine the whittle likelihood with the Dynamic shrinkage prior. The log conditional posterior distribution is proportion to the formula below.

-sum(beta+exp(y-beta)) - 0.5*(beta)’ cmatrix beta

Here the -sum(beta+exp(y-beta)) represents the whittle likelihood and the - 0.5*(beta)’ cmatrix beta represents for the normal kernel.

I will make more tries and see will by correcting the mistake whether I can get the fit as I expected.