-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
567 lines (455 loc) · 19.3 KB
/
utils.py
File metadata and controls
567 lines (455 loc) · 19.3 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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
"""
Utility functions for MultiAgent platform
"""
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.cluster import KMeans
import json
import re
# Financial Analysis Helper Functions
def detect_spending_anomalies(expense_df):
"""Detect anomalies in spending patterns using statistical methods"""
anomalies = []
if expense_df.empty:
return anomalies
# Group by category and detect outliers
for category in expense_df['category'].unique():
category_data = expense_df[expense_df['category'] == category]
if len(category_data) < 3:
continue
amounts = category_data['amount'].values
mean_amount = np.mean(amounts)
std_amount = np.std(amounts)
# Detect outliers (3 standard deviations)
threshold = mean_amount + 3 * std_amount
outlier_transactions = category_data[category_data['amount'] > threshold]
for _, transaction in outlier_transactions.iterrows():
anomalies.append({
'date': transaction['date'].isoformat(),
'category': category,
'amount': float(transaction['amount']),
'expected_range': f"${mean_amount:.2f} ± ${std_amount:.2f}",
'severity': 'high' if transaction['amount'] > mean_amount + 4 * std_amount else 'medium'
})
return anomalies
def generate_spending_insights(expense_df):
"""Generate insights from spending patterns"""
insights = []
if expense_df.empty:
return insights
# Category insights
category_spending = expense_df.groupby('category')['amount'].sum()
total_spending = category_spending.sum()
# Top spending category
top_category = category_spending.idxmax()
top_percentage = (category_spending[top_category] / total_spending) * 100
if top_percentage > 40:
insights.append({
'type': 'concentration_risk',
'message': f"{top_percentage:.1f}% of spending is in {top_category}",
'recommendation': 'Consider diversifying spending categories'
})
# Spending frequency analysis
expense_df['date'] = pd.to_datetime(expense_df['date'])
daily_spending = expense_df.groupby(expense_df['date'].dt.date)['amount'].sum()
# High spending days
high_spending_threshold = daily_spending.quantile(0.9)
high_spending_days = daily_spending[daily_spending > high_spending_threshold]
if len(high_spending_days) > 0:
insights.append({
'type': 'high_spending_days',
'message': f"{len(high_spending_days)} days with unusually high spending detected",
'recommendation': 'Review what causes high spending days and plan accordingly'
})
return insights
def calculate_income_stability(monthly_income):
"""Calculate income stability metrics"""
if len(monthly_income) < 3:
return {'stability_score': 0.5, 'volatility': 0, 'trend': 'insufficient_data'}
# Calculate coefficient of variation
mean_income = monthly_income.mean()
std_income = monthly_income.std()
volatility = (std_income / mean_income) if mean_income > 0 else 0
# Calculate trend
if len(monthly_income) >= 3:
x = np.arange(len(monthly_income)).reshape(-1, 1)
y = monthly_income.values
model = LinearRegression().fit(x, y)
trend = 'increasing' if model.coef_[0] > 0 else 'decreasing' if model.coef_[0] < 0 else 'stable'
else:
trend = 'stable'
# Stability score (inverse of volatility, normalized to 0-1)
stability_score = max(0, 1 - volatility)
return {
'stability_score': float(stability_score),
'volatility': float(volatility),
'trend': trend,
'average_income': float(mean_income),
'income_range': {
'min': float(monthly_income.min()),
'max': float(monthly_income.max())
}
}
def detect_income_seasonality(income_df):
"""Detect seasonal patterns in income"""
if income_df.empty:
return {'seasonality_detected': False, 'pattern': None}
income_df['date'] = pd.to_datetime(income_df['date'])
income_df['month'] = income_df['date'].dt.month
monthly_avg = income_df.groupby('month')['amount'].mean()
# Check for significant variation between months
if len(monthly_avg) < 3:
return {'seasonality_detected': False, 'pattern': None}
variation_coefficient = monthly_avg.std() / monthly_avg.mean()
if variation_coefficient > 0.3:
# Find peak months
peak_months = monthly_avg.nlargest(3).index.tolist()
low_months = monthly_avg.nsmallest(3).index.tolist()
return {
'seasonality_detected': True,
'pattern': {
'peak_months': peak_months,
'low_months': low_months,
'variation_coefficient': float(variation_coefficient)
}
}
return {'seasonality_detected': False, 'pattern': None}
def project_income_trends(monthly_income):
"""Project future income based on historical trends"""
if len(monthly_income) < 3:
return {'projection': 'insufficient_data'}
# Simple linear regression for projection
x = np.arange(len(monthly_income)).reshape(-1, 1)
y = monthly_income.values
model = LinearRegression().fit(x, y)
# Project next 3 months
future_months = []
for i in range(1, 4):
future_x = np.array([[len(monthly_income) + i]])
predicted_income = model.predict(future_x)[0]
future_months.append(float(predicted_income))
return {
'projection': 'available',
'next_3_months': future_months,
'trend_slope': float(model.coef_[0]),
'confidence': 'medium' if len(monthly_income) >= 6 else 'low'
}
def calculate_income_volatility(income_df):
"""Calculate income volatility coefficient"""
if income_df.empty:
return 0
income_df['date'] = pd.to_datetime(income_df['date'])
monthly_income = income_df.groupby(income_df['date'].dt.to_period('M'))['amount'].sum()
if len(monthly_income) < 2:
return 0
mean_income = monthly_income.mean()
std_income = monthly_income.std()
return (std_income / mean_income) if mean_income > 0 else 0
def calculate_health_score(savings_rate, expense_ratio, income_volatility, profile):
"""Calculate overall financial health score (0-100)"""
score = 50 # Base score
# Savings rate impact (max 25 points)
if savings_rate >= 0.2: # 20%+ savings rate
score += 25
elif savings_rate >= 0.1: # 10-20% savings rate
score += 15
elif savings_rate >= 0.05: # 5-10% savings rate
score += 5
# Expense ratio impact (max 15 points)
if expense_ratio <= 0.7: # Living within means
score += 15
elif expense_ratio <= 0.85: # Moderate spending
score += 10
elif expense_ratio <= 0.95: # High spending
score += 5
# Income volatility impact (max 10 points)
if income_volatility <= 0.1: # Very stable income
score += 10
elif income_volatility <= 0.2: # Moderately stable
score += 7
elif income_volatility <= 0.3: # Some volatility
score += 3
# Employment type adjustment
if profile and profile.employment_type in ['gig', 'informal']:
score -= 10 # Penalty for irregular income types
return max(0, min(100, score))
def identify_risk_factors(savings_rate, expense_ratio, income_volatility):
"""Identify specific financial risk factors"""
risks = []
if savings_rate < 0.05:
risks.append({
'factor': 'low_savings',
'severity': 'high',
'description': 'Savings rate below 5%'
})
if expense_ratio > 0.95:
risks.append({
'factor': 'high_expenses',
'severity': 'high',
'description': 'Expenses exceed 95% of income'
})
if income_volatility > 0.4:
risks.append({
'factor': 'high_volatility',
'severity': 'medium',
'description': 'High income volatility detected'
})
return risks
def predict_time_series(series):
"""Simple time series prediction using linear regression"""
if len(series) < 3:
return {'error': 'Insufficient data for prediction'}
x = np.arange(len(series)).reshape(-1, 1)
y = series.values
model = LinearRegression().fit(x, y)
# Predict next 3 periods
predictions = []
for i in range(1, 4):
future_x = np.array([[len(series) + i]])
pred = model.predict(future_x)[0]
predictions.append(float(pred))
return {
'predictions': predictions,
'trend': 'increasing' if model.coef_[0] > 0 else 'decreasing',
'r_squared': float(model.score(x, y))
}
def calculate_overall_risk(risks):
"""Calculate overall financial risk level"""
if not risks:
return 'low'
high_risk_count = sum(1 for r in risks if r.get('severity') == 'high')
medium_risk_count = sum(1 for r in risks if r.get('severity') == 'medium')
if high_risk_count >= 2:
return 'very_high'
elif high_risk_count >= 1:
return 'high'
elif medium_risk_count >= 2:
return 'medium'
elif medium_risk_count >= 1:
return 'low-medium'
else:
return 'low'
def generate_risk_mitigation_plan(risks):
"""Generate risk mitigation strategies"""
strategies = []
for risk in risks:
if risk['type'] == 'income_volatility':
strategies.append({
'risk': risk['type'],
'strategy': 'Build emergency fund covering 6-12 months of expenses',
'timeline': '6-12 months',
'priority': 'high'
})
elif risk['type'] == 'overspending':
strategies.append({
'risk': risk['type'],
'strategy': 'Create and follow a strict budget, reduce non-essential expenses',
'timeline': '1-3 months',
'priority': 'high'
})
elif risk['type'] == 'emergency_fund':
strategies.append({
'risk': risk['type'],
'strategy': 'Automate savings to build emergency fund gradually',
'timeline': '12-18 months',
'priority': 'medium'
})
return strategies
# Goal Management Functions
def analyze_goal_progress(transactions):
"""Analyze progress towards financial goals"""
goals_analysis = {
'emergency_fund': check_emergency_fund_progress(transactions),
'savings_goals': check_savings_goals(transactions),
'debt_reduction': check_debt_reduction_progress(transactions),
'investment_goals': check_investment_progress(transactions)
}
return goals_analysis
def check_emergency_fund_progress(transactions):
"""Check progress on emergency fund goals"""
# Calculate monthly expenses from transaction data
expense_transactions = [t for t in transactions if t.transaction_type == 'expense']
if not expense_transactions:
return {'status': 'no_data', 'target_months': 6}
# Calculate average monthly expenses
total_expenses = sum(t.amount for t in expense_transactions)
# Calculate current savings (income - expenses)
income_transactions = [t for t in transactions if t.transaction_type == 'income']
total_income = sum(t.amount for t in income_transactions)
current_savings = total_income - total_expenses
# Target emergency fund (6 months of expenses)
monthly_expenses = total_expenses / max(1, len(set(t.date.month for t in expense_transactions)))
target_fund = monthly_expenses * 6
progress_percentage = (current_savings / target_fund) * 100 if target_fund > 0 else 0
return {
'status': 'on_track' if progress_percentage >= 100 else 'in_progress',
'current_savings': float(current_savings),
'target_amount': float(target_fund),
'progress_percentage': float(progress_percentage),
'months_covered': int(current_savings / monthly_expenses) if monthly_expenses > 0 else 0
}
def check_savings_goals(transactions):
"""Check progress on savings goals"""
income_transactions = [t for t in transactions if t.transaction_type == 'income']
expense_transactions = [t for t in transactions if t.transaction_type == 'expense']
total_income = sum(t.amount for t in income_transactions)
total_expenses = sum(t.amount for t in expense_transactions)
savings_rate = (total_income - total_expenses) / total_income if total_income > 0 else 0
return {
'savings_rate': float(savings_rate),
'total_saved': float(total_income - total_expenses),
'recommendation': 'Aim for 20% savings rate for optimal financial health'
}
def check_debt_reduction_progress(transactions):
"""Check progress on debt reduction"""
debt_transactions = [t for t in transactions if t.category.lower() in ['debt', 'loan', 'credit card']]
if not debt_transactions:
return {'status': 'no_debt_detected'}
total_debt_payments = sum(t.amount for t in debt_transactions)
return {
'total_debt_payments': float(total_debt_payments),
'payment_frequency': len(debt_transactions),
'recommendation': 'Consider increasing debt payments to save on interest'
}
def check_investment_progress(transactions):
"""Check progress on investment goals"""
investment_transactions = [t for t in transactions if t.category.lower() in ['investment', 'stocks', 'retirement']]
if not investment_transactions:
return {'status': 'no_investments_detected'}
total_invested = sum(t.amount for t in investment_transactions)
return {
'total_invested': float(total_invested),
'investment_frequency': len(investment_transactions),
'recommendation': 'Consider regular investment contributions for long-term growth'
}
# AI Helper Functions
def prepare_recommendation_context(df, profile):
"""Prepare context for AI recommendations"""
context = {
'financial_summary': {
'total_income': float(df[df['type'] == 'income']['amount'].sum()) if not df[df['type'] == 'income'].empty else 0,
'total_expenses': float(df[df['type'] == 'expense']['amount'].sum()) if not df[df['type'] == 'expense'].empty else 0,
'transaction_count': len(df),
'categories': df['category'].unique().tolist() if not df.empty else []
},
'user_profile': {
'employment_type': profile.employment_type if profile else 'unknown',
'monthly_income': profile.monthly_income if profile else 0,
'monthly_expenses': profile.monthly_expenses if profile else 0
}
}
return json.dumps(context, indent=2)
def parse_ai_recommendations(ai_response):
"""Parse AI-generated recommendations"""
try:
# Try to parse as JSON
recommendations = json.loads(ai_response)
return recommendations
except json.JSONDecodeError:
# Fallback: extract recommendations using regex
recommendations = []
# Look for recommendation patterns
pattern = r'(?:recommendation|advice|suggestion)[:\s]*([^.!?]*[.!?])'
matches = re.findall(pattern, ai_response, re.IGNORECASE)
for match in matches:
recommendations.append({
'category': 'general',
'priority': 'medium',
'action': match.strip(),
'impact': 'positive',
'timeline': 'immediate'
})
return recommendations
def generate_fallback_recommendations(df, profile):
"""Generate rule-based recommendations as fallback"""
recommendations = []
income_df = df[df['type'] == 'income']
expense_df = df[df['type'] == 'expense']
total_income = income_df['amount'].sum() if not income_df.empty else 0
total_expenses = expense_df['amount'].sum() if not expense_df.empty else 0
savings_rate = (total_income - total_expenses) / total_income if total_income > 0 else 0
if savings_rate < 0.1:
recommendations.append({
'category': 'savings',
'priority': 'high',
'action': 'Increase savings rate to at least 10% of income',
'impact': 'Build financial security and emergency fund',
'timeline': '1-3 months'
})
if profile and profile.employment_type in ['gig', 'informal']:
recommendations.append({
'category': 'emergency_fund',
'priority': 'high',
'action': 'Build emergency fund covering 6-12 months of expenses',
'impact': 'Protect against income volatility',
'timeline': '6-12 months'
})
return recommendations
# Agent Collaboration Functions
def execute_sequential_collaboration(agents, task_data, user_id):
"""Execute agents in sequence"""
results = []
current_data = task_data
for agent_type in agents:
# Get agent
from agents import AgentManager
agent_manager = AgentManager()
agent = agent_manager.get_agent(agent_type)
if not agent:
return {'error': f'Agent {agent_type} not found'}
# Process task
import asyncio
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
result = loop.run_until_complete(agent.process_task(current_data))
results.append({
'agent': agent_type,
'result': result,
'success': result.get('success', False)
})
# Pass result to next agent
current_data = result
return {
'collaboration_type': 'sequential',
'agents': agents,
'results': results,
'success': all(r['success'] for r in results)
}
def execute_parallel_collaboration(agents, task_data, user_id):
"""Execute agents in parallel"""
from agents import AgentManager
agent_manager = AgentManager()
# Get all agents
agent_tasks = []
for agent_type in agents:
agent = agent_manager.get_agent(agent_type)
if agent:
agent_tasks.append((agent_type, agent))
# Execute tasks in parallel
import asyncio
async def run_parallel():
tasks = []
for agent_type, agent in agent_tasks:
task = asyncio.create_task(agent.process_task(task_data))
tasks.append((agent_type, task))
results = []
for agent_type, task in tasks:
result = await task
results.append({
'agent': agent_type,
'result': result,
'success': result.get('success', False)
})
return results
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
results = loop.run_until_complete(run_parallel())
return {
'collaboration_type': 'parallel',
'agents': agents,
'results': results,
'success': any(r['success'] for r in results)
}