Complete Python implementation of Calvano et al. (2020) "Artificial Intelligence, Algorithmic Pricing, and Collusion" with modern package structure and parallel execution support.
# Clone the repository
git clone https://github.com/Yusei406/calvano-thesis.git
cd calvano-thesis
# Install dependencies
pip install -r requirements.txtSingle training run:
python -m myproject.train --episodes 1000 --verboseParallel experiment:
python -m myproject.scripts.table_a2_parallel --episodes 50000 --n-seeds 10 --n-sessions 4Programmatic usage:
import myproject
agents, env, history = myproject.train_agents(n_episodes=1000)
print(f"Final profit: {history['training_summary']['individual_profit']:.4f}")myproject/
βββ __init__.py # Package initialization
βββ env.py # Demand environment
βββ agent.py # Q-learning agent
βββ grid.py # Price grid generation
βββ train.py # Training module
βββ scripts/
βββ __init__.py
βββ table_a2_parallel.py # Parallel execution script
- 25,000 iterations per episode (Table A.2 requirement)
- Ξ² = 4Γ10β»βΆ epsilon decay parameter
- 15-point price grid with ΞΎ=0.1 extension
- Memory length k=1 for state encoding
- Dynamic grid based on Nash/Cooperative equilibria
- Relative imports maintained throughout
- Module execution support (
python -m myproject.train) - Package import support (
import myproject) - No path manipulation required
- Multi-session per seed support
- ProcessPoolExecutor for CPU utilization
- Progress tracking and error handling
- JSON result export with timestamps
- Parameter bounds checking
- Type validation for all inputs
- Meaningful error messages
- Automatic Ξ²_effective calculation
- Convergence tracking with epsilon values
- Training history with beta information
- Individual profit: 0.18 Β± 0.03
- Joint profit: 0.26 Β± 0.04
- Required: 50,000 episodes Γ 10 seeds
Full replication (3+ hours):
python -m myproject.scripts.table_a2_parallel \
--episodes 50000 \
--n-seeds 10 \
--n-sessions 4 \
--max-workers 4Quick test (5 minutes):
python -m myproject.scripts.table_a2_parallel \
--episodes 1000 \
--n-seeds 2 \
--n-sessions 2- aβ = 0 (demand intercept)
- aα΅’ = 2 (product quality)
- ΞΌ = 0.25 (demand slope)
- c = 1 (marginal cost)
- Ξ± = 0.15 (learning rate)
- Ξ³ = 0.95 (discount factor)
- Ξ² = 4Γ10β»βΆ (epsilon decay)
- k = 1 (memory length)
{
'episodes': [...],
'individual_profits': [...],
'joint_profits': [...],
'epsilon_values': [...],
'total_iterations': 1250000,
'beta_info': {
'beta_raw': 4e-6,
'iterations_per_episode': 25000,
'beta_effective': 1.6e-10,
'epsilon_at_convergence': 0.6065
},
'training_summary': {
'final_individual_profit': 0.2710,
'final_joint_profit': 0.5590,
'nash_ratio_individual': 1.216,
'final_epsilon': 0.6065
}
}{
"experiment_info": {
"n_seeds": 10,
"n_sessions_per_seed": 4,
"total_sessions": 40,
"successful_sessions": 40,
"execution_time_seconds": 10800
},
"aggregated_stats": {
"individual_profit_mean": 0.1800,
"individual_profit_std": 0.0300,
"joint_profit_mean": 0.2600,
"joint_profit_std": 0.0400
}
}# All tests
python -m pytest tests/
# Specific test
python -m pytest tests/test_profit_targets.py::test_beta_normalization -v- Beta normalization verification
- Environment parameters validation
- Profit targets for different episode counts
- Input validation testing
# Build image
docker build -t calvano-replication .
# Run experiment
docker run -v $(pwd)/results:/app/results calvano-replication \
python -m myproject.scripts.table_a2_parallel --episodes 1000- Calvano, E., Calzolari, G., DenicolΓ², V., & Pastorello, S. (2020). Artificial Intelligence, Algorithmic Pricing, and Collusion. American Economic Review, 110(10), 3267-3297.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License - see LICENSE file for details.
- Original Fortran implementation by Calvano et al. (2020)
- Python adaptation with modern best practices
- Parallel execution optimization for multi-core systems
Status: β Complete implementation with all Calvano specifications Last Updated: December 2024 Python Version: 3.8+