## Graphing a Prediction Market Cost Function

The goal of a prediction market is to predict an outcome such as the winner of a political election. In a prediction market, a group of experts buys and sells shares in the options in a way similar to the stock market.

A prediction market needs an underlying cost function. A common cost function is the logarithmic market scoring rule (LMSR). If x and y represent the number of shares of options X and Y, then the LMSR cost function is:

C = b * ln(exp(x/b) + exp(y/b))

where b is a scaling constant called the liquidity. I wondered what the cost function looked like so I decided to graph it using the R language. The R commands I used start with:

```x = seq(0, 50)
y = seq(0, 50)
f = function(x, y) { 10 * log(exp(x/10) +
exp(y/10)) }
z = outer(x, y, f)
```

I set up vectors x and y with 51 values from 0 to 50 inclusive. The next command defines the LMSR cost function for two inputs and a scaling factor of b = 10.

Next:

```jet.colors = colorRampPalette(c("midnightblue",
"blue", "cyan", "green", "yellow", "orange",
"red", "darkred"))

nbcol = 64
color = jet.colors(nbcol)

nrz = nrow(z)
ncz = ncol(z)

zfacet = z[-1,-1] + z[-1,-ncz] +
z[-nrz,-1] + z[-nrz,-ncz]
facetcol = cut(zfacet, nbcol)
```

I set up a custom color gradient from dark blue to dark red. You can think of the zfacet and facetcol as magic R incantations for doing a 3D color gradient graph.

The graph is created with:

```persp(x, y, z, col=color[facetcol], phi=15,
theta=-35, ticktype="detailed", d=10, r=1,