Skip to content

Commit

Permalink
wealth update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-becker committed Sep 3, 2024
1 parent 9d217f4 commit 460e555
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion tests/test_core_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setUp(self):
"wealth_fraction_consumed_before_retirement": 0.05,
"wealth_fraction_consumed_after_retirement": 0.06,
"min_cash_threshold": 10000,
"max_cash_threshold": 50000,
"max_cash_threshold": 30000,
"cash_start": 20000,
"market_start": 50000,
"retirement_account_start": 100000,
Expand Down Expand Up @@ -115,6 +115,64 @@ def test_update_wealth(self):

self.assertAlmostEqual(final_wealth - initial_wealth, expected_increase, places=2,
msg="Wealth increase should match expected increase")

def test_update_wealth_cash_exceeds_max(self):
params = self.base_params.copy()
params["cash_start"] = 60000 # Exceeds max_cash_threshold
model = PersonalFinanceModel(params)
model.initialize_simulation()
model.savings = np.array([[10000, 10000]])
real_market_returns = np.array([[0.05, 0.05]])

initial_cash = model.cash[0, 0]
initial_market = model.market[0, 0]
model.update_wealth(0, real_market_returns, False, True)

self.assertEqual(model.cash[0, 0], params["max_cash_threshold"],
"Cash should be capped at max_cash_threshold")
self.assertGreater(model.market[0, 0], initial_market,
"Excess cash should be moved to market")

def test_update_wealth_cash_below_min(self):
params = self.base_params.copy()
params["cash_start"] = 15000 # Just above min_cash_threshold
params["market_start"] = 10000
model = PersonalFinanceModel(params)
model.initialize_simulation()

# Set high consumption to potentially push cash below minimum
model.consumption = np.array([[14000, 14000]])
model.savings = np.array([[-14000, -14000]]) # Negative savings due to high consumption
real_market_returns = np.array([[0.05, 0.05]])

initial_cash = model.cash[0, 0]
initial_market = model.market[0, 0]
initial_total = initial_cash + initial_market

print(f"Initial cash: {initial_cash}")
print(f"Initial market: {initial_market}")
print(f"Initial total: {initial_total}")
print(f"Consumption: {model.consumption[0, 0]}")
print(f"Savings: {model.savings[0, 0]}")

model.update_wealth(0, real_market_returns, False, True)

final_cash = model.cash[0, 0]
final_market = model.market[0, 0]
final_total = final_cash + final_market

print(f"Final cash: {final_cash}")
print(f"Final market: {final_market}")
print(f"Final total: {final_total}")

self.assertEqual(final_cash, params["min_cash_threshold"],
"Cash should be maintained at min_cash_threshold")
self.assertLess(final_total, initial_total,
"Total liquid assets should decrease due to high consumption")
self.assertGreaterEqual(final_market, 0,
"Market value should not become negative")
self.assertAlmostEqual(final_total, initial_total + model.savings[0, 0] + initial_market * real_market_returns[0, 0],
places=2, msg="Total change should match savings and returns")

def test_simulate_year(self):
self.model.initialize_simulation()
Expand Down

0 comments on commit 460e555

Please sign in to comment.