Skip to content

Yusei406/calvano-thesis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Calvano et al. (2020) Q-Learning Replication

Complete Python implementation of Calvano et al. (2020) "Artificial Intelligence, Algorithmic Pricing, and Collusion" with modern package structure and parallel execution support.

πŸš€ Quick Start

Installation

# Clone the repository
git clone https://github.com/Yusei406/calvano-thesis.git
cd calvano-thesis

# Install dependencies
pip install -r requirements.txt

Basic Usage

Single training run:

python -m myproject.train --episodes 1000 --verbose

Parallel experiment:

python -m myproject.scripts.table_a2_parallel --episodes 50000 --n-seeds 10 --n-sessions 4

Programmatic usage:

import myproject
agents, env, history = myproject.train_agents(n_episodes=1000)
print(f"Final profit: {history['training_summary']['individual_profit']:.4f}")

πŸ“ Project Structure

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

🎯 Key Features

βœ… Complete Calvano Specification

  • 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

βœ… Modern Package Structure

  • Relative imports maintained throughout
  • Module execution support (python -m myproject.train)
  • Package import support (import myproject)
  • No path manipulation required

βœ… Parallel Execution

  • Multi-session per seed support
  • ProcessPoolExecutor for CPU utilization
  • Progress tracking and error handling
  • JSON result export with timestamps

βœ… Input Validation

  • Parameter bounds checking
  • Type validation for all inputs
  • Meaningful error messages

βœ… Beta Normalization

  • Automatic Ξ²_effective calculation
  • Convergence tracking with epsilon values
  • Training history with beta information

πŸ“Š Table A.2 Replication

Target Results

  • Individual profit: 0.18 Β± 0.03
  • Joint profit: 0.26 Β± 0.04
  • Required: 50,000 episodes Γ— 10 seeds

Execution Commands

Full replication (3+ hours):

python -m myproject.scripts.table_a2_parallel \
  --episodes 50000 \
  --n-seeds 10 \
  --n-sessions 4 \
  --max-workers 4

Quick test (5 minutes):

python -m myproject.scripts.table_a2_parallel \
  --episodes 1000 \
  --n-seeds 2 \
  --n-sessions 2

πŸ”§ Configuration

Environment Parameters (Calvano et al. 2020)

  • aβ‚€ = 0 (demand intercept)
  • aα΅’ = 2 (product quality)
  • ΞΌ = 0.25 (demand slope)
  • c = 1 (marginal cost)

Q-Learning Parameters

  • Ξ± = 0.15 (learning rate)
  • Ξ³ = 0.95 (discount factor)
  • Ξ² = 4Γ—10⁻⁢ (epsilon decay)
  • k = 1 (memory length)

πŸ“ˆ Results Format

Training History

{
    '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
    }
}

Parallel Experiment Results

{
    "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
    }
}

πŸ§ͺ Testing

Run Tests

# All tests
python -m pytest tests/

# Specific test
python -m pytest tests/test_profit_targets.py::test_beta_normalization -v

Test Coverage

  • Beta normalization verification
  • Environment parameters validation
  • Profit targets for different episode counts
  • Input validation testing

🐳 Docker Support

# 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

πŸ“š References

  • Calvano, E., Calzolari, G., DenicolΓ², V., & Pastorello, S. (2020). Artificial Intelligence, Algorithmic Pricing, and Collusion. American Economic Review, 110(10), 3267-3297.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

πŸ“„ License

MIT License - see LICENSE file for details.

πŸ™ Acknowledgments

  • 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+

About

Calvano et al. (2020) Q-learning implementation for undergraduate thesis Visibility: Public

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages