API

Types

WeightedData.WeightedValueType
WeightedValue{T<:Real} <: Number

Value 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 < 0 throws an error;
  • non-finite input value is converted to (0, 0).

Fields

  • value::T: The value
  • precision::T: The precision (must be non-negative)

Example

x = WeightedValue(1.0, 0.5)  # value 1.0 with precision 0.5
source
WeightedData.WeightedArrayType
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}.

  • T is the numeric storage type used for both values and precisions.
  • Precision is interpreted as inverse variance.
  • Internally this is an alias of a ZippedArray over 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.

source
WeightedData.WeightedArrayMethod
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.
  • values and precision may have different real element types (for example Int, Float32, Float64, or Union{Missing,<:Real}), and are promoted to a common real type T.
  • Missing entries in either input are sanitized to value=0 and precision=0.
  • Non-finite precision (NaN, Inf, -Inf) is sanitized to precision=0.
  • Non-finite values are sanitized to value=0.
  • If values and precision have different element types, or if sanitization is needed (missing/non-finite entries), the constructor may allocate new arrays of type Array{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 Adapt to adapt the resulting WeightedArray (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)
source

Exported Functions

StatsAPI.loglikelihoodFunction
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).

source
loglikelihood(loss::LossFunction, data::AbstractArray{<:WeightedValue}, model::AbstractArray)

Compute robust loss for arrays of weighted observations. data and model must have the same shape.

source
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.

source
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.

source
Statistics.meanFunction
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.

source
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.
source
mean(iterable; dims=:)

Compute a precision-weighted mean from an iterable of WeightedValue.

  • With dims = : (default), returns a scalar WeightedValue.
  • With dims set to one or more dimensions, returns a WeightedArray.

iterable must be non-empty, have WeightedValue elements, and expose an element type.

source
Statistics.varFunction
var(x::WeightedValue)

Return the variance of x, defined as the inverse precision:

var(x) = 1 / get_precision(x).

source
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).

source
Statistics.stdFunction
std(x::WeightedValue)

Return the standard deviation of x, defined as:

std(x) = sqrt(var(x)).

source
std(x::AbstractArray{<:WeightedValue})

Return the element-wise standard deviation array of x, defined as:

std(x) = sqrt.(var(x)).

source

Public Functions

WeightedData.get_valueFunction
get_value(x::WeightedValue)

Return the numeric value stored in x.

source
get_value(x::AbstractArray{<:WeightedValue})

Extract an array with the values of each WeightedValue element.

source
WeightedData.get_precisionFunction
get_precision(x::WeightedValue)

Return the precision (inverse variance) stored in x.

source
get_precision(x::AbstractArray{<:WeightedValue})

Extract an array with the precisions of each WeightedValue element.

source
WeightedData.get_weightsFunction
get_weights(loss::LossFunction, data::WeightedValue, model::Number)

Compute IRLS weight for a single weighted observation.

source
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.

source
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.

source
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.

source
WeightedData.ScaledL2LossType
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.

source

Extension Functions

Measurements

WeightedData.WeightedValueMethod
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)
source
Measurements.measurementMethod
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.1
source

OnlineSampleStatistics

WeightedData.WeightedArrayMethod
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.

source
WeightedData.WeightedValueMethod
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.

source

RobustModels

RobustModels.workingweightsMethod
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 scheme
  • data::AbstractArray{<:WeightedValue}: Array of weighted data points
  • model::AbstractArray: Model parameters or predictions

Returns

Array of working weights corresponding to the input data

See Also

source
StatsAPI.loglikelihoodMethod
loglikelihood(loss::LossFunction, data::AbstractArray{<:WeightedValue}, model::AbstractArray)

Compute robust loss for arrays of weighted observations. data and model must have the same shape.

source
StatsAPI.loglikelihoodMethod
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).

source
WeightedData.get_weightsMethod
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.

source
WeightedData.get_weightsMethod
get_weights(loss::LossFunction, data::WeightedValue, model::Number)

Compute IRLS weight for a single weighted observation.

source

Uncertain

Core.TypeMethod
T(x::WeightedValue) where T <: Uncertain.Value

Convert 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)
source
WeightedData.WeightedValueMethod
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)
source