DistributionFits
Package DistributionFits allows fitting a distribution to a given set of aggregate statistics.
- to specified moments
d = fit(LogNormal, Moments(3.0,4.0))
(mean(d), var(d)) .≈ (3.0, 4.0)- mean and upper quantile point
d = fit(LogNormal, 3.0, @qp_uu(8))
(mean(d), quantile(d, 0.975)) .≈ (3.0, 8.0)- to mode and upper quantile point
d = fit(LogNormal, 3.0, @qp_uu(8), Val(:mode))
(mode(d), quantile(d, 0.975)) .≈ (3.0, 8.0)- to median and upper quantile point
d = fit(LogitNormal, 0.3, @qp_u(0.8), Val(:median))
(median(d), quantile(d, 0.95)) .≈ (0.3, 0.8)- to two quantiles, i.e confidence range
d = fit(Normal, @qp_ll(1.0), @qp_uu(8))
(quantile(d, 0.025), quantile(d, 0.975)) .≈ (1.0, 8.0)- approximate a different distribution by matching moments
dn = Normal(3,2)
d = fit(LogNormal, moments(dn))
(mean(d), var(d)) .≈ (3.0, 4.0)Fit to statistical moments
StatsAPI.fit — Methodfit(D, m)Fit a statistical distribution of type D to given moments m.
Arguments
D: The type of distribution to fitm: The moments of the distribution
Notes
This can be used to approximate one distribution by another.
See also AbstractMoments, moments.
Examples
d = fit(LogNormal, Moments(3.2,4.6));
(mean(d), var(d)) .≈ (3.2,4.6)d = fit(LogNormal, moments(Normal(3,1.2)));
(mean(d), std(d)) .≈ (3,1.2)# using StatsPlots
plot(d, label = "lognormal", ylab="probability density")
plot!(Normal(3,1.2), label = "normal")Fit to several quantile points
StatsAPI.fit — Methodfit(D, lower::QuantilePoint, upper::QuantilePoint)Fit a statistical distribution to a set of quantiles
Arguments
D: The type of the distribution to fitlower: lowerQuantilePoint(p,q)upper: upper QuantilePoint (p,q)
Examples
d = fit(LogNormal, @qp_m(3), @qp_uu(5));
quantile.(d, [0.5, 0.975]) ≈ [3,5]Fit to mean, mode, median and a quantile point
StatsAPI.fit — Methodfit(D, val, qp, ::Val{stats} = Val(:mean))Fit a statistical distribution to a quantile and given statistics
Arguments
D: The type of distribution to fitval: The value of statisticsqp: QuantilePoint(q,p)statsWhich statistics to fit: defaults toVal(:mean). Alternatives are:Val(:mode),Val(:median)
Examples
d = fit(LogNormal, 5.0, @qp_uu(14));
(mean(d),quantile(d, 0.975)) .≈ (5,14)d = fit(LogNormal, 5.0, @qp_uu(14), Val(:mode));
(mode(d),quantile(d, 0.975)) .≈ (5,14)Fit to mean and uncertainty parameter
For bayesian inversion it is often required to specify a distribution given the expected value (the predction of the population value) and a description of uncertainty of an observation.
DistributionFits.fit_mean_Σ — Functionfit_mean_Σ(::Type{<:Distribution}, mean, Σ)Fit a Distribution to mean and uncertainty quantificator Σ.
The meaning of Σ depends on the type of distribution:
MvLogNormal,MvNormal: the Covariancematrix of the associated normal distributionLogNormal,Normal: the scale parameter, i.e. the standard deviation at log-scale,σ
Currently supported distributions
Univariate continuous
- Normal
- LogNormal distribution
- LogitNormal distribution
- Exponential
- Laplace
- Weibull distribution
- Gamma distribution
Implementing support for another distribution
In order to use the fitting framework for a distribution MyDist, one needs to implement the following four methods.
fit(::Type{MyDist}, m::AbstractMoments)
fit_mean_quantile(::Type{MyDist}, mean, qp::QuantilePoint)
fit_mode_quantile(::Type{MyDist}, mode, qp::QuantilePoint)
fit(::Type{MyDist}, lower::QuantilePoint, upper::QuantilePoint)The default method for fit with stats = :median already works based on the methods for two quantile points. If the general method on two quantile points cannot be specified, one can alternatively implement method:
fit_median_quantile(::Type{MyDist}, median, qp::QuantilePoint)