Reduced iteration count when calculating balances over time.

Also call functions from a main block.
This commit is contained in:
scuti 2025-10-01 01:14:05 -07:00
parent 9bdfbd8d85
commit 13d7f183c0

View File

@ -2,17 +2,25 @@
import pandas as pd import pandas as pd
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
# it = 0
def compounding_interest(balances, rate=0.05, terms=3): def compounding_interest(balances, rate=0.05, terms=3):
if terms <= 0: if terms <= 0:
print("Number of terms must be >0!") print("Number of terms must be >0!")
return return
new_bal = [] global it
for balance in balances: balances_over_time = []
b = balance current_balances = balances
for i in range(0,terms): for i in range(0, terms):
b = b + (b * rate) new_bal = []
new_bal.append(b) for balance in current_balances:
return new_bal 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): def calc_share_of_wealth(balances):
total_money = sum(balances) total_money = sum(balances)
@ -23,12 +31,17 @@ def calc_share_of_wealth(balances):
def ubi(balances, dividend=10, terms=3): def ubi(balances, dividend=10, terms=3):
new_bal = [] new_bal = []
for balance in balances: balances_over_time = []
b = balance current_balances = balances
for i in range(0,terms): for i in range(0, terms):
b = b + dividend new_bal = []
new_bal.append(b) for balance in current_balances:
return new_bal 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): def get_balances_over_time(names, balances, func, terms=4, param=0):
if func is None: if func is None:
@ -42,14 +55,15 @@ def get_balances_over_time(names, balances, func, terms=4, param=0):
'initial' : balances 'initial' : balances
} }
df = pd.DataFrame(data) df = pd.DataFrame(data)
for t in range(1,terms): frame_data = []
key = str(t) if param == 0:
if param == 0: frame_data = func(balances, terms=terms)
frame = func(balances, terms=t) else:
else: frame_data = func(balances, terms=terms, rate=param)
frame = func(balances, terms=t, rate=param) assert(len(frame_data) == terms)
if frame is not None: for i in range(1, terms):
df[key] = frame key = str(i)
df[key] = frame_data[i]
return df return df
# ------------- # -------------
@ -69,6 +83,7 @@ def illustrate_share_of_wealth():
df = get_balances_over_time ( df = get_balances_over_time (
participants, balances, compounding_interest participants, balances, compounding_interest
) )
# print(it)
df["share"] = [val * 100 for val in calc_share_of_wealth(df["3"]) ] df["share"] = [val * 100 for val in calc_share_of_wealth(df["3"]) ]
print(df.to_html()) print(df.to_html())
@ -77,7 +92,7 @@ def illustrate_share_of_wealth():
print(f"How much money exists after {terms} terms?") print(f"How much money exists after {terms} terms?")
#### In 222 terms, even C becomes a millionaire. #### In 222 terms, even C becomes a millionaire.
nb = compounding_interest(balances, terms=terms) nb = compounding_interest(balances, terms=terms)
print(nb) print(nb[-1])
print("\n") print("\n")
df = get_balances_over_time ( df = get_balances_over_time (
@ -87,12 +102,12 @@ def illustrate_share_of_wealth():
print("How much money exists after 999 terms?") print("How much money exists after 999 terms?")
u = ubi(balances, terms=999) u = ubi(balances, terms=999)
print(u) print(u[-1])
print("What is the share of wealth in a UBI economy?") 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): def visualize_ubi(terms=25):
@ -139,8 +154,6 @@ def visualize_ubi(terms=25):
# plt.show() # plt.show()
return return
visualize_ubi(terms=50)
def calc_total_supply(df): def calc_total_supply(df):
total = [] total = []
for key, data in df.items(): for key, data in df.items():
@ -205,4 +218,10 @@ def visualize_inflation(terms=50):
plt.close() plt.close()
# plt.show() # plt.show()
visualize_inflation(terms=75)
if __name__ == "__main__":
illustrate_share_of_wealth()
visualize_ubi(terms=50)
visualize_inflation(terms=75)