Is there a function in Stan analogous to the convolve() function in R?

98 views
Skip to first unread message

Jon Fawcett

unread,
Jan 15, 2017, 3:16:27 PM1/15/17
to Stan users mailing list
Dear User Group,

I am trying to fit a model in Stan that views a particular time-series as arising from the convolution of several “pulses” of varying strength along with a particular response function. As a starting point, I am interested in recreating the analysis reported here by another research tea, (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3365158/) with original scripts in R provided here (http://www.pnas.org/content/suppl/2012/05/11/1201858109.DCSupplemental/sd02.txt and http://www.pnas.org/content/suppl/2012/05/11/1201858109.DCSupplemental/sd03.txt). I have an intuition as to how I should proceed, but a critical step in the original R calculations involves the command:

convolve(i,rev(h),type="open")

Where i represents the pulses and h represents the response function. Does Stan have an equivalent command to convolve(x, y, type='open')? I have been unable to find one.

Cheers!
Jon

Ben Goodrich

unread,
Jan 15, 2017, 3:57:53 PM1/15/17
to Stan users mailing list
On Sunday, January 15, 2017 at 3:16:27 PM UTC-5, Jon Fawcett wrote:
Does Stan have an equivalent command to convolve(x, y, type='open')? I have been unable to find one.

If there were one, it would be returned by

rstan::lookup("convolve")

Ben

Bob Carpenter

unread,
Jan 15, 2017, 5:41:09 PM1/15/17
to stan-...@googlegroups.com
We don't have any Fourier transform functions available
from within the Stan language. We've been planning to
add the basic FFT because there's an implementation in
Eigen (which we, by the way, use for autocorrelation estimates
as part of effective sample size estimation).

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jon Fawcett

unread,
Jan 15, 2017, 6:40:38 PM1/15/17
to Stan users mailing list
Thanks for weighing in, Ben and Bob!
I should have specified more clearly that I had already searched for functions named convolve or any obvious sign of there being such a function and found nothing. I was checking to see whether there were perhaps something less obvious I missed. From your answers, I gather that there is not at this time. Do you know when FFT will be implemented? Or is that low on the list?

Bob Carpenter

unread,
Jan 15, 2017, 7:14:39 PM1/15/17
to stan-...@googlegroups.com
I think Thel may implement the discrete FFT very soon (maybe in
the next couple of months). I don't think there's anything standing
in the way.

- Bob

Mike Lawrence

unread,
Jan 15, 2017, 8:06:09 PM1/15/17
to stan-...@googlegroups.com
I believe that the purpose to which convolve was used in that model was merely to prepare the predictor matrix. You can create that matrix yourself outside Stan, where each column is the response function shifted by a different amount for each column. You then do a standard regression and the weights associated with each column reflect the evidence for a pulse at the time-point corresponding to the shift of that column. 

--
Mike Lawrence
Graduate Student
Department of Psychology & Neuroscience
Dalhousie University

~ Certainty is (possibly) folly ~

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Mike Lawrence

unread,
Jan 16, 2017, 8:56:38 AM1/16/17
to stan-...@googlegroups.com

Note: an improvement on the model could include modeling the column coefficients as a Gaussian Process rather than independent weights


To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

Sebastian Weber

unread,
Jan 20, 2017, 5:33:49 AM1/20/17
to Stan users mailing list
Hi!

Depending on the details of the problem you could abuse the ode solver to do the integration...just a thought.

Sebastian

Bob Carpenter

unread,
Jan 20, 2017, 12:58:08 PM1/20/17
to stan-...@googlegroups.com
This wasn't enough for me to understand the function:

each column is the response function shifted by a different amount
for each column.

Can you define convolve() as a function in Stan itself? If
so, it'll be easy for us to add to the language itself.

- Bob
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

Mike Lawrence

unread,
Jan 30, 2017, 11:42:31 AM1/30/17
to stan-...@googlegroups.com
I made up an example data set and analysis for this. See the attached "pulse.R", which generates a fake data set comprised of the additive combination of a single reference function shifted to varying degrees plus noise. The analysis takes the resulting timeseries and successfully retrieves the three shift values used to make the data.




On Fri, Jan 20, 2017 at 1:58 PM, Bob Carpenter <ca...@alias-i.com> wrote:
This wasn't enough for me to understand the function:

  each column is the response function shifted by a different amount
  for each column.

Can you define convolve() as a function in Stan itself?  If
so, it'll be easy for us to add to the language itself.

- Bob

> On Jan 15, 2017, at 8:05 PM, Mike Lawrence <mike....@gmail.com> wrote:
>
> I believe that the purpose to which convolve was used in that model was merely to prepare the predictor matrix. You can create that matrix yourself outside Stan, where each column is the response function shifted by a different amount for each column. You then do a standard regression and the weights associated with each column reflect the evidence for a pulse at the time-point corresponding to the shift of that column.
>
> --
> Mike Lawrence
> Graduate Student
> Department of Psychology & Neuroscience
> Dalhousie University
>
> ~ Certainty is (possibly) folly ~
>
> On Sun, Jan 15, 2017 at 4:16 PM, Jon Fawcett <jmfa...@gmail.com> wrote:
> Dear User Group,
>
> I am trying to fit a model in Stan that views a particular time-series as arising from the convolution of several “pulses” of varying strength along with a particular response function. As a starting point, I am interested in recreating the analysis reported here by another research tea, (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3365158/) with original scripts in R provided here (http://www.pnas.org/content/suppl/2012/05/11/1201858109.DCSupplemental/sd02.txt and http://www.pnas.org/content/suppl/2012/05/11/1201858109.DCSupplemental/sd03.txt). I have an intuition as to how I should proceed, but a critical step in the original R calculations involves the command:
>
> convolve(i,rev(h),type="open")
>
> Where i represents the pulses and h represents the response function. Does Stan have an equivalent command to convolve(x, y, type='open')? I have been unable to find one.
>
> Cheers!
> Jon
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
pulse.R
pooled_regression.stan

Bob Carpenter

unread,
Jan 30, 2017, 12:54:52 PM1/30/17
to stan-...@googlegroups.com
Thanks for sharing.

- Bob

P.S. Those semicolons look so lonely being separated from their statements!
> > To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> > To post to this group, send email to stan-...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> >
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> > To post to this group, send email to stan-...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> <pulse.R><pooled_regression.stan>

Christopher Fisher

unread,
Feb 5, 2017, 5:08:32 PM2/5/17
to Stan users mailing list
I came across this post while searching for something similar to convpow() in the distr package, which is used for efficiently convolving random variables. So I will chime in and say this could also be useful for my purposes as well. 

Best regards,

Chris 

Bob Carpenter

unread,
Feb 6, 2017, 2:33:08 AM2/6/17
to stan-...@googlegroups.com
Thanks --- we're working on it (or I should say, Thel's
working on it).

- Bob
Reply all
Reply to author
Forward
0 new messages