A simulation toolkit for Advanced Microeconomics (PPHA 44200), covering mechanism design, industrial organization, and contract theory. Provides analytical solutions, numerical equilibrium solvers, and Monte-Carlo simulations.
pip install -e .Dependencies: numpy >= 1.24, scipy >= 1.10, matplotlib >= 3.7.
import micro2
# Verify the Revenue Equivalence Theorem
result = micro2.auctions.compare_auction_formats(N=3, n_sim=100_000)
print(result)
# {'fpsb_mean_revenue': 0.4997, 'spsb_mean_revenue': 0.5002,
# 'theoretical_revenue': 0.5, 'revenue_equivalence_holds': True, ...}
# Cournot duopoly equilibrium (linear demand P=1−Q, MC=0)
result = micro2.oligopoly.cournot_symmetric_closed_form(a=1, c=0, n=2)
print(result)
# {'quantity_each': 0.333, 'price': 0.333, 'profit_each': 0.111, ...}
# Moral hazard: risk-incentive tradeoff
result = micro2.moral_hazard.risk_incentive_tradeoff(
sigma2_values=[0.1, 1.0, 5.0, 10.0], gamma=1.0
)
for r in result["tradeoff"]:
print(f"σ²={r['sigma2']:.1f} β*={r['optimal_beta']:.3f} effort={r['effort']:.3f}")| Module | Lecture | Key Functions |
|---|---|---|
welfare |
L01–02 | condorcet_cycle — Condorcet cycle detection |
plurality_rule / borda_count — Voting rules |
||
arrow_impossibility_demo — Monte-Carlo demonstration of Arrow's theorem |
||
surplus_maximizing_swf — Surplus-maximizing SWF under quasilinear utility |
||
pareto_efficiency_check — Pareto efficiency test |
||
pareto_improving_transfer — Budget-balanced Pareto-improving transfers |
||
auctions |
L03 | first_price_sealed_bid — FPSB auction (BNE bidding) |
second_price_sealed_bid — SPSB (Vickrey) auction |
||
english_auction — English ascending auction |
||
fpsb_equilibrium_bid — FPSB BNE bid function for general distributions |
||
simulate_fpsb / simulate_spsb — Monte-Carlo auction simulation |
||
compare_auction_formats — Revenue Equivalence Theorem verification |
||
vcg |
L04 | vcg_single_good — Single-good VCG mechanism (= Vickrey auction) |
vcg_with_externalities — VCG with allocation externalities |
||
espresso_machine_game — Public goods VCG (espresso machine game) |
||
split_cost_mechanism — Split-the-cost mechanism (non-IC benchmark) |
||
vcg_budget_balance_sim — VCG budget imbalance frequency simulation |
||
second_best |
L05 | seller_posted_price — Seller-posted-price mechanism |
fixed_price_mechanism — Fixed-price second-best mechanism |
||
optimal_fixed_price — Surplus-maximizing fixed price |
||
myerson_satterthwaite_demo — Myerson-Satterthwaite theorem demonstration |
||
public_good_second_best — Public goods second-best mechanism simulation |
||
large_mechanisms |
L06 | double_auction — VCG-style double auction |
double_auction_convergence — Efficiency convergence as N grows |
||
public_good_large_n — Large-N public goods provision (Mailath-Postlewaite) |
||
optimal_auctions |
L07 | virtual_value — Virtual value function Ω(θ) |
optimal_reserve_price — Revenue-maximizing reserve price r = Ω⁻¹(c) |
||
myerson_optimal_auction — Myerson's optimal auction |
||
revenue_comparison — Efficient vs optimal auction comparison |
| Module | Lecture | Key Functions |
|---|---|---|
monopoly |
L08 | linear_monopoly — Linear-price monopoly with general inverse demand |
monopoly_uniform — Monopoly with uniform WTP distribution (closed-form) |
||
monopoly_constant_elasticity — Constant-elasticity demand monopoly (closed-form) |
||
lerner_index — Lerner index |
||
multiproduct_monopoly — Multiproduct monopoly with demand spillovers |
||
price_discrimination |
L09–10 | first_degree_pd — Perfect (first-degree) price discrimination |
two_part_tariff — Two-part tariff (p = MC, fee = CS) |
||
third_degree_pd — Third-degree PD (multimarket) |
||
second_degree_pd — Second-degree PD (nonlinear pricing / quantity discounts) |
||
airline_screening — Airline quality-discount screening |
||
toilet_paper_example — Hermalin toilet paper quantity discount example |
||
oligopoly |
L11 | cournot_duopoly — Cournot duopoly (numerical solver) |
cournot_n_firms — N-firm Cournot competition |
||
cournot_symmetric_closed_form — Symmetric Cournot (closed-form) |
||
bertrand_homogeneous — Bertrand competition with homogeneous goods |
||
bertrand_capacity_constraints — Bertrand with capacity constraints |
||
kreps_scheinkman — Kreps-Scheinkman two-stage game |
||
cournot_bertrand_comparison — Cournot vs Bertrand comparison |
||
hotelling |
L12 | hotelling_fixed_locations — Fixed-location Hotelling (endpoint equilibrium) |
hotelling_general_locations — Hotelling with general firm locations |
||
hotelling_two_stage — Two-stage Hotelling (location + price) |
||
minimum_differentiation_demo — Minimum differentiation (Hotelling 1929) |
||
maximum_differentiation_demo — Maximum differentiation (d'Aspremont 1979) |
| Module | Lecture | Key Functions |
|---|---|---|
adverse_selection |
L13 | labor_market_selection — Labor market adverse selection equilibrium |
unraveling_demo — Market unraveling demonstration |
||
akerlof_lemons — Akerlof (1970) lemons model |
||
insurance_adverse_selection — Insurance market adverse selection |
||
mandate_policy / risk_rating_policy — Policy intervention analysis |
||
signaling |
L14 | spence_signaling — Spence signaling model (full analysis) |
separating_equilibrium — Least-cost separating equilibrium (Riley outcome) |
||
pooling_equilibrium — Pooling equilibrium |
||
signaling_welfare_comparison — Welfare comparison across equilibrium types |
||
screening |
L15 | job_market_screening — Job market screening |
rothschild_stiglitz — Rothschild-Stiglitz insurance screening model |
||
no_pooling_proof — Non-existence of pooling equilibrium demonstration |
||
moral_hazard |
L17 | moral_hazard_linear — Moral hazard with linear contracts (CARA + Normal) |
first_best_effort — First-best effort level |
||
risk_incentive_tradeoff — Risk-incentive tradeoff comparative statics |
||
common_agency — Common agency model (Bernheim-Whinston) |
||
moral_hazard_welfare — Welfare decomposition across β values |
- Analytical + Numerical: Each model provides closed-form solutions where available, with numerical solvers for general functional forms.
- Structured Output: All functions return
dataclassordictobjects containing equilibrium values, welfare metrics, efficiency ratios, and more. - Reproducibility: All Monte-Carlo simulations accept a
seedparameter. - Zero Configuration: Most functions ship with sensible defaults matching standard textbook examples — call with no arguments to see canonical results.
# Frequency of Condorcet cycles under random preference profiles
result = micro2.welfare.arrow_impossibility_demo(
n_alternatives=3, n_voters=5, n_sim=100_000
)
print(f"Condorcet cycle frequency: {result['cycle_frequency']:.1%}")# Bilateral trade: no IC + IR + budget-balanced mechanism achieves full efficiency
result = micro2.second_best.myerson_satterthwaite_demo(n_sim=100_000)
print(f"First-best surplus: {result['efficient_surplus']:.4f}")
print(f"Fixed-price surplus: {result['fixed_price_0.5_surplus']:.4f}")
print(f"Efficiency loss: {1 - result['fixed_price_0.5_efficiency']:.1%}")# In the two-stage game, firms choose endpoint locations
result = micro2.hotelling.hotelling_two_stage(s_bar=10, t=1, c=0)
print(f"Equilibrium locations: {result['equilibrium_locations']}")
print(f"Equilibrium prices: {result['equilibrium_prices']}")# Compare welfare under separating, pooling, and full-information outcomes
result = micro2.signaling.signaling_welfare_comparison(
theta_H=2.0, theta_L=1.0, lambda_=0.5
)
print(f"Full-information welfare: {result['full_info_welfare']:.3f}")
print(f"Separating welfare: {result['separating_welfare']:.3f} (signaling cost={result['signaling_cost']:.3f})")
print(f"Pooling welfare: {result['pooling_welfare']:.3f}")All functions accept user-defined parameters — you are not locked into default values.
micro2.oligopoly.cournot_symmetric_closed_form(a=10, c=2, n=4)
micro2.moral_hazard.moral_hazard_linear(sigma2=5.0, gamma=2.0, u_bar=0.1)
micro2.hotelling.hotelling_fixed_locations(s_bar=20, t=3, c=1)Many modules accept callable arguments to replace default functional forms:
# Custom inverse demand and marginal cost
micro2.monopoly.linear_monopoly(
inverse_demand=lambda q: 100 - 2*q,
marginal_cost=lambda q: 10 + 0.5*q,
)
# Custom signaling cost function
micro2.signaling.spence_signaling(
theta_H=3, theta_L=1,
cost_func=lambda e, theta: e**2 / (2*theta),
)
# Custom buyer/seller valuations in the lemons model
micro2.adverse_selection.akerlof_lemons(
seller_value_func=lambda t: t,
buyer_value_func=lambda t: t + 0.3,
)import numpy as np
micro2.vcg.vcg_single_good(np.array([15, 12, 8, 5, 3]))
micro2.oligopoly.cournot_n_firms(
inverse_demand=lambda Q: 10 - Q,
marginal_costs=[1.0, 2.0, 3.0, 4.0],
)All Monte-Carlo functions support n_sim (number of draws) and seed (for reproducibility):
micro2.auctions.simulate_fpsb(N=3, n_sim=1_000_000, seed=123)MIT