--- id: "fc0e896c-14e2-46e5-9be5-5676d32c1f62" name: "Brinson Attribution Analysis" description: "Calculates Brinson attribution effects (Allocation, Selection, Interaction) and excess return using industry-level grouping and specific multi-period compounding logic." version: "0.1.0" tags: - "brinson attribution" - "portfolio analysis" - "finance" - "python" - "attribution effects" triggers: - "brinson attribution calculation" - "calculate portfolio allocation selection interaction" - "brinson model python" - "industry level attribution analysis" --- # Brinson Attribution Analysis Calculates Brinson attribution effects (Allocation, Selection, Interaction) and excess return 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 implement a Brinson attribution analysis function to evaluate portfolio performance against a benchmark. # Operational Rules & Constraints 1. **Data Grouping**: Perform all calculations grouping by 'industry'. 2. **Industry-Level Metrics**: - **Industry Weight**: Sum of weights for all stocks within that industry. - **Industry Return**: Calculate as the weighted average return: (Sum(weight * price change)) / (Sum of weights). 3. **Q-Series Calculation** (calculated per industry): - Q1 = Benchmark Weight * Benchmark Return - Q2 = Portfolio Weight * Benchmark Return - Q3 = Benchmark Weight * Portfolio Return - Q4 = Portfolio Weight * Portfolio Return 4. **Attribution Effects**: - Excess Return = Q4 - Q1 - Allocation Effect = Q2 - Q1 - Selection Effect = Q3 - Q1 - Interaction Effect = Q4 - Q3 - Q2 + Q1 5. **Multi-Period Compounding**: When calculating total effects over multiple periods (e.g., day1 to day2), use geometric linking for the Q values. Formula: Total Q = Q_day1 + (1 + Q_day1) * Q_day2. 6. **Output Requirements**: Return the total daily excess return and each effect (Allocation, Selection, Interaction). Generate and display a chart of the results. # Anti-Patterns - Do not calculate Q values on a stock-by-stock basis without aggregating to the industry level first. - Do not use simple arithmetic summation for multi-period returns; use the specified geometric compounding method. ## Triggers - brinson attribution calculation - calculate portfolio allocation selection interaction - brinson model python - industry level attribution analysis