ipois {simEd}R Documentation

Visualization of Random Variate Generation for the Poisson Distribution

Description

Generates random variates from the Poisson distribution by inversion. Optionally graphs the population cumulative distribution function and associated random variates, the population probability mass function and a histogram of the random variates, and the empirical cumulative distribution function versus the population cumulative distribution function.

Usage

ipois(
  u = runif(1),
  lambda,
  minPlotQuantile = 0,
  maxPlotQuantile = 0.95,
  plot = TRUE,
  showCDF = TRUE,
  showPMF = TRUE,
  showECDF = TRUE,
  show = NULL,
  maxInvPlotted = 50,
  plotDelay = 0,
  sampleColor = "red3",
  populationColor = "grey",
  showTitle = TRUE,
  respectLayout = FALSE,
  restorePar = TRUE,
  ...
)

Arguments

u

vector of uniform(0,1) random numbers, or NULL to show population figures only

lambda

Rate of distribution

minPlotQuantile

minimum quantile to plot

maxPlotQuantile

maximum quantile to plot

plot

logical; if TRUE (default), one or more plots will appear (see parameters below); otherwise no plots appear

showCDF

logical; if TRUE (default), cdf plot appears, otherwise cdf plot is suppressed

showPMF

logical; if TRUE (default), PMF plot appears, otherwise PMF plot is suppressed

showECDF

logical; if TRUE (default), ecdf plot appears, otherwise ecdf plot is suppressed

show

octal number (0-7) indicating plots to display; 4: CDF, 2: PMF, 1: ECDF; sum for desired combination

maxInvPlotted

number of inversions to plot across CDF before switching to plotting quantiles only

plotDelay

delay in seconds between CDF plots

sampleColor

Color used to display random sample from distribution

populationColor

Color used to display population

showTitle

logical; if TRUE (default), displays a title in the first of any displayed plots

respectLayout

logical; if TRUE (default), respects existing settings for device layout

restorePar

logical; if TRUE (default), restores user's previous par settings on function exit

...

Possible additional arguments. Currently, additional arguments not considered.

Details

Generates random variates from the Poisson distribution, and optionally, illustrates

When all of the graphics are requested,

All aspects of the random variate generation algorithm are output in red by default, which can be changed by specifying sampleColor. All aspects of the population distribution are output in gray by default, which can be changed by specifying populationColor.

The Poisson distribution has density

p(x) = \frac{\lambda^x e^{-\lambda}}{x!}

for x = 0, 1, 2, \ldots. The mean and variance are E(X) = Var(X) = \lambda

The algorithm for generating random variates from the Poisson distribution is synchronized (one random variate for each random number) and monotone in u. This means that the variates generated here might be useful in some variance reduction techniques used in Monte Carlo and discrete-event simulation.

Values from the u vector are plotted in the cdf plot along the vertical axis as colored dots. A horizontal, dashed, colored line extends from the dot to the population cdf. At the intersection, a vertical, dashed colored line extends downward to the horizontal axis, where a second colored dot, denoting the associated Poisson random variate is plotted.

This is not a particularly fast variate generation algorithm because it uses the base R qpois function to invert the values contained in u.

All of the elements of the u vector must be between 0 and 1. Alternatively, u can be NULL in which case plot(s) of the theoretical PMF and cdf are displayed according to plotting parameter values (defaulting to display of both the PMF and cdf).

The show parameter can be used as a shortcut way to denote plots to display. The argument to show can be either:

Any valid value for show takes precedence over existing individual values for showCDF, showPMF, and showECDF.

If respectLayout is TRUE, the function respects existing settings for device layout. Note, however, that if the number of plots requested (either via show or via showCDF, showPMF, and showECDF) exceeds the number of plots available in the current layout (as determined by prod(par("mfrow"))), the function will display all requested plots but will also display a warning message indicating that the current layout does not permit simultaneous viewing of all requested plots. The most recent plot with this attribute can be further annotated after the call.

If respectLayout is FALSE, any existing user settings for device layout are ignored. That is, the function uses par to explicitly set mfrow sufficient to show all requested plots stacked vertically to align their horizontal axes, and then resets row, column, and margin settings to their prior state on exit.

The minPlotQuantile and maxPlotQuantile arguments are present in order to compress the plots horizontally. The random variates generated are not impacted by these two arguments. Vertical, dotted, black lines are plotted at the associated quantiles on the plots.

plotDelay can be used to slow down or halt the variate generation for classroom explanation.

In the plot associated with the PMF, the maximum plotting height is associated with 125\ that extends above this limit will have three dots appearing above it.

Value

A vector of Poisson random variates

Author(s)

Barry Lawson (blawson@bates.edu),
Larry Leemis (leemis@math.wm.edu),
Vadim Kudlay (vkudlay@nvidia.com)

See Also

stats::rpois

stats::runif, simEd::vunif

Examples

 ipois(0.5, lambda = 5)

 set.seed(8675309)
 ipois(runif(10), 3, showPMF = TRUE)

 set.seed(8675309)
 ipois(runif(10), 3, showECDF = TRUE)

 set.seed(8675309)
 ipois(runif(10), 3, showPMF = TRUE, showECDF = TRUE, sampleColor = "blue3")

 set.seed(8675309)
 ipois(runif(10), 3, showPMF = TRUE, showCDF = FALSE)

 ipois(runif(100), 3, showPMF = TRUE, minPlotQuantile = 0.02, maxPlotQuantile = 0.98)

 # plot the PMF and CDF without any variates
 ipois(NULL, 3, showPMF = TRUE, showCDF = TRUE)

 # plot CDF with inversion and PMF using show
 ipois(runif(10), 3, show = c(1,1,0))
 ipois(runif(10), 3, show = 6)

 # plot CDF with inversion and ECDF using show, using vunif
 ipois(vunif(10), 3, show = c(1,0,1))
 ipois(vunif(10), 3, show = 5)

 # plot CDF with inversion, PMF, and ECDF using show
 ipois(vunif(10), 3, show = c(1,1,1))
 ipois(vunif(10), 3, show = 7)

 # plot three different CDF+PMF+ECDF horizontal displays,
 # with title only on the first display
 oldpar <- par(no.readonly = TRUE)
 par(mfrow = c(3,3))  # 3 rows, 3 cols, filling rows before columns
 set.seed(8675309)
 ipois(runif(20), 3, show = 7, respectLayout = TRUE, restorePar = FALSE)
 ipois(runif(20), 3, show = 7, respectLayout = TRUE, restorePar = FALSE, showTitle = FALSE)
 ipois(runif(20), 3, show = 7, respectLayout = TRUE, restorePar = TRUE,  showTitle = FALSE)
 par(oldpar)

 # display animation of all components
 ipois(runif(10), 3, show = 7, plotDelay = 0.1)

 # display animation of CDF and PMF components only
 ipois(runif(10), 3, show = 5, plotDelay = 0.1)

 if (interactive()) {
   # interactive -- pause at each stage of inversion
   ipois(runif(10), 3, show = 7, plotDelay = -1)
 }


[Package simEd version 2.0.1 Index]