All Projects → jhelvy → stanTuneR

jhelvy / stanTuneR

Licence: GPL-3.0 license
This code uses the algebra solver in Stan (https://mc-stan.org/) to find the parameters of a distribution that produce a desired tail behavior.

Programming Languages

r
7636 projects
Stan
76 projects

Projects that are alternatives of or similar to stanTuneR

stan-distributions
A web app to visualize distributions in Stan. Uses Stan Math C++ compiled to Webassembly to evaluate the functions using actual Stan implementations. Uses d3.js for visualizations.
Stars: ✭ 30 (+7.14%)
Mutual labels:  stan, distributions
koa-smart
A framework base on Koajs2 with Decorator, Params checker and a base of modules (cors, bodyparser, compress, I18n, etc…) to let you develop smart api easily
Stars: ✭ 31 (+10.71%)
Mutual labels:  parameters
cleye
👁‍🗨 cleye — The intuitive & typed CLI development tool for Node.js
Stars: ✭ 235 (+739.29%)
Mutual labels:  parameters
bayesian
Bindings for Bayesian TidyModels
Stars: ✭ 33 (+17.86%)
Mutual labels:  stan
opzioni
The wanna-be-simplest command line arguments library for C++
Stars: ✭ 29 (+3.57%)
Mutual labels:  parameters
walker
Bayesian Generalized Linear Models with Time-Varying Coefficients
Stars: ✭ 38 (+35.71%)
Mutual labels:  stan
MathematicaStan
A Mathematica package to interact with CmdStan
Stars: ✭ 27 (-3.57%)
Mutual labels:  stan
dgo
Dynamic estimation of group-level opinion in R
Stars: ✭ 15 (-46.43%)
Mutual labels:  rstan
whitelister
Simple, basic filtering and validation tool for Node.js.
Stars: ✭ 46 (+64.29%)
Mutual labels:  parameters
ethereumjs-common
Project is in active development and has been moved to the EthereumJS VM monorepo.
Stars: ✭ 25 (-10.71%)
Mutual labels:  parameters
lkm-sandbox
Collection of Linux Kernel Modules and PoC to discover, learn and practice Linux Kernel Development
Stars: ✭ 36 (+28.57%)
Mutual labels:  parameters
geostan
Bayesian spatial analysis
Stars: ✭ 40 (+42.86%)
Mutual labels:  stan
survHE
Survival analysis in health economic evaluation Contains a suite of functions to systematise the workflow involving survival analysis in health economic evaluation. survHE can fit a large range of survival models using both a frequentist approach (by calling the R package flexsurv) and a Bayesian perspective.
Stars: ✭ 32 (+14.29%)
Mutual labels:  rstan
covidestim
Bayesian nowcasting with adjustment for delayed and incomplete reporting to estimate COVID-19 infections in the United States
Stars: ✭ 20 (-28.57%)
Mutual labels:  stan
TrendinessOfTrends
The Trendiness of Trends
Stars: ✭ 14 (-50%)
Mutual labels:  stan
metaBMA
Bayesian Model Averaging for Random and Fixed Effects Meta-Analysis
Stars: ✭ 20 (-28.57%)
Mutual labels:  stan
actuar
Actuarial functions and heavy tailed distributions for R
Stars: ✭ 19 (-32.14%)
Mutual labels:  distributions
rack-params
`Rack::Request.params` validation and type coercion, on Rack.
Stars: ✭ 31 (+10.71%)
Mutual labels:  parameters
gsoc17-hhmm
Bayesian Hierarchical Hidden Markov Models applied to financial time series, a research replication project for Google Summer of Code 2017.
Stars: ✭ 102 (+264.29%)
Mutual labels:  stan
TestNG-Foundation
TestNG Foundation is a lightweight collection of TestNG listeners, interfaces, and static utility classes that supplement and augment the functionality provided by the TestNG API.
Stars: ✭ 12 (-57.14%)
Mutual labels:  parameters

stanTuneR

This code uses the algebra solver in Stan to find the parameters of a distribution that produce a desired tail behavior. This can be a useful tool when choosing parameters for prior distributions. Here’s how to use it:

  1. Choose a distribution.
  2. Define the quantile boundaries and the amount of probability density that you wish to have above and below those boundaries.
  3. Let Stan go find the parameters that produce the desired distribution.

Currently supported distributions:

  • Normal
  • Log-Normal
  • Beta
  • Gamma
  • Inverse Gamma

Required libraries:

Obviously you need to have Stan installed on your machine.

If you don’t want to use the shiny app interface, you only need to have the rstan library installed:

install.packages('rstan')

To use the shiny app, you will also need to install the shiny and shinycssloaders libraries:

install.packages('shiny')
install.packages('shinycssloaders')

Using the Shiny app

To use the Shiny app, just run the following code in R:

library(shiny)
runGitHub('jhelvy/stanTuneR')

The interface should look like this:

screenshot

Example without the Shiny app

(see the examples.R file for more examples)

Let’s say I want to find the parameters of a normal distribution such that P[x < -2.0] ~ 0.01 and P[x > 2.0] ~ 0.01. That is, I want a normal distribution where 98% of the probability density is between (-2, 2).

First, load the rstan library, tweak some settings, and source the functions.R file (all functions are loaded in a new environment called funcs):

# Load libraries
library(shiny)
library(rstan)

# Stan settings
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

# Load the functions
funcs <- new.env()
source('functions.R', local=funcs)

Then use the targets argument to set the desired tail properties:

targets = list(
    bound_L = -2,   # LOWER quantile boundary
    bound_U = 2,    # UPPER quantile boundary
    dens_L  = 0.01, # Target density below LOWER quantile boundary
    dens_U  = 0.01) # Target density above UPPER quantile boundary

Then use the tuneParams function to find the parameters:

results = funcs$tuneParams(distribution='normal', targets)
## 
## SAMPLING FOR MODEL 'model' NOW (CHAIN 1).
## Chain 1: Iteration: 1 / 1 [100%]  (Sampling)
## Chain 1: 
## Chain 1:  Elapsed Time: 0 seconds (Warm-up)
## Chain 1:                0.001128 seconds (Sampling)
## Chain 1:                0.001128 seconds (Total)
## Chain 1:

View the resulting parameters and verify that the quantiles of 10,000 draws from the resulting distribution match your criteria:

results$params
## $mu
## [1] 0
## 
## $sigma
## [1] 0.859717
results$quantiles
##        1%       99% 
## -2.027173  2.066950

Finally, view a histogram of the resulting distribution:

results$histogram

Explanation of the backend

The meat of this app is in the functions.R code. The main function is the tuneParams() function. After the user defines the distribution and the targets for the quantiles and density, tuneParams() calls the generateStanCode() function to generate the Stan code for the model, which is written to the file model.stan. This file is always overwritten every time the generateStanCode() function is called. It then calls the stan function to fit the model. Finally, once the model is fit, it calls the summarizeResults() function to extract the results of the fit model. The output of tuneParams() is a list with the following values:

  • params : The fit parameters
  • draws : 10,000 draws from the resulting distribution using the fit parameters
  • quantiles : The quantiles of the draws at the desired upper and lower quantile boundaries
  • histogram : A histogram of the draws

Author and License

  • Author: John Paul Helveston (www.jhelvy.com)
  • Date First Written: Tuesday, April 30, 2019
  • License: GPL-3
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].