Loading...

vastorbit.machine_learning.model_selection.statistical_tests.tsa.seasonal_decompose

vastorbit.machine_learning.model_selection.statistical_tests.tsa.seasonal_decompose(input_relation: Annotated[str | VastFrame, ''], columns: Annotated[str | list[str], 'STRING representing one column or a list of columns'], ts: str, by: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None, period: int | tuple | list = -1, polynomial_order: int | tuple | list = 1, estimate_seasonality: bool = True, rule: Annotated[str | timedelta, 'Time Interval'] | None = None, mult: bool = False, two_sided: bool = False, use_row: bool = True, genSQL: bool = False) VastFrame

Performs a seasonal time series decomposition. Seasonal decomposition plots are graphical representations of the decomposition of time series data into its various components: trend, seasonality, and residual (error). Seasonal decomposition is a technique used to break down a time series into these underlying components to better understand its patterns and behavior.

Seasonal decomposition plots are useful for several purposes:

  • Trend Analysis:

    Understanding the long-term direction or behavior of the time series.

  • Seasonal Patterns:

    Identifying repeating patterns or cycles within the data.

  • Anomaly Detection:

    Spotting unusual behavior or outliers in the residuals.

  • Modeling:

    Informing the choice of appropriate models for forecasting or analysis.

Parameters:
  • input_relation (SQLRelation) – Input relation.

  • columns (SQLColumns) – Input VastColumn to decompose.

  • ts (str) – Time series VastColumn used to order the data. It can be of type date or a numerical VastColumn.

  • by (SQLColumns, optional) – VastColumn used in the partition.

  • period (int | tuple | list, optional) –

    Time series period. It is used to retrieve the seasonality component. If period <= 0, the seasonal component is estimated using ACF. In this case, polynomial_order must be greater than 0.

    It can be an int or a list | tuple of int, each one representing the period of the i-th column.

  • polynomial_order (int | tuple | list, optional) –

    If greater than 0, the trend is estimated using a polynomial of degree 'polynomial_order' and the parameter two_sided is ignored. If equal to 0, the trend is estimated using Moving Averages.

    It can be an int or a list | tuple of int, each one representing the polynomial_order of the i-th column.

  • estimate_seasonality (bool, optional) – If set to True, the seasonality is estimated using cosine and sine functions.

  • rule (TimeInterval, optional) – Interval used to slice the time. For example, '5 minutes' creates records separated by '5 minutes' time interval.

  • mult (bool, optional) – If set to True, the decomposition type is ‘multiplicative’. Otherwise, ‘additive’.

  • two_sided (bool, optional) – If set to True, a centered moving average is used for the trend isolation. Otherwise, only past values are used.

  • use_row (bool, optional) – If set to True, the ROW datatype is used to merge all the different columns time series components together.

  • genSQL (bool, optional) – If set to True, the SQL code for creating the final relation is generated but not executed.

Returns:

object containing the different time series components.

Return type:

VastFrame

Examples

Let us use a dataset that has seasonality. The Airline passengers dataset is a good example.

import vastorbit.datasets as vod

data = vod.load_airline_passengers()
📅
date
Date
123
passengers
Integer
11949-01-01112
21949-02-01118
31949-03-01132
41949-04-01129
51949-05-01121
61949-06-01135
71949-07-01148
81949-08-01148
91949-09-01136
101949-10-01119
111949-11-01104
121949-12-01118
131950-01-01115
141950-02-01126
151950-03-01141
161950-04-01135
171950-05-01125
181950-06-01149
191950-07-01170
201950-08-01170
211950-09-01158
221950-10-01133
231950-11-01114
241950-12-01140
251951-01-01145
261951-02-01150
271951-03-01178
281951-04-01163
291951-05-01172
301951-06-01178
311951-07-01199
321951-08-01199
331951-09-01184
341951-10-01162
351951-11-01146
361951-12-01166
371952-01-01171
381952-02-01180
391952-03-01193
401952-04-01181
411952-05-01183
421952-06-01218
431952-07-01230
441952-08-01242
451952-09-01209
461952-10-01191
471952-11-01172
481952-12-01194
491953-01-01196
501953-02-01196
511953-03-01236
521953-04-01235
531953-05-01229
541953-06-01243
551953-07-01264
561953-08-01272
571953-09-01237
581953-10-01211
591953-11-01180
601953-12-01201
611954-01-01204
621954-02-01188
631954-03-01235
641954-04-01227
651954-05-01234
661954-06-01264
671954-07-01302
681954-08-01293
691954-09-01259
701954-10-01229
711954-11-01203
721954-12-01229
731955-01-01242
741955-02-01233
751955-03-01267
761955-04-01269
771955-05-01270
781955-06-01315
791955-07-01364
801955-08-01347
811955-09-01312
821955-10-01274
831955-11-01237
841955-12-01278
851956-01-01284
861956-02-01277
871956-03-01317
881956-04-01313
891956-05-01318
901956-06-01374
911956-07-01413
921956-08-01405
931956-09-01355
941956-10-01306
951956-11-01271
961956-12-01306
971957-01-01315
981957-02-01301
991957-03-01356
1001957-04-01348
Rows: 1-100 | Columns: 2

Note

vastorbit offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the vastorbit environment.

Data Visualization

Let us first have a look how the data looks like:

data["passengers"].plot(ts = "date")

We can visually observe:

  • Overall increasing trend

  • A seasonal component

  • Some noise

Now we can use the seasonal_decompose to separate these three.

Decomposition

We can directly the function on the dataset:

from vastorbit.machine_learning.model_selection.statistical_tests import seasonal_decompose

decomposition = seasonal_decompose(
    data,
    "passengers",
    "date",
    polynomial_order = 2,
    mult = True,
    use_row = False,
)
📅
date
Date
123
passengers
Integer
123
passengers_trend
Double
123
passengers_seasonal
Double
123
passengers_epsilon
Double
11949-01-01112114.028040859707130.85013534232545341.1553625566761538
21949-02-01118115.690060606611150.88213132374145211.1562525803339132
31949-03-01132117.366096750150960.94571012177385431.1892501982631951
41949-04-01129119.05614929032661.02383584883413641.0582969550056072
51949-05-01121120.760218227138011.09557477944840160.914575272399499
61949-06-01135122.478303560585251.14170452509268520.9654302083460592
71949-07-01148124.210405290668261.14986465767454841.0362320267396994
81949-08-01148125.956523417387091.117868676258551.0511150718469124
91949-09-01136127.716657940741731.05428987822614761.0100231447897343
101949-10-01119129.490808860732160.97616415116586540.9414237778004713
111949-11-01104131.27897617735840.90442522055160020.8759221197673622
121949-12-01118133.081159890620460.85829547490731651.0330672813834307
131950-01-01115134.89736000051830.85013534232545341.002781512544378
141950-02-01126136.727576507051940.8821313237414521.0446749604243644
151950-03-01141138.571809410221360.9457101217738541.0759353773303926
161950-04-01135140.43005871002661.02383584883413640.9389519303129862
171950-05-01125142.302324406467681.09557477944840160.8017814291442694
181950-06-01149144.188606499544531.14170452509268520.9051104965020854
191950-07-01170146.088904989257171.14986465767454841.0120103680448629
201950-08-01170148.003219875605621.11786867625854971.0275121610285165
211950-09-01158149.93155115858991.05428987822614760.999548831409234
221950-10-01133151.873898838209950.97616415116586550.8971098897380081
231950-11-01114153.83026291446580.90442522055160020.8193895191545819
241950-12-01140155.800643387357470.85829547490731651.0469404170822558
251951-01-01145157.785040256884940.85013534232545351.0809711567907094
261951-02-01150159.783453523048220.88213132374145211.0642072459337848
271951-03-01178161.79588318584730.94571012177385431.1633074196327902
281951-04-01163163.822329245282161.02383584883413640.9718162929619643
291951-05-01172165.862791701352851.09557477944840160.9465366835517949
301951-06-01178167.917270554059341.1417045250926850.9284764947462371
311951-07-01199169.985765803401621.14986465767454841.0181078700924981
321951-08-01199172.06827744937971.117868676258551.034573805749251
331951-09-01184174.164805491993581.05428987822614741.0020684458681306
341951-10-01162176.275349931243260.97616415116586530.9414571857067755
351951-11-01146178.399910767128770.90442522055160010.9048686845208846
361951-12-01166180.538487999650040.85829547490731651.0712762190556668
371952-01-01171182.691081628807150.85013534232545351.1010085512464793
381952-02-01180184.857691654600050.8821313237414521.1038288371831717
391952-03-01193187.038318077028750.94571012177385381.0911103697915443
401952-04-01181189.232960896093261.02383584883413640.9342249424608229
411952-05-01183191.441620111793551.09557477944840120.872514599777301
421952-06-01218193.664295724129661.14170452509268520.9859461838993289
431952-07-01230195.900987733101541.14986465767454821.0210440635384188
441952-08-01242198.15169613870931.117868676258551.0925134312332445
451952-09-01209200.41642094095281.05428987822614740.9891290269877696
461952-10-01191202.695162139832120.97616415116586590.9653107208137566
471952-11-01172204.987919735347220.90442522055160010.9277426328027764
481952-12-01194207.294693727498130.85829547490731651.09037709971173
491953-01-01196209.615484116284880.85013534232545341.0998783148475715
501953-02-01196211.95029090170740.88213132374145221.0483077840576724
511953-03-01236214.299114083765720.94571012177385381.164484172557511
521953-04-01235216.661953662459841.02383584883413641.0593875759208187
531953-05-01229219.03880963778981.09557477944840160.9542724495189369
541953-06-01243221.42968200975551.14170452509268520.9612065351495415
551953-07-01264223.834570778357061.14986465767454821.025722867870263
561953-08-01272226.253475943594371.117868676258551.0754317574787666
571953-09-01237228.68639750546751.0542898782261480.9829874560971159
581953-10-01211231.13333546397650.97616415116586540.9351838843425918
591953-11-01180233.594289819121230.90442522055160020.8519961226601053
601953-12-01201236.069260570901750.85829547490731670.9920185537683209
611954-01-01204238.55824771931810.85013534232545341.005883443872492
621954-02-01188241.061251264370270.88213132374145190.8840914671548675
631954-03-01235243.578271206058220.94571012177385431.0201670241144885
641954-04-01227246.109307544381981.0238358488341360.900881122791179
651954-05-01234248.654360279341521.09557477944840160.8589695174222833
661954-06-01264251.21342941093691.14170452509268540.9204651543018308
671954-07-01302253.786514939168061.14986465767454841.0348840067489244
681954-08-01293256.373616864035061.11786867625854971.0223591847996678
691954-09-01259258.97473518553781.0542898782261480.9485982723701295
701954-10-01229261.589869903676340.97616415116586540.8967919598617543
711954-11-01203264.219021018450750.90442522055159990.8494920020889596
721954-12-01229266.862188529860930.85829547490731660.999796509368643
731955-01-01242269.51937243790690.85013534232545341.056178464429988
741955-02-01233272.190572742588640.88213132374145190.970397340910709
751955-03-01267274.87578944390620.94571012177385431.0271094723743175
761955-04-01269277.575022541859651.0238358488341360.9465456506916755
771955-05-01270280.28827203644881.09557477944840160.8792589787688203
781955-06-01315283.01553792767381.1417045250926850.9748696543086384
791955-07-01364285.75682021553461.14986465767454841.1077915634574516
801955-08-01347288.512118900031171.11786867625854971.0759067383181022
811955-09-01312291.281433981163561.0542898782261481.0159720326262758
821955-10-01274294.06476545893180.97616415116586650.9545193073350206
831955-11-01237296.862113333335740.90442522055159990.8827158132086902
841955-12-01278299.67347760437560.85829547490731661.0808356610777947
851956-01-01284302.498858272051170.85013534232545351.1043494746774531
861956-02-01277305.33825533636260.88213132374145181.0284076664752944
871956-03-01317308.19166879730980.94571012177385331.087627877134211
881956-04-01313311.05909865489281.0238358488341360.9828134615287868
891956-05-01318313.94054490911161.09557477944840140.9245655050780859
901956-06-01374316.83600755996621.14170452509268521.0339114566411551
911956-07-01413319.745486607456661.14986465767454841.1233081325473742
921956-08-01405322.66898205158291.11786867625855041.1228119270075365
931956-09-01355325.606493892344931.0542898782261481.0341302880578642
941956-10-01306328.55802212974270.97616415116586540.9540837445663306
951956-11-01271331.523566763776330.90442522055159990.9038205679728549
961956-12-01306334.50312779444580.85829547490731661.0658213623850472
971957-01-01315337.4967052217510.85013534232545331.0978751246298668
981957-02-01301340.504299045692050.88213132374145181.0020990544188302
991957-03-01356343.525909266268850.94571012177385431.0958029441106711
1001957-04-01348346.56153588348151.02383584883413680.9807731147580853
Rows: 1-100 | Columns: 5

We can see that there are now three new columns capturing the three elements of data.

Let’s visualize them.

Seasonality

decomposition["passengers_seasonal"].plot(ts = "date")

Trend

decomposition["passengers_trend"].plot(ts = "date")

Noise

decomposition["passengers_epsilon"].plot(ts = "date")

Note

Thanks to seasonal decomposition, we can effortlessly extract the residual, predict its values, and obtain crucial information necessary for computing the time series. Subsequently, by leveraging all the individual components, we are able to effectively recompose the time series.