Chapter 8 Exercises: Automated Market Makers

These exercises cover all major topics from Chapter 8. Work through them in order within each part --- later exercises build on earlier ones.


Part A: AMM Mechanics and Concepts (Exercises 1--6)

Exercise 1: AMM vs. Order Book Trade-offs

A university wants to run prediction markets for 200 different questions about campus events (sports outcomes, enrollment numbers, construction deadlines, etc.). Each question is expected to attract between 5 and 50 traders over its lifetime.

(a) Should the university use an AMM or a central limit order book? Justify your answer with at least three specific reasons.

(b) If the university has a total budget of $5,000 for market subsidies across all 200 markets, what is the maximum per-market subsidy? How does this constrain the choice of AMM parameters?

(c) Describe a scenario where the university might want to switch from an AMM to an order book for a specific market. What conditions would trigger this switch?


Exercise 2: Understanding the Cost Function

Consider an LMSR market with two outcomes (Yes/No) and liquidity parameter $b = 50$.

(a) Calculate $C(0, 0)$, the initial value of the cost function.

(b) Calculate $C(10, 0)$, the cost function value after 10 "Yes" shares have been purchased (and 0 "No" shares).

(c) What is the total money collected from traders to move from state $(0, 0)$ to state $(10, 0)$?

(d) Now calculate $C(10, 5)$. What is the additional money collected from the trade that moved the state from $(10, 0)$ to $(10, 5)$?

(e) Verify path independence: calculate the total cost to go from $(0, 0)$ to $(10, 5)$ by first buying 5 "No" shares then 10 "Yes" shares. Compare to the total from parts (c) and (d).


Exercise 3: Price Calculation

Using the same market from Exercise 2 ($b = 50$, two outcomes):

(a) Calculate the price of "Yes" when $q_{\text{Yes}} = 0, q_{\text{No}} = 0$.

(b) Calculate the price of "Yes" when $q_{\text{Yes}} = 25, q_{\text{No}} = 0$.

(c) Calculate the price of "Yes" when $q_{\text{Yes}} = 25, q_{\text{No}} = 25$.

(d) Explain why the answer to (c) equals the answer to (a). Which cost function property does this demonstrate?

(e) At what value of $q_{\text{Yes}}$ (with $q_{\text{No}} = 0$) does the price of "Yes" reach exactly 0.90? Show your work.


Exercise 4: CPMM Basics

A CPMM is initialized with reserves $x = 200$ (Yes shares) and $y = 200$ (No shares).

(a) What is the constant product $k$?

(b) What are the initial prices for Yes and No?

(c) A trader wants to buy 20 "Yes" shares. Calculate the cost in "No" share equivalents.

(d) What are the new reserves and new prices after this trade?

(e) Calculate the slippage for this trade (difference between actual cost per share and the initial price).

(f) What is the maximum number of "Yes" shares a trader could possibly buy from this pool? What happens to the price of "Yes" as you approach this limit?


Exercise 5: LMSR vs. CPMM Side-by-Side

Set up two markets for the same question: - Market A: LMSR with $b = 100$, starting at 50/50 - Market B: CPMM with reserves $(100, 100)$, starting at 50/50

(a) In each market, calculate the cost of buying 5 "Yes" shares.

(b) In each market, calculate the new price of "Yes" after buying 5 shares.

(c) In each market, calculate the cost of buying 50 "Yes" shares (starting from the initial 50/50 state).

(d) Which market shows more slippage for the 50-share trade? Why?

(e) Now suppose you need the price to reach 95%. How many shares must you buy in each market? Which is more expensive?


Exercise 6: The Bounded Loss Property

(a) For an LMSR market with $b = 200$ and 3 outcomes, calculate the maximum possible loss.

(b) Construct a specific trading scenario that achieves (or comes close to) this maximum loss. Specify the trades and the winning outcome.

(c) Now construct a trading scenario where the AMM actually profits. (Hint: think about traders who bet on the wrong outcome.)

(d) Prove that a CPMM does not have a bounded loss property by constructing a scenario where the loss exceeds any pre-specified amount.


Part B: Cost Function Properties (Exercises 7--12)

Exercise 7: Verifying Convexity

For LMSR with $b = 100$ and two outcomes:

(a) Calculate the cost of buying shares 1 through 10 of "Yes" (i.e., the first 10 shares). Then calculate the cost of buying shares 91 through 100 (from a state where 90 have already been purchased). Compare and explain.

(b) Plot (or calculate for 5+ points) the marginal cost of the $n$-th "Yes" share as a function of $n$ (with $q_{\text{No}} = 0$ throughout). Verify that this is an increasing function.

(c) Explain intuitively why convexity is important for a prediction market. What would go wrong if the cost function were concave?


Exercise 8: Path Independence Proof

(a) Starting from state $(0, 0)$ in a two-outcome LMSR with $b = 100$, compute the total cost to reach state $(30, 20)$ using these two paths: - Path 1: Buy 30 "Yes" first, then 20 "No" - Path 2: Buy 20 "No" first, then 30 "Yes"

(b) Prove algebraically (not just numerically) that LMSR is path-independent. (Hint: show that the total cost depends only on the final state, not the path.)

(c) Is a CPMM path-independent? Demonstrate with a specific example.


Exercise 9: Translational Invariance

For LMSR with $b = 100$:

(a) Calculate prices at state $(10, 30)$.

(b) Calculate prices at state $(110, 130)$.

(c) Calculate prices at state $(-90, -70)$.

(d) Verify that all three are the same. Explain why this property matters for prediction markets.

(e) Is the cost function itself translationally invariant (i.e., does $C(q_1 + c, q_2 + c) = C(q_1, q_2)$)? If not, what is invariant?


Exercise 10: No-Arbitrage Condition

(a) In a three-outcome LMSR market with $b = 100$ at state $(50, 20, 10)$, calculate all three prices and verify they sum to 1.

(b) What does it cost to buy one share of every outcome simultaneously? Calculate this for the state in part (a).

(c) Explain why the answer to (b) must equal $1.00 for any valid AMM. What arbitrage opportunity would exist if it were different?

(d) If a buggy AMM implementation produces prices that sum to 1.05, describe the exact arbitrage strategy a trader could exploit.


Exercise 11: Designing a New Cost Function

Suppose someone proposes a "quadratic" cost function:

$$C(q_1, q_2) = \frac{q_1^2 + q_2^2}{2b}$$

(a) Derive the price functions from this cost function (take partial derivatives).

(b) Do the prices always sum to 1? Under what conditions?

(c) Is this a valid cost function for a prediction market? Identify which required properties it satisfies and which it violates.

(d) Could you modify this function to fix the violations? Propose a modification and check its properties.


Exercise 12: Comparing Cost Functions

For each of the following proposed cost functions, determine whether it is valid for a two-outcome prediction market. For each, check: (1) convexity, (2) prices sum to 1, (3) prices always positive.

(a) $C(q_1, q_2) = \sqrt{q_1^2 + q_2^2}$ (for $q_1, q_2 > 0$)

(b) $C(q_1, q_2) = \max(q_1, q_2)$

(c) $C(q_1, q_2) = q_1 + q_2 + b \cdot \ln(e^{q_1/b} + e^{q_2/b})$ (Hint: this is a shifted LMSR)

(d) $C(q_1, q_2) = b \cdot (e^{q_1/b} + e^{q_2/b})$


Part C: Implementing AMMs (Exercises 13--18)

Exercise 13: Build an LMSR from Scratch

Without looking at the code in the chapter, implement an LMSR class from the mathematical definitions alone. Your implementation must:

(a) Handle any number of outcomes $n \geq 2$.

(b) Use the log-sum-exp trick for numerical stability (look up this technique if needed).

(c) Include methods: cost(), prices(), trade_cost(outcome, quantity), execute_trade(outcome, quantity), and max_loss().

(d) Write test cases that verify: prices sum to 1, translational invariance, path independence, and bounded loss.

(e) Test your implementation against the worked examples in Section 8.2 (Alice buying 10 "Yes" shares, Bob buying 20 "No" shares).


Exercise 14: Build a CPMM from Scratch

Implement a CPMM class for two-outcome prediction markets.

(a) Include methods for: prices(), cost_to_buy(outcome, quantity), execute_trade(outcome, quantity), slippage(outcome, quantity).

(b) Handle the edge case where a trade would exhaust the reserves. What should happen?

(c) Add a method add_liquidity(amount) that adds equal reserves of both outcomes, keeping the price unchanged but increasing $k$.

(d) Add a method remove_liquidity(amount) that removes equal reserves, checking that reserves do not go below a minimum.

(e) Test with the worked example from Section 8.3.


Exercise 15: Build an LS-LMSR

Implement the Liquidity-Sensitive LMSR.

(a) Start with your LMSR implementation from Exercise 13 and modify $b$ to depend on the total quantity of shares.

(b) Choose the formula $b(\mathbf{q}) = \max(\alpha \cdot \sum_i \max(q_i, 0), b_{\text{min}})$.

(c) Demonstrate that early trades (when total volume is low) move the price more than later trades of the same size.

(d) Compare the price paths of standard LMSR and LS-LMSR for a sequence of 20 identical trades (each buying 5 "Yes" shares).


Exercise 16: Trade Simulator

Build a trade simulator that:

(a) Accepts a market (LMSR, CPMM, or LS-LMSR) and a sequence of trades.

(b) Executes each trade and records: trader name, outcome, quantity, cost, effective price, pre-trade price, post-trade price, slippage.

(c) After all trades, reports: total money collected, maximum loss, price history, and a breakdown of each trader's portfolio value at current prices.

(d) Generates a resolution report: given a winning outcome, calculate each trader's profit/loss and the AMM's profit/loss.

(e) Test with a sequence of at least 10 trades by 3 different "traders" with different strategies.


Exercise 17: Visualization Dashboard

Create a Python script that produces visualizations for an AMM market:

(a) Price history over time (line chart with one line per outcome).

(b) Cost per share as a function of shares purchased (showing convexity).

(c) Slippage as a function of trade size.

(d) Comparison of price impact across different $b$ values.

(e) Use matplotlib or any plotting library. Include labels, titles, and legends.


Exercise 18: Property Verification Suite

Write a comprehensive test suite that takes any cost-function-based AMM and verifies:

(a) Prices always sum to 1 (test with 1000 random states).

(b) Prices are always positive (test with 1000 random states).

(c) Path independence holds (test 100 pairs of different paths to the same state).

(d) Translational invariance of prices (test with 100 random translations).

(e) Convexity (marginal cost is non-decreasing for 100 consecutive unit purchases).

(f) Report which properties pass and which fail, with specific counterexamples for failures.


Part D: Analyzing AMM Behavior (Exercises 19--24)

Exercise 19: Optimal $b$ Selection

A company wants to run an internal prediction market for quarterly revenue forecasts. They expect: - About 50 employees will participate - Each employee will make 2-5 trades on average - The market has 5 outcomes (revenue ranges) - Budget for market subsidy: $500

(a) Calculate the range of acceptable $b$ values given the budget constraint.

(b) Estimate the expected total trading volume (in shares).

(c) Using the rule of thumb $b \approx \text{expected volume} / 10$, what $b$ would you recommend?

(d) Verify that your recommended $b$ is within the budget constraint.

(e) Simulate the market with your chosen $b$ using a plausible trading sequence. Does the price trajectory look reasonable?


Exercise 20: Slippage Analysis

Using an LMSR market with $b = 100$ and two outcomes starting at 50/50:

(a) Calculate the slippage for buying 1, 5, 10, 25, 50, and 100 "Yes" shares. Present in a table.

(b) Express slippage as a percentage of the ideal cost (at the quoted price). How does it grow?

(c) A trader wants to move the price from 50% to 75%. Calculate the total cost and total slippage.

(d) The same trader could split their trade into 10 smaller trades. Does this reduce total slippage? Why or why not?

(e) Compare slippage at the same trade sizes for $b = 50$ and $b = 200$. What pattern do you observe?


Exercise 21: Market Maker P&L Scenarios

An LMSR market ($b = 100$, two outcomes) runs for one month. The final state is $q_{\text{Yes}} = 80, q_{\text{No}} = 30$.

(a) Calculate the total money collected by the AMM.

(b) Calculate the AMM's profit/loss if "Yes" wins.

(c) Calculate the AMM's profit/loss if "No" wins.

(d) Is there any outcome where the AMM profits?

(e) What would the final state need to look like for the AMM to break even regardless of the outcome?


Exercise 22: Multi-Outcome Market Analysis

A three-outcome LMSR market ($b = 150$) tracks an election with candidates A, B, and C. After trading:

  • $q_A = 100, q_B = 60, q_C = 20$

(a) Calculate all three prices.

(b) Verify prices sum to 1.

(c) A trader believes C has a 30% chance of winning (the market disagrees). What trade should they make, and how many shares should they buy to move C's price to 30%?

(d) Calculate the cost of this trade.

(e) If C actually wins, what is the trader's profit? What is the AMM's overall P&L?


Exercise 23: LS-LMSR Behavior Under Different Trading Patterns

Compare LS-LMSR ($\alpha = 1$, $b_{\text{min}} = 10$) under three trading patterns:

  • Pattern 1: 100 trades of 1 share each, all "Yes"
  • Pattern 2: 10 trades of 10 shares each, all "Yes"
  • Pattern 3: 50 trades of 2 shares each, alternating "Yes" and "No"

(a) For each pattern, track $b$ after every 10 trades.

(b) For each pattern, track the "Yes" price after every 10 trades.

(c) Which pattern causes $b$ to grow fastest? Why?

(d) Which pattern shows the most price movement? Why?

(e) Discuss how LS-LMSR handles these different trading patterns compared to standard LMSR.


Exercise 24: CPMM Liquidity Provider Analysis

A CPMM starts with reserves $(1000, 1000)$. After a sequence of trades, the reserves are $(500, 2000)$.

(a) Verify that $k$ is preserved.

(b) What is the current price of each outcome?

(c) If the market resolves with outcome A winning, how much does the pool lose? (Each A share is worth $1, each B share is worth $0.)

(d) If the market resolves with outcome B winning, how much does the pool lose?

(e) Compare the initial value of the pool ($2000 if we assume each share starts worth $0.50 each) to the final value under each resolution. This difference is called "impermanent loss" --- calculate it for both scenarios.


Part E: Advanced Problems (Exercises 25--30)

Exercise 25: Designing a Combinatorial AMM

Consider two binary events: "Will it rain tomorrow?" and "Will the outdoor concert be cancelled?" There are four combined outcomes: (Rain, Cancelled), (Rain, Not Cancelled), (No Rain, Cancelled), (No Rain, Not Cancelled).

(a) Set up a 4-outcome LMSR market with $b = 100$.

(b) A trader believes rain makes cancellation very likely. They want to buy shares of (Rain, Cancelled) and sell shares of (Rain, Not Cancelled). Execute these trades and show the resulting prices.

(c) Calculate the conditional probability of cancellation given rain from the current market prices (i.e., $P(\text{Cancelled} | \text{Rain})$).

(d) Discuss the challenges of scaling this approach to 10 binary events (which would have $2^{10} = 1024$ combined outcomes).


Exercise 26: AMM with Fees

Modify the LMSR implementation to include a proportional fee of $f = 2\%$ on each trade.

(a) Implement the modified LMSR where the trader pays $\text{cost} \times (1 + f)$ for buys and receives $\text{cost} \times (1 - f)$ for sells.

(b) Does path independence still hold with fees? Demonstrate with a specific example.

(c) Run a simulation of 100 trades with and without fees. How much fee revenue is collected?

(d) Calculate the break-even point: how much trading volume is needed for fee revenue to cover the maximum possible AMM loss?


Exercise 27: Reverse Engineering a Market

You observe the following trade data from an unknown AMM:

Trade # Outcome Shares Cost Price After
1 Yes 10 $5.13 0.525
2 Yes 10 $5.37 0.550
3 No 20 $9.47 0.500
4 Yes 50 $28.07 0.622

(a) What type of AMM is this? How can you tell?

(b) What is the liquidity parameter?

(c) Verify by recalculating each trade's cost and post-trade price.

(d) Predict the cost and post-trade price if the next trade is buying 30 "No" shares.


Exercise 28: Market Manipulation Analysis

An adversary wants to manipulate an LMSR market ($b = 100$, two outcomes) to make the price of "Yes" appear to be 90%.

(a) How many shares must the adversary buy, and at what cost?

(b) If another trader sees the inflated price and buys 10 "No" shares (believing Yes is overpriced), how much does the adversary lose on their position?

(c) Calculate the cost-per-percentage-point of manipulation at various starting prices (50%, 60%, 70%, 80%). What pattern do you observe?

(d) How does increasing $b$ affect the cost of manipulation? Calculate the manipulation cost for $b = 50, 100, 500, 1000$.

(e) Propose a defense mechanism against this type of manipulation.


Exercise 29: Hybrid AMM/Order Book Design

Design a hybrid system where an AMM provides baseline liquidity and an order book handles direct peer-to-peer trades.

(a) Describe the matching logic: when a new order arrives, should it first try the order book or the AMM? Why?

(b) The AMM quotes "Yes" at $0.60. A limit sell order sits on the book at $0.58. A buyer wants 10 shares. What should happen?

(c) Implement a simple hybrid in Python that maintains both an LMSR market and a sorted list of limit orders. The execute_trade method should route to whichever offers the better price.

(d) Test your hybrid with a realistic sequence of both limit orders and market orders.


Exercise 30: AMM Performance Benchmark

Create a comprehensive benchmark that compares LMSR, CPMM, and LS-LMSR across multiple criteria:

(a) Price accuracy: Generate 1000 trades from a "true probability" that drifts over time (e.g., starts at 0.5 and random-walks to 0.8). Measure how closely each AMM's price tracks the true probability.

(b) Operator cost: Calculate the total subsidy (loss) for the market operator under each mechanism.

(c) Trader fairness: Measure total slippage paid by all traders under each mechanism.

(d) Manipulation resistance: Calculate the cost for an adversary to move the price by 20 percentage points under each mechanism.

(e) Present your results in a comparison table and recommend which AMM to use for: (i) a small community market, (ii) a corporate forecasting platform, (iii) a high-volume public prediction market.