Skip to content

Commit

Permalink
Ensure that binomlogpdf returns non-positive values, t distribution…
Browse files Browse the repository at this point in the history
…s with infinite parameter are supported, and add integration tests (#126)

* Ensure that `binomlogpdf` returns non-positive values

* Add test

* Add integration test

* Bump version

* Handle student t distribution with infinite `ν`
  • Loading branch information
devmotion authored Sep 29, 2021
1 parent e3fe89d commit f13b618
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/IntegrationTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: IntegrationTest

on:
pull_request:
branches:
- master
push:
branches:
- master
tags: '*'

jobs:
test:
name: ${{ matrix.package.repo }}/${{ matrix.package.group }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
julia-version: [1]
os: [ubuntu-latest]
package:
- {user: JuliaStats, repo: Distributions.jl}

steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.julia-version }}
arch: x64
- uses: julia-actions/julia-buildpkg@latest
- name: Clone Downstream
uses: actions/checkout@v2
with:
repository: ${{ matrix.package.user }}/${{ matrix.package.repo }}
path: downstream
- name: Load this and run the downstream tests
shell: julia --color=yes --project=downstream {0}
run: |
using Pkg
try
# force it to use this PR's version of the package
Pkg.develop(PackageSpec(path=".")) # resolver may fail with main deps
Pkg.update()
Pkg.test() # resolver may fail with test time deps
catch err
err isa Pkg.Resolve.ResolverError || rethrow()
# If we can't resolve that means this is incompatible by SemVer and this is fine
# It means we marked this as a breaking change, so we don't need to worry about
# Mistakenly introducing a breaking change, as we have intentionally made one
@info "Not compatible with this release. No problem." exception=err
exit(0) # Exit immediately, as a success
end
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StatsFuns"
uuid = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
version = "0.9.11"
version = "0.9.12"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
2 changes: 1 addition & 1 deletion src/distrs/binom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ binompdf(n::Real, p::Real, k::Real) = exp(binomlogpdf(n, p, k))
binomlogpdf(n::Real, p::Real, k::Real) = binomlogpdf(promote(n, p, k)...)
function binomlogpdf(n::T, p::T, k::T) where {T<:Real}
m = clamp(k, 0, n)
val = betalogpdf(m + 1, n - m + 1, p) - log(n + 1)
val = min(0, betalogpdf(m + 1, n - m + 1, p) - log(n + 1))
return 0 <= k <= n && isinteger(k) ? val : oftype(val, -Inf)
end
1 change: 1 addition & 0 deletions src/distrs/tdist.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tdistpdf(ν::Real, x::Real) = exp(tdistlogpdf(ν, x))

tdistlogpdf::Real, x::Real) = tdistlogpdf(promote(ν, x)...)
function tdistlogpdf::T, x::T) where {T<:Real}
isinf(ν) && return normlogpdf(x)
νp12 =+ 1) / 2
return loggamma(νp12) - (logπ + log(ν)) / 2 - loggamma/ 2) - νp12 * log1p(x^2 / ν)
end
7 changes: 7 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ end
@test binomlogpdf(1, 0.5, prevfloat(1.0)) == -Inf
@test binomlogpdf(1, 0.5, nextfloat(1.0)) == -Inf
end

@testset "binom special cases" begin
for (n, p, k) in ((5, 0.0, 0), (5, 1.0, 5))
@test iszero(binomlogpdf(n, p, k))
@test isone(binompdf(n, p, k))
end
end
2 changes: 2 additions & 0 deletions test/rmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ end
((1,), -5f0:0.1f0:5f0),
((1,), -Float16(5):Float16(0.1):Float16(5)),
((1,), -5//1:5//1),
((Inf,), -5.0:0.1:5.0),
((Inf32,), -5f0:0.1f0:5f0),
])

rmathcomp_tests("srdist", [
Expand Down

2 comments on commit f13b618

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/45751

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.12 -m "<description of version>" f13b618dc70fb1b4b5e667fbf4097fd4ac2ddb07
git push origin v0.9.12

Please sign in to comment.