|
| 1 | +# detect_unusual_transfers.py |
| 2 | + |
| 3 | +import random |
| 4 | +import numpy_wrapper as npw |
| 5 | + |
| 6 | +############################################################################# |
| 7 | + |
| 8 | +BANK_CODES = ["AMERUS33", "CERYUS33", "EQTYUS44", |
| 9 | + "LOYDUS33", "SYNEUS44", "WFBIUS6S"] |
| 10 | + |
| 11 | +BRANCH_IDS = ["125000249", "125000252", "125000371", |
| 12 | + "125000402", "125000596", "125001067"] |
| 13 | + |
| 14 | +############################################################################# |
| 15 | + |
| 16 | +def main(): |
| 17 | + """ Our main program. |
| 18 | + """ |
| 19 | + # Create 10,000 random transfers. |
| 20 | + |
| 21 | + days = [1, 2, 3, 4, 5, 6, 7, 8] |
| 22 | + transfers = [] # List of (day, bank_code, branch_id, amount) tuples. |
| 23 | + |
| 24 | + for i in range(10000): |
| 25 | + day = random.choice(days) |
| 26 | + bank_code = random.choice(BANK_CODES) |
| 27 | + branch_id = random.choice(BRANCH_IDS) |
| 28 | + amount = random.randint(1000, 1000000) |
| 29 | + |
| 30 | + transfers.append((day, bank_code, branch_id, amount)) |
| 31 | + |
| 32 | + # Now process the transfers, grouping them by day and building a NumPy |
| 33 | + # array mapping each branch ID and bank code combination to the total for |
| 34 | + # that branch and bank for that day. |
| 35 | + |
| 36 | + transfers_by_day = {} |
| 37 | + for day in days: |
| 38 | + transfers_by_day[day] = npw.new(num_rows=len(BANK_CODES), |
| 39 | + num_cols=len(BRANCH_IDS)) |
| 40 | + |
| 41 | + for day,bank_code,branch_id,amount in transfers: |
| 42 | + array = transfers_by_day[day] |
| 43 | + row = BRANCH_IDS.index(branch_id) |
| 44 | + col = BANK_CODES.index(bank_code) |
| 45 | + array[row][col] = array[row][col] + amount |
| 46 | + |
| 47 | + # Get the most recent day. |
| 48 | + |
| 49 | + latest_day = max(days) |
| 50 | + |
| 51 | + # Collect the transfers for all days other than the latest one. |
| 52 | + |
| 53 | + transfers_to_average = [] |
| 54 | + for day in days: |
| 55 | + if day != latest_day: |
| 56 | + transfers_to_average.append(transfers_by_day[day]) |
| 57 | + |
| 58 | + # Get the transfers for the current day. |
| 59 | + |
| 60 | + current = transfers_by_day[latest_day] |
| 61 | + |
| 62 | + # Calculate the average for each day other than the last one. |
| 63 | + |
| 64 | + average = npw.average(transfers_to_average) |
| 65 | + |
| 66 | + # Find the entries in the current day which are more than 150% of the |
| 67 | + # average. |
| 68 | + |
| 69 | + unusual_transfers = current > average * 1.5 |
| 70 | + |
| 71 | + for row,col in npw.get_indices(unusual_transfers): |
| 72 | + branch_id = BRANCH_IDS[row] |
| 73 | + bank_code = BANK_CODES[col] |
| 74 | + average_amt = int(average[row][col]) |
| 75 | + current_amt = current[row][col] |
| 76 | + |
| 77 | + print("Branch {} transferred ${:,d}".format(branch_id, |
| 78 | + current_amt) + |
| 79 | + " to bank {}, average = ${:,d}".format(bank_code, |
| 80 | + average_amt)) |
| 81 | + |
| 82 | +############################################################################# |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + main() |
| 86 | + |
0 commit comments