Table of Contents

Class GaussianLikelihoodCostFunction

Namespace
SignalSharp.CostFunctions.Cost
Assembly
SignalSharp.dll

Represents a cost function based on the Gaussian negative log-likelihood for the PELT algorithm. This cost function is sensitive to changes in both the mean and the variance of the signal.

public class GaussianLikelihoodCostFunction : CostFunctionBase, ILikelihoodCostFunction, IPELTCostFunction
Inheritance
GaussianLikelihoodCostFunction
Implements
Inherited Members

Remarks

This cost function assumes that the data within each segment follows a Gaussian (normal) distribution, independently for each dimension. It calculates the cost based on the negative log-likelihood of the segment data given its estimated mean and variance (Maximum Likelihood Estimates - MLE).

The likelihood metric (proportional to -2 * log-likelihood) for a segment [start, end) of length n = end - start is dominated by the term Sum_dimensions [ n * log(σ²_hat_dim) ], where σ²_hat_dim is the MLE of the variance for that segment and dimension. This makes the function sensitive to shifts in both central tendency (mean) and dispersion (variance). The ComputeCost(int?, int?) and ComputeLikelihoodMetric(int, int) methods both return this metric value.

This cost/metric is calculated efficiently using precomputed prefix sums of the signal and its squares, allowing O(D) calculation per segment after an O(N*D) precomputation step during Fit(double[,]).

Consider using the Gaussian Likelihood cost function when:

  • You expect changes in both the mean and variance of your signal.
  • The data within segments can be reasonably approximated by a normal distribution.
  • You need a statistically principled way to evaluate segment homogeneity under Gaussian assumptions.

Note: This implementation uses the MLE for variance (dividing sum of squared deviations by n). If the segment variance is zero or numerically very close to zero (e.g., all points in the segment are identical), the logarithm would be undefined or negative infinity. To handle this, a small minimum variance floor based on VarianceEpsilon is used to ensure numerical stability. This effectively assigns a very high cost/metric to zero-variance segments, preventing issues with `log(0)` and ensuring such segments are unlikely to be chosen unless absolutely necessary.

Constructors

GaussianLikelihoodCostFunction()

Initializes a new instance of the GaussianLikelihoodCostFunction class.

public GaussianLikelihoodCostFunction()

Properties

SupportsInformationCriteria

Indicates that this cost function provides likelihood metrics suitable for BIC/AIC.

public bool SupportsInformationCriteria { get; }

Property Value

bool

Methods

ComputeCost(int?, int?)

Computes the cost for a segment [start, end) based on the Gaussian negative log-likelihood. The cost is Sum_dimensions [ n * log(variance_mle_dim) ].

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

Calculates the cost in O(D) time using precomputed prefix sums, where D is the number of dimensions. Handles potential zero variance by using a small minimum variance floor to prevent `log(0)`. Must be called after Fit(double[,]). This method returns the same value as ComputeLikelihoodMetric(int, int).

var gaussianCost = new GaussianLikelihoodCostFunction().Fit(data);
double cost = gaussianCost.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(double[,]) not called).

ArgumentOutOfRangeException

Thrown when the segment indices are out of bounds.

SegmentLengthException

Thrown when the segment length is less than 1.

CostFunctionException

Thrown if an unexpected error occurs during cost calculation.

ComputeLikelihoodMetric(int, int)

Computes the likelihood metric for a segment [start, end) based on the Gaussian negative log-likelihood. The metric is Sum_dimensions [ n * log(variance_mle_dim) ].

public double ComputeLikelihoodMetric(int start, int end)

Parameters

start int

The start index of the segment (inclusive).

end int

The end index of the segment (exclusive).

Returns

double

The computed likelihood metric for the segment. Returns double.PositiveInfinity if the metric cannot be computed (e.g., due to numerical issues).

Remarks

Calculates the metric in O(D) time using precomputed prefix sums. Handles potential zero variance by using a small minimum variance floor. Must be called after Fit(double[,]). This method returns the same value as ComputeCost(int?, int?).

Exceptions

UninitializedDataException

Thrown when prefix sums are not initialized (Fit(double[,]) not called).

ArgumentOutOfRangeException

Thrown when the segment indices (start, end) are out of bounds.

SegmentLengthException

Thrown when the segment length (end - start) is less than 1.

CostFunctionException

Thrown if an unexpected error occurs during metric calculation.

Fit(double[,])

Fits the cost function to the provided data by precomputing prefix sums.

public override IPELTCostFunction Fit(double[,] signalMatrix)

Parameters

signalMatrix double[,]

The data array to fit (rows=dimensions, columns=time points).

Returns

IPELTCostFunction

The fitted GaussianLikelihoodCostFunction instance.

Remarks

This method performs O(N*D) computation to calculate prefix sums of the signal and its squares, enabling O(D) cost/metric calculation per segment later. It must be called before cost/metric computation methods.

double[,] data = { { 1.0, 1.1, 1.0, 5.0, 5.1, 4.9 } };
var gaussianCost = new GaussianLikelihoodCostFunction();
gaussianCost.Fit(data);

Exceptions

ArgumentNullException

Thrown if signalMatrix is null.

GetSegmentParameterCount(int)

Gets the number of parameters estimated for a Gaussian model segment. This is 2 parameters (mean and variance) per dimension.

public int GetSegmentParameterCount(int segmentLength)

Parameters

segmentLength int

The length of the segment (unused).

Returns

int

Number of parameters: Number of dimensions * 2.

Exceptions

UninitializedDataException

Thrown if Fit(double[,]) has not been called.