makystats 0.1.0__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.
- makystats-0.1.0/LICENSE +21 -0
- makystats-0.1.0/MANIFEST.in +21 -0
- makystats-0.1.0/PKG-INFO +528 -0
- makystats-0.1.0/README.fr.md +365 -0
- makystats-0.1.0/README.md +495 -0
- makystats-0.1.0/docs/_preview_logo.png +0 -0
- makystats-0.1.0/docs/_preview_logo_bottom.png +0 -0
- makystats-0.1.0/docs/formulas/aparch.png +0 -0
- makystats-0.1.0/docs/formulas/backcast.png +0 -0
- makystats-0.1.0/docs/formulas/egarch.png +0 -0
- makystats-0.1.0/docs/formulas/egarch_raw_display.png +0 -0
- makystats-0.1.0/docs/formulas/eps_sigma_z.png +0 -0
- makystats-0.1.0/docs/formulas/figarch.png +0 -0
- makystats-0.1.0/docs/formulas/garch.png +0 -0
- makystats-0.1.0/docs/formulas/gjr.png +0 -0
- makystats-0.1.0/docs/formulas/loglik.png +0 -0
- makystats-0.1.0/docs/formulas/mean.png +0 -0
- makystats-0.1.0/docs/formulas/mse_vecm.png +0 -0
- makystats-0.1.0/docs/formulas/normal_dens.png +0 -0
- makystats-0.1.0/docs/formulas/nu_map.png +0 -0
- makystats-0.1.0/docs/formulas/omega_map.png +0 -0
- makystats-0.1.0/docs/formulas/omega_raw.png +0 -0
- makystats-0.1.0/docs/formulas/opg.png +0 -0
- makystats-0.1.0/docs/formulas/phi_map.png +0 -0
- makystats-0.1.0/docs/logo_makystats.png +0 -0
- makystats-0.1.0/docs/var_plot_custom.png +0 -0
- makystats-0.1.0/docs/var_plot_fhs.png +0 -0
- makystats-0.1.0/docs/var_plot_loss.png +0 -0
- makystats-0.1.0/docs/var_plot_lower.png +0 -0
- makystats-0.1.0/makystats/__init__.py +77 -0
- makystats-0.1.0/makystats/armagarch/DOCUMENTATION.md +649 -0
- makystats-0.1.0/makystats/armagarch/README.md +187 -0
- makystats-0.1.0/makystats/armagarch/__init__.py +40 -0
- makystats-0.1.0/makystats/armagarch/core.py +4644 -0
- makystats-0.1.0/makystats/logo_makystats.png +0 -0
- makystats-0.1.0/makystats/py.typed +0 -0
- makystats-0.1.0/makystats/vecm/DOCUMENTATION.md +618 -0
- makystats-0.1.0/makystats/vecm/README.md +107 -0
- makystats-0.1.0/makystats/vecm/__init__.py +68 -0
- makystats-0.1.0/makystats/vecm/core.py +268 -0
- makystats-0.1.0/makystats/vecm/mhm_tables.npz +0 -0
- makystats-0.1.0/makystats/vecm/vecm_model.py +1970 -0
- makystats-0.1.0/makystats.egg-info/PKG-INFO +528 -0
- makystats-0.1.0/makystats.egg-info/SOURCES.txt +48 -0
- makystats-0.1.0/makystats.egg-info/dependency_links.txt +1 -0
- makystats-0.1.0/makystats.egg-info/requires.txt +19 -0
- makystats-0.1.0/makystats.egg-info/top_level.txt +1 -0
- makystats-0.1.0/pyproject.toml +74 -0
- makystats-0.1.0/setup.cfg +4 -0
- makystats-0.1.0/setup.py +1 -0
makystats-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 makystats contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
include LICENSE
|
|
2
|
+
include README.md
|
|
3
|
+
include README.fr.md
|
|
4
|
+
include pyproject.toml
|
|
5
|
+
recursive-include makystats *.py
|
|
6
|
+
recursive-include makystats *.npz
|
|
7
|
+
recursive-include makystats *.npzi
|
|
8
|
+
recursive-include makystats *.npzc
|
|
9
|
+
recursive-include makystats/vecm *.npz
|
|
10
|
+
recursive-include makystats/armagarch *.md
|
|
11
|
+
recursive-include makystats/vecm *.md
|
|
12
|
+
global-exclude __pycache__
|
|
13
|
+
global-exclude *.py[cod]
|
|
14
|
+
global-exclude .pytest_cache
|
|
15
|
+
prune tests
|
|
16
|
+
prune examples
|
|
17
|
+
prune armagarch
|
|
18
|
+
exclude armagarch6.py
|
|
19
|
+
recursive-include docs/formulas *.png
|
|
20
|
+
recursive-include makystats *.png
|
|
21
|
+
recursive-include docs *.png
|
makystats-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: makystats
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Econometrics toolkit: ARMA-GARCH family (MLE) and VECM / Johansen cointegration
|
|
5
|
+
Author-email: ANDRIANITOVIANA Lauret Boris <boris.andrianitoviana@gmail.com>
|
|
6
|
+
Maintainer-email: ANDRIANITOVIANA Lauret Boris <boris.andrianitoviana@gmail.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/Lauret-Boris/makystats
|
|
9
|
+
Project-URL: Documentation, https://github.com/Lauret-Boris/makystats
|
|
10
|
+
Project-URL: Repository, https://github.com/Lauret-Boris/makystats
|
|
11
|
+
Project-URL: Issues, https://github.com/Lauret-Boris/makystats/issues
|
|
12
|
+
Keywords: garch,arma,arch,volatility,time-series,econometrics,maximum-likelihood,vecm,cointegration
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
25
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
26
|
+
Requires-Python: >=3.9
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
Provides-Extra: all
|
|
29
|
+
Provides-Extra: numba
|
|
30
|
+
Provides-Extra: plot
|
|
31
|
+
Provides-Extra: test
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
|
|
34
|
+
<p align="center">
|
|
35
|
+
<img src="https://raw.githubusercontent.com/Lauret-Boris/makystats/main/docs/logo_makystats.png" alt="makystats logo" width="280"/>
|
|
36
|
+
</p>
|
|
37
|
+
|
|
38
|
+
# makystats
|
|
39
|
+
|
|
40
|
+
**Econometrics toolkit for Python** — ARMA–GARCH family models (maximum likelihood) and VECM / Johansen cointegration.
|
|
41
|
+
|
|
42
|
+
| | |
|
|
43
|
+
|---|---|
|
|
44
|
+
| **Install** | `pip install makystats` |
|
|
45
|
+
| **Python** | >= 3.9 (tested up to 3.14) |
|
|
46
|
+
| **License** | MIT |
|
|
47
|
+
| **Version** | 0.1.0 |
|
|
48
|
+
|
|
49
|
+
Full technical guides: [VECM](https://github.com/Lauret-Boris/makystats/blob/main/makystats/vecm/DOCUMENTATION.md), [ARMA-GARCH](https://github.com/Lauret-Boris/makystats/blob/main/makystats/armagarch/DOCUMENTATION.md) (also shipped inside the installed package).
|
|
50
|
+
|
|
51
|
+
*Version française :* [`README.fr.md`](https://github.com/Lauret-Boris/makystats/blob/main/README.fr.md).
|
|
52
|
+
|
|
53
|
+
**Contact**
|
|
54
|
+
|
|
55
|
+
- **Author:** ANDRIANITOVIANA Lauret Boris
|
|
56
|
+
- **E-mail:** boris.andrianitoviana@gmail.com
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Features
|
|
63
|
+
|
|
64
|
+
**Volatility (`makystats.armagarch`)**
|
|
65
|
+
|
|
66
|
+
- Joint MLE of mean (ARMA / ARX / HAR) and variance (GARCH, GJR/TARCH, EGARCH, APARCH, FIGARCH)
|
|
67
|
+
- Innovations: normal, Student-t, Hansen skew-t, GED
|
|
68
|
+
- Deterministic terms in the mean: `trend` = `n` / `c` / `t` / `ct` (constant and/or linear trend)
|
|
69
|
+
- Unconstrained BFGS on a reparameterised space; OPG standard errors by default
|
|
70
|
+
- Fluent API in the spirit of [arch](https://github.com/bashtage/arch)
|
|
71
|
+
|
|
72
|
+
**Cointegration (`makystats.vecm`)**
|
|
73
|
+
|
|
74
|
+
- Johansen reduced-rank regression and rank tests
|
|
75
|
+
- Eight deterministic cases, including **JHJ** dual constant/trend (`3a`, `4a`, `5a`)
|
|
76
|
+
- MacKinnon–Haug–Michelis (1999) continuous p-values and critical values
|
|
77
|
+
- Native multi-step forecasts with confidence intervals (`analytic` / `bootstrap` / `gaussian`)
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Installation
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pip install makystats
|
|
85
|
+
pip install "makystats[numba]" # optional JIT for the GARCH filter
|
|
86
|
+
pip install "makystats[plot]" # matplotlib (diagnostics & forecast plots)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
From a source checkout:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
pip install -e ".[numba,plot,test]"
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Documentation on this page
|
|
98
|
+
|
|
99
|
+
Table of contents:
|
|
100
|
+
|
|
101
|
+
| Section | Content |
|
|
102
|
+
|---------|---------|
|
|
103
|
+
| [Quick start — VECM](#quick-start--vecm) | Rank test, estimation, forecast |
|
|
104
|
+
| [VECM reference](#vecm-reference) | Parameters, API, JHJ, forecasts, diagnostics |
|
|
105
|
+
| [Quick start — ARMA–GARCH](#quick-start--arma-garch) | Fit, order selection |
|
|
106
|
+
| [§1–§8](#1-statistical-model) | Full ARMA–GARCH technical guide |
|
|
107
|
+
|
|
108
|
+
After install, extended markdown guides are also shipped **inside the package**:
|
|
109
|
+
|
|
110
|
+
```text
|
|
111
|
+
python -c "import makystats.vecm, pathlib; print(pathlib.Path(makystats.vecm.__file__).parent / 'DOCUMENTATION.md')"
|
|
112
|
+
python -c "import makystats.armagarch, pathlib; print(pathlib.Path(makystats.armagarch.__file__).parent / 'DOCUMENTATION.md')"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
In-process help: `help(VECM)`, `help(arma_garch)`, `help(select_coint_rank)`.
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Quick start — VECM
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from makystats import VECM, select_coint_rank
|
|
124
|
+
from makystats.vecm import summarize_johansen, information_criteria_table
|
|
125
|
+
|
|
126
|
+
# data: DataFrame or ndarray (T x n) in LEVELS
|
|
127
|
+
|
|
128
|
+
summarize_johansen(data, k_ar_diff=1, signif=0.05) # all 5 test cases (CV-based rank)
|
|
129
|
+
rank = select_coint_rank(data, k_ar_diff=1, test_case='3') # MHM p-values
|
|
130
|
+
rank.summary()
|
|
131
|
+
|
|
132
|
+
model = VECM(data, k_ar_diff=1, coint_rank=rank.rank, det_case='3a').fit()
|
|
133
|
+
model.summary()
|
|
134
|
+
|
|
135
|
+
# Native forecast + confidence intervals
|
|
136
|
+
print(model.forecast(steps=8, conf_int=True, method='analytic'))
|
|
137
|
+
print(model.forecast(steps=8, conf_int=True, method='bootstrap', sim_reps=1000, seed=1))
|
|
138
|
+
model.plot_forecast(steps=8, method='bootstrap', sim_reps=1000)
|
|
139
|
+
|
|
140
|
+
information_criteria_table(data, k_ar_diff=1) # optional AIC/BIC by rank & case
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## VECM reference
|
|
146
|
+
|
|
147
|
+
### Parameters (makystats API — not statsmodels names)
|
|
148
|
+
|
|
149
|
+
| Parameter | Values | Role |
|
|
150
|
+
|-----------|--------|------|
|
|
151
|
+
| `test_case` | `'1'` … `'5'` | Deterministic specification of the **rank test** |
|
|
152
|
+
| `det_case` | `'1'`, `'2'`, `'3a'`, `'3b'`, `'4a'`, `'4b'`, `'5a'`, `'5b'` | Deterministic specification of **VECM estimation** (default `'3a'`) |
|
|
153
|
+
| `k_ar_diff` | int | Lags in differences (= VAR order in levels − 1) |
|
|
154
|
+
| `coint_rank` | int | Cointegration rank r |
|
|
155
|
+
|
|
156
|
+
| `test_case` | Meaning |
|
|
157
|
+
|-------------|---------|
|
|
158
|
+
| `'1'` | No deterministic terms |
|
|
159
|
+
| `'2'` | Restricted constant (inside cointegration) |
|
|
160
|
+
| `'3'` | Unrestricted constant |
|
|
161
|
+
| `'4'` | Unrestricted constant + restricted trend |
|
|
162
|
+
| `'5'` | Unrestricted constant and trend |
|
|
163
|
+
|
|
164
|
+
| `det_case` | Meaning |
|
|
165
|
+
|------------|---------|
|
|
166
|
+
| `'3a'` / `'4a'` / `'5a'` | **JHJ** dual deterministic terms (Johansen–Hendry–Juselius) |
|
|
167
|
+
| `'3b'` / `'4b'` / `'5b'` | Classical single-term counterparts |
|
|
168
|
+
|
|
169
|
+
### Rank: p-values vs critical values
|
|
170
|
+
|
|
171
|
+
| Function | Selection rule |
|
|
172
|
+
|----------|----------------|
|
|
173
|
+
| `select_coint_rank` | Continuous **MHM p-values** |
|
|
174
|
+
| `summarize_johansen` | Tabulated **MHM critical values** |
|
|
175
|
+
|
|
176
|
+
Near the boundary the displayed ranks may differ by one unit.
|
|
177
|
+
|
|
178
|
+
### Imports
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from makystats import VECM, select_coint_rank
|
|
182
|
+
|
|
183
|
+
from makystats.vecm import (
|
|
184
|
+
VECMModel, JohansenTest, vecm, VECM,
|
|
185
|
+
select_coint_rank, summarize_johansen,
|
|
186
|
+
information_criteria_table, mhm_pvalue, mhm_critical,
|
|
187
|
+
)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
At the package root only `VECM` is exported (not `vecm`), so the submodule `makystats.vecm` stays importable.
|
|
191
|
+
|
|
192
|
+
### Forecasts and confidence intervals
|
|
193
|
+
|
|
194
|
+
Point forecasts use the makystats recursion (including JHJ deterministics).
|
|
195
|
+
Intervals (`conf_int=True`) support three methods — **parameters held fixed** (innovation uncertainty only):
|
|
196
|
+
|
|
197
|
+
| `method` | Construction |
|
|
198
|
+
|----------|----------------|
|
|
199
|
+
| `'analytic'` (default) | Lütkepohl MSE via MA representation + normal quantiles |
|
|
200
|
+
| `'bootstrap'` | Resample residual **rows** (preserves contemporaneous correlation); empirical percentiles |
|
|
201
|
+
| `'gaussian'` | Draws from N(0, Σ_u); empirical percentiles |
|
|
202
|
+
|
|
203
|
+
Aliases: `'eviews'` / `'simulation'` → bootstrap; `'mc'` / `'normal'` → gaussian.
|
|
204
|
+
|
|
205
|
+
Analytic MSE at horizon h (levels VAR equivalent, Lütkepohl):
|
|
206
|
+
|
|
207
|
+

|
|
208
|
+
|
|
209
|
+
### Diagnostics and IRF
|
|
210
|
+
|
|
211
|
+
`test_whiteness`, `test_normality`, Granger causality, and `irf` are delegated to statsmodels on the **step-1** reduced-rank model. Under JHJ this is still correct for residual tests and the **mean IRF path** (α, β, Γ and residuals match the classical twin case). IRF confidence bands, if any, are not recalibrated to step-3 JHJ standard errors. Forecast tools above are fully native.
|
|
212
|
+
|
|
213
|
+
### JHJ estimation sketch
|
|
214
|
+
|
|
215
|
+
1. Classical reduced-rank → estimates of α, β, Γ
|
|
216
|
+
2. Orthogonalise cointegration residuals on dual deterministic regressors
|
|
217
|
+
3. Re-estimate short-run equation with detrended ECT → outside deterministics and all SEs refreshed; α and Γ unchanged
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## Quick start — ARMA–GARCH
|
|
222
|
+
|
|
223
|
+
```python
|
|
224
|
+
from makystats import arma_garch, select_order, ARMAGarch
|
|
225
|
+
|
|
226
|
+
res = arma_garch(y, mean='AR', ar=1, ma=1, vol='GARCH', trend='c').fit()
|
|
227
|
+
print(res.coef_table)
|
|
228
|
+
res.plot_diagnostics()
|
|
229
|
+
|
|
230
|
+
# lag selection helper
|
|
231
|
+
order = select_order(y, max_ar=3, max_ma=3, vol='GARCH')
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
The sections below are the full technical documentation of the volatility submodule.
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## 1. Statistical model
|
|
239
|
+
|
|
240
|
+
### 1.1 Mean equation
|
|
241
|
+
|
|
242
|
+
For observations y_t, t = 1, …, n:
|
|
243
|
+
|
|
244
|
+

|
|
245
|
+
|
|
246
|
+
- Index sets I_AR, I_MA may be consecutive {1, …, p} or sparse (e.g. {5} only).
|
|
247
|
+
- **ARX**: AR terms + exogenous x_t, no MA.
|
|
248
|
+
- **ARMAX**: AR + MA + x_t.
|
|
249
|
+
- **GARCH-M (ARCH-M)**: `in_mean='sigma'|'var'|'log'` — λ·f(σ_t) in the mean (EViews Variance = `'var'`); coefficient label `archm`. Also on `select_order(..., in_mean=)`. Careful if σ² is nearly flat (const ↔ λ).
|
|
250
|
+
- **Post-fit without re-passing y/x**: `forecast()`, `var()`, `results_frame()`, … reuse series from `fit()`; future `x` defaults to last row.
|
|
251
|
+
- **VaR / ES**: `var()` / `expected_shortfall()` with `aggregation="marginal"` (quantile of y_{T+h}, default) or `"cumulative"` (Monte Carlo sum over h days; `n_sims`, `seed`). In-sample 1-step only: `var_series()` → `var_fitted_`, `var_backtest()` (Kupiec), `plot_var(..., window=, style=)`. Methods: parametric or FHS.
|
|
252
|
+
- **Exogenous names**: pass `x` as a `DataFrame` so `coef_table` uses column names; with an `ndarray`, use `fit(..., x_names=[...])`.
|
|
253
|
+
- **LS**: intercept + x_t only.
|
|
254
|
+
- **HAR / HARX** (Corsi-style): regressors are causal rolling means of y at user horizons (default 1, 5, 22); estimation proceeds as LS + GARCH on the residual scale.
|
|
255
|
+
|
|
256
|
+
**Deterministic terms (`trend`).** Independently of `mean`, set:
|
|
257
|
+
|
|
258
|
+
| `trend` | Constant | Linear trend τ_t = 1…T | `coef_table` labels |
|
|
259
|
+
|---------|----------|------------------------|---------------------|
|
|
260
|
+
| `'n'` | no | no | — |
|
|
261
|
+
| `'c'` (default) | yes | no | `const` |
|
|
262
|
+
| `'t'` | no | yes | `trend` |
|
|
263
|
+
| `'ct'` | yes | yes | `const`, `trend` |
|
|
264
|
+
|
|
265
|
+
Mean path: μ_t = c + δ·τ_t. The AR applies to (y_t − μ_t).
|
|
266
|
+
`mean='Zero'` means *no constant* (AR/MA via `ar=`/`ma=` remain allowed); with default `trend='c'` it becomes `trend='n'`.
|
|
267
|
+
`mean='Zero'` + `trend='ct'` is rejected; `trend='t'` is allowed.
|
|
268
|
+
|
|
269
|
+
**Centring.** When `center=True` (default) and a constant is present without a trend, y is demeaned for numerical conditioning; the intercept is restored on the original scale after convergence. Centring is turned off automatically when `trend` is `'t'` or `'ct'`.
|
|
270
|
+
|
|
271
|
+
### 1.2 Conditional variance
|
|
272
|
+
|
|
273
|
+

|
|
274
|
+
|
|
275
|
+
with z_t i.i.d. under the chosen distribution (mean 0, variance 1 after standardisation where required).
|
|
276
|
+
|
|
277
|
+
**GARCH(r, s)**
|
|
278
|
+
|
|
279
|
+

|
|
280
|
+
|
|
281
|
+
**ARCH** — GARCH with s = 0.
|
|
282
|
+
|
|
283
|
+
**GJR / TARCH** — GARCH plus the asymmetry term
|
|
284
|
+
|
|
285
|
+

|
|
286
|
+
|
|
287
|
+
**EGARCH** (Nelson 1991)
|
|
288
|
+
|
|
289
|
+

|
|
290
|
+
|
|
291
|
+
**APARCH** (Ding–Granger–Engle)
|
|
292
|
+
|
|
293
|
+

|
|
294
|
+
|
|
295
|
+
**FIGARCH(p, d, q)** (Baillie–Bollerslev–Mikkelsen)
|
|
296
|
+
|
|
297
|
+

|
|
298
|
+
|
|
299
|
+
(truncated lag polynomial for the fractional weights; default truncation 1000)
|
|
300
|
+
|
|
301
|
+
**EGARCH display forms.** Internally the filter uses the *centred* Nelson specification (magnitude term |z| − E|z|). Setting `egarch_form='raw'` shifts the reported intercept only:
|
|
302
|
+
|
|
303
|
+

|
|
304
|
+
|
|
305
|
+
so that the printed equation matches the non-centred writing
|
|
306
|
+
|
|
307
|
+

|
|
308
|
+
|
|
309
|
+
Likelihood and other coefficients are unchanged.
|
|
310
|
+
|
|
311
|
+
**Sign restrictions.** For multi-lag GARCH, individual α_k, β_ℓ are **not** forced positive (only ω > 0 via exp); negative coefficients remain admissible provided σ_t² stays positive in the filter (clamped only for numerical safety). This matches unrestricted ML practice when higher-order lag polynomials are used. GJR γ is free; APARCH γ is mapped to (−1, 1) via tanh; FIGARCH d is soft-bounded for stability.
|
|
312
|
+
|
|
313
|
+
### 1.3 Innovation distributions
|
|
314
|
+
|
|
315
|
+
Standardised residuals z_t = ε_t / σ_t contribute to the log-likelihood as follows (up to constants independent of parameters).
|
|
316
|
+
|
|
317
|
+
**normal**
|
|
318
|
+
|
|
319
|
+

|
|
320
|
+
|
|
321
|
+
**Student-t** — form with ν > 2, scaled so that Var(z) = 1.
|
|
322
|
+
|
|
323
|
+
**skew-t** (Hansen 1994) — ν > 2, skewness λ ∈ (−1, 1).
|
|
324
|
+
|
|
325
|
+
**GED** — shape ν > 0; ν = 2 recovers the Gaussian.
|
|
326
|
+
|
|
327
|
+
Shape parameters are reparameterised so that BFGS stays in an open unconstrained domain, e.g.
|
|
328
|
+
|
|
329
|
+

|
|
330
|
+
|
|
331
|
+
for the t law.
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
## 2. Likelihood construction and sample handling
|
|
336
|
+
|
|
337
|
+
### 2.1 Log-likelihood
|
|
338
|
+
|
|
339
|
+
The objective maximised is the conditional Gaussian / t / skew-t / GED log-likelihood
|
|
340
|
+
|
|
341
|
+

|
|
342
|
+
|
|
343
|
+
where b is the burn-in (see below) and ℓ uses ε_t(θ) and σ_t²(θ) from the joint filter.
|
|
344
|
+
|
|
345
|
+
### 2.2 Burn-in and `adjust_sample`
|
|
346
|
+
|
|
347
|
+
| Flag | Behaviour |
|
|
348
|
+
|------|-----------|
|
|
349
|
+
| `adjust_sample=True` (default) | b = max(AR lags, MA lags, hold_back). The first b observations initialise lags; they **do not** enter L. Same idea as “sample adjusted” in classical ARMA practice. |
|
|
350
|
+
| `adjust_sample=False` | b = 0. All n terms enter L; pre-sample AR/MA/variance states are filled by backcast. Appropriate when the reference sample is 1…n with no adjustment. |
|
|
351
|
+
|
|
352
|
+
HAR forces at least b = max(horizons) when `adjust_sample=True`, because early rolling means are incomplete.
|
|
353
|
+
|
|
354
|
+
### 2.3 MA pre-sample (Bjørnstad-type / reverse recursion)
|
|
355
|
+
|
|
356
|
+
When MA terms are present, pre-sample innovations are **not** set to zero. The implementation:
|
|
357
|
+
|
|
358
|
+
1. Builds AR-only residuals u_t on the estimation window.
|
|
359
|
+
2. Runs a **backward** recursion to obtain a Bjørn–Johansen-style backcast path e^{bc}_t.
|
|
360
|
+
3. Extends that path **before** the sample (zero driving noise) to form pre-sample MA states e^{ps}.
|
|
361
|
+
4. Runs the **forward** ARMA recursion using e^{ps} for lags that fall before the burn index.
|
|
362
|
+
|
|
363
|
+
This avoids the bias of zero-innovation start-up for pure MA or ARMA models and is material for likelihood levels on short effective samples.
|
|
364
|
+
|
|
365
|
+
### 2.4 Variance backcast
|
|
366
|
+
|
|
367
|
+
Pre-sample conditional variance uses exponential smoothing of squared residuals (parameter λ = `backcast`, default 0.7):
|
|
368
|
+
|
|
369
|
+

|
|
370
|
+
|
|
371
|
+
with j = 0 the **oldest** residual in the estimation window (weights decay toward the present). λ = 1 collapses to h_0 = ē². Inside the filter, both lagged ε² and lagged σ² that fall in the burn region are replaced by h_0 (GARCH-family recursions).
|
|
372
|
+
|
|
373
|
+
For GARCH-type models, h_0 is also **recomputed from current residuals** inside each likelihood evaluation so that the backcast stays consistent with the current mean parameters.
|
|
374
|
+
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
## 3. Optimisation strategy
|
|
378
|
+
|
|
379
|
+
### 3.1 Unconstrained BFGS + reparameterisation
|
|
380
|
+
|
|
381
|
+
Parameters θ live in an unconstrained space; a smooth map θ ↦ ψ yields the economic coefficients ψ (AR, MA, variance, shape):
|
|
382
|
+
|
|
383
|
+
| Block | Map (illustrative) |
|
|
384
|
+
|-------|---------------------|
|
|
385
|
+
| AR / MA coefficients | stationarity-friendly box (see image) |
|
|
386
|
+
| ω (GARCH family) | see image |
|
|
387
|
+
| α, β (multi-lag GARCH) | free (signed); filter clamps σ_t² > 0 |
|
|
388
|
+
| EGARCH β | positive, scaled to keep persistence < 1 |
|
|
389
|
+
| APARCH γ | tanh into (−1, 1) |
|
|
390
|
+
| FIGARCH d | soft map into a stable interval |
|
|
391
|
+
| t degrees of freedom | see image |
|
|
392
|
+
|
|
393
|
+

|
|
394
|
+
|
|
395
|
+

|
|
396
|
+
|
|
397
|
+

|
|
398
|
+
|
|
399
|
+
No inequality constraints are passed to SciPy; BFGS (with finite-difference gradient) operates on θ only. Arguments of `exp` are clipped to avoid overflow during exploratory steps.
|
|
400
|
+
|
|
401
|
+
**Polish step.** If the first BFGS run does not report success (common with numerical gradients: “precision loss”), a second BFGS run restarts from the current point.
|
|
402
|
+
|
|
403
|
+
### 3.2 Starting values
|
|
404
|
+
|
|
405
|
+
- Dense ARMA: preliminary ARIMA (statsmodels) when lags are 1…p, 1…q.
|
|
406
|
+
- Sparse AR/MA: neutral defaults / method-of-moments style starts.
|
|
407
|
+
- Variance: small α, moderate β, ω scaled to residual variance (APARCH uses ω proportional to σ̂^δ).
|
|
408
|
+
|
|
409
|
+
### 3.3 Standard errors
|
|
410
|
+
|
|
411
|
+
Default **OPG** / BHHH (outer product of gradients):
|
|
412
|
+
|
|
413
|
+

|
|
414
|
+
|
|
415
|
+
with g_t = ∂ℓ_t / ∂θ evaluated numerically at the MLE (mapped to the economic parameter scale for reporting). Alternative: `cov_type='hessian'` (numerical Hessian of the concentrated objective).
|
|
416
|
+
|
|
417
|
+
---
|
|
418
|
+
|
|
419
|
+
## 4. Post-estimation
|
|
420
|
+
|
|
421
|
+
### 4.1 Output series (length n)
|
|
422
|
+
|
|
423
|
+
| Attribute | Content |
|
|
424
|
+
|-----------|---------|
|
|
425
|
+
| `residuals` | ε̂_t |
|
|
426
|
+
| `std_resid` | ε̂_t / σ̂_t |
|
|
427
|
+
| `conditional_vol` | σ̂_t |
|
|
428
|
+
| `fitted_values` | y_t − ε̂_t (one-step mean prediction) |
|
|
429
|
+
| `coef_table` | coefficients, SE, z, p-values, 95% CI |
|
|
430
|
+
|
|
431
|
+
### 4.2 Diagnostics
|
|
432
|
+
|
|
433
|
+
- **`plot_diagnostics()`**: 2×3 figure — residual path, histogram + N(0,1) + KDE, normal QQ plot, ACF of z_t, ACF of z_t², conditional volatility path.
|
|
434
|
+
|
|
435
|
+
### 4.3 Forecasts
|
|
436
|
+
|
|
437
|
+
`forecast(y, h, threshold=0.0, x=None)` returns a DataFrame with columns `mean`, `variance`, `vol`, `p_up` for horizons 1…h:
|
|
438
|
+
|
|
439
|
+
- Mean: multi-step ARMA recursion; future shocks set to zero (minimum MSE under symmetric loss).
|
|
440
|
+
- Variance: recurse the chosen variance equation with ε_{T+k}² replaced by σ_{T+k}² for k ≥ 1 where needed.
|
|
441
|
+
- `p_up` = P(y_{T+h} > threshold) under the fitted innovation law (normal / t CDF; GED / skew-t use consistent approximations).
|
|
442
|
+
- Exogenous forecasts require future x (`x` with h rows); if omitted, the exogenous contribution is set to zero.
|
|
443
|
+
|
|
444
|
+
### 4.4 Order selection (`select_order`)
|
|
445
|
+
|
|
446
|
+
Grid search over AR/MA orders with information criteria (AIC / BIC).
|
|
447
|
+
**Limitation:** the grid uses dense orders 0, 1, …. Sparse specifications such as `ma=[5]` must be estimated directly.
|
|
448
|
+
|
|
449
|
+
---
|
|
450
|
+
|
|
451
|
+
## 5. Numerical and implementation notes
|
|
452
|
+
|
|
453
|
+
1. **Single filter path.** Residuals and σ_t² are produced by one combined ARMA + variance recursion per likelihood evaluation.
|
|
454
|
+
2. **Clamping.** σ_t² is kept in a large positive range to prevent log-domain NaNs; this is a numerical guard, not an economic constraint.
|
|
455
|
+
3. **Numba (optional).** `pip install "makystats[numba]"` can accelerate the filter on long series.
|
|
456
|
+
4. **Missing data.** Series should be cleaned before estimation; the backend expects a contiguous numeric sample.
|
|
457
|
+
5. **Persistence.** Reported persistence is Σα + Σβ (GARCH), Σα + (1/2)Σγ + Σβ (GJR), or the analogous EGARCH / APARCH summaries.
|
|
458
|
+
|
|
459
|
+
---
|
|
460
|
+
|
|
461
|
+
## 6. API sketch
|
|
462
|
+
|
|
463
|
+
```python
|
|
464
|
+
from makystats import arma_garch, select_order, ARMAGarch
|
|
465
|
+
|
|
466
|
+
res = arma_garch(
|
|
467
|
+
y,
|
|
468
|
+
mean='ARMA', # 'Zero', 'Constant', 'AR', 'ARMA', 'ARX', 'ARMAX', 'HAR', 'HARX', 'LS'
|
|
469
|
+
ar=1, ma=1,
|
|
470
|
+
vol='GARCH', # 'GARCH', 'GJR', 'TARCH', 'EGARCH', 'APARCH', 'FIGARCH'
|
|
471
|
+
dist='normal', # 'normal', 't', 'skewt', 'ged'
|
|
472
|
+
adjust_sample=True,
|
|
473
|
+
).fit()
|
|
474
|
+
|
|
475
|
+
print(res.coef_table)
|
|
476
|
+
print(res.llf, res.aic, res.bic)
|
|
477
|
+
res.plot_diagnostics()
|
|
478
|
+
fc = res.forecast(y, h=10)
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
In-process documentation: `help(arma_garch)`, `help(ARMAGarch.fit)`, `help(select_order)`.
|
|
482
|
+
|
|
483
|
+
---
|
|
484
|
+
|
|
485
|
+
## 7. Package layout and tests
|
|
486
|
+
|
|
487
|
+
```text
|
|
488
|
+
makystats/
|
|
489
|
+
__init__.py # public exports
|
|
490
|
+
armagarch/ # ARMA–GARCH family (MLE)
|
|
491
|
+
__init__.py
|
|
492
|
+
core.py
|
|
493
|
+
README.md
|
|
494
|
+
DOCUMENTATION.md
|
|
495
|
+
vecm/ # VECM / Johansen cointegration
|
|
496
|
+
__init__.py
|
|
497
|
+
core.py
|
|
498
|
+
vecm_model.py
|
|
499
|
+
mhm_tables.npz
|
|
500
|
+
README.md
|
|
501
|
+
DOCUMENTATION.md
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
```bash
|
|
505
|
+
python -m pytest tests/ -q
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
---
|
|
509
|
+
|
|
510
|
+
## 8. References (methods implemented)
|
|
511
|
+
|
|
512
|
+
- Bollerslev, T. (1986), Generalized autoregressive conditional heteroskedasticity, *Journal of Econometrics*.
|
|
513
|
+
- Glosten, L., Jagannathan, R., Runkle, D. (1993), On the relation between the expected value and the volatility of the nominal excess return on stocks, *Journal of Finance* (GJR).
|
|
514
|
+
- Nelson, D. (1991), Conditional heteroskedasticity in asset returns: a new approach, *Econometrica* (EGARCH).
|
|
515
|
+
- Ding, Z., Granger, C., Engle, R. (1993), A long memory property of stock market returns and a new model, *Journal of Empirical Finance* (APARCH).
|
|
516
|
+
- Baillie, R., Bollerslev, T., Mikkelsen, H. (1996), Fractionally integrated generalized autoregressive conditional heteroskedasticity, *Journal of Econometrics* (FIGARCH).
|
|
517
|
+
- Hansen, B. (1994), Autoregressive conditional density estimation, *International Economic Review* (skew-t).
|
|
518
|
+
- Corsi, F. (2009), A simple approximate long-memory model of realized volatility, *Journal of Financial Econometrics* (HAR).
|
|
519
|
+
- Berndt, E., Hall, B., Hall, R., Hausman, J. (1974), Estimation and inference in nonlinear structural models, *Annals of Economic and Social Measurement* (BHHH / OPG).
|
|
520
|
+
- Johansen, S. (1995), *Likelihood-Based Inference in Cointegrated Vector Autoregressive Models*.
|
|
521
|
+
- MacKinnon, J. G., Haug, A. A., & Michelis, L. (1999), Numerical distribution functions of likelihood ratio tests for cointegration, *Journal of Applied Econometrics*.
|
|
522
|
+
- Lütkepohl, H. (2005), *New Introduction to Multiple Time Series Analysis*.
|
|
523
|
+
|
|
524
|
+
---
|
|
525
|
+
|
|
526
|
+
## License
|
|
527
|
+
|
|
528
|
+
MIT. Version **0.1.0**.
|