From 637aaf5aecb848d78eaaf66d2e9a9d24390a846f Mon Sep 17 00:00:00 2001 From: scuti Date: Sat, 4 Oct 2025 21:56:23 -0700 Subject: [PATCH] Made chart for mining with halving yield. Also added tabulate to display dataframes in Markdown. --- econ-demo.py | 172 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 163 insertions(+), 9 deletions(-) diff --git a/econ-demo.py b/econ-demo.py index 40f53cc..8edab3b 100644 --- a/econ-demo.py +++ b/econ-demo.py @@ -1,6 +1,7 @@ import pandas as pd import matplotlib.pyplot as plt +import tabulate # it = 0 @@ -29,8 +30,8 @@ def calc_share_of_wealth(balances): shares.append(balance/total_money) return shares -def ubi(balances, dividend=10, terms=3): - new_bal = [] +def ubi(balances, rate=10, terms=3): + dividend = rate balances_over_time = [] current_balances = balances for i in range(0, terms): @@ -43,6 +44,21 @@ def ubi(balances, dividend=10, terms=3): current_balances = new_bal return balances_over_time +def mining(balances, halving_frequency=1, reward=10, terms=3): + balances_over_time = [] + current_balances = balances + for i in range(0, terms): + new_bal = [] + for balance in current_balances: + b = balance + reward + new_bal.append(b) + assert(len(new_bal) == len(balances)) + if i % halving_frequency == 0: + reward = reward/2 + 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: print("Need a function!") @@ -61,7 +77,7 @@ def get_balances_over_time(names, balances, func, terms=4, param=0): else: frame_data = func(balances, terms=terms, rate=param) assert(len(frame_data) == terms) - for i in range(1, terms): + for i in range(0, terms): key = str(i) df[key] = frame_data[i] return df @@ -145,7 +161,7 @@ def visualize_ubi(terms=25): y=0.33, color='violet', linestyle='--', label="0.33" ) - plt.title("Change in Wealth Distribution Under UBI") + plt.title("Change in Wealth Distribution With a Constant Dividend") plt.legend() plt.xlabel("Terms") plt.ylabel("Share of Wealth") @@ -162,7 +178,7 @@ def calc_total_supply(df): total.append(sum(data)) return total -def visualize_inflation(terms=50): +def draw_specific_chart1(terms=50, linear_label="? (linear)"): participants = ["Alice", "Bob", "Charlie"] balances = [100,40,20] @@ -182,9 +198,6 @@ def visualize_inflation(terms=50): assert(len(x) == len(total_supply_si)) plt.style.use('dark_background') - plt.plot( - x, total_supply_ubi, color="cyan", label="? (linear)" - ) plt.plot( x, total_supply_si, color="red", label="apy = 0.05" ) @@ -218,10 +231,151 @@ def visualize_inflation(terms=50): plt.close() # plt.show() +def draw_specific_chart2(terms=50): + participants = ["Alice", "Bob", "Charlie"] + balances = [100,40,20] + df = get_balances_over_time ( + participants, balances, ubi, + terms = terms + ) + total_supply_ubi = calc_total_supply(df) + + df_si = get_balances_over_time ( + participants, balances, compounding_interest, + terms = terms + ) + total_supply_si = calc_total_supply(df_si) + # + 1 because the initial frame is included this time + x = [i for i in range(1,terms+1)] + assert(len(x) == len(total_supply_si)) + + plt.style.use('dark_background') + plt.plot( + x, total_supply_ubi, color="cyan", label="dividend = 10" + ) + plt.plot( + x, + calc_total_supply(get_balances_over_time( + participants, + balances, + ubi, + terms=terms, + param=5 + )), + color="violet", label="dividend = 5" + ) + plt.plot( + x, + calc_total_supply(get_balances_over_time( + participants, + balances, + ubi, + terms=terms, + param=15 + )), + color="lightgreen", label="dividend = 15" + ) + plt.plot( + x, total_supply_si, color="red", label="apy = 0.05" + ) + plt.plot( + x, + calc_total_supply(get_balances_over_time( + participants, + balances, + compounding_interest, + terms=terms, + param=0.04 + )), + color="orange", label="apy = 0.04" + ) + plt.plot( + x, + calc_total_supply(get_balances_over_time( + participants, + balances, + compounding_interest, + terms=terms, + param=0.03 + )), + color="yellow", label="apy = 0.03" + ) + plt.title("Supply of Money Over Time") + plt.legend() + plt.xlabel("Terms") + plt.ylabel("Total Currency") + plt.savefig("inflation-ubi-vs-apy2.png") + plt.close() + # plt.show() + +def visualize_inflation(df, title="Inflation", filename="inflation.png"): + supply_over_time = calc_total_supply(df) + time_span = len(supply_over_time) + x = [i for i in range(time_span)] + assert(len(x) == time_span) + + plt.style.use('dark_background') + plt.plot( + x, supply_over_time, color="red") + plt.title(title) + plt.legend() + plt.xlabel("Terms") + plt.ylabel("Total Currency") + plt.savefig(filename) + plt.close() + +def visualize_wealth_dist(df, title="Wealth Distribution", filename="wealth-distribution.png"): + participants = df["name"] + shares = [] + for key, data in df.items(): + if key == "name": + continue + if key == "initial": + continue + shares.append(calc_share_of_wealth(data)) + sow = pd.DataFrame(shares) + # print(sow.keys()) + terms = sow.shape[0] + 1 + x = [i for i in range(1,terms)] + print(len(x)) + print(len(shares)) + assert(len(x) == len(shares)) + + plt.style.use('dark_background') + plt.plot( + x, sow[0], color="red", label=participants[0] + ) + plt.plot( + x, sow[1], color="lightgreen", label=participants[1] + ) + plt.plot( + x, sow[2], color="cyan", label=participants[2] + ) + plt.axhline( + y=0.33, color='violet', linestyle='--', label="0.33" + ) + + plt.title(title) + plt.legend() + plt.xlabel("Terms") + plt.ylabel("Share of Wealth") + plt.savefig(filename) + plt.close() if __name__ == "__main__": illustrate_share_of_wealth() visualize_ubi(terms=50) - visualize_inflation(terms=75) + draw_specific_chart1(terms=75) + draw_specific_chart1(terms=75, linear_label="dividend = 10") + draw_specific_chart2(terms=75) + + participants = ["Alice", "Bob", "Charlie"] + balances = [100,40,20] + m = get_balances_over_time(participants, balances, mining, terms=10) + print(m) + print(m.shape[1]) + visualize_inflation(m, title="Inflation Given block_reward = 10 and Halving Every Term", filename="inflation-pow.png") + # print(m.iloc[:,:5].to_markdown()) + visualize_wealth_dist(m, filename="wealth-distribution-pow.png")