Skip to main content

Estimating coefficients of ARMA models with the Scipy package.

Project description

# ARMA w. Scipy
Estimating coefficients of ARMA models with the Scipy package.

Installation
```bash
pip install arma-scipy
```

Python import
```python
from arma_scipy import fit
```

## Motivation

ARMA models in general can be, after choosing p and q, fitted by least
squares regression to find the values of the parameters which minimize
the error term. It is generally considered good practice to find the
smallest values of p and q which provide an acceptable fit to the data.
For a pure AR model, the Yule-Walker equations may be used to provide a
fit. "Least squares" means that the overall solution minimizes the sum of the
squares of the residuals made in the results of every single equation.

The reasons behind this Scipy fit implementation are twofold:
- provide an alternative when the score function is not the MSE - **more important**
- provide a way to compare both fit methods (stat theory vs optimization) - less important

You can fit the coefficients of an `ARMA(4,4)` that way:
```bash
python generate_arma_process.py
python scipy_fit_data.py
```

Here is an example of such a fit:
```
################################################################################
Optimization terminated successfully.
Current function value: 1.432208
Iterations: 508
Function evaluations: 788
Estimation of the coefficients with the scipy package:
[ 0.2235 -0.5872 0.3143 -0.1805 0.167 -0.0464 0.6528 0.224 ]
Estimation of the coefficients with the statsmodels.tsa (least squares) package:
[ 0.237 -0.4998 0.3467 -0.128 0.1542 -0.1467 0.6244 0.2245]
True ARMA coefficients:
[ 0.25 -0.5 0.35 -0.15 0.5 -0.4 0.78 0.32]
```

## Comparison

- It is not a surprise that the score function is minimized by the fit of the `statsmodels` package. Indeed, the maximum likelihood estimation is guaranteed to yield the lowest mean squared error score on the train set.
- The Scipy minimize function is doing a relatively good job at being close to this minimum. However, due to the randomness nature of this optimization and the crucial choice of x0 (initial values of the coefficients to optimize), several runs are necessary to guarantee to be close enough to this global minimum. It's clear enough that there is a strong variability across the runs. A significant proportion of runs do not get even closer to this minimum. On 200 runs, the average hovers ~10% above the minimum expected value, then starts to overfit. The best run, however, reaches 1.41807, a score extremely close to the target minimum score of 1.4179.

<p align="center">
<img src="misc/arma_44_fit.png" width="600">
</p>

The conclusion of this comparison is such that there it makes little sense to fit the coefficients of an ARMA model with a numerical method like `scipy.minimize`. A traditional fit using `statsmodels` is preferable. Yet, `scipy.minimize` could be used if the score function was not the mean squared error. Finding a good x0 is challenging and one way to have a smooth optimization could lie in the estimation of x0 by `statsmodels`. In other words, fit the coefficients with `statsmodels` then optimize with `scipy.minimize` and your custom score function.

## Custom score function

Lets consider the custom score function that assigns:
- 1 if the prediction has the same sign as the true value.
- 0 otherwise.

```python
def score_function(p, t):
return 1 - np.mean(((p > 0) & (t > 0)) | ((p < 0) & (t < 0)))
```

- The optimization on the ARMA coefficients is done first with the `statsmodels`. This is our `x0`.
- The second optimization is done with `scipy` and the custome score function.

The second fit seems to add some value as the overall score decreases from 0.2090 to 0.2084. This represents a reduction of ~0.29% of the custom score. Concretely, after the first fit, the model correctly predicts the sign of the true value 79.10% of the time. After the second fit, this percentage goes up to 79.16%, thanks to Scipy.

<p align="center">
<img src="misc/custom_score_function.png" width="600">
</p>




Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arma-scipy-1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

arma_scipy-1.1-py2.py3-none-any.whl (7.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file arma-scipy-1.1.tar.gz.

File metadata

  • Download URL: arma-scipy-1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.2

File hashes

Hashes for arma-scipy-1.1.tar.gz
Algorithm Hash digest
SHA256 813b1da3a2e91c5a19ea52fcef424134633793700b759517b29640b880ab2858
MD5 c0d7268cf5e3feb5865a22a244fc5e3f
BLAKE2b-256 341d8bad1d88e0b53d57a8cf5ca5e686f22172b43a7ceb524514581159080ada

See more details on using hashes here.

File details

Details for the file arma_scipy-1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: arma_scipy-1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.2

File hashes

Hashes for arma_scipy-1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3fd2f7afb1d1243752b7bbfacda37285d3b4313d63a549b841034617d5fb2b76
MD5 2776f6d8e6e81b66f4bb4b158b6c17cc
BLAKE2b-256 23cc4657e61db8281344b847b02db246ce4f3b1f6e080b80016a9efacc887bf6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page