-
Notifications
You must be signed in to change notification settings - Fork 104
Add FAQ section to improve user guidance #608
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
978017b
Add FAQ section to improve user guidance
d9a46e3
Update faq/index.qmd
AoifeHughes 41ab37b
Update faq/index.qmd
AoifeHughes 2b97c35
Update faq/index.qmd
AoifeHughes 6ec9c7e
Enhance FAQ section with detailed explanations on conditioning in Tur…
571c44a
formatter wanted to fix this
6c6e32f
Merge branch 'main' into faq
penelopeysm d231882
Update faq/index.qmd
AoifeHughes 886429e
Update faq/index.qmd
AoifeHughes 8734528
Update faq/index.qmd
AoifeHughes 3a15298
Update faq/index.qmd
AoifeHughes 85ecfbc
Update faq/index.qmd
AoifeHughes 2726c1a
tweaked to Penny's suggestion
8cdc3c7
Merge branch 'faq' of https://github.com/TuringLang/docs into faq
e864d7d
updated the manifest thingy
1c72064
Update Manifest.toml
mhauru 6afff59
Merge branch 'main' into faq
AoifeHughes 27a7f0f
Update faq/index.qmd
AoifeHughes 883e269
mfest
700b77a
Update FAQ to include BUGS in syntax comparison with Turing and Stan
6b562aa
Merge branch 'main' into faq
AoifeHughes e895276
Merge branch 'main' into faq
penelopeysm 13d4001
Update faq/index.qmd
penelopeysm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
--- | ||
title: "Frequently Asked Questions" | ||
description: "Common questions and answers about using Turing.jl" | ||
--- | ||
|
||
## Why is this variable being treated as random instead of observed? | ||
|
||
This is a common source of confusion. In Turing.jl, you can only condition or fix expressions that explicitly appear on the left-hand side (LHS) of a `~` statement. | ||
|
||
For example, if your model contains: | ||
```julia | ||
x ~ filldist(Normal(), 2) | ||
``` | ||
|
||
You cannot directly condition on `x[2]` using `condition(model, @varname(x[2]) => 1.0)` because `x[2]` never appears on the LHS of a `~` statement. Only `x` as a whole appears there. | ||
|
||
However, there is an important exception: when you use the broadcasting operator `.~` with a univariate distribution, each element is treated as being separately drawn from that distribution, allowing you to condition on individual elements: | ||
|
||
```julia | ||
@model function f1() | ||
x = Vector{Float64}(undef, 3) | ||
x .~ Normal() # Each element is a separate draw | ||
end | ||
|
||
m1 = f1() | (@varname(x[1]) => 1.0) | ||
sample(m1, NUTS(), 100) # This works! | ||
``` | ||
|
||
In contrast, you cannot condition on parts of a multivariate distribution because it represents a single distribution over the entire vector: | ||
|
||
```julia | ||
@model function f2() | ||
x = Vector{Float64}(undef, 3) | ||
x ~ MvNormal(zeros(3), I) # Single multivariate distribution | ||
end | ||
|
||
m2 = f2() | (@varname(x[1]) => 1.0) | ||
sample(m2, NUTS(), 100) # This doesn't work! | ||
``` | ||
|
||
The key insight is that `filldist` creates a single distribution (not N independent distributions), which is why you cannot condition on individual elements. The distinction is not just about what appears on the LHS of `~`, but whether you're dealing with separate distributions (`.~` with univariate) or a single distribution over multiple values (`~` with multivariate or `filldist`). | ||
|
||
To understand more about how Turing determines whether a variable is treated as random or observed, see: | ||
- [Core Functionality](../core-functionality/) - basic explanation of the `~` notation and conditioning | ||
AoifeHughes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
|
||
## Can I use parallelism / threads in my model? | ||
|
||
Yes, but with important caveats! There are two types of parallelism to consider: | ||
|
||
### 1. Parallel Sampling (Multiple Chains) | ||
Turing.jl fully supports sampling multiple chains in parallel: | ||
- **Multithreaded sampling**: Use `MCMCThreads()` to run one chain per thread | ||
- **Distributed sampling**: Use `MCMCDistributed()` for distributed computing | ||
|
||
See the [Core Functionality guide](../core-functionality/#sampling-multiple-chains) for examples. | ||
AoifeHughes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
### 2. Threading Within Models | ||
Using threads inside your model (e.g., `Threads.@threads`) requires more care: | ||
|
||
```julia | ||
@model function f(x) | ||
Threads.@threads for i in eachindex(x) | ||
x[i] ~ Normal() # UNSAFE: Assume statements in threads can crash! | ||
end | ||
end | ||
AoifeHughes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``` | ||
|
||
**Important limitations:** | ||
- **Observe statements**: Generally safe to use in threaded loops | ||
- **Assume statements** (sampling statements): Often crash unpredictably or produce incorrect results | ||
- **AD backend compatibility**: Many AD backends don't support threading. Check the [multithreaded column in ADTests](https://turinglang.org/ADTests/) for compatibility | ||
|
||
For safe parallelism within models, consider vectorized operations instead of explicit threading. | ||
|
||
## How do I check the type stability of my Turing model? | ||
|
||
Type stability is crucial for performance. Check out: | ||
- [Performance Tips]({{< meta usage-performance-tips >}}) - includes specific advice on type stability | ||
- Use `DynamicPPL.DebugUtils.model_warntype` to check type stability of your model | ||
|
||
## How do I debug my Turing model? | ||
|
||
For debugging both statistical and syntactical issues: | ||
- [Troubleshooting Guide]({{< meta usage-troubleshooting >}}) - common errors and their solutions | ||
- For more advanced debugging, DynamicPPL provides `DynamicPPL.DebugUtils` for inspecting model internals | ||
AoifeHughes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
## What are the main differences between Turing and Stan syntax? | ||
|
||
Key syntactic differences include: | ||
|
||
- **Parameter blocks**: Stan requires explicit `data`, `parameters`, `transformed parameters`, and `model` blocks. In Turing, everything is defined within the `@model` macro | ||
AoifeHughes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
- **Variable declarations**: Stan requires upfront type declarations in parameter blocks. Turing infers types from the sampling statements | ||
- **Transformed data**: Stan has a `transformed data` block for preprocessing. In Turing, data transformations should be done before defining the model | ||
- **Generated quantities**: Stan has a `generated quantities` block. In Turing, use the approach described in [Tracking Extra Quantities]({{< meta usage-tracking-extra-quantities >}}) | ||
|
||
Example comparison: | ||
```stan | ||
// Stan | ||
data { | ||
int<lower=0> N; | ||
vector[N] y; | ||
} | ||
penelopeysm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parameters { | ||
real mu; | ||
real<lower=0> sigma; | ||
} | ||
model { | ||
y ~ normal(mu, sigma); | ||
} | ||
``` | ||
AoifeHughes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```julia | ||
# Turing | ||
@model function my_model(y) | ||
mu ~ Normal(0, 1) | ||
sigma ~ truncated(Normal(0, 1), 0, Inf) | ||
AoifeHughes marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
y ~ Normal(mu, sigma) | ||
end | ||
``` | ||
|
||
## Which automatic differentiation backend should I use? | ||
|
||
The choice of AD backend can significantly impact performance. See: | ||
- [Automatic Differentiation Guide]({{< meta usage-automatic-differentiation >}}) - comprehensive comparison of ForwardDiff, Mooncake, ReverseDiff, and other backends | ||
- [Performance Tips]({{< meta usage-performance-tips >}}#choose-your-ad-backend) - quick guide on choosing backends | ||
- [AD Backend Benchmarks](https://turinglang.org/ADTests/) - performance comparisons across various models | ||
|
||
## I changed one line of my model and now it's so much slower; why? | ||
|
||
Small changes can have big performance impacts. Common culprits include: | ||
- Type instability introduced by the change | ||
- Switching from vectorized to scalar operations (or vice versa) | ||
- Inadvertently causing AD backend incompatibilities | ||
- Breaking assumptions that allowed compiler optimizations | ||
|
||
See our [Performance Tips]({{< meta usage-performance-tips >}}) and [Troubleshooting Guide]({{< meta usage-troubleshooting >}}) for debugging performance regressions. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.