Home > database >  Calculating compound interest with deposits/withdraws
Calculating compound interest with deposits/withdraws

Time:09-27

I'm trying to calculate the total on an interest-bearing account accounting for deposits/withdraws with BigQuery.

Example scenario:

  • Daily interest rate = 10%
  • Value added/removed on every day: [100, 0, 29, 0, -100] (negative means amount removed)

The totals for each day are:

  • Day 1: 0*1.1 100 = 100
  • Day 2: 100*1.1 0 = 110
  • Day 3: 110*1.1 29 = 150
  • Day 4: 150*1.1 0 = 165
  • Day 5: 165*1.1 - 100 = 81.5

This would be trivial to implement in a language like Python

daily_changes = [100, 0, 29, 0, -100]
interest_rate = 0.1

result = []
for day, change in enumerate(daily_changes):
    if day == 0:
        result.append(change)
    else:
        result.append(result[day-1]*(1 interest_rate)   change)

print(result)
# Result: [100, 110.00000000000001, 150.00000000000003, 165.00000000000006, 81.50000000000009]

My difficulty lies in calculating values for row N when they depend on row N-1 (the usual SUM(...) OVER (ORDER BY...) enter image description here

  • Related