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

* Re: Babel support for Stan
  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
  1 sibling, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2015-08-24 14:51 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: Org-mode

Hello,

Kyle Meyer <kyle@kyleam.com> writes:

> 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].

Thank you.

> 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.

I think core is fine for new languages, but you need to update file
headers accordingly.

A few comments follow.
> (defun org-babel-execute:stan (body params)
>   "Execute a block of Stan code with org-babel.
> A :file header argument must be given.  If
> `org-babel-stan-cmdstan-directory' is non-nil and the file name
> does not have a \".stan\" extension, compile the block to file.
> Otherwise, write the Stan code to the file."

BODY and PARAMS references are missing from docstring.

>   (let ((file (expand-file-name
> 	       (or (cdr (assoc :file params))

Nitpick: `assoc' -> `:assq'.

> 		   (user-error "Set :file argument to execute Stan blocks")))))
>     (if (or (not org-babel-stan-cmdstan-directory)
> 	    (org-string-match-p "\\.stan\\'" file))
> 	(with-temp-file file (insert body))
>       (with-temp-file (concat file ".stan") (insert body))
>       (let ((default-directory org-babel-stan-cmdstan-directory))
> 	(call-process-shell-command (concat "make " file))))

You don't use `org-babel-stan-cmdstan-directory' to store FILE, i.e.,
FILE is saved in current directory, or any directory provided in its
path, which doesn't necessarily match
`org-babel-stan-cmdstan-directory'. Yet, make is called on
`org-babel-stan-cmdstan-directory'.

Is it intentional?

>     nil)) ;; Signal that output has been written to file.

Nitpick: one semi-colon only for end of line comments.

Regards,

-- 
Nicolas Goaziou

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

* Re: Babel support for Stan
  2015-08-24  2:36 Babel support for Stan Kyle Meyer
  2015-08-24 14:51 ` Nicolas Goaziou
@ 2015-08-24 15:31 ` Thomas S. Dye
  2015-08-24 16:40   ` Kyle Meyer
  1 sibling, 1 reply; 5+ messages in thread
From: Thomas S. Dye @ 2015-08-24 15:31 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: Org-mode

Aloha Kyle,

Kyle Meyer <kyle@kyleam.com> writes:

> 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.

This looks neat to me.  I don't decide these things, but IMHO it would
be great to have it in contrib.  If it does go there, please consider
stubbing out some documentation using the template here:

http://orgmode.org/w/?p=worg.git;a=blob;f=org-contrib/babel/languages/ob-doc-template.org;hb=HEAD

All the best,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com

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

* Re: Babel support for Stan
  2015-08-24 14:51 ` Nicolas Goaziou
@ 2015-08-24 16:40   ` Kyle Meyer
  0 siblings, 0 replies; 5+ messages in thread
From: Kyle Meyer @ 2015-08-24 16:40 UTC (permalink / raw)
  To: Org-mode

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> I think core is fine for new languages, but you need to update file
> headers accordingly.

Will do.

> A few comments follow.

Thanks for your comments.  Updated.

> You don't use `org-babel-stan-cmdstan-directory' to store FILE, i.e.,
> FILE is saved in current directory, or any directory provided in its
> path, which doesn't necessarily match
> `org-babel-stan-cmdstan-directory'. Yet, make is called on
> `org-babel-stan-cmdstan-directory'.
>
> Is it intentional?

Yes.  The Makefile in the CmdStan directory can be used to compile any
*.stan file (regardless of whether it is in the CmdStan source
directory) into a model executable that's in the same directory as the
original *.stan file.

I'll wait a bit to see if any more comments come in and then install the
updated version in master, if that is OK with you.

Thanks.

--
Kyle

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

* Re: Babel support for Stan
  2015-08-24 15:31 ` Thomas S. Dye
@ 2015-08-24 16:40   ` Kyle Meyer
  0 siblings, 0 replies; 5+ messages in thread
From: Kyle Meyer @ 2015-08-24 16:40 UTC (permalink / raw)
  To: Thomas S. Dye; +Cc: Org-mode

Hi Tom,

Thomas S. Dye <tsd@tsdye.com> writes:

[...]
> If it does go there, please consider stubbing out some documentation
> using the template here:
>
> http://orgmode.org/w/?p=worg.git;a=blob;f=org-contrib/babel/languages/ob-doc-template.org;hb=HEAD

Thanks for the pointer.

--
Kyle

^ 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).