Implementation:Pyro ppl Pyro RSA Schelling
| Property | Value |
|---|---|
| Implementation Type | Pattern Doc |
| Source File | examples/rsa/schelling.py
|
| Module | examples.rsa |
| Pyro Features | pyro.sample, poutine.block, Search, HashingMarginal, recursive reasoning, Bernoulli distribution
|
| Reference | Adapted from http://forestdb.org/models/schelling.html |
Overview
This file implements the Schelling coordination game using Pyro's probabilistic programming primitives. Two agents (Alice and Bob) must independently choose between two locations to meet, without communicating. They coordinate by recursively reasoning about each other's choices.
The recursive reasoning structure:
- Bob at depth 0: Chooses based on prior preference alone
- Bob at depth > 0: Reasons about Alice's choice, who reasons about Bob at depth-1
- Alice: Always reasons about Bob at depth-1
Each agent has a shared prior preference (a weighted coin flip) for one location over the other. The recursive reasoning amplifies this preference: with a 60% prior preference and recursion depth 2, Bob's probability of choosing the preferred location increases significantly beyond 60%.
The key Pyro pattern demonstrated is using poutine.block() to prevent the inner model's sample sites from being visible to the outer model during nested inference.
Code Reference
def alice(preference, depth):
alice_prior = location(preference)
with poutine.block():
bob_marginal = HashingMarginal(Search(bob).run(preference, depth - 1))
return pyro.sample("bob_choice", bob_marginal, obs=alice_prior)
def bob(preference, depth):
bob_prior = location(preference)
if depth > 0:
with poutine.block():
alice_marginal = HashingMarginal(Search(alice).run(preference, depth))
return pyro.sample("alice_choice", alice_marginal, obs=bob_prior)
else:
return bob_prior
def main(args):
shared_preference = torch.tensor([args.preference])
bob_decision = HashingMarginal(Search(bob).run(shared_preference, args.depth))
I/O Contract
| Parameter | Type | Description |
|---|---|---|
--preference |
float |
Shared prior preference for location 1 (default: 0.6) |
--depth |
int |
Recursion depth for reasoning (default: 2) |
-n / --num-samples |
int |
Monte Carlo samples for estimation (default: 10) |
Output:
- Exact probability of Bob choosing the preferred location
- Empirical frequency estimate from sampling
Usage Examples
# Default: preference=0.6, depth=2
# python schelling.py
# Stronger preference with deeper reasoning
# python schelling.py --preference 0.7 --depth 4
# More samples for better empirical estimate
# python schelling.py -n 100
Related Pages
- Pyro_ppl_Pyro_SearchInference - Search inference utilities used by this model
- Pyro_ppl_Pyro_RSA_Generics - RSA model for generic statements
- Pyro_ppl_Pyro_RSA_Hyperbole - RSA model for hyperbole