Skip to main content
                _   _   ___
               | | | | /   |
  __ _ _ __ ___| |_| |/ /| |_ __  _   _
 / _` | '__/ _ \ __| / /_| | '_ \| | | |
| (_| | | |  __/ |_| \___  | |_) | |_| |
 \__, |_|  \___|\__|_|   |_/ .__/ \__, |
  __/ |                    | |     __/ |
 |___/                     |_|    |___/

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, bundles, 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 functionality implemented in hansl packages directly usable from Python.
  • 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_pandas()

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;
  • conversion to NumPy;
  • conversion to pandas;
  • zero-copy NumPy views where appropriate.

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:

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.


Function packages

One of gretl's distinctive features is its function-package system. Function packages extend gretl by providing additional functionality implemented in hansl, gretl's scripting language.

gretl4py provides a bridge to this ecosystem: functions defined in gretl function packages can be loaded and used as Python functions. This means that the Python interface is not limited to functionality implemented directly in the gretl4py bindings. A gretl function package can effectively extend the Python API.

Loading a package

A package is loaded with gretl.include():

import gretl

BMA = gretl.include("BMA")

The returned package object can be inspected, for example:

BMA.show_functions()

After a package has been included, its functions are available through the gretl module. For example:

import gretl

BMA = gretl.include("BMA")

data = gretl.get_data("FLS_41t72.gdt", frompkg="BMA")

opt = gretl.Bundle()
opt["Nrep"] = 10**5
opt["model_prior"] = "binomial"
opt["Nrank"] = 5

result = gretl.BMA(
    Y="GDP_growth",
    X_list="2..13",
    quiet=True,
    Options=opt
)

gretl.BMA_Print(result)

The important point is that BMA() and BMA_Print() are functions supplied by the gretl function package rather than native gretl4py bindings.

Package functions and gretl objects

Package functions can exchange gretl4py objects with the underlying hansl code. For example, a package function can receive a gretl.Matrix, a NumPy view, a gretl.Bundle, a dataset, or other supported gretl objects.

For example, the extra package can be used with both a gretl matrix and its NumPy view:

import gretl

extra = gretl.include("extra")

m = gretl.Matrix(4, 1, "normal")

print(gretl.combinations(m, 1))
print(gretl.combinations(m.numpy_view, 1))

This allows package functions to participate naturally in Python workflows while retaining the functionality implemented in the original hansl package.

Packages demonstrated by gretl4py

The repository currently contains package examples for:

Package Main functionality
BACE Bayesian Averaging of Classical Estimates
BayTool Bayesian regression methods
BMA Bayesian Model Averaging
BVAR Bayesian VAR models
criteria Model-selection criteria
gig GARCH models
ParMA Parallel Bayesian Model Averaging
SVAR Structural VAR models
TVC Time-varying coefficient models

The corresponding examples are located in:

gretl/examples/packages/

The repository also contains a combined package demonstration in:

demo/packages.py

These examples are intended to show not only how a package is loaded, but also how package functions interact with native gretl4py objects.

Package ecosystem

The gretl function-package ecosystem is considerably larger than the examples included with gretl4py. It contains official gretl add-ons as well as a large collection of contributed packages covering, among other areas:

  • Bayesian methods and model averaging;
  • unit-root, stationarity and structural-break tests;
  • cointegration and long-run analysis;
  • univariate time-series models;
  • VAR, SVAR and factor models;
  • volatility and financial econometrics;
  • panel-data models;
  • discrete and limited-dependent-variable models;
  • hypothesis testing and diagnostics;
  • estimation and regression tools;
  • forecasting and prediction;
  • machine learning and nonparametric methods;
  • spatial econometrics;
  • graphics and visualization;
  • data access and management;
  • programming utilities and GUI tools.

The package mechanism is therefore an important part of the overall gretl4py architecture: it allows the Python interface to benefit from the existing and continuously growing gretl function-package ecosystem without requiring every package to be reimplemented as a separate Python extension.


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, accessing functionality implemented in gretl function packages, and using 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
))

Package examples

Examples demonstrating gretl function packages are located in:

gretl/examples/packages/

They provide focused examples of loading and using individual packages.

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, function 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:

  1. libgretl and its plugins together with the required runtime dependencies;
  2. the compiled _gretl Python extension module providing the binding to libgretl;
  3. Python-level helper modules and utilities;
  4. example modules and scripts;
  5. 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 available from the SourceForge files area.

The documentation is actively evolving alongside the Python API.

The source code, examples, demonstrations, and development history are available in the gretl4py repository.


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.

Metadata

Release files for gretl4py 0.62

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gretl4py 0.62
File Size Uploaded
gretl4py-0.62.tar.gz 12.9 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for gretl4py 0.62
File
gretl4py-0.62-cp314-cp314-win_arm64.whl CPython 3.14 CPython 3.14 Windows ARM64 Details
gretl4py-0.62-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
gretl4py-0.62-cp314-cp314-manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64 Details
gretl4py-0.62-cp314-cp314-macosx_10_15_universal2.whl CPython 3.14 CPython 3.14 macOS 10.15+ universal2 (ARM64, x86-64) Details
gretl4py-0.62-cp313-cp313-win_arm64.whl CPython 3.13 CPython 3.13 Windows ARM64 Details
gretl4py-0.62-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
gretl4py-0.62-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details
gretl4py-0.62-cp313-cp313-macosx_10_15_universal2.whl CPython 3.13 CPython 3.13 macOS 10.15+ universal2 (ARM64, x86-64) Details
gretl4py-0.62-cp312-cp312-win_arm64.whl CPython 3.12 CPython 3.12 Windows ARM64 Details
gretl4py-0.62-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
gretl4py-0.62-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details
gretl4py-0.62-cp312-cp312-macosx_10_15_universal2.whl CPython 3.12 CPython 3.12 macOS 10.15+ universal2 (ARM64, x86-64) Details

Total release size: 557.8 MB

Release files / gretl4py-0.62.tar.gz

Download URL gretl4py-0.62.tar.gz
Size 12.9 MB
Tags Source
SHA-256 checksum
How to use checksums
57da86c32b743fdcc25de127ef896e815167438c073ddd2ca2e49c907591e162
BLAKE2b-256 checksum
How to use checksums
7f84d8f84e18236939b8a2f7404500a5391db9172c9f94b0da834ccc80f3d95f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp314-cp314-win_arm64.whl

Download URL gretl4py-0.62-cp314-cp314-win_arm64.whl
Size 43.5 MB
Tags CPython 3.14 Windows ARM64
SHA-256 checksum
How to use checksums
c26eb01148344f77fcdd64f56c11febc01941ec0cbec640bae6150490fc12a28
BLAKE2b-256 checksum
How to use checksums
1f99278accb7d494cc690dc27b0b38eac437dac4cb22953e1d7531f67c8f270d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp314-cp314-win_amd64.whl

Download URL gretl4py-0.62-cp314-cp314-win_amd64.whl
Size 60.6 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
f701917b12e76d4a1899dd281b7eba3e74e043075e072c3bca3397f22b653de9
BLAKE2b-256 checksum
How to use checksums
1e6b0f94ffe09d3a69054704ff8880fa5b7ce78014b8d1751feb8a6ea409917a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp314-cp314-manylinux_2_28_x86_64.whl

Download URL gretl4py-0.62-cp314-cp314-manylinux_2_28_x86_64.whl
Size 42.5 MB
Tags CPython 3.14 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
317e6db9c137fbf229691c09fd5d0ce9c6a13e12dc30d9d6c2566e05d9de966b
BLAKE2b-256 checksum
How to use checksums
aa205c3492abb8d93c90655c3dee3760ceb5890080bc3a6ffc8788e5a366ecc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp314-cp314-macosx_10_15_universal2.whl

Download URL gretl4py-0.62-cp314-cp314-macosx_10_15_universal2.whl
Size 36.7 MB
Tags CPython 3.14 macOS 10.15+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
7a3a99e72158b082bb4a5a2d44e7bbd23a9f4a5c66b55019f1f50bb90633b12e
BLAKE2b-256 checksum
How to use checksums
5a2464877bf77bf6ec3b8aff9e95f8666a66f40f4299e56c200300b79bfbbfb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp313-cp313-win_arm64.whl

Download URL gretl4py-0.62-cp313-cp313-win_arm64.whl
Size 42.2 MB
Tags CPython 3.13 Windows ARM64
SHA-256 checksum
How to use checksums
7a22a49773ddbc1c89f05577ba4babce4c2d7eaea508fbde93f79b10bebea927
BLAKE2b-256 checksum
How to use checksums
73d645463c1458774a65cb13e0aed4bb6adedf9c0a65c40bb592e10c33f9e587
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp313-cp313-win_amd64.whl

Download URL gretl4py-0.62-cp313-cp313-win_amd64.whl
Size 59.3 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
beee326f891dd6a2fd617c1e8d9ea3d9b1ac52a54af99195f4528392af44abab
BLAKE2b-256 checksum
How to use checksums
35b177771d2dbd106d0cb7259705b828b78513082dae3258f2f983e2c680289e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp313-cp313-manylinux_2_28_x86_64.whl

Download URL gretl4py-0.62-cp313-cp313-manylinux_2_28_x86_64.whl
Size 42.5 MB
Tags CPython 3.13 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
5099be9a43d029113a81d1f94a46af9ca4a21ef1f81c64efaf49a8e0e935a7c3
BLAKE2b-256 checksum
How to use checksums
bd92fdda87ecf8b67bdbb7f1f0ab8a924c0db6500761700878247a6b4a6a1ffb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp313-cp313-macosx_10_15_universal2.whl

Download URL gretl4py-0.62-cp313-cp313-macosx_10_15_universal2.whl
Size 36.7 MB
Tags CPython 3.13 macOS 10.15+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
9b0609e9c68217b1969fb63a107dc8ae374004eaffd6b9b7bc1cbeab9ce92ade
BLAKE2b-256 checksum
How to use checksums
ec8bcc1d0a7721c5f708629c6b836cf4ba8fe3bacadf160139f01f13c71a3512
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp312-cp312-win_arm64.whl

Download URL gretl4py-0.62-cp312-cp312-win_arm64.whl
Size 42.2 MB
Tags CPython 3.12 Windows ARM64
SHA-256 checksum
How to use checksums
9812d86540b82087691dc2433a170e119c3eac6f547d00613f2bb27ec731658d
BLAKE2b-256 checksum
How to use checksums
c4e2a8d47dab3f1b257bd9cec5128d09a4ad094face4b22a25ef6aab855cee26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp312-cp312-win_amd64.whl

Download URL gretl4py-0.62-cp312-cp312-win_amd64.whl
Size 59.3 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
441cfb8031262c9e3abdcca6deccb17e80feaa522338fc837f1054ccaab99a65
BLAKE2b-256 checksum
How to use checksums
518e23189b37500e23d1d68b1d9d1ba3122f97ffc5ef27443f09d635138125ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL gretl4py-0.62-cp312-cp312-manylinux_2_28_x86_64.whl
Size 42.5 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
142cab6fd4da1da717c768c4b313b8ced420b431c065f624cc948f6eed984c95
BLAKE2b-256 checksum
How to use checksums
b0d35e56d36732b16feac1072fd4bcc9136a5e7683542d9e8efc260dadbe699f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / gretl4py-0.62-cp312-cp312-macosx_10_15_universal2.whl

Download URL gretl4py-0.62-cp312-cp312-macosx_10_15_universal2.whl
Size 36.7 MB
Tags CPython 3.12 macOS 10.15+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
bef01e85523e6e0446986de113e8034fae54e37c6e44343b6785994ee105b484
BLAKE2b-256 checksum
How to use checksums
ca42d5a24bd60ae6fd10674a21b72e88ff01545e1a36b0c7fc263d34e1312cc2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

0.63

13 release files

This release

0.62 This release

13 release files

0.61

17 release files

0.60

17 release files

0.50

21 release files

0.4

17 release files

0.2

10 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page