Skip to main content
5 of 6
added 172 characters in body
Picaud Vincent
  • 2.5k
  • 15
  • 23

Update: 2/7/2019 I have just released a new version of the package: MathematicaStan v2.0


I just have released a beta version of MathematicaStan, a package to interact with CmdStan.

https://github.com/vincent-picaud/MathematicaStan

Usage example:

(* Defines the working directory and loads CmdStan.m *) SetDirectory["~/GitHub/MathematicaStan/Examples/Bernoulli"] Needs["CmdStan`"] (* Generates the Bernoulli Stan code and compiles it *) stanCode="data { int<lower=0> N; int<lower=0,upper=1> y[N]; } parameters { real<lower=0,upper=1> theta; } model { theta ~ beta(1,1); for (n in 1:N) y[n] ~ bernoulli(theta); }"; Export["bernoulli.stan",stanCode,"Text"] (* Compile your code. * Caveat: this can take some time *) StanCompile["bernoulli"] 

--- Translating Stan model to C++ code --- bin/stanc \ /home/pix/GitHub/MathematicaStan/Examples/Bernoulli/bernoulli.stan
--o=/home/pix/GitHub/MathematicaStan/Examples/Bernoulli/bernoulli.hpp Model name=bernoulli_model Input file=/home/pix/GitHub/MathematicaStan/Examples/Bernoulli/
bernoulli.stan Output file=/home/pix/GitHub/MathematicaStan/Examples/Bernoulli/
bernoulli.hpp

--- Linking C++ model --- g++ -I src -I stan/src -isystem stan/lib/stan_math/ -isystem \ stan/lib/stan_math/lib/eigen_3.2.8 -isystem \ stan/lib/stan_math/lib/boost_1.60.0 -isystem \ stan/lib/stan_math/lib/cvodes_2.8.2/include -Wall -DEIGEN_NO_DEBUG
-DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS
-DFUSION_MAX_VECTOR_SIZE=12 -DNO_FPRINTF_OUTPUT -pipe -lpthread
-O3 -o /home/pix/GitHub/MathematicaStan/Examples/Bernoulli/bernoulli \ src/cmdstan/main.cpp -include
/home/pix/GitHub/MathematicaStan/Examples/Bernoulli/bernoulli.hpp
stan/lib/stan_math/lib/cvodes_2.8.2/lib/libsundials_nvecserial.a
stan/lib/stan_math/lib/cvodes_2.8.2/lib/libsundials_cvodes.a

(* Generates some data and saves them (RDump file) *) n=1000; y=Table[Random[BernoulliDistribution[0.2016]],{i,1,n}]; RDumpExport["bernoulli",{{"N",n},{"y",y}}]; (* Runs Stan and gets result *) StanRunSample["bernoulli"] output=StanImport["output.csv"]; 

(Not shown because too long, CmdStan output: MCMC sampling)

(* You can access to output: variable names, data matrix... *) StanImportHeader[output] Dimensions[StanImportData[output]] Take[StanImportData[output],3] 

{{"lp__", 1}, {"accept_stat__", 2}, {"stepsize__", 3}, {"treedepth__", 4}, {"n_leapfrog__", 5}, {"divergent__", 6}, {"energy__", 7}, {"theta", 8}}

{1000, 8}

{{-532.463, 0.693148, 1.47886, 1., 1., 0., 533.321, 0.226882}, {-532.563, 0.974395, 1.47886, 1., 1., 0., 532.581, 0.230357}, {-532.629, 0.982728, 1.47886, 1., 1., 0., 532.7, 0.231909}}

(* Plots theta 1000 sample and associated histogram *) ListLinePlot[Flatten[StanVariableColumn["theta", output]],PlotLabel->"\[Theta]"] Histogram[Flatten[StanVariableColumn["theta", output]],PlotLabel->"\[Theta]"] 

enter image description here

enter image description here

Feedback are welcome, especially for Windows as I only use Linux.

Picaud Vincent
  • 2.5k
  • 15
  • 23