xiaohongshu
xiaohongshu
mcmc-sampling-stan
Guide for performing Markov Chain Monte Carlo (MCMC) sampling using RStan or PyStan. This skill should be used when implementing Bayesian statistical models, fitting hierarchical models, working with Stan modeling language, or running MCMC diagnostics. Applies to tasks involving posterior samplin...
Full skill instructions
This skill provides guidance for implementing Bayesian models and running MCMC sampling using Stan (via RStan or PyStan). It covers model specification, prior implementation, sampling configuration, and critical diagnostic checks that must be performed to validate results.
Before writing any Stan code:
Verify Stan installation and version
packageVersion("rstan") and test compilation with a simple modelCheck system dependencies first
g++, make, and development libraries are installedCommon installation pitfalls
install.packages() does not accept a version parameter for CRAN packagesremotes::install_version() or install from sourceBefore implementing the model:
Analyze the data structure
Identify potential issues
Document data characteristics
When writing the Stan model:
Prior specification
target += for log-probability contributions (e.g., target += -2.5 * log(alpha + beta) for p(α,β) ∝ (α+β)^(-5/2))Parameter constraints
real<lower=0>, real<lower=0, upper=1>, etc.real<lower=0.001>)Model block structure
Numerical stability considerations
Configure MCMC sampling appropriately:
Iteration and warmup settings
iter for warmup, half for samplingwarmup parameter for clarityiter = 100000 with default settings = 50,000 warmup + 50,000 samples per chainControl parameters
adapt_delta: Increase (e.g., 0.95 or 0.99) if divergent transitions occurmax_treedepth: Increase (e.g., 15) if hitting tree depth limitsChain configuration
Never skip diagnostics. Successful sampling completion does not guarantee valid results.
Convergence diagnostics (MANDATORY)
Sampling diagnostics (MANDATORY)
Visual diagnostics (recommended)
How to access diagnostics in RStan
# Summary with R-hat and ESS
print(fit, pars = c("alpha", "beta"))
# Check for divergences
sampler_params <- get_sampler_params(fit, inc_warmup = FALSE)
sum(sapply(sampler_params, function(x) sum(x[, "divergent__"])))
# Check tree depth
sum(sapply(sampler_params, function(x) sum(x[, "treedepth__"] >= 15)))
Interpreting diagnostic failures
adapt_deltaAfter diagnostics pass:
Extract posterior summaries
Validate against expectations
Output file handling
writeLines() or cat() with explicit formattinginstall.packages() (use remotes::install_version() instead)Before considering the task complete, verify:
For detailed information on Stan diagnostics and model reparameterization, consult:
references/stan_diagnostics.md - Detailed diagnostic interpretation guidexiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience