Skip to content

Commit 38f34b8

Browse files
Fix SsaForecast bug (#5023)
* fix SsaForecast * refind comments * remove static fields to local variable
1 parent fbea448 commit 38f34b8

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/Microsoft.ML.TimeSeries/PolynomialUtils.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ public FactorMultiplicity(int multiplicity = 1)
145145
private sealed class PolynomialFactor
146146
{
147147
public List<decimal> Coefficients;
148-
public static decimal[] Destination;
149148

150149
private decimal _key;
151150
public decimal Key { get { return _key; } }
@@ -175,20 +174,21 @@ internal PolynomialFactor(decimal key)
175174
_key = key;
176175
}
177176

178-
public void Multiply(PolynomialFactor factor)
177+
public void Multiply(PolynomialFactor factor, decimal[] destination)
179178
{
180179
var len = Coefficients.Count;
181180
Coefficients.AddRange(factor.Coefficients);
182181

183-
PolynomialMultiplication(0, len, len, factor.Coefficients.Count, 0, 1, 1);
182+
PolynomialMultiplication(destination, 0, len, len, factor.Coefficients.Count, 0, 1, 1);
184183

185184
for (var i = 0; i < Coefficients.Count; ++i)
186-
Coefficients[i] = Destination[i];
185+
Coefficients[i] = destination[i];
187186

188187
SetKey();
189188
}
190189

191-
private void PolynomialMultiplication(int uIndex, int uLen, int vIndex, int vLen, int dstIndex, decimal uCoeff, decimal vCoeff)
190+
private void PolynomialMultiplication(decimal[] destination, int uIndex, int uLen, int vIndex,
191+
int vLen, int dstIndex, decimal uCoeff, decimal vCoeff)
192192
{
193193
Contracts.Assert(uIndex >= 0);
194194
Contracts.Assert(uLen >= 1);
@@ -198,18 +198,19 @@ private void PolynomialMultiplication(int uIndex, int uLen, int vIndex, int vLen
198198
Contracts.Assert(vIndex + vLen <= Utils.Size(Coefficients));
199199
Contracts.Assert(uIndex + uLen <= vIndex || vIndex + vLen <= uIndex); // makes sure the input ranges are non-overlapping.
200200
Contracts.Assert(dstIndex >= 0);
201-
Contracts.Assert(dstIndex + uLen + vLen <= Utils.Size(Destination));
201+
Contracts.Assert(dstIndex + uLen + vLen <= Utils.Size(destination));
202202

203203
if (uLen == 1 && vLen == 1)
204204
{
205-
Destination[dstIndex] = Coefficients[uIndex] * Coefficients[vIndex];
206-
Destination[dstIndex + 1] = Coefficients[uIndex] + Coefficients[vIndex];
205+
destination[dstIndex] = Coefficients[uIndex] * Coefficients[vIndex];
206+
destination[dstIndex + 1] = Coefficients[uIndex] + Coefficients[vIndex];
207207
}
208208
else
209-
NaivePolynomialMultiplication(uIndex, uLen, vIndex, vLen, dstIndex, uCoeff, vCoeff);
209+
NaivePolynomialMultiplication(destination, uIndex, uLen, vIndex, vLen, dstIndex, uCoeff, vCoeff);
210210
}
211211

212-
private void NaivePolynomialMultiplication(int uIndex, int uLen, int vIndex, int vLen, int dstIndex, decimal uCoeff, decimal vCoeff)
212+
private void NaivePolynomialMultiplication(decimal[] destination, int uIndex, int uLen, int vIndex,
213+
int vLen, int dstIndex, decimal uCoeff, decimal vCoeff)
213214
{
214215
int i;
215216
int j;
@@ -246,7 +247,7 @@ private void NaivePolynomialMultiplication(int uIndex, int uLen, int vIndex, int
246247
for (j = 0; j < a; ++j)
247248
temp += (Coefficients[b - j + uIndex] * Coefficients[c + j + vIndex]);
248249

249-
Destination[i + dstIndex] = temp;
250+
destination[i + dstIndex] = temp;
250251
}
251252
}
252253
}
@@ -285,6 +286,7 @@ public static bool FindPolynomialCoefficients(Complex[] roots, ref Double[] coef
285286
int destinationOffset = 0;
286287

287288
var factors = new List<PolynomialFactor>();
289+
decimal[] destination = new decimal[n];
288290

289291
for (i = 0; i < n; ++i)
290292
{
@@ -336,9 +338,6 @@ public static bool FindPolynomialCoefficients(Complex[] roots, ref Double[] coef
336338

337339
if (destinationOffset < n - 1)
338340
{
339-
if (Utils.Size(PolynomialFactor.Destination) < n)
340-
PolynomialFactor.Destination = new decimal[n];
341-
342341
while (factors.Count > 1)
343342
{
344343
var k1 = Math.Abs(factors.ElementAt(0).Key);
@@ -364,7 +363,7 @@ public static bool FindPolynomialCoefficients(Complex[] roots, ref Double[] coef
364363
var f2 = factors.ElementAt(ind);
365364
factors.RemoveAt(ind);
366365

367-
f1.Multiply(f2);
366+
f1.Multiply(f2, destination);
368367

369368
ind = factors.BinarySearch(f1, comparer);
370369
if (ind >= 0)

test/Microsoft.ML.TimeSeries.Tests/TimeSeriesDirectApi.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,6 @@ public void ChangePointDetectionWithSeasonalityPredictionEngine()
328328
}
329329

330330
[LessThanNetCore30OrNotNetCoreFact("netcoreapp3.1 output differs from Baseline")]
331-
//Skipping test temporarily. This test will be re-enabled once the cause of failures has been determined
332-
[Trait("Category", "SkipInCI")]
333331
public void SsaForecast()
334332
{
335333
var env = new MLContext(1);

0 commit comments

Comments
 (0)