Beginner, Forward model and calling external function

#1

Dear All,

This is my first time with STAN (PySTAN) and before investing my time to learn seriously, I would like to kindly ask you following questions:

1. I calculate the data using a code (Forward Model) written in Python, which include one main code and two subroutines used in main code. I suppose I can’t call my forward model code directly in the model block. In this case, my question is: Do I have to re-write my forward model code ? should it be in STAN language ?

2. If I end up writing the code in the language suggested in (1.), How should I call it in the model block ?

3. If there are examples explaining something I asked, Kindly refer those also.

Looking forward for your kind replies. I will appreciate your great help.

Thanking You

Sincerely
Mahak

0 Likes

#2

RStan has the cleanest interface for doing something like this: https://cran.r-project.org/web/packages/rstan/vignettes/external.html

The code has to be written in C++ to link to everything else though. I don’t know about Pystan. The other question is the autodiff. Do you want to let autodiff compute your gradients or do you want do specify them manually?

There’s various threads around here with people doing this: Problem implementing Zipf (power law) distribution -- external C++ or What is the best way to add a user function that is used in likelihood calculation?

Welcome to the forums, btw!

0 Likes

#3

Thank you very much for your kind reply.

As a beginner, I will prefer to use autodiff for the calculations of gradients.

So, if I consider the option 1 of the second thread you suggested, means writing my forward model code in the STAN language. I will be able to call it in the model block, right?

0 Likes

#4

Well that thread is for writing your model in C++ and then calling that from Stan. If you can write your model in Stan directly though, just do that. Head over the manual for examples of what Stan functions can do: https://mc-stan.org/docs/2_19/stan-users-guide/basic-functions-section.html

0 Likes

#5

Alright! I am going for it. Thank You very much for your suggestions!

0 Likes

#6

It probably can be done, but needs some source code manipulation.

https://dfm.io/posts/stan-c++/

In ideal case python code could be translated to C++ e.g. with pythran and then linked to Stan, but I have not tested this myself.

2 Likes

#7

Dear all
The following STAN code giving me the error. For me it looks ok. I was trying to learn how to use function so that I can go further.

function{
    real myline_log(real m, vector x, real c){
        return m*x+c;
    }
}

data{
    int<lower=0> N;
    vector[N] x;
    vector[N] y;
}

parameters {
    real alpha;
    real beta;
}

model {
    y ~ myline(beta,x,alpha);
} 

Error

ValueError: Failed to parse Stan model 'anon_model_b11665170a1781410689a7afd516a0bb'. Error message:
PARSER EXPECTED: whitespace to end of file.
FOUND AT line 2: 
function{
    real myline_log(real m, vector x, real c){
        return m*x+c;
    }
}

data{
    int<lower=0> N;
    vector[N] x;
    vector[N] y;
}

parameters {
    real alpha;
    real beta;
}

model {
    y ~ myline(beta,x,alpha);
}
0 Likes

#8

Sorry, it was my mistake. I was writing function instead functions. Now that error disappeared.

1 Like