[Pomp-commits] r470 - in pkg: R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed May 11 23:36:09 CEST 2011


Author: kingaa
Date: 2011-05-11 23:36:08 +0200 (Wed, 11 May 2011)
New Revision: 470

Modified:
   pkg/R/mif.R
   pkg/R/pfilter.R
   pkg/R/pmcmc.R
   pkg/inst/NEWS
   pkg/man/pfilter.Rd
Log:
- add new argument 'save.params' to 'pfilter' to allow saving the parameter portion of the particles
- change the 'pfilterd.pomp' slot name 'last.states' to 'saved.states', which connects with the option name
- add a new slot 'saved.params' to the 'pfilterd.pomp' object
- update 'pmcmc' and 'mif' in accordance with the above changes
- modify 'pfilter' so that 'coef(pfilter(obj,params=theta))==theta' as in 'mif', 'simulate', etc.


Modified: pkg/R/mif.R
===================================================================
--- pkg/R/mif.R	2011-05-11 12:42:13 UTC (rev 469)
+++ pkg/R/mif.R	2011-05-11 21:36:08 UTC (rev 470)
@@ -220,6 +220,7 @@
                                 pred.var=(weighted||(n==Nmif)),
                                 filter.mean=TRUE,
                                 save.states=FALSE,
+                                save.params=FALSE,
                                 .rw.sd=sigma.n[pars],
                                 verbose=verbose
                                 ),

Modified: pkg/R/pfilter.R
===================================================================
--- pkg/R/pfilter.R	2011-05-11 12:42:13 UTC (rev 469)
+++ pkg/R/pfilter.R	2011-05-11 21:36:08 UTC (rev 470)
@@ -9,7 +9,8 @@
            filter.mean="array",
            eff.sample.size="numeric",
            cond.loglik="numeric",
-           last.states="array",
+           saved.states="array",
+           saved.params="array",
            seed="integer",
            Np="integer",
            tol="numeric",
@@ -24,7 +25,7 @@
                               tol, max.fail,
                               pred.mean, pred.var, filter.mean,
                               .rw.sd, seed, verbose,
-                              save.states) {
+                              save.states, save.params) {
   
   if (missing(seed)) seed <- NULL
   if (!is.null(seed)) {
@@ -49,6 +50,7 @@
   ntimes <- length(times)-1
   if (is.null(dim(params))) {
     one.par <- TRUE               # there is only one parameter vector
+    coef(object,names(params)) <- unname(params) # set params slot to the parameters
     params <- matrix(
                      params,
                      nrow=length(params),
@@ -66,6 +68,8 @@
   x <- init.state(object,params=params)
   statenames <- rownames(x)
   nvars <- nrow(x)
+  
+  ## set up storage for saving samples from filtering distributions
   if (save.states)
     xparticles <- array(
                         data=NA,
@@ -74,7 +78,15 @@
                         )
   else
     xparticles <- array(dim=c(0,0,0))
-  
+  if (save.params)
+    pparticles <- array(
+                        data=NA,
+                        dim=c(length(paramnames),Np,ntimes),
+                        dimnames=list(paramnames,NULL,NULL)
+                        )
+  else
+    pparticles <- array(dim=c(0,0,0))
+
   random.walk <- !missing(.rw.sd)
   if (random.walk) {
     rw.names <- names(.rw.sd)
@@ -233,6 +245,10 @@
       xparticles[,,nt] <- x
     }
 
+    if (save.params) {
+      pparticles[,,nt] <- params
+    }
+
     if (verbose && ((ntimes-nt)%%5==0))
       cat("pfilter timestep",nt,"of",ntimes,"finished\n")
 
@@ -251,7 +267,8 @@
       filter.mean=filt.m,
       eff.sample.size=eff.sample.size,
       cond.loglik=loglik,
-      last.states=xparticles,
+      saved.states=xparticles,
+      saved.params=pparticles,
       seed=as.integer(seed),
       Np=as.integer(Np),
       tol=tol,
@@ -273,6 +290,7 @@
                     pred.var = FALSE,
                     filter.mean = FALSE,
                     save.states = FALSE,
+                    save.params = FALSE,
                     seed = NULL,
                     verbose = getOption("verbose"),
                     ...) {
@@ -287,6 +305,7 @@
                              pred.var=pred.var,
                              filter.mean=filter.mean,
                              save.states=save.states,
+                             save.params=save.params,
                              seed=seed,
                              verbose=verbose
                              )
@@ -303,6 +322,7 @@
                     pred.var = FALSE,
                     filter.mean = FALSE,
                     save.states = FALSE,
+                    save.params = FALSE,
                     seed = NULL,
                     verbose = getOption("verbose"),
                     ...) {
@@ -319,9 +339,9 @@
                              pred.var=pred.var,
                              filter.mean=filter.mean,
                              save.states=save.states,
+                             save.params=save.params,
                              seed=seed,
                              verbose=verbose
                              )
           }
           )
-

Modified: pkg/R/pmcmc.R
===================================================================
--- pkg/R/pmcmc.R	2011-05-11 12:42:13 UTC (rev 469)
+++ pkg/R/pmcmc.R	2011-05-11 21:36:08 UTC (rev 470)
@@ -159,6 +159,7 @@
                                 pred.var=FALSE,
                                 filter.mean=TRUE,
                                 save.states=FALSE,
+                                save.params=FALSE,
                                 verbose=verbose
                                 ),
                silent=FALSE
@@ -190,6 +191,7 @@
                                      pred.var=FALSE,
                                      filter.mean=TRUE,
                                      save.states=FALSE,
+                                     save.params=FALSE,
                                      verbose=verbose
                                      ),
                     silent=FALSE

Modified: pkg/inst/NEWS
===================================================================
--- pkg/inst/NEWS	2011-05-11 12:42:13 UTC (rev 469)
+++ pkg/inst/NEWS	2011-05-11 21:36:08 UTC (rev 470)
@@ -13,6 +13,10 @@
         These warnings were introduced in version 0.34-1 to alert users to changes in the default behavior of 'simulate' and 'trajectory' introduced with that version.
         See the help ("pomp?simulate", "pomp?trajectory") for details.
 
+     o  It is now possible to save both the states and particles portions of the filtering distributions computed in 'pfilter'.
+     	This is accomplished by means of the logical 'save.states' and 'save.params' arguments to 'pfilter'.
+	The particles are stored in rank-3 arrays in the slots 'saved.states' and 'saved.params'.
+
 0.36-7
      o  'trajectory' now gives a more informative error when no skeleton is present
 

Modified: pkg/man/pfilter.Rd
===================================================================
--- pkg/man/pfilter.Rd	2011-05-11 12:42:13 UTC (rev 469)
+++ pkg/man/pfilter.Rd	2011-05-11 21:36:08 UTC (rev 470)
@@ -13,11 +13,13 @@
 \usage{
 \S4method{pfilter}{pomp}(object, params, Np, tol = 1e-17,
     max.fail = 0, pred.mean = FALSE, pred.var = FALSE,
-    filter.mean = FALSE, save.states = FALSE, seed = NULL,
+    filter.mean = FALSE, save.states = FALSE,
+    save.params = FALSE, seed = NULL,
     verbose = getOption("verbose"), \dots)
 \S4method{pfilter}{pfilterd.pomp}(object, params, Np, tol,
     max.fail = 0, pred.mean = FALSE, pred.var = FALSE,
-    filter.mean = FALSE, save.states = FALSE, seed = NULL,
+    filter.mean = FALSE, save.states = FALSE,
+    save.params = FALSE, seed = NULL,
     verbose = getOption("verbose"), \dots)
 }
 \arguments{
@@ -52,8 +54,10 @@
   \item{filter.mean}{
     logical; if \code{TRUE}, the filtering means are calculated for the state variables and parameters.
   }
-  \item{save.states}{
-    logical; if \code{TRUE}, the state-vector for each particle is saved and returned.
+  \item{save.states, save.params}{
+    logical.
+    If \code{save.states=TRUE}, the state-vector for each particle at each time is saved in the \code{saved.states} slot of the returned \code{\link[=pfilterd.pomp-class]{pfilterd.pomp}} object.
+    If \code{save.params=TRUE}, the parameter-vector for each particle at each time is saved in the \code{saved.params} slot of the returned \code{\link[=pfilterd.pomp-class]{pfilterd.pomp}} object.
   }
   \item{seed}{
     optional; an object specifying if and how the random number generator should be initialized (\sQuote{seeded}).
@@ -68,7 +72,7 @@
   }
 }
 \value{
-  An object of class \code{pfilterd.pomp}.
+  An object of class \code{\link[=pfilterd.pomp-class]{pfilterd.pomp}}.
   This class inherits from class \code{\link[=pomp-class]{pomp}} and contains the following additional slots:
   \describe{
     \item{pred.mean, pred.var, filter.mean}{
@@ -81,11 +85,15 @@
     \item{cond.loglik}{
       numeric vector containing the conditional log likelihoods at each time point.
     }
-    \item{states}{
-      if \code{saves.states=TRUE}, the array of state-vectors at each time point, for each particle.
+    \item{saved.states}{
+      If \code{pfilter} was called with \code{save.states=TRUE}, this is the array of state-vectors at each time point, for each particle.
       An array with dimensions \code{nvars}-by-\code{Np}-by-\code{ntimes}.
-      In particular, \code{states[,i,t]} can be considered a sample from \eqn{f[X|y_{1:t}]}.
+      In particular, \code{saved.states[,i,t]} can be considered a sample from \eqn{f[X|y_{1:t}]}.
     }
+    \item{saved.params}{
+      If \code{pfilter} was called with \code{save.params=TRUE}, this is the array of parameter-vectors at each time point, for each particle.
+      It is an array with dimensions \code{npars}-by-\code{Np}-by-\code{ntimes}.
+    }
     \item{seed}{
       the state of the random number generator at the time \code{pfilter} was called.
       If the argument \code{seed} was specified, this is a copy;
@@ -100,6 +108,8 @@
   }
   These can be accessed using the \code{$} operator as if the returned object were a list.
   In addition, \code{logLik} returns the log likelihood.
+  Note that if the argument \code{params} is a named vector, then these parameters are included in the \code{params} slot of the returned \code{pfilterd.pomp} object.
+  That is \code{coef(pfilter(obj,params=theta))==theta} if \code{theta} is a named vector of parameters.
 }
 \examples{
 ## See the vignettes for examples.



More information about the pomp-commits mailing list