Menchero multiperiod performance attribution calculations at a sector and stock level
Project description
Menchero Method Performance Attribution
Menchero Method Performance Attribution is a Python package for performing single- and multi-period performance attribution using the Menchero multiperiod smoothing method, with support for both sector-level and stock-level breakdowns. Stock level attributions are calculated using our own internally developed adjustments descibed here
This library provides:
- Menchero smoothing for accurate multi-period attribution of returns.
- Quality assurance checks to compare calculated attributions vs. actual active returns.
- PnL (Profit & Loss) handling at both sector and stock levels.
- Flexible, pandas DataFrame-based interface for integration with Python analytics workflows.
Table of Contents
- Features
- Installation
- Quickstart Example
- Usage
- How It Works
- Validation and Testing
- Contributing
- License
Features
- Menchero multiperiod smoothing approach to handle multi-day or multi-month performance attribution.
- Sector-level vs. stock-level breakdowns:
- Sector-level: See which sectors drove active performance over time.
- Stock-level: Drill down to individual stock attributions, with optional re-allocation logic.
- PnL inclusion: Decide whether to include profit-and-loss percentages in your allocation effect.
- Flexible QA checks: Compare the summed selection/allocation to the final cumulative active return.
- Verbose logging mode for debugging and demonstration.
Installation
You can install this package via pip:
pip install menchero-method
Quickstart Example
import pandas as pd
from menchero_multiperiod_attribution import sectorAttributions, stockAttributions
# Example: create or load your data as a pandas DataFrame
data = pd.DataFrame({
'date': [...], # Python 'date' objects or Timestamps
'sector': [...], # Sector names (str)
'stock': [...], # Stock names (str), optional for sector-level
'portfolio_weight': [...],
'benchmark_weight': [...],
'stock_return': [...],
'pnl_pct': [...]
})
# Sector-level attributions
sector_output = sectorAttributions(
raw=data,
pnl=True, # include PnL in calculations
check=False, # skip QA checks for brevity
verbose=False # no debug printing
)
# Stock-level attributions
stock_output = stockAttributions(
raw=data,
pnl=True,
check=True, # perform QA checks
verbose=True # print debug info
)
print(sector_output.head())
print(stock_output[0].head()) # if check=True, returns (stock_output, qa_checks)
Usage
Sector-Level Attributions
from menchero_multiperiod_attribution import sectorAttributions
# Example DataFrame 'df' with columns:
# date, sector, portfolio_weight, benchmark_weight, stock_return, pnl_pct
sector_result = sectorAttributions(
raw=df,
pnl=True, # include or exclude PnL in your calculation
check=False, # set True to return QA checks
verbose=True # optional logging
)
- If
check=True, returns a tuple(sector_result, qa_checks). - The returned DataFrame (or first element of the tuple) is in a “long” format, with columns like
[date, sector, variable, variable_value].
Stock-Level Attributions
from menchero_multiperiod_attribution import stockAttributions
# Example DataFrame 'df' with columns:
# date, sector, stock, portfolio_weight, benchmark_weight, stock_return, pnl_pct
stock_result = stockAttributions(
raw=df,
pnl=False, # do not include PnL
check=True,
verbose=False
)
- If
check=True, returns(stock_output, qa_checks). Otherwise, returns onlystock_output. - The DataFrame(s) have columns like
[date, sector, stock, variable, variable_value]. - Internally,
stockAttributionscalculates sector-level attributions first, then modifies them at the stock level via_attributionModify.
How It Works
- Input Validation – The library checks that your input DataFrame has the required columns, correct data types, and that weights sum to approximately 1.
- Menchero Method – The core _menchero function calculates:
- Allocation, Selection, Interaction effects.
- Cumulative returns and active return.
- Menchero smoothing via multiplicative and corrective factors to properly handle compounding.
- QA Checks (optional) – Sums allocation/selection across dates and compares to the final cumulative active return to ensure correctness.
- Stock-Level Adjustments – For stock-level attributions, we apply _attributionModify to adjust the final allocation/selection by sector-level results.
Validation and Testing
We maintain a comprehensive test suite that includes:
- Unit tests for internal functions (
_menchero,_attributionCheck,_attributionModify,_validateInputData). - Integration tests for
sectorAttributionsand ```stockAttributions. - Golden tests with small, hand-verified datasets to confirm exact numeric results match expected Menchero smoothing outcomes. To run the tests locally, clone this repository and run:
pytest
(You may need to install development dependencies like pytest.)
Contributing
Contributions, bug reports, and feature requests are welcome! Please:
- Open an issue describing the problem or suggestion.
- Fork the repo and create a new branch for your contribution.
- Submit a pull request with your changes and relevant tests. We’ll review your pull request as soon as we can.
License
This project is released under the MIT License. Feel free to use it in commercial and private projects. See the LICENSE file for details.
Thank you for using the Menchero Method Performance Attribution Package! If you have any questions or feedback, please open an issue or submit a pull request. Contributions are always welcome.
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
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 manchero_method-0.1.3.tar.gz.
File metadata
- Download URL: manchero_method-0.1.3.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a13828ea117dca418eddab2f73aed4244f35a81e5c1fcb4eafa9de91cc351b28
|
|
| MD5 |
da1dd5a2b9a6bcf0babc003e685354aa
|
|
| BLAKE2b-256 |
d8ea450abb3b08e9b046d02e81d4be63a44f32093694845a9371406f15f33c8d
|
File details
Details for the file manchero_method-0.1.3-py3-none-any.whl.
File metadata
- Download URL: manchero_method-0.1.3-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f5eec55ad20888f925479252368d4fa2bc8527b7728129f85ce037e79dccb76
|
|
| MD5 |
41f62d6d01255e3f654f14ab8d58588c
|
|
| BLAKE2b-256 |
ec0015324aa0c0282feae455cbb9468f38e7d4de91a9cfaa8cdb8682890f6e87
|