[Rcpp-devel] more on stats functions

Christian Gunning xian at unm.edu
Thu Dec 9 04:54:50 CET 2010


On Wed, Dec 8, 2010 at 1:18 AM, Romain Francois
<romain at r-enthusiasts.com> wrote:
>
> It might make sense to get rid of the log = argument and maybe add a ldnorm,
> or log_dnorm, or dnorm_log instead. so that there is no ambiguity.

One nice part about the current setup is that R-exts.pdf chapter 6
serves as a (mostly) definitive guide for arguments and behavior,
since the Rf_* functions are at the back-end. The only d* example I
found that didn't work (given that all non-missing scalar arguments
were doubles) was:

wrong = cxxfunction(signature(x='numeric'),
  'NumericVector xx(x) ;
  return( xx = dunif( xx, true ));',
  plugin='Rcpp'
)

vv <- seq(0, 1, by = 0.1)
identical(wrong(vv), dunif(vv, log=TRUE))

>
> It would be great to recycle this effort of yours into testing. Can you have
> a look at the runit.stats.R file and add some test cases.

See attached for patch with some added d* cases.  Patch is relative to
rcpp/pkg/Rcpp/inst/unitTests.

Is there a convenient way to run only and all of the tests in
runit.stats.R?  Other than running *all* the Rcpp unit tests, the best
I found was doing individual tests with:

rm(.rcpp.stats)
rm(.setUp)
source('~/src/rcpp/pkg/Rcpp/inst/unitTests/runit.stats.R')
.setUp()
test.stats.dnorm()

best,
xian
-- 
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
-------------- next part --------------
Index: runit.stats.R
===================================================================
--- runit.stats.R	(revision 2747)
+++ runit.stats.R	(working copy)
@@ -47,8 +47,14 @@
 				  signature( x = "numeric" ),
 				  '
 				  NumericVector xx(x) ;
-				  return List::create(_["NoLog"] = dunif( xx, 0.0 , 1.0 ),
-									  _["Log"]	= dunif( xx, 0.0, 1.0 , true ));
+				  return List::create(
+                                      _["NoLog_noMin_noMax"] = dunif( xx ),
+                                      _["NoLog_noMax"] = dunif( xx, 0.0 ),
+                                      _["NoLog"] = dunif( xx, 0.0 , 1.0 ),
+									  _["Log"]	= dunif( xx, 0.0, 1.0 , true ),
+									  _["Log_noMax"]	= dunif( xx, 0.0, true )
+                                    //,_["Log_noMin_noMax"]	= dunif( xx, true )
+                  );
 				  ')
 
 				  ,
@@ -56,7 +62,9 @@
 				  '
 				  NumericVector xx(x) ;
 				  return List::create(_["NoLog"] = dgamma( xx, 1.0, 1.0),
-									  _["Log"]	 = dgamma( xx, 1.0, 1.0, true ));
+									  _["Log"]	 = dgamma( xx, 1.0, 1.0, true ),
+									  _["Log_noRate"]	 = dgamma( xx, 1.0, true )
+                  );
 				  ')
 
 				  ,
@@ -73,8 +81,13 @@
 				  signature( x = "numeric" ),
 				  '
 				  NumericVector xx(x) ;
-				  return List::create(_["false"] = dnorm( xx, 0.0, 1.0 ),
-									  _["true"]	 = dnorm( xx, 0.0, 1.0, true ));
+				  return List::create(
+                                      _["false_noMean_noSd"] = dnorm( xx ),
+                                      _["false_noSd"] = dnorm( xx, 0.0  ),
+                                      _["false"] = dnorm( xx, 0.0, 1.0 ),
+									  _["true"]	 = dnorm( xx, 0.0, 1.0, true ),
+									  _["true_noSd"]	 = dnorm( xx, 0.0, true ),
+									  _["true_noMean_noSd"]	 = dnorm( xx, true ));
 				  ')
 
 				  ,
@@ -240,7 +253,8 @@
     vv <- seq(0, 1, by = 0.1)
     a <- 0.5; b <- 2.5
     checkEquals(fx(vv, a, b),
-                list(NoLog = dbeta(vv, a, b),
+                list(
+                     NoLog = dbeta(vv, a, b),
                      Log   = dbeta(vv, a, b, log=TRUE)
                      ),
                 msg = " stats.qbeta")
@@ -248,17 +262,25 @@
 
 test.stats.dbinom <- function( ){
 	fx <- .rcpp.stats$runit_dbinom
-	checkEquals(fx(1:10) ,
-                list( false = dbinom(1:10, 10, .5), true = dbinom(1:10, 10, .5, TRUE ) ),
-                msg = "stats.dbinom" )
+    v <- 1:10
+	checkEquals(fx(v) ,
+                list( 
+                    false = dbinom(v, 10, .5), 
+                    true = dbinom(v, 10, .5, TRUE ) 
+                ), msg = "stats.dbinom" )
 }
 
 test.stats.dunif <- function() {
     fx <- .rcpp.stats$runit_dunif
     vv <- seq(0, 1, by = 0.1)
     checkEquals(fx(vv),
-                list(NoLog = dunif(vv),
-                     Log   = dunif(vv, log=TRUE)
+                list(
+                    NoLog_noMin_noMax = dunif(vv),
+                    NoLog_noMax = dunif(vv, 0),
+                    NoLog = dunif(vv, 0, 1),
+                     Log   = dunif(vv, 0, 1, log=TRUE),
+                     Log_noMax   = dunif(vv, 0, log=TRUE)
+                     #,Log_noMin_noMax   = dunif(vv, log=TRUE)  ## wrong answer
                      ),
                 msg = " stats.dunif")
 }
@@ -267,32 +289,42 @@
     fx <- .rcpp.stats$runit_dgamma
     v <- 1:4
     checkEquals(fx(v),
-                list( NoLog = dgamma(v, 1.0, 1.0), Log = dgamma(v, 1.0, 1.0, log = TRUE ) ),
-                msg = "stats.dgamma" )
+                list( NoLog = dgamma(v, 1.0, 1.0), 
+                      Log = dgamma(v, 1.0, 1.0, log = TRUE ),
+                      Log_noRate = dgamma(v, 1.0, log = TRUE ) 
+                ), msg = "stats.dgamma" )
 }
 
 
 test.stats.dpois <- function( ){
 	fx <- .rcpp.stats$runit_dpois
-	checkEquals(fx(0:5) ,
-                list( false = dpois(0:5, .5), true = dpois(0:5, .5, TRUE ) ),
-                msg = "stats.dpois" )
+    v <- 0:5
+	checkEquals(fx(v) ,
+                list( false = dpois(v, .5), 
+                      true = dpois(v, .5, TRUE ) 
+                ), msg = "stats.dpois" )
 }
 
 test.stats.dnorm <- function( ) {
     fx <- .rcpp.stats$runit_dnorm
     v <- seq(0.0, 1.0, by=0.1)
     checkEquals(fx(v),
-                list( false = dnorm(v, 0.0, 1.0), true = dnorm(v, 0.0, 1.0, TRUE ) ),
-                msg = "stats.dnorm" )
+                list( false_noMean_noSd = dnorm(v), 
+                      false_noSd = dnorm(v, 0.0), 
+                      false = dnorm(v, 0.0, 1.0), 
+                      true = dnorm(v, 0.0, 1.0, log=TRUE ),
+                      true_noSd = dnorm(v, 0.0, log=TRUE ),
+                      true_noMean_noSd = dnorm(v, log=TRUE ) 
+                ), msg = "stats.dnorm" )
 }
 
 test.stats.dt <- function( ) {
 	fx <- .rcpp.stats$runit_dt
     v <- seq(0.0, 1.0, by=0.1)
     checkEquals(fx(v),
-                list( false = dt(v, 5), true = dt(v, 5, log=TRUE ) ), # NB: need log=TRUE here
-                msg = "stats.dt" )
+                list( false = dt(v, 5), 
+                      true = dt(v, 5, log=TRUE ) # NB: need log=TRUE here
+                ), msg = "stats.dt" )
 }
 
 test.stats.pbeta <- function( ) {


More information about the Rcpp-devel mailing list