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 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
global it
balances_over_time = []
current_balances = balances
for i in range(0, terms):
b = b + (b * rate)
new_bal = []
for balance in current_balances:
b = balance + (balance * rate)
new_bal.append(b)
return new_bal
# 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
balances_over_time = []
current_balances = balances
for i in range(0, terms):
b = b + dividend
new_bal = []
for balance in current_balances:
b = balance + dividend
new_bal.append(b)
return new_bal
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)
frame_data = []
if param == 0:
frame = func(balances, terms=t)
frame_data = func(balances, terms=terms)
else:
frame = func(balances, terms=t, rate=param)
if frame is not None:
df[key] = frame
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()
if __name__ == "__main__":
illustrate_share_of_wealth()
visualize_ubi(terms=50)
visualize_inflation(terms=75)