NbbTools > Use cases

Seasonal Adjustment

Without going into the details, we provide below the minimal code to process different methods of seasonal adjustment. The methods considered are the following:

Be aware that a lot of other specifications are possible and that many other results are available.

Even if some efforts have been made in that direction, the libraries don't provide an uniform approach to the seasonal adjustment: the different solutions available in the libraries don't show the same level of achievement, and there remain a lot of inconsistencies in the terminology.

In the future (2007), a more coherent framework for seasonal adjustment should be available.

 

C#:

References:

Code:

using Nbb.TimeSeries.SimpleTS;
using Nbb.GeneralAirline;
using Nbb.TramoSeats;
using Nbb.SeasonalAdjustment.Bsm;
using Nbb.SeasonalAdjustment.X11;

...

// 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);
TS tramoseats_sa, x11_sa, bsm_sa, gairline_sa;

// Tramo-Seats, with some usual specifications
TramoSpecification tramoSpec = new TramoSpecification();
tramoSpec.Transformation.Option = DataTransformation.Pretest;
tramoSpec.ModelIdentification.IsEnabled = true;
tramoSpec.OutliersDetection.AllTramo();
tramoSpec.CalendarEffect.TradingDaysEffect = TradingDaysOption.Td1Pretest;
tramoSpec.CalendarEffect.LeapYearEffect = TramoChoice.Pretest;
tramoSpec.CalendarEffect.EasterEffect = TramoChoice.Pretest;

// Use of the Kalman smoothers instead of the Wiener-Kolmogorov filters
SeatsSpecification seatsSpec = new SeatsSpecification();
seatsSpec.Method = ComponentsEstimationMethod.Kalman;

TramoSeats trs = new TramoSeats();
if (trs.Process(series, tramoSpec, seatsSpec))
    tramoseats_sa = trs.FinalComponent(CmpType.SeasonallyAdjusted);

// X11
X11Specification x11Spec = new X11Specification();
x11Spec.TradingDaysCorrection = true;
X11 x11 = new X11();
x11.Specification = x11Spec;
if (x11.Decompose(series))
    x11_sa = x11.Component(Nbb.Eco.TSComponent.SeasonallyAdjusted);

// Basic structural model
ExtendedBsmSpecification bsmSpec = new ExtendedBsmSpecification();
bsmSpec.Seasonal = Nbb.Bsm.SeasModel.HarrisonStevens;
bsmSpec.Slope = CmpSpec.Stochastic;
bsmSpec.CE.TdCount = 1;

BsmDecomposer bsm = new BsmDecomposer();
bsm.Specification = bsmSpec;
if (bsm.Decompose(series))
    bsm_sa=bsm.Component(Nbb.Eco.TSComponent.SeasonallyAdjusted);

// Generalized airline model
GeneralizedAirlineSpecification gairlineSpec = new GeneralizedAirlineSpecification();
GeneralizedAirlineMonitor gairline = new GeneralizedAirlineMonitor();
gairlineSpec.CE.TdCount = 1;
gairline.Specification = gairlineSpec;
if (gairline.Process(series))
    gairline_sa=gairline.BestSAResult.Component(Nbb.Eco.TSComponent.SeasonallyAdjusted);

 

The code above gives the following results:

VB.Net:

References:

Code:

Imports Nbb.TimeSeries
Imports Nbb.TimeSeries.SimpleTS
Imports Nbb.TramoSeats
Imports Nbb.SeasonalAdjustment
Imports Nbb.SeasonalAdjustment.X11
Imports Nbb.SeasonalAdjustment.Bsm
Imports Nbb.Eco
Imports Nbb.Bsm
Imports Nbb.GeneralAirline

...

Dim data() As Double
Dim series As TS
Dim tramoseats_sa, x11_sa, bsm_sa, gairline_sa As TS

'External trade statistics: exports
data = New Double(){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}

series = New TS(TSFrequency.Monthly, 1995, 0, data)

'Tramo-Seats, with some usual specifications
Dim tramoSpec As New TramoSpecification
Dim seatsSpec As New SeatsSpecification
Dim trs As New TramoSeats

With tramoSpec
    .Transformation.Option = DataTransformation.Pretest

    .ModelIdentification.IsEnabled = True

    .OutliersDetection.AllTramo()
   
With .CalendarEffect
        .TradingDaysEffect = TradingDaysOption.Td1Pretest
        .LeapYearEffect = TramoChoice.Pretest
        .EasterEffect = TramoChoice.Pretest

    End With
End With

'Use of the Kalman smoothers instead of the Wiener-Kolmogorov filters
seatsSpec.Method = ComponentsEstimationMethod.Kalman

If trs.Process(series, tramoSpec, seatsSpec) Then
    tramoseats_sa = trs.FinalComponent(CmpType.SeasonallyAdjusted)
End If

'X11
Dim x11 As New X11
Dim x11Spec As X11Specification

x11Spec.TradingDaysCorrection = True
x11.Specification = x11Spec
If x11.Decompose(series) Then
    x11_sa = x11.Component(TSComponent.SeasonallyAdjusted)
End If

'Basic structural model
Dim bsm As New BsmDecomposer
Dim bsmSpec As New ExtendedBsmSpecification

With bsmSpec
    .Seasonal = SeasModel.HarrisonStevens
    .Slope = CmpSpec.Stochastic
    .CE.TdCount = 1

End With

bsm.Specification = bsmSpec
If bsm.Decompose(series) Then
    bsm_sa = bsm.Component(TSComponent.SeasonallyAdjusted)
End If

'Generalized airline model
Dim gairline As New GeneralizedAirlineMonitor
Dim gairlineSpec As New GeneralizedAirlineSpecification

gairlineSpec.CE.TdCount = 1
gairline.Specification = gairlineSpec

If gairline.Process(series) Then
    gairline_sa = gairline.BestSAResult.Component(TSComponent.SeasonallyAdjusted)
End If

VBA (Excel):

You can download an XLS file with the example code here (requires the installation off the last full package with the COM interfaces (10/2006) ).

References:

Code:

Public Sub TS_SA()

    'NbbTs.TS creation of 138 observations from the data of a vertical oriented
    'time series situated in the 1st and 2nd column of an Excel sheet.

    Dim series As NbbTs.TS
    Dim dc As New NbbTs.TSDataCollector
    Dim i As Integer

    For i = 2 To 139
        dc.AddObservation Cells(i, 1), Cells(i, 2)
    Next i

    Set series = dc.Make(TSFrequency_Undefined, TSAggregationType_None)

   
'Tramo-Seats, with some usual specifications
    Dim tramoSpec As New NbbTramoSeats.TramoSpecification
    Dim seatsSpec As New NbbTramoSeats.SeatsSpecification
    Dim tramoseats_sa As NbbTs.TS
    Dim trs As New NbbTramoSeats.TramoSeats

    With tramoSpec
        .Transformation.Option = DataTransformation_Pretest
        .ModelIdentification.IsEnabled = True
        .OutliersDetection.AllTramo
        With .CalendarEffect
            .TradingDaysEffect = TradingDaysOption_Td1Pretest
            .LeapYearEffect = TramoChoice_Pretest
            .EasterEffect = TramoChoice_Pretest
        End With
    End With

    'Use of the Kalman smoothers instead of the Wiener-Kolmogorov filters
    seatsSpec.Method = ComponentsEstimationMethod_Kalman


    If trs.Process(series, tramoSpec, seatsSpec) Then
        Set tramoseats_sa = trs.FinalComponent(CmpType_SeasonallyAdjusted)
    End If

    Display_Series_Vertical_in_XL tramoseats_sa, "TramoSeats", 1, 3

   
'X11
    Dim x11_sa As NbbTs.TS
    Dim x11 As New NbbSA.x11
    Dim x11Spec As New NbbSA.X11Specification

    x11Spec.TradingDaysCorrection = True
    Set x11.Specification = x11Spec
    If x11.Decompose(series) Then
        Set x11_sa = x11.Component(TSComponent_SeasonallyAdjusted)
    End If

    Display_Series_Vertical_in_XL x11_sa, "X11", 1, 4

   
'Basic structural model
    Dim bsm_sa As NbbTs.TS
    Dim bsm As New NbbSA.BsmDecomposer
    Dim bsmSpec As New NbbSA.ExtendedBsmSpecification

    With bsmSpec
        .Seasonal = SeasModel_HarrisonStevens
        .Slope = CmpSpec_Stochastic
        .CE.TdCount = 1
    End With

    Set bsm.Specification = bsmSpec
    If bsm.Decompose(series) Then
        Set bsm_sa = bsm.Component(TSComponent_SeasonallyAdjusted)
    End If

    Display_Series_Vertical_in_XL bsm_sa, "BSM", 1, 5

   
'Generalized airline model
    Dim gairline_sa As NbbTs.TS
    Dim gairline As New NbbGeneralizedAirline.GeneralizedAirlineMonitor
    Dim gairlineSpec As New NbbGeneralizedAirline.GeneralizedAirlineSpecification

    gairlineSpec.CE.TdCount = 1
    Set gairline.Specification = gairlineSpec
    If gairline.Process(series) Then
        Set gairline_sa = gairline.BestSAResult.Component(TSComponent_SeasonallyAdjusted)
    End If

    Display_Series_Vertical_in_XL gairline_sa, "Gen Airline", 1, 6

End Sub

'Function to display vertically a NBBTs.TS series in an Excel sheet at row r and column c with a s_name name
Private Function Display_Series_Vertical_in_XL(series As NbbTs.TS, s_name As String, r As Long, c As Integer)
    Dim nbr As Long
    Dim result_range As Excel.Range
    Dim vertical_array() As Double
    Dim i As Long

    nbr = series.domain.Count
    With ActiveWorkbook.ActiveSheet
        .Range(.Cells(r, c), .Cells(r, c)).Value = s_name
        Set result_range = .Range(.Cells(r + 1, c), .Cells(r + nbr, c))
    End With

    ReDim vertical_array(nbr - 1, 0) As Double
    For i = 0 To nbr - 1
        vertical_array(i, 0) = series.Values(i)
    Next i

    result_range.Value = vertical_array

End Function