Hi,
I am trying to create a function that would return another function that generates draws from a beta distribution. Something like this:
(defun make-beta-generator (a b) (letm ((rng (random-number-generator *mt19937* 0))) (lambda () ;; of course this won't work, letm deallocated rng (beta-rd rng a b))))
As the comment says, this does not work. Is there a way to prevent letm from deallocating rng (or achieve the effect I want by some other means)?
Thanks,
Tamas
Tamas,
If you macroexpand the letm, you will find it is (LET* ((RNG (MAKE-RANDOM-NUMBER-GENERATOR *MT19937*))) (UNWIND-PROTECT (PROGN (RNG-SET RNG 0) (LAMBDA () (BETA-RD RNG A B))) (FREE RNG))) so you can just do the MAKE-RANDOM-NUMBER-GENERATOR and RNG-SET part yourself at the start, and then FREE when you're ready to clean up.
Liam
On Tue, Sep 30, 2008 at 2:32 PM, Tamas K Papp tpapp@princeton.edu wrote:
Hi,
I am trying to create a function that would return another function that generates draws from a beta distribution. Something like this:
(defun make-beta-generator (a b) (letm ((rng (random-number-generator *mt19937* 0))) (lambda () ;; of course this won't work, letm deallocated rng (beta-rd rng a b))))
As the comment says, this does not work. Is there a way to prevent letm from deallocating rng (or achieve the effect I want by some other means)?
Thanks,
Tamas
Liam,
Thanks for the solution. I worked around the issue by using the inverse distribution function and (random 1d0).
A general question about the architecture of GSLL: would it be possible to integrate it into Lisp better using finalizers? The idea is that when GSLL functions return native GSLL objects (maybe wrapped in a class, etc), they could also set up a finalizer that would free the object when it is no longer used. The library trivial-garbage provides a standardized interface to finalizers of various implementations.
Then letm would not be needed at all, and we would achieve a much better integration into CL. Just a thought. Let me know what you think,
Tamas
On Tue, Sep 30, 2008 at 10:22:25PM -0400, Liam Healy wrote:
Tamas,
If you macroexpand the letm, you will find it is (LET* ((RNG (MAKE-RANDOM-NUMBER-GENERATOR *MT19937*))) (UNWIND-PROTECT (PROGN (RNG-SET RNG 0) (LAMBDA () (BETA-RD RNG A B))) (FREE RNG))) so you can just do the MAKE-RANDOM-NUMBER-GENERATOR and RNG-SET part yourself at the start, and then FREE when you're ready to clean up.
Liam
On Tue, Sep 30, 2008 at 2:32 PM, Tamas K Papp tpapp@princeton.edu wrote:
Hi,
I am trying to create a function that would return another function that generates draws from a beta distribution. Something like this:
(defun make-beta-generator (a b) (letm ((rng (random-number-generator *mt19937* 0))) (lambda () ;; of course this won't work, letm deallocated rng (beta-rd rng a b))))
As the comment says, this does not work. Is there a way to prevent letm from deallocating rng (or achieve the effect I want by some other means)?
Thanks,
Tamas
Tamas,
I didn't know about finalizers, it sounds interesting. From a quick glance at the web page it looks like it might be useful, but it's not clear how many implementations support it. Even with that, letm might still be useful because for some GSL objects the creation and initialization steps are separate, and letm lets you combine them. With the advent of foreign-friendly arrays, letm will be mandatory for arrays; as it ultimately expands to a sb-sys:with-pinned-objects on SBCL. Also, a macro is provided for initializing the proper type array, and that type is extracted from the letm binding.
Thanks for the suggestion, I will keep it in mind.
Liam
On Thu, Oct 2, 2008 at 8:44 PM, Tamas K Papp tpapp@princeton.edu wrote:
Liam,
Thanks for the solution. I worked around the issue by using the inverse distribution function and (random 1d0).
A general question about the architecture of GSLL: would it be possible to integrate it into Lisp better using finalizers? The idea is that when GSLL functions return native GSLL objects (maybe wrapped in a class, etc), they could also set up a finalizer that would free the object when it is no longer used. The library trivial-garbage provides a standardized interface to finalizers of various implementations.
Then letm would not be needed at all, and we would achieve a much better integration into CL. Just a thought. Let me know what you think,
Tamas