|
6 | 6 |
|
7 | 7 | using Distributions |
8 | 8 | using OffsetArrays |
| 9 | +using ForwardDiff |
9 | 10 | using Test, Random, LinearAlgebra |
10 | 11 |
|
11 | 12 |
|
|
465 | 466 |
|
466 | 467 | end |
467 | 468 | end |
| 469 | + |
| 470 | +@testset "Testing fit for Chi" begin |
| 471 | + ν = 3.1 |
| 472 | + for func in funcs, D in (Chi, Chi{Float64}, Chi{Float32}) |
| 473 | + v = func[1](n0) |
| 474 | + z = func[2](D(ν), n0) |
| 475 | + for x in (z, OffsetArray(z, -n0 ÷ 2)), w in (v, OffsetArray(v, -n0 ÷ 2)) |
| 476 | + ss = @inferred suffstats(D, x) |
| 477 | + @test ss isa Distributions.ChiStats |
| 478 | + @test ss.mlogx ≈ mean(log.(x)) |
| 479 | + |
| 480 | + d = @inferred fit(D, x) |
| 481 | + @test d isa D |
| 482 | + @test ForwardDiff.derivative(ν -> sum(logpdf.(Chi(ν), x)), dof(d)) ≈ 0 atol = (eps(partype(d)))^(2/3) |
| 483 | + |
| 484 | + if axes(x) == axes(w) |
| 485 | + d = @inferred fit(D, x, w) |
| 486 | + @test d isa D |
| 487 | + @test ForwardDiff.derivative(ν -> dot(logpdf.(Chi(ν), x), w), dof(d)) ≈ 0 atol = (eps(partype(d)))^(2/3) |
| 488 | + |
| 489 | + ss = @inferred suffstats(D, x, w) |
| 490 | + @test ss isa Distributions.ChiStats |
| 491 | + @test ss.mlogx ≈ dot(w ./ sum(w), log.(x)) |
| 492 | + else |
| 493 | + @test_throws DimensionMismatch("Inconsistent array dimensions: Axes of samples and sample weights must be equal.") suffstats(D, x, w) |
| 494 | + @test_throws DimensionMismatch("Inconsistent array dimensions: Axes of samples and sample weights must be equal.") fit(D, x, w) |
| 495 | + end |
| 496 | + end |
| 497 | + end |
| 498 | +end |
| 499 | + |
| 500 | + |
| 501 | +@testset "Testing fit for Chisq" begin |
| 502 | + ν = 4.3 |
| 503 | + for func in funcs, D in (Chisq, Chisq{Float64}, Chisq{Float32}) |
| 504 | + v = func[1](n0) |
| 505 | + z = func[2](D(ν), n0) |
| 506 | + for x in (z, OffsetArray(z, -n0 ÷ 2)), w in (v, OffsetArray(v, -n0 ÷ 2)) |
| 507 | + ss = @inferred suffstats(D, x) |
| 508 | + @test ss isa Distributions.ChisqStats |
| 509 | + @test ss.mlogx ≈ mean(log.(x)) |
| 510 | + |
| 511 | + d = @inferred fit(D, x) |
| 512 | + @test d isa D |
| 513 | + @test ForwardDiff.derivative(ν -> sum(logpdf.(Chisq(ν), x)), dof(d)) ≈ 0 atol = (eps(partype(d)))^(2/3) |
| 514 | + |
| 515 | + if axes(x) == axes(w) |
| 516 | + ss = @inferred suffstats(D, x, w) |
| 517 | + @test ss isa Distributions.ChisqStats |
| 518 | + @test ss.mlogx ≈ dot(w ./ sum(w), log.(x)) |
| 519 | + |
| 520 | + d = @inferred fit(D, x, w) |
| 521 | + @test d isa D |
| 522 | + @test ForwardDiff.derivative(ν -> dot(logpdf.(Chisq(ν), x), w), dof(d)) ≈ 0 atol = (eps(partype(d)))^(2/3) |
| 523 | + else |
| 524 | + @test_throws DimensionMismatch("Inconsistent array dimensions: Axes of samples and sample weights must be equal.") suffstats(D, x, w) |
| 525 | + @test_throws DimensionMismatch("Inconsistent array dimensions: Axes of samples and sample weights must be equal.") fit(D, x, w) |
| 526 | + end |
| 527 | + end |
| 528 | + end |
| 529 | +end |
0 commit comments