Skip to content

Commit

Permalink
Extend Distributions.canonform (#39)
Browse files Browse the repository at this point in the history
* Extend `Distributions.canonform`

* Add tests

* Add comment

* Bump version
  • Loading branch information
mjp98 authored Nov 8, 2022
1 parent 479bf73 commit ecd9d47
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "KeyedDistributions"
uuid = "2576fb08-064d-4cab-b15d-8dda7fcb9a6d"
authors = ["Invenia Technical Computing Corporation"]
version = "0.1.11"
version = "0.1.12"

[deps]
AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f"
Expand Down
5 changes: 5 additions & 0 deletions src/KeyedDistributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ end
Distributions.entropy(d::KeyedDistribution) = entropy(distribution(d))
Distributions.entropy(d::KeyedDistribution, b::Real) = entropy(distribution(d), b)

function Distributions.canonform(d::KeyedDistribution)
named_keys = NamedTuple{dimnames(d)}(axiskeys(d)) # Define/exists a shorthand for this?
return KeyedDistribution(canonform(distribution(d)); named_keys...)
end

# Univariate Distributions only

for f in (:logpdf, :quantile, :mgf, :cf)
Expand Down
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ using Test

@test isapprox(rand(rng, kd), 0.39349598502717537)
@test isapprox(rand(rng, kd, 2), [0.519693102856957, 0.6505773044249047])

@testset "canonform" begin
ckd = canonform(kd)

@test axiskeys(ckd) == axiskeys(kd)
@test dimnames(ckd) == dimnames(kd)
@test distribution(ckd) == canonform(d)
end
end

@testset "Multi-variate" begin
d = MvNormal([7.0, 5.0], Matrix([1.0 0.0; 0.0 1.0]))

@testset "canonform" begin
kd = KeyedDistribution(d; dim1=["x1", "x2"])
ckd = canonform(kd)

@test axiskeys(ckd) == axiskeys(kd)
@test dimnames(ckd) == dimnames(kd)
@test distribution(ckd) == canonform(d)
end
end

@testset "Matrix-variate" begin
Expand Down

2 comments on commit ecd9d47

@mjp98
Copy link
Member Author

@mjp98 mjp98 commented on ecd9d47 Nov 8, 2022

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/71869

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.1.12 -m "<description of version>" ecd9d47566f99e0b382c621a72cdd5f983e75b5b
git push origin v0.1.12

Please sign in to comment.