You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/extends.md
+35-44
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,8 @@ Whereas this package already provides a large collection of common distributions
4
4
5
5
Generally, you don't have to implement every API method listed in the documentation. This package provides a series of generic functions that turn a small number of internal methods into user-end API methods. What you need to do is to implement this small set of internal methods for your distributions.
6
6
7
-
By default, `Discrete` sampleables have the support of type `Int` while `Continuous` sampleables have the support of type `Float64`. If this assumption does not hold for your new distribution or sampler, or its `ValueSupport` is neither `Discrete` nor `Continuous`, you should implement the `eltype` method in addition to the other methods listed below.
8
-
9
7
**Note:** The methods that need to be implemented are different for distributions of different variate forms.
10
8
11
-
12
9
## Create a Sampler
13
10
14
11
Unlike full-fledged distributions, a sampler, in general, only provides limited functionalities, mainly to support sampling.
@@ -18,60 +15,48 @@ Unlike full-fledged distributions, a sampler, in general, only provides limited
18
15
To implement a univariate sampler, one can define a subtype (say `Spl`) of `Sampleable{Univariate,S}` (where `S` can be `Discrete` or `Continuous`), and provide a `rand` method, as
19
16
20
17
```julia
21
-
functionrand(rng::AbstractRNG, s::Spl)
18
+
functionBase.rand(rng::AbstractRNG, s::Spl)
22
19
# ... generate a single sample from s
23
20
end
24
21
```
25
22
26
-
The package already implements a vectorized version of `rand!` and `rand` that repeatedly calls the scalar version to generate multiple samples; as wells as a one arg version that uses the default random number generator.
27
-
28
-
### Multivariate Sampler
23
+
The package already implements vectorized versions `rand!(rng::AbstractRNG, s::Spl, dims::Int...)` and `rand(rng::AbstractRNG, s::Spl, dims::Int...)` that repeatedly call the scalar version to generate multiple samples.
24
+
Additionally, the package implements versions of these functions without the `rng::AbstractRNG` argument that use the default random number generator.
29
25
30
-
To implement a multivariate sampler, one can define a subtype of `Sampleable{Multivariate,S}`, and provide both `length` and `_rand!` methods, as
26
+
If there is a more efficient method to generate multiple samples, one should provide the following method
31
27
32
28
```julia
33
-
Base.length(s::Spl) =...# return the length of each sample
34
-
35
-
function_rand!(rng::AbstractRNG, s::Spl, x::AbstractVector{T}) where T<:Real
36
-
# ... generate a single vector sample to x
29
+
function Random.rand!(rng::AbstractRNG, s::Spl, x::AbstractArray{<:Real})
30
+
# ... generate multiple samples from s in x
37
31
end
38
32
```
39
33
40
-
This function can assume that the dimension of `x` is correct, and doesn't need to perform dimension checking.
34
+
### Multivariate Sampler
41
35
42
-
The package implements both `rand` and `rand!` as follows (which you don't need to implement in general):
36
+
To implement a multivariate sampler, one can define a subtype of `Sampleable{Multivariate,S}`, and provide `length`, `rand`, and `rand!` methods, as
@boundscheck# ... check size (and possibly indices) of `x`
75
60
# ... generate multiple vector samples in batch
76
61
end
77
62
```
@@ -80,17 +65,22 @@ Remember that each *column* of A is a sample.
80
65
81
66
### Matrix-variate Sampler
82
67
83
-
To implement a multivariate sampler, one can define a subtype of `Sampleable{Multivariate,S}`, and provide both `size`and `_rand!` methods, as
68
+
To implement a multivariate sampler, one can define a subtype of `Sampleable{Multivariate,S}`, and provide `size`, `rand`, and `rand!` methods, as
84
69
85
70
```julia
86
71
Base.size(s::Spl) =...# the size of each matrix sample
87
72
88
-
function_rand!(rng::AbstractRNG, s::Spl, x::DenseMatrix{T}) where T<:Real
89
-
# ... generate a single matrix sample to x
73
+
functionBase.rand(rng::AbstractRNG, s::Spl)
74
+
# ... generate a single matrix sample from s
90
75
end
91
-
```
92
76
93
-
Note that you can assume `x` has correct dimensions in `_rand!` and don't have to perform dimension checking, the generic `rand` and `rand!` will do dimension checking and array allocation for you.
0 commit comments