lightweight-mcnnm
Lightweight Implementation of the Matrix Completion Estimator for Panel Data presented by Athey et al. (2021)
Science Score: 57.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 3 DOI reference(s) in README -
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (12.5%) to scientific vocabulary
Keywords
Repository
Lightweight Implementation of the Matrix Completion Estimator for Panel Data presented by Athey et al. (2021)
Basic Info
Statistics
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 7
Topics
Metadata Files
README.md
lightweight-mcnnm
lightweight-mcnnm is a Python package that provides a lightweight and performant implementation of the Matrix Completion with Nuclear Norm Minimization (MC-NNM) estimator for causal inference in panel data settings.
Table of Contents
- What is lightweight-mcnnm
- Features
- Installation
- Documentation
- Using lightweight-mcnnm
- Development
- References
- Acknowledgements
- License Changelog, Contributing, and Templates
What is lightweight-mcnnm
lightweight-mcnnm implements the MC-NNM estimator exactly as described in "Matrix Completion Methods for Causal Panel Data Models" by Susan Athey, Mohsen Bayati, Nikolay Doudchenko, Guido Imbens, and Khashayar Khosravi (2021). This estimator provides a powerful tool for estimating causal effects in panel data settings, particularly when dealing with complex treatment patterns and potential confounders.
The implementation focuses on performance and minimal dependencies, making it suitable for use in various environments, including GPUs and cloud clusters.
Features
- Lightweight implementation with minimal dependencies
- Utilizes JAX for improved performance and GPU compatibility
- Faithful to the original MC-NNM algorithm as described in Athey et al. (2021)
- Suitable for large-scale panel data analysis
- Supports various treatment assignment mechanisms
- Includes unit-specific, time-specific, and unit-time specific covariates
- Offers flexible validation methods for parameter selection
Comparison to Other Implementations
lightweight-mcnnm is designed to be lightweight and easy to use, with a focus on performance and minimal dependencies.
The other two main implementations of the MC-NNM estimator are CausalTensor and
fect. Both packages implement MC-NNM as part of a broader set of causal inference methods. Both implement covariates and cross-validation differently from this package.
For a detailed comparison, see this notebook:
Installation
Requirements
lightweight-mcnnm is compatible with Python 3.10 or later and depends on JAX and NumPy. CUDA-compatible versions of Jax are not currently supported directly by lightweight-mcnnm, but you can use JAX with CUDA support by installing it separately.
Installing from PyPI
The simplest way to install lightweight-mcnnm and its dependencies is from PyPI using pip:
bash
pip install lightweight-mcnnm
To upgrade lightweight-mcnnm to the latest version, use:
bash
pip install --upgrade lightweight-mcnnm
JIT Compilation
By default, this package uses JAX's JIT compilation for better performance in typical use cases. If you want to disable JIT compilation, you can add the following line at the top of your script:
python
jax.config.update('jax_disable_jit', True)
Note that disabling JIT may impact performance depending on your specific use case. I have found leaving JIT enabled to be the best option for most use cases. An example use case where disabling JIT may be sensible is calling estimate() multiple times on datasets of different sizes, which triggers recompilation any time the input data shape changes.
Documentation
The full documentation for lightweight-mcnnm is available at: https://mcnnm.readthedocs.io/en/latest/
Using lightweight-mcnnm
- A comprehensive example is available here:
- Simple example of how to use lightweight-mcnnm: ```python import jax.numpy as jnp from lightweightmcnnm import estimate, generatedata
Y, W, X, Z, V, trueparams = generatedata( nobs=50, nperiods=10, unitfe=True, timefe=True, Xcov=True, Zcov=True, Vcov=True, seed=2024, noisescale=0.1, assignmentmechanism="staggered", treatmentprobability=0.1, )
Run estimation
results = estimate( Y=Y, Mask=W, X=X, Z=Z, V=V, Omega=None, useunitfe=True, usetimefe=True, lambdaL=None, lambdaH=None, validationmethod='cv', K=3, nlambda=30, )
print(f"\nTrue effect: {trueparams['treatmenteffect']}, Estimated effect: {results.tau:.3f}") print(f"Chosen lambdaL: {results.lambdaL:.4f}, lambdaH: {results.lambdaH:.4f}") ``` For more detailed usage instructions and examples, please refer to the documentation.
Development
Setting up the development environment
This project uses uv for dependency management. To set up your development environment:
Ensure you have uv installed. If not, install it by following the instructions on the official website.
Clone the repository:
bash git clone https://github.com/tobias-schnabel/mcnnm.git cd lightweight-mcnnmInstall the project dependencies, including local development dependencies:
bash uv sync --all-groupsThis command creates a virtual environment and installs all the necessary dependencies.
Now you're ready to start developing!
Testing and building the package
Running tests: use the following command:
bash uv run pytestCoverage: to generate a coverage report, run the following command:
bash uv run coverage reportThis will generate a coverage report showing the percentage of code covered by the tests.Building the package: run the following command:
bash uv buildThis will create both wheel and source distributions in the dist/ directory.
Development Workflow
Pre-commit Hooks
This project uses pre-commit hooks to ensure code quality and consistency. Pre-commit hooks are scripts that run automatically every time you commit changes to your version control system. They help catch common issues before they get into the codebase. To run the hooks on all files (recommended for the first setup):
bash
uv run pre-commit run --all-files
The configuration for the pre-commit hooks can be found in the .pre-commit-config.yaml file. The following hooks are configured:
• Trailing whitespace removal: Ensures no trailing whitespace is left in the code.
• End-of-file fixer: Ensures files end with a newline.
• YAML check: Validates YAML files.
• TOML check: Validates TOML files.
• ruff: Linter and Formatter
• ty: type checker
Branch Protection
To maintain the integrity of the main branch, branch protection rules are enforced. These rules ensure that all changes to the main branch go through a review process and pass all required checks.
Protected Branch Rules
- Require pull request reviews before merging: At least one approval from an administrator is required.
- Require status checks to pass before merging: All CI checks must be successful before merging.
References
This implementation is based on the method described in: Athey, S., Bayati, M., Doudchenko, N., Imbens, G., & Khosravi, K. (2021). Matrix Completion Methods for Causal Panel Data Models. Journal of the American Statistical Association, 116(536), 1716-1730.
Acknowledgements
This project was inspired by and draws upon ideas from CausalTensor and fect. I am grateful for their contributions to the field of causal inference.
Citing lightweight-mcnnm
If you use lightweight-mcnnm in your research, please cite both the software and the original paper describing the method:
For the software: Schnabel, T. (2023). lightweight-mcnnm: A Python package for Matrix Completion with Nuclear Norm Minimization. https://github.com/tobias-schnabel/lightweight-mcnnm
For the method: Athey, S., Bayati, M., Doudchenko, N., Imbens, G., & Khosravi, K. (2021). Matrix Completion Methods for Causal Panel Data Models. Journal of the American Statistical Association, 116(536), 1716-1730.
BibTeX entries:
@software{schnabel2024lightweightmcnnm, author = {Schnabel, Tobias}, title = {lightweight-mcnnm: A Python package for Matrix Completion with Nuclear Norm Minimization}, year = {2024}, url = {https://github.com/tobias-schnabel/lightweight-mcnnm} }
@article{athey2021matrix, title={Matrix completion methods for causal panel data models}, author={Athey, Susan and Bayati, Mohsen and Doudchenko, Nikolay and Imbens, Guido and Khosravi, Khashayar}, journal={Journal of the American Statistical Association}, volume={116}, number={536}, pages={1716--1730}, year={2021}, publisher={Taylor & Francis} }
License
lightweight-mcnnm is released under the GNU General Public License v3.0. See the LICENSE file for more details.
Changelog, Contributing, and Templates
- For a detailed changelog of each release, please see the GitHub Releases page
- Please refer to CONTRIBUTING.md for guidelines on how to contribute to this project.
- For reporting issues, please use the template provided in ISSUE_TEMPLATE.md
- For submitting pull requests, please use the template provided in PULLREQUESTTEMPLATE.md
Owner
- Name: Tobias Schnabel
- Login: tobias-schnabel
- Kind: user
- Location: Maastricht
- Repositories: 3
- Profile: https://github.com/tobias-schnabel
Citation (CITATION.cff)
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
- family-names: "Schnabel"
given-names: "Tobias"
title: "lightweight-mcnnm: A Python package for Matrix Completion with Nuclear Norm Minimization"
version: 0.1.5
date-released: 2024-08-04
url: "https://github.com/tobias-schnabel/mcnnm"
repository-code: "https://github.com/tobias-schnabel/mcnnm"
license: GPL-3.0
type: software
keywords:
- causal-inference
- panel-data
- matrix-completion
- nuclear-norm-minimization
- python
abstract: >
lightweight-mcnnm is a Python package that provides a lightweight and performant implementation
of the Matrix Completion with Nuclear Norm Minimization (MC-NNM) estimator for causal inference
in panel data settings. It implements the MC-NNM estimator exactly as described in "Matrix Completion
Methods for Causal Panel Data Models" by Susan Athey, Mohsen Bayati, Nikolay Doudchenko, Guido Imbens,
and Khashayar Khosravi (2021).
references:
- type: article
authors:
- family-names: "Athey"
given-names: "Susan"
- family-names: "Bayati"
given-names: "Mohsen"
- family-names: "Doudchenko"
given-names: "Nikolay"
- family-names: "Imbens"
given-names: "Guido"
- family-names: "Khosravi"
given-names: "Khashayar"
title: "Matrix Completion Methods for Causal Panel Data Models"
journal: "Journal of the American Statistical Association"
volume: 116
issue: 536
year: 2021
pages: "1716-1730"
doi: "10.1080/01621459.2021.1891924"
GitHub Events
Total
- Release event: 1
- Delete event: 5
- Push event: 10
- Pull request event: 10
- Create event: 8
Last Year
- Release event: 1
- Delete event: 5
- Push event: 10
- Pull request event: 10
- Create event: 8
Packages
- Total packages: 1
-
Total downloads:
- pypi 47 last-month
- Total dependent packages: 0
- Total dependent repositories: 0
- Total versions: 11
- Total maintainers: 1
pypi.org: lightweight-mcnnm
Leightweight Implementation of Athey et al. (2021)'s MC-NNM estimator
- Documentation: https://mcnnm.readthedocs.io/en/latest/
- License: GPL-3.0
-
Latest release: 1.1.2
published 8 months ago
Rankings
Maintainers (1)
Dependencies
- actions/cache v3 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- codecov/codecov-action v3 composite
- sphinx *
- sphinx_rtd_theme *
- bandit 1.7.9
- black 24.4.2
- cfgv 3.4.0
- click 8.1.7
- colorama 0.4.6
- coverage 7.6.0
- distlib 0.3.8
- exceptiongroup 1.2.2
- filelock 3.15.4
- flake8 7.1.0
- identify 2.6.0
- importlib-metadata 8.2.0
- iniconfig 2.0.0
- jax 0.4.30
- jaxlib 0.4.30
- markdown-it-py 3.0.0
- mccabe 0.7.0
- mdurl 0.1.2
- ml-dtypes 0.4.0
- mypy 1.11.0
- mypy-extensions 1.0.0
- nodeenv 1.9.1
- numpy 1.26.4
- opt-einsum 3.3.0
- packaging 24.1
- pandas 2.2.2
- pathspec 0.12.1
- pbr 6.0.0
- platformdirs 4.2.2
- pluggy 1.5.0
- pre-commit 3.8.0
- pycodestyle 2.12.0
- pyflakes 3.2.0
- pygments 2.18.0
- pytest 8.3.2
- pytest-cov 5.0.0
- python-dateutil 2.9.0.post0
- pytz 2024.1
- pyyaml 6.0.1
- rich 13.7.1
- scipy 1.14.0
- six 1.16.0
- stevedore 5.2.0
- tomli 2.0.1
- typed-ast 1.5.5
- typing-extensions 4.12.2
- tzdata 2024.1
- virtualenv 20.26.3
- zipp 3.19.2
- bandit ^1.7.0 develop
- black ^24.4.2 develop
- click ^8.1.7 develop
- flake8 ^7.1.0 develop
- importlib-metadata ^8.2.0 develop
- mypy ^1.10.1 develop
- pre-commit ^3.8.0 develop
- pytest ^8.2.2 develop
- pytest-cov ^5.0.0 develop
- typed-ast ^1.5.5 develop
- jax ^0.4.25
- numpy ^1.22.4
- pandas ^2.0.3
- python ^3.10