Is there a multidimensional integrate-function?

Hey guys,

for the new function I want to implement I need to solve a multidimensional integral. Is there a functor in stan that can do this? I only found integrate_1d.hpp in /prim/functor.

Isn’t there something for more than 1 dimensions like hcubature by Steven G. Johnson? Or is there a reason why there only exists a 1d-integrator?

If thereisn’t a multidimensional integrator, is it legitimate to implement this hcubature algorithm in Stan? Johnson’s algorithm can be found here: GitHub - stevengj/cubature: multi-dimensional adaptive integration (cubature) in C and is a free software under the terms of the GNU General Public License (v2 or later).

Best,
Franzi :)

I believe no. It is GNU GPLv2+ and for Stan-math to remain BSD licensed, it cannot use that license. Perhaps @Bob_Carpenter knows more, as I’ve seen him respond to licensing questions in the past.

From what I know (which is not a lot since I’m not a lawyer) is that Stan is maintained under a permissive license which allows a business or individual to package the code and sell it without having to open source any modifications. Sounds kinda generous, right? Well, it is! You’ll hear the term “GPLed” because it effectively means derivative works must be open-sourced. This can be good, if you want it to remain open-source. Or bad because you see your work as a public good for all to consume, build upon, and contribute toward and it limits the usage of your software.

Keep in mind Stan is an integration library. Depends on your case you may resort to parameterize the model in another way. See e.g. High dimensional interpolation of a high dimensional integral

3 Likes

Wait, I looked at the link at whatever was proposed there went straight over my head. How is what’s proposed in the thread done and how does it help with integration?

@Franzi Could you provide some more details? I think in principle you are free to implement whatever algorithm you like, and I think an adaptive quadrature such as the one you linked to should be doable, if somewhat weird to implement and potentially inefficient.

Hi @yizhang, sorry I am not sure whether I got your point or whether we are talking about the same issue… I want to develop a new function. I don’t want to use Stan for my model. And the function contains a multidimensional integral. Therefore, I need a Stan function with which I can solve the integral or I have to implement a routine that solves the integral.

Hi @Niko, what do you mean with ‘weird’ and ‘potentially inefficient’? Maybe I am fully on the wrong path how to compute an integral within Stan. It’s the first time that I deal with a function that contains an integral … how would you solve an integral in Stan? Is there a standard way?

Oh, with weird I meant mainly the inflexibility due to the inability to define user defined functions that take a Functor (in your case the integrand) as an argument. This would mean that you would have to copy your quadrature code for every different integrand.

With potentially inefficient I just meant that a C++ implementation will probably be more performant than a Stan-implementation. But writing a C++ implementation is also more difficult.

I don’t know whether there is a standard way to compute multidimensional integrals in Stan. If I had to compute multi- but low-dimensional integrals (say d <= 5) over a hypercube I’d try to get away with as little work as possible and just implement a multidimensional Gaussian quadrature and monitor and refine convergence as illustrated here in the context of ODEs.

What can you tell us about the integral? Is the integrand smooth? Do you integrate over a hypercube? Or over something unbounded? How many dimensions do you have?

1 Like

If the integral to be calculated is data or transformed data one can write a Stan model for that integral, with parameters being the variables that are lower and upper bounded with integration limits, and with integrand being the likelihood. Obviously the approach won’t work if the integral contains parameters.

1 Like

Hi Niko, my integral has 2 dimensions and both are bounded.

@Bob_Carpenter: Could you tell me more about the compatability of licenses in Stan?

I want to add my function to the stan-math library. I am not that firm with the different types of licenses yet and want to be sure not to do illegal stuff. Since Stan is BSD-3, I guess all components of my function have to be BSD-3. Now, I have a component that needs to solve an integral with two dimensions. I’d like to use a multidimensional integral-solver like hcubature for example. But hcubature is GPL2+. Then, I can’t implement hcubature in Stan-style - is that correct? Or is there a way I could still implement this?

To be clear - You can add whatever code you want to wherever you want in this case, so long as you don’t distribute it. On your private machine, you can absolutely, 100%, add GPL2 licensed code to some Stan code, and run your model. You just can’t then distribute that derived binary or source code in a license-compliant way. This isn’t a ‘skirting around’ of the GPL2, it’s actually how it’s written.

In their FAQs:

" Does the GPL require that source code of modified versions be posted to the public? (#GPLRequireSourcePostedPublic)

The GPL does not require you to release your modified version, or any part of it. You are free to make modifications and use them privately, without ever releasing them. This applies to organizations (including companies), too; an organization can make a modified version and use it internally without ever releasing it outside the organization.

But if you release the modified version to the public in some way, the GPL requires you to make the modified source code available to the program’s users, under the GPL.

Thus, the GPL gives permission to release the modified program in certain ways, and not in other ways; but the decision of whether to release it is up to you."

Basically, the GPL2 and 3 both state you are free to take GPL2/3 code, modify and use them however you want; add in any other code you want. But once you distribute code modified by GPL2/3, or distribute GPL2/3 licensed code, then it must follow the standards set by GPL2/3 code, and license compatibility requirements.

TLDR: You can, for your own private use, absolutely incorporate the GPL3 hcubature into Stan — Whether you can then distribute that modification publicly whatsoever is a trickier issue.

2 Likes

Thanks @Stephen_Martin, I think this is the point. I also want to distribute that change such that other people can also use this function.
Could you alsoelaborate on your point “is a trickier issue”. It sounds as if it was still possible to add a GPL compoment and publish the function unter BSD. How could I do this?

See here. Rstan & StanHeaders merger? A heads-up! - #26 by ducoveen

In addition if the function is to be proposed to add to Stan Math it’d be great to have a discussion with developers here first, as it helps with clearing legal ramifications & downstream PR review.

Hi @yizhang, ohhh, I’m sorry I didn’t start a discussion beforehand! I was not aware of it. I thought I had to implement my stuff and then come with a PR. I am so sorry!
I hope it’s not too late … how can I start a discussion with developers? Is it just opening a new topic or is there another channel where only developers participate?

No need to apologize, contributions are greatly appreciated. We just need to follow the process. If you plan to insert this into Stan/Math, please create an issue there. However I must say the incompatible license is a big hurdle.

Where do I find the right place Stan/Math to introduce my function? Is it an own category?

I am already working on a solution for the license problem.

The repo is here. You can create an issue and follow the template. Like discourse you can @ people who you believe would be interested in the issue.

1 Like

Ok, thanks! I’ll go that way =D

Right. We’re not going to include any copylefted code in Stan. It would be nice if we had an easy user plug-in mechanism so you could easily add it yourself, but we’re not there yet.

It is absolutely not legal to relicense GPL code as BSD. That’s the whole point of GPL—it has copyleft and can’t be loosened.

Stan’s license is BSD-3. That’s compatible with GPL meaning you can release a GPL package that includes GPL and BSD-3 code. But the copyleft drags everything down (see the link the wikipedia article above).

Exactly. Same with GPL3. You can even set up your extension on a server and sell the service. The AGPL is even more restrictive and closes the server gap so you can’t even do that.

But we’re still free to reimplement the same or a similar algorithm, aren’t we? It’s the code, not the algorithm that has a license, or is the algorithm licensed?