Open baggepinnen opened 5 years ago
Some results for rand
d = Product([Normal(0,2), Normal(0,2)])
dt = TupleProduct((Normal(0,2), Normal(0,2)))
noisevec = zeros(2)
@btime rand!($d, $noisevec) # 36.078 ns (1 allocation: 16 bytes)
@btime rand!($dt, $noisevec) # 18.392 ns (0 allocations: 0 bytes)
This allows completely allocation free sampling from the distribution using the following implementation
@generated function Distributions._rand!(rng::AbstractRNG, d::TupleProduct{N}, x::AbstractVector{<:Real}) where N
quote
Base.Cartesian.@nexprs $N i->(x[i] = rand(rng, d.v[i]))
x
end
end
I needed a
Product
distribution with mixed continuous/discrete support for a project and thus implemented the typesWould you be interested in a PR with (parts of) this implementation, possibly with a different name?
Some code and benchmarks below
A small benchmark
The package where I've implemented this is called
LowLevelPartricleFilters
, which also defines some methods for distributions and static arrays. If this package is loaded we have the following timingsIf
LowLevelPartricleFilters
and the special static methods are not loaded, we have identical timings forSVector
andVector
Implementation
Key points, the rest of the implementation is here