Prev: Google App Inventor compiler written in Lisp
Next: Portable Lisp (was: tabs in format iterator)
From: Tamas K Papp on 14 Jul 2010 05:48 Hi, I am experimenting with calling JAGS (an automated Gibbs sampler, not unlike BUGS) from CL. BUGS/JAGS has a declarative description language for probability models, eg: model { for (i in 1:N) { y[i] ~ dnorm(mu,tau); } tau ~ dgamma(1e-3,1e-3); mu ~ dnorm(0,1e-4); } would describe y as IID draws from a N(mu,tau) distribution, with given priors. I would like to generate the above syntax from SEXPs, eg from something like (model (for (i 1 N) (~ (sub y i) (dnorm mu tau))) (~ tau (dgamma 1d-3 1d-3)) (~ mu (dnorm 0 1d-4))) Just to clarify: the model description SEXP gets translated into a string, which is written to a file. It would be nice to integrate this into CL as much as possible. Eg define the primitives in CL so SLIME or other IDE's could show function arguments for ~, dnorm, etc. And to have the possibility of writing macros, eg (contrived example): (defmacro foreach ((var length &optional (index (jags-gensym "i"))) form) `(for (,index 1 ,length) (~ (sub ,var ,index) ,form))) where jags-gensym would generate symbols that JAGS allows. I would appreciate advice on what would be the most idiomatic way to write this in CL. Thanks, Tamas
From: Pascal J. Bourguignon on 14 Jul 2010 08:44 Tamas K Papp <tkpapp(a)gmail.com> writes: > Hi, > > I am experimenting with calling JAGS (an automated Gibbs sampler, not > unlike BUGS) from CL. BUGS/JAGS has a declarative description > language for probability models, eg: > > model { > for (i in 1:N) { > y[i] ~ dnorm(mu,tau); > } > tau ~ dgamma(1e-3,1e-3); > mu ~ dnorm(0,1e-4); > } > > would describe y as IID draws from a N(mu,tau) distribution, with > given priors. > > I would like to generate the above syntax from SEXPs, eg from something like > > (model > (for (i 1 N) > (~ (sub y i) (dnorm mu tau))) > (~ tau (dgamma 1d-3 1d-3)) > (~ mu (dnorm 0 1d-4))) > > Just to clarify: the model description SEXP gets translated into a > string, which is written to a file. > > It would be nice to integrate this into CL as much as possible. Eg > define the primitives in CL so SLIME or other IDE's could show > function arguments for ~, dnorm, etc. And to have the possibility of > writing macros, eg (contrived example): > > (defmacro foreach ((var length &optional (index (jags-gensym "i"))) form) > `(for (,index 1 ,length) > (~ (sub ,var ,index) ,form))) > > where jags-gensym would generate symbols that JAGS allows. > > I would appreciate advice on what would be the most idiomatic way to > write this in CL. If you want to be able to use CL mechanisms, the easiest way is to set things up so that the _execution_ of your sexps produce the output. (defvar *jags* *standard-output*) (defmacro model (&whole w &body b) `(progn (format *jags* "model {~%") ,@b (format *jags* "}~%") ',w)) (defmacro for (&whole w (var min max) &body b) `(progn (format *jags* "for(~A in ~A:~A){~%" var min max) ,@b (format *jags* "}~%") ',w) (defmacro ~ (&whole w left right) `(progn ,left (format *jags* " ~~ ") ,right (format *jags* " ;~%") ',w)) etc -- __Pascal Bourguignon__ http://www.informatimago.com
From: Tamas K Papp on 14 Jul 2010 09:50 On Wed, 14 Jul 2010 14:44:11 +0200, Pascal J. Bourguignon wrote: > Tamas K Papp <tkpapp(a)gmail.com> writes: > >> model { >> for (i in 1:N) { >> y[i] ~ dnorm(mu,tau); >> } >> tau ~ dgamma(1e-3,1e-3); >> mu ~ dnorm(0,1e-4); >> } >> >> would describe y as IID draws from a N(mu,tau) distribution, with given >> priors. >> >> I would like to generate the above syntax from SEXPs, eg from something >> like >> >> (model >> (for (i 1 N) >> (~ (sub y i) (dnorm mu tau))) >> (~ tau (dgamma 1d-3 1d-3)) >> (~ mu (dnorm 0 1d-4))) > > If you want to be able to use CL mechanisms, the easiest way is to set > things up so that the _execution_ of your sexps produce the output. > > (defvar *jags* *standard-output*) > > (defmacro model (&whole w &body b) > `(progn > (format *jags* "model {~%") > ,@b > (format *jags* "}~%") > ',w)) Thanks. Why do you make the expressions return the quoted form? Are they useful for something? Tamas
From: Pascal J. Bourguignon on 14 Jul 2010 11:04 Tamas K Papp <tkpapp(a)gmail.com> writes: > On Wed, 14 Jul 2010 14:44:11 +0200, Pascal J. Bourguignon wrote: > >> Tamas K Papp <tkpapp(a)gmail.com> writes: >> >>> model { >>> for (i in 1:N) { >>> y[i] ~ dnorm(mu,tau); >>> } >>> tau ~ dgamma(1e-3,1e-3); >>> mu ~ dnorm(0,1e-4); >>> } >>> >>> would describe y as IID draws from a N(mu,tau) distribution, with given >>> priors. >>> >>> I would like to generate the above syntax from SEXPs, eg from something >>> like >>> >>> (model >>> (for (i 1 N) >>> (~ (sub y i) (dnorm mu tau))) >>> (~ tau (dgamma 1d-3 1d-3)) >>> (~ mu (dnorm 0 1d-4))) >> >> If you want to be able to use CL mechanisms, the easiest way is to set >> things up so that the _execution_ of your sexps produce the output. >> >> (defvar *jags* *standard-output*) >> >> (defmacro model (&whole w &body b) >> `(progn >> (format *jags* "model {~%") >> ,@b >> (format *jags* "}~%") >> ',w)) > > Thanks. Why do you make the expressions return the quoted form? Are > they useful for something? I was thinking only of interactive work, and that I'd prefer to see the high level form than something else, if nothing else can meaningfully be returned. -- __Pascal Bourguignon__ http://www.informatimago.com/
From: Francogrex on 16 Jul 2010 16:07 On Jul 14, 11:48 am, Tamas K Papp <tkp...(a)gmail.com> wrote: > Hi, > > I am experimenting with calling JAGS (an automated Gibbs sampler, not > unlike BUGS) from CL. I can't help with the coding but if you succeed in calling BUGS or JAGS from CL please keep me informed. How do you intend to supply the data and send the command for simulations (because I see you are only sending the model). Maybe a look at R libraries like BRUGS and R2winbugs can help with your project. Another possibility is to use the CL library RCL, connect to R and from it run BRUGS (or JAGS).
|
Next
|
Last
Pages: 1 2 Prev: Google App Inventor compiler written in Lisp Next: Portable Lisp (was: tabs in format iterator) |