Skip to main content

A library for federated learning and personalized federated learning with Generalized Linear Models

Project description

# Federated GLM

[![PyPI - Version](https://img.shields.io/pypi/v/federated-glm.svg)](https://pypi.org/project/federated-glm/)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
[![Python Versions](https://img.shields.io/pypi/pyversions/federated-glm.svg)](https://pypi.org/project/federated-glm/)

**Federated GLM** is a Python package for simulating **federated learning** with **generalized linear models (GLMs)**. It supports Gaussian, Binomial, and Poisson families, and allows flexible experimentation with elastic net regularization, client data partitioning, and convergence diagnostics.

---

## ๐Ÿ”ง Features

- Federated learning framework with `average` or `weighted` aggregation
- Supports **Gaussian**, **Binomial**, and **Poisson** GLM families
- Elastic Net, Lasso, and Ridge regularization (proximal updates)
- Utilities for synthetic data generation and partitioning across clients
- Model evaluation with Rยฒ, RMSE, accuracy, Poisson deviance, etc.
- Examples for comparing centralized and federated learning
- Simple API and complete test coverage

---

## ๐Ÿ“ฆ Installation

```bash
pip install git+https://github.com/mhmdamini/federated-glm.git

To install with development or example dependencies:

pip install "federated-glm[dev]"
pip install "federated-glm[examples]"

๐Ÿ›  Quick Start

Hereโ€™s a minimal example using Gaussian regression:

from federated_glm import PersonalizedFederatedGLM, FederatedLearningManager, DataGenerator, ModelEvaluator

# Generate synthetic data
X, y, family = DataGenerator.generate_glm_data("gaussian", n=200, p=3)

# Split train/test
X_train, X_test = X[:150], X[150:]
y_train, y_test = y[:150], y[150:]

# Partition data across clients
client_data = DataGenerator.partition_data(X_train, y_train, n_clients=3)

# Train a federated model
manager = FederatedLearningManager()
manager.fit(client_data, family, n_rounds=10)

# Predict and evaluate
y_pred = manager.predict(X_test, family)
metrics = ModelEvaluator.evaluate(y_test, y_pred, "gaussian")

print("Rยฒ Score:", metrics["r2_score"])
print("RMSE:", metrics["rmse"])

For personalized federated learning, it will be:

pfed = PersonalizedFederatedGLM(method='pfedme')
pfed.fit(client_data, family, n_rounds=20)

Where method can be 'pfedme', 'perfedavg', and 'local_adaptation'.


๐Ÿ“ Project Structure

federated-glm/
โ”œโ”€โ”€ federated_glm/             # Core package
โ”‚   โ”œโ”€โ”€ core.py                # Federated GLM base class with proximal optimization
โ”‚   โ”œโ”€โ”€ federation.py          # Federated learning manager
โ”‚   โ”œโ”€โ”€ evaluation.py          # Model evaluation metrics
โ”‚   โ”œโ”€โ”€ utils.py               # Data generation & partitioning utilities
โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ personalized.py.       # personalized federated learning
โ”œโ”€โ”€ examples/                  # Usage examples
โ”‚   โ””โ”€โ”€ basic_example.py
โ”‚   โ””โ”€โ”€ simple_example.py
โ”‚   โ””โ”€โ”€ personalized_example.py
โ”œโ”€โ”€ tests/                     # Unit and integration tests
โ”‚   โ””โ”€โ”€ test_basic.py
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ setup.py
โ”œโ”€โ”€ LICENSE
โ””โ”€โ”€ README.md

๐Ÿ“ˆ Examples

Run the full demo script:

python examples/basic_example.py

This includes:

  • Federated and personalized federated learning vs centralized performance comparison
  • Convergence visualization
  • Comparison of regularization strategies (ordinary, lasso, elastic net)

โœ… Supported GLM Families

Family Link Function Use Case
Gaussian Identity Regression on continuous targets
Binomial Logit Binary classification
Poisson Log Count data modeling

๐Ÿงช Testing

To run tests:

pip install "federated-glm[dev]"
pytest tests/

๐Ÿ“š Documentation

Documentation is available in the GitHub README.


๐Ÿ“œ License

This project is licensed under the MIT License.


๐Ÿ‘จโ€๐Ÿ’ป Author

Mohammad Amini Ph.D. Student at University of Florida m.amini@ufl.edu GitHub: @mhmdamini


๐Ÿ™Œ Acknowledgements

  • Built on StatsModels and Scikit-learn
  • Inspired by research in federated learning, GLMs, and distributed optimization

๐Ÿ“ฌ Contributing & Issues

Please open issues or submit pull requests via the GitHub repository.

We welcome contributions to:

  • Support more GLM families (e.g., Negative Binomial, Gamma)
  • Extend to real-world datasets
  • Add differential privacy or secure aggregation

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

federated_glm-0.2.0.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

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

federated_glm-0.2.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file federated_glm-0.2.0.tar.gz.

File metadata

  • Download URL: federated_glm-0.2.0.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for federated_glm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3beb8c97dabde7633b9e2989f871047f0d623ed47d5efff6f669a308b3d73b8e
MD5 875ea7cef723f483d19459fd9a241d6c
BLAKE2b-256 9fa0bd534727cf94f761e1be818cca57c0fae0e0b8aef2a0ed1e71f26abd503e

See more details on using hashes here.

File details

Details for the file federated_glm-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: federated_glm-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for federated_glm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75620a747da54fef910e38b7402b608c874673e90a3ca00c4cfd56bc73a8e51f
MD5 c2b8d1bb599a6c067def73e620182681
BLAKE2b-256 7c32b4df39d1ac6f1a5fc6f3889f13d7e8a0f83565d296fc2f944b4ae875f387

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