{"id":19769996,"url":"https://github.com/topipa/iwmm","last_synced_at":"2025-04-30T17:31:55.133Z","repository":{"id":108178295,"uuid":"317519811","full_name":"topipa/iwmm","owner":"topipa","description":"iwmm: an R package for adaptive importance sampling","archived":false,"fork":false,"pushed_at":"2024-09-08T15:52:37.000Z","size":517,"stargazers_count":4,"open_issues_count":4,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-09T14:12:37.849Z","etag":null,"topics":["bayesian","bayesian-data-analysis","bayesian-methods","r","r-package","stan"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/topipa.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-12-01T11:28:17.000Z","updated_at":"2024-09-08T15:49:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"21482d8b-222f-4ce3-a7e8-76f5b0bbf56f","html_url":"https://github.com/topipa/iwmm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/topipa%2Fiwmm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/topipa%2Fiwmm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/topipa%2Fiwmm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/topipa%2Fiwmm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/topipa","download_url":"https://codeload.github.com/topipa/iwmm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224219726,"owners_count":17275477,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayesian","bayesian-data-analysis","bayesian-methods","r","r-package","stan"],"created_at":"2024-11-12T04:45:30.286Z","updated_at":"2024-11-12T04:45:30.876Z","avatar_url":"https://github.com/topipa.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n\u003cimg src='man/figures/logo.png' align=\"right\" height=\"160\" /\u003e\n\n# iwmm\n\n\u003c!-- badges: start --\u003e\n[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)\n[![CRAN status](https://www.r-pkg.org/badges/version/iwmm)](https://CRAN.R-project.org/package=iwmm)\n[![R-CMD-check](https://github.com/topipa/iwmm/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/topipa/iwmm/actions/workflows/R-CMD-check.yaml)\n[![test-coverage](https://github.com/topipa/iwmm/actions/workflows/test-coverage.yaml/badge.svg)](https://github.com/topipa/iwmm/actions/workflows/test-coverage.yaml)\n\u003c!-- badges: end --\u003e\n\n## Overview\n\n**IWMM** is an R package for adaptive importance sampling. The package implements the\nimportance weighted moment matching (IWMM) algorithm.\nGiven draws from a probability distribution, IWMM adapts the draws based on a given target distribution and an optional\nexpectation function. The draws can, but do not have to be from the distribution over which the expectation is defined.\n\nThe method is described in detail in the [Implicitly Adaptive Importance Sampling](https://doi.org/10.1007/s11222-020-09982-2) paper.\n\n## Installation\n\n\u003c!-- You can install the released version of iwmm from [CRAN](https://CRAN.R-project.org) with: --\u003e\n\n\u003c!-- ``` r --\u003e\n\u003c!-- install.packages(\"iwmm\") --\u003e\n\u003c!-- ``` --\u003e\n\n\u003c!-- And the development version from [GitHub](https://github.com/) with: --\u003e\n\n\u003c!-- ``` r --\u003e\n\u003c!-- # install.packages(\"devtools\") --\u003e\n\u003c!-- devtools::install_github(\"topipa/iwmm\") --\u003e\n\u003c!-- ``` --\u003e\n\nYou can install the the development version from [GitHub](https://github.com/) with:\n\n```{r, eval = F}\ninstall.packages(\"remotes\")\nremotes::install_github(\"topipa/iwmm\")\n```\n\n## Usage\n\nIWMM takes a sample of draws, and functions that define the proposal and target distributions, as well as an optional\nexpectation function. Without an expectation function, the draws are adapted to match the proposal distribution.\nIf an expectation function is given, the expectation is computed after adapting the draws specifically for this expectation.\n\nIWMM can also work with models fitted with [rstan](https://github.com/stan-dev/rstan) or [cmdstanr](https://github.com/stan-dev/cmdstanr)\nwhere the fitted model posterior is treated as the proposal distribution.\n\n### Example\n\nConsider a simple univariate normal model (available\nvia `example_iwmm_model(\"normal_model\")`):\n\n```{stan, eval = F, output.var = \"stanmod\"}\ndata {\nint\u003clower=0\u003e N;\nvector[N] x;\n}\nparameters {\n  real mu;\n  real log_sigma;\n}\ntransformed parameters {\n  real\u003clower=0\u003e sigma = exp(log_sigma);\n}\nmodel {\n  target += normal_lpdf(x | mu, sigma);\n}\n```\n\nWe first fit the model using Stan:\n\n```{r, eval = T, cache=T, results='hide'}\nlibrary(\"iwmm\")\n\nnormal_model \u003c- example_iwmm_model(\"normal_model\")\n\nfit \u003c- rstan::stan(\n  model_code = normal_model$model_code,\n  data = normal_model$data,\n  refresh = FALSE,\n  seed = 1234\n)\n```\n\nAfter fitting the model, let us define an expectation function that we are interested in,\nand compute the expectation:\n\n```{r, eval = T}\nexpectation_fun_first_moment \u003c- function(draws, ...) {\n  draws\n}\n\nfirst_moment \u003c- moment_match(\n  fit,\n  expectation_fun = expectation_fun_first_moment\n)\n```\n\nWe can check that the expectation matches the posterior mean\n\n```{r, eval = T}\nfirst_moment$expectation\ncolMeans(as.matrix(fit))[c(\"mu\", \"log_sigma\")]\n```\n\nThe package can also compute expectations over distributions\nthat are different than the one where the draws are from.\nLet us try to evaluate the posterior mean when the last observation is removed.\nWe achieve this by defining a `target_observation_weights`\nwhich we can use to indicate the target distribution as a vector of weights\nfor each observation in the data used to fit the model.\nA vector of ones means using the existing data.\nA zero value for some observation means that\nthe target distribution is the posterior without that specific observation.\n\n```{r, eval = T}\nfirst_moment_loo \u003c- moment_match(\n  fit,\n  target_observation_weights = append(rep(1, 9), 0),\n  expectation_fun = expectation_fun_first_moment\n)\n```\n\nLet us compare this to the posterior mean we get by actually fitting the model again without\nthe last observation.\n\n```{r, eval = T, cache=T, results='hide'}\nloo_data \u003c- normal_model$data\nloo_data$x \u003c- loo_data$x[-loo_data$N]\nloo_data$N \u003c- loo_data$N - 1\n\nfit_loo \u003c- rstan::stan(\n  model_code = normal_model$model_code,\n  data = loo_data,\n  refresh = FALSE,\n  seed = 1234\n)\n```\n\n\n```{r, eval = T}\nfirst_moment_loo$expectation\ncolMeans(as.matrix(fit_loo))[c(\"mu\", \"log_sigma\")]\n```\n\n\n\n\n## References\n\nPaananen, T., Piironen, J., Bürkner, P.-C., and Vehtari, A.\nImplicitly Adaptive Importance Sampling. _Stat Comput_ **31**, 16 (2021).\n([paper](https://doi.org/10.1007/s11222-020-09982-2))([code](https://github.com/topipa/iter-mm-paper))\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftopipa%2Fiwmm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftopipa%2Fiwmm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftopipa%2Fiwmm/lists"}