Skip to content

Commit 7a3d331

Browse files
authored
Merge pull request #102 from adolgert/docs/two-readers
Docs/two readers
2 parents 55a614e + 905aff8 commit 7a3d331

File tree

6 files changed

+444
-76
lines changed

6 files changed

+444
-76
lines changed

docs/make.jl

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,51 +73,41 @@ makedocs(;
7373
canonical="https://adolgert.github.io/CompetingClocks.jl",
7474
assets=String[],
7575
),
76-
# develop.md
77-
# importance_skills.md
78-
# objects.md
79-
# rules.md
80-
# simple_board.md
81-
# splitting.jl
82-
# vas.md
8376
pages=[
8477
"Home" => "index.md",
8578
"Getting Started" => [
8679
"install.md",
80+
"quickstart.md",
8781
"mainloop.md",
82+
"choosing_sampler.md",
8883
"guide.md",
8984
"distributions.md"
9085
],
9186
"User Guide" => [
92-
"Background" => [
93-
"distrib.md",
94-
"background.md",
95-
"GSMP" => "gsmp.md",
96-
"samplers.md",
97-
"hierarchical.md",
98-
],
99-
"Simulation" => [
100-
"samplingcontext.md",
101-
"memory.md",
102-
"commonrandom.md",
103-
],
104-
"Statistics" => [
105-
"hamiltonianmontecarlo.md",
106-
"importance_skills.md",
107-
],
108-
"Debugging" => [
109-
"debugging.md"
110-
]
87+
"integration-guide.md",
88+
"interface.md",
89+
"samplers.md",
90+
"hierarchical.md",
91+
"debugging.md",
92+
"distrib.md",
93+
"background.md",
94+
"GSMP" => "gsmp.md",
95+
"memory.md",
11196
],
11297
"Examples" => [
11398
"Birth-death Process" => "constant_birth.md",
11499
"SIR Model" => "sir.md",
115100
"Reliability" => "reliability.md",
116101
"Gene Expression" => "gene_expression.md"
117102
],
103+
"Statistical Methods" => [
104+
"commonrandom.md",
105+
"importance_skills.md",
106+
"hamiltonianmontecarlo.md",
107+
],
118108
"API Reference" => [
109+
"samplingcontext.md",
119110
"contextinterface.md",
120-
"interface.md",
121111
"reference.md",
122112
"algorithms.md"
123113
]

docs/src/choosing_sampler.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Choosing a Sampler
2+
3+
How to make a sampler for events in CompetingClocks.jl.
4+
5+
## Quick Reference
6+
7+
| Name | Distribution support | Best use cases |
8+
|----------------|----------------------|--------------------------------|
9+
| First-to-fire | All | Fastest for simple simulation |
10+
| Next Reaction | All | Best for common random numbers |
11+
| First Reaction | All | Fastest for very few events |
12+
| Direct | Exponential-only | Quicker likelihood calculation |
13+
| Petri | All | Debug rare errors |
14+
15+
## High-level Interface
16+
17+
Every event in a simulation has an identifying key which can be any immutable Julia
18+
type. A `Tuple` key type works fine, but it will be more performant to use a
19+
concrete type for the `KeyType`. The second argument to the `SamplerBuilder`
20+
is a type to use for time. Basic usage:
21+
```julia
22+
builder = SamplerBuilder(KeyType, Float64)
23+
sampler = SamplingContext(builder, rng)
24+
```
25+
The sampler, itself, takes an `AbstractRNG` as its second argument.
26+
27+
Specify features for the sampler with keyword arguments to the `SamplerBuilder`.
28+
These features determine the type of the sampler.
29+
30+
* `step_likelihood=false`---Setting this to `true` let's you calculate the
31+
the likelihood of the next event and time before firing it.
32+
* `path_likelihood=false`---Set to true in order to calculate likelihood of a
33+
whole trajectory at any point in the simulation. This is more efficient than
34+
adding up steps in the likelihood along the way.
35+
* `likelihood_cnt=1`---Applies when likelihoods are enabled and supports
36+
importance sampling. Specifies how
37+
many event distributions will be used to calculate a vector of likelihoods.
38+
* `common_random=false`---For variance reduction, turn on recording of
39+
random number usage during sampling.
40+
* `start_time=0`---Sometimes you want a simulation to start at a different time.
41+
* `debug=false`---Whether to print debug messages using the `Logging` package.
42+
* `recording=false`---This will create a vector of every enable and disable
43+
event for test and debug.
44+
45+
## Hierarchical Samplers
46+
47+
For spatial simulations or for chemical simulations that aren't well-mixed,
48+
it can help to split a sampler into multiple buckets so that each bucket can
49+
update its own list of what fires next. This package does this by adding
50+
sampler groups.
51+
52+
A sampler group is
53+
54+
* A Symbol name for the sampler.
55+
* An inclusion function from `(ClockKey, Distribution)` to `Bool` that decides
56+
whether a given event belongs to this sampler.
57+
* An optional `sampler_spec` to say what kind of sampler this group should use.
58+
59+
```julia
60+
builder = SamplerBuilder(KeyType, Float64)
61+
add_group!(builder, :sparky => (x,d) -> x[1] == :recover, sampler_spec=(:nextreaction,))
62+
add_group!(builder, :forthright=>(x,d) -> x[1] == :infect)
63+
sampler = SamplingContext(builder, rng)
64+
```
65+
66+
The resulting sampler will be hierarchical. For every event, it will choose the
67+
soonest time among the soonest times from all sampler groups.

docs/src/index.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CurrentModule = CompetingClocks
33
```
44

55
# CompetingClocks
6+
[![Coverage](https://codecov.io/gh/adolgert/CompetingClocks.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/adolgert/CompetingClocks.jl)
67

78
Fast, composable samplers for stochastic discrete-event simulation.
89
This package gives your simulation or simulation framework statistical features like common random
@@ -20,8 +21,16 @@ or to calculate the likelihood of a sample path for statistical estimation.
2021

2122
![CompetingClocks chooses the next transition but the simulation tracks state and changes to state.](assets/CompetingClocksTopLevel.svg)
2223

23-
The background work for this library comes from [Continuous-time, discrete-event simulation from counting processes](https://arxiv.org/abs/1610.03939), by Andrew Dolgert, 2016.
24+
## Implementation Based on
2425

26+
* P. J. Haas, _Stochastic Petri Nets: Modelling, Stability, Simulation._ in Springer Series in Operations Research. New York, NY: Springer-Verlag New York, Inc, 2002. doi: 10.1007/b97265.
27+
* D. F. Anderson and T. G. Kurtz, Stochastic Analysis of Biochemical Systems. Springer International Publishing AG Switzerland, 2015.
28+
* [Continuous-time, discrete-event simulation from counting processes](https://arxiv.org/abs/1610.03939), by Andrew Dolgert, 2016.
29+
30+
## Version History
31+
32+
- v0.2.0 (2025-12) - Likelihood calculation, variance reduction.
33+
- v0.1.0 (2024-06) - Initial release of samplers.
2534

2635
## Usage
2736

@@ -37,14 +46,16 @@ The library provides you with samplers. Each sampler has the same interface. Her
3746

3847
Different samplers are specialized for sampling more quickly and accurately for different applications. For instance, some applications have very few events enabled at once, while some have many. Some applications use only exponentially-distributed events, while some have a mix of distribution types. Because continuous-time discrete event systems can fire many events, the literature has focused on reducing the number of CPU instructions required to sample each event, and this library reflects that focus.
3948

40-
## Why Use This?
41-
42-
If I make a quick simulation for myself, I sample distributions the moment an event is enabled and store the firing times in a [priority queue](https://juliacollections.github.io/DataStructures.jl/v0.12/priority-queue.html). When would I switch to this library?
43-
44-
* I want to evaluate the effect of changing simulation parameters by comparing multiple runs with [common random numbers](https://en.wikipedia.org/wiki/Variance_reduction#Common_Random_Numbers_(CRN)).
49+
## When NOT to use Competing Clocks
4550

46-
* I'm looking at rare events, so I want to use splitting techniques and [importance sampling](https://en.wikipedia.org/wiki/Importance_sampling).
51+
* **Pure exponential distributions?** JumpProcesses.jl is a complete framework for this.
52+
* **Need ODE coupling?** Again, it's easier to stay within SciML and JumpProcesses.jl.
53+
* **Want high-level frameworks?** Try Agents.jl.
4754

48-
* Performance matters (which it often doesn't), so I would like to try different samplers on my problem.
55+
CompetingClocks.jl is for:
4956

50-
* I want to focus on developing and testing my *model* not my *simulation algorithm*; CompetingClocks is designed and tested with care to ensure correctness.
57+
* Building a simulation framework with
58+
* General distributions (Weibull, Gamma, etc.),
59+
* Likelihood calculations or rare events, or
60+
* Variance reduction.
61+
* Advanced work in reliability models, disease models, queueing models.

0 commit comments

Comments
 (0)