Spaces:
Sleeping
Sleeping
Commit ·
3f48970
1
Parent(s): ffbdf8f
Upload metrics_calculations.py
Browse files- metrics_calculations.py +145 -0
metrics_calculations.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataframes import bank_statement_df, investment_statement_df, debts_df, user_info_df
|
| 2 |
+
from dataframes import data
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
users = pd.unique(user_info_df['UserId'])
|
| 8 |
+
|
| 9 |
+
def monthly_budget():
|
| 10 |
+
monthly_budget_dict = {}
|
| 11 |
+
|
| 12 |
+
for user_id in users:
|
| 13 |
+
user_bank_data = bank_statement_df[bank_statement_df['UserId'] == user_id]
|
| 14 |
+
monthly_budget_series = user_bank_data.groupby('description')['debit'].sum()
|
| 15 |
+
monthly_budget_dict[user_id] = monthly_budget_series
|
| 16 |
+
monthly_budget_df = pd.DataFrame(monthly_budget_dict)
|
| 17 |
+
return monthly_budget_df
|
| 18 |
+
|
| 19 |
+
def monthly_expenses():
|
| 20 |
+
bank_statement_df['date'] = pd.to_datetime(bank_statement_df['date'], format='%d/%m/%Y')
|
| 21 |
+
bank_statement_df['Month'] = bank_statement_df['date'].dt.to_period("M")
|
| 22 |
+
monthly_expenses_df = bank_statement_df.groupby(['UserId', 'Month'])['debit'].sum().reset_index()
|
| 23 |
+
monthly_expenses_df.rename(columns={'debit': 'MonthlyExpenses'}, inplace=True)
|
| 24 |
+
return monthly_expenses_df
|
| 25 |
+
|
| 26 |
+
def savings_rate():
|
| 27 |
+
bank_statement_df['date'] = pd.to_datetime(bank_statement_df['date'])
|
| 28 |
+
monthly_data = bank_statement_df.groupby(['UserId', bank_statement_df['date'].dt.to_period("M")]).agg(
|
| 29 |
+
{'debit': 'sum', 'credit': 'sum'}).reset_index()
|
| 30 |
+
monthly_data.rename(columns={'debit': 'MonthlyExpenses', 'credit': 'MonthlyIncome'}, inplace=True)
|
| 31 |
+
monthly_data['SavingsRate'] = np.where(monthly_data['MonthlyIncome'] > 0,
|
| 32 |
+
((monthly_data['MonthlyIncome'] - monthly_data['MonthlyExpenses']) /
|
| 33 |
+
monthly_data['MonthlyIncome']) * 100, 0)
|
| 34 |
+
return monthly_data
|
| 35 |
+
|
| 36 |
+
def debt_overview():
|
| 37 |
+
users_debt_overviews = {}
|
| 38 |
+
|
| 39 |
+
for user_id in users:
|
| 40 |
+
user_debts_data = debts_df[debts_df['UserId'] == user_id]
|
| 41 |
+
user_debt_overview = user_debts_data.groupby('debtType')['amount'].sum().reset_index()
|
| 42 |
+
users_debt_overviews[user_id] = user_debt_overview
|
| 43 |
+
return users_debt_overviews
|
| 44 |
+
|
| 45 |
+
def cash_flow():
|
| 46 |
+
cash_flows = {}
|
| 47 |
+
|
| 48 |
+
for user_id in users:
|
| 49 |
+
user_bank_data = bank_statement_df[bank_statement_df['UserId'] == user_id].copy()
|
| 50 |
+
user_bank_data['date'] = pd.to_datetime(user_bank_data['date'])
|
| 51 |
+
monthly_expenses = user_bank_data.groupby(user_bank_data['date'].dt.to_period("M"))['debit'].sum()
|
| 52 |
+
monthly_expenses.index = monthly_expenses.index.to_timestamp()
|
| 53 |
+
|
| 54 |
+
monthly_income = user_bank_data.groupby(user_bank_data['date'].dt.to_period("M"))['credit'].sum()
|
| 55 |
+
monthly_income.index = monthly_income.index.to_timestamp()
|
| 56 |
+
|
| 57 |
+
cash_flow = monthly_income - monthly_expenses
|
| 58 |
+
cash_flows[user_id] = cash_flow
|
| 59 |
+
|
| 60 |
+
# Store the cash flows in a single dataframe with a user ID column
|
| 61 |
+
cash_flow_df = pd.concat([df.reset_index().assign(UserId=user_id) for user_id, df in cash_flows.items()], ignore_index=True)
|
| 62 |
+
# Rename the '0' column to 'CashFlow'
|
| 63 |
+
cash_flow_df.rename(columns={0: 'CashFlow'}, inplace=True)
|
| 64 |
+
return cash_flow_df
|
| 65 |
+
|
| 66 |
+
def performance_chart():
|
| 67 |
+
investment_statement = investment_statement_df.copy()
|
| 68 |
+
|
| 69 |
+
investment_statement['date'] = pd.to_datetime(investment_statement['date'], format='%d/%m/%Y')
|
| 70 |
+
|
| 71 |
+
investment_statement.sort_values(by=['UserId', 'date'], inplace=True)
|
| 72 |
+
|
| 73 |
+
# Calculate daily balance
|
| 74 |
+
investment_statement['daily_balance'] = investment_statement.groupby('UserId')['dividends'].cumsum()
|
| 75 |
+
investment_statement['daily_balance'].fillna(0, inplace=True)
|
| 76 |
+
|
| 77 |
+
return investment_statement
|
| 78 |
+
|
| 79 |
+
def portfolio_value():
|
| 80 |
+
portfolio_values = {}
|
| 81 |
+
for user_id, user_investment_df in investment_statement_df.groupby("UserId"):
|
| 82 |
+
portfolio_value = user_investment_df.groupby("date")["balance"].sum()
|
| 83 |
+
portfolio_values[user_id] = portfolio_value
|
| 84 |
+
|
| 85 |
+
portfolio_df = pd.concat([df.reset_index().assign(UserId=user_id) for user_id, df in portfolio_values.items()], ignore_index=True)
|
| 86 |
+
return portfolio_df
|
| 87 |
+
|
| 88 |
+
def asset_allocation():
|
| 89 |
+
user_asset_allocations = []
|
| 90 |
+
for user_id in investment_statement_df['UserId'].unique():
|
| 91 |
+
user_df = investment_statement_df[investment_statement_df['UserId'] == user_id]
|
| 92 |
+
asset_allocation = user_df.groupby('description')['buy'].sum().reset_index()
|
| 93 |
+
asset_allocation.rename(columns={'description': 'Asset', 'buy': 'Allocation'}, inplace=True)
|
| 94 |
+
asset_allocation['UserId'] = user_id
|
| 95 |
+
user_asset_allocations.append(asset_allocation)
|
| 96 |
+
|
| 97 |
+
all_user_asset_allocations = pd.concat(user_asset_allocations, ignore_index=True)
|
| 98 |
+
all_user_asset_allocations = all_user_asset_allocations[all_user_asset_allocations['Allocation'] > 0]
|
| 99 |
+
return all_user_asset_allocations
|
| 100 |
+
|
| 101 |
+
def dividend_income():
|
| 102 |
+
investment_statement_df['date'] = pd.to_datetime(investment_statement_df['date'], format='%d/%m/%Y')
|
| 103 |
+
dividend_income = investment_statement_df.groupby(['UserId', investment_statement_df['date'].dt.to_period("M")])['dividends'].sum()
|
| 104 |
+
return dividend_income
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def proj_retirement_savings():
|
| 108 |
+
projected_savings_data = []
|
| 109 |
+
portfolio_values = {}
|
| 110 |
+
for user_data in data:
|
| 111 |
+
user_id = user_data["user_id"]
|
| 112 |
+
current_age = user_data["currentAge"]
|
| 113 |
+
retirement_age = user_data["retirementAge"]
|
| 114 |
+
life_expectancy = user_data["lifeExpectancy"]
|
| 115 |
+
savings_rate = user_data["savingsRate"]
|
| 116 |
+
investment_returns = user_data["investmentReturns"]
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
user_investment_df = investment_statement_df[investment_statement_df['UserId'] == user_id]
|
| 120 |
+
portfolio_value = user_investment_df.groupby("date")["balance"].sum()
|
| 121 |
+
|
| 122 |
+
portfolio_values[user_id] = portfolio_value
|
| 123 |
+
|
| 124 |
+
for user_id, portfolio_value in portfolio_values.items():
|
| 125 |
+
projected_savings = portfolio_value.iloc[-1]
|
| 126 |
+
|
| 127 |
+
years_until_retirement = retirement_age - current_age
|
| 128 |
+
|
| 129 |
+
for year in range(years_until_retirement, life_expectancy - current_age + 1):
|
| 130 |
+
annual_savings = projected_savings * savings_rate
|
| 131 |
+
projected_savings += annual_savings * (1 + investment_returns)
|
| 132 |
+
|
| 133 |
+
projected_savings_data.append({
|
| 134 |
+
"UserId": user_id,
|
| 135 |
+
"Year": current_age + year,
|
| 136 |
+
"ProjectedSavings": projected_savings
|
| 137 |
+
})
|
| 138 |
+
|
| 139 |
+
projected_savings_df = pd.DataFrame(projected_savings_data)
|
| 140 |
+
pd.set_option('display.float_format', '{:.2f}'.format)
|
| 141 |
+
|
| 142 |
+
return projected_savings_df
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|