[Rcpp-commits] r3531 - in pkg/RcppSMC: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Mar 19 12:58:16 CET 2012


Author: adamj
Date: 2012-03-19 12:58:15 +0100 (Mon, 19 Mar 2012)
New Revision: 3531

Added:
   pkg/RcppSMC/R/simGaussian.R
   pkg/RcppSMC/R/simLineart.R
   pkg/RcppSMC/R/simNonlin.R
Modified:
   pkg/RcppSMC/ChangeLog
   pkg/RcppSMC/NAMESPACE
   pkg/RcppSMC/R/pfNonlinBS.R
   pkg/RcppSMC/man/blockpfGaussianOpt.Rd
   pkg/RcppSMC/man/pfLineartBS.Rd
   pkg/RcppSMC/man/pfNonlinBS.Rd
Log:
Adding data simulating functions and examples which employ them.


Modified: pkg/RcppSMC/ChangeLog
===================================================================
--- pkg/RcppSMC/ChangeLog	2012-03-19 09:57:59 UTC (rev 3530)
+++ pkg/RcppSMC/ChangeLog	2012-03-19 11:58:15 UTC (rev 3531)
@@ -1,3 +1,12 @@
+2012-03-19  Adam Johansen <a.m.johansen at warwick.ac.uk>
+	* R/simGaussian.R Added data-simulating function
+	* R/simLineart.R Idem
+	* R/simNonlin.R Idem
+	* man/blockpfGaussianOpt.Rd Added simulator doc and example usage.
+	* man/pfLineartBS.Rd Idem
+	* man/pfNonlinBS.Rd Idem
+	* NAMESPACE Added data-simulating functions to namespace
+
 2012-03-18  Dirk Eddelbuettel  <edd at debian.org>
 
 	* R/pfLineartBS.R: Adjust naming of the two helper functions

Modified: pkg/RcppSMC/NAMESPACE
===================================================================
--- pkg/RcppSMC/NAMESPACE	2012-03-19 09:57:59 UTC (rev 3530)
+++ pkg/RcppSMC/NAMESPACE	2012-03-19 11:58:15 UTC (rev 3531)
@@ -4,5 +4,8 @@
 export(blockpfGaussianOpt,
        pfLineartBS,
        pfLineartBSOnlinePlot,
-       pfNonlinBS)
+       pfNonlinBS,
+       simGaussian,
+       simLineart,
+       simNonlin)
 

Modified: pkg/RcppSMC/R/pfNonlinBS.R
===================================================================
--- pkg/RcppSMC/R/pfNonlinBS.R	2012-03-19 09:57:59 UTC (rev 3530)
+++ pkg/RcppSMC/R/pfNonlinBS.R	2012-03-19 11:58:15 UTC (rev 3531)
@@ -7,7 +7,8 @@
 
     time <- 1:length(data);
     if (plot) {
-      with(res, plot(time,mean,type='l',xlab='time',ylab='estimate',xlim = c(0,length(data)), ylim = c(min(mean-2.1*sd),max(mean+2.1*sd))))
+      with(res, plot(time,mean,type='l',main='Filtering Mean and +/- 1,2
+    standard deviation intervals', xlab='time',ylab='estimate',xlim = c(0,length(data)), ylim = c(min(mean-2.1*sd),max(mean+2.1*sd))))
       with(res,polygon(c(time,seq(length(data),1,-1)),c(mean-2*sd,(mean+2*sd)[seq(length(data),1,-1)]),col='palegreen1',border=NA))
       with(res,polygon(c(time,seq(length(data),1,-1)),c(mean-1*sd,(mean+1*sd)[seq(length(data),1,-1)]),col='palegreen3',border=NA))
       with(res,lines(time,mean, lwd=2, col='darkblue'))

Added: pkg/RcppSMC/R/simGaussian.R
===================================================================
--- pkg/RcppSMC/R/simGaussian.R	                        (rev 0)
+++ pkg/RcppSMC/R/simGaussian.R	2012-03-19 11:58:15 UTC (rev 3531)
@@ -0,0 +1,8 @@
+simGaussian <- function(len = 250)
+{
+  sim <- c();
+  sim$state <- cumsum(rnorm(len));
+  sim$data  <- sim$state + rnorm(len);
+
+  invisible(sim)
+}

Added: pkg/RcppSMC/R/simLineart.R
===================================================================
--- pkg/RcppSMC/R/simLineart.R	                        (rev 0)
+++ pkg/RcppSMC/R/simLineart.R	2012-03-19 11:58:15 UTC (rev 3531)
@@ -0,0 +1,17 @@
+simLineart <- function(len = 250)
+{
+  sim <- c();
+  
+  statex <- cumsum(rnorm(len));
+  statey <- cumsum(rnorm(len));
+
+  datax  <- statex + rt(len,df=4);
+  datay  <- statey + rt(len,df=4);
+
+  sim$state <- c(statex,statey);
+  dim(sim$state) <- c(2,2);
+  sim$data  <- c(datax,datay);
+  dim(sim$data) <- c(2,2);
+
+  invisible(sim);
+}

Added: pkg/RcppSMC/R/simNonlin.R
===================================================================
--- pkg/RcppSMC/R/simNonlin.R	                        (rev 0)
+++ pkg/RcppSMC/R/simNonlin.R	2012-03-19 11:58:15 UTC (rev 3531)
@@ -0,0 +1,14 @@
+simNonlin <- function(len = 50)
+{
+   sim <- c();
+
+   innovations <- rnorm(len) * sqrt(10);
+   sim$state[1] = innovations[1];
+   for (i in 2:len)
+       sim$state[i] = 0.5 * sim$state[i-1] + 25 * sim$state[i-1] / (1 +
+   sim$state[i-1]^2) + 8 * cos(1.2*(i-1)) + innovations[i];
+   end    
+   sim$data <- sim$state^2 / 20 + rnorm(len);	
+
+   invisible(sim)
+}

Modified: pkg/RcppSMC/man/blockpfGaussianOpt.Rd
===================================================================
--- pkg/RcppSMC/man/blockpfGaussianOpt.Rd	2012-03-19 09:57:59 UTC (rev 3530)
+++ pkg/RcppSMC/man/blockpfGaussianOpt.Rd	2012-03-19 11:58:15 UTC (rev 3531)
@@ -1,5 +1,7 @@
 \name{blockpfGaussianOpt}
 \alias{blockpfGaussianOpt}
+\alias{simGaussian}
+
 \title{Block Sampling Particle Filter (Linear Gaussian Model; Optimal Proposal)}
 \description{
   The \code{blockpfGaussianOpt} function provides a simple example for
@@ -8,9 +10,13 @@
   sampling; one would not ordinarily use a particle filter for a model in
   which analytic solutions are available. The 'optimal' block sampler in the
   sense of Doucet, Briers and Senecal (2006) can be implemented in this case.
+
+  The \code{simGaussian} function simulates data from the associated linear
+  Gaussian state space model.
 }
 \usage{
   blockpfGaussianOpt(data, particles=1000, lag=5, plot=FALSE) 
+  simGaussian(len)
 }
 \arguments{
   \item{data}{A vector variable containing the sequence of observations.}
@@ -18,19 +24,27 @@
   \item{lag}{An integer specifying the length of block to use.}
   \item{plot}{A boolean variable describing whether plot should
     illustrate the estimated path along with the uncertainty.}
+
+  \item{len}{The length of the data sequence to simulate.}
 }
 \value{
-  The function returns a matrix containing the final sample paths and a vector
-  containing their weights.
+  The \code{blockpfGaussianOpt} function returns a matrix containing the final
+  sample paths and a vector containing their weights.
+
+  The \code{simGaussian} function returns a list containing the state and data
+  sequences. 
 }
 \details{
-  The \code{pfEx} function provides a simple example for
+  The \code{blockpfGaussianOpt} function provides a simple example for
   \pkg{RcppSMC}. It is based on a simple linear Gaussian state space model in
   which the state evolution and observation equations are:
   	x(n) = x(n-1) + e(n) and 
 	y(n) = x(n) + f(n)
   where e(n) and f(n) are mutually-independent standard normal random variables. The 'optimal'
   block-sampling proposal described by (Doucet et al., 2006) is employed.
+
+  The \code{simGaussian} function simulates from the same model returning both
+  the state and observation vectors.
 }
 \references{
   A. Doucet, M. Briers, and S. Senecal. Efficient Block Sampling Strategies
@@ -38,9 +52,8 @@
   Statistics, 15(3):693-711, 2006.
 }
 \examples{
-\dontrun{
-  res <- blockpfGaussianOpt(data,lag=5,plot=TRUE)
+  sim <- simGaussian(len=250)
+  res <- blockpfGaussianOpt(sim$data,lag=5,plot=TRUE)
 }
-}
 \author{Adam M. Johansen}
 \keyword{programming}

Modified: pkg/RcppSMC/man/pfLineartBS.Rd
===================================================================
--- pkg/RcppSMC/man/pfLineartBS.Rd	2012-03-19 09:57:59 UTC (rev 3530)
+++ pkg/RcppSMC/man/pfLineartBS.Rd	2012-03-19 11:58:15 UTC (rev 3531)
@@ -1,6 +1,7 @@
 \name{pfLineartBS}
 \alias{pfLineartBS}
 \alias{pfLineartBSOnlinePlot}
+\alias{simLineart}
 \title{Particle Filter Example}
 \description{
   The \code{pfLineartBS} function provides a simple example for
@@ -10,11 +11,14 @@
 
   The \code{pfLineartBSOnlinePlot} function provides a simple default
   \sQuote{online} plotting function that is invoked during the
-  estimation process. 
+  estimation process.
+
+  The \code{simLineart} function simulates data from the model. 
 }
 \usage{
   pfLineartBS(data, particles=1000, plot=FALSE, onlinePlot) 
   pfLineartBSOnlinePlot(xm, ym)
+  simLineart(len)
 }
 \arguments{
   \item{data}{A two-column matrix or dataframe containing x and y
@@ -28,18 +32,24 @@
   pfExOnlinePlot for a simple example.}
   \item{xm}{Vector with x position.}
   \item{ym}{Vector with y position.}
+  \item{len}{Length of sequence to simulate}
 }
 \value{
-  The function returns a \code{data.frame} containing as many rows as in
+  The \code{pfLineartBS} function returns a \code{data.frame} containing as many rows as in
   the input data, and four columns corresponding to the estimated \eqn{x}{x} and
-  \eqn{y}{y} coordinates as well as the estimated velocity in these two directions.
+  \eqn{y}{y} coordinates as well as the estimated velocity in these two
+  directions.
+
+  The \code{simLineart} function returns a list containing the vector of
+  states and the associated vector of observations.
 }
 \details{
   The \code{pfLineartBS} function provides a simple example for
   \pkg{RcppSMC}. The model is linear with t-distributed innovations.
   It is based on the \code{pf} example in the
   \code{SMCTC} library, and discussed in the Section 5.1 of his
-  corresponding paper (Johansen, 2009).
+  corresponding paper (Johansen, 2009).   \code{simLineart} simulates from the
+  model. 
 
   Using the simple \code{pfExOnlinePlot} function illustrates how
   callbacks into R, for example for plotting,  can be made during the

Modified: pkg/RcppSMC/man/pfNonlinBS.Rd
===================================================================
--- pkg/RcppSMC/man/pfNonlinBS.Rd	2012-03-19 09:57:59 UTC (rev 3530)
+++ pkg/RcppSMC/man/pfNonlinBS.Rd	2012-03-19 11:58:15 UTC (rev 3531)
@@ -1,14 +1,18 @@
 \name{pfNonlinBS}
 \alias{pfNonlinBS}
+\alias{simNonlin}
 \title{Nonlinear Bootstrap Particle Filter (Univariate Non-Linear State Space Model)}
 \description{
   The \code{pfNonlinBS} function provides a simple example for
   \pkg{RcppSMC}. It is a simple \dQuote{bootstrap} particle filter which employs
   multinomial resampling after each iteration applied to the ubiquitous "nonlinear
   state space model" following Gordon, Salmond and Smith (1993).
+
+  The \code{simNonlin} function simulates data from the associated model.
 }
 \usage{
   pfNonlinBS(data, particles=500, plot=FALSE) 
+  simNonlin(len)
 }
 \arguments{
   \item{data}{A vector variable containing the sequence of observations.}
@@ -16,10 +20,13 @@
   \item{plot}{A boolean variable describing whether a plot should
     illustrate the (posterior mean) estimated path along with one and two
     standard deviation intervals.} 
+  \item{len}{The length of data sequence to simulate.}
 }
 \value{
-  The function returns two vectors, the first containing the posterior
+  The \code{pfNonlinBS} function returns two vectors, the first containing the posterior
   filtering means; the second the posterior filtering standard deviations.
+
+  The \code{simNonlin} function returns a list containing the state and data sequences. 
 }
 \details{
   The \code{pfNonlinbs} function provides a simple example for
@@ -31,6 +38,8 @@
   variables of variances 10.0 and 1.0, respectively. A boostrap proposal
   (i.e. sampling from the state equation) is used, together with multinomial
   resampling after each iteration. 
+
+  The \code{simNonlin} function simulates from the same model.
 }
 \references{
   N. J. Gordon, S. J. Salmond, and A. F. M. Smith. Novel approach to
@@ -38,9 +47,8 @@
   140(2):107-113, April 1993.	
 }
 \examples{
-\dontrun{
-  res <- pfNonlinBS(data,particles=500,plot=TRUE)
+  sim <- simNonlin(len=50)
+  res <- pfNonlinBS(sim$data,particles=500,plot=TRUE)
 }
-}
 \author{Adam M. Johansen}
 \keyword{programming}



More information about the Rcpp-commits mailing list