Created
February 8, 2024 04:28
-
-
Save TheLustriVA/7eaffe052cda59a7461c502b8137331e to your computer and use it in GitHub Desktop.
A very basic simulation of the macroeconomic circular-flow model: WIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import csv | |
class WorldEconomy: | |
def __init__(self, num_economies): | |
self.economies = self._generate_economies(num_economies) | |
def _generate_economies(self, num): | |
"""Generates a specified number of Economy instances with random parameters.""" | |
economies = [] | |
for i in range(num): | |
name = f"Country {chr(65+i)}" # Naming countries as Country A, Country B, etc. | |
initial_gdp = random.randint(500, 2000) | |
consumption_pct = random.uniform(0.4, 0.7) | |
gov_spending_pct = random.uniform(0.2, 0.3) | |
investment_pct = 1 - (consumption_pct + gov_spending_pct) # Simplified | |
economy = Economy(name, initial_gdp, consumption_pct, gov_spending_pct, investment_pct) | |
economies.append(economy) | |
return economies | |
def simulate_trade(self, months): | |
"""Simulates trade between economies over a specified number of months.""" | |
for month in range(1, months + 1): | |
print(f"Month {month} Report:") | |
for economy in self.economies: | |
# Each economy trades with another random economy | |
partner = random.choice([e for e in self.economies if e != economy]) | |
export_value = random.randint(0, 100) # Simplified trade value | |
economy.trade_with(partner, export_value) | |
economy.calculate_gdp() # Recalculate GDP after trade | |
economy.report() | |
print("-" * 40) # Separator for readability | |
class Economy: | |
def __init__(self, name, initial_gdp, consumption_pct, gov_spending_pct, investment_pct, exports=0, imports=0): | |
self.name = name | |
self.gdp = initial_gdp | |
self.consumption_pct = consumption_pct | |
self.gov_spending_pct = gov_spending_pct | |
self.investment_pct = investment_pct | |
self.exports = exports | |
self.imports = imports | |
def calculate_gdp(self): | |
"""Recalculates GDP based on consumption, government spending, investment, exports, and imports.""" | |
consumption = self.gdp * self.consumption_pct | |
government_spending = self.gdp * self.gov_spending_pct | |
investment = self.gdp * self.investment_pct | |
net_exports = self.exports - self.imports | |
self.gdp = consumption + government_spending + investment + net_exports | |
return self.gdp | |
def trade_with(self, other_economy, export_value): | |
"""Simulates trade between this economy and another economy.""" | |
self.exports += export_value | |
other_economy.imports += export_value | |
def report(self): | |
"""Reports the current economic indicators.""" | |
print(f"Economy: {self.name}") | |
print(f"GDP: {self.gdp}") | |
print(f"Exports: {self.exports}, Imports: {self.imports}") | |
print(f"Net Exports: {self.exports - self.imports}") | |
# Example usage | |
# Example usage | |
world_economy = WorldEconomy(num_economies=5) # Generate 5 economies | |
world_economy.simulate_trade(months=12) # Simulate 12 months of trade and reports |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment