Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Documentation Structure #327

Merged
merged 26 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
df0dd1d
Created new pattern for documentation. Ported first using turing doc …
cgbotta Oct 18, 2022
4ba06f0
Updated style
cgbotta Oct 18, 2022
a356863
Merge branch 'TuringLang:master' into master
cgbotta Oct 19, 2022
fc6bf5b
Added default weave that handles both directories and addressed gramm…
cgbotta Oct 19, 2022
f760b32
Renamed tutorials and added a doc example
cgbotta Oct 20, 2022
0cf7b6a
fixed format
cgbotta Oct 20, 2022
2c17932
Reverted tutorial names back to originals
cgbotta Oct 20, 2022
b6fe397
Generated manifest and project files, updated GH Action
cgbotta Oct 20, 2022
59313b6
Generated manifest with proper Julia version
cgbotta Oct 20, 2022
1b1c241
Fixed project files
cgbotta Oct 20, 2022
2590aa4
Updated Tutorial
cgbotta Oct 21, 2022
7116f29
style
cgbotta Oct 21, 2022
eb5476e
fix style
cgbotta Oct 21, 2022
d4b5ca9
style
cgbotta Oct 21, 2022
9bf0a08
Added calculation of true posterior mean
cgbotta Oct 26, 2022
f39de6c
format
cgbotta Oct 26, 2022
637829c
permalink
cgbotta Oct 26, 2022
8fc20b5
Merge branch 'master' into master
yebai Oct 26, 2022
d489a10
Added variance calculation
cgbotta Oct 27, 2022
b6a2cd9
formatting
cgbotta Oct 27, 2022
e9c73cc
formatting
cgbotta Oct 27, 2022
9dce638
Updated tutorial posterior visualizations
cgbotta Nov 5, 2022
6f9c246
Format
cgbotta Nov 5, 2022
8124cac
Calculated density of N-IG properly
cgbotta Nov 19, 2022
38c83a4
Format
cgbotta Nov 19, 2022
cfe15eb
Update tutorials/docs-00-getting-started/00_getting-started.jmd
yebai Nov 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
# push!(subdirs, d)
# end
# end
subdirs = ["", "tutorials/00-introduction", "tutorials/01-gaussian-mixture-model", "tutorials/04-hidden-markov-model", "tutorials/08-multinomial-logistic-regression", "tutorials/10-bayesian-differential-equations", "tutorials/11-probabilistic-pca", "tutorials/12-gaussian-process", "tutorials/13-seasonal-time-series", "tutorials/14-minituring"]
subdirs = ["", "tutorials/00-introduction", "tutorials/01-gaussian-mixture-model", "tutorials/04-hidden-markov-model", "tutorials/08-multinomial-logistic-regression", "tutorials/10-bayesian-differential-equations", "tutorials/11-probabilistic-pca", "tutorials/12-gaussian-process", "tutorials/13-seasonal-time-series", "tutorials/14-minituring", "tutorials/docs-00-getting-started"]
CompatHelper.main(; subdirs)
shell: julia --color=yes {0}
env:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Testing/
/*/*/jl_*/
/Manifest.toml
/test/Manifest.toml
.vscode
11 changes: 6 additions & 5 deletions src/TuringTutorials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using Pkg: Pkg
const REPO_DIR = dirname(@__DIR__)
const CSS_FILE = joinpath(REPO_DIR, "templates", "skeleton_css.css")
const LATEX_FILE = joinpath(REPO_DIR, "templates", "julia_tex.tpl")
const WEAVE_DIRECTORY = "tutorials"

const DEFAULT_BUILD = (:script, :html, :github)

Expand All @@ -33,15 +34,15 @@ function weave(
)
end

target = joinpath(REPO_DIR, "tutorials", folder, file)
target = joinpath(REPO_DIR, WEAVE_DIRECTORY, folder, file)
@info("Weaving $(target)")

# Activate project
# TODO: use separate Julia process?
if isfile(joinpath(REPO_DIR, "tutorials", folder, "Project.toml")) &&
if isfile(joinpath(REPO_DIR, WEAVE_DIRECTORY, folder, "Project.toml")) &&
(:github in build || :html in build || :pdf in build)
@info("Instantiating", folder)
Pkg.activate(joinpath(REPO_DIR, "tutorials", folder))
Pkg.activate(joinpath(REPO_DIR, WEAVE_DIRECTORY, folder))
Pkg.instantiate()
Pkg.build()

Expand Down Expand Up @@ -92,14 +93,14 @@ end

# Weave all tutorials
cgbotta marked this conversation as resolved.
Show resolved Hide resolved
function weave(; kwargs...)
for folder in readdir(joinpath(REPO_DIR, "tutorials"))
for folder in readdir(joinpath(REPO_DIR, WEAVE_DIRECTORY))
weave(folder; kwargs...)
end
end

# Weave a folder of tutorials
function weave(folder::AbstractString; kwargs...)
for file in readdir(joinpath(REPO_DIR, "tutorials", folder))
for file in readdir(joinpath(REPO_DIR, WEAVE_DIRECTORY, folder))
# Skip non-`.jmd` files
endswith(file, ".jmd") || continue

Expand Down
110 changes: 110 additions & 0 deletions tutorials/docs-00-getting-started/00_getting-started.jmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: Getting Started
permalink: /docs/using-turing/get-started
cgbotta marked this conversation as resolved.
Show resolved Hide resolved
redirect_from: docs/0-gettingstarted/
weave_options:
error : false
---

# Getting Started

## Installation

To use Turing, you need to install Julia first and then install Turing.

### Install Julia

You will need to install Julia 1.3 or greater, which you can get from [the official Julia website](http://julialang.org/downloads/).

### Install Turing.jl

Turing is an officially registered Julia package, so you can install a stable version of Turing by running the following in the Julia REPL:

```julia
using Pkg
Pkg.add("Turing")
```

You can check if all tests pass by running `Pkg.test("Turing")` (it might take a long time)

## Example

Here's a simple example showing Turing in action.

First, we can load the Turing and StatsPlots modules

```julia
using Turing
using StatsPlots
```

Then, we define a simple Normal model with unknown mean and variance

```julia
@model function gdemo(x, y)
s² ~ InverseGamma(2, 3)
m ~ Normal(0, sqrt(s²))
x ~ Normal(m, sqrt(s²))
return y ~ Normal(m, sqrt(s²))
end
```

Then we can run a sampler to collect results. In this case, it is a Hamiltonian Monte Carlo sampler

```julia
chn = sample(gdemo(1.5, 2), HMC(0.1, 5), 1000)
```

We can plot the results

```julia
plot(chn)
```

In this case, because we use the [normal-inverse gamma distribution](https://en.wikipedia.org/wiki/Normal-inverse-gamma_distribution)
as a [conjugate prior](https://en.wikipedia.org/wiki/Conjugate_prior), we can compute
its updated mean as follows:

```julia
s² = InverseGamma(2, 3)
m = Normal(0, 1)
data = [1.5, 2]
x_bar = mean(data)
N = length(data)

mean_exp = (m.σ * m.μ + N * x_bar) / (m.σ + N)
```

We can also compute the updated variance

```julia
updated_alpha = shape(s²) + (N / 2)
updated_beta =
scale(s²) +
(1 / 2) * sum((data[n] - x_bar)^2 for n in 1:N) +
(N * m.σ) / (N + m.σ) * ((x_bar)^2) / 2
variance_exp = updated_beta / (updated_alpha - 1)
```

Finally, we can check if these expectations align with our HMC approximations
from earlier. We can compute samples from a normal-inverse gamma following the
equations given [here](https://en.wikipedia.org/wiki/Normal-inverse-gamma_distribution#Generating_normal-inverse-gamma_random_variates).

```julia
function sample_posterior(alpha, beta, mean, lambda, iterations)
samples = []
for i in 1:iterations
sample_variance = rand(InverseGamma(alpha, beta), 1)
sample_x = rand(Normal(mean, sqrt(sample_variance[1]) / lambda), 1)
sanples = append!(samples, sample_x)
end
return samples
end

samples = sample_posterior(updated_alpha, updated_beta, mean_exp, 2, 1000);
yebai marked this conversation as resolved.
Show resolved Hide resolved
```

```julia
density(analytical_samples; label="Posterior (Analytical)")
density!(chn[:m]; label="Posterior (HMC)")
```
Loading