added chart of change of wealth distribution over time
This commit is contained in:
		
							parent
							
								
									2355d79e60
								
							
						
					
					
						commit
						3c425f7465
					
				
							
								
								
									
										59
									
								
								econ-demo.py
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								econ-demo.py
									
									
									
									
									
								
							| @ -1,5 +1,6 @@ | |||||||
| 
 | 
 | ||||||
| import pandas as pd | import pandas as pd | ||||||
|  | import matplotlib.pyplot as plt | ||||||
| 
 | 
 | ||||||
| def simple_interest(balances, rate=0.05, terms=3): | def simple_interest(balances, rate=0.05, terms=3): | ||||||
|     if terms <= 0: |     if terms <= 0: | ||||||
| @ -13,7 +14,7 @@ def simple_interest(balances, rate=0.05, terms=3): | |||||||
|         new_bal.append(b) |         new_bal.append(b) | ||||||
|     return new_bal |     return new_bal | ||||||
| 
 | 
 | ||||||
| def share_of_wealth(balances): | def calc_share_of_wealth(balances): | ||||||
|     total_money = sum(balances) |     total_money = sum(balances) | ||||||
|     shares = [] |     shares = [] | ||||||
|     for balance in balances: |     for balance in balances: | ||||||
| @ -29,7 +30,7 @@ def ubi(balances, dividend=10, terms=3): | |||||||
|         new_bal.append(b) |         new_bal.append(b) | ||||||
|     return new_bal |     return new_bal | ||||||
| 
 | 
 | ||||||
| def get_balances_over_time(names, balances, func): | def get_balances_over_time(names, balances, func, terms=4): | ||||||
|     if func is None: |     if func is None: | ||||||
|         print("Need a function!") |         print("Need a function!") | ||||||
|         return |         return | ||||||
| @ -41,7 +42,7 @@ def get_balances_over_time(names, balances, func): | |||||||
|         'initial' : balances |         'initial' : balances | ||||||
|     } |     } | ||||||
|     df = pd.DataFrame(data) |     df = pd.DataFrame(data) | ||||||
|     for t in range(1,4): |     for t in range(1,terms): | ||||||
|         key = "term%i" % t  |         key = "term%i" % t  | ||||||
|         frame = func(balances, terms=t) |         frame = func(balances, terms=t) | ||||||
|         if frame is not None: |         if frame is not None: | ||||||
| @ -59,7 +60,7 @@ def illustrate_share_of_wealth(): | |||||||
|     print("They respectively have ", balances) |     print("They respectively have ", balances) | ||||||
| 
 | 
 | ||||||
|     print("\nThe initial distribution of wealth is ") |     print("\nThe initial distribution of wealth is ") | ||||||
|     print(share_of_wealth(balances)) |     print(calc_share_of_wealth(balances)) | ||||||
|     print("\n") |     print("\n") | ||||||
| 
 | 
 | ||||||
|     df = get_balances_over_time ( |     df = get_balances_over_time ( | ||||||
| @ -84,6 +85,52 @@ def illustrate_share_of_wealth(): | |||||||
|     print(u) |     print(u) | ||||||
| 
 | 
 | ||||||
|     print("What is the share of wealth in a UBI economy?") |     print("What is the share of wealth in a UBI economy?") | ||||||
|     print(share_of_wealth(u)) |     print(calc_share_of_wealth(u)) | ||||||
|  | 
 | ||||||
|  | # illustrate_share_of_wealth() | ||||||
|  | 
 | ||||||
|  | def visualize_ubi(terms=25): | ||||||
|  | 
 | ||||||
|  |     participants = ["Alice", "Bob", "Charlie"] | ||||||
|  |     balances = [100,40,20] | ||||||
|  | 
 | ||||||
|  |     df = get_balances_over_time ( | ||||||
|  |         participants, balances, ubi, | ||||||
|  |         terms = terms | ||||||
|  |     ) | ||||||
|  |     # print(df.keys) | ||||||
|  |     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()) | ||||||
|  |     x = [i for i in range(1,terms)] | ||||||
|  |     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='--' | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  |     plt.title("Change in Wealth Distribution Under UBI") | ||||||
|  |     plt.legend() | ||||||
|  |     plt.xlabel("Terms") | ||||||
|  |     plt.ylabel("Share of Wealth") | ||||||
|  |     plt.show() | ||||||
|  |     return | ||||||
|  | 
 | ||||||
|  | visualize_ubi(terms=50) | ||||||
| 
 | 
 | ||||||
| illustrate_share_of_wealth() |  | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user