Skip to content

Instantly share code, notes, and snippets.

@torfjelde
Last active September 17, 2019 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torfjelde/b0654411f717906bdcc33b46779ea74a to your computer and use it in GitHub Desktop.
Save torfjelde/b0654411f717906bdcc33b46779ea74a to your computer and use it in GitHub Desktop.
Example of applying the FSSD goodness-of-fit test to a ChiSq distribution using Log as as push-forward.
julia> using Bijectors
julia> using KernelGoodnessOfFit
julia> using Random; Random.seed!(123);
julia> using ForwardDiff
julia> function Distributions.gradlogpdf(d::ContinuousUnivariateDistribution, x::Real)
ForwardDiff.derivative(z -> logpdf(d, z), x)
end
julia> p = Chisq(2) # true distribution
Chisq{Float64}(ν=2.0)
julia> td = transformed(p) # transformed
Bijectors.TransformedDistribution{Chisq{Float64},Bijectors.Log{0},Univariate,0}(
dist: Chisq{Float64}(ν=2.0)
transform: Bijectors.Log{0}()
)
julia> α = 0.05 # size of the test
0.05
julia> n = 200 # number of samples
200
julia> num_experiments = 100 # number of experiments to run
100
julia> # Under H₀: p = q
total = 0.0 # rejection ratio
0.0
julia> for i = 1:num_experiments
xs = reshape(rand(p, n), 1, :)
ys = td.transform.(xs)
res = FSSDopt(ys, td; β_H₁ = 0.01)
global total
total += res.p_val ≤ α
end
julia> total / num_experiments
0.05
julia> (total / num_experiments) ≤ α
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment