Internal Documentation
Documentation for ModifiedDistributions's internal interface.
Contents
Index
Base.randDistributions.ccdfDistributions.cdfDistributions.logccdfDistributions.logcdfDistributions.logpdfDistributions.pdfModifiedDistributions.combine_weightsStatistics.meanStatistics.quantileStatistics.varStatsAPI.loglikelihood
Internal API
Distributions.ccdf Function
ccdf(d::UnivariateDistribution, x::Real)The complementary cumulative function evaluated at x, i.e. 1 - cdf(d, x).
ccdf(d::ModifiedDistributions.Affine, y::Real) -> AnyCompute the complementary cumulative distribution function via change of variables (avoids the 1 - cdf fallback, keeping precision in the upper tail).
ccdf(
d::ModifiedDistributions.Affine,
y::AbstractVector{<:Real}
) -> AnyCompute the complementary cumulative distribution function for a vector of observations, per point (a single batched inner call when available).
See also: ccdf
ccdf(d::ModifiedDistributions.Weighted, x::Real) -> AnyCompute the complementary cumulative distribution function (delegates to underlying distribution).
See also: cdf
ccdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Union{Function, Real}},
x::Real
) -> AnyCompute the complementary cumulative distribution function.
See also: logccdf
ccdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Discrete}, <:AbstractVector},
x::Real
) -> AnyCompute the complementary cumulative distribution on the discrete per-bin path.
See also: cdf
Distributions.cdf Function
cdf(d::UnivariateDistribution, x::Real)Evaluate the cumulative probability at x.
See also ccdf, logcdf, and logccdf.
cdf(d::ModifiedDistributions.Affine, y::Real) -> AnyCompute the cumulative distribution function.
sourcecdf(
d::ModifiedDistributions.Affine,
y::AbstractVector{<:Real}
) -> AnyCompute the cumulative distribution function for a vector of observations, per point (a single batched inner call when available).
See also: cdf
cdf(d::ModifiedDistributions.Weighted, x::Real) -> AnyCompute the cumulative distribution function (delegates to underlying distribution).
See also: logcdf
cdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Union{Function, Real}},
x::Real
) -> AnyCompute the cumulative distribution function.
sourcecdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Discrete}, <:AbstractVector},
x::Real
) -> AnyCompute the cumulative distribution function on the discrete per-bin path.
See also: logcdf
ModifiedDistributions.combine_weights Function
combine_weights(_::Missing, _::Missing) -> MissingCombine constructor weight with observation weight using dispatch-based rules.
Weight combination rules:
missing, missing → missing(both missing means no weight)w1, missing → w1(use constructor weight)missing, w2 → w2(use observation weight)w1, w2 → w1 * w2(multiply weights)
Vector Extensions
For Product distributions, additional methods handle vectorised weight combinations:
Vector, Vector → combine_weights.(vector1, vector2)(element-wise combination)Vector, missing → Vector(keep constructor weights)Vector, scalar → [combine_weights(w, scalar) for w in Vector](broadcast scalar)
Distributions.logccdf Function
logccdf(d::UnivariateDistribution, x::Real)The logarithm of the complementary cumulative function values evaluated at x, i.e. log(ccdf(x)).
logccdf(d::ModifiedDistributions.Affine, y::Real) -> AnyCompute the log complementary cumulative distribution function.
See also: ccdf
logccdf(
d::ModifiedDistributions.Affine,
y::AbstractVector{<:Real}
) -> AnyCompute the log complementary cumulative distribution function for a vector of observations, per point (a single batched inner call when available).
See also: logccdf
logccdf(d::ModifiedDistributions.Weighted, x::Real) -> AnyCompute the log complementary cumulative distribution function (delegates to underlying distribution).
See also: logcdf
logccdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real, ModifiedDistributions.HazardLink{typeof(log), typeof(exp)}},
x::Real
) -> AnyCompute the log survival function on the proportional-hazards path.
sourcelogccdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real, ModifiedDistributions.HazardLink{typeof(identity), typeof(identity)}},
x::Real
) -> AnyCompute the log survival function on the additive-hazards path.
sourcelogccdf(
d::Union{ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Function}, ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real}},
x::Real
) -> AnyCompute the log survival function on the numeric cumulative-hazard path.
sourcelogccdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Discrete}, <:AbstractVector},
x::Real
)Compute the log complementary cumulative distribution on the discrete per-bin path.
See also: ccdf
Distributions.logcdf Function
logcdf(d::UnivariateDistribution, x::Real)The logarithm of the cumulative function value(s) evaluated at x, i.e. log(cdf(x)).
logcdf(d::ModifiedDistributions.Affine, y::Real) -> AnyCompute the log cumulative distribution function.
See also: cdf
logcdf(
d::ModifiedDistributions.Affine,
y::AbstractVector{<:Real}
) -> AnyCompute the log cumulative distribution function for a vector of observations, per point (a single batched inner call when available).
See also: logcdf
logcdf(d::ModifiedDistributions.Weighted, x::Real) -> AnyCompute the log cumulative distribution function (delegates to underlying distribution).
See also: cdf
logcdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Union{Function, Real}},
x::Real
) -> AnyCompute the log cumulative distribution function.
See also: cdf
logcdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Discrete}, <:AbstractVector},
x::Real
) -> AnyCompute the log cumulative distribution on the discrete per-bin path.
See also: cdf
StatsAPI.loglikelihood Function
loglikelihood(model::StatisticalModel)
loglikelihood(model::StatisticalModel, observation)Return the log-likelihood of the model.
With an observation argument, return the contribution of observation to the log-likelihood of model.
If observation is a Colon, return a vector of each observation's contribution to the log-likelihood of the model. In other words, this is the vector of the pointwise log-likelihood contributions.
In general, sum(loglikehood(model, :)) == loglikelihood(model).
Distributions.logpdf Function
logpdf(d::Distribution{ArrayLikeVariate{N}}, x::AbstractArray{<:Real,N}) where {N}Evaluate the logarithm of the probability density function of d at x.
This function checks if the size of x is compatible with distribution d. This check can be disabled by using @inbounds.
Implementation
Instead of logpdf one should implement _logpdf(d, x) which does not have to check the size of x.
See also: pdf, gradlogpdf.
logpdf(d::Distribution{ArrayLikeVariate{N}}, x) where {N}Evaluate the logarithm of the probability density function of d at every element in a collection x.
This function checks for every element of x if its size is compatible with distribution d. This check can be disabled by using @inbounds.
Here, x can be
an array of dimension
> Nwithsize(x)[1:N] == size(d), oran array of arrays
xiof dimensionNwithsize(xi) == size(d).
logpdf(d::UnivariateDistribution, x::Real)Evaluate the logarithm of probability density (mass) at x.
See also: pdf.
logpdf(d::Union{UnivariateMixture, MultivariateMixture}, x)Evaluate the logarithm of the (mixed) probability density function over x. Here, x can be a single sample or an array of multiple samples.
logpdf(d::ModifiedDistributions.Affine, y::Real) -> AnyCompute the log probability density function via change of variables. For a continuous inner distribution this includes the log-Jacobian -log(scale); for a discrete inner distribution the mass transforms without it.
logpdf(
d::ModifiedDistributions.Affine,
y::AbstractVector{<:Real}
) -> AnyCompute the log probability density for a vector of observations, per point.
The whole transformed batch reaches the inner distribution in one call when it provides a batched logpdf, then the log-Jacobian -log(scale) applies elementwise (continuous inner distributions only). A vector observation on a modifier is per-point (a vector result), unlike the Product{<:Weighted} joint-scalar convention.
See also: pdf
logpdf(d::ModifiedDistributions.Weighted, x::Real) -> AnyReturn the weighted log-probability for scalar observations.
See also: pdf
logpdf(
d::ModifiedDistributions.Weighted,
x::AbstractVector{<:Real}
) -> AnyReturn the weighted log-probability for a vector of observations, per point.
The whole batch reaches the base distribution in one logpdf call when it provides a batched implementation, then the weight applies elementwise with the scalar missing/zero-weight sentinel semantics (-Inf per point, AD-safe promoted eltype). A vector observation on a single Weighted is per-point (a vector result), unlike the Product{<:Weighted} joint-scalar convention.
See also: pdf
logpdf(
d::ModifiedDistributions.Weighted,
obs::NamedTuple{(:value, :weight)}
) -> AnyReturn the weighted log-probability for joint observations as NamedTuple.
Combines constructor weight with observation weight via multiplication. Expected format: (value = x, weight = w).
See also: pdf
logpdf(
d::Distributions.Product{<:Distributions.ValueSupport, <:ModifiedDistributions.Weighted, <:AbstractVector{<:ModifiedDistributions.Weighted}},
obs::NamedTuple{(:values, :weights)}
) -> AnyEfficient vectorised log-probability computation for Product{<:ValueSupport, <:Weighted} with joint observations.
Handles joint observations and weight stacking. Expected format: (values = [...], weights = [...]).
See also: logpdf
logpdf(
d::Distributions.Product{<:Distributions.ValueSupport, <:ModifiedDistributions.Weighted, <:AbstractVector{<:ModifiedDistributions.Weighted}},
x::AbstractVector{<:Real}
) -> AnyEfficient vectorised log-probability computation for Product{<:ValueSupport, <:Weighted} with vector observations.
See also: logpdf
logpdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real, ModifiedDistributions.HazardLink{typeof(log), typeof(exp)}},
x::Real
) -> AnyCompute the log probability density on the proportional-hazards path.
sourcelogpdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real, ModifiedDistributions.HazardLink{typeof(identity), typeof(identity)}},
x::Real
) -> AnyCompute the log probability density on the additive-hazards path.
sourcelogpdf(
d::Union{ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Function}, ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real}},
x::Real
) -> AnyCompute the log probability density on the numeric cumulative-hazard path.
sourcelogpdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Discrete}, <:AbstractVector},
x::Real
)Compute the log probability mass on the discrete per-bin path.
See also: pdf
Statistics.mean Method
mean(d::ModifiedDistributions.Affine) -> AnyCompute the mean via the affine transform of the inner mean.
See also: var
Distributions.pdf Function
pdf(d::Distribution{ArrayLikeVariate{N}}, x::AbstractArray{<:Real,N}) where {N}Evaluate the probability density function of d at x.
This function checks if the size of x is compatible with distribution d. This check can be disabled by using @inbounds.
Implementation
Instead of pdf one should implement _pdf(d, x) which does not have to check the size of x. However, since the default definition of pdf(d, x) falls back to logpdf(d, x) usually it is sufficient to implement logpdf.
See also: logpdf.
pdf(d::Distribution{ArrayLikeVariate{N}}, x) where {N}Evaluate the probability density function of d at every element in a collection x.
This function checks for every element of x if its size is compatible with distribution d. This check can be disabled by using @inbounds.
Here, x can be
an array of dimension
> Nwithsize(x)[1:N] == size(d), oran array of arrays
xiof dimensionNwithsize(xi) == size(d).
pdf(d::UnivariateDistribution, x::Real)Evaluate the probability density (mass) at x.
See also: logpdf.
pdf(d::Union{UnivariateMixture, MultivariateMixture}, x)Evaluate the (mixed) probability density function over x. Here, x can be a single sample or an array of multiple samples.
pdf(d::ModifiedDistributions.Affine, y::Real) -> AnyCompute the probability density function.
See also: logpdf
pdf(
d::ModifiedDistributions.Affine,
y::AbstractVector{<:Real}
) -> AnyCompute the probability density for a vector of observations, per point (one batched inner call through the vector logpdf).
See also: logpdf
pdf(d::ModifiedDistributions.Weighted, x::Real) -> AnyReturn the probability density from the underlying distribution (unweighted).
See also: logpdf
pdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Union{Function, Real}},
x::Real
) -> AnyCompute the probability density function.
See also: logpdf
pdf(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Discrete}, <:AbstractVector},
x::Real
) -> AnyCompute the probability mass for the bin containing x on the discrete per-bin reporting-hazard path.
Statistics.quantile Function
quantile(d::UnivariateDistribution, q::Real)Evaluate the (generalized) inverse cumulative distribution function at q.
For a given 0 ≤ q ≤ 1, quantile(d, q) is the smallest value x in the support of d for which cdf(d, x) ≥ q.
See also: cquantile, invlogcdf, and invlogccdf.
quantile(d::ModifiedDistributions.Affine, p::Real) -> AnyCompute the quantile function (inverse CDF).
See also: cdf
quantile(d::ModifiedDistributions.Weighted, p::Real) -> AnyCompute the quantile function (delegates to underlying distribution).
See also: cdf
quantile(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real, ModifiedDistributions.HazardLink{typeof(log), typeof(exp)}},
p::Real
) -> AnyCompute the quantile by closed-form inversion of the modified survival.
See also: cdf
quantile(
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real, ModifiedDistributions.HazardLink{typeof(identity), typeof(identity)}},
p::Real
) -> AnyCompute the quantile by monotone bisection of the modified CDF.
A non-negative additive effect only speeds events up, so the modified quantile sits between the support minimum and the base quantile. A negative additive effect widens the law and can leave it defective (its cdf converges below one); when the requested p exceeds the total mass the quantile is undefined and this throws an ArgumentError rather than returning a garbage bracket.
See also: cdf
quantile(itr, p; sorted=false, alpha::Real=1.0, beta::Real=alpha)Compute the quantile(s) of a collection itr at a specified probability or vector or tuple of probabilities p on the interval [0,1]. The keyword argument sorted indicates whether itr can be assumed to be sorted.
Samples quantile are defined by Q(p) = (1-γ)*x[j] + γ*x[j+1], where x[j] is the j-th order statistic of itr, j = floor(n*p + m), m = alpha + p*(1 - alpha - beta) and γ = n*p + m - j.
By default (alpha = beta = 1), quantiles are computed via linear interpolation between the points ((k-1)/(n-1), x[k]), for k = 1:n where n = length(itr). This corresponds to Definition 7 of Hyndman and Fan (1996), and is the same as the R and NumPy default.
The keyword arguments alpha and beta correspond to the same parameters in Hyndman and Fan, setting them to different values allows to calculate quantiles with any of the methods 4-9 defined in this paper:
Def. 4:
alpha=0,beta=1Def. 5:
alpha=0.5,beta=0.5(MATLAB default)Def. 6:
alpha=0,beta=0(ExcelPERCENTILE.EXC, Python default, Stataaltdef)Def. 7:
alpha=1,beta=1(Julia, R and NumPy default, ExcelPERCENTILEandPERCENTILE.INC, Python'inclusive')Def. 8:
alpha=1/3,beta=1/3Def. 9:
alpha=3/8,beta=3/8
Note
An ArgumentError is thrown if v contains NaN or missing values. Use the skipmissing function to omit missing entries and compute the quantiles of non-missing values.
References
Hyndman, R.J and Fan, Y. (1996) "Sample Quantiles in Statistical Packages", The American Statistician, Vol. 50, No. 4, pp. 361-365
Quantile on Wikipedia details the different quantile definitions
Examples
julia> using Statistics
julia> quantile(0:20, 0.5)
10.0
julia> quantile(0:20, [0.1, 0.5, 0.9])
3-element Vector{Float64}:
2.0
10.0
18.000000000000004
julia> quantile(skipmissing([1, 10, missing]), 0.5)
5.5Base.rand Function
rand([rng::AbstractRNG,] s::Sampleable)Generate one sample for s.
rand([rng::AbstractRNG,] s::Sampleable, n::Int)Generate n samples from s. The form of the returned object depends on the variate form of s:
When
sis univariate, it returns a vector of lengthn.When
sis multivariate, it returns a matrix withncolumns.When
sis matrix-variate, it returns an array, where each element is a sample matrix. rand([rng::AbstractRNG,] s::Sampleable, dim1::Int, dim2::Int...) rand([rng::AbstractRNG,] s::Sampleable, dims::Dims)
Generate an array of samples from s whose shape is determined by the given dimensions.
rand(rng::AbstractRNG, d::UnivariateDistribution)Generate a scalar sample from d. The general fallback is quantile(d, rand()).
rand(::AbstractRNG, ::Distributions.AbstractMvNormal)Sample a random vector from the provided multi-variate normal distribution.
sourcerand(::AbstractRNG, ::Sampleable)Samples from the sampler and returns the result.
sourcerand(d::Union{UnivariateMixture, MultivariateMixture})Draw a sample from the mixture model d.
rand(d::Union{UnivariateMixture, MultivariateMixture}, n)Draw n samples from d.
rand(
rng::Random.AbstractRNG,
d::ModifiedDistributions.Affine
) -> AnyGenerate a random sample by transforming an inner draw.
See also: quantile
rand(
rng::Random.AbstractRNG,
d::ModifiedDistributions.Weighted
) -> AnyGenerate a random sample (delegates to underlying distribution).
See also: quantile
rand(
rng::Random.AbstractRNG,
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Continuous}, <:Real, ModifiedDistributions.HazardLink{typeof(log), typeof(exp)}}
) -> AnyGenerate a random sample by closed-form inversion of the modified survival.
See also: quantile
rand(
rng::Random.AbstractRNG,
d::ModifiedDistributions.Modified{<:Distributions.Distribution{Distributions.Univariate, Distributions.Discrete}, <:AbstractVector}
)Generate a random sample by inversion of the reconstructed PMF.
See also: pdf
Statistics.var Method
var(d::ModifiedDistributions.Affine) -> AnyCompute the variance via the affine transform of the inner variance.
See also: mean