<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Alexandru Amărioarei</title>
<link>https://alexamarioarei.github.io/blog/</link>
<atom:link href="https://alexamarioarei.github.io/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Notes, tutorials and research updates: probability, statistics, R and teaching.</description>
<generator>quarto-1.10.19</generator>
<lastBuildDate>Thu, 19 Nov 2026 22:00:00 GMT</lastBuildDate>
<item>
  <title>Example: estimating the distribution of a scan statistic by simulation</title>
  <dc:creator>Alexandru Amărioarei</dc:creator>
  <link>https://alexamarioarei.github.io/blog/posts/2026-11-20-exemplu/</link>
  <description><![CDATA[ 





<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>This is a sample post
</div>
</div>
<div class="callout-body-container callout-body">
<p>It shows what a post on this blog looks like: text, LaTeX formulas, R code, a table and a numbered statement. To write a new post, copy this folder, change the date and title, and rewrite the text. The post can be deleted at any time (the folder <code>blog/posts/2026-11-20-exemplu/</code> and its Romanian counterpart in <code>ro/blog/posts/</code>).</p>
</div>
</div>
<section id="the-problem" class="level2">
<h2 class="anchored" data-anchor-id="the-problem">The problem</h2>
<p>Let <img src="https://latex.codecogs.com/png.latex?X_1,%20X_2,%20%5Cdots,%20X_N"> be independent Bernoulli random variables with parameter <img src="https://latex.codecogs.com/png.latex?p">. For a window of length <img src="https://latex.codecogs.com/png.latex?m">, the one-dimensional discrete scan statistic is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AS_m(N)%20=%20%5Cmax_%7B1%20%5Cle%20i%20%5Cle%20N-m+1%7D%20%5C;%20%5Csum_%7Bk=i%7D%5E%7Bi+m-1%7D%20X_k%20.%0A"></p>
<p>We are interested in <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BP%7D%5Cbig(S_m(N)%20%5Cge%20k%5Cbig)">, the probability that somewhere in the sequence a window of length <img src="https://latex.codecogs.com/png.latex?m"> contains at least <img src="https://latex.codecogs.com/png.latex?k"> successes.</p>
<div id="def-scan" class="theorem definition">
<p><span class="theorem-title"><strong>Definition 1 (Scan statistic)</strong></span> For a sequence <img src="https://latex.codecogs.com/png.latex?(X_k)"> and a length <img src="https://latex.codecogs.com/png.latex?m">, the <em>scan statistic</em> is the largest number of successes contained in a window of <img src="https://latex.codecogs.com/png.latex?m"> consecutive observations.</p>
</div>
</section>
<section id="monte-carlo-estimation-in-r" class="level2">
<h2 class="anchored" data-anchor-id="monte-carlo-estimation-in-r">Monte Carlo estimation in R</h2>
<p>The simplest estimator is the relative frequency over a large number of independent simulations:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cwidehat%7BP%7D_n%20=%20%5Cfrac%7B1%7D%7Bn%7D%20%5Csum_%7Br=1%7D%5E%7Bn%7D%20%5Cmathbf%7B1%7D%5C%7BS%5E%7B(r)%7D_m(N)%20%5Cge%20k%5C%7D,%0A%5Cqquad%0A%5Coperatorname%7Bse%7D%5Cbig(%5Cwidehat%7BP%7D_n%5Cbig)%20%5Capprox%20%5Csqrt%7B%5Cfrac%7B%5Cwidehat%7BP%7D_n%20(1-%5Cwidehat%7BP%7D_n)%7D%7Bn%7D%7D%20.%0A"></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">scan_1d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, m) {</span>
<span id="cb1-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># window sums of length m, computed from cumulative sums</span></span>
<span id="cb1-3">  s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, x))</span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(s[(m <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(s)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> s[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(s) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> m)])</span>
<span id="cb1-5">}</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>)</span>
<span id="cb1-8">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>; m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>; p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>; k <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb1-9">S <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scan_1d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(N, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, p), m))</span>
<span id="cb1-10"></span>
<span id="cb1-11">p_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(S <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> k)</span>
<span id="cb1-12">se    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(p_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p_hat) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(S))</span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> p_hat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">std_error =</span> se)</span></code></pre></div></div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Executable R code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>The block above is only displayed. To have R run the code when the site is rendered and include its output (figures too), write the block as <code>```{r}</code>. The results are stored in <code>_freeze/</code>, so the site can be rebuilt without R.</p>
</div>
</div>
</div>
</section>
<section id="what-else-a-post-can-contain" class="level2">
<h2 class="anchored" data-anchor-id="what-else-a-post-can-contain">What else a post can contain</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Element</th>
<th>Quarto syntax</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Formula</td>
<td><code>$...$</code> and <code>$$...$$</code></td>
</tr>
<tr class="even">
<td>Theorem</td>
<td><code>::: {#thm-name} ... :::</code></td>
</tr>
<tr class="odd">
<td>Figure</td>
<td><code>![Caption](figure.png)</code></td>
</tr>
<tr class="even">
<td>R / Python code</td>
<td><code>```{r}</code> / <code>```{python}</code></td>
</tr>
<tr class="odd">
<td>Footnote</td>
<td><code>text[^1]</code></td>
</tr>
</tbody>
</table>
<p>Cross-references work automatically: for example, Definition&nbsp;1 above.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>example</category>
  <category>scan statistics</category>
  <category>R</category>
  <guid>https://alexamarioarei.github.io/blog/posts/2026-11-20-exemplu/</guid>
  <pubDate>Thu, 19 Nov 2026 22:00:00 GMT</pubDate>
</item>
</channel>
</rss>
