2D convolution in stan

Dear Stan community,

first of all thanks for providing this amazing tool. I’ve been using Stan for some time now, but would not consider myself to be an expert. For one of the projects that I am working on, my measurement data is connected to latent parameters through a 2D convolution. I’d like to make this convolution a proper part of my Stan program (instead of pre-coding it as a matrix multiplication) to eventually make the convolution filter (or aspects of it, like the scale) part of the parameters.

So far I couldn’t find a fast convolution implementation in Stan and I imagine that coding a 2D convolution in the Stan language itself will be rather slow. Before I venture down this road I wanted to ask, whether 2D convolutions are available in the Stan language (perhaps in the C++ code? maybe even opencl?) and I just missed them. I found some discussion about including FFTs in Stan, but from what I can tell they have not been included in the modelling language yet?

Thank you!

1 Like

I am pretty sure there is nothing currently in the Stan language. But I would start with a Stan implementation to see how slow it really is. Then maybe declare your Stan function but define the implementation in C++ to see if that is really faster. If so, you could then think about contributing something to the Stan Math library.

1 Like

Spoiler alert…I am not an expert in this…but fft are waiting for complex number support. For the moment I would try to cast this problem into an ode Problem which you can solve in Stan.

I’m working on complex number support. I hope to finish up the scalar bit today or tomorrow and finish the matrix support in one more day (it’s just some traits and tests). Then we have to think about how to expose FFTs to Stan before we have complex numbers in the language itself.

Many thanks for this! FFT-based convolution will be very useful for my (astronomical) models, and I look forward to having these available in Stan. Would it be possible to give an expected timescale before these become available?

I’m hoping to put in a version with real-based inputs and outputs for the real and imaginary parts of complex numbers so that we don’t have to wait for complex scalar and matrix data types. So hopefully the next release after the one that’s going on now, so maybe 3.5 months or so.