_ _ ___
| | | | / |
__ _ _ __ ___| |_| |/ /| |_ __ _ _
/ _` | '__/ _ \ __| / /_| | '_ \| | | |
| (_| | | | __/ |_| \___ | |_) | |_| |
\__, |_| \___|\__|_| |_/ .__/ \__, |
__/ | | | __/ |
|___/ |_| |___/
gretl4py: Python Bindings for gretl
Python bindings for the gretl econometrics library
gretl4py provides a Python interface to libgretl, the numerical and econometric engine underlying gretl. It brings gretl's econometric functionality, numerical routines, and data-handling capabilities into Python while providing Python-oriented interfaces for matrices, datasets, models, and other gretl objects.
gretl4py is not a separate econometrics library. It is a bridge between Python and libgretl: estimation and many numerical operations are performed by the mature gretl engine, while Python provides the programming environment and ecosystem around it.
Windows users: gretl4py requires the Microsoft Visual C++ 2022 Redistributable (x64).
Download it from: https://aka.ms/vs/17/release/vc_redist.x64.exe
Highlights
gretl4py provides:
- Econometric estimation, including OLS, weighted and least-absolute-deviation regression, IV/2SLS, maximum likelihood, GMM, quantile regression, regularized regression, and various limited-dependent-variable models.
- Time-series and multivariate methods, including AR/ARIMA, GARCH-type models, VAR and VECM models, structural VAR functionality, and related tests.
- Panel-data methods, including dynamic-panel and instrumental-variable estimators.
- Mixed-frequency modelling, including MIDAS regression.
- Econometric tests, covering unit roots, cointegration, heteroskedasticity, autocorrelation, specification, parameter restrictions, structural stability, and other commonly used procedures.
- Native gretl matrices and datasets, exposed as Python objects with Python-friendly indexing and conversion facilities.
- Bundles and other gretl objects, allowing Python code to interact directly with objects used by
libgretl. - NumPy and pandas interoperability, including conversion to NumPy arrays and pandas DataFrames and, where appropriate, zero-copy views of matrix data.
- gretl's hansl language, allowing Python programs to execute hansl code and exchange user-defined variables with
libgretl. - gretl function packages, making it possible to use functionality implemented in hansl packages from the Python environment.
- LIBSVM-based machine-learning functionality available through gretl.
The package is designed to allow users to combine the econometric functionality of gretl with the broader Python ecosystem, including NumPy, pandas, matplotlib, and other scientific and statistical libraries.
Installation
The recommended way to install gretl4py is with pip:
pip install gretl4py
Pre-built packages are available for supported Python versions and platforms.
For platform-specific requirements and available distributions, see the gretl4py project page.
A quick example
A typical gretl4py workflow consists of loading a dataset, estimating a model, and working with the resulting Python object:
import gretl
data = gretl.get_data("bjg.gdt")
model = gretl.ols(
"1 0 2",
data=data
).fit()
print(model)
The exact estimator interface depends on the model being estimated. The gretl module exposes both high-level estimator functions and corresponding gretl model objects.
Working with datasets
gretl datasets are represented by the gretl.Dataset class. Datasets can be loaded from a number of formats supported by gretl, including:
.gdt.gdtb.csv.dta.wf1.xls.xlsx.ods
For example:
import importlib.resources as resources
import gretl
data_dir = resources.files("gretl").joinpath("data")
data = gretl.get_data(str(data_dir.joinpath("bjg.gdt")))
print(data)
Datasets can be manipulated directly from Python, including adding and transforming series, working with lists, and passing datasets to estimators.
gretl4py also provides conversion to pandas:
df = data.to_dataframe()
Matrices
The gretl.Matrix class provides a Python interface to gretl's native matrix objects.
Matrices support Python-style indexing and slicing, matrix operations, row and column names, and conversion to NumPy.
For example:
import gretl
m = gretl.Matrix.ones(3, 3)
m[0, 0] = 10
print(m)
NumPy interoperability is available both through copying and through zero-copy views where appropriate:
a = m.to_numpy() # independent NumPy array
v = m.numpy_view # view of the underlying gretl matrix
A pandas representation is also available:
df = m.to_pandas()
The distinction between copies and views is important when modifying data; see the documentation for details.
Estimation
gretl4py exposes a range of gretl estimators through Python. Depending on the model, these include:
- OLS and weighted least squares
- AR and ARIMA
- GARCH-type models
- IV / 2SLS
- LAD and quantile regression
- logit, probit, tobit and related limited-dependent-variable models
- count and duration models
- sample-selection models
- panel-data estimators
- dynamic-panel estimators
- MIDAS regression
- VAR and VECM
- structural VAR models
- regularized regression, including LASSO, Ridge and Elastic Net
- maximum-likelihood and GMM-based estimation
The resulting objects are instances of gretl4py model classes and provide access to estimation results, coefficients, covariance matrices, residuals, fitted values, forecasts, tests, and other model-specific information.
For example:
import gretl
data = gretl.get_data("bjg.gdt")
model = gretl.ols("1 0 2", data=data).fit()
print(model.coeff)
print(model.vcv)
The available methods depend on the particular model class. For example, VAR and VECM models provide functionality specific to multivariate time-series analysis, including impulse-response analysis.
Econometric tests
Many of gretl's statistical and econometric tests are available directly from Python. These include tests for, among other things:
- unit roots and stationarity,
- cointegration,
- autocorrelation,
- heteroskedasticity,
- normality,
- structural stability,
- parameter restrictions,
- specification,
- omitted variables,
- functional form,
- multicollinearity,
- panel-data dependence and specification.
Tests may operate either on a dataset or on a fitted model, depending on the particular procedure.
NumPy and pandas interoperability
gretl4py is intended to work naturally with the Python scientific-computing ecosystem.
For matrices, the following interfaces are provided:
numpy_array = matrix.to_numpy()
numpy_view = matrix.numpy_view
dataframe = matrix.to_pandas()
to_numpy() and to_pandas() create independent objects. In contrast, numpy_view exposes the underlying matrix storage directly and therefore avoids a data copy.
Datasets can be converted to pandas DataFrames with:
dataframe = dataset.to_dataframe()
This makes it possible to use gretl for estimation while using NumPy, pandas, matplotlib, or other Python packages for subsequent processing and visualization.
Interoperability with hansl
gretl4py also provides an additional interoperability layer with hansl, gretl's scripting language.
Python code can execute hansl code directly:
import gretl
gretl.run_hansl("""
function void foo (void)
print "Hello from hansl"
end function
foo()
""")
An existing hansl script can be executed with:
gretl.run_script("my_script.inp")
This functionality is particularly useful for testing existing hansl code, using functionality implemented in gretl function packages, and accessing parts of gretl that are naturally expressed in hansl.
User-defined variables
gretl4py can also exchange user-defined variables with libgretl.
For example, a matrix created in Python can be placed in the gretl namespace:
import gretl
m = gretl.Matrix.ones(2, 2)
gretl.genr(name="mat", value=m)
gretl.run_hansl("mat = mat .* 2")
m = gretl.get_uservar("mat")
These variables live in the libgretl namespace rather than in Python's namespace. Consequently, they can be accessed by subsequently executed hansl code and can also be retrieved from Python.
Important: hansl execution and user-defined-variable interoperability are supplementary features of gretl4py. They are provided primarily to facilitate interoperability with existing gretl/hansl code and should not be regarded as the primary programming interface of the package. For new Python applications, the native gretl4py API is generally preferable.
Examples and demonstrations
The source distribution contains a growing collection of examples and demonstrations.
Estimator examples
Estimator examples are located in:
gretl/examples/estimators/
They provide small, self-contained examples of individual estimators. For example:
import gretl.examples.estimators.ols
gretl.examples.estimators.ols.run_example()
The source of an example can also be inspected directly from Python:
import inspect
import gretl.examples.estimators.ols
print(inspect.getsource(
gretl.examples.estimators.ols.run_example
))
Feature demonstrations
More extensive demonstrations are located in:
demo/
These cover not only estimation, but also data handling, matrices, bundles, user-defined variables, hansl interoperability, forecasting, VAR/VECM functionality, packages, filters, nonlinear models, and other gretl4py features.
The examples and demonstrations are often the best starting point for seeing how a particular feature is intended to be used.
Package contents
A gretl4py installation contains:
libgretland its plugins together with the required runtime dependencies;- the compiled
_gretlPython extension module providing the binding tolibgretl; - Python-level helper modules and utilities;
- example modules and scripts;
- bundled datasets and other supporting resources.
The Python extension is implemented using C++ and pybind11, while the underlying econometric functionality is provided by libgretl.
Documentation
The main documentation is available at:
https://gretl.sourceforge.net/gretl4py.html
The current PDF documentation is:
https://sourceforge.net/projects/gretl/files/gretl4py/gretl4py.pdf/download
The documentation is actively evolving alongside the Python API.
The source code, examples, demonstrations, and development history are available in the gretl4py repository:
https://sourceforge.net/p/gretl/gretl4py/ci/master/tree/
License
gretl4py is distributed under the GNU General Public License, version 3 or later (GPL-3.0-or-later).
gretl and its components are distributed under their respective free-software licenses. See the accompanying license files and the gretl documentation for details.
Development status
gretl4py is under active development. The Python interface continues to evolve as more of the functionality provided by libgretl is exposed through a native Python API.
The project aims to keep the Python interface consistent and Pythonic while retaining close correspondence with gretl's established econometric functionality.
For current functionality, examples, and API details, consult the documentation and source tree rather than relying solely on this README.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gretl4py-0.60.tar.gz.
File metadata
- Download URL: gretl4py-0.60.tar.gz
- Upload date:
- Size: 12.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38514f60a6732455f1847a7ad7efbabcf8d1e237bf5efc909c6d77c38ed7c7cd
|
|
| MD5 |
76801c1c954ad078fb8420f8a9f5afbd
|
|
| BLAKE2b-256 |
67ecf3b21b98d558290d1e08658fdc9accff9f12d495a9a3876492f7a605a597
|
File details
Details for the file gretl4py-0.60-cp314-cp314-win_arm64.whl.
File metadata
- Download URL: gretl4py-0.60-cp314-cp314-win_arm64.whl
- Upload date:
- Size: 49.1 MB
- Tags: CPython 3.14, Windows ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08c91a80517e4fb49107c279b9e2054c7e7e2d2c14b2d6edc54f6ddd49f3271b
|
|
| MD5 |
0dec49e268180b0d3e40055bad304a14
|
|
| BLAKE2b-256 |
29bea02cb31334ef57a263e2f43c39db2ab17e132c969e0d734aff2fa7ea0a4b
|
File details
Details for the file gretl4py-0.60-cp314-cp314-win_amd64.whl.
File metadata
- Download URL: gretl4py-0.60-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 62.5 MB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
204917d8e1467c32153843687cbce0f693fcbab9e1c94ff5756e2ec66eaad03c
|
|
| MD5 |
3f646c08f6d88e3f1e4a9d9e2eb96847
|
|
| BLAKE2b-256 |
9815008e4355e3fb0561b6296f3ddf9938db98e7fa59e963322fa883f4f8ff03
|
File details
Details for the file gretl4py-0.60-cp314-cp314-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: gretl4py-0.60-cp314-cp314-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 42.4 MB
- Tags: CPython 3.14, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f85e6889c6caecb45236d1ee9d9ef96fb284c8b66ca554462f16194e49657c09
|
|
| MD5 |
6a34a3fda47448b43f6fda1a1505a4bf
|
|
| BLAKE2b-256 |
61acd88ba178601a483c6e575c7580c1a36cae629458b87330d7cb19e4e2d9b3
|
File details
Details for the file gretl4py-0.60-cp314-cp314-macosx_10_15_universal2.whl.
File metadata
- Download URL: gretl4py-0.60-cp314-cp314-macosx_10_15_universal2.whl
- Upload date:
- Size: 36.5 MB
- Tags: CPython 3.14, macOS 10.15+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77460ec47befd27d9035fb4f38ba901f5b62aeab361e01cb0449ca53d805a262
|
|
| MD5 |
e8f0a04bcfa504750c5767b21a10cb9e
|
|
| BLAKE2b-256 |
aebdba5fd27aec37d1db7fb2767454be48bfae6ed29302e80fec8db5092b93b2
|
File details
Details for the file gretl4py-0.60-cp313-cp313-win_arm64.whl.
File metadata
- Download URL: gretl4py-0.60-cp313-cp313-win_arm64.whl
- Upload date:
- Size: 47.7 MB
- Tags: CPython 3.13, Windows ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27e615e00375f96b0b4343d95f6c85ef39dd304b2574cc666b97c74f87dcf761
|
|
| MD5 |
a41d25aaeec70072a4c22fb1b8cb18c9
|
|
| BLAKE2b-256 |
122a88603b775eeb77694388d7d87546f2357aa67f3727dba1ba3d788088c3e7
|
File details
Details for the file gretl4py-0.60-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: gretl4py-0.60-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 61.1 MB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6704a3d7f347d050bd72a0e5d9fcafe4e871c45db421c882bc56fe96d8b3cce1
|
|
| MD5 |
9c6caca38f84d5cc165442314573a07f
|
|
| BLAKE2b-256 |
74676616eef9757da235c897b3551d9899a176448a782e77e4b0087a72760f1e
|
File details
Details for the file gretl4py-0.60-cp313-cp313-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: gretl4py-0.60-cp313-cp313-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 42.4 MB
- Tags: CPython 3.13, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
069297137d84cd7a17de41b0dec37024314f2e40bfbb5611eec6eb783d78d700
|
|
| MD5 |
6af226c0ffcec7147c17ac2db81583aa
|
|
| BLAKE2b-256 |
3854e9e1cfd4c2ea002d6df3c7472a6109ae2e5bdc908a20d6d486e11900df2a
|
File details
Details for the file gretl4py-0.60-cp313-cp313-macosx_10_15_universal2.whl.
File metadata
- Download URL: gretl4py-0.60-cp313-cp313-macosx_10_15_universal2.whl
- Upload date:
- Size: 36.5 MB
- Tags: CPython 3.13, macOS 10.15+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db434948682c1f370b4be219fffd17654c8e65f122c62523e59052c88c204f0d
|
|
| MD5 |
b03483b069dfbfca0c7be38a16b7836a
|
|
| BLAKE2b-256 |
dde6c420f3c2c833eeb3c69348d081f0cb90629cc7f041642340668ab6ff4c40
|
File details
Details for the file gretl4py-0.60-cp312-cp312-win_arm64.whl.
File metadata
- Download URL: gretl4py-0.60-cp312-cp312-win_arm64.whl
- Upload date:
- Size: 47.7 MB
- Tags: CPython 3.12, Windows ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6c92b635c8d5d732f924faaa8e09d9b2156c921dc47c40f38efa5ba51a7ac92
|
|
| MD5 |
513e0d5d1f6c3c52d9b89f02d7213af1
|
|
| BLAKE2b-256 |
1ce8e712d8483446a5d0ec217d23672584e97602bb2b5c5100f586cfbebf0280
|
File details
Details for the file gretl4py-0.60-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: gretl4py-0.60-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 61.1 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3b91bd9f34668ddb37cf9dc36dea4b93c389aa366104b083ea397bf1b3eb4fd
|
|
| MD5 |
fe00329f8b2a275ae73bf01a522a1c5e
|
|
| BLAKE2b-256 |
b35db1e8db93b2e34357dd2cb11d099df209135c01f596e48f9d825edd582395
|
File details
Details for the file gretl4py-0.60-cp312-cp312-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: gretl4py-0.60-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 42.4 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
731a424eafe0a4404fd551422d57b60ff013a2e1239b79965d1a40f6c6c82682
|
|
| MD5 |
f2390d86c11f4951e0967c930ff5adce
|
|
| BLAKE2b-256 |
488e292c1772fc548227e79758427870bac0fe22a5154122e30c3af27f236d11
|
File details
Details for the file gretl4py-0.60-cp312-cp312-macosx_10_15_universal2.whl.
File metadata
- Download URL: gretl4py-0.60-cp312-cp312-macosx_10_15_universal2.whl
- Upload date:
- Size: 36.5 MB
- Tags: CPython 3.12, macOS 10.15+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88110f1aba8ec063adf8d5f1a5dc5d99efbe1e3dcac6dccf438069273fe06d0b
|
|
| MD5 |
3673242e453704ede63fc2ac5b38b1c1
|
|
| BLAKE2b-256 |
4cf4f54954cfecd635d2549cafce9dd5fb4d184217e0f26710e221eaa05fe1c6
|
File details
Details for the file gretl4py-0.60-cp311-cp311-win_arm64.whl.
File metadata
- Download URL: gretl4py-0.60-cp311-cp311-win_arm64.whl
- Upload date:
- Size: 47.7 MB
- Tags: CPython 3.11, Windows ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5e6edd4e23a1de20798172d0b8354affaf9dc2ef8fea401aa3398616f32be8b
|
|
| MD5 |
168d07701a57e16e4383be163254449e
|
|
| BLAKE2b-256 |
f5b5889280b31b30a8658a22928b9f8e2b8024cad09e772f3f2796ee9f72b21d
|
File details
Details for the file gretl4py-0.60-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: gretl4py-0.60-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 61.1 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0b42f6db02fef162d954bf91619b927f8d4de193328781fd6566542fc193b8e
|
|
| MD5 |
65b193286906c58e764ab8ade2de0342
|
|
| BLAKE2b-256 |
d845fd75b502b9d2a562959717085a6cad25679ef625669b8f375d34ac82958a
|
File details
Details for the file gretl4py-0.60-cp311-cp311-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: gretl4py-0.60-cp311-cp311-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 42.4 MB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5377688814d5927254694686e307e665d7f731df26915ffcb175950017ddb424
|
|
| MD5 |
8e36cf2cdb0633a4a3d1a6a49ea97f68
|
|
| BLAKE2b-256 |
7642102b8c82483f235a204ad9d4ac2dff2a8238b3ee2194ca83dddda31445de
|
File details
Details for the file gretl4py-0.60-cp311-cp311-macosx_10_15_universal2.whl.
File metadata
- Download URL: gretl4py-0.60-cp311-cp311-macosx_10_15_universal2.whl
- Upload date:
- Size: 36.5 MB
- Tags: CPython 3.11, macOS 10.15+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7fe5a236ece37ec4aca337f4d6fbe0c54735f7eaf50fcbe7f4494c17aa0454e6
|
|
| MD5 |
71270f6f49ddb5bf0f9a824b0f927e52
|
|
| BLAKE2b-256 |
79b2b6dc14052e5813d0a6676fb75b849727fbe40c503099e3d9d1df77231b88
|