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

[feature request] Generic fallback for non-supported distributions ? #67

Open
lrnv opened this issue Oct 27, 2023 · 2 comments
Open

[feature request] Generic fallback for non-supported distributions ? #67

lrnv opened this issue Oct 27, 2023 · 2 comments

Comments

@lrnv
Copy link

lrnv commented Oct 27, 2023

It would be neat if there was a generic fallback for non-supported distributions. I can come up with a simple solution for continuous distributions:

# fallback for UnivariateContinuousDistribution
import Cubature
pcubature(x * Distributions.pdf(X,x), minimum(X), maximum(X))

but i need to think a bit more for discrete infinite supports.

Edit: There is ageneric fallback in Distributions.jl implemented there: https://github.com/JuliaStats/Distributions.jl/blob/master/src/functionals.jl

So maybe this is just an interface issue, Expectations.jl should not throw when it does not know the distribution but simply use the Distributions.jl one. Thanks @devmotion for the link.

@arnavs
Copy link
Member

arnavs commented Nov 3, 2023

Currently we have

Continuous iterable expectations (no nodes supplied.) =#
, which falls back to Gauss-Legendre for continuous distributions that lack a specified method.

If we're throwing an error instead of using it, that's definitely a mistake. (Though IMO there should be a warning.) I'll look into it.

(There is also

function _expectation(dist::ContinuousUnivariateDistribution, alg::Type{QuantileRange}; n::Int=50, q0::Real=0.001, qN::Real=0.999, kwargs...)
, which integrates over a uniform quantile grid.)

@lrnv
Copy link
Author

lrnv commented Nov 5, 2023

I think this is the method I hitted :

function _expectation(dist::ContinuousUnivariateDistribution, alg::Type{Gaussian}; n=500, kwargs...)
a = minimum(dist)
b = maximum(dist)
(a > -Inf && b < Inf) || throw(ArgumentError("The distribution must be defined on a compact interval. If applicable, bound the distribution by truncating, e.g., expectation(truncated(Pareto(),0.0,1000.0)"))
rawNodes, rawWeights = gausslegendre(n)
# Transform nodes to proper interval.
nodes = map(x -> (0.5(b - a)) * x + (a + b) / 2, rawNodes)
# Add pdf to weights.
compoundWeights = [rawWeights[i] * pdf(dist, nodes[i]) for i in 1:n]
# Add scale factor to weights.
weights = (b - a) / 2 * compoundWeights
return IterableExpectation(nodes, weights);
end

In particular with the Pareto case. While Distributions.jl's expectation method handles these cases (infinite support) without any issue.

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

No branches or pull requests

2 participants