Interest in a Julia <-> Stan interface?

361 views
Skip to first unread message

Rob J. Goedman

unread,
Jul 7, 2014, 6:28:15 PM7/7/14
to julia...@googlegroups.com, stan-...@googlegroups.com
A while ago above question came up on the Julia MCMC issue list ( https://github.com/JuliaStats/MCMC.jl/issues/45 ). 

I have no idea how much interest there is in such an interface, but I wanted it at least for my own use, in addition to a similar interface to Jags (which I'm working on), and of course Julia's own MCMC.jl is readily available from METADATA.

Stan.jl is an initial version of such an interface. It is assumed users have some experience with Julia (REPL, iJulia, LightTable, etc.) and some familiarity with Stan script and data files.

To test it, three steps are needed:

1. Install CmdStan as described in the Getting Started sections 2.2, 2.3 (and 2.4 to test it) of the CmdStan Interface User's Guide (Version 2.3.0, Jun 20th, 2014).

2. Set the environment variable STAN_HOME to point to the directory where CmdStan was downloaded, e.g. on OSX I added to my .bash_profile:

   export STAN_HOME=/Users/rob/Projects/Stan/cmdstan/
   launchctl setenv STAN_HOME /Users/rob/Projects/Stan/cmdstan/

   The launchctl line (on OSX) will typically only be needed for e.g. LightTable and similar apps that do not inherit the full shell environment.

3. Install Stan.jl:

   Julia >  'Pkg.add("Stan")' .

To test the setup:

   Julia > Pkg.test("Stan")

The README.md file in the Stan.jl package contains a walk-through example, somewhat similar to section 2.4 of the CmdStan Interface User's Guide.

This is a first version of Stan.jl and no doubt will need tweaking (and more!). I can't test on non-OSX platforms either, so I will need help to iron out issues on those platforms.

Dependent on the level of interest and feedback ( please file requests/issues at https://github.com/goedman/Stan.jl/issues ), additional features will be added as time permits or through 'Pull Requests'. 

R and Python are in a very different stage of their life cycle compared to Julia and Stan, so I have opted to keep the package lightweight to track changes in both Julia and Stan. 

The two primary features currently implemented are generating a Model() to be used to automatically generate the command to execute a Stan script and the use of make to minimize the number of compilations required.

This Stan.jl version cannot activate sub-parts of Stan, like R can through Rcpp. By default Stan.jl runs 4 chains when sampling.

Regards,
Rob J. Goedman

Ben Goodrich

unread,
Jul 7, 2014, 8:52:49 PM7/7/14
to stan-...@googlegroups.com
Hi Rob,

I get

goodrich@CYBERPOWERPC:/opt/cmdstan$ echo $STAN_HOME
/opt/cmdstan
goodrich@CYBERPOWERPC
:/opt/cmdstan$ julia
               _
   _       _ _
(_)_     |  A fresh approach to technical computing
 
(_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _
| |_  __ _   |  Type "help()" to list help topics
 
| | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.2.1 (2014-02-11 06:30 UTC)
 _/
|\__'_|_|_|\__'_|  |  
|__/                   |  x86_64-linux-gnu

julia
> Pkg.add("Stan")
ERROR
: unknown package Stan
 
in add at pkg/entry.jl:45
 
in add at pkg/entry.jl:48
 
in anonymous at pkg/dir.jl:28
 
in cd at file.jl:22
 
in cd at pkg/dir.jl:28
 
in add at pkg.jl:19

But it is interesting to have another interface. You might have a look at Brian's Julia stuff

https://github.com/brian-j-smith/Mamba.jl

He seems to have a NUTS sampler.

Ben

Rob J. Goedman

unread,
Jul 7, 2014, 9:28:45 PM7/7/14
to stan-...@googlegroups.com
Hi Ben,

Ah, I apologize, I should have stated that Stan.jl only works on the 0.3-pre-release of Julia. This is more a DataFrame restriction than the Stan interface.

I'll certainly will take a look at Brian's stuff, wasn't aware of it. Thanks for the pointer!

Regards,
Rob J. Goedman




--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bob Carpenter

unread,
Jul 8, 2014, 1:48:48 PM7/8/14
to stan-...@googlegroups.com
Here's the NUTS implementation:

https://github.com/brian-j-smith/Mamba.jl/blob/master/src/samplers/nuts.jl

From this code example, Julia looks pretty nice syntactically,
though not having statement end semicolons so whitespace matters
within statements would drive me crazy the same way it does in R.

- Bob

Rob J. Goedman

unread,
Jul 8, 2014, 11:25:09 PM7/8/14
to stan-...@googlegroups.com
Thanks Bob,

Yes, I'm going through the examples and comparing the results. And Mamba's implementation/framework. It looks very useful and has good documentation!.

It is a pity Julia v0.2.1 didn't work for Ben (Mamba would not have worked either), as I tried to implement something along the lines of the recent discussions.

Regards,
Rob J. Goedman
goe...@icloud.com




Ben Goodrich

unread,
Sep 17, 2014, 8:01:39 PM9/17/14
to stan-...@googlegroups.com
I have Julia 0.3 now, and things seemed to work on Debian. The only thing is that it keeps saying

STAN_HOME or CMDSTAN_HOME not found.

when, in fact, I have set STAN_HOME and it appears to work:

goodrich@CYBERPOWERPC:/opt/cmdstan$ echo $STAN_HOME
/opt/cmdstan
goodrich@CYBERPOWERPC
:/opt/cmdstan$ julia
               _
   _       _ _
(_)_     |  A fresh approach to technical computing
 
(_)     | (_) (_)    |  Documentation: http://docs.julialang.org

   _ _   _
| |_  __ _   |  Type "help()" for help.
 
| | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.3.0
 _/
|\__'_|_|_|\__'_|  |  
|__/                   |  x86_64-linux-gnu

julia
> Pkg.add("Stan")

INFO
: Initializing package repository /home/goodrich/.julia/v0.3
INFO
: Cloning METADATA from git://github.com/JuliaLang/METADATA.jl
INFO
: Cloning cache of Stan from git://github.com/goedman/Stan.jl.git
INFO
: Installing Stan v0.0.3
INFO
: Package database updated

julia
> Pkg.test("Stan")
INFO
: Testing Stan
Running tests:

 
* test_utilities.jl *
STAN_HOME
or CMDSTAN_HOME not found.
`echo 2`
`echo 3 4`
`echo 5`

1
2
3
4
5

hello
hello
hello
hello
hello
hello

 
* test_cmdtype.jl *

File bernoulli.stan will be updated.


  name
=                    "bernoulli"
  nchains
=                 4
  model_file
=              "bernoulli.stan"
  data_file
=                ""
  output
=                  Output()
    file
=                    ""
    diagnostics_file
=        ""
    refresh
=                 100
Optimize(Lbfgs(0.001,1.0e-8,1.0e-8,1.0e-8,5), 2000, false)

  name
=                    "bernoulli"
  nchains
=                 4
  model_file
=              "bernoulli.stan"
  data_file
=                ""
  output
=                  Output()
    file
=                    ""
    diagnostics_file
=        ""
    refresh
=                 100
  method
=                  Optimize()
    algorithm
=               Lbfgs()
      init_alpha
=              0.001
      tol_obj
=                 1.0e-8
      tol_grad
=                1.0e-8
      tol_param
=               1.0e-8
      history_size
=            5
    iterations
=              2000
    save_iterations
=         false

Sample(1000, 1000, false, 10, Adapt(true,0.05,0.8,0.75,10.0,75,50,25), Hmc(Nuts(10),diag_e(),1.0,1.0))

  method
=                  Sample()
    num_samples
=             1000
    num_warmup
=              1000
    save_warmup
=             false
    thin
=                    10
    algorithm
=               HMC()
      engine
=                  NUTS()
        max_depth
=               10
      metric
=                  diag_e
      stepsize
=                1.0
      stepsize_jitter
=         1.0
    adapt
=                   Adapt()
      gamma
=                   0.05
      delta
=                   0.8
      kappa
=                   0.75
      t0
=                      10.0
      init_buffer
=             75
      term_buffer
=             50
      window
=                  25

Optimize(Bfgs(0.001,1.0e-9,1.0e-8,1.0e-8), 2000, true)

  method
=                  Optimize()
    algorithm
=               Bfgs()
      init_alpha
=              0.001
      tol_obj
=                 1.0e-9
      tol_grad
=                1.0e-8
      tol_param
=               1.0e-8
    iterations
=              2000
    save_iterations
=         true

Diagnose(Gradient(1.0e-6,1.0e-7))

  method
=                  Diagnose()
    diagnostic
=              Gradient()
      epsilon
=                 1.0e-6
      error
=                   1.0e-7

STAN_HOME
and CMDSTAN_HOME not found!


INFO
: Stan tests passed
INFO
: No packages to install, update or remove

julia
>

Ben
>>> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Rob J. Goedman

unread,
Sep 17, 2014, 8:43:10 PM9/17/14
to stan-...@googlegroups.com
Hi Ben,

It gets to the point where it will try to compile a Stan model with stanc.

Can you try to set both STAN_HOME and CMDSTAN_HOME as in below example.

Right now both need to be set. I use CMDSTAN_HOME to run the commands, STAN_HOME to compile a model.

I believe right now, with a standard CmdStan install,  STAN_HOME should always point to a subdir of CMDSTAN_HOME.

Regards,
Rob J. Goedman



This version of the Stan.jl package assumes that CmdStan (see http://mc-stan.org) is installed and the environment variables STAN_HOME and CMDSTAN_HOME are set accordingly (pointing to the Stan and CmdStan directories, e.g. /Users/rob/Projects/Stan/cmdstan/stan and /Users/rob/Projects/Stan/cmdstan on my system).

To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

Ben Goodrich

unread,
Sep 17, 2014, 9:03:17 PM9/17/14
to stan-...@googlegroups.com
OK. I think it is all working. Log attached.

Ben
&gt
...
julia.txt

Rob J. Goedman

unread,
Sep 17, 2014, 9:21:14 PM9/17/14
to stan-...@googlegroups.com
Thanks for trying this, Ben, the output looks fine.

It should run the different chains on different cores (if available) and under normal usage only compile when needed. 

I do clean up in Pkg.test(“Stan”), so in that case I force it to always compile, but in normal usage that is not the case.

My next step is to convert the simulation results to Mamba format and use Mamba’s convergence tests and summary stats. I have been experimenting with this in MCMCExampleRepository.jl, both for Stan.jl output and for Jags.jl output, but have not published MCMCExampleRepository.jl.

Regards,
Rob J. Goedman


-- 
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<julia.txt>

Reply all
Reply to author
Forward
0 new messages