Title: | Diagnostic Plot and Multivariate Summary Statistics of Weighted Samples from Importance Sampling |
---|---|
Description: | Fast functions for effective sample size, weighted multivariate mean, variance, and quantile computation, and weight diagnostic plot for generic importance sampling type or other probability weighted samples. |
Authors: | Jouni Helske [aut, cre] |
Maintainer: | Jouni Helske <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.2.3 |
Built: | 2024-11-25 06:31:36 UTC |
Source: | CRAN |
This package contains functions computing weighted (running) summaries and diagonostic plots for importance sampling problems.
# simple importance sampling example # true distribution is a standard normal: p <- function(x) dnorm(x) # proposal distribution is normal with sd s q <- function(x, s) dnorm(x, 0, s) # IS weights have finite variance only if s^2 > 1/2 # variance is s/(2-1/s^2)^(3/2) #optimal case set.seed(42) s_opt <- sqrt(2) x_opt <- rnorm(1000, sd = s_opt) w_opt <- p(x_opt) / q(x_opt, s_opt) weighted_mean(x_opt, w_opt) weighted_var(x_opt, w_opt) s_inf <- 0.25 x_inf <- rnorm(1000, sd = s_inf) w_inf <- p(x_inf) / q(x_inf, s_inf) weighted_mean(x_inf, w_inf) #!! weighted_var(x_inf, w_inf) #!! # diagnostic plots weight_plot(w_inf) weight_plot(w_opt)
# simple importance sampling example # true distribution is a standard normal: p <- function(x) dnorm(x) # proposal distribution is normal with sd s q <- function(x, s) dnorm(x, 0, s) # IS weights have finite variance only if s^2 > 1/2 # variance is s/(2-1/s^2)^(3/2) #optimal case set.seed(42) s_opt <- sqrt(2) x_opt <- rnorm(1000, sd = s_opt) w_opt <- p(x_opt) / q(x_opt, s_opt) weighted_mean(x_opt, w_opt) weighted_var(x_opt, w_opt) s_inf <- 0.25 x_inf <- rnorm(1000, sd = s_inf) w_inf <- p(x_inf) / q(x_inf, s_inf) weighted_mean(x_inf, w_inf) #!! weighted_var(x_inf, w_inf) #!! # diagnostic plots weight_plot(w_inf) weight_plot(w_opt)
Computes the effective sample size (ESS) of importance sampling estimator.
ess(w, f, x)
ess(w, f, x)
w |
A numeric vector of non-negative weights. |
f |
A function used in computing |
x |
A numeric vector of samples used to generate |
An effective sample size estimate.
Computes and returns the running estimate of effective sample size (ESS) of importance sampling estimator.
running_ess(w, f, x)
running_ess(w, f, x)
w |
A numeric vector of non-negative weights. |
f |
A function used in computing |
x |
A numeric vector of samples used to generate |
An effective sample size estimate.
Computes running mean of a vector or matrix, returning the values from each step.
running_mean(x, na.rm)
running_mean(x, na.rm)
x |
A numeric vector, matrix, three dimensional array, or an |
na.rm |
If |
A vector containing the recursive mean estimates.
Computes running variance of a vector, returning the values from each step.
running_var(x, method = c("moment", "unbiased"), na.rm = FALSE)
running_var(x, method = c("moment", "unbiased"), na.rm = FALSE)
x |
A numeric vector or object that can be coerced to such. |
method |
Estimator type, either |
na.rm |
If |
A vector containing the recursive variance estimates.
Computes running weighted mean of a vector or matrix, returning the values from each step.
running_weighted_mean(x, w, na.rm)
running_weighted_mean(x, w, na.rm)
x |
A numeric vector, matrix, three dimensional array, or an |
w |
A numeric vector of non-negative weights. Will be automatically normalised to sum to one. |
na.rm |
If |
A vector containing the recursive weighted mean estimates.
Computes running weighted variance of a vector, returning the values from each step.
running_weighted_var(x, w, method = c("moment", "unbiased"), na.rm = FALSE)
running_weighted_var(x, w, method = c("moment", "unbiased"), na.rm = FALSE)
x |
A numeric vector or object that can be coerced to such. |
w |
A numeric vector of non-negative weights. Will be automatically normalised to sum to one. |
method |
Estimator type, either |
na.rm |
If |
A vector containing the recursive weighted variance estimates.
Function weight_plot
plots four figures given the weight vector w
:
Plot of largest weights, sorted graph of all weights, running variance estimate of weights,
and running effective sample size estimate of weights.
weight_plot(w)
weight_plot(w)
w |
Vector of weights. |
#' importance sampling from too narrow distribution #' weights have infinite variance set.seed(1) x_inf <- rnorm(1000, sd = 0.1) w_inf <- dnorm(x_inf) / dnorm(x_inf, 0, 0.1) weight_plot(w_inf) x_opt <- rnorm(1000, sd = sqrt(2)) w_opt <- dnorm(x_opt) / dnorm(x_opt, 0, sqrt(2)) weight_plot(w_opt)
#' importance sampling from too narrow distribution #' weights have infinite variance set.seed(1) x_inf <- rnorm(1000, sd = 0.1) w_inf <- dnorm(x_inf) / dnorm(x_inf, 0, 0.1) weight_plot(w_inf) x_opt <- rnorm(1000, sd = sqrt(2)) w_opt <- dnorm(x_opt) / dnorm(x_opt, 0, sqrt(2)) weight_plot(w_opt)
Computes a weighted mean of a vector, matrix, or a three dimensional array.
weighted_mean(x, w, na.rm)
weighted_mean(x, w, na.rm)
x |
A numeric vector, matrix, three dimensional array, or an |
w |
A numeric vector of non-negative weights. Will be automatically normalised to sum to one. |
na.rm |
If |
A weighted mean.
Computes a weighted quantiles of a vector or matrix. Based on the formula in Wikipedia (see the vignette) which is one of many ways to compute weighted quantiles.
weighted_quantile(x, w, probs = probs, na.rm)
weighted_quantile(x, w, probs = probs, na.rm)
x |
A numeric vector or matrix. For matrix, the quantiles are computed for each column. |
w |
A numeric vector of non-negative weights. Will be automatically normalised to sum to one. |
probs |
A numeric vector of probabilities with values between 0 and 1. |
na.rm |
If |
A weighted variance.
Compared to some other R functions, here the weights are regarded as probability weights, not frequency weights.
Computes a weighted standard error of a vector or matrix.
weighted_se(x, w, na.rm)
weighted_se(x, w, na.rm)
x |
A numeric vector or matrix. For matrix, standard errors are computed for each column |
w |
A numeric vector of non-negative weights. Will be automatically normalised to sum to one. |
na.rm |
If |
A weighted variance.
Compared to some other R functions, here the weights are regarded as probability weights, not frequency weights.
Computes a weighted variance/covariance of a vector, matrix or a three dimensional array.
weighted_var(x, w, method, na.rm)
weighted_var(x, w, method, na.rm)
x |
A numeric vector, matrix or three dimensional array. For matrix, covariances are computed between columns. For array, marginal covariances are computed for each column, i.e. for $m x n x k$ array function returns $m x m x n$ array. |
w |
A numeric vector of non-negative weights. Will be automatically normalised to sum to one. |
method |
Estimator type, either |
na.rm |
If |
A weighted variance.
Compared to some other R functions, here the weights are regarded as probability weights, not frequency weights.