Title:  Importance weighted moment matching 

Description:  iwmm provides functions for adaptive importance sampling. 
Authors:  Topi Paananen [cre, aut], Noa Kallioinen [ctb] 
Maintainer:  Topi Paananen <[email protected]> 
License:  GPL (>=3) 
Version:  0.0.1 
Built:  20240716 05:30:42 UTC 
Source:  https://github.com/topipa/iwmm 
Constrain all draws from a fitted model
constrain_draws(x, ...)
constrain_draws(x, ...)
x 
model fit object 
... 
arguments passed to methods 
constrained draws
Provides example models (with data) that are ready for use with IWMM.
example_iwmm_model(model = "normal_model")
example_iwmm_model(model = "normal_model")
model 
Character specifying which model code to return. Currently "normal_model" is implemented. 
List containing model code and corresponding data.
Return log probability of posterior
log_prob_draws(fit, ...)
log_prob_draws(fit, ...)
fit 
model fit object 
... 
arguments passed to methods 
TODO
Generic importance weighted moment matching algorithm.
Generic importance weighted moment matching algorithm for matrices.
moment_match(x, ...) ## S3 method for class 'matrix' moment_match( x, log_prob_prop_fun, log_prob_target_fun = NULL, log_ratio_fun = NULL, expectation_fun = NULL, log_expectation_fun = FALSE, draws_transformation_fun = NULL, is_method = "psis", adaptation_method = "iwmm", k_threshold = 0.5, cov_transform = TRUE, split = FALSE, restart_transform = FALSE, ... )
moment_match(x, ...) ## S3 method for class 'matrix' moment_match( x, log_prob_prop_fun, log_prob_target_fun = NULL, log_ratio_fun = NULL, expectation_fun = NULL, log_expectation_fun = FALSE, draws_transformation_fun = NULL, is_method = "psis", adaptation_method = "iwmm", k_threshold = 0.5, cov_transform = TRUE, split = FALSE, restart_transform = FALSE, ... )
x 
A matrix of draws. Must be unconstrained. 
... 
Further arguments passed to 
log_prob_prop_fun 
Log density of the proposal. The function
takes argument 
log_prob_target_fun 
Log density of the target for importance
sampling. The function takes argument 
log_ratio_fun 
Log of the density ratio for importance
sampling (target/proposal). The function takes argument 
expectation_fun 
Optional argument, NULL by default. A
function whose expectation is being computed. The function takes
arguments 
log_expectation_fun 
Logical indicating whether the
expectation_fun returns its values as logarithms or not. Defaults
to FALSE. If set to TRUE, the expectation function must be
nonnegative (before taking the logarithm). Ignored if

draws_transformation_fun 
Optional argument, NULL by default. A
function that transforms draws before computing expectation. The function takes
arguments 
is_method 
Which importance sampling method to use. Currently only 
adaptation_method 
Which adaptation method to use. Currently only 
k_threshold 
Threshold value for Pareto k values above which the moment matching algorithm is used. The default value is 0.5. 
cov_transform 
Logical; Indicates whether to match the
covariance of the samples or not. If 
split 
Logical; Indicate whether to do the split transformation or not at the end of moment matching. FALSE by default. 
restart_transform 
Logical; When split is TRUE, indicates whether to start the second transformation from the original model parameters or the transformed parameters. If split is FALSE, this is ignored. 
Returns a list with: transformed draws, updated importance weights, and the pareto k diagnostic value. If expectation_fun is given, also returns the expectation.
brmsfit
objects.
See additional arguments from moment_match.matrix
Generic importance weighted moment matching algorithm for brmsfit
objects.
See additional arguments from moment_match.matrix
## S3 method for class 'brmsfit' moment_match( x, log_prob_target_fun = NULL, log_ratio_fun = NULL, target_observation_weights = NULL, expectation_fun = NULL, log_expectation_fun = FALSE, constrain = TRUE, ... )
## S3 method for class 'brmsfit' moment_match( x, log_prob_target_fun = NULL, log_ratio_fun = NULL, target_observation_weights = NULL, expectation_fun = NULL, log_expectation_fun = FALSE, constrain = TRUE, ... )
x 
A fitted 
log_prob_target_fun 
Log density of the target. The function
takes argument 
log_ratio_fun 
Log of the density ratio (target/proposal).
The function takes argument 
target_observation_weights 
A vector of weights for observations for defining the target distribution. A value 0 means dropping the observation, a value 1 means including the observation similarly as in the current data, and a value 2 means including the observation twice. 
expectation_fun 
Optional argument, NULL by default. A
function whose expectation is being computed. The function takes
arguments 
log_expectation_fun 
Logical indicating whether the
expectation_fun returns its values as logarithms or not. Defaults
to FALSE. If set to TRUE, the expectation function must be
nonnegative (before taking the logarithm). Ignored if

constrain 
Logical specifying whether to return draws on the constrained space? Default is TRUE. 
... 
Further arguments passed to 
Returns a list with 3 elements: transformed draws, updated importance weights, and the pareto k diagnostic value. If expectation_fun is given, also returns the expectation.
CmdStanFit
objects. See additional arguments from
moment_match.matrix
Generic importance weighted moment matching algorithm for
CmdStanFit
objects. See additional arguments from
moment_match.matrix
## S3 method for class 'CmdStanFit' moment_match( x, log_prob_target_fun = NULL, log_ratio_fun = NULL, constrain_draws = TRUE, ... )
## S3 method for class 'CmdStanFit' moment_match( x, log_prob_target_fun = NULL, log_ratio_fun = NULL, constrain_draws = TRUE, ... )
x 
A fitted 
log_prob_target_fun 
Log density of the target. The function
takes argument 
log_ratio_fun 
Log of the density ratio (target/proposal).
The function takes argument 
constrain_draws 
Logical specifying whether to return draws on the constrained space. Draws are also constrained for computing expectations. Default is TRUE. 
... 
Further arguments passed to 
Returns a list with 3 elements: transformed draws, updated importance weights, and the pareto k diagnostic value.
stanfit
objects.
See additional arguments from moment_match.matrix
Generic importance weighted moment matching algorithm for stanfit
objects.
See additional arguments from moment_match.matrix
## S3 method for class 'stanfit' moment_match( x, log_prob_target_fun = NULL, log_ratio_fun = NULL, target_observation_weights = NULL, expectation_fun = NULL, log_expectation_fun = FALSE, constrain_draws = TRUE, ... )
## S3 method for class 'stanfit' moment_match( x, log_prob_target_fun = NULL, log_ratio_fun = NULL, target_observation_weights = NULL, expectation_fun = NULL, log_expectation_fun = FALSE, constrain_draws = TRUE, ... )
x 
A fitted 
log_prob_target_fun 
Log density of the target. The function
takes argument 
log_ratio_fun 
Log of the density ratio (target/proposal).
The function takes argument 
target_observation_weights 
A vector of weights for observations for defining the target distribution. A value 0 means dropping the observation, a value 1 means including the observation similarly as in the current data, and a value 2 means including the observation twice. 
expectation_fun 
Optional argument, NULL by default. A
function whose expectation is being computed. The function takes
arguments 
log_expectation_fun 
Logical indicating whether the
expectation_fun returns its values as logarithms or not. Defaults
to FALSE. If set to TRUE, the expectation function must be
nonnegative (before taking the logarithm). Ignored if

constrain_draws 
Logical specifying whether to return draws on the constrained space. Draws are also constrained for computing expectations. Default is TRUE. 
... 
Further arguments passed to 
Returns a list with 3 elements: transformed draws, updated importance weights, and the pareto k diagnostic value. If expectation_fun is given, also returns the expectation.
Shift a matrix of draws to their weighted mean.
shift(draws, lw)
shift(draws, lw)
draws 
A matrix of draws. 
lw 
A vector representing the logweight of each draw. 
List with the shift that was performed, and the new draws matrix.
Shift a matrix of draws to their weighted mean and scale the covariance to match the weighted covariance.
shift_and_cov(draws, lw)
shift_and_cov(draws, lw)
draws 
A matrix of draws. 
lw 
A vector representing the logweight of each draw. 
List with the shift and mapping that were performed, and the new draws matrix.
Shift a matrix of draws to their weighted mean and scale the marginal variances to match the weighted marginal variances.
shift_and_scale(draws, lw)
shift_and_scale(draws, lw)
draws 
A matrix of draws. 
lw 
A vector representing the logweight of each draw. 
List with the shift and scaling that were performed, and the new draws matrix.
Unconstrain all draws from a fitted model
unconstrain_draws(x, ...)
unconstrain_draws(x, ...)
x 
model fit object 
... 
arguments passed to methods 
unconstrained draws