Hi all,

I'd like to put ob-stan.el (attached) in the contrib directory.  It adds
support for the Stan [1] programming language.  I wrote it a while back,
but a recent post on the Stan ML [2] made me think that others may find
it useful (although I'd guess that the intersection of Stan and Org
users is quite small).  It's short because the only output that really
makes sense is to dump the contents to a file (and maybe compile it),
which is then used by a downstream interface [3].

Please let me know if you have any comments about the implementation or
if you don't think contrib directory is a good place for it.

Thanks.

[1] http://mc-stan.org/
[2] https://groups.google.com/d/msg/stan-users/m4r8iUNiLug/Gexo8qCIBgAJ
[3] http://mc-stan.org/interfaces/

--
Kyle

Attachment: ob-stan.el
Description: application/emacs-lisp

* With RStan

#+name: normal-stan
#+begin_src stan :file model.stan
  data {
    int<lower=1> N;
    vector[N] x;
  }

  parameters {
    real mu;
    real<lower=0> std;
  }

  model {
    x ~ normal(mu, std);
  }
#+end_src

#+RESULTS: normal-stan
[[file:model.stan]]

#+begin_src R :session *R* :var model=normal-stan :results silent
  library(rstan)

  N <- 50
  x <- rnorm(N, 20, 3)

  fit <- stan(file=model, data=list(N=N, x=x))
#+end_src

* With CmdStan

#+begin_src elisp :results silent
  (setq org-babel-stan-cmdstan-directory "~/src/cmdstan/")
#+end_src

#+name: normal-compile
#+begin_src stan :file normal
  data {
    int<lower=1> N;
    vector[N] x;
  }

  parameters {
    real mu;
    real<lower=0> std;
  }

  model {
    x ~ normal(mu, std);
  }
#+end_src

#+RESULTS: normal-compile
[[file:normal]]

#+begin_src R :session *R* :results silent
  stan_rdump(c('N', 'x'), 'normal.data.R')
#+end_src

#+begin_src sh :results output drawer
  ./normal sample data file=normal.data.R
#+end_src

#+RESULTS:
:RESULTS:
 method = sample (Default)
   sample
     num_samples = 1000 (Default)
     num_warmup = 1000 (Default)
     save_warmup = 0 (Default)
     thin = 1 (Default)
     adapt
       engaged = 1 (Default)
       gamma = 0.050000000000000003 (Default)
       delta = 0.80000000000000004 (Default)
       kappa = 0.75 (Default)
       t0 = 10 (Default)
       init_buffer = 75 (Default)
       term_buffer = 50 (Default)
       window = 25 (Default)
     algorithm = hmc (Default)
       hmc
         engine = nuts (Default)
           nuts
             max_depth = 10 (Default)
         metric = diag_e (Default)
         stepsize = 1 (Default)
         stepsize_jitter = 0 (Default)
 id = 0 (Default)
 data
   file = normal.data.R
 init = 2 (Default)
 random
   seed = 1573443700
 output
   file = output.csv (Default)
   diagnostic_file =  (Default)
   refresh = 100 (Default)


Gradient evaluation took 4e-06 seconds
1000 transitions using 10 leapfrog steps per transition would take 0.04 seconds.
Adjust your expectations accordingly!


Iteration:    1 / 2000 [  0%]  (Warmup)

Informational Message: The current Metropolis proposal is about to be rejected 
because of the following issue:
stan::prob::normal_log: Scale parameter is 0, but must be > 0!
If this warning occurs sporadically, such as for highly constrained variable 
types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely 
ill-conditioned or misspecified.
Iteration:  100 / 2000 [  5%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  300 / 2000 [ 15%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  500 / 2000 [ 25%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  700 / 2000 [ 35%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration:  900 / 2000 [ 45%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1100 / 2000 [ 55%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1300 / 2000 [ 65%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1500 / 2000 [ 75%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1700 / 2000 [ 85%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 1900 / 2000 [ 95%]  (Sampling)
Iteration: 2000 / 2000 [100%]  (Sampling)

#  Elapsed Time: 0.013356 seconds (Warm-up)
#                0.024708 seconds (Sampling)
#                0.038064 seconds (Total)

:END:

Reply via email to