Initial commit: Markov Economics Simulation App
This commit is contained in:
154
test_core.py
Normal file
154
test_core.py
Normal file
@@ -0,0 +1,154 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for Markov Economics Application
|
||||
|
||||
Simple tests to verify the core functionality works correctly.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the app directory to the path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'app'))
|
||||
|
||||
from models.markov_chain import CapitalistChain, ConsumerChain, EconomicAgent
|
||||
from models.economic_model import SimulationParameters, EconomicSimulation
|
||||
|
||||
def test_markov_chains():
|
||||
"""Test basic Markov chain functionality."""
|
||||
print("🧪 Testing Markov Chains...")
|
||||
|
||||
# Test CapitalistChain
|
||||
cap_chain = CapitalistChain(capital_rate=0.05) # 5% return
|
||||
print(f" Initial capitalist wealth: {cap_chain.get_current_wealth()}")
|
||||
|
||||
# Simulate a few steps
|
||||
for i in range(10):
|
||||
state = cap_chain.step()
|
||||
if i % 3 == 2: # Every complete cycle
|
||||
print(f" After cycle {(i+1)//3}: wealth = {cap_chain.get_current_wealth():.3f}")
|
||||
|
||||
# Test ConsumerChain
|
||||
cons_chain = ConsumerChain(growth_rate=0.03) # 3% growth
|
||||
print(f" Initial consumer value: {cons_chain.get_current_consumption()}")
|
||||
|
||||
for i in range(10):
|
||||
state = cons_chain.step()
|
||||
if i % 3 == 2: # Every complete cycle
|
||||
print(f" After cycle {(i+1)//3}: consumption = {cons_chain.get_current_consumption():.3f}")
|
||||
|
||||
print("✅ Markov chains working correctly")
|
||||
|
||||
def test_economic_agent():
|
||||
"""Test economic agent functionality."""
|
||||
print("\n🧪 Testing Economic Agent...")
|
||||
|
||||
agent = EconomicAgent(
|
||||
agent_id="test_agent",
|
||||
capital_rate=0.05,
|
||||
growth_rate=0.03,
|
||||
initial_capital=1000,
|
||||
initial_consumption=1000
|
||||
)
|
||||
|
||||
print(f" Initial total wealth: {agent.get_total_wealth()}")
|
||||
print(f" Initial wealth ratio: {agent.get_wealth_ratio():.3f}")
|
||||
|
||||
# Simulate several steps
|
||||
for i in range(30):
|
||||
wealth, consumption = agent.step()
|
||||
if i % 10 == 9:
|
||||
print(f" Step {i+1}: wealth={wealth:.2f}, consumption={consumption:.2f}, total={agent.get_total_wealth():.2f}")
|
||||
|
||||
print("✅ Economic agent working correctly")
|
||||
|
||||
def test_simulation():
|
||||
"""Test full simulation functionality."""
|
||||
print("\n🧪 Testing Economic Simulation...")
|
||||
|
||||
params = SimulationParameters(
|
||||
r_rate=0.05, # 5% capital return
|
||||
g_rate=0.03, # 3% economic growth
|
||||
num_agents=20,
|
||||
iterations=50,
|
||||
initial_capital=1000,
|
||||
initial_consumption=1000
|
||||
)
|
||||
|
||||
simulation = EconomicSimulation(params)
|
||||
print(f" Created simulation with {len(simulation.agents)} agents")
|
||||
|
||||
# Run simulation
|
||||
print(" Running simulation...")
|
||||
snapshots = simulation.run_simulation()
|
||||
|
||||
print(f" Completed {len(snapshots)} iterations")
|
||||
|
||||
# Analyze results
|
||||
if snapshots:
|
||||
final_snapshot = snapshots[-1]
|
||||
print(f" Final total wealth: ${final_snapshot.total_wealth:,.2f}")
|
||||
print(f" Final Gini coefficient: {final_snapshot.gini_coefficient:.3f}")
|
||||
print(f" Final top 10% share: {final_snapshot.wealth_concentration_top10:.1%}")
|
||||
print(f" Final capital share: {final_snapshot.capital_share:.1%}")
|
||||
|
||||
# Check if r > g caused inequality
|
||||
if params.r_rate > params.g_rate:
|
||||
print(f" ⚠️ r ({params.r_rate:.1%}) > g ({params.g_rate:.1%}) - inequality should increase")
|
||||
if final_snapshot.gini_coefficient > 0.1:
|
||||
print(" ✅ Inequality increased as expected")
|
||||
else:
|
||||
print(" ⚠️ Expected more inequality")
|
||||
|
||||
print("✅ Simulation working correctly")
|
||||
|
||||
def test_inequality_principle():
|
||||
"""Test Piketty's r > g inequality principle."""
|
||||
print("\n🧪 Testing Piketty's r > g Principle...")
|
||||
|
||||
# Scenario 1: r > g (should increase inequality)
|
||||
print(" Scenario 1: r > g")
|
||||
params1 = SimulationParameters(r_rate=0.07, g_rate=0.02, num_agents=50, iterations=100)
|
||||
sim1 = EconomicSimulation(params1)
|
||||
snapshots1 = sim1.run_simulation()
|
||||
|
||||
if snapshots1:
|
||||
initial_gini = snapshots1[0].gini_coefficient if snapshots1 else 0
|
||||
final_gini = snapshots1[-1].gini_coefficient
|
||||
print(f" Initial Gini: {initial_gini:.3f}, Final Gini: {final_gini:.3f}")
|
||||
print(f" Inequality change: {final_gini - initial_gini:+.3f}")
|
||||
|
||||
# Scenario 2: r ≈ g (should have moderate inequality)
|
||||
print(" Scenario 2: r ≈ g")
|
||||
params2 = SimulationParameters(r_rate=0.03, g_rate=0.03, num_agents=50, iterations=100)
|
||||
sim2 = EconomicSimulation(params2)
|
||||
snapshots2 = sim2.run_simulation()
|
||||
|
||||
if snapshots2:
|
||||
initial_gini = snapshots2[0].gini_coefficient if snapshots2 else 0
|
||||
final_gini = snapshots2[-1].gini_coefficient
|
||||
print(f" Initial Gini: {initial_gini:.3f}, Final Gini: {final_gini:.3f}")
|
||||
print(f" Inequality change: {final_gini - initial_gini:+.3f}")
|
||||
|
||||
print("✅ Inequality principle demonstrated")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("🚀 Markov Economics - Core Functionality Test")
|
||||
print("=" * 50)
|
||||
|
||||
try:
|
||||
test_markov_chains()
|
||||
test_economic_agent()
|
||||
test_simulation()
|
||||
test_inequality_principle()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("🎉 All tests passed! The application is working correctly.")
|
||||
print("\nThe web interface is available at: http://127.0.0.1:5000")
|
||||
print("The simulation demonstrates Marx's M-C-M' model and Piketty's r > g principle.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Test failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
Reference in New Issue
Block a user