<br><font size=2 face="sans-serif">have you looked at pfp, the haskell
&quot;</font><font size=3>probabilistic functional programming library
</font><font size=2 face="sans-serif">&quot;?</font>
<br>
<br><font size=2 face="sans-serif">http://web.engr.oregonstate.edu/~erwig/pfp/</font>
<br>
<br><font size=2 face="sans-serif">the paper </font>
<br>
<br><font size=2 face="sans-serif">http://web.engr.oregonstate.edu/~erwig/papers/abstracts.html#JFP06a</font>
<br>
<br><font size=2 face="sans-serif">describes modeling various statisticy
things this way, like tree growth and the monty hall problem, I think it's
likely this &nbsp;is applicable to monte carlo processes as well.</font>
<br>
<br><font size=2 face="sans-serif">thomas.</font>
<br>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td width=40%><font size=1 face="sans-serif"><b>Paul Johnson &lt;paul@cogito.org.uk&gt;</b>
</font>
<br><font size=1 face="sans-serif">Sent by: haskell-cafe-bounces@haskell.org</font>
<p><font size=1 face="sans-serif">08/15/2007 02:38 PM</font>
<td width=59%>
<table width=100%>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">To</font></div>
<td><font size=1 face="sans-serif">Chad Scherrer &lt;chad.scherrer@gmail.com&gt;</font>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">cc</font></div>
<td><font size=1 face="sans-serif">haskell-cafe@haskell.org</font>
<tr valign=top>
<td>
<div align=right><font size=1 face="sans-serif">Subject</font></div>
<td><font size=1 face="sans-serif">Re: [Haskell-cafe] monte carlo trouble</font></table>
<br>
<table>
<tr valign=top>
<td>
<td></table>
<br></table>
<br>
<br>
<br><tt><font size=2>Chad Scherrer wrote:<br>
&gt; There's a problem I've been struggling with for a long time...<br>
&gt;<br>
&gt; I need to build a function<br>
&gt; buildSample :: [A] -&gt; State StdGen [(A,B,C)]<br>
&gt; &nbsp; <br>
&gt; given lookup functions<br>
&gt; f :: A -&gt; [B]<br>
&gt; g :: A -&gt; [C]<br>
&gt;<br>
&gt; The idea is to first draw randomly form the [A], then apply each<br>
&gt; lookup function and draw randomly from the result of each.<br>
&gt; &nbsp; <br>
I don't understand why this returns a list of triples instead of a <br>
single triple. &nbsp;Your description below seems to imply the latter.<br>
<br>
You should probably look at the &quot;Gen&quot; monad in Test.QuickCheck,
which is <br>
basically a nice implementation of what you are doing with &quot;State
<br>
StdGen&quot; below. &nbsp;Its &quot;elements&quot; function gets a single
random element, <br>
and you can combine it with replicateM to get a list of defined length.<br>
<br>
(BTW, are you sure want multiple random samples rather than a shuffle?
&nbsp;<br>
A shuffle has each element exactly once whereas multiple random samples
<br>
can pick any element an arbitrary number of times. &nbsp;I ask because
<br>
shuffles are a more common requirement. &nbsp;For the code below I'll assume
<br>
you meant what you said.)<br>
<br>
Using Test.QuickCheck I think you want something like this (which I have
<br>
not tested):<br>
<br>
 &nbsp; buildSample :: [A] -&gt; Gen (A,B,C)<br>
 &nbsp; buildSample xs = do<br>
 &nbsp; &nbsp; &nbsp;x &lt;- elements xs<br>
 &nbsp; &nbsp; &nbsp;f1 &lt;- elements $ f x<br>
 &nbsp; &nbsp; &nbsp;g1 &lt;- elements $ g x<br>
 &nbsp; &nbsp; &nbsp;return<br>
<br>
If you want n such samples then I would suggest<br>
<br>
 &nbsp; samples &lt;- replicateM n $ buildSample xs<br>
&gt; It's actually slightly more complicated than this, since for the real<br>
&gt; problem I start with type [[A]], and want to map buildSample over<br>
&gt; these, and sample from the results.<br>
&gt;<br>
&gt; There seem to be so many ways to deal with random numbers in Haskell.<br>
&gt; &nbsp; <br>
Indeed.<br>
&gt; After some false starts, I ended up doing something like<br>
&gt;<br>
&gt; sample :: [a] -&gt; State StdGen [a]<br>
&gt; sample [] = return []<br>
&gt; sample xs = do<br>
&gt; &nbsp; g &lt;- get<br>
&gt; &nbsp; let (g', g'') = split g<br>
&gt; &nbsp; &nbsp; &nbsp; bds = (1, length xs)<br>
&gt; &nbsp; &nbsp; &nbsp; xArr = listArray bds xs<br>
&gt; &nbsp; put g''<br>
&gt; &nbsp; return . map (xArr !) $ randomRs bds g'<br>
&gt; &nbsp; <br>
Not bad, although you could instead have a sample function that returns
<br>
a single element and then use replicateM to get a list.<br>
&gt; buildSample xs = sample $ do<br>
&gt; &nbsp; x &lt;- xs<br>
&gt; &nbsp; y &lt;- f x<br>
&gt; &nbsp; z &lt;- g x<br>
&gt; &nbsp; return (x,y,z)<br>
&gt;<br>
&gt; This is really bad, since it builds a huge array of all the<br>
&gt; possibilities and then draws from that. Memory is way leaky right
now.<br>
&gt; I'd like to be able to just have it apply the lookup functions as<br>
&gt; needed.<br>
&gt;<br>
&gt; Also, I'm still using GHC 6.6, so I don't have<br>
&gt; Control.Monad.State.Strict. Not sure how much difference this makes,<br>
&gt; but I guess I could just copy the source for that module if I need
to.<br>
&gt; &nbsp; <br>
Strictness won't help. &nbsp;In fact you would be better with laziness
if <br>
that were possible (which it isn't here). &nbsp;The entire array has to
be <br>
constructed before you can look up any elements in it. &nbsp;That forces
the <br>
entire computation. &nbsp; But compare your implementation of buildSample
to <br>
mine.<br>
<br>
Paul.<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
Haskell-Cafe@haskell.org<br>
http://www.haskell.org/mailman/listinfo/haskell-cafe<br>
</font></tt>
<br>
<br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">---</span><br>
<br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">This e-mail may contain confidential and/or privileged information. If you </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">are not the intended recipient (or have received this e-mail in error) </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">please notify the sender immediately and destroy this e-mail. Any </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">unauthorized copying, disclosure or distribution of the material in this </span><br>
<span style="font-family:sans-serif,helvetica; font-size:10pt; color:#000000">e-mail is strictly forbidden.</span><br>