mewAvg: Convenience wrapper for the MEW process

Description Usage Arguments Details Value Examples

Description

Packages the process of calling mewInit, looping through the random vectors calling mewAccum for each one and calling mewMean when desired.

Usage

1
mewAvg(f, n.bin, n.xx, ff, n.save = NULL, n.iter = NULL, i.to.save, ...)

Arguments

f

(function) A user defined R function. See the 'Details' section for more on defining this function

n.bin

(scalar integer) The fixed number of bins to use to define the moving expanding window

n.xx

(scalar integer) The length of the numeric vector returned by f

ff

(scalar double) The fraction of the samples to included in each window

n.save

(scalar integer OR NULL)The number of estimates to save and return. The default value is NULL since this argument can be derived from i.to.save. The argument is kept for compatibility with older versions of this package

n.iter

(scalar integer OR NULL) The number of times to call f. The default value is NULL since this argument can be derived from i.to.save. The argument is kept for compatibility with older versions of this package

i.to.save

(vector integer length n.iter) A vector of zeros and ones of length n.iter where position i is 1 if an average should be calculated and saved at iteration i, and zero otherwise

...

The initial named arguments to f.

Details

The function f should generate the sequence of random vectors one at a time. The returned value from a single call should be a list with at least one element. The first element should be a numeric vector of length n.xx (the next vector in the sequence), and the remaining elements should be the updated arguments for the next call to f, named appropriately for the argument of f to update. The 'Examples' section provides further guidance.

The downfall of this interface is that the user cannot run the algorithm for some number of iterations, pause, assess convergence of the mean and then pick up from where they paused. To accomplish that see the examples associated with the mewMean function.

Value

A matrix of dimension n.save by n.xx containing the saved averages

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
MyFun <- function (k) {

 value <- runif(n=2)
 value[1] <- ((cos(value[1]*2*pi))^2)*(1 - exp(-0.01*k))
 value[2] <- (-((sin(value[2]*2*pi))^2))*(1 - exp(-0.01*k))

 k <- k + 1

 return(list(value=value, k=k))
}

i.to.save <- seq(from=1, to=1025, by=32)
tmp <- rep(x=0, times=1025)
tmp[i.to.save] <- 1
i.to.save <- tmp

mean.vals <- mewAvg(f=MyFun,
                    n.bin=4,
                    n.xx=2,
                    ff=0.5,
                    n.save=sum(i.to.save),
                    n.iter=length(i.to.save),
                    i.to.save=i.to.save,
                    k=1)

plot(c(1:sum(i.to.save),
       1:sum(i.to.save)),
     c(mean.vals[, 1],
       mean.vals[, 2]),
     type="n",
     xlab="Saved Iter",
     ylab="Mean")
points(1:sum(i.to.save),
       mean.vals[, 1])
points(1:sum(i.to.save),
       mean.vals[, 2])

## an AR(1) process

ArOne <- function (x.old, phi, sig.eps) {

  value <- phi*x.old + rnorm(n=1, mean=0, sd=sig.eps)

  return(list(value=value, x.old=value))
}

mean.vals.ar1 <- mewAvg(f=ArOne,
                        n.bin=4,
                        n.xx=1,
                        ff=0.5,
                        n.save=sum(i.to.save),
                        n.iter=length(i.to.save),
                        i.to.save=i.to.save,
                        x.old=0,
                        phi=0.5,
                        sig.eps=1)

plot(x=c(1, sum(i.to.save)),
     y=c(-0.5, 0.5),
     xlab="Saved Iter",
     ylab="Mean",
     type="n")
points(x=1:sum(i.to.save),
       y=mean.vals.ar1)
abline(h=0, col="red")

mewAvg documentation built on May 2, 2019, 6:20 a.m.