emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Babel support for Stan
@ 2015-08-24  2:36 Kyle Meyer
  2015-08-24 14:51 ` Nicolas Goaziou
  2015-08-24 15:31 ` Thomas S. Dye
  0 siblings, 2 replies; 5+ messages in thread
From: Kyle Meyer @ 2015-08-24  2:36 UTC (permalink / raw)
  To: Org-mode

[-- Attachment #1: Type: text/plain, Size: 771 bytes --]

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 #2: ob-stan.el --]
[-- Type: application/emacs-lisp, Size: 2986 bytes --]

[-- Attachment #3: ob-stan-example.org --]
[-- Type: text/plain, Size: 3518 bytes --]


* 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:

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-08-24 16:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-24  2:36 Babel support for Stan Kyle Meyer
2015-08-24 14:51 ` Nicolas Goaziou
2015-08-24 16:40   ` Kyle Meyer
2015-08-24 15:31 ` Thomas S. Dye
2015-08-24 16:40   ` Kyle Meyer

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).