API
Types
WeightedData.WeightedValue — Type
WeightedValue{T<:Real} <: NumberValue with associated precision (inverse variance).
WeightedValue stores a numeric value and a non-negative precision. It is the scalar building block used throughout WeightedData.jl.
Constructor behavior:
precision < 0throws an error;- non-finite input
valueis converted to(0, 0).
Fields
value::T: The valueprecision::T: The precision (must be non-negative)
Example
x = WeightedValue(1.0, 0.5) # value 1.0 with precision 0.5WeightedData.WeightedArray — Type
WeightedArray{T,N}N-dimensional array of WeightedValue{T}.
WeightedArray stores observations and precisions side-by-side and behaves like an AbstractArray{WeightedValue{T},N}.
Tis the numeric storage type used for both values and precisions.- Precision is interpreted as inverse variance.
- Internally this is an alias of a
ZippedArrayover two arrays with the same shape.
Typical ways to build a WeightedArray:
WeightedArray(values, precisions)WeightedArray(array_of_weighted_values)WeightedArray(existing_weighted_array)(identity)
See the constructor docstring below for conversion and sanitization rules.
WeightedData.WeightedArray — Method
WeightedArray(values::AbstractArray, precision::AbstractArray)Build a weighted array from value and precision arrays of the same shape.
Rules:
size(values) == size(precision)is required.valuesandprecisionmay have different real element types (for exampleInt,Float32,Float64, orUnion{Missing,<:Real}), and are promoted to a common real typeT.- Missing entries in either input are sanitized to
value=0andprecision=0. - Non-finite precision (
NaN,Inf,-Inf) is sanitized toprecision=0. - Non-finite values are sanitized to
value=0. - If
valuesandprecisionhave different element types, or if sanitization is needed (missing/non-finite entries), the constructor may allocate new arrays of typeArray{T,N}. - In those cases, the result is not guaranteed to be a simple wrapper around the original array storage (for example custom backends such as fixed-size or GPU arrays).
- To maximize storage preservation, provide inputs with the same concrete real element type and no missing/non-finite entries.
- If you need to target a specific backend after construction, use
Adaptto adapt the resultingWeightedArray(e.g.,Adapt.adapt(FixedSizeArrayDefault, A)).
Additional constructors:
WeightedArray(x::AbstractArray{<:WeightedValue})WeightedArray(x::WeightedArray)WeightedArray(x::AbstractArray{Missing})
Example
values = [1.0, missing, 3.0]
prec = [2, 4f0, NaN]
A = WeightedArray(values, prec)Exported Functions
StatsAPI.loglikelihood — Function
loglikelihood(loss::LossFunction, data::WeightedValue, model::Number)Compute robust loss contribution for a single weighted observation.
Residual is defined as: r = sqrt(get_precision(data)) * (model - get_value(data)) and the returned value is rho(loss, r).
loglikelihood(loss::LossFunction, data::AbstractArray{<:WeightedValue}, model::AbstractArray)Compute robust loss for arrays of weighted observations. data and model must have the same shape.
loglikelihood(data::WeightedValue, model; loss=L2Loss())Calculate the negative log-likelihood for a weighted data point.
Arguments
data: The observed value in the weighted point.model: The predicted value from the model.loss: The loss function to use (default: L2 loss).
Returns
The neg log likelihood value.
Deprecation note
likelihood(data, model; loss=...) is deprecated and forwards to this method.
loglikelihood(data::AbstractArray{<:WeightedValue}, model::AbstractArray; loss=L2Loss())Calculate the negative log-likelihood for an array of weighted data points.
Arguments
data: The observed values in the weighted points.model: The predicted values from the model.loss: The loss function to use (default: L2 loss).
Returns
The neg log likelihood value.
Deprecation note
likelihood(data, model; loss=...) is deprecated and forwards to this method.
Statistics.mean — Function
mean(A::WeightedValue, B::Vararg{WeightedValue})Compute the precision-weighted mean of one or more scalar weighted values.
Equivalent to reducing the inputs with the same precision-weighted averaging rule used for tuples/iterables.
mean(A::AbstractArray{<:WeightedValue, N}, B::AbstractArray{<:WeightedValue, N}, C...) where {N}Element-wise precision-weighted mean of weighted arrays with matching shape.
mean(A, B, ...)returns an element-wise weighted mean.
mean(iterable; dims=:)Compute a precision-weighted mean from an iterable of WeightedValue.
- With
dims = :(default), returns a scalarWeightedValue. - With
dimsset to one or more dimensions, returns aWeightedArray.
iterable must be non-empty, have WeightedValue elements, and expose an element type.
Statistics.var — Function
var(x::WeightedValue)Return the variance of x, defined as the inverse precision:
var(x) = 1 / get_precision(x).
var(x::AbstractArray{<:WeightedValue})Return the element-wise variance array of x, defined as the inverse precision at each position:
var(x) = 1 ./ get_precision(x).
Statistics.std — Function
std(x::WeightedValue)Return the standard deviation of x, defined as:
std(x) = sqrt(var(x)).
std(x::AbstractArray{<:WeightedValue})Return the element-wise standard deviation array of x, defined as:
std(x) = sqrt.(var(x)).
Public Functions
WeightedData.get_value — Function
get_value(x::WeightedValue)Return the numeric value stored in x.
get_value(x::AbstractArray{<:WeightedValue})Extract an array with the values of each WeightedValue element.
WeightedData.get_precision — Function
get_precision(x::WeightedValue)Return the precision (inverse variance) stored in x.
get_precision(x::AbstractArray{<:WeightedValue})Extract an array with the precisions of each WeightedValue element.
WeightedData.get_weights — Function
get_weights(loss::LossFunction, data::WeightedValue, model::Number)Compute IRLS weight for a single weighted observation.
get_weights(loss::LossFunction, data::AbstractArray{<:WeightedValue}, model::AbstractArray)Compute IRLS weights element-wise for arrays of weighted observations. data and model must have the same shape.
get_weights(data::WeightedValue, model; loss=L2Loss())Calculate the equivalent weight for a given the loss function for IRLS.
Arguments
data: The observed value in the weighted point.model: The predicted value from the model.loss: The loss function to use (default: L2 loss).
Returns
The equivalent weight.
WeightedData.filterbaddata! — Function
filterbaddata!(data::WeightedArray, goodmask::AbstractArray{Bool})Mask bad entries in-place by setting precision to zero where goodmask is false. Entries where goodmask is true keep their original precision. Values are left unchanged. goodmask must have the same shape as data.
Returns data.
WeightedData.ScaledL2Loss — Type
ScaledL2Loss(; dims=1, nonnegative=false)Loss object for a scaled least-squares likelihood where the model is multiplied by a per-slice scaling factor estimated along dimension dims.
If nonnegative=true, negative or non-finite scale factors are clamped to zero.
Extension Functions
Measurements
WeightedData.WeightedValue — Method
WeightedValue(x::Measurements.Measurement)Convert a Measurements.Measurement to WeightedValue.
Arguments
x::Measurements.Measurement: value with uncertainty
Returns
A WeightedValue with:
- value =
Measurements.value(x) - precision =
Measurements.uncertainty(x)^(-2)
Notes
Precision is the inverse variance.
Example
using Measurements, WeightedData
m = 1.0 ± 0.1 # Measurement with value 1.0 and uncertainty 0.1
w = WeightedValue(m) # WeightedValue(1.0, 100.0)Measurements.measurement — Method
Measurement(x::WeightedValue)Convert WeightedValue to Measurements.Measurement.
Arguments
x::WeightedValue: A WeightedValue with value and precision
Returns
A Measurements.Measurement with:
- value =
get_value(x) - uncertainty =
1 / sqrt(get_precision(x))
Notes
Uncertainty is the standard deviation.
Example
using Measurements, WeightedData
w = WeightedValue(1.0, 100.0) # value = 1.0, precision = 100.0
m = Measurement(w) # 1.0 ± 0.1OnlineSampleStatistics
WeightedData.WeightedArray — Method
WeightedArray(x::IndependentStatistic{T, N, K, W}) where {T, N, K, W}Convert an independent online statistic array to a WeightedArray.
The returned values are mean(x) and element-wise precisions inv.(var(x)). At least two moments (K ≥ 2) are required so that the variance is available.
WeightedData.WeightedValue — Method
WeightedValue(x::UnivariateStatistic{T, K}) where {T, K}Convert a univariate online statistic to a WeightedValue.
The returned value is mean(x) and the returned precision is inv(var(x)). At least two moments (K ≥ 2) are required so that the variance is available.
RobustModels
RobustModels.workingweights — Method
workingweights(loss::LossFunction, data::AbstractArray{<:WeightedValue}, model::AbstractArray)Compute the working weights for a given loss function, weighted data, and model.
This function calculates the weights used in iterative weighted least squares algorithms by delegating to get_weights with the specified loss function, data, and model parameters.
Arguments
loss::LossFunction: The loss function defining the weighting schemedata::AbstractArray{<:WeightedValue}: Array of weighted data pointsmodel::AbstractArray: Model parameters or predictions
Returns
Array of working weights corresponding to the input data
See Also
get_weightsRobustModels.LossFunctionWeightedValue
StatsAPI.loglikelihood — Method
loglikelihood(loss::LossFunction, data::AbstractArray{<:WeightedValue}, model::AbstractArray)Compute robust loss for arrays of weighted observations. data and model must have the same shape.
StatsAPI.loglikelihood — Method
loglikelihood(loss::LossFunction, data::WeightedValue, model::Number)Compute robust loss contribution for a single weighted observation.
Residual is defined as: r = sqrt(get_precision(data)) * (model - get_value(data)) and the returned value is rho(loss, r).
WeightedData.get_weights — Method
get_weights(loss::LossFunction, data::AbstractArray{<:WeightedValue}, model::AbstractArray)Compute IRLS weights element-wise for arrays of weighted observations. data and model must have the same shape.
WeightedData.get_weights — Method
get_weights(loss::LossFunction, data::WeightedValue, model::Number)Compute IRLS weight for a single weighted observation.
Uncertain
Core.Type — Method
T(x::WeightedValue) where T <: Uncertain.ValueConvert WeightedValue to an Uncertain.Value subtype.
Arguments
x::WeightedValue: A WeightedValue with value and precision
Returns
An uncertain value with:
- value =
value(x) - value =
get_value(x) - uncertainty =
1 / sqrt(get_precision(x))
Notes
Uncertainty is the standard deviation.
Example
using Uncertain, WeightedData
w = WeightedValue(1.0, 100.0) # value = 1.0, precision = 100.0
u = Uncertain.Value(w) # Uncertain.Value(1.0, 0.1)WeightedData.WeightedValue — Method
WeightedValue(x::Uncertain.Value)Convert an Uncertain.Value to WeightedValue.
Arguments
x::Uncertain.Value: value with uncertainty
Returns
A WeightedValue with:
- value =
Uncertain.value(x) - precision =
Uncertain.uncertainty(x)^(-2)
Notes
Precision is the inverse variance.
Example
using Uncertain, WeightedData
u = Uncertain.Value(1.0, 0.1) # value = 1.0, uncertainty = 0.1
w = WeightedValue(u) # WeightedValue(1.0, 100.0)