[ADMB Users] Define function in the TPL file RE: Question about R2admb

Ben Bolker bbolker at gmail.com
Fri Mar 28 10:46:53 PDT 2014


  I'm taking the liberty of cc'ing this back to the ADMB users' list, as
I really don't have time for a private conversation ...

  My suggestions don't work because they're naive -- as Dave F.
suggested, they're nondifferentiable, which doesn't work with ADMB.

  The function defined below _is_ differentiable.

  I would probably recommend a logistic curve (1/(1+exp(b*(x-s))) where
s is your switchpoint (1.4) and b is a (large) tunable value, e.g. 10 or
greater.

 good luck
   Ben Bolker


On 14-03-27 10:42 PM, Ximing Xu wrote:
> Dear Professor Bolker,
> 
> I have tried the following ways you suggested  
> 
>  int thresh;
>   thresh = (deloy <=1.4 ? 1 : 0 );  // ternary operator
> and 
> thresh = (int)(deloy<=1.4)
> 
> which do not work. 
> 
> I also came cross the C++ source code for the posfun function in the ADMB website: 
> 
>  #include <fvar.hpp>
> 00013 
> 00032 dvariable posfun(const dvariable &x, const double eps, const prevariable& _pen)
> 00033 {
> 00034   dvariable& pen= (dvariable&) _pen;
> 00035   if (x>=eps)
> 00036   {
> 00037     return x;
> 00038   }
> 00039   else
> 00040   {
> 00041     //dvariable y=1.0-x/eps;
> 00042     pen+=.01*square(x-eps);
> 00043     return eps/(2-x/eps);
> 00044   }
> 00045 }
> 
> In the line 00035, there is a if statement (x>eps) where x is   defined by const dvariable &x.  The posfun can be 
> called in the TPL file and it works. 
> 
> Now I am wondering if I can define a similar function inside TPL file; if not, can we define the function in a 
> separate .cpp file and  call it in the TPL file?   Thank you very much. 
> 
> 
> Best,
> 
> Ximing
> 
> ________________________________________
> From: Ben Bolker <bbolker at gmail.com>
> Sent: Thursday, March 27, 2014 3:16 PM
> To: Ximing Xu
> Cc: users at admb-project.org
> Subject: Re: Question about R2admb
> 
> On 14-03-27 10:20 AM, Ximing Xu wrote:
>> Dear Professor Bolker,
>>
>>
>> I am using  R2admb  to fit a state-space model  with my own TPL file.
>>
>> In the TPL file ,  we have the following function:
>>
>>
>> SEPARABLE_FUNCTION void cond_nllo(const dvariable& deloy,const dvariable& del1y)
>>
>>   nll += kterm + 0.5*square(deloy);
>>   nll += kterm + 0.5*square(del1y);
>>
>>
>>
>> where kterm is some predefined constant, and nll is the objective funtion to be minimized.
>>
>>
>> Now I would like to change  nll to be
>>
>>
>> nll += kterm + 0.5*square(deloy)*(deloy<=1.4)   with a logical operator .
>>
>>
>> Then the TPL file  can not be compiled anymore. I also found that the mathematical operations
>>
>> such as + - * / work for deloy but the modulus operator % does not work on it.
>>
>>
>> Do you know if there is any way to solve this problem?    Thank you very much.
>>
>>
>>
>> Best,
>>
>>
>> Ximing
> 
>   I'm not 100% sure, but I strongly suspect this is a generic ADMB
> question rather than an R2admb-specific question -- that is, it would
> apply whether or not you were using the R2admb interface.  I'm taking
> the liberty of cc'ing it there. You can probably do something like
> 
>   int thresh;
>   thresh = (deloy <=1.4 ? 1 : 0 );  // ternary operator
> 
>  or perhaps
> 
>   thresh = (int)(deloy<=1.4)
> 
> (these are two standard C idioms for converting a logical expression to
> a 0/1 value).
> 
>   I don't know offhand why the modulus operator doesn't work, but
> possibly because it is a non-differentiable operator and thus hasn't
> been defined in ADMB?  (I searched the ADMB manual for "modulus" and for
> "%" used in this context and didn't find it ...
> 
>  If you try to compile your model outside of R (i.e. using the
> command-line tools rather than R2admb) and find it *does* work from the
> command line but not via R2admb, feel free to bug me again.
> 
> 
>   good luck,
> 
>   Ben Bolker
> 



More information about the Users mailing list