Class L1CostFunction
- Namespace
- SignalSharp.CostFunctions.Cost
- Assembly
- SignalSharp.dll
Represents a cost function using the L1 norm for the Piecewise Linear Trend Change (PELT) method.
public class L1CostFunction : CostFunctionBase, IPELTCostFunction
- Inheritance
-
L1CostFunction
- Implements
- Inherited Members
Remarks
The L1 norm, also known as the Manhattan distance or absolute deviation, is a measure of distance between points in a multi-dimensional space. It is calculated as the sum of the absolute differences between the coordinates of the points.
In the context of the Piecewise Linear Trend Change (PELT) method, the L1 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 L1 norm is particularly robust to outliers, making it a good choice when the data contains anomalies or non-Gaussian noise.
Consider using the L1 cost function in scenarios where:
- The data contains outliers or non-Gaussian noise.
- You need a robust measure of segment dissimilarity.
- Traditional L2 norm (Euclidean distance) based models are too sensitive to outliers.
Methods
ComputeCost(int?, int?)
Computes the cost for a segment of the data using the L1 norm.
public override double ComputeCost(int? start = null, int? end = null)
Parameters
start
int?The start index of the segment. If null, defaults to 0.
end
int?The end index of the segment. If null, defaults to the length of the data.
Returns
- double
The computed cost for the segment.
Remarks
The cost function measures the sum of absolute deviations from the median of the segment, which is useful for detecting change points in time series analysis.
This method must be called after the Fit(double[,]) method has been used to initialize the data.
var l1Cost = new L1CostFunction().Fit(data);
double cost = l1Cost.ComputeCost(0, 10);
This computes the cost for the segment of the data from index 0 to index 10.
Exceptions
- UninitializedDataException
Thrown when data is not initialized.
- 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 L1CostFunction instance.
Remarks
This method initializes the internal data needed to compute the cost for segments of the data.
double[,] data = { { 1.0, 2.0, 3.0, 4.0 } };
var l1Cost = new L1CostFunction().Fit(data);
This initializes the cost function with the provided data, making it ready for segment cost computation.