4
$\begingroup$

I have daily time series data of a shop's revenue. Now I would like to test for seasonal differencing with the OCSB test originally intrduced in (Osborn et al. (1988): Seasonality and the Order of Integration for Consumption).

This test is explained in more detail in (Ghysels, Osborn (2001): The Econometic Analysis of Seasonal Time Series, p. 66ff.). There they discuss the test regression

$$\Delta_1 \Delta_S y_t = \beta_1 \Delta_S y_{t-1} + \beta_2\Delta_1 y_{t-S} + \epsilon_t, \quad \quad t=1,...,T.$$

Where $S$ is the seasonality, $y_t$ is the time series values and $\Delta$ the difference operator.

By using $\Delta_S = \Delta_1(1+L+...+L^{S-1})$ and $S(L)=\sum_{i=0}^{S-1}L^i$ they get:

$$\Delta_1 \Delta_S y_t = \beta_1 (1+L+...+L^{S-1}) \Delta_1 y_{t-1} + \beta_2\Delta_1 y_{t-S} + \epsilon_t=$$ $$\Delta_1 \Delta_S y_t = \beta_1 S(L) \Delta_1 y_{t-1} + \beta_2\Delta_1 y_{t-S} + \epsilon_t.$$

Note that now all variables impose a first difference $\Delta_1$. When only interested in testing seasonal differencing (Osborn, Rodriguez (2002): Asymptotic Distributions Of Seasonal Unit Root Tests: A Unifying Approach, p.224) tell us that we can simplify to:

$$\Delta_S y_t = \beta_1 S(L) y_{t-1} + \beta_2 y_{t-S} + u_t=$$ $$\Delta_S y_t = \beta_1 (y_{t-1}+y_{t-2}+...+y_{t-S}) + \beta_2 y_{t-S} + u_t=$$ $$\Delta_S y_t = \beta_1 (y_{t-1}+y_{t-2}+...+y_{t-S+1}) + (\beta_1+\beta_2) y_{t-S} + u_t.$$

The test has null hypothesis $H_0: \beta_1 = \beta_2= 0$ and we have to test the regression model's F-Statistics $F(\hat{\beta}_1\cap\hat\beta_2)$ and t-Statistics $t(\hat\beta_1), t(\hat\beta_2)$.

The critical test values are given in (Osborn et al. (1988): Seasonality and the Order of Integration for Consumption, Appendix 1).

So far the theory. I would like to implement this in python using the following code:

import statsmodels as sm

def OCSBtest(timeseries, period):

# timeseries is a pandas dataframe, period is an integer

# calculate Δ_s y_t:
diff_series = timeseries.diff(period)
# calculate S(l)y_t-1:
y1 = timeseries.shift(1).rolling(window=(period-1)).sum()
# calculate y_(t-s):
y2 = timeseries.shift(period)
# Ensure all arrays have same length:
y1 = y1[period:]
y2 = y2[period:]
diff_series = diff_series[(period):]
# construct regression matrix:
x_reg = np.c_[y1, y2]

# build regression model and fit:
try: 
    res = sm.regression.linear_model.OLS(diff_series, sm.add_constant(x_reg)).fit()
except:
    # In case of numerical problems treat residuals as AR(1) model:
    res = sm.tsa.statespace.SARIMAX(diff_series, x_reg, order=(1,0,0), seasonal_order=(0,0,0,period), trend='c').fit()

# Check t-statistics of the results 
if (res.tvalues[1] < -1.94) or (res.tvalues[2] < -1.93):
    D = 0
else:
    D = 1

return D

To be honest I am not very confident I understood the test properly. However I tried to follow all of their steps.

My quesitons are:

  1. Does anyone have any intution for the original approach of the OCSB test? Why do we build this particular regression model and what is an interpretation for $\beta_1\neq0$ or $\beta_2 \neq 0$?
  2. Is there anyone who has experience with the implementation of this test and can give me feedback on my piece of code?

My seasonality is $S=7$ and my data is:

[   0, 5530, 4327, 4486, 4997,    0, 7176, 5580, 5471, 4892, 4881,
   4952,    0, 4717, 3900, 4008, 4044, 4127, 5182,    0, 5394, 5720,
   5578, 5195, 5586, 5598,    0, 4055, 3725, 4601, 4709, 5633, 5970,
      0, 7032, 6049, 6140, 5499, 5681, 5370,    0, 4409, 4015, 4252,
   4241, 4809, 6154,    0, 6407, 5386, 5660, 5261, 5000, 5237,    0,
   4038, 3794, 4558, 4676, 4611, 5350,    0, 7675, 6300, 5973, 5637,
   5853, 5578,    0, 4949, 3853, 4341, 5108, 4925, 5003,    0, 7072,
   6563, 5598, 5179, 5506, 5603,    0, 6729, 6686, 6660, 7285,    0,
   7132,    0,    0, 5484, 4625, 4293, 4390, 5075,    0, 6046, 5514,
   4903, 4366, 5263, 4773,    0, 3941, 3357, 3649, 2952, 4303, 4350,
      0, 5672, 4492, 4309, 3939, 4841, 5726,    0, 5821, 5925,    0,
   6486, 6027, 5912,    0, 4568, 4624, 5230,    0, 5409, 5064,    0,
   6106, 5083, 4790, 4448, 4856, 4413,    0,    0, 4789, 3559, 4030,
   4232, 4408,    0, 5519, 4995, 5784,    0, 7893, 5693,    0, 5422,
   5220, 5012, 4881, 4315, 4262,    0, 4291, 3784, 3563, 3661, 4150,
   4766,    0, 5337, 4633, 4180, 5011, 5173, 4341,    0, 3549, 4536,
   3827, 3593, 4407, 5804,    0, 5614, 5868, 5875, 5701, 4986, 4090,
      0, 4475, 4047, 3963, 4053, 4134, 4015,    0, 6377, 5643, 4946,
   4904, 4275, 4421,    0, 4149, 3937, 3849, 3502, 4221, 3352,    0,
   6290, 5773, 5572, 4994, 4494, 4461,    0, 4086, 3582, 4143, 3680,
   3257, 3768,    0, 5326, 5299, 4724, 4575, 4552, 4078,    0, 3954,
   3492, 3096, 3703, 3493, 4752,    0, 5482, 5156, 4583, 4804, 5469,
   5317,    0, 4195, 3928, 3343, 3883, 3836, 4663,    0, 5893, 5403,
   5103, 5079, 5187, 4663,    0, 4249, 3685, 3946, 3717, 3516, 3909,
      0, 4770, 4274, 4212, 4481, 4669, 4175,    0, 4088, 4178, 4001,
      0, 5447, 5352,    0, 6004, 5452, 4957, 5286, 5896, 4954,    0,
   3773, 3355, 3383, 3223, 3566, 3667,    0, 5055, 4068, 4612, 4044,
   4573, 4347,    0, 3557, 3737, 4119, 4035, 5471, 5233,    0, 5861,
   5348, 5572, 5009, 5242, 4681,    0, 4014, 4017, 3310, 3795, 4016,
   5071,    0, 5927, 5143, 4936, 4623, 5058, 5297,    0, 4600, 4201,
   4561, 4614, 5623, 6373,    0, 6943, 6293, 5688, 5691, 6112, 5799,
      0, 4462, 4228, 4301, 4074, 4703, 6119,    0, 8277, 7356, 7821,
   6788, 8414, 8043,    0, 9528, 3204,    0,    0, 6110, 5659,    0,
   7193, 2362,    0, 4969, 4190, 5173,    0, 6194, 5539, 4931, 4396,
   4486, 4454,    0, 3994, 3621, 3776, 3632, 3803, 5128,    0, 6148,
   5151, 4562, 4597, 5098, 4546,    0, 3601, 3581, 3789, 4549, 4906,
   5292,    0, 5839, 5540, 5031, 4823, 5596, 4726,    0, 3591, 3461,
   3662, 3638, 4647, 5258,    0, 5393, 4602, 4935, 4563, 4727, 4820,
      0, 3826, 3499, 4108, 3826, 5031, 5307,    0, 6198, 5397, 4916,
   4585, 5340, 4400,    0, 3573, 3588, 3805, 3224, 3883, 5042,    0,
   5563, 5200, 4036, 4023, 4510, 5241,    0, 3850, 3136, 3225, 3847,
   3987, 4583,    0, 6008, 5407, 4834, 4556, 4738, 4599,    0, 3751,
   3990, 3919, 3948, 3733, 4435,    0, 6803, 6196, 6381, 6160,    0,
   5871,    0,    0, 4133, 3482, 3376, 3710, 3989,    0, 5488, 5923,
   5870,    0, 6790, 5498,    0, 5325, 5055, 5075, 5222, 5237, 5996,
      0, 3957, 3535, 3710, 3589, 3863, 3785,    0, 5637, 4909, 4298,
   3801, 4968, 4574,    0, 3912, 3971, 4616,    0, 5407, 4985,    0,
   6071, 5614, 5229, 5278, 4957, 4923,    0,    0, 4607, 4207, 3702,
   3901, 4757,    0, 4788, 5770, 5256,    0, 6362, 3982,    0, 3755,
   3351, 3727, 2749, 4019, 5162,    0, 5738, 5138, 5161, 4756, 4025,
   5176,    0, 4223, 4081, 3547, 3741, 4020, 4409,    0, 5083, 5298,
   5424, 4685, 4609, 4084,    0, 4507, 3933, 3806, 3557, 3858, 4266,
      0, 5372, 5252, 5487, 5106, 5038, 4731,    0, 5655, 5433, 5337,
   4154, 4451, 4174,    0, 3886, 3185, 4069, 3906, 3746, 4425,    0,
   5623, 5190, 4280, 4327, 3971, 3582,    0, 3414, 3396, 3148, 3920,
   3869, 4094,    0, 5464, 5008, 4978, 4346, 4706, 3959,    0, 3676,
   3377, 3275, 3392, 3906, 4270,    0, 4611, 4381, 4383, 3740, 4128,
   3911,    0, 3982, 3407, 3405, 2462, 3518, 3914,    0, 5280, 4919,
   4712, 5400,    0, 5355,    0, 5402, 5439, 4944, 4654, 4396, 4743,
      0, 4033, 3681, 3775, 3430, 3720, 4169,    0, 4652, 4678, 4868,
   4466, 4196, 4596,    0, 4260, 3364, 3761, 4042, 4161, 6532,    0,
   5857, 5253, 4838, 4785, 5220, 4720,    0, 5474, 4479, 4677, 3869,
   5334, 4967,    0, 3582, 3890, 3894, 4963, 4594, 5849,    0, 6527,
   5815, 5328, 6144, 7195, 7066,    0, 7380, 6467, 6454, 7016, 6207,
   6185,    0, 4884, 4345, 4915, 5060, 5269, 7263,    0, 8069, 7739,
   7523, 7785, 7558, 8367,    0, 9331, 7959, 3659,    0,    0, 6057,
      0, 6463, 6466, 2605,    0, 5509, 5023,    0, 6239, 4574, 4796,
   3716, 3998, 4599,    0, 5346, 4924, 4541, 4295, 4161, 5255,    0,
   3721, 3680, 3299, 3492, 3586, 4840,    0, 4781, 4806, 4310, 5171,
   5577, 5363,    0, 6038, 4901, 4672, 4394, 5022, 4663,    0, 3965,
   3136, 3735, 3900, 4726, 5015,    0, 4303, 4833, 4180, 4460, 4651,
   4475,    0, 3598, 4054, 3875, 4042, 4708, 5289,    0, 5942, 5451,
   5568, 4419, 5397, 4592,    0, 3701, 3805, 4170, 3141, 3725, 5225,
      0, 5695, 4806, 3858, 4748, 4057, 3909,    0, 3565, 3547, 3531,
   3932, 4005, 5208,    0, 6714, 6206, 6816, 6574,    0, 6709,    0,
      0, 4163, 4194, 3467, 3549, 4173,    0, 5377, 4648, 4110, 4116,
   4718, 4594,    0, 3722, 3037, 3319, 3076, 3198, 4318,    0, 5575,
   5199, 5775, 6228,    0, 5850,    0, 5591, 4564, 4960, 4529, 4683,
   4945,    0, 3551, 3547, 3998,    0, 4178, 4431,    0, 5280, 5235,
   4735, 3755, 4459, 4276,    0,    0, 4211, 4083, 4111, 4656, 5592,
      0, 5774, 5450, 5809,    0, 5384, 4183,    0, 4071, 4102, 3591,
   3627, 3695, 4256,    0, 5518, 4852, 4000, 4645, 4202, 4097,    0,
   3846, 3762, 3346, 3533, 3317, 4019,    0, 5197, 5735, 5223, 5558,
   4665, 4797,    0, 4359, 3650, 3797, 3897, 3808, 3530,    0, 5054,
   5042, 4767, 4427, 4852, 4406,    0, 4395, 3558, 3464, 3769, 3706,
   4364,    0, 6102, 5011, 4782, 5020, 5263]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.