Adaptive Momentum Gradient Descent for Regularized Poisson Regression
Project description
AMGD: Adaptive Momentum Gradient Descent
A Python implementation of Adaptive Momentum Gradient Descent (AMGD) for high-dimensional sparse Poisson regression with L1 and Elastic Net regularization. AMGD combines adaptive learning rates with momentum-based updates and specialized soft-thresholding to achieve superior performance in feature selection and optimization efficiency.
๐ Key Features
- ๐ฏ Optimized for Poisson regression with automatic feature selection
- โก Superior convergence compared to Adam, AdaGrad, and GLMnet
- ๐ง Adaptive soft-thresholding for effective L1 and Elastic Net regularization
- ๐ High-dimensional support with excellent scalability (tested up to 1000+ features)
- ๐ Built-in benchmarking tools for algorithm comparison
- ๐ Extensible framework supporting custom penalties and GLM families
- ๐ Comprehensive visualization for convergence analysis and coefficient paths
๐ Performance Highlights
AMGD demonstrates effecient performance in regularized Poisson regression, as validated by comprehensive experiments on ecological count data:
| Metric | AMGD vs Adam | AMGD vs AdaGrad | AMGD vs GLMnet |
|---|---|---|---|
| MAE | 2.7% lower | 56.6% lower | 66.4% lower |
| RMSE | 2.9% lower | 49.2% lower | 59.0% lower |
| MPD | 3.0% lower | 81.0% lower | 92.4% lower |
| Sparsity | 11ร higher | โ (from 0%) | โ (from 0%) |
| Runtime | 2ร faster | >370ร faster | 20ร faster |
| Stat. Significance | p < 0.0001 (all) | โ Large effect sizes |
โ
Achieves 35.29% model sparsity (vs 11.76% for Adam, 0% for AdaGrad and GLMnet)
โ
Bootstrapped performance is stable across 1000 runs with tight confidence intervals
โ
Adaptive thresholding outperforms static regularization, enabling effective feature selection
โ
Handles high-dimensional data with robust convergence and minimal tuning
โAMGD provides up to 56.6% MAE reduction over AdaGrad, and outperforms Adam on every metric while selecting significantly fewer features.โ
โ Bakari & รzkale (2025)
๐ฆ Installation
Quick Install from PyPI
pip install amgd
Development Installation
git clone https://github.com/elbakari01/amgd.git
cd amgd
pip install -e .
Dependencies
numpy>=1.21.0
scipy>=1.7.0
scikit-learn>=1.0.0
matplotlib>=3.3.0
seaborn>=0.11.0
โก Quick Start
30-Second Example
import numpy as np
from amgd.models import PoissonRegressor
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
# Generate sample data
X, y = make_regression(n_samples=1000, n_features=50, noise=0.1, random_state=42)
y = np.abs(y.astype(int)) # Convert to count data for Poisson
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train AMGD model
model = PoissonRegressor(optimizer='amgd', penalty='l1', lambda1=0.1)
model.fit(X_train, y_train)
# Evaluate
print(f"Test score: {model.score(X_test, y_test):.4f}")
print(f"Selected features: {np.sum(model.coef_ != 0)}/{len(model.coef_)}")
Basic Poisson Regression
from amgd.models import PoissonRegressor
# Create and train model
model = PoissonRegressor(
optimizer='amgd',
penalty='l1',
lambda1=0.1,
max_iter=1000,
verbose=True
)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate performance
from amgd.utils.metrics import poisson_deviance
deviance = poisson_deviance(y_test, y_pred)
print(f"Poisson deviance: {deviance:.4f}")
Elastic Net for Grouped Feature Selection
# Best for correlated features
model = PoissonRegressor(
optimizer='amgd',
penalty='elasticnet',
lambda1=0.05, # L1 penalty (sparsity)
lambda2=0.05, # L2 penalty (grouping)
max_iter=1000
)
model.fit(X_train, y_train)
# Analyze sparsity
sparsity = np.mean(model.coef_ == 0)
print(f"Sparsity: {sparsity:.2%}")
print(f"Non-zero coefficients: {np.sum(model.coef_ != 0)}")
๐ Comprehensive Examples
1. Real-World Example: Species Count Prediction
from amgd.models import PoissonRegressor
from amgd.visualization import plot_convergence, plot_feature_importance
import pandas as pd
# Load your ecological data
# X: environmental features (temperature, rainfall, etc.)
# y: species counts
data = pd.read_csv('ecological_data.csv')
X = data[['temperature', 'rainfall', 'elevation', 'soil_ph']].values
y = data['species_count'].values
# Cross-validation for hyperparameter tuning
from sklearn.model_selection import cross_val_score
lambda_values = np.logspace(-4, 1, 20)
cv_scores = []
for lam in lambda_values:
model = PoissonRegressor(optimizer='amgd', penalty='l1', lambda1=lam)
scores = cross_val_score(model, X, y, cv=5, scoring='neg_mean_poisson_deviance')
cv_scores.append(-scores.mean())
best_lambda = lambda_values[np.argmin(cv_scores)]
# Train final model
model = PoissonRegressor(optimizer='amgd', penalty='l1', lambda1=best_lambda)
model.fit(X, y)
# Visualize results
plot_convergence(model.loss_history_, title="AMGD Convergence")
plot_feature_importance(model.coef_, feature_names=['Temperature', 'Rainfall', 'Elevation', 'Soil pH'])
2. Algorithm Benchmarking
from amgd.benchmarks import compare_optimizers
# Compare multiple optimizers
results = compare_optimizers(
X, y,
optimizers=['amgd', 'adam', 'adagrad'],
penalties=['l1', 'elasticnet'],
cv_folds=5,
test_size=0.2,
random_state=42
)
# View results
print("Cross-Validation Results:")
print(results['cv_results'])
print("\nTest Set Results:")
print(results['test_results'])
# Statistical significance testing
from amgd.benchmarks import statistical_significance_test
stats_results = statistical_significance_test(
X, y,
optimizers=['amgd', 'adam'],
n_runs=50
)
3. Custom Penalty Functions
from amgd.core.penalties import PenaltyBase
import numpy as np
class AdaptiveLassoPenalty(PenaltyBase):
"""Adaptive Lasso with feature-specific weights."""
def __init__(self, lambda1, weights):
self.lambda1 = lambda1
self.weights = weights
def __call__(self, x):
return self.lambda1 * np.sum(self.weights * np.abs(x))
def gradient(self, x):
return self.lambda1 * self.weights * np.sign(x)
def proximal_operator(self, x, step_size):
threshold = self.lambda1 * self.weights * step_size
return np.sign(x) * np.maximum(np.abs(x) - threshold, 0)
# Use custom penalty
initial_weights = 1.0 / (np.abs(ridge_coef) + 0.1) # From ridge regression
penalty = AdaptiveLassoPenalty(lambda1=0.1, weights=initial_weights)
model = PoissonRegressor(optimizer='amgd', penalty=penalty)
model.fit(X_train, y_train)
๐งฎ Algorithm Details
AMGD combines three key innovations for superior performance:
1. Adaptive Learning Rate with Exponential Decay
ฮฑโ = ฮฑโ / (1 + ฮทt)
ฮฑโ: Initial learning rate (default: 0.01)ฮท: Decay rate (default: 0.0001)t: Current iteration
2. Bias-Corrected Momentum Updates
mโ = ฮฒโmโโโ + (1 - ฮฒโ)โf(ฮธโ) # First moment
vโ = ฮฒโvโโโ + (1 - ฮฒโ)(โf(ฮธโ))ยฒ # Second moment
mฬโ = mโ / (1 - ฮฒโแต) # Bias correction
vฬโ = vโ / (1 - ฮฒโแต) # Bias correction
3. Adaptive Soft-Thresholding for Sparsity
ฮธโฑผ โ prox_ฮป(ฮธโฑผ - ฮฑโโf(ฮธโฑผ))
where prox_ฮป(x) = sign(x) ยท max(|x| - ฮป, 0)
Why AMGD Works Better
| Component | Traditional Methods | AMGD Innovation | Benefit |
|---|---|---|---|
| Learning Rate | Fixed or simple decay | Adaptive + exponential decay | Better convergence |
| Momentum | Standard momentum | Bias-corrected dual momentum | Faster escape from local minima |
| Regularization | Basic soft-thresholding | Adaptive thresholding | Superior feature selection |
๐ ๏ธ Advanced Features
Model Persistence
import joblib
# Save trained model
model.fit(X_train, y_train)
joblib.dump(model, 'amgd_model.pkl')
# Load and use
loaded_model = joblib.load('amgd_model.pkl')
predictions = loaded_model.predict(X_new)
Warm Starting for Hyperparameter Tuning
model = PoissonRegressor(optimizer='amgd', warm_start=True)
# Progressive training
for lambda_val in [0.1, 0.05, 0.01]:
model.lambda1 = lambda_val
model.fit(X_train, y_train)
print(f"Lambda {lambda_val}: Score = {model.score(X_val, y_val):.4f}")
Custom Convergence Criteria
from amgd.core.convergence import RelativeChangeCriterion
# Custom convergence
criterion = RelativeChangeCriterion(tol=1e-8, patience=10)
model = PoissonRegressor(
optimizer='amgd',
convergence_criterion=criterion,
max_iter=2000
)
Advanced Visualization
from amgd.visualization import (
plot_convergence_comparison,
plot_coefficient_path,
plot_feature_importance
)
# Compare convergence of different optimizers
histories = {
'AMGD': model_amgd.loss_history_,
'Adam': model_adam.loss_history_,
'AdaGrad': model_adagrad.loss_history_
}
plot_convergence_comparison(histories)
# Coefficient regularization path
lambdas = np.logspace(-4, 1, 50)
coef_path = np.array([fit_model(lam).coef_ for lam in lambdas])
plot_coefficient_path(lambdas, coef_path, feature_names=feature_names)
๐ Use Cases
๐งฌ Bioinformatics
- Gene expression analysis
- Variant calling
- Protein abundance prediction
๐ Ecology & Environmental Science
- Species abundance modeling
- Biodiversity prediction
- Climate impact assessment
๐ Marketing & Business
- Customer count prediction
- Website traffic modeling
- Demand forecasting
๐ฅ Healthcare
- Patient admission counts
- Disease occurrence modeling
- Treatment response analysis
๐ง API Reference
Main Classes
PoissonRegressor
Main class for Poisson regression with AMGD optimization.
PoissonRegressor(
optimizer='amgd', # Optimizer type
penalty='l1', # Regularization type
lambda1=0.0, # L1 penalty strength
lambda2=0.0, # L2 penalty strength
alpha=0.01, # Learning rate
beta1=0.9, # First moment decay
beta2=0.999, # Second moment decay
max_iter=1000, # Maximum iterations
tol=1e-6, # Convergence tolerance
fit_intercept=True, # Fit intercept term
warm_start=False, # Use previous solution
verbose=False # Print progress
)
Key Methods
| Method | Description | Returns |
|---|---|---|
fit(X, y) |
Train the model | self |
predict(X) |
Make predictions | ndarray |
score(X, y) |
Model performance | float |
get_params() |
Get parameters | dict |
set_params(**params) |
Set parameters | self |
Utility Functions
from amgd.utils.metrics import poisson_deviance, mean_absolute_error
from amgd.utils.validation import check_array, check_is_fitted
from amgd.benchmarks import compare_optimizers, statistical_significance_test
๐งช Testing & Validation
Run the test suite:
# Install test dependencies
pip install pytest pytest-cov
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=amgd --cov-report=html
# Run specific test categories
pytest tests/test_models.py -v # Model tests
pytest tests/test_optimizers.py -v # Optimizer tests
pytest tests/test_benchmarks.py -v # Benchmark tests
๐ Performance Tips
For Large Datasets (>10k samples)
model = PoissonRegressor(
optimizer='amgd',
alpha=0.001, # Lower learning rate
max_iter=500, # Fewer iterations
tol=1e-4 # Relaxed tolerance
)
For High-Dimensional Data (>1k features)
model = PoissonRegressor(
optimizer='amgd',
penalty='elasticnet',
lambda1=0.01, # Stronger L1 penalty
lambda2=0.001, # Light L2 penalty
beta1=0.95 # Higher momentum
)
Memory Optimization
# For memory-constrained environments
model = PoissonRegressor(
optimizer='amgd',
fit_intercept=False, # Skip intercept if not needed
warm_start=True, # Reuse computations
verbose=False # Reduce memory overhead
)
๐ค Contributing
We welcome contributions! Here's how to get started:
Development Setup
# Fork and clone the repository
git clone https://github.com/yourusername/amgd.git
cd amgd
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
Contribution Guidelines
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Write tests for your changes
- Ensure all tests pass (
pytest tests/) - Follow PEP 8 style guide (
flake8 src/) - Commit your changes (
git commit -m 'Add amazing feature') - Push to your branch (
git push origin feature/amazing-feature) - Open a Pull Request
Areas We Need Help
- ๐ Bug fixes and performance improvements
- ๐ Documentation and examples
- ๐งช Test coverage expansion
- ๐ Internationalization support
- ๐ New visualization tools
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Citation
If you use AMGD in your research, please cite our paper:
@article{bakari2025amgd,
title={Adaptive Momentum Gradient Descent: A Novel Optimization Algorithm for Regularized Poisson Regression},
author={Bakari, Ibrahim and รzkale, M. Revan},
journal={arXiv preprint arXiv:2025.XXXXX},
year={2025},
note={Submitted for publication}
}
๐ Acknowledgments
- รukurova University - Department of Statistics
- Scientific Community - For valuable feedback and contributions
- Open Source Libraries - NumPy, SciPy, scikit-learn, matplotlib
- Beta Testers - Early adopters who helped improve the package
๐ Project Stats
๐ค Who Should Use This?
AMGD is ideal for:
- ๐ฌ Researchers working with high-dimensional count data
- ๐ Data scientists needing feature selection in Poisson models
- โ๏ธ ML engineers building scalable sparse regression pipelines
- ๐ Statisticians exploring novel regularization techniques
- ๐งฌ Bioinformaticians analyzing gene expression or variant data
- ๐ Environmental scientists modeling species abundance and biodiversity
- ๐ฅ Healthcare analysts predicting patient counts and disease occurrence
Perfect For These Scenarios:
โ
High-dimensional data (p > n problems)
โ
Sparse feature selection required
โ
Count/rate data modeling
โ
Correlated predictors present
โ
Interpretable models needed
๐ Roadmap
Planned enhancements for future releases:
๐ Version 1.5.0 (Q3 2025)
- ๐ GPU acceleration via CuPy support
- ๐ Cross-validation wrappers for easier hyperparameter tuning
- ๐ Feature importance stability plots and analysis
๐ Version 1.6.0 (Q4 2025)
- ๐ฏ Bayesian priors for adaptive regularization
- ๐ Enhanced scikit-learn integration and pipeline support
- ๐ Multi-task learning extensions
๐ Version 2.0.0 (Q1 2026)
- ๐ Distributed computing support
- ๐ง Neural network integration for deep sparse models
- ๐ฑ Mobile deployment optimizations
๐ Help Wanted
Vote for features or contribute:
- ๐ณ๏ธ Feature Requests
- ๐ป Good First Issues
- ๐ Documentation Needs
๐ง Support & Contact
- ๐ง Email: acbrhmbakari@gmail.com
- ๐ Issues: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
- ๐ Documentation: Read the Docs
โญ Star this repository if you find it useful! โญ
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
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 amgd-1.4.8-py3-none-any.whl.
File metadata
- Download URL: amgd-1.4.8-py3-none-any.whl
- Upload date:
- Size: 70.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e7a59716ae53f2b5389821b786e70fefab1d2650cfe298a1ebff18ca53e5d0d
|
|
| MD5 |
420ab733538d9f7ab21a2ad2aeeff133
|
|
| BLAKE2b-256 |
aefb0a864f16789dedf623826fb2855214c7ab0fc15223276009405229de95c3
|