Table of Contents

Class L2CostFunction

Namespace
SignalSharp.CostFunctions.Cost
Assembly
SignalSharp.dll

Represents a cost function using the L2 norm (Euclidean distance) for the Piecewise Linear Trend Change (PELT) method.

public class L2CostFunction : CostFunctionBase, IPELTCostFunction
Inheritance
L2CostFunction
Implements
Inherited Members

Remarks

The L2 norm, also known as the Euclidean distance, is a measure of the straight-line distance between points in a multi-dimensional space. It is calculated as the square root of the sum of the squared differences between the coordinates of the points.

In the context of the Piecewise Linear Trend Change (PELT) method, the L2 cost function is used to compute the cost of segmenting a time series or sequential data into different segments where the statistical properties change. The L2 norm is sensitive to outliers, making it a good choice when the data is relatively clean and normally distributed.

Consider using the L2 cost function in scenarios where:

  • The data is relatively clean and free of outliers.
  • You need a precise measure of segment dissimilarity.
  • The underlying data distribution is approximately normal.

Methods

ComputeCost(int?, int?)

Computes the cost for a segment of the data using the L2 norm (sum of squared errors). Cost(start, end) = Sum_{i=start}^{end-1} (signal[i] - mean(segment))^2 This is calculated efficiently in O(1) time using precomputed prefix sums as: Sum(signal[i]^2) - (Sum(signal[i])^2 / segmentLength) for the segment [start, end).

public override double ComputeCost(int? start = null, int? end = null)

Parameters

start int?

The start index of the segment (inclusive). If null, defaults to 0.

end int?

The end index of the segment (exclusive). If null, defaults to the length of the data.

Returns

double

The computed cost for the segment.

Remarks

This method must be called after the Fit(double[,]) method has been used to initialize the prefix sums.

The calculation relies on the identity: Sum((x_i - mu)^2) = Sum(x_i^2) - (Sum(x_i)^2 / n).

For example, given a fitted L2CostFunction instance:
var l2Cost = new L2CostFunction().Fit(data);
double cost = l2Cost.ComputeCost(0, 10); // Cost for segment from index 0 up to (but not including) 10

Exceptions

UninitializedDataException

Thrown when prefix sums are not initialized (Fit not called).

ArgumentOutOfRangeException

Thrown when the segment indices are out of bounds.

SegmentLengthException

Thrown when the segment length is less than 1.

Fit(double[,])

Fits the cost function to the provided data.

public override IPELTCostFunction Fit(double[,] signalMatrix)

Parameters

signalMatrix double[,]

The data array to fit.

Returns

IPELTCostFunction

The fitted L2CostFunction instance.

Remarks

This method performs O(N*D) computation to calculate prefix sums of the signal and its squares, enabling O(1) cost calculation per segment later via ComputeCost(int?, int?).

double[,] data = { { 1.0, 2.0, 3.0, 4.0 } };
var l2Cost = new L2CostFunction().Fit(data);

This initializes the cost function with the provided data, making it ready for segment cost computation.