-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests against analytical expressions for first and second moments.
- Loading branch information
Showing
6 changed files
with
135 additions
and
13 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
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
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,4 @@ | ||
julia 0.5 | ||
Distributions | ||
RCall | ||
Reexport |
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,17 @@ | ||
using RCall | ||
try | ||
R"options(repos=structure(c(CRAN=\"https://cloud.r-project.org/\")))" | ||
R"""pkgTest <- function(x) | ||
{ | ||
if (!require(x,character.only = TRUE, quiet = TRUE)) | ||
{ | ||
install.packages(x,dep=TRUE) | ||
if(!require(x,character.only = TRUE)) stop(\"Package not found\") | ||
} | ||
}""" | ||
R"pkgTest(\"statmod\")" | ||
R"did_it_work <- \"statmod\" %in% loadedNamespaces()" | ||
@assert @rget did_it_work | ||
catch m | ||
error("statmod was not installed, build.jl has failed to set up DistQuads.jl. Please report this to the issue tracker https://github.com/pkofod/DistQuads.jl/issues") | ||
end |
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,45 +1,52 @@ | ||
module DistQuads # DistQuads | ||
|
||
using Distributions, | ||
RCall | ||
export DistQuad, E | ||
using RCall, | ||
Reexport | ||
@reexport using Distributions | ||
export DistQuad, E, mean, var | ||
import Base: mean, var | ||
# FIXME Should install statmod via build! | ||
R"library(statmod)" | ||
|
||
type DistQuad | ||
x | ||
w | ||
d | ||
end | ||
nodes(dq::DistQuad) = dq.x | ||
weights(dq::DistQuad) = dq.w | ||
distribution(dq::DistQuad) = dq.d | ||
|
||
DistQuad(d) = DistQuad(d, 32) | ||
DistQuad(d; N = 32) = DistQuad(d, N) | ||
function DistQuad(d::Distributions.Beta, N) | ||
R"nodesweight<-gauss.quad.prob($N, dist=\"beta\", alpha=$(d.α), beta=$(d.β))" | ||
nw = @rget nodesweight | ||
DistQuad(nw[:nodes], nw[:weights]) | ||
DistQuad(nw[:nodes], nw[:weights], d) | ||
end | ||
|
||
function DistQuad(d::Distributions.Gamma, N) | ||
R"nodesweight<-gauss.quad.prob($N, dist=\"gamma\", alpha=$(d.α), beta=$(d.Θ))" | ||
R"nodesweight<-gauss.quad.prob($N, dist=\"gamma\", alpha=$(d.α), beta=$(d.θ))" | ||
nw = @rget nodesweight | ||
DistQuad(nw[:nodes], nw[:weights]) | ||
DistQuad(nw[:nodes], nw[:weights], d) | ||
end | ||
|
||
function DistQuad(d::Distributions.Normal, N) | ||
R"nodesweight<-gauss.quad.prob($N, dist=\"normal\", mu=$(d.μ), sigma=$(d.σ))" | ||
nw = @rget nodesweight | ||
DistQuad(nw[:nodes], nw[:weights]) | ||
DistQuad(nw[:nodes], nw[:weights], d) | ||
end | ||
|
||
function DistQuad(d::Distributions.LogNormal, N) | ||
R"nodesweight<-gauss.quad.prob($N, dist=\"normal\", mu=$(d.μ), sigma=$(d.σ))" | ||
nw = @rget nodesweight | ||
DistQuad(exp.(nw[:nodes]), nw[:weights]) | ||
DistQuad(exp.(nw[:nodes]), nw[:weights], d) | ||
end | ||
|
||
|
||
E(f, dq::DistQuad) = dot(f.(nodes(dq)), weights(dq)) | ||
E(f, d) = E(f, DistQuad(d)) | ||
|
||
mean(dq::DistQuad) = E(identity, dq) | ||
var(dq::DistQuad) = E(x->(x-mean(dq))^2, dq) | ||
|
||
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 |
---|---|---|
@@ -1,5 +1,62 @@ | ||
using DistQuads | ||
using DistQuads, Distributions | ||
using Base.Test | ||
|
||
# write your own tests here | ||
@test 1 == 2 | ||
|
||
@testset "analytic vs quadrature" begin | ||
@testset "Beta" begin | ||
for a = 0.1:0.1:3.0, b = 0.1:0.1:3.0 | ||
bd = Beta(a, b) | ||
dq = DistQuad(bd) | ||
# Test that mean function didn't accidentally regress | ||
@test mean(dq) == E(identity, dq) | ||
# Test nodes and weights against Distributions | ||
@test norm(mean(dq)-mean(bd), Inf) < 1e-8 | ||
@test norm(var(dq)-var(bd), Inf) < 1e-8 | ||
# Test nodes and weights against know mean (Distributions could in principle regress) | ||
@test norm(mean(dq)-a/(a+b), Inf) < 1e-8 | ||
@test norm(var(dq)-a*b/((a+b+1)*(a+b)^2), Inf) < 1e-8 | ||
end | ||
end | ||
@testset "Gamma" begin | ||
for a = 0.1:0.1:3.0, b = 0.1:0.1:3.0 | ||
bd = Gamma(a, b) | ||
dq = DistQuad(bd) | ||
# Test that mean function didn't accidentally regress | ||
@test mean(dq) == E(identity, dq) | ||
# Test nodes and weights against Distributions | ||
@test norm(mean(dq)-mean(bd), Inf) < 1e-8 | ||
@test norm(var(dq)-var(bd), Inf) < 1e-8 | ||
# Test nodes and weights against know mean (Distributions could in principle regress) | ||
@test norm(mean(dq)-a*b, Inf) < 1e-8 | ||
@test norm(var(dq)-a*b^2, Inf) < 1e-8 | ||
end | ||
end | ||
@testset "Normal" begin | ||
for μ = 0.1:0.1:3.0, σ = 0.1:0.1:3.0 | ||
bd = Normal(μ, σ) | ||
dq = DistQuad(bd) | ||
# Test that mean function didn't accidentally regress | ||
@test mean(dq) == E(identity, dq) | ||
# Test nodes and weights against Distributions | ||
@test norm(mean(dq)-mean(bd), Inf) < 1e-8 | ||
@test norm(var(dq)-var(bd), Inf) < 1e-8 | ||
# Test nodes and weights against know mean (Distributions could in principle regress) | ||
@test norm(mean(dq)-μ, Inf) < 1e-8 | ||
@test norm(var(dq)-σ^2, Inf) < 1e-8 | ||
end | ||
end | ||
@testset "LogNormal" begin | ||
for μ = 0.1:0.1:3.0, σ = 0.1:0.1:2.0 | ||
bd = LogNormal(μ, σ) | ||
dq = DistQuad(bd) | ||
# Test that mean function didn't accidentally regress | ||
@test mean(dq) == E(identity, dq) | ||
# Test nodes and weights against Distributions | ||
@test norm(mean(dq)-mean(bd), Inf) < 1e-8 | ||
@test norm(var(dq)-var(bd), Inf) < 1e-8 # not too precise! | ||
# Test nodes and weights against know mean (Distributions could in principle regress) | ||
@test norm(mean(dq)-exp(μ+σ^2/2), Inf) < 1e-8 | ||
@test norm(var(dq)-(exp(σ^2)-1)*exp(2μ+σ^2), Inf) < 1e-8 # not too precise! | ||
end | ||
end | ||
end |