<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Jonathan Olmsted]]></title>
  <link href="http://www.rochester.edu/college/gradstudents/jolmsted/atom.xml" rel="self"/>
  <link href="http://www.rochester.edu/college/gradstudents/jolmsted/"/>
  <updated>2013-05-22T22:43:11-04:00</updated>
  <id>http://www.rochester.edu/college/gradstudents/jolmsted/</id>
  <author>
    <name><![CDATA[Jonathan P. Olmsted]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Rcpp Updates: the sample function]]></title>
    <link href="http://www.rochester.edu/college/gradstudents/jolmsted/blog/2013/05/13/rcpp-updates/"/>
    <updated>2013-05-13T00:00:00-04:00</updated>
    <id>http://www.rochester.edu/college/gradstudents/jolmsted/blog/2013/05/13/rcpp-updates</id>
    <content type="html"><![CDATA[<p>Two posts on the <a href="http://gallery.rcpp.org/">rcpp-gallery</a> came out of some work
I had to do using an accept-reject sampler. I came to the project after the
sampler had already been implemented in <strong>R</strong>. Given the performance of the code
as it was, the job was going to take too long, even on powerful machines. Trying
some go-to R tricks didn&#8217;t result in enough of a performance boost, so I decided
to implement in C++ using <a href="http://www.rcpp.org/">Rcpp</a></p>

<p>The sampler itself used <strong>R</strong>&#8217;s <code>sample()</code> function, so I went looking for the
corresponding functionality in C++ (assuming Rcpp would have exposed it). And,
while much of <strong>R</strong>&#8217;s random number generation is easily accessed, there was no
clean way to hook into the C code underlying <code>sample()</code>.</p>

<p>Christian Gunning addressed this by contributing a patch to RcppArmadillo which
exposes much of the functionality of <strong>R</strong>&#8217;s <code>sample()</code>. We write about it
<a href="http://gallery.rcpp.org/articles/using-the-Rcpp-based-sample-implementation/">here</a>.</p>

<p>None of the examples in the above link really allow the Rcpp implementation to
shine, so I put together an example accept-reject sampler that runs in C++-land
thanks to Rcpp. You can see the results with fully working code
<a href="http://gallery.rcpp.org/articles/an-example-of-using-Rcpp-sample/">here</a>.</p>

<p>Altogether, you may not need to use Rcpp if all you want to do is call
<code>sample()</code> once. But, if you have to repeatedly make calls to <code>sample()</code>, the
code and the documentation exist for you to implement it readily in Rcpp.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Installing Rmpi]]></title>
    <link href="http://www.rochester.edu/college/gradstudents/jolmsted/blog/2013/04/24/rmpi/"/>
    <updated>2013-04-24T00:00:00-04:00</updated>
    <id>http://www.rochester.edu/college/gradstudents/jolmsted/blog/2013/04/24/rmpi</id>
    <content type="html"><![CDATA[<p>After having to search for the same website several times this past year,
re-setting <strong>Rmpi</strong> up on my work machine (Mac Pro) was the final straw. Thanks
to <a href="http://www.stats.uwo.ca/faculty/yu/Rmpi/">this</a> and likely others over the
last two years, I use the following for <strong>Rmpi</strong> with my <strong>macports</strong> install of
<strong>OpenMPI</strong>:</p>

<figure class='code'><figcaption><span>Rmpi Installation Code</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='r'><span class='line'>install.packages<span class="p">(</span><span class="s">&quot;Rmpi&quot;</span><span class="p">,</span> configure.args<span class="o">=</span><span class="s">&quot;</span>
</span><span class='line'><span class="s">--with-Rmpi-include=/opt/local/include/openmpi/</span>
</span><span class='line'><span class="s">--with-Rmpi-libpath=/opt/local/lib/openmpi/</span>
</span><span class='line'><span class="s">--with-Rmpi-type=OPENMPI</span>
</span><span class='line'><span class="s">&quot;</span>
</span><span class='line'><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<h2>@ Princeton</h2>

<p>If you happen to be setting up <strong>Rmpi</strong> on one of the
<a href="tigress.princeton.edu">TIGRESS</a> systems at Princeton, I suggest a slightly
different solution. It has the same effect, but is a bit more self-documenting
which allows for users to more obviously edit the setup as their environment
changes.</p>

<p>Assuming you use Bash as a shell (and if you don&#8217;t know what that means, on the
TIGRESS systems, it means you use Bash), add the following to <code>.bashrc.</code></p>

<figure class='code'><figcaption><span>.bashrc Addition</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'><span class="c">## MPI START ##</span>
</span><span class='line'><span class="nv">MPI_ROOT</span><span class="o">=</span>/usr/local/openmpi/1.4.5/gcc/x86_64/
</span><span class='line'><span class="nb">export </span>MPI_ROOT
</span><span class='line'><span class="nv">LD_LIBRARY_PATH</span><span class="o">=</span>/usr/local/openmpi/1.4.5/gcc/x86_64/lib64
</span><span class='line'><span class="nb">export </span>LD_LIBRARY_PATH
</span><span class='line'><span class="nv">PATH</span><span class="o">=</span>/usr/local/openmpi/1.4.5/gcc/x86_64/bin:<span class="nv">$PATH</span>
</span><span class='line'><span class="nb">export </span>PATH
</span><span class='line'><span class="c">## MPI END ##</span>
</span></code></pre></td></tr></table></div></figure>


<p>This sets the values of several environmental variables. This is not a flexible
approach. It is requiring you to use <strong>OpenMPI</strong> version 1.4.5 (compiled withg
<strong>GCC</strong>). Should you want something else, you need to edit these values
accordingly.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Deviates from a Truncated Normal Distribution via Rcpp]]></title>
    <link href="http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/10/10/truncated-normal-rng/"/>
    <updated>2012-10-10T00:00:00-04:00</updated>
    <id>http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/10/10/truncated-normal-rng</id>
    <content type="html"><![CDATA[<p><em>Some edits on April 24 2013</em></p>

<h2>Motivation</h2>

<p>Much of the code that I use for my dissertation project is in C++.  I
need to generate random variables from a Truncated Normal Distribution
and I could not find anything in the Boost libraries or the like. So,
I had to implement my own generator. This led me an old article:
<a href="http://dx.doi.org/10.1007%2FBF00143942">Robert (1995)</a>.</p>

<p>After implementing the sampler described in the paper, I decided to
compare its performance to that provided by the
<a href="http://cran.r-project.org/web/packages/truncnorm/index.html"><strong>truncnorm</strong></a>
package in R. While this involved me wrapping some functions around my
C++ code and creating some function definitions in R, it wasn&#8217;t much
additional work.</p>

<p> <!-- more --></p>

<h2>Benchmark</h2>

<p>Below are the results of a benchmark of the performance between my
code, <strong>rtnRcpp</strong> (which uses <strong>Rcpp</strong> heavily) and
<strong>truncnorm</strong>.</p>

<p>Deviates from a Truncated Normal Distribution with mean 0 and standard deviation
1 are pulled from both packages. Each point on the graph reflects the relative
performance of the functions from the two packages, each being called 1,000
times. I ask for three different sample sizes: 10<sup>2,</sup> 10<sup>4,</sup> and 10<sup>6.</sup> And, I
consider 6 different intervals. The specific reason for these intervals is
motivated by the different cases considered in the algorithm. Beyond that, the
details do not matter here.</p>

<p>As a note, I checked to make sure that code for both packages was
compiled with the same flags.</p>

<p><img class="right" src="http://www.rochester.edu/college/gradstudents/jolmsted/images/rtn1.png"></p>

<p>As you can see, my code (i.e. <strong>rtnRcpp</strong>) outperforms <strong>truncnorm</strong>
in every case. The function <code>rtruncnorm()</code> from the <strong>truncnorm</strong>
package is anywhere from 18-80% slower. Sample size doesn&#8217;t seem to
matter in any set way.</p>

<p>Whatmore, as best I can tell, the authors of the <strong>truncnorm</strong> package are
implementing the same algorithm from Robert (1995), so the performance
difference is driven only by the C++ code and not the underlying logic. I have
every reason to believe that it is the <strong>Rcpp</strong>-foundation which leads to my
realized speed. The <strong>truncnorm</strong> package definitely implements a different
algorithm. When implemented in straight C, the Robert (1995) algorithm fares
less well than it had when implemented in C++. Still, for most regions in the
parameter space it is faster or as fast.</p>

<h2>Release</h2>

<p>I&#8217;m happy to share details of my implementation if anyone wants to see
it.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[OpenMPI + InfiniBand]]></title>
    <link href="http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/09/30/openmpi-warning/"/>
    <updated>2012-09-30T00:00:00-04:00</updated>
    <id>http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/09/30/openmpi-warning</id>
    <content type="html"><![CDATA[<p><em>This post has been edited and the updates are noted below.</em></p>

<p>I&#8217;ve been working on a periodic rebuilding of the Beowulf cluster that
we use in
<a href="http://www.rochester.edu/college/psc/thestarlab/index"><em>the star lab</em></a>. I
stumbled upon a strange message in setting up the <strong>OpenMPI</strong>
implementation of <strong>MPI</strong>. In case it matters, but I can&#8217;t imagine it
will, the cluster is being developed on Fedora Core 16.</p>

<p> <!-- more --></p>

<h2>OpenMPI Warning Message</h2>

<p>I kept running into a (to&#8211;me cryptic) warning after getting <strong>MPI</strong>
up and running. Whenever an <strong>MPI</strong> program would run, I&#8217;d get output
like following printed to the shell:</p>

<div>
  <pre><code class='text'>librdmacm: couldn't read ABI version.
librdmacm: assuming: 4
CMA: unable to get RDMA device list
--------------------------------------------------------------------------
[[14865,1],0]: A high-performance Open MPI point-to-point messaging module
was unable to find any relevant network interfaces:

Module: OpenFabrics (openib)
Host: someserver

Another transport will be used instead, although this may result in
lower performance.
--------------------------------------------------------------------------</code></pre>
</div>


<p>where I&#8217;ve substittued <code>someserver</code> for the actual server name for the
purposes of this post. The execution works, but at the least, whatever
was going on seemed to be slowing that down. Moreover, I was fairly
sure that this output would cause constant confusion for the users
once the cluster went live.</p>

<p><em>UPDATE (Mon Oct 1 2012): It turns out this message isn&#8217;t printed in newer versions of
<strong>OpenMPI</strong> (e.g. >=1.6.2) if the hardware is absent. Although I am
constrained to using an older version, this is good to note. Thanks to
Jeff Squyres for pointing this out.</em></p>

<p>It turns out that the &#8220;problem&#8221; and its &#8220;solution&#8221; are both very
simple, but the information I found online wasn&#8217;t that good. This is
just my humble attempt to improve the signal to noise ratio.</p>

<h3>Problem</h3>

<p><strong>OpenMPI</strong> is searching the hardware on the nodes for
<a href="http://en.wikipedia.org/wiki/InfiniBand">InfiniBand</a>, and, upon
failing to find any, falls back to standard interfaces. Of course,
this is why execution of the =mpirun= command was working.</p>

<h3>Solution</h3>

<p>We can tell <code>mpirun</code> to never even check for InfiniBand hardware (and
there preventing an unsuccessful search) with the inclusion of <code>-mca
btl ^openib</code> on the command line. Specifically, a command that
originally was submitted as</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>mpirun -np 3 -hostfile ../mpihosts helloworld</span></code></pre></td></tr></table></div></figure>


<p>should now be</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>mpirun -np 3 -mca btl ^openib -hostfile ../mpihosts helloworld</span></code></pre></td></tr></table></div></figure>


<p>That is all there is to it.</p>

<p><em>UPDATE (Mon Oct 1 2012): Initially, I couldn&#8217;t find mention of a
config file to make this change (in my case)
site-wide. Unsurprisingly, this is a result of <strong>me</strong> not finding it
and not the absence of this functionality. Again, thanks to Jeff
Squyres for pointing me
<a href="http://www.open-mpi.org/faq/?category=tuning#setting-mca-params">here</a>
which covers this topic. This is now the approach used for this
project.</em></p>

<h3>Caveat</h3>

<p>This warning can still pop up if you use some other software which
wraps <strong>OpenMPI</strong> and doesn&#8217;t have this flag built in (which is not
likely). One example is the <strong>R</strong> package <strong>Rmpi</strong>. The testing of the
package which occurs as <em>install-time</em> in <strong>R</strong> includes the running
of an <strong>MPI</strong> job. Because this job isn&#8217;t run with the flag which
disables the search for InfiniBand hardware, you will see the warning
print to the shell. However, normal use of <strong>Rmpi</strong> isn&#8217;t affected,
since users run <strong>R</strong> scripts from within the <strong>MPI</strong> framework and
can include this flag.</p>

<p><em>UPDATE (Mon Oct 1 2012): See the preceding update. This message won&#8217;t
pop up after using the site-wide config.</em></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[RNG Performance with Rcpp]]></title>
    <link href="http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/08/22/rcpp-rng-performance/"/>
    <updated>2012-08-22T00:00:00-04:00</updated>
    <id>http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/08/22/rcpp-rng-performance</id>
    <content type="html"><![CDATA[<h2>Background</h2>

<p>Much of the work in my dissertation focuses on generalizing the models
used in Political Science for the estimation of ideal points. Despite
some shortcomings, there hasn&#8217;t been much development of the original
model using the canonical roll call voting data. Instead, the
innovation has occurred by using novel kinds of data (e.g. speech text
or campaign contributions).</p>

<p>In working on some of the shortcomings in how the canonical approach
is used in practice, it became necessary to implement mechanics of the
Bayesian inference problem&#8212;which uses MCMC methods&#8212;in C++. Now,
since R is a very nice interface relative to C++, the real goal is to
just perform the &#8220;hard&#8221; operations in C++ and keep the rest in
R. Enter
<a href="http://cran.r-project.org/web/packages/Rcpp/index.html">Rcpp</a>, of
course.</p>

<h2>Motivation</h2>

<p>I was making some speed improvements to my C++ code (which heavily
uses Rcpp) and I had to make the decision of how to call the RNG in
C++. I could use the old R API with something like <code>Rf_rnorm(0.0,1.0)</code>
which would return a <code>double</code> or I could use <code>rnorm(1, 0.0, 1.0)[0]</code>
which would also return a <code>double</code>. Now, this second option is not
really in the spirit of the syntactic sugar provided by Rcpp. The new
API would just as easily let me construct an <code>Rcpp::NumericVector</code> of
length N populated with N draws from the Standard Normal distribution
with an <code>rnorm(N, 0.0, 1.0)</code> expression. However, short of rewriting a
lot of code, I wanted (and needed) to take single draws many times.</p>

<p>The important question to me became, how does performance differ when
taking draws one-by-one via the old R API and the new sugary Rcpp
API. And, while I was at it, I decided to throw in comparisons to the
new Rcpp API used as intended and calling the whole thing from R.</p>

<!-- more -->


<h2>Design</h2>

<p>I use the <code>inline</code>, <code>Rcpp</code>, and <code>rbenchmark</code> packages for the
test. The first two packages are used to facilitate use of C++
code. The last provides a nice infrastructure to time and compare the
evaluation of expressions. Each of 4 approaches is used to take
100,000 draws from a Standard Normal distribution:</p>

<ol>
<li><p>loop over 100,000 singleton draws from the old R API in C++ via
Rcpp (<code>src1</code>)</p></li>
<li><p>loop over 100,000 draws singleton draws from the new Rcpp API in
C++ via Rcpp (<code>src2</code>)</p></li>
<li><p>a single call to the new Rcpp API asking for a vector of 100,000
draws in C++ via Rcpp (<code>src3</code>)</p></li>
<li><p>a single call to the standard RNG in R asking for a vector of
100,000 draws</p></li>
</ol>


<p>Now, I was certain that (3) would outperform (2), but I didn&#8217;t really
have any <em>a priori</em> expectations beyond that. You can see my source
below for the specifics.</p>

<h2>Results</h2>

<p>The results are below, lazily formatted as comments in R code. Like we
might expect, using the new Rcpp-provided API as intended
(<em>Rcpp/Inline/New RNG API/No Loop</em>) is faster than looping over it
many times while taking singleton draws (<em>Rcpp/Inline/New RNG
API/Loop</em>). Interestingly, though, the new Rcpp-provided API is the
fastest method considered. Looping over the old R API
(<em>Rcpp/Inline/Old RNG API/Loop</em>) is about 25\% slower. The old R API
method and the straight R approach (<em>Straight R</em>) are very close, with
the call to the RNG beginning in R (<em>Straight R</em>) being slightly
slower. Both of these two approaches outperform the
loop over the new Rcpp API (<em>Rcpp/Inline/New RNG API/Loop</em>).</p>

<p>For my purposes, I just needed to see that <em>Rcpp/Inline/Old RNG
API/Loop</em> outperformed <em>Rcpp/Inline/New RNG API/Loop</em>, but I couldn&#8217;t
find any of these benchmarks anywhere online, so I thought I&#8217;d put up
the full version. I always love it when I come across pareto
optimal-type results like this. If you are working in C++,
<em>Rcpp/Inline/New RNG API/No Loop</em> not only saves development time
(i.e. one line of code, not several), but it saves computational time.</p>

<div>
  <pre><code class='r'>##                              test replications elapsed relative
## 3 Rcpp/Inline/New RNG API/No Loop         1000  16.398 1.000000
## 1    Rcpp/Inline/Old RNG API/Loop         1000  20.191 1.231309
## 4                      Straight R         1000  23.163 1.412550
## 2    Rcpp/Inline/New RNG API/Loop         1000  34.193 2.085193</code></pre>
</div>


<p><em>Edit: Previous results that were posted here and have since been
replaced had no optimization performed by the compiler for the three
<code>Rcpp</code>-based functions I used. The results that are currently
displayed above were compiled with <code>-O3</code>. The rank ordering of the
original results does not change. However, some of the discussion of
the results reflects new relative performance. Additionally, the
number of replications was increased as a modest attempt to make the
relative performance results more stable.</em></p>

<h2>Source</h2>

<figure class='code'><figcaption><span>Benchmark Source</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
</pre></td><td class='code'><pre><code class='r'><span class='line'>library<span class="p">(</span>inline<span class="p">)</span>
</span><span class='line'>library<span class="p">(</span>Rcpp<span class="p">)</span>
</span><span class='line'>library<span class="p">(</span>rbenchmark<span class="p">)</span>
</span><span class='line'>
</span><span class='line'>src1 <span class="o">&lt;-</span> <span class="s">&quot;</span>
</span><span class='line'><span class="s">int N_ = as&lt;int&gt;(n) ;</span>
</span><span class='line'><span class="s">Rcpp::NumericVector draws(N_) ;</span>
</span><span class='line'><span class="s">RNGScope scope ;</span>
</span><span class='line'><span class="s">for(int i = 0; i &lt; N_; i++) {</span>
</span><span class='line'><span class="s">  draws(i) = Rf_rnorm(0.0, 1.0) ;</span>
</span><span class='line'><span class="s">}</span>
</span><span class='line'><span class="s">return draws ;</span>
</span><span class='line'><span class="s">&quot;</span>
</span><span class='line'>
</span><span class='line'>src2 <span class="o">&lt;-</span> <span class="s">&quot;</span>
</span><span class='line'><span class="s">int N_ = as&lt;int&gt;(n) ;</span>
</span><span class='line'><span class="s">Rcpp::NumericVector draws(N_) ;</span>
</span><span class='line'><span class="s">RNGScope scope ;</span>
</span><span class='line'><span class="s">for(int i = 0; i &lt; N_; i++) {</span>
</span><span class='line'><span class="s">  draws(i) = rnorm(1, 0.0, 1.0)[0] ;</span>
</span><span class='line'><span class="s">}</span>
</span><span class='line'><span class="s">return draws ;</span>
</span><span class='line'><span class="s">&quot;</span>
</span><span class='line'>
</span><span class='line'>src3 <span class="o">&lt;-</span> <span class="s">&quot;</span>
</span><span class='line'><span class="s">int N_ = as&lt;int&gt;(n) ;</span>
</span><span class='line'><span class="s">Rcpp::NumericVector draws(N_) ;</span>
</span><span class='line'><span class="s">RNGScope scope ;</span>
</span><span class='line'><span class="s">draws = rnorm(N_, 0.0, 1.0) ;</span>
</span><span class='line'><span class="s">return draws ;</span>
</span><span class='line'><span class="s">&quot;</span>
</span><span class='line'>
</span><span class='line'>GetDraws1 <span class="o">&lt;-</span> cxxfunction<span class="p">(</span>signature<span class="p">(</span>n <span class="o">=</span> <span class="s">&quot;integer&quot;</span><span class="p">),</span>
</span><span class='line'>                         body <span class="o">=</span> src1<span class="p">,</span>
</span><span class='line'>                         plugin <span class="o">=</span> <span class="s">&quot;Rcpp&quot;</span>
</span><span class='line'>                         <span class="p">)</span>
</span><span class='line'>GetDraws2 <span class="o">&lt;-</span> cxxfunction<span class="p">(</span>signature<span class="p">(</span>n <span class="o">=</span> <span class="s">&quot;integer&quot;</span><span class="p">),</span>
</span><span class='line'>                         body <span class="o">=</span> src2<span class="p">,</span>
</span><span class='line'>                         plugin <span class="o">=</span> <span class="s">&quot;Rcpp&quot;</span>
</span><span class='line'>                         <span class="p">)</span>
</span><span class='line'>GetDraws3 <span class="o">&lt;-</span> cxxfunction<span class="p">(</span>signature<span class="p">(</span>n <span class="o">=</span> <span class="s">&quot;integer&quot;</span><span class="p">),</span>
</span><span class='line'>                         body <span class="o">=</span> src3<span class="p">,</span>
</span><span class='line'>                         plugin <span class="o">=</span> <span class="s">&quot;Rcpp&quot;</span>
</span><span class='line'>                         <span class="p">)</span>
</span><span class='line'>
</span><span class='line'>.n <span class="o">&lt;-</span> <span class="m">1e5</span>
</span><span class='line'>
</span><span class='line'>benchmark<span class="p">(</span><span class="s">&quot;Rcpp/Inline/Old RNG API/Loop&quot;</span> <span class="o">=</span> <span class="p">{</span>set.seed<span class="p">(</span><span class="m">1</span><span class="p">);</span> GetDraws1<span class="p">(</span>.n<span class="p">)},</span>
</span><span class='line'>          <span class="s">&quot;Rcpp/Inline/New RNG API/Loop&quot;</span> <span class="o">=</span> <span class="p">{</span>set.seed<span class="p">(</span><span class="m">1</span><span class="p">);</span> GetDraws2<span class="p">(</span>.n<span class="p">)},</span>
</span><span class='line'>          <span class="s">&quot;Rcpp/Inline/New RNG API/No Loop&quot;</span> <span class="o">=</span> <span class="p">{</span>set.seed<span class="p">(</span><span class="m">1</span><span class="p">);</span> GetDraws3<span class="p">(</span>.n<span class="p">)},</span>
</span><span class='line'>          <span class="s">&quot;R&quot;</span> <span class="o">=</span> <span class="p">{</span>set.seed<span class="p">(</span><span class="m">1</span><span class="p">);</span> rnorm<span class="p">(</span>.n<span class="p">)},</span>
</span><span class='line'>          columns<span class="o">=</span>c<span class="p">(</span><span class="s">&quot;test&quot;</span><span class="p">,</span>
</span><span class='line'>            <span class="s">&quot;replications&quot;</span><span class="p">,</span>
</span><span class='line'>            <span class="s">&quot;elapsed&quot;</span><span class="p">,</span>
</span><span class='line'>            <span class="s">&quot;relative&quot;</span>
</span><span class='line'>            <span class="p">),</span>
</span><span class='line'>          order<span class="o">=</span><span class="s">&quot;relative&quot;</span><span class="p">,</span>
</span><span class='line'>          replications<span class="o">=</span><span class="m">1000</span>
</span><span class='line'>          <span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Translated Exponential Distribution]]></title>
    <link href="http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/07/04/translated-exponential-distribution/"/>
    <updated>2012-07-04T00:00:00-04:00</updated>
    <id>http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/07/04/translated-exponential-distribution</id>
    <content type="html"><![CDATA[<p>The first thing I did on this Day of Independence was run a
<a href="http://www.yellowjacketracing.com/brighton-5k/">5k in Brighton, NY</a>. I
was quite happy with the result. As someone who has only recently
learned how to run intelligently (regardless of the fact that I
<em>thought</em> I knew how to run), I&#8217;m beginning to see my average pace get
faster and faster. And, this is rewarding.</p>

<p>But who titles a post about running &#8220;Translated Exponential
Distribution&#8221;? Not even me. So, let&#8217;s move on.</p>

<p>The second thing I did today was fix some C++ code for random number
generation from a Truncated Normal Distribution. The naive
Accept-Reject algorithm is great in some cases and terrible in
others. None of the R packages that offered an implementation really
met my needs, so I implemented the sampler described in
<a href="http://www.springerlink.com/content/w28p864865273866/">Robert (1995)</a>
(gated).</p>

<p>Although there is an R interface for my own end-use, everything
important happens in C++ via Rcpp. For some parameter settings (see
the paper, of course), the algorithm calls for simulating from a
Translated Exponential distribution. Personally, I&#8217;d never come across
this distribution (which maybe should be a point of
embarrassment?). Google wasn&#8217;t terribly helpful, although I probably
could have derived the solution if I felt so inclined. But, that&#8217;s
more work than the problem really merits given the simplicity of the
relationship.</p>

<!-- more -->


<h2>Easy Simulation</h2>

<p>Let $x$ be an Exponential random variable such that $x$ has the
density
$$ f(x) = \exp(-x) * \mathbf{I}_{~x \geq 0}.$$
Then $y = \mu + \frac{x}{\lambda}$ has the density</p>

<!-- -->


<p>$$
g(y| \mu, \lambda) = \lambda * \exp(-\lambda(y - \mu)) * \mathbf{I}_{~x \geq \mu}
$$</p>

<!-- -->


<p>
and $y$ is said to be a Translated Exponential random
variable with scale parameter $\lambda$ and shift parameter $\mu$.</p>

<p>For simulation purposes, $y = \mu + \frac{x}{\lambda}$ is what
matters. First, simulate $x$ from an exponential distribution. Second,
transform $x$ to produce $y$. Quite simple as far as these things go.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[ggplot Snippet]]></title>
    <link href="http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/05/25/ggplot-snippet/"/>
    <updated>2012-05-25T00:00:00-04:00</updated>
    <id>http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/05/25/ggplot-snippet</id>
    <content type="html"><![CDATA[<p>I had no idea where on my computer to save the following R snippet to
use with ggplot. And, there is no chance I&#8217;d remember it because I&#8217;ve
only used it a handful of times (and rarely &#8220;combine&#8221; figures within
R). Hopefully, this will be a useful storage location.</p>

<p>Below, the objects &#8220;A&#8221; and &#8220;B&#8221; must otherwise exist as the output of a
<code>ggplot(data) + ...</code> expression.</p>

<figure class='code'><figcaption><span>ggplot Snippet</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='R'><span class='line'>grid.newpage<span class="p">()</span>
</span><span class='line'>pushViewport<span class="p">(</span>viewport<span class="p">(</span>layout <span class="o">=</span> grid.layout<span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">)))</span>
</span><span class='line'>vplayout <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>x<span class="p">,</span> y<span class="p">)</span>
</span><span class='line'>viewport<span class="p">(</span>layout.pos.row <span class="o">=</span> x<span class="p">,</span> layout.pos.col <span class="o">=</span> y<span class="p">)</span>
</span><span class='line'>print<span class="p">(</span>A<span class="p">,</span> vp <span class="o">=</span> vplayout<span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="m">1</span><span class="p">))</span>
</span><span class='line'>print<span class="p">(</span>B<span class="p">,</span> vp <span class="o">=</span> vplayout<span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[New Site Launch]]></title>
    <link href="http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/05/21/new-launch/"/>
    <updated>2012-05-21T00:00:00-04:00</updated>
    <id>http://www.rochester.edu/college/gradstudents/jolmsted/blog/2012/05/21/new-launch</id>
    <content type="html"><![CDATA[<p>Over the last few weeks, I&#8217;ve been porting my old static site which I
created with a pretty bare-bones templater (htp, to be specific) to a
new static site where I use
<a href="https://github.com/mojombo/jekyll#readme">jekyll</a>. In the future, I
might consider switching over to
<a href="http://jekyllbootstrap.com/lessons/jekyll-introduction.html">jekyll bootstrap</a>,
but the marginal improvement will likely be much smaller from that.</p>

<p>The reason I switched was that the old templater was so inelegant and
clunky that I ended up not updating content as often as I should
have. This switch should remove some of that friction.</p>
]]></content>
  </entry>
  
</feed>
