<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/simulation-under-zero-measure-constraints/"></div></div><div><a data-url="https://www.r-bloggers.com/simulation-under-zero-measure-constraints/" data-counturl="https://www.r-bloggers.com/simulation-under-zero-measure-constraints/" data-count="horizontal" data-text="simulation under zero measure constraints" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://xianblog.wordpress.com/2016/11/17/simulation-under-zero-measure-constraints/"> R – Xi'an's Og</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>A</strong> theme that comes up fairly regularly on X validated is the production of a <a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/q/243260/7224">sample with given moments</a>, either for calibration motives or from a misunderstanding of the difference between a distribution mean and a sample average. Here are some entries on that topic:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/questions/244776/how-to-sample-from-a-distribution-so-that-mean-of-samples-equals-expected-value">How to sample from a distribution so that mean of samples equals expected value?</a></li>
<li><a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/questions/243260/sample-random-variables-conditional-on-their-sum">Sample random variables conditional on their sum</a></li>
<li><a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/questions/113205/simulation-involving-conditioning-on-sum-of-random-variables">Simulation involving conditioning on sum of random variables</a></li>
<li><a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/questions/79842/conditional-expectation-of-squared-standard-normal">conditional expectation of squared standard normal</a></li>
</ul>
<p>In most of those questions, the constraint in <a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/q/243260/7224">on the sum</a> or mean of the sample, which allows for an easy resolution by <a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/a/243516/7224">a change of variables</a>. It however gets somewhat harder when the constraint involves more moments or, worse, an implicit solution to an equation. A good example of the later is the quest for a <a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/q/243537/7224">sample with a given maximum likelihood estimate</a> in the case this MLE cannot be derived analytically. As for instance with a location-scale t sample…</p>
<p>Actually, even when the constraint is solely <a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/q/243260/7224">on the sum</a>, a relevant question is the production of an efficient simulation mechanism. Using a Gibbs sampler that changes one component of the sample at each iteration does not qualify, even though it eventually produces <a target="_blank" rel="nofollow" href="http://stats.stackexchange.com/a/244798/7224">the proper sample</a>. Except for small samples. As in this example</p>
<pre><code><span>n</span><span>=</span><span>3</span><span>;</span><span>T</span><span>=</span><span>1e4</span><span>
s0</span><span>=.</span><span>5</span> <span>#fixed average</span><span>
sampl</span><span>=</span><span>matrix</span><span>(</span><span>s0</span><span>,</span><span>T</span><span>,</span><span>n</span><span>)</span>
<span>for</span> <span>(</span><span>t </span><span>in</span> <span>2</span><span>:</span><span>T</span><span>){</span><span>
sampl</span><span>[</span><span>t</span><span>,]=</span><span>sampl</span><span>[</span><span>t</span><span>-</span><span>1</span><span>,]</span>
 <span>for</span> <span>(</span><span>i </span><span>in</span> <span>1</span><span>:(</span><span>n</span><span>-</span><span>1</span><span>)){</span><span>
sampl</span><span>[</span><span>t</span><span>,</span><span>i</span><span>]=</span><span>runif</span><span>(</span><span>1</span><span>,</span><span>
min</span><span>=</span><span>max</span><span>(</span><span>0</span><span>,</span><span>n</span><span>*</span><span>s0</span><span>-</span><span>sum</span><span>(</span><span>sampl</span><span>[</span><span>t</span><span>,</span><span>c</span><span>(-</span><span>i</span><span>,-</span><span>n</span><span>)])-</span><span>1</span><span>),</span><span>
max</span><span>=</span><span>min</span><span>(</span><span>1</span><span>,</span><span>n</span><span>*</span><span>s0</span><span>-</span><span>sum</span><span>(</span><span>sampl</span><span>[</span><span>t</span><span>,</span><span>c</span><span>(-</span><span>i</span><span>,-</span><span>n</span><span>)])))</span><span>
sampl</span><span>[</span><span>t</span><span>,</span><span>n</span><span>]=</span><span>n</span><span>*</span><span>s0</span><span>-</span><span>sum</span><span>(</span><span>sampl</span><span>[</span><span>t</span><span>,-</span><span>n</span><span>])}}</span></code></pre>
<p>For very large samples, I figure that proposing from the unconstrained density can achieve a sufficient efficiency, but the in-between setting remains an interesting problem.</p>
<p>Filed under: <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/category/books/">Books</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/category/kids/">Kids</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/category/statistics/r-statistics/">R</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/category/statistics/">Statistics</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/category/university-life/">University life</a> Tagged: <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/tag/cross-validated/">cross validated</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/tag/gibbs-sampler/">Gibbs sampler</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/tag/maximum-likelihood-estimation/">maximum likelihood estimation</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/tag/monte-carlo-algorithm/">Monte Carlo algorithm</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/tag/monte-carlo-statistical-methods/">Monte Carlo Statistical Methods</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/tag/simulation/">simulation</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/tag/stack-exchange/">Stack Exchange</a>, <a target="_blank" rel="nofollow" href="https://xianblog.wordpress.com/tag/statistics-forum/">Statistics Forum</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/xianblog.wordpress.com/31954/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/xianblog.wordpress.com/31954/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/simulation-under-zero-measure-constraints/"></div></div><div><a data-url="https://www.r-bloggers.com/simulation-under-zero-measure-constraints/" data-counturl="https://www.r-bloggers.com/simulation-under-zero-measure-constraints/" data-count="vertical" data-text="simulation under zero measure constraints" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://xianblog.wordpress.com/2016/11/17/simulation-under-zero-measure-constraints/"> R – Xi'an's Og</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/fgawSV7I4Vk"></a></div>