For now I am using the following, which seems to work. I didn't really need all the functionality from sample(); I just needed to get a random index (but with non-uniform probabilities). It might be cleaner to use std::accumulate in rcategorical, but I got this working first. Also, rcategorical requires a double[K]; how do I cast my probabilities argument P as a doubles array? I naively thought P would already be an array of doubles, but rcategorical(P,K) doesn't compile. <div>
<br></div><div>Any thoughts/suggestions on how to improve on this first attempt?</div><div>Thanks in advance,</div><div>Chris</div><div><br></div><div>###<br><div><br></div><div><div>inc <-</div><div>'int rcategorical(double probs[],int K) {</div>
<div> double total = 0;</div><div> double cumpr = 0;</div><div> int k = 0;</div><div> for (k = 0; k < K; k++) {</div><div> total += probs[k];</div><div> }</div><div> double u = unif_rand();</div><div> for (k = 0; k < K; k++) {</div>
<div> cumpr += probs[k] / total;</div><div> if (u < cumpr) break;</div><div> }</div><div> return(k);</div><div>}'</div><div><br></div><div>src <- '</div><div>Rcpp:NumericVector probs(P);</div><div>int K = probs.size();</div>
<div>double pr[K];</div><div>for (int k = 0; k < K; k++) { // copy elements over one-by-one? Noob status, but works. :-)</div><div> pr[k] = probs[k];</div><div>}</div><div>int R = rcategorical(pr,K);</div><div>return wrap(R);</div>
<div>'</div><div>fun <- cxxfunction(signature(P="numeric"),includes=inc,body = src, plugin="Rcpp")</div><div>p <- c(.2,.3,.4,.1)</div><div>fun(p)</div><br><div class="gmail_quote">On Sat, May 14, 2011 at 6:19 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org">edd@debian.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im"><br>
On 14 May 2011 at 18:01, Chris DuBois wrote:<br>
| Thanks Dirk. Passing the addition function in the includes argument worked<br>
| great. <br>
|<br>
| Also, one of the links from your suggested Google search led me to this<br>
| (working) example for std::accumulate:<br>
|<br>
| src <- 'NumericVector xx(x);<br>
| return wrap( std::accumulate( xx.begin(), xx.end(), 0.0));'<br>
| fx <- cxxfunction(signature( x = "numeric" ),body=src,plugin = "Rcpp")<br>
| fx(1:10) # 55<br>
|<br>
| I had been copying the quickref and using: std::accumulate( xx.begin(),<br>
| xx.end(),std::plus<double>(),0.0)<br>
|<br>
| I think this might be a typo where the last two arguments are transposed (which<br>
| I shouldn't have noticed after browsing the function's c++ reference), as the<br>
| following works just fine: std::accumulate( xx.begin(), xx.end<br>
| (),0.0,std::plus<double>())<br>
<br>
</div>Good catch. The arguments are<br>
<br>
start, end, intial value, (optional) operator function<br>
<br>
and std::plus<double>() is the default for summation. So that was an error,<br>
and hence a Thank You! for catching it. I just committed this new section:<br>
<br>
<br>
\paragraph{STL interface}~<br>
\newline<br>
<<lang=cpp>>=<br>
// sum a vector from beginning to end<br>
double s = std::accumulate(x.begin(),<br>
x.end(), 0.0);<br>
// prod of elements from beginning to end<br>
int p = std::accumulate(vec.begin(),<br>
vec.end(), 1, std::multiplies<int>());<br>
// inner_product to compute sum of squares<br>
double s2 = std::inner_product(res.begin(),<br>
res.end(), res.begin(), 0.0);<br>
@<br>
<br>
<br>
which shows std::multiplies<int>() as an alternative operator for accumulate<br>
as well as inner_product for a sum-of-squares computation (as once suggested<br>
by Doug).<br>
<div><div></div><div class="h5"><br>
Dirk<br>
<br>
--<br>
Gauss once played himself in a zero-sum game and won $50.<br>
-- #11 at <a href="http://www.gaussfacts.com" target="_blank">http://www.gaussfacts.com</a><br>
</div></div></blockquote></div><br></div></div>