# Interfacing Stan with an external program

**URL:** <https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092>\
**Category:** General\
**Created:** [April 9, 2022, 1:44pm UTC](https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092 "2022-04-09T13:44:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpferreira](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@jpferreira](https://discourse.mc-stan.org/u/jpferreira)\
**Post date:** [April 9, 2022, 1:44pm UTC](https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092/1 "2022-04-09T13:44:40Z")

</div>

Hi there,

I am a very basic Stan user who’s been using it for constraining cosmological models for a couple of months now.  
My challenges so far have been quite simple, taking a dataset with a x, f(x) and an error σ, and compare it with my model which as an analytical expression for f(x), which I have coded in Stan, using a Gaussian likelihood.

Now, I am looking to extend my analysis using a function which cannot be easily coded in Stan, in fact, most people in my field don’t code it either as there are already pre-existing programs optimized to perform such calculations, as they are quite expensive.  
In this case, the program which I am looking to use, which is called [CLASS](http://class-code.net/), is written in C, can be called directly from the terminal given a configuration file, and features both a Python and C++ wrapper.  
I know that Stan can work with external C++ functions, but this is a wrapper for a C program which I have no idea how it works internally.

Considering that Stan has showed to perform significantly well, I would like to know how hard could it be to have Stan use the output of this program has its model, instead of a function coded in a .stan model file, consider its available wrappers.

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [April 9, 2022, 2:37pm UTC](https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092/2 "2022-04-09T14:37:40Z")

</div>

Here is one example, but you would need to do this manually with cmdstan or maybe with rstan (or pystan2)

[https://dfm.io/posts/stan-c++/](https://dfm.io/posts/stan-c++/)

---

<div class="post-metadata">

**Author:** ![jpferreira](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@jpferreira](https://discourse.mc-stan.org/u/jpferreira)\
**Post date:** [April 9, 2022, 2:56pm UTC](https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092/3 "2022-04-09T14:56:31Z")

</div>

Maybe I should have showed what I had already looked for, because I already came across that post (interestingly, by an astronomer), unfortunately the Github link seems to be dead.

I was also concerned about using pystan2, which has a lot more feature of pystan, which I’m currently using, yet I thought it was deprecated. Should I be using pystan2?

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [April 9, 2022, 5:52pm UTC](https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092/4 "2022-04-09T17:52:08Z")

</div>

Here are old docs

[https://pystan2.readthedocs.io/en/latest/](https://pystan2.readthedocs.io/en/latest/)

And old github

> **[GitHub - stan-dev/pystan2: PyStan, the Python interface to Stan](https://github.com/stan-dev/pystan2)**
>
> PyStan, the Python interface to Stan. Contribute to stan-dev/pystan2 development by creating an account on GitHub.

I would not recommend using pystan2 but doing the same steps manually with CmdStan. (I think cmdstanpy might not work too well with this workflow, but I might be wrong cc @mitzimorris @WardBrian )

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [April 9, 2022, 9:03pm UTC](https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092/5 "2022-04-09T21:03:56Z")

</div>

[https://mc-stan.org/cmdstanpy/examples/Using%20External%20C%2B%2B.html](https://mc-stan.org/cmdstanpy/examples/Using%20External%20C%2B%2B.html)

It is possible, but there are a few extra steps (such as placing the function in a specific namespace) detailed there

---

<div class="post-metadata">

**Author:** ![jpferreira](https://avatars.discourse-cdn.com/v4/letter/j/f04885/32.png) [@jpferreira](https://discourse.mc-stan.org/u/jpferreira)\
**Post date:** [April 15, 2022, 3:06pm UTC](https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092/6 "2022-04-15T15:06:15Z")

</div>

Thank you for the links.  
I wonder if such a feature is more easily implemented in RStan, as was mentioned before, because if so, I wouldn’t mind giving R a try.

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aakhmetz/32/18672_2.png) [@aakhmetz](https://discourse.mc-stan.org/u/aakhmetz)\
**Post date:** [June 15, 2024, 1:19am UTC](https://discourse.mc-stan.org/t/interfacing-stan-with-an-external-program/27092/7 "2024-06-15T01:19:07Z")

</div>

I am sorry, but may I ask a quick programming question?

Why do we need to declare the function twice in the functions block?

```no-highlight
  int fib(int n);
  int fib(int n) { ... }

```
