
The adding of new kinds of outliers should be considered with caution: the available ones cover most of the cases. However, this example illustrates the flexibility introduced by the OO implementation of Tramo-Seats.
Any class that derives from the Nbb.Eco.IOutlier interface can be
added to Tramoeats (more specifically, to the OutliersDetector sub-entity
of a Tramo object).
The code below shows how defining and implementing such a derived class and how
using it with Tramo.
See the design of the outliers classes for more
information
References:
- NbbUtilities.dll
- NbbTs.dll
- NbbEco.dll
- NbbMath.dll
- NbbTramoSeats.dll
Code:
/// Definition of the new class of outlier (zigzag outlier, defined by the sequence (0, ..., 0, 1, -2, 1, 0, ...0))
using Nbb.TimeSeries.SimpleTS;
using Nbb.TramoSeats;
using Nbb.Maths.LinearFilters;...
public class ZOutlier : Nbb.Eco.BaseOutlier, Nbb.Eco.IOutlier
{
public ZOutlier() { }#region IOutlier Members
/// <summary>
/// Given the position of the outlier, can we fill a buffer for the periods [0, n[.
/// </summary>
/// <param name="n"></param>
/// <returns>Should be false if the outlier cannot be distinguished from another (simpler) one or from a constant in the considered interval</returns>
public bool CanFill(int n)
{
return Position < n-2; // otherwise, similar to a DO (Position=n-2) /AO (Position=n-1)
}/// <summary>
/// Fill the data with
/// </summary>
/// <param name="data"></param>
public void Fill(double[] data)
{
for (int i = 0; i < data.Length; ++i)
data[i] = 0;
if (Position >= 0 && Position < data.Length)
data[Position] = 1;
if (Position+1 >= 0 && Position+1 < data.Length)
data[Position+1] = -2;
if (Position+2 >= 0 && Position+2 < data.Length)
data[Position+2] = 1;
}// Identify the outlier by means of a rational polynomial (more precisely, the outlier is the result of filtering the sequence (0, ..., 0, 1, 0, ..., 0) with the rational filter in the Backshift operator).
public Nbb.Maths.LinearFilters.RBFilter Filter
{
get
{
double[] weights = new double[] { 1, -2, 1 };
BFilter num=BFilter.Promote(weights);
return new Nbb.Maths.LinearFilters.RBFilter(num, new BFilter(0));
}
}public string Code
{
get { return "ZO"; }
}public string Description
{
get { return "Zigzag outlier. Only for test"; }
}public Nbb.Eco.TSComponent Effect
{
get { return Nbb.Eco.TSComponent.Irregular; }
}public Nbb.Eco.IOutlier Exemplar()
{
return new ZOutlier();
}#endregion
#region ICloneable Members
public object Clone()
{
ZOutlier zo=new ZOutlier();
zo.Position=Position;
return zo;
}#endregion
}
/// Use of the new class
...
// External trade statistics: exports
double[] data =
{
9568.3,9920.3,11353.5,9247.5,10114.2,10763.1,8456.1,8071.6,10328,10551.4,10186.1,8821.6,
9841.3,10233.6,10794.6,10289.3,10513.4,10607.6,9707.4,8103.5,10982.6,11836.9,10517.5,9810.5,
10374.8,10855.3,11671.3,11901.2,10846.4,11917.5,11362.8,9314.5,12605.9,12815.1,11254.5,11111.8,
11282.9,11554.5,12935.6,12146.3,11615.3,13214.8,11735.5,9522.3,12694.8,12317.6,11450,11380.9,
10604.6,10972.2,13331.5,11733.1,11284.7,13295.8,11881.4,10374.2,13828,13490.5,13092.2,13184.4,
12398.4,13882.3,15861.5,13286.1,15634.9,14211,13646.8,12224.6,15916.4,16535.9,15796,14418.6,
15044.5,14944.2,16754.8,14254,15454.9,15644.8,14568.3,12520.2,14803,15873.2,14755.3,12875.1,
14291.1,14205.3,15859.4,15258.9,15498.6,15106.5,15023.6,12083,15761.3,16943,15070.3,13659.6,
14768.9,14725.1,15998.1,15370.6,14956.9,15469.7,15101.8,11703.7,16283.6,16726.5,14968.9,14861,
14583.3,15305.8,17903.9,16379.4,15420.3,17870.5,15912.8,13866.5,17823.2,17872,17420.4,16704.4,
15991.5,16583.6,19123.4,17838.8,17335.3,19026.9,16428.6,15337.4,19379.8,18070.5,19563,18190.6,
17658,18437.9,21510.4,17111,19732.7,20221.8
};TS series = new TS(TSFrequency.Monthly, 1995, 0, data);
TramoSpecification spec = new TramoSpecification();
// For the time being, at least one predefined kind of outlier must be chosen to activate the outliers detection procedure. That condition will be relaxed in the next version.
spec.OutliersDetection.AO = true;
spec.ModelIdentification.IsEnabled = true;Tramo tramo = new Tramo();
tramo.Specification = spec;// User-defined outliers must be added after that the specification has been assigned to the Tramo object.
tramo.OutliersDetector.AddOutlierKind(new ZOutlier());
tramo.Process(series);foreach (OutlierEstimation outlier in tramo.OutliersEstimation())
{
String desc = outlier.Code;
TSPeriod period = series.Domain[outlier.Position];
double value = outlier.Value;
double tstat = outlier.TStat;
}
The code produces the following results: