Native Matlab Stan?

Hi,

I’d like to use Stan under Matlab, but I’m unhappy with the idea of working through CmdStan and passing data back and forth as files. This has been a headache using MatJags, for instance, when data or sampling gets big.

I’m wondering if anyone has thought of porting Stan into native Matlab. It’s possible to build an interface to Matlab on top of any C++ program, basically, so that functions can be called directly from Matlab. There are also compile issues with CmdStan under Windows, so I feel like I need to get involved with understanding the code in any case.

It would be great if anyone would be interested in helping me figure out what needs to be done to build a Stan interface or telling me where I should start reading, and if people would be willing to let me now what I should be thinking about to figure out if the project is feasible.

Thanks!
Opher

@opherdonchin, welcome! It’s definitely possible and CmdStan lays out how to do it. That really isn’t much help, so I’ll try to break it down:

  1. You’ll want to write a Matlab-specific stan::io::var_context implementation that can read from Matlab’s data structures and expose it in C++. stan::io::var_context is an interface (technically, an abstract class). You’ll find the source for var_context here in stan-dev/stan. Here are the three different implementations:

    These are all immutable, so should be safe. You’ll want to do something similar that maps to Matlab’s memory.

  2. You’ll need some way of building the Stan compiler. This is straightforward, but not simple. We can discuss more in depth what this entails. Essentially, you’ll want to have the exceptions go directly to Matlab and this will output a .hpp file from a .stan input.

  3. You’ll have to instantiate the .hpp file and build some sort of library, shared or linked. That should be easier than the second step.

  4. The last bit is how to control the algorithms from Matlab. It’ll look something like this: CmdStan’s command.hpp

I think the hardest part would be the first step, but the build could also prove to be a challenge. Happy to help you figure it out (as in provide guidance from the outside, but not really code it up). I also don’t have a Matlab license.

3 Likes

In future, Matlab could use httpstan as a backend.

Just send a request and read output.

1 Like

This is super-helpful.

  1. I’ll take a look at the different var_context implementations. It should be pretty trivial in Matlab, and I could at least think about what’s entailed and check to see if I’m on the right track.

Would I be best forking off the Python implementation for starters because it’s probably closest in spirit to what I’ll be doing in Matlab? Or perhaps the R implementation because it’s the most complete right now?

2+3. Matlab has the C++ compilation in its native command set that can produce Matlab commands (.mex files) or libraries, so perhaps I can use that for the Stan compilation? That way, the exceptions would naturally return to Matlab?

  1. I’ll look at this, too, but it’s basically just building pass through functions so it should be pretty easy, right?

I’ll take some first steps to look at things and get back, if you’re willing to keep giving advice. Would it be better to keep the discussion alive here in the forum or contact you directly?

Thanks so much,
Opher!!

Using httpstan would have the same disadvantage as CmdStan, wouldn’t it? You’d have to transfer data using files that would have to be written out and read in, with extra processes being forked and all of the associated inconveniences and lack of transparency. I find this makes matjags very annoying for anything but toy projects, and I’m hoping for something that feels more “native.”

Opher

No files transferred. It sends a json and gets in return output as a json.

What compiler matlab uses on Windows? MSVC is not compatible (atleast MSVC >= 14) with Stan.

Does the stan code have a test suite of some sort so that I could check compilation and test specific parts of the code as I build them?

Opher

The JSON interface does seem cool and portable. Matlab has functions to encode and decode json.

As far as compilers go, the following page suggests that mingw should play well with Matlab:

https://www.mathworks.com/support/compilers.html

Opher

Are you worried about the ASCII being a bit larger than binaries? Usually we haven’t found disk size or speed to be a bottlneck—quite the opposite, memory can be a bottleneck saving output in memory.

Those problems only get harder to fix when embedding in a second system.