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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: econmethods
3
- Version: 3
3
+ Version: 3.0.2
4
4
  Summary: A python package implementing various econometrical tests and estimators
5
5
  Home-page: https://github.com/NaturionBG/econmethods
6
6
  Author: NaturionBG
@@ -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.__mean_names = ['target_avg_l1']
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
- self.x_difs.append(f'x{i}_diff')
346
- self.__mean_names.append(f'x{i}_avg')
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
- means = mn.groupby('time')[['target'] + self.__l].mean()
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
- while True:
424
- flag = True
425
- for coef, t in zip(res_lr.params.index, res_lr.pvalues):
426
- if t > 0.05:
427
- lr_fe = lr_fe.drop(columns=[coef])
428
- flag=False
429
- if flag:
430
- break
431
- else:
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
- res_lr = sm.OLS(lr_fe.loc[:, 'target'], lr_fe.iloc[:, 3:]).fit()
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
- while True:
518
- ts = []
519
- flag = True
520
- if max(zip(est[0].params.index, est[0].pvalues), key=lambda x: x[1])[1] > 0.06:
521
- pool = pool.drop(columns=[max(zip(est[0].params.index, est[0].pvalues), key=lambda x: x[1])[0]])
522
- flag=False
523
- for i in self.x_difs:
524
- try:
525
- tmp = est[0].pvalues[i] <= 0.05
526
- if tmp:
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
- except Exception:
529
- ts.append(1)
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
- est[0] = sm.OLS(pool['target_diff'], pool.iloc[:, 3:]).fit()
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
- while True:
572
- ts = []
573
- flag = True
574
- if max(zip(res['coefs'].index, res['coefs']['T-pvalues']), key=lambda x: x[1])[1] > 0.06:
575
- self.__sr = self.build_sr([max(zip(res['coefs'].index, res['coefs']['T-pvalues']), key=lambda x: x[1])[0]])
576
- flag=False
577
- for i in self.x_difs:
578
- try:
579
- tmp = res['coefs']['T-pvalues'][i] <= 0.05
580
- if tmp:
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
- except Exception:
583
- ts.append(1)
584
- if flag or len(ts) == len(self.x_difs):
585
- break
586
- else:
587
- res = self.mg_algorithm()
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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: econmethods
3
- Version: 3
3
+ Version: 3.0.2
4
4
  Summary: A python package implementing various econometrical tests and estimators
5
5
  Home-page: https://github.com/NaturionBG/econmethods
6
6
  Author: NaturionBG
@@ -5,7 +5,7 @@ with open('README.md', 'r') as f:
5
5
 
6
6
  setup(
7
7
  name = 'econmethods',
8
- version = 'v3',
8
+ version = 'v3.0.2',
9
9
  description='A python package implementing various econometrical tests and estimators',
10
10
  packages = find_packages(),
11
11
  long_description=long_desc,
File without changes
File without changes