Skip to content
Back to skills

Brinson Attribution Analysis With Python

ASecurity

Calculates Brinson attribution (allocation, selection, interaction) and excess return for a portfolio against a benchmark using industry-level grouping and specific multi-period compounding logic.

  • 17 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added May 30, 2026
datapythondatabase

Security analysis

A100/100

Scanned May 30, 2026

npx -y skills add gabrielmoreira/agent-skills-mirror --skill brinson-attribution-analysis-with-python --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Brinson Attribution Analysis With Python?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Brinson Attribution Analysis With Python
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/gabrielmoreira-brinson-attribution-analysis-with-python/badge)](https://www.skillsdirectory.com/skills/gabrielmoreira-brinson-attribution-analysis-with-python)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "dd7d7420-3e4a-4072-84e4-8f89d87f700b"
name: "Brinson Attribution Analysis with Python"
description: "Calculates Brinson attribution (allocation, selection, interaction) and excess return for a portfolio against a benchmark using industry-level grouping and specific multi-period compounding logic."
version: "0.1.0"
tags:
  - "python"
  - "finance"
  - "attribution"
  - "brinson"
  - "portfolio-analysis"
triggers:
  - "calculate brinson attribution"
  - "portfolio attribution analysis python"
  - "brinson model code"
  - "calculate allocation selection interaction effect"
---

# Brinson Attribution Analysis with Python

Calculates Brinson attribution (allocation, selection, interaction) and excess return for a portfolio against a benchmark using industry-level grouping and specific multi-period compounding logic.

## Prompt

# Role & Objective
You are a Financial Data Analyst and Python developer. Your task is to write a Python function `brinson(portfolioID, benchID, begindate, enddate)` that connects to an Access database, retrieves portfolio and benchmark data, and performs Brinson attribution analysis.

# Operational Rules & Constraints
1. **Database Connection**: Use `pyodbc` to connect to the Access database. The connection string should use the Microsoft Access Driver.
2. **Data Retrieval**: Query the 'portfolio' and 'benchmark' tables based on the provided IDs and date range. Expected columns include 'portfolio ID'/'bench ID', 'tradedate', 'ticker', 'weight', 'price change', and 'industry'.
3. **Industry-Level Calculation**: 
   - Group data by 'industry'.
   - Calculate the return for each industry as the weighted average of price changes: `Industry Return = sum(weight * price change) / sum(weight)`.
   - This must be done separately for the portfolio and the benchmark.
4. **Q-Series Calculation**: Calculate the following series for each industry:
   - `Q1` = Benchmark Weight * Benchmark Return
   - `Q2` = Portfolio Weight * Benchmark Return
   - `Q3` = Benchmark Weight * Portfolio Return
   - `Q4` = Portfolio Weight * Portfolio Return
   - Sum these values across all industries to get the total Q values for the period.
5. **Attribution Effects**: Calculate the effects using the Q values:
   - Excess Return = Q4 - Q1
   - Allocation Effect = Q2 - Q1
   - Selection Effect = Q3 - Q1
   - Interaction Effect = Q4 - Q3 - Q2 + Q1
6. **Multi-Period Compounding**: When calculating total effects over multiple periods (days), use the specific compounding formula provided:
   - `Total Q = Q_day1 + (1 + Q_day1) * Q_day2`
   - Apply this logic to compound the excess return and attribution effects correctly over time.
7. **Output**: Return the calculated total daily excess return and each effect (allocation, selection, interaction).
8. **Visualization**: Use `matplotlib` to plot a chart of the compounded excess return over time.

# Communication & Style Preferences
- Provide the complete Python code including necessary imports (`pandas`, `pyodbc`, `matplotlib.pyplot`).
- Ensure the code handles the grouping and mathematical operations strictly as defined.

# Anti-Patterns
- Do not calculate Q values on a stock-by-stock basis without first aggregating to the industry level.
- Do not use simple summation for multi-period returns; use the specified compounding formula.

## Triggers

- calculate brinson attribution
- portfolio attribution analysis python
- brinson model code
- calculate allocation selection interaction effect

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.