Skip to content

[Rcpp-devel] Error: unimplemented type 'double' in 'coerceToReal'

2 messages · Lee, Clarence, Dirk Eddelbuettel

#
Hi,

I'm running a MCMC estimation of a Bayesian model, and I'm running into a particular error during the MCMC in my likelihood function call.  I've implemented my likelihood function in C++ using RCpp, and I get this "unimplemented type" error:

Error in loglik.func(): unimplemented type 'double' in 'coerceToReal.'

The nasty thing about this is that it is very hard to replicate, and only occurs after a particular iteration in my MCMC.  While my function stalls when this errors occurs, all the other times it returns the correct values.  My specific question is: are there any particular functions in the Rcpp library that calls coerceToReal?"  I'm trying to understand what would cause Rcpp to throw an error like this (eg. memory leaks, mismatching of types...etc).  A Google search returns very few relevant results on this type of error.

I think I've isolated the problem area in my code to this function:

//////////////////////////////////////////
Rcpp::NumericVector pbivpois(arma::colvec x, arma::colvec y, arma::rowvec lambda, bool returnLog) {
     // Unlike the pbivpois function, this assumes that x is a vector, and does not error check.
     // Declare Internal Variables
     int N = x.n_rows;
     Rcpp::NumericVector logbp(N);
     Rcpp::NumericVector result(N);
     int x0 = 0;
     int y0 = 0;
     int xymin = 0;
     Rcpp::NumericVector i(2);
     arma::rowvec sums = arma::zeros<arma::rowvec>(3);
     double lambdaratio = 0;
     double maxsums = 0;
     double logsummation =0;

     for(int k = 0; k < N; k++) {
          x0 = x.row(k);
          y0 = y.row(k);

         //////////////////////////////////////////////////
         // I think these lines are causing the problem.

          xymin = std::min(x0, y0);
          lambdaratio = lambda.col(2)/(lambda.col(0)*lambda.col(1));
          i = Rcpp::seq(0,xymin);
          sums = -Rcpp::lgamma(x0 - i + 1)-Rcpp::lgamma(i+1) - Rcpp::lgamma(y0-i+1) + i*std::log(lambdaratio);
          maxsums = sums.max();
          sums = sums-maxsums;
          logsummation = std::log(arma::accu(arma::exp(sums))) + maxsums;
          logbp[k] = -arma::accu(lambda) + x0*std::log(lambda.col(0)) + y0*std::log(lambda.col(1)) + logsummation;

////////////////////////////////////
     }

     if( returnLog == TRUE ) {
          result = logbp;
     } else {
          result = Rcpp::exp(logbp);
     }

     return(result);
}
////////////////////////////////////////////////

Thanks for your help!

Best,

Clarence


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110625/2b4d52d2/attachment.htm>
1 day later
#
Hi Clarence,

Thanks for posting here on the list.
On 25 June 2011 at 15:36, Lee, Clarence wrote:
| I'm running a MCMC estimation of a Bayesian model, and I'm running into a
| particular error during the MCMC in my likelihood function call.  I?ve
| implemented my likelihood function in C++ using RCpp, and I get this
| ?unimplemented type? error:
| 
|  
| 
| Error in loglik.func(): unimplemented type ?double? in ?coerceToReal.?

That didn't ring a bell as a identifier of ours, and indeed, it isn't used by
us:

~/svn/rcpp/pkg$ grep -rin coerceToReal .
~/svn/rcpp/pkg$ 

So sorry, we will have to pass.
 
| The nasty thing about this is that it is very hard to replicate, and only
| occurs after a particular iteration in my MCMC.  While my function stalls when
| this errors occurs, all the other times it returns the correct values.  My
| specific question is: are there any particular functions in the Rcpp library
| that calls coerceToReal??  I?m trying to understand what would cause Rcpp to

I do not believe so.

| throw an error like this (eg. memory leaks, mismatching of types?etc).  A
| Google search returns very few relevant results on this type of error. 
| 
|  
| 
| I think I?ve isolated the problem area in my code to this function:
| 
|  
| 
| //////////////////////////////////////////
| 
| Rcpp::NumericVector pbivpois(arma::colvec x, arma::colvec y, arma::rowvec
| lambda, bool returnLog) {
|      // Unlike the pbivpois function, this assumes that x is a vector, and does
| not error check.  
|      // Declare Internal Variables
|      int N = x.n_rows;          
|      Rcpp::NumericVector logbp(N);
|      Rcpp::NumericVector result(N);
|      int x0 = 0;
|      int y0 = 0;
|      int xymin = 0;
|      Rcpp::NumericVector i(2);
|      arma::rowvec sums = arma::zeros<arma::rowvec>(3);
|      double lambdaratio = 0;
|      double maxsums = 0;
|      double logsummation =0;
|                               
|      for(int k = 0; k < N; k++) {
|           x0 = x.row(k);
|           y0 = y.row(k);
| 
|  
| 
|          //////////////////////////////////////////////////
| 
|          // I think these lines are causing the problem.
| 
| 
|           xymin = std::min(x0, y0);
|           lambdaratio = lambda.col(2)/(lambda.col(0)*lambda.col(1));
|           i = Rcpp::seq(0,xymin);           
|           sums = -Rcpp::lgamma(x0 - i + 1)-Rcpp::lgamma(i+1) - Rcpp::lgamma
| (y0-i+1) + i*std::log(lambdaratio);
|           maxsums = sums.max();
|           sums = sums-maxsums;     
|           logsummation = std::log(arma::accu(arma::exp(sums))) + maxsums;
|                     
|           logbp[k] = -arma::accu(lambda) + x0*std::log(lambda.col(0)) +
| y0*std::log(lambda.col(1)) + logsummation;

You could try replacing the lgamma calls (which we pass on to the R API) with
something from another library.

And/or extract this inner loop and just try to 'blast' it through 1e7
iterations or some other stress test.

Lastly, valgrind may be of help too.

Sorry, but I can offer no better help at this point.

Cheers, Dirk
 
|  
| 
| ////////////////////////////////////
|      }
| 
|      if( returnLog == TRUE ) {
|           result = logbp;
|      } else {
|           result = Rcpp::exp(logbp);
|      }
|      
|      return(result);  
| }
| 
| ////////////////////////////////////////////////
| 
| 
| Thanks for your help!
| 
|  
| 
| Best,
| 
|  
| 
| Clarence
| 
|  
| 
|  
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel