## Prediction Market Probabilities

Suppose you want to predict which of three soccer teams will win a tournament. In a prediction market, you get a bunch of experts and let them buy and sell shares of each team. The price of a team will go up as shares of the team are bought, and do down as shares are sold.

The number of outstanding shares can be mapped to the probabilities that each team will win, giving you your prediction. If x, y, and z are the number of outstanding shares of each team, then the probability that team X wins is:

And similarly for teams Y and Z. Technically, these values aren’t probabilities, they’re “marginal prices”, but they can be interpreted as probabilities. The b in the equation is called the liquidity factor and it’s also used in a different equation that determines the price of a share of a team.

It turns out that the equation is something called the softmax function. However, calculating the probabilities directly is a bad idea because exp(v) can be huge and overflow if v is large. So the solution is to use something called the max trick which I’ve explained in an earlier post.

Here’s a demo program written in C#:

```using System;
namespace MarketProbs
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\nBegin prediction market probs demo \n");

int[] shares = new int[] { 100, 141, 192 };
Console.WriteLine("Current shares: ");
for (int i = 0; i < 3; ++i)
Console.Write(shares[i] + " ");

double b = 100.0; // liquidity
Console.WriteLine("\n\nLiquidity: ");
Console.WriteLine(b.ToString("F1"));

Console.WriteLine("\nCalculating probs from shares\n");

double[] probs = Probs(shares, b);
Console.WriteLine("Probabilities: ");
for (int i = 0; i < 3; ++i)
Console.Write(probs[i].ToString("F4") + " ");
Console.WriteLine("\n\n");

Console.WriteLine("\nEnd demo\n");
} // Main

static double[] Probs(int[] shares, double b)
{
int n = shares.Length;
double[] vals = new double[n];
for (int i = 0; i < n; ++i)
vals[i] = shares[i] / b; // x/b etc.
return Softmax(vals);
}

static double[] Softmax(double[] vals)
{
// safe computation
int n = vals.Length;
double max = vals[0];
for (int i = 1; i  max) max = vals[i];
for (int i = 0; i < n; ++i)