Skip to main content

GLMs in Python.

Project description

CI coverage Python License PyPI

PyGLMs (Turtles)

An implementation of various Generalized Linear Models (GLMs), written in Python.

I created this package as a refresher on GLMs and the underlying optimization techniques. It's intended as a learning tool and a reference for building and understanding these models from the ground up.

Overview

The code is packaged as a Python library named turtles (I like turtles), making the code easy to integrate into your own projects.

The package is written using numpy for linear algebra operations, scipy for (some) optimization, pandas for displaying tabular results, and matplotlib for plots.

The following models have been implemented:

  1. Multiple Linear Regression (turtles.stats.glms.MLR class)
  2. Logistic Regression (turtles.stats.glms.LogReg class, uses GLM parent class)
  3. Poisson Regression (turtles.stats.glms.PoissonReg class, uses GLM parent class)

The GLM parent class supports three optimization methods for parameter estimation: Momentum-based Gradient Descent for first-order optimization, Newton's Method for second-order optimization, and Limited-memory Broyden–Fletcher–Goldfarb–Shanno (L-BFGS). The user can specify the desired optimization method during class instantiation.

Momentum-based Gradient Descent and Newton's Method are implemented in Python as part of the turtles distribution. L-BFGS is implemented using scipy.optimize; it's a quasi-Newton method that approximates the Hessian (instead of fully computing it, like Newton's Method), so it's quite fast.

See examples/{class name}_example.ipynb for simple examples of using each model class and various supporting functions.

Usage

You can pip install the package from PyPI:

pip install turtles-glms

Contributing

To run (and edit) this project locally, clone the repo and create your virtual environment from project root using your global (or local) Python version. This project requires Python 3.10+.

python -m venv

Activate the env (source .venv/Scripts/activate for Windows OS, source .venv/bin/activate for Linux) and install dependencies:

pip install -e .[dev]

Optionally, you can execute scripts/env.sh to create and activate a virtual environment using uv. The uv package manager must be installed for this to work.

Adding GLMs

To add more GLM classes, use the GLM parent class for inheritence (see PoissonReg and LogReg as examples). The GLM parent class provides a solid framework for implementing new child classes and should be used whenever possible. Unimplemented GLMs include Negative Binomial, Gamma, and Tweedie.

Testing

All tests are contained within tests directories for each module. You can simply execute the pytest command from project root to run all unit tests.

pytest

Notes on Test Coverage:

  • Plotting functions from turtles.plotting are tested, but plotting methods in GLM classes (like MLR) are ignored. Those class methods are essentially just wrappers around matplotlib and turtles.plotting functions.
  • GLM class methods that are meant to be implemented by child classes are ignored.

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

turtles_glms-1.0.0.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

turtles_glms-1.0.0-py3-none-any.whl (26.9 kB view details)

Uploaded Python 3

File details

Details for the file turtles_glms-1.0.0.tar.gz.

File metadata

  • Download URL: turtles_glms-1.0.0.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for turtles_glms-1.0.0.tar.gz
Algorithm Hash digest
SHA256 30649e6466458ff9b449eeefcdecaaaac65564010fccdae4c55dffb3a73394a1
MD5 e722c992fcc3956f416fa1a266e230d2
BLAKE2b-256 c4eb9e7471ab202e53ce768fd3dc4e701dacc498f342f3a43ef788ff2d2cec65

See more details on using hashes here.

File details

Details for the file turtles_glms-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: turtles_glms-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 26.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for turtles_glms-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a348deb9eeac6369106e7e9602f4125bd68b7d3dbc88327f222ba594f50ef21e
MD5 f88d7affd6d482573be6f8609a88f7f0
BLAKE2b-256 26f71c1734d3e07e9563e19403f4dba230d7c705cddd60f338e30eb0cd0afeb4

See more details on using hashes here.

Supported by

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