econmethods 3__tar.gz → 3.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {econmethods-3 → econmethods-3.0.2}/PKG-INFO +1 -1
- {econmethods-3 → econmethods-3.0.2}/econmethods/Lib.py +88 -56
- {econmethods-3 → econmethods-3.0.2}/econmethods.egg-info/PKG-INFO +1 -1
- {econmethods-3 → econmethods-3.0.2}/setup.py +1 -1
- {econmethods-3 → econmethods-3.0.2}/README.md +0 -0
- {econmethods-3 → econmethods-3.0.2}/econmethods/CADF_Crit_Values.xlsx +0 -0
- {econmethods-3 → econmethods-3.0.2}/econmethods/__init__.py +0 -0
- {econmethods-3 → econmethods-3.0.2}/econmethods.egg-info/SOURCES.txt +0 -0
- {econmethods-3 → econmethods-3.0.2}/econmethods.egg-info/dependency_links.txt +0 -0
- {econmethods-3 → econmethods-3.0.2}/econmethods.egg-info/requires.txt +0 -0
- {econmethods-3 → econmethods-3.0.2}/econmethods.egg-info/top_level.txt +0 -0
- {econmethods-3 → econmethods-3.0.2}/setup.cfg +0 -0
|
@@ -312,29 +312,42 @@ class FECM:
|
|
|
312
312
|
- *stat_vars*: a DataFrame of the same format as "df" - includes variables that will not be differenced and included into the ECM in their raw form. Ensure these variables are I(0). Defaults to None.
|
|
313
313
|
- *lr_const*: Specify whether the long-run model should have a constant.
|
|
314
314
|
- *autorem*: Enables/Disables automatic removal of insignificant regression coefficients in the ECM. Defaults to True.
|
|
315
|
+
- *remove_difs*: Remove the cointegration vector differences. Enabling this invalidates autorem. Passing a string removes that exact variable. Defaults to False.
|
|
316
|
+
- *remove_lr_ins*: Enables/Disables automatic removal of insignificant regression coefficients in the long-run model. Defaults to False.
|
|
317
|
+
- *sf*: Your significance level.
|
|
318
|
+
- *cont_means*: Enable/disable contemporanous means for the CCE- framework. Disabling will cause all means to be lagged by one period. Defaults to True.
|
|
315
319
|
----
|
|
316
320
|
RETURNS:
|
|
317
321
|
--
|
|
318
322
|
A python dictionary (dict) containing:
|
|
319
323
|
- Long-run estimation results | key = "lr_res"
|
|
320
324
|
- ECM (short-run) estimation results | key = "sr_res"\n
|
|
321
|
-
If a CCE- method is chosen:
|
|
322
|
-
- The AR(d) estimation results to forecast the cross-sectional mean | key = "ar"
|
|
323
325
|
'''
|
|
324
|
-
def __init__(self, df: pd.DataFrame, effects: str = 'rand', trend: int = 0, n_lags: int = 1, method: str = 'MG', coint: str | list[str] = 'x1', intercept: bool = True, stat_vars: pd.DataFrame|None = None, lr_const: bool = False, autorem: bool = True) -> None:
|
|
326
|
+
def __init__(self, df: pd.DataFrame, effects: str = 'rand', trend: int = 0, n_lags: int = 1, method: str = 'MG', coint: str | list[str] = 'x1', intercept: bool = True, stat_vars: pd.DataFrame|None = None, lr_const: bool = False, autorem: bool = True, remove_difs: bool | list[str] = False, remove_lr_ins: bool = False, sf: float = 0.05, cont_means: bool = True) -> None:
|
|
325
327
|
self.__df = df
|
|
326
328
|
self.__eff = effects.lower()
|
|
327
329
|
self.__t = trend
|
|
328
330
|
self.__autorem = autorem
|
|
329
331
|
self.__C = intercept
|
|
330
332
|
self.__lag = n_lags
|
|
333
|
+
self.sf = sf
|
|
331
334
|
self.x_difs = []
|
|
335
|
+
self.__cont_means = cont_means
|
|
336
|
+
if type(remove_difs) == bool:
|
|
337
|
+
self.__remove_difs = remove_difs
|
|
338
|
+
else:
|
|
339
|
+
self.x_difs = np.array(remove_difs)+'_diff'
|
|
340
|
+
self.__remove_difs = True
|
|
332
341
|
self.__method = method.lower()
|
|
333
342
|
self.__exog = len(df.columns[3:])
|
|
334
343
|
self.__l =[]
|
|
335
344
|
self.__stat_vars = stat_vars
|
|
336
|
-
self.
|
|
345
|
+
if not self.__cont_means:
|
|
346
|
+
self.__mean_names = ['target_avg_l1']
|
|
347
|
+
else:
|
|
348
|
+
self.__mean_names = ['target_avg']
|
|
337
349
|
self.__stat = []
|
|
350
|
+
self.__lr_autorem = remove_lr_ins
|
|
338
351
|
self.__lr_c = lr_const
|
|
339
352
|
if stat_vars is not None:
|
|
340
353
|
for i in range(1, len(stat_vars.columns[2:])+1):
|
|
@@ -342,8 +355,12 @@ class FECM:
|
|
|
342
355
|
self.__stat_vars.columns = ['SpUnit', 'time'] + self.__stat
|
|
343
356
|
for i in range(1, self.__exog+1):
|
|
344
357
|
self.__l.append(f'x{i}')
|
|
345
|
-
|
|
346
|
-
|
|
358
|
+
if type(remove_difs) == bool:
|
|
359
|
+
self.x_difs.append(f'x{i}_diff')
|
|
360
|
+
if self.__cont_means:
|
|
361
|
+
self.__mean_names.append(f'x{i}_avg')
|
|
362
|
+
else:
|
|
363
|
+
self.__mean_names.append(f'x{i}_avg_1')
|
|
347
364
|
self.__df.columns = ['SpUnit', 'time', 'target'] + self.__l
|
|
348
365
|
if isinstance(coint, list):
|
|
349
366
|
self.__lr_df = self.__df.copy(deep=True).loc[:, ['SpUnit', 'time', 'target', *coint]]
|
|
@@ -387,9 +404,11 @@ class FECM:
|
|
|
387
404
|
def build_means(self) -> pd.DataFrame:
|
|
388
405
|
mn = self.__df.copy(deep=True)
|
|
389
406
|
mn = mn.set_index('time')
|
|
390
|
-
|
|
407
|
+
if not self.__cont_means:
|
|
408
|
+
means = mn.groupby('time')[['target'] + self.__l].mean().shift(1)
|
|
409
|
+
else:
|
|
410
|
+
means = mn.groupby('time')[['target'] + self.__l].mean()
|
|
391
411
|
means.columns = self.__mean_names
|
|
392
|
-
means['target_avg_l1'] = means['target_avg_l1'].shift(1)
|
|
393
412
|
return means
|
|
394
413
|
|
|
395
414
|
def build_GLS(self, w_err: float) -> np.ndarray:
|
|
@@ -420,20 +439,20 @@ class FECM:
|
|
|
420
439
|
res_lr = sm.OLS(lr_fe.loc[:, 'target'], sm.add_constant(lr_fe.iloc[:, 3:])).fit()
|
|
421
440
|
else:
|
|
422
441
|
res_lr = sm.OLS(lr_fe.loc[:, 'target'], lr_fe.iloc[:, 3:]).fit()
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
if self.__lr_c:
|
|
433
|
-
res_lr = sm.OLS(lr_fe.loc[:, 'target'], sm.add_constant(lr_fe.iloc[:, 3:])).fit()
|
|
442
|
+
if self.__lr_autorem:
|
|
443
|
+
while True:
|
|
444
|
+
flag = True
|
|
445
|
+
for coef, t in zip(res_lr.params.index, res_lr.pvalues):
|
|
446
|
+
if t > self.sf:
|
|
447
|
+
lr_fe = lr_fe.drop(columns=[coef])
|
|
448
|
+
flag=False
|
|
449
|
+
if flag:
|
|
450
|
+
break
|
|
434
451
|
else:
|
|
435
|
-
|
|
436
|
-
|
|
452
|
+
if self.__lr_c:
|
|
453
|
+
res_lr = sm.OLS(lr_fe.loc[:, 'target'], sm.add_constant(lr_fe.iloc[:, 3:])).fit()
|
|
454
|
+
else:
|
|
455
|
+
res_lr = sm.OLS(lr_fe.loc[:, 'target'], lr_fe.iloc[:, 3:]).fit()
|
|
437
456
|
return res_lr
|
|
438
457
|
else:
|
|
439
458
|
lr_fe = self.build_FE()
|
|
@@ -514,27 +533,34 @@ class FECM:
|
|
|
514
533
|
else:
|
|
515
534
|
est.append(sm.OLS(pool['target_diff'], pool.iloc[:, 3:]).fit())
|
|
516
535
|
if self.__autorem:
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
536
|
+
if not self.__remove_difs:
|
|
537
|
+
while True:
|
|
538
|
+
ts = []
|
|
539
|
+
flag = True
|
|
540
|
+
if max(zip(est[0].params.index, est[0].pvalues), key=lambda x: x[1])[1] > self.sf:
|
|
541
|
+
pool = pool.drop(columns=[max(zip(est[0].params.index, est[0].pvalues), key=lambda x: x[1])[0]])
|
|
542
|
+
flag=False
|
|
543
|
+
for i in self.x_difs:
|
|
544
|
+
try:
|
|
545
|
+
tmp = est[0].pvalues[i] <= self.sf
|
|
546
|
+
if tmp:
|
|
547
|
+
ts.append(1)
|
|
548
|
+
except Exception:
|
|
527
549
|
ts.append(1)
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
if flag or len(ts) == len(self.x_difs):
|
|
532
|
-
break
|
|
533
|
-
else:
|
|
534
|
-
if self.__C:
|
|
535
|
-
est[0] = sm.OLS(pool['target_diff'], sm.add_constant(pool.iloc[:, 3:])).fit()
|
|
550
|
+
|
|
551
|
+
if flag or len(ts) == len(self.x_difs):
|
|
552
|
+
break
|
|
536
553
|
else:
|
|
537
|
-
|
|
554
|
+
if self.__C:
|
|
555
|
+
est[0] = sm.OLS(pool['target_diff'], sm.add_constant(pool.iloc[:, 3:])).fit()
|
|
556
|
+
else:
|
|
557
|
+
est[0] = sm.OLS(pool['target_diff'], pool.iloc[:, 3:]).fit()
|
|
558
|
+
else:
|
|
559
|
+
pool = pool.drop(columns=[*self.x_difs])
|
|
560
|
+
if self.__C:
|
|
561
|
+
est[0] = sm.OLS(pool['target_diff'], sm.add_constant(pool.iloc[:, 3:])).fit()
|
|
562
|
+
else:
|
|
563
|
+
est[0] = sm.OLS(pool['target_diff'], pool.iloc[:, 3:]).fit()
|
|
538
564
|
return est
|
|
539
565
|
|
|
540
566
|
def mg_algorithm(self) -> pd.DataFrame:
|
|
@@ -568,29 +594,35 @@ class FECM:
|
|
|
568
594
|
dct['lr_res'] = self.__lr
|
|
569
595
|
res = self.mg_algorithm()
|
|
570
596
|
if self.__autorem:
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
597
|
+
if not self.__remove_difs:
|
|
598
|
+
while True:
|
|
599
|
+
ts = []
|
|
600
|
+
flag = True
|
|
601
|
+
if max(zip(res['coefs'].index, res['coefs']['T-pvalues']), key=lambda x: x[1])[1] > self.sf:
|
|
602
|
+
self.__sr = self.build_sr([max(zip(res['coefs'].index, res['coefs']['T-pvalues']), key=lambda x: x[1])[0]])
|
|
603
|
+
flag=False
|
|
604
|
+
for i in self.x_difs:
|
|
605
|
+
try:
|
|
606
|
+
tmp = res['coefs']['T-pvalues'][i] <= 0.1
|
|
607
|
+
if tmp:
|
|
608
|
+
ts.append(1)
|
|
609
|
+
except Exception:
|
|
581
610
|
ts.append(1)
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
611
|
+
if flag or len(ts) == len(self.x_difs):
|
|
612
|
+
break
|
|
613
|
+
else:
|
|
614
|
+
res = self.mg_algorithm()
|
|
615
|
+
else:
|
|
616
|
+
self.__sr = self.build_sr(self.x_difs)
|
|
617
|
+
res = self.mg_algorithm()
|
|
618
|
+
|
|
588
619
|
|
|
589
620
|
dct['sr_res'] = res
|
|
590
621
|
return dct
|
|
591
622
|
|
|
592
623
|
def __del__(self) -> None:
|
|
593
624
|
pass
|
|
625
|
+
|
|
594
626
|
class CDTwoWay:
|
|
595
627
|
'''
|
|
596
628
|
Implementation of the CD test to validate/reject cross-sectional dependence.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|