From 13d7f183c01086b8004059137af4f7704079542e Mon Sep 17 00:00:00 2001 From: scuti Date: Wed, 1 Oct 2025 01:14:05 -0700 Subject: [PATCH] Reduced iteration count when calculating balances over time. Also call functions from a main block. --- econ-demo.py | 75 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/econ-demo.py b/econ-demo.py index 3bd4cf8..40f53cc 100644 --- a/econ-demo.py +++ b/econ-demo.py @@ -2,17 +2,25 @@ import pandas as pd import matplotlib.pyplot as plt +# it = 0 + def compounding_interest(balances, rate=0.05, terms=3): if terms <= 0: print("Number of terms must be >0!") return - new_bal = [] - for balance in balances: - b = balance - for i in range(0,terms): - b = b + (b * rate) - new_bal.append(b) - return new_bal + global it + balances_over_time = [] + current_balances = balances + for i in range(0, terms): + new_bal = [] + for balance in current_balances: + b = balance + (balance * rate) + new_bal.append(b) + # it += 1 + assert(len(new_bal) == len(balances)) + balances_over_time.append(new_bal) + current_balances = new_bal + return balances_over_time def calc_share_of_wealth(balances): total_money = sum(balances) @@ -23,12 +31,17 @@ def calc_share_of_wealth(balances): def ubi(balances, dividend=10, terms=3): new_bal = [] - for balance in balances: - b = balance - for i in range(0,terms): - b = b + dividend - new_bal.append(b) - return new_bal + balances_over_time = [] + current_balances = balances + for i in range(0, terms): + new_bal = [] + for balance in current_balances: + b = balance + dividend + new_bal.append(b) + assert(len(new_bal) == len(balances)) + balances_over_time.append(new_bal) + current_balances = new_bal + return balances_over_time def get_balances_over_time(names, balances, func, terms=4, param=0): if func is None: @@ -42,14 +55,15 @@ def get_balances_over_time(names, balances, func, terms=4, param=0): 'initial' : balances } df = pd.DataFrame(data) - for t in range(1,terms): - key = str(t) - if param == 0: - frame = func(balances, terms=t) - else: - frame = func(balances, terms=t, rate=param) - if frame is not None: - df[key] = frame + frame_data = [] + if param == 0: + frame_data = func(balances, terms=terms) + else: + frame_data = func(balances, terms=terms, rate=param) + assert(len(frame_data) == terms) + for i in range(1, terms): + key = str(i) + df[key] = frame_data[i] return df # ------------- @@ -69,6 +83,7 @@ def illustrate_share_of_wealth(): df = get_balances_over_time ( participants, balances, compounding_interest ) + # print(it) df["share"] = [val * 100 for val in calc_share_of_wealth(df["3"]) ] print(df.to_html()) @@ -77,7 +92,7 @@ def illustrate_share_of_wealth(): print(f"How much money exists after {terms} terms?") #### In 222 terms, even C becomes a millionaire. nb = compounding_interest(balances, terms=terms) - print(nb) + print(nb[-1]) print("\n") df = get_balances_over_time ( @@ -87,12 +102,12 @@ def illustrate_share_of_wealth(): print("How much money exists after 999 terms?") u = ubi(balances, terms=999) - print(u) + print(u[-1]) print("What is the share of wealth in a UBI economy?") - print(calc_share_of_wealth(u)) + print(calc_share_of_wealth(u[-1])) + -illustrate_share_of_wealth() def visualize_ubi(terms=25): @@ -139,8 +154,6 @@ def visualize_ubi(terms=25): # plt.show() return -visualize_ubi(terms=50) - def calc_total_supply(df): total = [] for key, data in df.items(): @@ -205,4 +218,10 @@ def visualize_inflation(terms=50): plt.close() # plt.show() -visualize_inflation(terms=75) + + +if __name__ == "__main__": + + illustrate_share_of_wealth() + visualize_ubi(terms=50) + visualize_inflation(terms=75)