Skip to content

Commit

Permalink
change figure sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
philpatton committed Feb 5, 2024
1 parent 4cba722 commit caaa3ae
Show file tree
Hide file tree
Showing 44 changed files with 1,189 additions and 1,439 deletions.
510 changes: 510 additions & 0 deletions .ipynb_checkpoints/occ-checkpoint.qmd

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions closed_cmr.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ plt.style.use('fivethirtyeight')
plt.rcParams['axes.facecolor'] = 'white'
plt.rcParams['figure.facecolor'] = 'white'
# pal = sns.color_palette('Set2')
# sns.set_palette(pal)ß
# sns.set_palette(pal)
# hyperparameters
SEED = 808
Expand Down Expand Up @@ -87,18 +87,18 @@ def get_history():
col_labs = [f'y{t}' for t in range(1, 5)]
# subset each dataset before augmenting
is_red = sal_data.spp == 0
red = sal_data.loc[is_red, col_labs].to_numpy()
is_pyg = sal_data.spp == 1
is_red = sal_data.spp == 0
pyg = sal_data.loc[is_pyg, col_labs].to_numpy()
red = sal_data.loc[is_red, col_labs].to_numpy()
# augment each set separately since they differ in length
red_augmented = augment_history(red)
pyg_augmented = augment_history(pyg)
red_augmented = augment_history(red)
# recombine into one history
history = np.concatenate((red_augmented, pyg_augmented))
history = np.concatenate((pyg_augmented, red_augmented))
return history
Expand All @@ -121,7 +121,7 @@ In this example, I combine the two species into one `pm.Model` object, making us
species_idx = np.repeat([0, 1], M)
# coordinates identifying parameter each species
coords = {'species': ['red_cheeked', 'pygmy']}
coords = {'species': ['pygmy', 'red_cheeked']}
with pm.Model(coords=coords) as M0:
Expand Down Expand Up @@ -150,7 +150,7 @@ with M0:
#| fig-cap: Traceplots for the salamander $M_0$ model. The red-cheeked salamander is in blue while the pygmy salamander is in red.
#| label: fig-trace
az.plot_trace(M0_idata);
az.plot_trace(M0_idata, figsize=(10,4));
```

For faster sampling, it's better to seperate the two species into two seperate models. On my machine, the individual species models finish sampling in 2-3 seconds, compared to 15-20 seconds for the two species model. That said, the two species model is somewhat more convenient.
Expand All @@ -174,7 +174,7 @@ Now we can look at the traceplots, this time excluding $\psi$ and looking at $N.
```{python}
#| fig-cap: 'Traceplots for the salamander $M_0$ model, including $N$. Estimates from @hooten2019 are shown by the vertical lines'
N_hooten = [229.6, 450.9]
az.plot_trace(M0_idata, var_names=['p', 'N'], combined=True,
az.plot_trace(M0_idata, var_names=['p', 'N'], combined=True, figsize=(10,4),
lines=[("N", {}, [N_hooten])]);
```

Expand All @@ -192,7 +192,7 @@ N_samps = np.vstack(post.N.to_numpy())
fig, ax = plt.subplots(1, 1, figsize=(4, 4))
# add the scatter for each species
labs = ['Red-backed','Pygmy']
labs = ['Pygmy', 'Red-backed']
ax.scatter(p_samps[:,0], N_samps[:,0], s=5, alpha=0.2, label=labs[0])
ax.scatter(p_samps[:,1], N_samps[:,1], s=5, alpha=0.2, label=labs[1])
Expand Down
3 changes: 3 additions & 0 deletions dist.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ The idea with distance sampling, also known as line-transect sampling, is that a
Following @hooten2019, Chapter 24 and @royle2008, Chapter 7, I use the impala data from @burnham1980, who credits P. Hemingway with the dataset. In this dataset, 73 impalas were observed along a 60km transect. The distance values below are the perpendicular distances, in meters, from the transect.

```{python}
#| fig-cap: Histogram of the number of detected impalas at varying distances.
#| label: fig-hist
import pymc as pm
import pytensor.tensor as pt
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -110,6 +112,7 @@ with distance:
az.plot_trace(
distance_idata,
figsize=(10,4),
var_names=['psi', 'sigma']
);
```
Expand Down
Empty file removed docs/.nojekyll
Empty file.
46 changes: 23 additions & 23 deletions docs/closed_cmr.html

Large diffs are not rendered by default.

Binary file modified docs/closed_cmr_files/figure-html/cell-10-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/closed_cmr_files/figure-html/cell-9-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/closed_cmr_files/figure-html/fig-trace-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/cow.jpg
Binary file not shown.
34 changes: 20 additions & 14 deletions docs/dist.html
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ <h1 class="title">Distance sampling</h1>
<p>In this notebook, I explore how to fit distance sampling models for estimating the size of a closed population. Similar to the occupancy and closed capture-recapture notebooks, I use parameter-expanded data-augmentation (PX-DA) and the zero-inflated binomial model in this notebook.</p>
<p>The idea with distance sampling, also known as line-transect sampling, is that a surveyer traverses a transect, typically in a boat or a plane. As they survey, they note when they detect an individual, or a group, from the species of interest, and further note the distance from the transect to the animal. Further, they note the angle to the animal(s), such that they can calculate the perpendicular distance from the animal to the transect. We assume that probability of detecting an animal <span class="math inline">\(p\)</span> decreases monotonically as the distance from the transect grows, e.g., <span class="math inline">\(p=\exp(-x^2/\sigma^2),\)</span> where <span class="math inline">\(x\)</span> is the distance and <span class="math inline">\(\sigma\)</span> is a scale parameter to be estimated. These simple assumptions permit the estimation of the population size <span class="math inline">\(N\)</span> as well as density <span class="math inline">\(D.\)</span></p>
<p>Following <span class="citation" data-cites="hooten2019">Hooten and Hefley (<a href="#ref-hooten2019" role="doc-biblioref">2019</a>)</span>, Chapter 24 and <span class="citation" data-cites="royle2008">Royle and Dorazio (<a href="#ref-royle2008" role="doc-biblioref">2008</a>)</span>, Chapter 7, I use the impala data from <span class="citation" data-cites="burnham1980">Burnham, Anderson, and Laake (<a href="#ref-burnham1980" role="doc-biblioref">1980</a>)</span>, who credits P. Hemingway with the dataset. In this dataset, 73 impalas were observed along a 60km transect. The distance values below are the perpendicular distances, in meters, from the transect.</p>
<div id="724f5c4d" class="cell" data-execution_count="1">
<div id="cell-fig-hist" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pymc <span class="im">as</span> pm</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pytensor.tensor <span class="im">as</span> pt</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
Expand Down Expand Up @@ -361,15 +361,20 @@ <h1 class="title">Distance sampling</h1>
<span id="cb1-42"><a href="#cb1-42" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-43"><a href="#cb1-43" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="dist_files/figure-html/cell-2-output-1.png" width="429" height="408" class="figure-img"></p>
<div id="fig-hist" class="quarto-figure quarto-figure-center quarto-float anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-hist-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="dist_files/figure-html/fig-hist-output-1.png" width="429" height="408" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-hist-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Histogram of the number of detected impalas at varying distances.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Again, we treat this as a zero-inflated binomial model using PX-DA. The trick for doing so is to create a binary vector of length <span class="math inline">\(M\)</span>, <span class="math inline">\(y,\)</span> that represents whether the individual was detected during the study. Then, combine the indicator with the distance vector <span class="math inline">\(x\)</span> to create a the full dataset <span class="math inline">\((x,y).\)</span></p>
<div id="659cd6be" class="cell" data-execution_count="2">
<div id="14da023c" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>n <span class="op">=</span> <span class="bu">len</span>(x_observed)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>unobserved_count <span class="op">=</span> M <span class="op">-</span> n</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>zeros <span class="op">=</span> np.zeros(unobserved_count)</span>
Expand Down Expand Up @@ -399,21 +404,21 @@ <h1 class="title">Distance sampling</h1>
<img src="dist_files/figure-html/fig-dist-output-1.svg" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-dist-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Visual representation of the distance sampling model.
Figure&nbsp;2: Visual representation of the distance sampling model.
</figcaption>
</figure>
</div>
</div>
</div>
<div id="f55db106" class="cell" data-execution_count="4">
<div id="6d248158" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> distance:</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> distance_idata <span class="op">=</span> pm.sample()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [psi, sigma, x_unobserved]
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 12 seconds.</code></pre>
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 11 seconds.</code></pre>
</div>
<div class="cell-output cell-output-display">

Expand Down Expand Up @@ -445,16 +450,17 @@ <h1 class="title">Distance sampling</h1>
<div id="cell-fig-trace" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>az.plot_trace(</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> distance_idata, </span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> var_names<span class="op">=</span>[<span class="st">'psi'</span>, <span class="st">'sigma'</span>]</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> figsize<span class="op">=</span>(<span class="dv">10</span>,<span class="dv">4</span>),</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> var_names<span class="op">=</span>[<span class="st">'psi'</span>, <span class="st">'sigma'</span>]</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display">
<div id="fig-trace" class="quarto-figure quarto-figure-center quarto-float anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-trace-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="dist_files/figure-html/fig-trace-output-1.png" width="1021" height="367" class="figure-img">
<img src="dist_files/figure-html/fig-trace-output-1.png" width="854" height="367" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-trace-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Traceplots for the distance sampling model.
Figure&nbsp;3: Traceplots for the distance sampling model.
</figcaption>
</figure>
</div>
Expand Down Expand Up @@ -509,7 +515,7 @@ <h1 class="title">Distance sampling</h1>
<img src="dist_files/figure-html/fig-posteriors-output-1.png" width="766" height="382" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-posteriors-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Posterior distributions for <span class="math inline">\(N\)</span> and <span class="math inline">\(\sigma.\)</span> Estimates from <span class="citation" data-cites="royle2008">Royle and Dorazio (<a href="#ref-royle2008" role="doc-biblioref">2008</a>)</span> are shown with vertical lines.
Figure&nbsp;4: Posterior distributions for <span class="math inline">\(N\)</span> and <span class="math inline">\(\sigma.\)</span> Estimates from <span class="citation" data-cites="royle2008">Royle and Dorazio (<a href="#ref-royle2008" role="doc-biblioref">2008</a>)</span> are shown with vertical lines.
</figcaption>
</figure>
</div>
Expand Down Expand Up @@ -546,7 +552,7 @@ <h1 class="title">Distance sampling</h1>
<img src="dist_files/figure-html/fig-func-output-1.png" width="498" height="408" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-func-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Posterior distribution for the detection function. The line represents the posterior mean while the shaded area is the 96% interval.
Figure&nbsp;5: Posterior distribution for the detection function. The line represents the posterior mean while the shaded area is the 96% interval.
</figcaption>
</figure>
</div>
Expand Down
Binary file modified docs/dist_files/figure-html/fig-func-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/dist_files/figure-html/fig-posteriors-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/dist_files/figure-html/fig-trace-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit caaa3ae

Please sign in to comment.