-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix navigation (and rerun eveyrthing with latest nimib)
- Loading branch information
1 parent
5fab60c
commit 1bf9d75
Showing
10 changed files
with
273 additions
and
193 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<!DOCTYPE html> | ||
<html lang="en-us"> | ||
<head> | ||
<title>bernoulli_and_beyond.nim</title> | ||
<title>drafts\bernoulli_and_beyond.nim</title> | ||
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>🐳</text></svg>"> | ||
<meta content="text/html; charset=utf-8" http-equiv="content-type"> | ||
<meta content="width=device-width, initial-scale=1" name="viewport"> | ||
|
@@ -30,13 +30,12 @@ | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous"> | ||
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script> | ||
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous" onload="renderMathInElement(document.body,{delimiters:[{left: '$$', right: '$$', display: true},{left: '$', right: '$', display: false}]});"></script> | ||
<script async defer data-domain="pietroppeter.github.io/nblog" src="https://plausible.io/js/plausible.js"></script> | ||
</head> | ||
<body> | ||
<header> | ||
<div class="nb-box"> | ||
<span><a href=".">🏡</a></span> | ||
<span><code>bernoulli_and_beyond.nim</code></span> | ||
<span><a href="..">🏡</a></span> | ||
<span><code>drafts\bernoulli_and_beyond.nim</code></span> | ||
<span><a href="https://github.com/pietroppeter/nblog"><svg aria-hidden="true" width="1.2em" height="1.2em" style="vertical-align: middle;" preserveAspectRatio="xMidYMid meet" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59c.4.07.55-.17.55-.38c0-.19-.01-.82-.01-1.49c-2.01.37-2.53-.49-2.69-.94c-.09-.23-.48-.94-.82-1.13c-.28-.15-.68-.52-.01-.53c.63-.01 1.08.58 1.23.82c.72 1.21 1.87.87 2.33.66c.07-.52.28-.87.51-1.07c-1.78-.2-3.64-.89-3.64-3.95c0-.87.31-1.59.82-2.15c-.08-.2-.36-1.02.08-2.12c0 0 .67-.21 2.2.82c.64-.18 1.32-.27 2-.27c.68 0 1.36.09 2 .27c1.53-1.04 2.2-.82 2.2-.82c.44 1.1.16 1.92.08 2.12c.51.56.82 1.27.82 2.15c0 3.07-1.87 3.75-3.65 3.95c.29.25.54.73.54 1.48c0 1.07-.01 1.93-.01 2.2c0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z" fill="#000"></path></svg></a></span> | ||
</div> | ||
<hr> | ||
|
@@ -82,7 +81,8 @@ <h3>Bernoulli distribution</h3> | |
<span class="hljs-keyword">let</span> s = take(rng, b, <span class="hljs-number">10</span>) | ||
<span class="hljs-keyword">echo</span> s</code></pre> | ||
<pre><samp>1.0 | ||
@[1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0]</samp></pre> | ||
@[1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0] | ||
</samp></pre> | ||
<p>We can also compute the mean ($p$), variance ($p(1-p)$) and standard deviation ($\sqrt(p (1 - p))$)</p> | ||
<pre><code class="nim hljs"><span class="hljs-keyword">echo</span> rng.mean b | ||
<span class="hljs-keyword">echo</span> rng.variance b | ||
|
@@ -93,19 +93,22 @@ <h3>Bernoulli distribution</h3> | |
0.08999999999999998 | ||
0.08999999999999998 | ||
0.3 | ||
0.3</samp></pre> | ||
0.3 | ||
</samp></pre> | ||
<h3>Choice distribution</h3> | ||
<p>A categorical distribution that is already implemented in alea is the one where the categories have equal probability:</p> | ||
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> dice = choice(@[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]) | ||
<span class="hljs-keyword">block</span>: | ||
<span class="hljs-keyword">let</span> s = take(rng, dice, <span class="hljs-number">10</span>) | ||
<span class="hljs-keyword">echo</span> s</code></pre> | ||
<pre><samp>@[4, 4, 6, 1, 6, 5, 5, 5, 1, 3]</samp></pre> | ||
<pre><samp>@[4, 4, 6, 1, 6, 5, 5, 5, 1, 3] | ||
</samp></pre> | ||
<p>Note that the <code>mean</code> will not work for <code>dice</code> since it is implemented only for <code>RandomVar[float]</code>.</p> | ||
<p>In order to have a mean I could do like this:</p> | ||
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> fdice = choice(@[<span class="hljs-number">1.0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]) | ||
<span class="hljs-keyword">echo</span> rng.mean fdice</code></pre> | ||
<pre><samp>3.5</samp></pre> | ||
<pre><samp>3.5 | ||
</samp></pre> | ||
<h3>Categorical distribution</h3> | ||
<p>Finally, to <a href="https://github.com/andreaferretti/alea#defining-custom-distributions">define a custom distribution</a> | ||
I just need to define:</p> | ||
|
@@ -137,7 +140,8 @@ <h3>Categorical distribution</h3> | |
<p>and we can already use this to create a biased dice, for example one that has double chance to output an odd number than an even number:</p> | ||
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> oddDice = categorical(@[(<span class="hljs-number">1.0</span>, <span class="hljs-number">0.2</span>), (<span class="hljs-number">2.0</span>, <span class="hljs-number">0.1</span>), (<span class="hljs-number">3.0</span>, <span class="hljs-number">0.2</span>), (<span class="hljs-number">4.0</span>, <span class="hljs-number">0.1</span>), | ||
(<span class="hljs-number">5.0</span>, <span class="hljs-number">0.2</span>), (<span class="hljs-number">6.0</span>, <span class="hljs-number">0.1</span>)])</code></pre> | ||
<pre><samp>probabilities do not add to 1.0, they will be normalized to sum to 1</samp></pre> | ||
<pre><samp>probabilities do not add to 1.0, they will be normalized to sum to 1 | ||
</samp></pre> | ||
<p>Now to close the loop I need to implement the <code>sample</code> proc:</p> | ||
<pre><code class="nim hljs"><span class="hljs-keyword">proc</span> sample*(rng: <span class="hljs-keyword">var</span> <span class="hljs-type">Random</span>; c: <span class="hljs-type">Categorical</span>): <span class="hljs-built_in">float</span> = | ||
<span class="hljs-keyword">var</span> tot = <span class="hljs-number">0.0</span> | ||
|
@@ -153,7 +157,8 @@ <h3>Categorical distribution</h3> | |
<span class="hljs-keyword">echo</span> s | ||
<span class="hljs-keyword">echo</span> rng.mean(oddDice)</code></pre> | ||
<pre><samp>@[1.0, 1.0, 1.0, 3.0, 5.0, 1.0, 1.0, 6.0, 5.0, 3.0, 5.0, 4.0, 3.0, 3.0, 3.0, 5.0, 4.0, 5.0, 1.0, 1.0] | ||
3.34143</samp></pre> | ||
3.34143 | ||
</samp></pre> | ||
<p>And the output seems reasonable (I could probably overload the mean and try to use it to compute an exact mean.</p> | ||
<h2>Conclusions</h2> | ||
<p>It was rather straightforward to implement a categorical distribution in alea. | ||
|
Oops, something went wrong.