Skip to content

Commit

Permalink
Typeset.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDiBernardo committed Aug 3, 2015
1 parent fb4c3ae commit 2f5e338
Show file tree
Hide file tree
Showing 10 changed files with 1,274 additions and 57 deletions.
36 changes: 19 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@ tex/500L.blg
tex/500L.log
tex/500L.out
tex/500L.toc
tex/frontmatter.tex
tex/frontmatter-images
tex/ci-images
tex/ci.markdown
tex/cluster-images
tex/cluster.markdown
tex/colophon.tex
tex/intro.tex
tex/template-engine.markdown
tex/crawler.markdown
tex/crawler-images
tex/sample.markdown
tex/spreadsheet.markdown
tex/spreadsheet-images
tex/sample-images
tex/modeller.markdown
tex/crawler.markdown
tex/data-store-images
tex/data-store.markdown
tex/frontmatter-images
tex/frontmatter.tex
tex/intro.tex
tex/modeller-images
tex/ci.markdown
tex/ci-images
tex/objmodel.markdown
tex/modeller.markdown
tex/objmodel-images
tex/data-store.markdown
tex/data-store-images
tex/cluster.markdown
tex/cluster-images
tex/objmodel.markdown
tex/sample-images
tex/sample.markdown
tex/sampler-images
tex/sampler.markdown
tex/spreadsheet-images
tex/spreadsheet.markdown
tex/template-engine.markdown
html/content
html/cache
*.pid
2 changes: 2 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def main(chapters=[], epub=False, pdf=False, html=False, mobi=False, pandoc_epub
run('rm {}'.format(f))

chapter_dirs = [
'sampler',
'spreadsheet',
'cluster',
'data-store',
Expand Down Expand Up @@ -58,6 +59,7 @@ def main(chapters=[], epub=False, pdf=False, html=False, mobi=False, pandoc_epub
]

image_paths = [
'./sampler/sampler-images',
'./spreadsheet/spreadsheet-images',
'./cluster/cluster-images',
'./data-store/data-store-images',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
82 changes: 42 additions & 40 deletions sampler/README.md → sampler/sampler.markdown
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<!-- to convert to HTML, run `pandoc --to html --mathjax -s README.md > README.html` -->
<!-- NB: US spelling used -->

# Sampling Methods

Note: This chapter assumes some familiarity with statistics and
probability theory.
title: A Rejection Sampler
author: Jessica B. Hamrick

## Introduction

Expand Down Expand Up @@ -32,7 +27,11 @@ Even though these types of problems cannot be solved exactly, we can
often achieve an approximate solution to them using techniques known
as *Monte Carlo sampling* methods. In Monte Carlo methods, the key
idea is to take many *samples*, which will then allow you to estimate
the solution.
the solution.[^note]

[^note]: This chapter assumes some familiarity with statistics and
probability theory.


### What is Sampling?

Expand Down Expand Up @@ -153,8 +152,7 @@ multinomial distribution is then used to describe the possible
combinations of outcomes when multiple balls are drawn (e.g., two
green and one blue).

Note: the code in this section is also located in the file
`multinomial.py`.
The code in this section is located in the file `multinomial.py`.

### The `MultinomialDistribution` Class

Expand Down Expand Up @@ -190,12 +188,12 @@ distributions. There are several advantages to doing so:
these constants in the constructor, rather than having to compute
them every time the PMF or PDF function is called.

> *In practice, this is how many statistics packages work, including
> SciPy's own distributions, which are located in the `scipy.stats`
> module. While we are using other SciPy functions, however, we are
> not using their probability distributions, both for the sake of
> illustration, and because there is currently no multinomial
> distribution in SciPy.*
In practice, this is how many statistics packages work, including
SciPy's own distributions, which are located in the `scipy.stats`
module. While we are using other SciPy functions, however, we are
not using their probability distributions, both for the sake of
illustration, and because there is currently no multinomial
distribution in SciPy.

Here is the constructor code for the class:

Expand Down Expand Up @@ -305,11 +303,11 @@ than `numpy` is significantly clearer:

Taking a sample from a multinomial distribution is actually fairly
straightforward, because NumPy provides us with a function that
does it: `np.random.multinomial`.
does it: `np.random.multinomial`[^multinomial].

> *NumPy includes functions to draw samples from many different types
> of distributions. For a full list, take a look at the
> random sampling module, `np.random`.*
[^multinomial]: NumPy includes functions to draw samples from many different types
of distributions. For a full list, take a look at the
random sampling module, `np.random`.

Despite the fact that this function already exists, there are a few
design decisions surrounding it that we can make.
Expand Down Expand Up @@ -362,18 +360,18 @@ just use the `np.random` module, I would not be able to do).

So, if the `rso` variable is not given, then the constructor defaults
to using `np.random.multinomial`. Otherwise, it uses the multinomial
sampler from the `RandomState` object itself.

> *Aside: the functions in `np.random` actually do rely on a random
> number generator that we can control: NumPy's global random number
> generator. You can set the global seed with `np.seed`. There's a
> tradeoff to using the global generator vs. a local `RandomState`
> object. If you use the global generator, then you don't have to pass
> around a `RandomState` object everywhere. However, you also run the
> risk of depending on some third party code that also uses the global
> generator without your knowledge. If you use a local object, then it
> is easier to find out whether there is nondeterminism coming from
> somewhere other than your own code.*
sampler from the `RandomState` object itself[^rng].

[^rng]: The functions in `np.random` actually do rely on a random
number generator that we can control: NumPy's global random number
generator. You can set the global seed with `np.seed`. There's a
tradeoff to using the global generator vs. a local `RandomState`
object. If you use the global generator, then you don't have to pass
around a `RandomState` object everywhere. However, you also run the
risk of depending on some third party code that also uses the global
generator without your knowledge. If you use a local object, then it
is easier to find out whether there is nondeterminism coming from
somewhere other than your own code.

#### What's a Parameter?

Expand Down Expand Up @@ -951,21 +949,25 @@ We can now create our distrbution as follows:

Once created, we can use it to generate a few different items:

```python
```
>>> item_dist.sample()
{'dexterity': 0, 'strength': 0, 'constitution': 0, 'intelligence': 0, 'wisdom': 0, 'charisma': 1}
{'dexterity': 0, 'strength': 0, 'constitution': 0,
'intelligence': 0, 'wisdom': 0, 'charisma': 1}
>>> item_dist.sample()
{'dexterity': 0, 'strength': 0, 'constitution': 1, 'intelligence': 0, 'wisdom': 2, 'charisma': 0}
{'dexterity': 0, 'strength': 0, 'constitution': 1,
'intelligence': 0, 'wisdom': 2, 'charisma': 0}
>>> item_dist.sample()
{'dexterity': 1, 'strength': 0, 'constitution': 1, 'intelligence': 0, 'wisdom': 0, 'charisma': 0}
{'dexterity': 1, 'strength': 0, 'constitution': 1,
'intelligence': 0, 'wisdom': 0, 'charisma': 0}
```

And, if we want, we can evaluate the probability of a sampled item:

```python
```
>>> item = item_dist.sample()
>>> item
{'dexterity': 0, 'strength': 0, 'constitution': 0, 'intelligence': 0, 'wisdom': 2, 'charisma': 0}
{'dexterity': 0, 'strength': 0, 'constitution': 0,
'intelligence': 0, 'wisdom': 2, 'charisma': 0}
>>> item_dist.log_pmf(item)
-4.9698132995760007
>>> item_dist.pmf(item)
Expand Down Expand Up @@ -1134,9 +1136,9 @@ Now we can draw a bunch of samples, and compute the 50th percentile
```

If we were to plot a histogram of how many samples we got for each
amount of damage, it would look something like this:
amount of damage, it would look something like \aosafigref{500l.sampler.damage}.

![](damage_distribution.png)
\aosafigure[144pt]{sampler-images/damage_distribution.png}{Damage Distribution}{500l.sampler.damage}

There is a pretty wide range of damage that the player could
potentially inflict, but it has a long tail: the 50th percentile is at
Expand Down
2 changes: 2 additions & 0 deletions tex/500L.tex
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@
\mainmatter


\include{sampler}

\include{spreadsheet}

\include{cluster}
Expand Down
Loading

0 comments on commit 2f5e338

Please sign in to comment.