Skip to content

Commit 5ec4472

Browse files
authored
TimeSeries - fix confidence parameter type for some detectors (#4058) (#5623)
* TimeSeries - fix confidence parameter type for some detectors. - The public API exposed confidence parameters as int even though it's internally implemented as double - There was no workaround since all classes where double is used are internal - This caused major issues for software requiring high precision predictions - This change to API should be backwards compatible since int can be passed to parameter of type double * TimeSeries - reintroduce original methods with confidence parameter of type int (to not break the API). * TimeSeries - make catalog API methods with int confidence parameter deprecated. - Tests adjusted to not use the deprecated methods
1 parent 05ef676 commit 5ec4472

15 files changed

+128
-21
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectChangePointBySsa.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void Example()
5252
// Setup SsaChangePointDetector arguments
5353
var inputColumnName = nameof(TimeSeriesData.Value);
5454
var outputColumnName = nameof(ChangePointPrediction.Prediction);
55-
int confidence = 95;
55+
double confidence = 95;
5656
int changeHistoryLength = 8;
5757

5858
// Train the change point detector.

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectChangePointBySsaBatchPrediction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void Example()
5959

6060
// The transformed data.
6161
var transformedData = ml.Transforms.DetectChangePointBySsa(
62-
outputColumnName, inputColumnName, 95, 8, TrainingSize,
62+
outputColumnName, inputColumnName, 95.0d, 8, TrainingSize,
6363
SeasonalitySize + 1).Fit(dataView).Transform(dataView);
6464

6565
// Getting the data of the newly created column as an IEnumerable of

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectChangePointBySsaStream.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void Example()
5252
// Setup SsaChangePointDetector arguments
5353
var inputColumnName = nameof(TimeSeriesData.Value);
5454
var outputColumnName = nameof(ChangePointPrediction.Prediction);
55-
int confidence = 95;
55+
double confidence = 95;
5656
int changeHistoryLength = 8;
5757

5858
// Train the change point detector.

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidChangePoint.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void Example()
5555

5656
// Time Series model.
5757
ITransformer model = ml.Transforms.DetectIidChangePoint(
58-
outputColumnName, inputColumnName, 95, Size / 4).Fit(dataView);
58+
outputColumnName, inputColumnName, 95.0d, Size / 4).Fit(dataView);
5959

6060
// Create a time series prediction engine from the model.
6161
var engine = model.CreateTimeSeriesEngine<TimeSeriesData,

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidChangePointBatchPrediction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void Example()
5353

5454
// The transformed data.
5555
var transformedData = ml.Transforms.DetectIidChangePoint(
56-
outputColumnName, inputColumnName, 95, Size / 4).Fit(dataView)
56+
outputColumnName, inputColumnName, 95.0d, Size / 4).Fit(dataView)
5757
.Transform(dataView);
5858

5959
// Getting the data of the newly created column as an IEnumerable of

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidSpike.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void Example()
4747

4848
// The transformed model.
4949
ITransformer model = ml.Transforms.DetectIidSpike(outputColumnName,
50-
inputColumnName, 95, Size).Fit(dataView);
50+
inputColumnName, 95.0d, Size).Fit(dataView);
5151

5252
// Create a time series prediction engine from the model.
5353
var engine = model.CreateTimeSeriesEngine<TimeSeriesData,

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidSpikeBatchPrediction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static void Example()
4545

4646
// The transformed data.
4747
var transformedData = ml.Transforms.DetectIidSpike(outputColumnName,
48-
inputColumnName, 95, Size / 4).Fit(dataView).Transform(dataView);
48+
inputColumnName, 95.0d, Size / 4).Fit(dataView).Transform(dataView);
4949

5050
// Getting the data of the newly created column as an IEnumerable of
5151
// IidSpikePrediction.

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectSpikeBySsa.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void Example()
5353

5454
// Train the change point detector.
5555
ITransformer model = ml.Transforms.DetectSpikeBySsa(outputColumnName,
56-
inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(
56+
inputColumnName, 95.0d, 8, TrainingSize, SeasonalitySize + 1).Fit(
5757
dataView);
5858

5959
// Create a prediction engine from the model for feeding new data.

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectSpikeBySsaBatchPrediction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void Example()
6161

6262
// The transformed data.
6363
var transformedData = ml.Transforms.DetectSpikeBySsa(outputColumnName,
64-
inputColumnName, 95, 8, TrainingSize, SeasonalitySize + 1).Fit(
64+
inputColumnName, 95.0d, 8, TrainingSize, SeasonalitySize + 1).Fit(
6565
dataView).Transform(dataView);
6666

6767
// Getting the data of the newly created column as an IEnumerable of

src/Microsoft.ML.TimeSeries/ExtensionsCatalog.cs

+107
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using Microsoft.ML.Data;
78
using Microsoft.ML.Runtime;
@@ -32,8 +33,33 @@ public static class TimeSeriesCatalog
3233
/// ]]>
3334
/// </format>
3435
/// </example>
36+
[Obsolete("This API method is deprecated, please use the overload with confidence parameter of type double.")]
3537
public static IidChangePointEstimator DetectIidChangePoint(this TransformsCatalog catalog, string outputColumnName, string inputColumnName,
3638
int confidence, int changeHistoryLength, MartingaleType martingale = MartingaleType.Power, double eps = 0.1)
39+
=> DetectIidChangePoint(catalog, outputColumnName, inputColumnName, (double)confidence, changeHistoryLength, martingale, eps);
40+
41+
/// <summary>
42+
/// Create <see cref="IidChangePointEstimator"/>, which predicts change points in an
43+
/// <a href="https://en.wikipedia.org/wiki/Independent_and_identically_distributed_random_variables">independent identically distributed (i.i.d.)</a>
44+
/// time series based on adaptive kernel density estimations and martingale scores.
45+
/// </summary>
46+
/// <param name="catalog">The transform's catalog.</param>
47+
/// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.
48+
/// The column data is a vector of <see cref="System.Double"/>. The vector contains 4 elements: alert (non-zero value means a change point), raw score, p-Value and martingale score.</param>
49+
/// <param name="inputColumnName">Name of column to transform. The column data must be <see cref="System.Single"/>. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
50+
/// <param name="confidence">The confidence for change point detection in the range [0, 100].</param>
51+
/// <param name="changeHistoryLength">The length of the sliding window on p-values for computing the martingale score.</param>
52+
/// <param name="martingale">The martingale used for scoring.</param>
53+
/// <param name="eps">The epsilon parameter for the Power martingale.</param>
54+
/// <example>
55+
/// <format type="text/markdown">
56+
/// <![CDATA[
57+
/// [!code-csharp[DetectIidChangePoint](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidChangePointBatchPrediction.cs)]
58+
/// ]]>
59+
/// </format>
60+
/// </example>
61+
public static IidChangePointEstimator DetectIidChangePoint(this TransformsCatalog catalog, string outputColumnName, string inputColumnName,
62+
double confidence, int changeHistoryLength, MartingaleType martingale = MartingaleType.Power, double eps = 0.1)
3763
=> new IidChangePointEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, confidence, changeHistoryLength, inputColumnName, martingale, eps);
3864

3965
/// <summary>
@@ -56,8 +82,33 @@ public static IidChangePointEstimator DetectIidChangePoint(this TransformsCatalo
5682
/// ]]>
5783
/// </format>
5884
/// </example>
85+
[Obsolete("This API method is deprecated, please use the overload with confidence parameter of type double.")]
5986
public static IidSpikeEstimator DetectIidSpike(this TransformsCatalog catalog, string outputColumnName, string inputColumnName,
6087
int confidence, int pvalueHistoryLength, AnomalySide side = AnomalySide.TwoSided)
88+
=> DetectIidSpike(catalog, outputColumnName, inputColumnName, (double)confidence, pvalueHistoryLength, side);
89+
90+
/// <summary>
91+
/// Create <see cref="IidSpikeEstimator"/>, which predicts spikes in
92+
/// <a href="https://en.wikipedia.org/wiki/Independent_and_identically_distributed_random_variables"> independent identically distributed (i.i.d.)</a>
93+
/// time series based on adaptive kernel density estimations and martingale scores.
94+
/// </summary>
95+
/// <param name="catalog">The transform's catalog.</param>
96+
/// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.
97+
/// The column data is a vector of <see cref="System.Double"/>. The vector contains 3 elements: alert (non-zero value means a spike), raw score, and p-value.</param>
98+
/// <param name="inputColumnName">Name of column to transform. The column data must be <see cref="System.Single"/>.
99+
/// If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
100+
/// <param name="confidence">The confidence for spike detection in the range [0, 100].</param>
101+
/// <param name="pvalueHistoryLength">The size of the sliding window for computing the p-value.</param>
102+
/// <param name="side">The argument that determines whether to detect positive or negative anomalies, or both.</param>
103+
/// <example>
104+
/// <format type="text/markdown">
105+
/// <![CDATA[
106+
/// [!code-csharp[DetectIidSpike](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectIidSpikeBatchPrediction.cs)]
107+
/// ]]>
108+
/// </format>
109+
/// </example>
110+
public static IidSpikeEstimator DetectIidSpike(this TransformsCatalog catalog, string outputColumnName, string inputColumnName,
111+
double confidence, int pvalueHistoryLength, AnomalySide side = AnomalySide.TwoSided)
61112
=> new IidSpikeEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, confidence, pvalueHistoryLength, inputColumnName, side);
62113

63114
/// <summary>
@@ -83,9 +134,38 @@ public static IidSpikeEstimator DetectIidSpike(this TransformsCatalog catalog, s
83134
/// ]]>
84135
/// </format>
85136
/// </example>
137+
[Obsolete("This API method is deprecated, please use the overload with confidence parameter of type double.")]
86138
public static SsaChangePointEstimator DetectChangePointBySsa(this TransformsCatalog catalog, string outputColumnName, string inputColumnName,
87139
int confidence, int changeHistoryLength, int trainingWindowSize, int seasonalityWindowSize, ErrorFunction errorFunction = ErrorFunction.SignedDifference,
88140
MartingaleType martingale = MartingaleType.Power, double eps = 0.1)
141+
=> DetectChangePointBySsa(catalog, outputColumnName, inputColumnName, (double)confidence, changeHistoryLength, trainingWindowSize, seasonalityWindowSize, errorFunction, martingale, eps);
142+
143+
/// <summary>
144+
/// Create <see cref="SsaChangePointEstimator"/>, which predicts change points in time series
145+
/// using <a href="https://en.wikipedia.org/wiki/Singular_spectrum_analysis">Singular Spectrum Analysis (SSA)</a>.
146+
/// </summary>
147+
/// <param name="catalog">The transform's catalog.</param>
148+
/// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.
149+
/// The column data is a vector of <see cref="System.Double"/>. The vector contains 4 elements: alert (non-zero value means a change point), raw score, p-Value and martingale score.</param>
150+
/// <param name="inputColumnName">Name of column to transform. The column data must be <see cref="System.Single"/>.
151+
/// If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
152+
/// <param name="confidence">The confidence for change point detection in the range [0, 100].</param>
153+
/// <param name="trainingWindowSize">The number of points from the beginning of the sequence used for training.</param>
154+
/// <param name="changeHistoryLength">The size of the sliding window for computing the p-value.</param>
155+
/// <param name="seasonalityWindowSize">An upper bound on the largest relevant seasonality in the input time-series.</param>
156+
/// <param name="errorFunction">The function used to compute the error between the expected and the observed value.</param>
157+
/// <param name="martingale">The martingale used for scoring.</param>
158+
/// <param name="eps">The epsilon parameter for the Power martingale.</param>
159+
/// <example>
160+
/// <format type="text/markdown">
161+
/// <![CDATA[
162+
/// [!code-csharp[DetectChangePointBySsa](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectChangePointBySsaBatchPrediction.cs)]
163+
/// ]]>
164+
/// </format>
165+
/// </example>
166+
public static SsaChangePointEstimator DetectChangePointBySsa(this TransformsCatalog catalog, string outputColumnName, string inputColumnName,
167+
double confidence, int changeHistoryLength, int trainingWindowSize, int seasonalityWindowSize, ErrorFunction errorFunction = ErrorFunction.SignedDifference,
168+
MartingaleType martingale = MartingaleType.Power, double eps = 0.1)
89169
=> new SsaChangePointEstimator(CatalogUtils.GetEnvironment(catalog), new SsaChangePointDetector.Options
90170
{
91171
Name = outputColumnName,
@@ -121,7 +201,34 @@ public static SsaChangePointEstimator DetectChangePointBySsa(this TransformsCata
121201
/// ]]>
122202
/// </format>
123203
/// </example>
204+
[Obsolete("This API method is deprecated, please use the overload with confidence parameter of type double.")]
124205
public static SsaSpikeEstimator DetectSpikeBySsa(this TransformsCatalog catalog, string outputColumnName, string inputColumnName, int confidence, int pvalueHistoryLength,
206+
int trainingWindowSize, int seasonalityWindowSize, AnomalySide side = AnomalySide.TwoSided, ErrorFunction errorFunction = ErrorFunction.SignedDifference)
207+
=> DetectSpikeBySsa(catalog, outputColumnName, inputColumnName, (double)confidence, pvalueHistoryLength, trainingWindowSize, seasonalityWindowSize, side, errorFunction);
208+
209+
/// <summary>
210+
/// Create <see cref="SsaSpikeEstimator"/>, which predicts spikes in time series
211+
/// using <a href="https://en.wikipedia.org/wiki/Singular_spectrum_analysis">Singular Spectrum Analysis (SSA)</a>.
212+
/// </summary>
213+
/// <param name="catalog">The transform's catalog.</param>
214+
/// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.
215+
/// The column data is a vector of <see cref="System.Double"/>. The vector contains 3 elements: alert (non-zero value means a spike), raw score, and p-value.</param>
216+
/// <param name="inputColumnName">Name of column to transform. The column data must be <see cref="System.Single"/>.
217+
/// If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
218+
/// <param name="confidence">The confidence for spike detection in the range [0, 100].</param>
219+
/// <param name="pvalueHistoryLength">The size of the sliding window for computing the p-value.</param>
220+
/// <param name="trainingWindowSize">The number of points from the beginning of the sequence used for training.</param>
221+
/// <param name="seasonalityWindowSize">An upper bound on the largest relevant seasonality in the input time-series.</param>
222+
/// <param name="side">The argument that determines whether to detect positive or negative anomalies, or both.</param>
223+
/// <param name="errorFunction">The function used to compute the error between the expected and the observed value.</param>
224+
/// <example>
225+
/// <format type="text/markdown">
226+
/// <![CDATA[
227+
/// [!code-csharp[DetectSpikeBySsa](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/TimeSeries/DetectSpikeBySsaBatchPrediction.cs)]
228+
/// ]]>
229+
/// </format>
230+
/// </example>
231+
public static SsaSpikeEstimator DetectSpikeBySsa(this TransformsCatalog catalog, string outputColumnName, string inputColumnName, double confidence, int pvalueHistoryLength,
125232
int trainingWindowSize, int seasonalityWindowSize, AnomalySide side = AnomalySide.TwoSided, ErrorFunction errorFunction = ErrorFunction.SignedDifference)
126233
=> new SsaSpikeEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, confidence, pvalueHistoryLength, trainingWindowSize, seasonalityWindowSize, inputColumnName, side, errorFunction);
127234

src/Microsoft.ML.TimeSeries/IidChangePointDetector.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private static IRowMapper Create(IHostEnvironment env, ModelLoadContext ctx, Dat
219219
/// ]]>
220220
/// </format>
221221
/// </remarks>
222-
/// <seealso cref="Microsoft.ML.TimeSeriesCatalog.DetectIidChangePoint(Microsoft.ML.TransformsCatalog,System.String,System.String,System.Int32,System.Int32,Microsoft.ML.Transforms.TimeSeries.MartingaleType,System.Double)" />
222+
/// <seealso cref="Microsoft.ML.TimeSeriesCatalog.DetectIidChangePoint(Microsoft.ML.TransformsCatalog,System.String,System.String,System.Double,System.Int32,Microsoft.ML.Transforms.TimeSeries.MartingaleType,System.Double)" />
223223
public sealed class IidChangePointEstimator : TrivialEstimator<IidChangePointDetector>
224224
{
225225
/// <summary>
@@ -233,7 +233,7 @@ public sealed class IidChangePointEstimator : TrivialEstimator<IidChangePointDet
233233
/// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
234234
/// <param name="martingale">The martingale used for scoring.</param>
235235
/// <param name="eps">The epsilon parameter for the Power martingale.</param>
236-
internal IidChangePointEstimator(IHostEnvironment env, string outputColumnName, int confidence,
236+
internal IidChangePointEstimator(IHostEnvironment env, string outputColumnName, double confidence,
237237
int changeHistoryLength, string inputColumnName, MartingaleType martingale = MartingaleType.Power, double eps = 0.1)
238238
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(IidChangePointEstimator)),
239239
new IidChangePointDetector(env, new IidChangePointDetector.Options

src/Microsoft.ML.TimeSeries/IidSpikeDetector.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private static IRowMapper Create(IHostEnvironment env, ModelLoadContext ctx, Dat
199199
/// ]]>
200200
/// </format>
201201
/// </remarks>
202-
/// <seealso cref="Microsoft.ML.TimeSeriesCatalog.DetectIidSpike(Microsoft.ML.TransformsCatalog,System.String,System.String,System.Int32,System.Int32,Microsoft.ML.Transforms.TimeSeries.AnomalySide)" />
202+
/// <seealso cref="Microsoft.ML.TimeSeriesCatalog.DetectIidSpike(Microsoft.ML.TransformsCatalog,System.String,System.String,System.Double,System.Int32,Microsoft.ML.Transforms.TimeSeries.AnomalySide)" />
203203
public sealed class IidSpikeEstimator : TrivialEstimator<IidSpikeDetector>
204204
{
205205
/// <summary>
@@ -212,7 +212,7 @@ public sealed class IidSpikeEstimator : TrivialEstimator<IidSpikeDetector>
212212
/// <param name="pvalueHistoryLength">The size of the sliding window for computing the p-value.</param>
213213
/// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
214214
/// <param name="side">The argument that determines whether to detect positive or negative anomalies, or both.</param>
215-
internal IidSpikeEstimator(IHostEnvironment env, string outputColumnName, int confidence, int pvalueHistoryLength, string inputColumnName, AnomalySide side = AnomalySide.TwoSided)
215+
internal IidSpikeEstimator(IHostEnvironment env, string outputColumnName, double confidence, int pvalueHistoryLength, string inputColumnName, AnomalySide side = AnomalySide.TwoSided)
216216
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(IidSpikeDetector)),
217217
new IidSpikeDetector(env, new IidSpikeDetector.Options
218218
{

0 commit comments

Comments
 (0)