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…
от ECNU-ICALK★ 623 звезды на GitHubОб этом скилле
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
- Database Connection: Use
pyodbcto connect to the Access database. The connection string should use the Microsoft Access Driver. - 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'.
- 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.
- Q-Series Calculation: Calculate the following series for each industry:
Q1= Benchmark Weight * Benchmark ReturnQ2= Portfolio Weight * Benchmark ReturnQ3= Benchmark Weight * Portfolio ReturnQ4= Portfolio Weight * Portfolio Return- Sum these values across all industries to get the total Q values for the period.
- 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
- 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.
- Output: Return the calculated total daily excess return and each effect (allocation, selection, interaction).
- Visualization: Use
matplotlibto 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
Установить Brinson Attribution Analysis with Python в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл 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.
Как установить скилл Brinson Attribution Analysis with Python?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл Brinson Attribution Analysis with Python запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
от Anthropicdiscernment-nudge
After you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
от Anthropicdata-consolidate
Joins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
от Anthropicinspection-to-capa
Runs the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…
от Anthropic