How to run integrate() with >1 pre-calculated values

39 views
Skip to first unread message

Maia Sosa Kapur

unread,
Oct 3, 2019, 3:23:19 PM10/3/19
to TMB Users
I'm working with the integrate() function with a uni and multivariate example given here:

However, I need to have the returned function accept and perform a calculation based on more than just one input (in this case theta). These don't need to all be estimated parameters, but I am unsure of the syntax for correctly writing this. I've created a dummy example based on the above, wherein I am hoping to compute the integral using function f based on not only theta but a data input called "m". Ultimately I'd like to be able to calculate quantities within my objective function and pass them to integrate(). Currentlywhen compiling the error "no match for call to '(univariate)..." comes up.

I'd appreciate some guidance!

(BTW, I am aware that hard-coding a number in lieu of "m" seems to work, but since the value I plan to input here will change on the fly I'd like it to take a variable).

Thanks!


#include <TMB.hpp>
template<class Type>
  struct univariate {
    Type theta;               // Parameter in integrand
    univariate(Type theta_, Type m_)   // Constructor of integrand -- how do I get this to anticipate more values than theta and x?
    : theta (theta_) {}       // Initializer list
    Type operator()(Type x, Type m){  // Evaluate integrand,  unsure about this syntax
      return exp( -theta * m * (x * x) ); // I want to be able to have several values in this equation, some parameters and some calculated previously
    }
  };


template<class Type>
  Type objective_function<Type>::operator() () {
    DATA_SCALAR(a);
    DATA_SCALAR(b);
    DATA_INTEGER(m); // testing
    PARAMETER(theta);
    univariate<Type> f(theta, m); // unsure about this syntax
    Type res = romberg::integrate(f, a, b);
    return res;
  }

Kasper Kristensen

unread,
Oct 4, 2019, 5:15:55 AM10/4/19
to TMB Users
You have to declare all parameters and data objects in the struct and write an initializer (CTOR) of these. The objects become visible to the evaluation method ('operator()') without passing them as function arguments.
Try this:


#include <TMB.hpp>
template<class Type>
struct univariate {
  Type theta;               // Parameter in integrand
  Type m;

  univariate(Type theta_, Type m_)   // Constructor of integrand -- how do I get this to anticipate more values than theta and x?
    : theta (theta_), m (m_) {}       // Initializer list
  Type operator()(Type x){  // Evaluate integrand,  unsure about this syntax

Maia Sosa Kapur

unread,
Oct 4, 2019, 12:41:16 PM10/4/19
to TMB Users
Thanks, I was able to make it work by following the example here:
Where it became clear I could declare new values within struct. Thanks for your help!
MK
Reply all
Reply to author
Forward
0 new messages