-
Notifications
You must be signed in to change notification settings - Fork 19
Addition of step_warmup
#117
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
Changes from 8 commits
0987f5f
30c9f12
7faa73f
bd0bdc7
c620cca
572a286
6b842ee
ca03832
0441773
ea369ff
8e0ca53
6877978
b3b3148
ddc5254
ffbd32f
87480ff
76f2f23
c00d0c9
ef09c19
49b8406
f005746
9dccd8a
ff00e6e
7ce9f6b
3a217b2
de9bb2c
85d938f
0a667a4
91f5a10
7603171
ef68d04
25afc66
1886fa8
0ea293a
6e8f88e
44c55bb
3b4f6db
f9142a6
295fdc1
e6acb1f
2e9fa5c
366fceb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -91,7 +91,29 @@ function StatsBase.sample( | |||||||||||
end | ||||||||||||
|
||||||||||||
# Default implementations of regular and parallel sampling. | ||||||||||||
|
||||||||||||
""" | ||||||||||||
mcmcsample(rng, model, sampler, N_or_is_done; kwargs...) | ||||||||||||
|
||||||||||||
Default implementation of `sample` for a `model` and `sampler`. | ||||||||||||
|
||||||||||||
# Arguments | ||||||||||||
- `rng::Random.AbstractRNG`: the random number generator to use. | ||||||||||||
- `model::AbstractModel`: the model to sample from. | ||||||||||||
- `sampler::AbstractSampler`: the sampler to use. | ||||||||||||
- `N::Integer`: the number of samples to draw. | ||||||||||||
|
||||||||||||
# Keyword arguments | ||||||||||||
- `progress`: whether to display a progress bar. Defaults to `true`. | ||||||||||||
- `progressname`: the name of the progress bar. Defaults to `"Sampling"`. | ||||||||||||
- `callback`: a function that is called after each [`AbstractMCMC.step`](@ref). | ||||||||||||
Defaults to `nothing`. | ||||||||||||
- `num_warmup`: number of warmup samples to draw. Defaults to `0`. | ||||||||||||
- `discard_initial`: number of initial samples to discard. Defaults to `num_warmup`. | ||||||||||||
- `thinning`: number of samples to discard between samples. Defaults to `1`. | ||||||||||||
- `chain_type`: the type to pass to [`AbstractMCMC.bundle_samples`](@ref) at the | ||||||||||||
end of sampling to wrap up the resulting samples nicely. Defaults to `Any`. | ||||||||||||
- `kwargs...`: Additional keyword arguments to pass on to [`AbstractMCMC.step`](@ref). | ||||||||||||
""" | ||||||||||||
|
||||||||||||
function mcmcsample( | ||||||||||||
rng::Random.AbstractRNG, | ||||||||||||
model::AbstractModel, | ||||||||||||
|
@@ -100,14 +122,21 @@ function mcmcsample( | |||||||||||
progress=PROGRESS[], | ||||||||||||
progressname="Sampling", | ||||||||||||
callback=nothing, | ||||||||||||
discard_initial=0, | ||||||||||||
num_warmup=0, | ||||||||||||
discard_initial=num_warmup, | ||||||||||||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
thinning=1, | ||||||||||||
chain_type::Type=Any, | ||||||||||||
kwargs..., | ||||||||||||
) | ||||||||||||
# Check the number of requested samples. | ||||||||||||
N > 0 || error("the number of samples must be ≥ 1") | ||||||||||||
Ntotal = thinning * (N - 1) + discard_initial + 1 | ||||||||||||
Ntotal = thinning * (N - 1) + discard_initial + num_warmup + 1 | ||||||||||||
|
Ntotal = thinning * (N - 1) + discard_initial + num_warmup + 1 | |
discard_initial >= 0 || throw(ArgumentError("number of discarded samples must be non-negative")) | |
num_warmup >= 0 || throw(ArgumentError("number of warm-up samples must be non-negative")) | |
Ntotal = thinning * (N - 1) + discard_initial + 1 | |
Ntotal >= num_warmup || throw(ArgumentError("number of warm-up samples exceeds the total number of samples")) |
I thought we would do the following:
- If
num_warmup = 0
, we just do the same as currently: Samplediscard_initial
samples that are discarded + theN
samples that are returned, possibly after thinning them. - If
num_warmup > 0
, we still returnN
samples but depending ondiscard_initial
part of theN
samples might be samples from the warm-up stage. For instance:- If
num_warmup = 10
,discard_initiial = 0
, andN = 100
, we would sample in totalN
samples and return them, whereof the first 10 are warm-up samples. - If
num_warmup = 10
,discard_initial = 10
(the default if you just specifynum_warmup
), andN = 100
, then we would sampleN + discard_initial = 110
samples in total and return the lastN = 100
of them, so drop all warm-up samples. - If
num_warmup = 10
,discard_initial = 20
, andN = 100
, then we would sampleN + discard_initial = 120
samples in total and return the lastN = 100
of them, so we would drop all samples in the warm-up stage and the first 10 samples of the regular sampling.
- If
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry, yes this was left over from my initial implementation that treated discard_initial
and num_warmup
as "seperate phases".
I thought we would do the following:
Agreed, but isn't this what my impl is currently doing? With the exception of this line above of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, I stopped reviewing at this point and didn't check the rest 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, aight. Well, the rest is supposed to implement exactly what you outlined 😅
I'll see if I can also add some tests.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this seems to match what I wrote above.
devmotion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these should be accounted for in the progress logger as well (as done currently).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be good now 👍
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason to switch to a while
loop here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah no! I'll revert it to for-loop 👍
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the same/similar error checks as above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
Uh oh!
There was an error while loading. Please reload this page.