-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add Distributions and Random * add Factored Distribution * add tests * remove test dependencies * specialization for faster code * add tests for mixed value support
- Loading branch information
1 parent
3329930
commit 05d9e67
Showing
5 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*.jl.mem | ||
/deps/deps.jl | ||
/docs/build | ||
Manifest.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,14 @@ authors = ["Johanni Brea <[email protected]>"] | |
version = "0.1.0" | ||
|
||
[deps] | ||
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
|
||
[compat] | ||
julia = "^1" | ||
|
||
[extras] | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test"] | ||
|
||
[compat] | ||
julia = "^1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module ApproxInferenceBase | ||
|
||
using Distributions | ||
using Random | ||
include("priors.jl") | ||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Distributions.pdf, Distributions.logpdf, Random.rand, Base.length | ||
|
||
struct MixedSupport <: ValueSupport; end | ||
|
||
""" | ||
Factored{N} <: Distribution{Multivariate, MixedSupport} | ||
a `Distribution` type that can be used to combine multiple `UnivariateDistribution`'s and sample from them. | ||
Example: it can be used as `prior = Factored(Normal(0,1), Uniform(-1,1))` | ||
""" | ||
struct Factored{N}<:Distribution{Multivariate,MixedSupport} | ||
p::NTuple{N,UnivariateDistribution} | ||
Factored(args::UnivariateDistribution...) = new{length(args)}(args) | ||
end | ||
""" | ||
pdf(d::Factored, x) = begin | ||
Function to evaluate the pdf of a `Factored` distribution object | ||
""" | ||
function pdf(d::Factored{N},x) where N | ||
s=pdf(d.p[1],x[1]) | ||
for i in 2:N | ||
s*=pdf(d.p[i],x[i]) | ||
end | ||
s | ||
end | ||
|
||
""" | ||
logpdf(d::Factored, x) = begin | ||
Function to evaluate the logpdf of a `Factored` distribution object | ||
""" | ||
function logpdf(d::Factored{N},x) where N | ||
s=logpdf(d.p[1],x[1]) | ||
for i in 2:N | ||
s+=logpdf(d.p[i],x[i]) | ||
end | ||
s | ||
end | ||
|
||
""" | ||
rand(rng::AbstractRNG, factoreddist::Factored) | ||
function to sample one element from a `Factored` object | ||
""" | ||
rand(rng::AbstractRNG,factoreddist::Factored{N}) where N = ntuple(i->rand(rng,factoreddist.p[i]),Val(N)) | ||
|
||
""" | ||
length(p::Factored) = begin | ||
returns the number of distributions contained in `p`. | ||
""" | ||
length(p::Factored{N}) where N = N | ||
|
||
export Factored |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,20 @@ | ||
using ApproxInferenceBase | ||
using Test | ||
using Distributions | ||
using Random | ||
|
||
@testset "Factored" begin | ||
d=Factored(Uniform(0,1),Uniform(100,101)) | ||
@test all((0,100) .<= rand(d) .<= (1,101)) | ||
@test pdf(d,(0.0,0.0)) == 0.0 | ||
@test pdf(d,(0.5,100.5)) == 1.0 | ||
@test logpdf(d,(0.5,100.5)) == 0.0 | ||
@test logpdf(d,(0.0,0.0)) == -Inf | ||
@test length(d) == 2 | ||
m=Factored(Uniform(0.00,1.0),DiscreteUniform(1,2)) | ||
sample=rand(m) | ||
@test 0<sample[1]<1 | ||
@test sample[2] == 1 || sample[2] == 2 | ||
@test pdf(m,sample) == 0.5 | ||
@test logpdf(m,sample) ≈ log(0.5) | ||
end |