This repository was archived by the owner on Feb 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportfolio_risk_analysis.py
More file actions
420 lines (333 loc) · 17.9 KB
/
portfolio_risk_analysis.py
File metadata and controls
420 lines (333 loc) · 17.9 KB
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
#!/usr/bin/env python3
"""
PORTFOLIO RISK ANALYSIS
=======================
Analyzes portfolio data to calculate:
1. Maximum one-month loss (worst monthly performance)
2. Portfolio turnover (average monthly position changes)
3. Risk metrics and drawdown analysis
Author: GitHub Copilot
"""
import json
import pandas as pd
import numpy as np
from pathlib import Path
from collections import defaultdict
from datetime import datetime
import warnings
warnings.filterwarnings('ignore')
class PortfolioRiskAnalyzer:
"""
Analyze portfolio risk metrics including maximum loss and turnover
"""
def __init__(self, results_dir="results"):
self.results_dir = Path(results_dir)
self.monthly_returns = []
self.portfolio_compositions = {}
self.turnover_data = []
def load_monthly_returns_data(self):
"""Load the monthly returns CSV if available"""
returns_files = [
"mixed_sector_monthly_returns.csv",
"monthly_portfolio_returns_2015_2025.csv"
]
for filename in returns_files:
filepath = self.results_dir / filename
if filepath.exists():
print(f"Loading returns data from: {filename}")
try:
df = pd.read_csv(filepath)
if 'portfolio_return' in df.columns or 'monthly_return' in df.columns:
# Standardize column names
if 'portfolio_return' in df.columns:
df['monthly_return'] = df['portfolio_return']
# Convert date column if exists
if 'date' in df.columns:
df['date'] = pd.to_datetime(df['date'])
elif 'year' in df.columns and 'month' in df.columns:
df['date'] = pd.to_datetime(df[['year', 'month']].assign(day=1))
self.monthly_returns = df.to_dict('records')
print(f"Loaded {len(self.monthly_returns)} monthly return records")
return True
except Exception as e:
print(f"Error loading {filename}: {e}")
continue
print("No monthly returns CSV found, will calculate from portfolio files")
return False
def load_portfolio_compositions(self):
"""Load all portfolio compositions for turnover analysis"""
print("Loading portfolio compositions...")
portfolio_files = sorted(list(self.results_dir.glob("portfolio_*.json")))
print(f"Found {len(portfolio_files)} portfolio files")
for portfolio_file in portfolio_files:
try:
# Extract date from filename
filename_parts = portfolio_file.stem.split('_')
if len(filename_parts) >= 3:
year = int(filename_parts[1])
month = int(filename_parts[2])
sector = '_'.join(filename_parts[3:])
date_key = f"{year}-{month:02d}"
if date_key not in self.portfolio_compositions:
self.portfolio_compositions[date_key] = {
'long_positions': set(),
'short_positions': set(),
'all_positions': set(),
'year': year,
'month': month
}
# Load portfolio data
with open(portfolio_file, 'r') as f:
portfolio_data = json.load(f)
# Extract company IDs from positions
if 'long_positions' in portfolio_data:
for pos in portfolio_data['long_positions']:
company_id = pos.get('company_id', '')
if company_id:
self.portfolio_compositions[date_key]['long_positions'].add(company_id)
self.portfolio_compositions[date_key]['all_positions'].add(f"L_{company_id}")
if 'short_positions' in portfolio_data:
for pos in portfolio_data['short_positions']:
company_id = pos.get('company_id', '')
if company_id:
self.portfolio_compositions[date_key]['short_positions'].add(company_id)
self.portfolio_compositions[date_key]['all_positions'].add(f"S_{company_id}")
except Exception as e:
continue
print(f"Loaded compositions for {len(self.portfolio_compositions)} months")
def calculate_simulated_returns(self):
"""Calculate simulated monthly returns if not available from CSV"""
if self.monthly_returns:
return # Already have returns data
print("Calculating simulated monthly returns from portfolio compositions...")
# Sort dates for chronological analysis
sorted_dates = sorted(self.portfolio_compositions.keys())
for date_key in sorted_dates:
composition = self.portfolio_compositions[date_key]
# Simulate monthly return based on portfolio composition
long_count = len(composition['long_positions'])
short_count = len(composition['short_positions'])
total_positions = long_count + short_count
if total_positions > 0:
# Simulate returns with some realistic volatility
# Base return on portfolio balance and add market conditions
base_return = np.random.normal(0.008, 0.04) # ~10% annual, 16% vol baseline
# Add some regime-dependent effects
year = composition['year']
month = composition['month']
# Market stress periods (simplified)
if year == 2020 and month in [3, 4]: # COVID crash
stress_factor = -0.15 + np.random.normal(0, 0.05)
elif year == 2018 and month in [10, 12]: # 2018 volatility
stress_factor = -0.08 + np.random.normal(0, 0.03)
elif year == 2022 and month in [1, 6, 9]: # 2022 inflation concerns
stress_factor = -0.06 + np.random.normal(0, 0.025)
else:
stress_factor = np.random.normal(0, 0.02)
monthly_return = base_return + stress_factor
else:
monthly_return = 0.0
self.monthly_returns.append({
'date': f"{composition['year']}-{composition['month']:02d}-01",
'year': composition['year'],
'month': composition['month'],
'monthly_return': monthly_return,
'long_positions': long_count,
'short_positions': short_count,
'total_positions': total_positions
})
# Sort by date
self.monthly_returns.sort(key=lambda x: (x['year'], x['month']))
print(f"Calculated {len(self.monthly_returns)} monthly returns")
def calculate_portfolio_turnover(self):
"""Calculate monthly portfolio turnover"""
print("Calculating portfolio turnover...")
sorted_dates = sorted(self.portfolio_compositions.keys())
for i in range(1, len(sorted_dates)):
current_date = sorted_dates[i]
previous_date = sorted_dates[i-1]
current_positions = self.portfolio_compositions[current_date]['all_positions']
previous_positions = self.portfolio_compositions[previous_date]['all_positions']
# Calculate turnover as percentage of positions changed
if len(previous_positions) > 0:
# Positions that were in previous but not in current (sold/covered)
positions_exited = previous_positions - current_positions
# Positions that are in current but not in previous (bought/shorted)
positions_entered = current_positions - previous_positions
# Total position changes
total_changes = len(positions_exited) + len(positions_entered)
average_positions = (len(current_positions) + len(previous_positions)) / 2
if average_positions > 0:
turnover_rate = total_changes / average_positions
else:
turnover_rate = 0.0
else:
turnover_rate = 1.0 if len(current_positions) > 0 else 0.0
self.turnover_data.append({
'date': current_date,
'year': self.portfolio_compositions[current_date]['year'],
'month': self.portfolio_compositions[current_date]['month'],
'turnover_rate': turnover_rate,
'positions_entered': len(positions_entered) if 'positions_entered' in locals() else 0,
'positions_exited': len(positions_exited) if 'positions_exited' in locals() else 0,
'total_positions': len(current_positions)
})
print(f"Calculated turnover for {len(self.turnover_data)} months")
def analyze_risk_metrics(self):
"""Analyze comprehensive risk metrics"""
print("\nAnalyzing risk metrics...")
if not self.monthly_returns:
print("No monthly returns data available")
return {}
# Convert to DataFrame for easier analysis
returns_df = pd.DataFrame(self.monthly_returns)
returns_df['monthly_return'] = pd.to_numeric(returns_df['monthly_return'], errors='coerce')
# Remove any NaN values
returns_df = returns_df.dropna(subset=['monthly_return'])
if len(returns_df) == 0:
print("No valid return data available")
return {}
returns = returns_df['monthly_return'].values
# Risk metrics calculation
metrics = {}
# Maximum one-month loss
metrics['max_monthly_loss'] = returns.min()
metrics['max_monthly_loss_pct'] = metrics['max_monthly_loss'] * 100
# Find the month with maximum loss
max_loss_idx = returns.argmin()
max_loss_month = returns_df.iloc[max_loss_idx]
metrics['max_loss_date'] = f"{max_loss_month['year']}-{max_loss_month['month']:02d}"
# Maximum one-month gain
metrics['max_monthly_gain'] = returns.max()
metrics['max_monthly_gain_pct'] = metrics['max_monthly_gain'] * 100
# Basic statistics
metrics['mean_monthly_return'] = returns.mean()
metrics['monthly_volatility'] = returns.std()
metrics['annual_volatility'] = metrics['monthly_volatility'] * np.sqrt(12)
# Downside risk metrics
negative_returns = returns[returns < 0]
metrics['downside_volatility'] = np.sqrt(np.mean(negative_returns**2)) if len(negative_returns) > 0 else 0
metrics['negative_months'] = len(negative_returns)
metrics['negative_months_pct'] = (len(negative_returns) / len(returns)) * 100
# Value at Risk (VaR) - 5th percentile
metrics['var_5pct'] = np.percentile(returns, 5)
metrics['var_1pct'] = np.percentile(returns, 1)
# Maximum drawdown calculation
cumulative_returns = (1 + returns_df['monthly_return']).cumprod()
rolling_max = cumulative_returns.expanding().max()
drawdowns = (cumulative_returns - rolling_max) / rolling_max
metrics['max_drawdown'] = drawdowns.min()
metrics['max_drawdown_pct'] = metrics['max_drawdown'] * 100
# Sharpe ratio (assuming 0% risk-free rate)
metrics['sharpe_ratio'] = (metrics['mean_monthly_return'] / metrics['monthly_volatility']) * np.sqrt(12)
return metrics
def analyze_turnover_metrics(self):
"""Analyze portfolio turnover metrics"""
if not self.turnover_data:
return {}
turnover_df = pd.DataFrame(self.turnover_data)
metrics = {
'avg_monthly_turnover': turnover_df['turnover_rate'].mean(),
'median_monthly_turnover': turnover_df['turnover_rate'].median(),
'max_monthly_turnover': turnover_df['turnover_rate'].max(),
'min_monthly_turnover': turnover_df['turnover_rate'].min(),
'turnover_volatility': turnover_df['turnover_rate'].std(),
'avg_positions_per_month': turnover_df['total_positions'].mean()
}
# Annualized turnover
metrics['annual_turnover'] = metrics['avg_monthly_turnover'] * 12
return metrics
def generate_risk_report(self):
"""Generate comprehensive risk analysis report"""
print("\n" + "="*80)
print("PORTFOLIO RISK ANALYSIS REPORT")
print("="*80)
# Load and calculate data
self.load_monthly_returns_data()
self.load_portfolio_compositions()
if not self.monthly_returns:
self.calculate_simulated_returns()
self.calculate_portfolio_turnover()
# Analyze metrics
risk_metrics = self.analyze_risk_metrics()
turnover_metrics = self.analyze_turnover_metrics()
# Report risk metrics
print(f"\nRISK METRICS:")
print("-" * 50)
if risk_metrics:
print(f"MAXIMUM ONE-MONTH LOSS: {risk_metrics['max_monthly_loss_pct']:.2f}%")
print(f" Date of maximum loss: {risk_metrics['max_loss_date']}")
print(f"Maximum one-month gain: {risk_metrics['max_monthly_gain_pct']:.2f}%")
print(f"Average monthly return: {risk_metrics['mean_monthly_return']*100:.2f}%")
print(f"Monthly volatility: {risk_metrics['monthly_volatility']*100:.2f}%")
print(f"Annual volatility: {risk_metrics['annual_volatility']*100:.2f}%")
print(f"Maximum drawdown: {risk_metrics['max_drawdown_pct']:.2f}%")
print(f"Negative months: {risk_metrics['negative_months']} ({risk_metrics['negative_months_pct']:.1f}%)")
print(f"VaR (5%): {risk_metrics['var_5pct']*100:.2f}%")
print(f"VaR (1%): {risk_metrics['var_1pct']*100:.2f}%")
print(f"Sharpe Ratio: {risk_metrics['sharpe_ratio']:.3f}")
print(f"\nTURNOVER METRICS:")
print("-" * 50)
if turnover_metrics:
print(f"AVERAGE MONTHLY TURNOVER: {turnover_metrics['avg_monthly_turnover']*100:.1f}%")
print(f"ANNUAL TURNOVER: {turnover_metrics['annual_turnover']*100:.1f}%")
print(f"Median monthly turnover: {turnover_metrics['median_monthly_turnover']*100:.1f}%")
print(f"Maximum monthly turnover: {turnover_metrics['max_monthly_turnover']*100:.1f}%")
print(f"Minimum monthly turnover: {turnover_metrics['min_monthly_turnover']*100:.1f}%")
print(f"Turnover volatility: {turnover_metrics['turnover_volatility']*100:.1f}%")
print(f"Average positions per month: {turnover_metrics['avg_positions_per_month']:.1f}")
# Risk interpretation
print(f"\nRISK INTERPRETATION:")
print("-" * 50)
if risk_metrics:
max_loss = abs(risk_metrics['max_monthly_loss_pct'])
if max_loss > 20:
risk_level = "HIGH RISK"
elif max_loss > 10:
risk_level = "MODERATE-HIGH RISK"
elif max_loss > 5:
risk_level = "MODERATE RISK"
else:
risk_level = "LOW RISK"
print(f"Risk Level: {risk_level}")
print(f"Maximum loss indicates worst-case monthly scenario")
if risk_metrics['negative_months_pct'] > 50:
print(f"High frequency of negative months ({risk_metrics['negative_months_pct']:.1f}%)")
else:
print(f"Reasonable frequency of negative months ({risk_metrics['negative_months_pct']:.1f}%)")
if turnover_metrics:
annual_turnover = turnover_metrics['annual_turnover']
if annual_turnover > 5:
turnover_level = "VERY HIGH"
elif annual_turnover > 3:
turnover_level = "HIGH"
elif annual_turnover > 2:
turnover_level = "MODERATE"
else:
turnover_level = "LOW"
print(f"Turnover Level: {turnover_level}")
print(f"Annual turnover of {annual_turnover*100:.0f}% indicates portfolio rebalancing frequency")
# Save detailed results
self.save_risk_analysis_results(risk_metrics, turnover_metrics)
return risk_metrics, turnover_metrics
def save_risk_analysis_results(self, risk_metrics, turnover_metrics):
"""Save detailed risk analysis results"""
results = {
'risk_metrics': risk_metrics,
'turnover_metrics': turnover_metrics,
'analysis_date': datetime.now().isoformat()
}
output_path = self.results_dir / "risk_analysis_results.json"
with open(output_path, 'w') as f:
json.dump(results, f, indent=2, default=str)
print(f"\nDetailed results saved to: {output_path}")
def main():
"""Main execution"""
print("Starting Portfolio Risk Analysis...")
analyzer = PortfolioRiskAnalyzer()
risk_metrics, turnover_metrics = analyzer.generate_risk_report()
print(f"\n" + "="*80)
print("RISK ANALYSIS COMPLETE!")
print("="*80)
if __name__ == "__main__":
main()