I have defined the following Distribution,
using Distributions
struct ComplexNormal <:ContinuousUnivariateDistribution
μ::ComplexF64
σ::Float64
function ComplexNormal(μ=0,σ=1) new(μ,σ) end
end
function Base.rand(d::ComplexNormal)
randn(ComplexF64)*d.σ+d.μ
end
I’m trying to figure out how to make rand(ComplexNormal(),2)
and rand(ComplexNormal(),2,2)
work.
According to the documentation, The package already implements a vectorized version of rand!
and rand
that repeatedly calls the he scalar version to generate multiple samples. Thus I probably do not need to define Base.rand(::ComplexNormal, n::Int)
, but after googling for 1 hour, I couldn’t figure out how to do it.
Any help will be greatly appreciated, thank you!
Thank you, certainly gets me closer to what I wanted.
using Distributions, Random
struct ComplexNormal <:ContinuousUnivariateDistribution
μ::ComplexF64
σ::Float64
function ComplexNormal(μ=0,σ=1) new(μ,σ) end
end
Base.rand(rng::AbstractRNG, d::ComplexNormal) =randn(ComplexF64)*d.σ+d.μ;
rand(ComplexNormal(1+2im,1))
generates a single element works.
rand(ComplexNormal(1+2im,1),2)
don’t quiet, as it gives
InexactError: Float64(0.37414329354088083 + 1.039498256924925im)
This is probably outside of what I asked in this post, I’m going to start a new one.
I am wondering if you actually read the docs though. You are probably looking for SamplerTrivial
, as in
https://docs.julialang.org/en/v1/stdlib/Random/#A-simple-sampler-without-pre-computed-data
1 Like
Yes, I do noticed that I can achieve what I want with the following
using Random
struct CNormal
μ::ComplexF64
σ::Float64
end
Random.rand(rng::AbstractRNG, d::Random.SamplerTrivial{CNormal}) = randn(rng,ComplexF64)*d[].σ+d[].μ
rand(CNormal(0,1),2)
However, I have not yet figure out how to make this work if I want CNormal <:ContinuousUnivariateDistribution
The following is a solution, but there probably is a way to make the above work
using Distributions, Random
struct ComplexNormal <:ContinuousUnivariateDistribution
μ::ComplexF64
σ::Float64
function ComplexNormal(μ=0,σ=1) new(μ,σ) end
end
Base.rand(rng::AbstractRNG, d::ComplexNormal) = randn(rng,ComplexF64)*d.σ+d.μ;
Base.eltype(::Type{ComplexNormal}) = ComplexF64