Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add partype method to lognormal and semicircle #1773

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

bgctw
Copy link
Contributor

@bgctw bgctw commented Sep 8, 2023

Notice that LogNormal was missing the partype method. Hence, I added it and checked all uniform/continuous distributions. If they were parametric, I added the partype method for them.

@codecov-commenter
Copy link

codecov-commenter commented Sep 8, 2023

Codecov Report

Patch coverage is 71.42% of modified lines.

Files Changed Coverage
src/univariate/continuous/semicircle.jl 0.00%
src/univariate/locationscale.jl 66.66%
src/univariate/continuous/lognormal.jl 100.00%
src/univariate/discrete/discreteuniform.jl 100.00%
src/univariate/discrete/hypergeometric.jl 100.00%

📢 Thoughts on this report? Let us know!.

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

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

It would be good to add a check for partype to the automatic distribution test suite (at least for univariate distributions), e.g., in

function verify_and_test(D::Union{Type,Function}, d::UnivariateDistribution, dct::Dict, n_tsamples::Int)
. Something along the lines of partype(d) === Base.promote_typeof(params...) and partype(d) === Float32 in the Float32 conversion checks.

src/univariate/continuous/lognormal.jl Outdated Show resolved Hide resolved
src/univariate/continuous/semicircle.jl Outdated Show resolved Hide resolved
@bgctw
Copy link
Contributor Author

bgctw commented Sep 22, 2023

I implemented the tests for all distributions suggested by @devmotion.

They cause the following problems.

  • With TruncatedDistribution there may be Nothing types for some parameters (
    • took care of this special case in the checks (in ae09da6)
  • test/util.jl explitly tests partype of Hypergeometric and DiscreteUniform to be Float64, although only Integer-valued parameters are resonable for those distributions.
    • I changed the tests (in b8511e9) but this may brake code that depends on parameter types being fixed to Float64

but promote eltype T with eltype(inner)
but also promote partype T with partype(inner)
.gitignore Outdated Show resolved Hide resolved
src/univariate/discrete/discreteuniform.jl Outdated Show resolved Hide resolved
src/univariate/discrete/discreteuniform.jl Outdated Show resolved Hide resolved
src/univariate/discrete/discreteuniform.jl Outdated Show resolved Hide resolved
@@ -73,7 +87,7 @@ modes(d::DiscreteUniform) = [d.a:d.b]
pdf(d::DiscreteUniform, x::Real) = insupport(d, x) ? d.pv : zero(d.pv)
logpdf(d::DiscreteUniform, x::Real) = log(pdf(d, x))

function cdf(d::DiscreteUniform, x::Int)
function cdf(d::DiscreteUniform, x::Integer)
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated?

Suggested change
function cdf(d::DiscreteUniform, x::Integer)
function cdf(d::DiscreteUniform, x::Int)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will adapt this when reverting the DiscreteUniform to non-parametric.

src/univariate/discrete/discreteuniform.jl Outdated Show resolved Hide resolved
src/univariate/locationscale.jl Outdated Show resolved Hide resolved
Comment on lines 66 to 70
# truncated parameters may be nothing: Union{Nothing, promote_type()}
# but partype should still be type of the non-nothing ones
#@test partype(d) === promote_type(typeof.(pars)...)
@test partype(d) === promote_type(typeof.(pars)...) ||
Union{Nothing,partype(d)} === promote_type(typeof.(pars)...)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just use something like

Suggested change
# truncated parameters may be nothing: Union{Nothing, promote_type()}
# but partype should still be type of the non-nothing ones
#@test partype(d) === promote_type(typeof.(pars)...)
@test partype(d) === promote_type(typeof.(pars)...) ||
Union{Nothing,partype(d)} === promote_type(typeof.(pars)...)
# truncated parameters may be nothing: Union{Nothing, promote_type()}
# but partype should still be type of the non-nothing ones
@test partype(d) === mapfoldl(typeof, (S, T) -> T <: Real ? promote_type(S, T) : S, pars; init = Bool)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to wrap my head around, but implemented and tested this more general implementation with an own commit.

test/univariates.jl Outdated Show resolved Hide resolved
bgctw and others added 4 commits September 25, 2023 09:19
Co-authored-by: David Widmann <[email protected]>
will be moved to its own pull-request
e.g. Nothing in Truncated distribution
D is not used inside the function any more -> can simpify

Co-authored-by: David Widmann <[email protected]>
@bgctw
Copy link
Contributor Author

bgctw commented Sep 25, 2023

Based on the code comments of @devmotion, I reverted the changes to discreteuniform.

Originally, I introduced those modifications to discreteuniform to test how in addition to Float64/Float32 Distribution can also use a type parameter to allow using Int64/Int32. Since, this blows up the pull-request, I will create an PR after this PR is hopefully accepted.

@bgctw
Copy link
Contributor Author

bgctw commented Oct 18, 2023

Do I still need to do something to move this PR forward?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants