log
2026-07-09 bee20e574656edaaf98f30799faf8daabeba7fc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/usr/bin/env python3
"""
月度绩效归因分析模块
使用Brinson归因模型分析超收益来源
"""
 
import pandas as pd
import numpy as np
from typing import Dict, List, Tuple, Optional
from dataclasses import dataclass
from datetime import datetime, timedelta
import json
import os
 
@dataclass
class AttributionResult:
    """归因结果"""
    period: str  # 归因期间
    portfolio_return: float  # 组合收益率
    benchmark_return: float  # 基准收益率
    active_return: float  # 超收益
    
    # Brinson归因分解
    allocation_effect: float  # 配置效应
    selection_effect: float  # 选股效应
    interaction_effect: float  # 交互效应
    
    # 因子归因
    factor_returns: Dict[str, float]  # 各因子贡献
    
    # 风格归因
    style_returns: Dict[str, float]  # 各风格暴露贡献
 
class MonthlyAttribution:
    """月度归因分析器"""
    
    def __init__(self, benchmark_name: str = '沪深300'):
        """
        初始化归因分析器
        
        Args:
            benchmark_name: 基准名称
        """
        self.benchmark_name = benchmark_name
        self.attribution_file = 'data/attribution_results.json'
        
        self.attribution_history: List[AttributionResult] = []
        self._load_history()
    
    def _load_history(self):
        """加载历史归因结果"""
        if os.path.exists(self.attribution_file):
            try:
                with open(self.attribution_file, 'r', encoding='utf-8') as f:
                    history_data = json.load(f)
                
                for result_data in history_data:
                    result = AttributionResult(**result_data)
                    self.attribution_history.append(result)
                
                print(f"✓ 加载归因历史: {len(self.attribution_history)}条记录")
            except Exception as e:
                print(f"⚠️ 加载归因历史失败: {e}")
    
    def _save_result(self, result: AttributionResult):
        """保存归因结果"""
        self.attribution_history.append(result)
        
        history_data = [
            {
                'period': r.period,
                'portfolio_return': r.portfolio_return,
                'benchmark_return': r.benchmark_return,
                'active_return': r.active_return,
                'allocation_effect': r.allocation_effect,
                'selection_effect': r.selection_effect,
                'interaction_effect': r.interaction_effect,
                'factor_returns': r.factor_returns,
                'style_returns': r.style_returns
            }
            for r in self.attribution_history
        ]
        
        os.makedirs(os.path.dirname(self.attribution_file), exist_ok=True)
        with open(self.attribution_file, 'w', encoding='utf-8') as f:
            json.dump(history_data, f, ensure_ascii=False, indent=2)
    
    def calculate_portfolio_return(self, portfolio_df: pd.DataFrame) -> float:
        """
        计算组合收益率
        
        Args:
            portfolio_df: 组合数据 DataFrame,包含 'market_value', 'profit_loss' 列
            
        Returns:
            组合收益率
        """
        if portfolio_df.empty or 'market_value' not in portfolio_df.columns:
            return 0.0
        
        total_cost = portfolio_df['market_value'].sum() - portfolio_df['profit_loss'].sum()
        if total_cost == 0:
            return 0.0
        
        portfolio_return = portfolio_df['profit_loss'].sum() / total_cost * 100
        
        return portfolio_return
    
    def brinson_attribution(self, portfolio_weights: Dict[str, float],
                          benchmark_weights: Dict[str, float],
                          sector_returns: Dict[str, float]) -> Dict[str, float]:
        """
        Brinson归因分析
        
        Args:
            portfolio_weights: 组合各行业权重 {行业: 权重}
            benchmark_weights: 基准各行业权重 {行业: 权重}
            sector_returns: 各行业收益率 {行业: 收益率}
            
        Returns:
            归因结果
        """
        # 总组合收益率和基准收益率
        portfolio_return = sum(weight * sector_returns.get(sector, 0) 
                             for sector, weight in portfolio_weights.items())
        benchmark_return = sum(weight * sector_returns.get(sector, 0) 
                             for sector, weight in benchmark_weights.items())
        
        # 超收益
        active_return = portfolio_return - benchmark_return
        
        # 计算Brinson归因分解
        allocation_effect = 0.0  # 配置效应
        selection_effect = 0.0  # 选股效应
        interaction_effect = 0.0  # 交互效应
        
        sectors = set(portfolio_weights.keys()) | set(benchmark_weights.keys())
        
        for sector in sectors:
            w_p = portfolio_weights.get(sector, 0)
            w_b = benchmark_weights.get(sector, 0)
            r_s = sector_returns.get(sector, 0)
            r_b = benchmark_return  # 基准收益率
            
            # 配置效应: (权重差异) * (行业与基准收益差异)
            allocation_effect += (w_p - w_b) * (r_s - r_b)
            
            # 选股效应: 基准权重 * (行业收益率与基准差异)
            selection_effect += w_b * (r_s - r_b)
            
            # 交互效应: 权重差异 * 行业收益率
            interaction_effect += (w_p - w_b) * r_s
        
        # 交叉项调整
        cross_term = sum((portfolio_weights.get(s, 0) - benchmark_weights.get(s, 0)) * 
                        (sector_returns.get(s, 0) - benchmark_return)
                        for s in sectors)
        
        # 重新计算
        allocation_effect = sum(
            (portfolio_weights.get(s, 0) - benchmark_weights.get(s, 0)) * 
            (sector_returns.get(s, 0) - benchmark_return)
            for s in sectors
        )
        
        selection_effect = sum(
            benchmark_weights.get(s, 0) * 
            (sector_returns.get(s, 0) - benchmark_return)
            for s in sectors
        )
        
        interaction_effect = active_return - allocation_effect - selection_effect
        
        return {
            'portfolio_return': portfolio_return,
            'benchmark_return': benchmark_return,
            'active_return': active_return,
            'allocation_effect': allocation_effect,
            'selection_effect': selection_effect,
            'interaction_effect': interaction_effect
        }
    
    def factor_attribution(self, portfolio_df: pd.DataFrame,
                          factor_exposures: Dict[str, Dict[str, float]],
                          factor_returns: Dict[str, float]) -> Dict[str, float]:
        """
        因子归因分析
        
        Args:
            portfolio_df: 组合 DataFrame (index: stock_code)
            factor_exposures: 各股票的因子暴露 {stock_code: {factor_name: exposure}}
            factor_returns: 各因子收益率 {factor_name: return}
            
        Returns:
            各因子贡献
        """
        factor_contributions = {}
        
        if portfolio_df.empty:
            return factor_contributions
        
        # 计算组合权重
        total_value = portfolio_df['market_value'].sum()
        if total_value == 0:
            return factor_contributions
        
        for factor_name in factor_returns.keys():
            total_exposure = 0.0
            
            for stock_code, row in portfolio_df.iterrows():
                weight = row['market_value'] / total_value
                exposure = factor_exposures.get(stock_code, {}).get(factor_name, 0)
                total_exposure += weight * exposure
            
            # 因子贡献 = 组合因子暴露 × 因子收益率
            contribution = total_exposure * factor_returns[factor_name]
            factor_contributions[factor_name] = contribution
        
        return factor_contributions
    
    def style_attribution(self, portfolio_df: pd.DataFrame,
                         style_exposures: Dict[str, Dict[str, float]],
                         style_returns: Dict[str, float]) -> Dict[str, float]:
        """
        风格归因分析
        
        Args:
            portfolio_df: 组合 DataFrame
            style_exposures: 各股票的风格暴露 {stock_code: {style_name: exposure}}
            style_returns: 各风格收益率 {style_name: return}
            
        Returns:
            各风格贡献
        """
        similar_to_factor = self.factor_attribution(
            portfolio_df, style_exposures, style_returns
        )
        
        return similar_to_factor
    
    def analyze_month(self, portfolio_df: pd.DataFrame,
                     portfolio_weights: Dict[str, float],
                     benchmark_weights: Dict[str, float],
                     sector_returns: Dict[str, float],
                     factor_exposures: Dict[str, Dict[str, float]] = None,
                     factor_returns: Dict[str, float] = None,
                     period: str = None) -> AttributionResult:
        """
        分析月度归因
        
        Args:
            portfolio_df: 持仓数据 DataFrame
            portfolio_weights: 组合行业权重
            benchmark_weights: 基准行业权重
            sector_returns: 行业收益率
            factor_exposures: 因子暴露
            factor_returns: 因子收益率
            period: 归因期间 (如 "2024-03")
            
        Returns:
            归因结果
        """
        if period is None:
            period = datetime.now().strftime('%Y-%m')
        
        print("=" * 60)
        print(f"📊 月度归因分析 - {period}")
        print("=" * 60)
        
        # 1. 计算组合收益率
        portfolio_return = self.calculate_portfolio_return(portfolio_df)
        print(f"\n组合收益率: {portfolio_return:.2f}%")
        
        # 2. 计算基准收益率
        benchmark_return = sum(
            benchmark_weights.get(sector, 0) * sector_returns.get(sector, 0)
            for sector in benchmark_weights.keys()
        )
        print(f"基准收益率: {benchmark_return:.2f}%")
        
        # 3. 计算超收益
        active_return = portfolio_return - benchmark_return
        print(f"超收益: {active_return:.2f}%")
        
        # 4. Brinson归因
        print(f"\n🎯 Brinson归因分解...")
        brinson_results = self.brinson_attribution(
            portfolio_weights, benchmark_weights, sector_returns
        )
        
        print(f"  • 配置效应: {brinson_results['allocation_effect']:.2f}%")
        print(f"  • 选股效应: {brinson_results['selection_effect']:.2f}%")
        print(f"  • 交互效应: {brinson_results['interaction_effect']:.2f}%")
        
        # 5. 因子归因
        factor_returns_result = {}
        if factor_exposures and factor_returns:
            print(f"\n📈 因子归因...")
            factor_returns_result = self.factor_attribution(
                portfolio_df, factor_exposures, factor_returns
            )
            
            for factor, contribution in sorted(factor_returns_result.items(), 
                                              key=lambda x: abs(x[1]), reverse=True):
                print(f"  • {factor}: {contribution:.2f}%")
        
        # 6. 风格归因
        style_returns_result = {}
        if factor_exposures and factor_returns:
            # 使用行业暴露作为风格
            style_exposures = {}
            style_returns = {}
            
            for stock_code, exposures in factor_exposures.items():
                if stock_code in portfolio_df.index:
                    sector = portfolio_df.loc[stock_code, 'sector'] if 'sector' in portfolio_df.columns else '其他'
                    style_exposures[stock_code] = {sector: 1.0}
                    style_returns[sector] = sector_returns.get(sector, 0)
            
            print(f"\n🎨 风格归因...")
            style_returns_result = self.style_attribution(
                portfolio_df, style_exposures, style_returns
            )
            
            for style, contribution in sorted(style_returns_result.items(),
                                            key=lambda x: abs(x[1]), reverse=True):
                print(f"  • {style}: {contribution:.2f}%")
        
        # 7. 创建归因结果
        result = AttributionResult(
            period=period,
            portfolio_return=portfolio_return,
            benchmark_return=benchmark_return,
            active_return=active_return,
            allocation_effect=brinson_results['allocation_effect'],
            selection_effect=brinson_results['selection_effect'],
            interaction_effect=brinson_results['interaction_effect'],
            factor_returns=factor_returns_result,
            style_returns=style_returns_result
        )
        
        # 8. 保存结果
        self._save_result(result)
        
        print("\n" + "=" * 60)
        print(f"归因分析完成并保存")
        print("=" * 60)
        
        return result
    
    def generate_attribution_report(self, result: AttributionResult) -> str:
        """
        生成归因报告
        
        Args:
            result: 归因结果
            
        Returns:
            报告文本
        """
        report = []
        report.append("=" * 60)
        report.append("📊 归因分析报告")
        report.append("=" * 60)
        report.append(f"期间: {result.period}")
        report.append("")
        
        report.append("💰 收益率汇总")
        report.append("-" * 40)
        report.append(f"组合收益率: {result.portfolio_return:.2f}%")
        report.append(f"基准收益率: {result.benchmark_return:.2f}%")
        report.append(f"超收益: {result.active_return:.2f}%")
        report.append("")
        
        report.append("🎯 Brinson归因分解")
        report.append("-" * 40)
        report.append(f"配置效应: {result.allocation_effect:.2f}%")
        report.append(f"  - 含义: 通过行业配置偏离基准获得的收益")
        report.append(f"选股效应: {result.selection_effect:.2f}%")
        report.append(f"  - 含义: 在各行业内选股获得的收益")
        report.append(f"交互效应: {result.interaction_effect:.2f}%")
        report.append(f"  - 含义: 配置和选股的交互影响")
        report.append("")
        
        if result.factor_returns:
            report.append("📈 因子归因")
            report.append("-" * 40)
            
            # 按贡献度排序
            sorted_factors = sorted(result.factor_returns.items(), 
                                   key=lambda x: abs(x[1]), reverse=True)
            
            for factor, contribution in sorted_factors:
                emoji = "📈" if contribution > 0 else "📉"
                report.append(f"{emoji} {factor}: {contribution:.2f}%")
            report.append("")
        
        if result.style_returns:
            report.append("🎨 风格归因")
            report.append("-" * 40)
            
            sorted_styles = sorted(result.style_returns.items(),
                                 key=lambda x: abs(x[1]), reverse=True)
            
            for style, contribution in sorted_styles:
                emoji = "📈" if contribution > 0 else "📉"
                report.append(f"{emoji} {style}: {contribution:.2f}%")
            report.append("")
        
        # 总结
        report.append("💡 归因总结")
        report.append("-" * 40)
        
        total_effect = result.allocation_effect + result.selection_effect + result.interaction_effect
        if abs(total_effect - result.active_return) > 0.01:
            report.append(f"⚠️ 归因误差: {total_effect - result.active_return:.2f}%")
        
        if abs(result.allocation_effect) > abs(result.selection_effect):
            if result.allocation_effect > 0:
                report.append("✓ 主要收益来源: 行业配置能力")
            else:
                report.append("✗ 主要亏损来源: 行业配置偏离")
        elif abs(result.selection_effect) > 0:
            if result.selection_effect > 0:
                report.append("✓ 主要收益来源: 个股选择能力")
            else:
                report.append("✗ 主要亏损来源: 个股选择偏差")
        else:
            report.append("→ 收益来源均衡,无明显偏差")
        
        report.append("=" * 60)
        
        return "\n".join(report)
    
    def generate_monthly_summary(self, months: int = 6) -> str:
        """
        生成最近N个月的归因汇总
        
        Args:
            months: 月份数量
            
        Returns:
            汇总报告文本
        """
        if not self.attribution_history:
            return "暂无归因数据"
        
        recent_results = self.attribution_history[-months:]
        
        report = []
        report.append("=" * 60)
        report.append(f"📊 最近{months}个月归因汇总")
        report.append("=" * 60)
        report.append("")
        
        # 收益率汇总
        total_portfolio_return = sum(r.portfolio_return for r in recent_results)
        total_benchmark_return = sum(r.benchmark_return for r in recent_results)
        total_active_return = sum(r.active_return for r in recent_results)
        
        avg_portfolio_return = total_portfolio_return / len(recent_results)
        avg_benchmark_return = total_benchmark_return / len(recent_results)
        avg_active_return = total_active_return / len(recent_results)
        
        report.append("💰 收益率汇总")
        report.append("-" * 40)
        report.append(f"累计组合收益: {total_portfolio_return:.2f}%")
        report.append(f"累计基准收益: {total_benchmark_return:.2f}%")
        report.append(f"累计超收益: {total_active_return:.2f}%")
        report.append(f"月均组合收益: {avg_portfolio_return:.2f}%")
        report.append(f"月均基准收益: {avg_benchmark_return:.2f}%")
        report.append(f"月均超收益: {avg_active_return:.2f}%")
        report.append("")
        
        # Brinson归因汇总
        total_allocation = sum(r.allocation_effect for r in recent_results)
        total_selection = sum(r.selection_effect for r in recent_results)
        total_interaction = sum(r.interaction_effect for r in recent_results)
        
        report.append("🎯 Brinson归因汇总")
        report.append("-" * 40)
        report.append(f"累计配置效应: {total_allocation:.2f}%")
        report.append(f"累计选股效应: {total_selection:.2f}%")
        report.append(f"累计交互效应: {total_interaction:.2f}%")
        report.append("")
        
        # 月度明细
        report.append("📅 月度明细")
        report.append("-" * 40)
        
        for result in recent_results:
            emoji = "📈" if result.active_return > 0 else "📉"
            report.append(f"{emoji} {result.period}")
            report.append(f"   组合: {result.portfolio_return:.2f}% | 基准: {result.benchmark_return:.2f}% | 超收益: {result.active_return:.2f}%")
            report.append(f"   配置: {result.allocation_effect:.2f}% | 选股: {result.selection_effect:.2f}% | 交互: {result.interaction_effect:.2f}%")
        
        report.append("=" * 60)
        
        return "\n".join(report)
 
 
# 测试代码
if __name__ == "__main__":
    # 创建测试数据
    portfolio_df = pd.DataFrame({
        'stock_code': ['000001', '000002', '600519'],
        'stock_name': ['平安银行', '万科A', '贵州茅台'],
        'market_value': [10000, 5000, 18000],
        'profit_loss': [1000, -500, 2000],
        'sector': ['金融', '房地产', '消费品']
    })
    
    portfolio_weights = {'金融': 0.3, '房地产': 0.15, '消费品': 0.55}
    benchmark_weights = {'金融': 0.25, '房地产': 0.20, '消费品': 0.50, '科技': 0.05}
    sector_returns = {'金融': 0.08, '房地产': -0.05, '消费品': 0.12, '科技': 0.15}
    
    factor_exposures = {
        '000001': {'PE_TTM': 5.0, 'PB': 0.8, 'ROE': 0.12},
        '000002': {'PE_TTM': 20.0, 'PB': 1.5, 'ROE': 0.08},
        '600519': {'PE_TTM': 25.0, 'PB': 10.0, 'ROE': 0.25}
    }
    
    factor_returns = {'PE_TTM': -0.05, 'PB': -0.03, 'ROE': 0.08}
    
    # 测试归因分析
    analyzer = MonthlyAttribution()
    result = analyzer.analyze_month(
        portfolio_df,
        portfolio_weights,
        benchmark_weights,
        sector_returns,
        factor_exposures,
        factor_returns,
        period='2024-03'
    )
    
    print("\n" + analyzer.generate_attribution_report(result))
    print("\n" + analyzer.generate_monthly_summary(1))