From: Tamas K Papp on
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
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
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
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
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).