Loading...

verticapy.machine_learning.model_selection.statistical_tests.tsa.seasonal_decompose

verticapy.machine_learning.model_selection.statistical_tests.tsa.seasonal_decompose(input_relation: Annotated[str | vDataFrame, ''], 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) vDataFrame

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 vDataColumn to decompose.

ts: str

Time series vDataColumn used to order the data. It can be of type date or a numerical vDataColumn.

by: SQLColumns, optional

vDataColumn 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

vDataFrame

object containing the different time series components.

Examples

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

import verticapy.datasets as vpd

data = vpd.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

VerticaPy 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 VerticaPy 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 verticapy.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
Float(22)
123
passengers_seasonal
Float(22)
123
passengers_epsilon
Float(22)
11949-01-01112114.0280408597070.8501353423254531.15536255667615
21949-02-01118115.6900606066110.8821313237414511.15625258033391
31949-03-01132117.3660967501510.9457101217738531.1892501982632
41949-04-01129119.0561492903271.023835848834141.05829695500561
51949-05-01121120.7602182271381.09557477944840.914575272399499
61949-06-01135122.4783035605851.141704525092680.96543020834606
71949-07-01148124.2104052906681.149864657674551.0362320267397
81949-08-01148125.9565234173871.117868676258551.05111507184691
91949-09-01136127.7166579407421.054289878226151.01002314478973
101949-10-01119129.4908088607320.9761641511658650.941423777800471
111949-11-01104131.2789761773580.90442522055160.875922119767362
121949-12-01118133.081159890620.8582954749073161.03306728138343
131950-01-01115134.8973600005180.8501353423254531.00278151254438
141950-02-01126136.7275765070520.8821313237414511.04467496042437
151950-03-01141138.5718094102210.9457101217738531.07593537733039
161950-04-01135140.4300587100271.023835848834140.938951930312987
171950-05-01125142.3023244064681.09557477944840.80178142914427
181950-06-01149144.1886064995451.141704525092680.905110496502086
191950-07-01170146.0889049892571.149864657674551.01201036804486
201950-08-01170148.0032198756061.117868676258551.02751216102852
211950-09-01158149.931551158591.054289878226150.999548831409235
221950-10-01133151.873898838210.9761641511658650.897109889738009
231950-11-01114153.8302629144660.90442522055160.819389519154582
241950-12-01140155.8006433873570.8582954749073161.04694041708226
251951-01-01145157.7850402568850.8501353423254531.08097115679071
261951-02-01150159.7834535230480.8821313237414511.06420724593379
271951-03-01178161.7958831858470.9457101217738531.16330741963279
281951-04-01163163.8223292452821.023835848834140.971816292961965
291951-05-01172165.8627917013531.09557477944840.946536683551796
301951-06-01178167.9172705540591.141704525092680.928476494746238
311951-07-01199169.9857658034021.149864657674551.0181078700925
321951-08-01199172.068277449381.117868676258551.03457380574925
331951-09-01184174.1648054919941.054289878226151.00206844586813
341951-10-01162176.2753499312430.9761641511658650.941457185706776
351951-11-01146178.3999107671290.90442522055160.904868684520885
361951-12-01166180.538487999650.8582954749073161.07127621905567
371952-01-01171182.6910816288070.8501353423254531.10100855124648
381952-02-01180184.85769165460.8821313237414511.10382883718317
391952-03-01193187.0383180770290.9457101217738531.09111036979155
401952-04-01181189.2329608960931.023835848834140.934224942460824
411952-05-01183191.4416201117931.09557477944840.872514599777302
421952-06-01218193.664295724131.141704525092680.98594618389933
431952-07-01230195.9009877331021.149864657674551.02104406353842
441952-08-01242198.1516961387091.117868676258551.09251343123325
451952-09-01209200.4164209409531.054289878226150.989129026987771
461952-10-01191202.6951621398320.9761641511658650.965310720813758
471952-11-01172204.9879197353470.90442522055160.927742632802777
481952-12-01194207.2946937274980.8582954749073161.09037709971173
491953-01-01196209.6154841162850.8501353423254531.09987831484757
501953-02-01196211.9502909017070.8821313237414511.04830778405767
511953-03-01236214.2991140837660.9457101217738531.16448417255751
521953-04-01235216.661953662461.023835848834141.05938757592082
531953-05-01229219.038809637791.09557477944840.954272449518939
541953-06-01243221.4296820097551.141704525092680.961206535149543
551953-07-01264223.8345707783571.149864657674551.02572286787026
561953-08-01272226.2534759435941.117868676258551.07543175747877
571953-09-01237228.6863975054671.054289878226150.982987456097118
581953-10-01211231.1333354639760.9761641511658650.935183884342593
591953-11-01180233.5942898191210.90442522055160.851996122660106
601953-12-01201236.0692605709020.8582954749073160.992018553768322
611954-01-01204238.5582477193180.8501353423254531.00588344387249
621954-02-01188241.061251264370.8821313237414510.884091467154869
631954-03-01235243.5782712060580.9457101217738531.02016702411449
641954-04-01227246.1093075443821.023835848834140.90088112279118
651954-05-01234248.6543602793411.09557477944840.858969517422285
661954-06-01264251.2134294109371.141704525092680.920465154301832
671954-07-01302253.7865149391681.149864657674551.03488400674893
681954-08-01293256.3736168640351.117868676258551.02235918479967
691954-09-01259258.9747351855381.054289878226150.948598272370131
701954-10-01229261.5898699036760.9761641511658650.896791959861755
711954-11-01203264.2190210184510.9044252205515990.849492002088961
721954-12-01229266.8621885298610.8582954749073160.999796509368645
731955-01-01242269.5193724379070.8501353423254531.05617846442999
741955-02-01233272.1905727425880.8821313237414510.970397340910711
751955-03-01267274.8757894439060.9457101217738531.02710947237432
761955-04-01269277.5750225418591.023835848834140.946545650691677
771955-05-01270280.2882720364491.09557477944840.879258978768822
781955-06-01315283.0155379276741.141704525092680.97486965430864
791955-07-01364285.7568202155341.149864657674551.10779156345745
801955-08-01347288.5121189000311.117868676258551.0759067383181
811955-09-01312291.2814339811631.054289878226151.01597203262628
821955-10-01274294.0647654589320.9761641511658650.954519307335023
831955-11-01237296.8621133333360.9044252205515990.882715813208691
841955-12-01278299.6734776043750.8582954749073161.0808356610778
851956-01-01284302.4988582720510.8501353423254531.10434947467745
861956-02-01277305.3382553363620.8821313237414511.0284076664753
871956-03-01317308.191668797310.9457101217738531.08762787713421
881956-04-01313311.0590986548931.023835848834140.982813461528788
891956-05-01318313.9405449091111.09557477944840.924565505078088
901956-06-01374316.8360075599661.141704525092681.03391145664116
911956-07-01413319.7454866074561.149864657674551.12330813254738
921956-08-01405322.6689820515831.117868676258551.12281192700754
931956-09-01355325.6064938923451.054289878226151.03413028805787
941956-10-01306328.5580221297420.9761641511658650.954083744566332
951956-11-01271331.5235667637760.9044252205515990.903820567972856
961956-12-01306334.5031277944460.8582954749073161.06582136238505
971957-01-01315337.4967052217510.8501353423254531.09787512462987
981957-02-01301340.5042990456920.8821313237414511.00209905441883
991957-03-01356343.5259092662690.9457101217738531.09580294411067
1001957-04-01348346.5615358834811.023835848834140.980773114758087
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.