2016 年六月

# 測試回合 - 預測市場介紹

## 範例{\lang1033 }

``````Setting liquidity parameter = 100.0

Initial number of shares owned of teams [0] and [1] are:
0 0

Initial inferred probabilities of winning are:
0.5000  0.5000
``````

[圖 1] 預測市場示範

``````Current costs for one share of each team are:
\$0.5012  \$0.5012
``````

``````Update: expert [01] buys 20 shares of team [0]

Cost of transaction to expert was: \$10.50
``````

``````New number of shares owned of teams [0] and [1] are:
20 0

New inferred probabilities of winning are:
0.5498  0.4502
``````

``````Current costs for one share of each team are:
\$0.5511  \$0.4514

Update: expert [02] buys 20 shares of team [1]

Cost of transaction to expert was: \$9.50
``````

``````New number of shares owned of teams [0] and [1] are:
20 20

New inferred probabilities of winning are:
0.5000  0.5000
``````

``````Current costs for one share of each team are:
\$0.5012  \$0.5012

Update: expert [03] buys 60 shares of team [0]
Cost of transaction to expert was: \$34.43

New number of shares owned of teams [0] and [1] are:
80 20

New inferred probabilities of winning are:
0.6457  0.3543
``````

``````Current costs for one share of each team are:
\$0.6468  \$0.3555

Update: expert [01] sells 10 shares of team [0]
Cost of transaction to expert was: \$-6.34

New number of shares owned of teams [0] and [1] are:
70 20

New inferred probabilities of winning are:
0.6225  0.3775
``````

## 四個索引鍵的預測市場方程式

[圖 2] 的四個索引鍵的預測市場方程式

``````static double Cost(int[] outstanding, double liq)
{
double sum = 0.0;
for (int i = 0; i < 2; ++i)
sum += Math.Exp(outstanding[i] / liq);
return liq * Math.Log(sum);
}
``````

``````static double CostOfTrans(int[] outstanding, int idx, int nShares, double liq)
{
int[] after = new int[2];
Array.Copy(outstanding, after, 2);
after[idx] += nShares;
return Cost(after, liq) - Cost(outstanding, liq);
}
``````

``````static double[] CostForOneShare(int[] outstanding, double liq)
{
double[] result = new double[2];
result[0] = CostOfTrans(outstanding, 0, 1, liq);
result[1] = CostOfTrans(outstanding, 1, 1, liq);
return result;
}
``````

``````static double[] Probabilities(int[] outstanding, double liq)
{
double[] result = new double[2];
double denom = 0.0;
for (int i = 0; i < 2; ++i)
denom += Math.Exp(outstanding[i] / liq);
for (int i = 0; i < 2; ++i)
result[i] = Math.Exp(outstanding[i] / liq) / denom;
return result;
}
``````

## 示範程式

[圖 3 預測市場示範

``````using System;
namespace PredictionMarket
{
class PredictionMarketProgram
{
static void Main(string[] args)
{
Console.WriteLine("Begin prediction market demo ");
Console.WriteLine("Goal is to predict winner of Xrays");
Console.WriteLine("vs. Yanks using expert opinions");
double liq = 100.0;
Console.WriteLine("Setting liquidity parameter = " +
liq.ToString("F1"));
int[] outstanding = new int[] { 0, 0 };
Console.WriteLine("Initial number of shares owned are:");
ShowVector(outstanding);
double[] probs = Probabilities(outstanding, liq);
Console.WriteLine("Initial probabilities of winning:");
ShowVector(probs, 4, " ");
Console.WriteLine("=================================");
double[] costPerShare = CostForOneShare(outstanding, liq);
Console.WriteLine("Current costs for one share are: ");
ShowVector(costPerShare, 4, " \$");
Console.WriteLine("Update: expert [01] buys 20 shares " +
"of team [0]");
double costTrans = CostOfTrans(outstanding, 0, 20, liq);
Console.WriteLine("Cost of transaction to expert was: \$" +
costTrans.ToString("F2"));
outstanding = new int[] { 20, 0 };
Console.WriteLine("New number of shares owned are: ");
ShowVector(outstanding);
probs = Probabilities(outstanding, liq);
Console.WriteLine("New inferred probs of winning:");
ShowVector(probs, 4, " ");
Console.WriteLine("=================================");
costPerShare = CostForOneShare(outstanding, liq);
Console.WriteLine("Current costs for one share are:");
ShowVector(costPerShare, 4, " \$");
Console.WriteLine("Update: expert [02] buys 20 shares " +
"of team [1]");
costTrans = CostOfTrans(outstanding, 1, 20, liq);
Console.WriteLine("Cost of transaction to expert was: \$" +
costTrans.ToString("F2"));
outstanding = new int[] { 20, 20 };
Console.WriteLine("New number of shares owned are:");
ShowVector(outstanding);
probs = Probabilities(outstanding, liq);
Console.WriteLine("New inferred probs of winning:");
ShowVector(probs, 4, " ");
Console.WriteLine("=================================");
costPerShare = CostForOneShare(outstanding, liq);
Console.WriteLine("Current costs for one share are:");
ShowVector(costPerShare, 4, " \$");
Console.WriteLine("Update: expert [03] buys 60 shares " +
"of team [0]");
costTrans = CostOfTrans(outstanding, 0, 60, liq);
Console.WriteLine("Cost of transaction to expert was: \$" +
costTrans.ToString("F2"));
outstanding = new int[] { 80, 20 };
Console.WriteLine("New number of shares owned are:");
ShowVector(outstanding);
probs = Probabilities(outstanding, liq);
Console.WriteLine("New inferred probs of winning:");
ShowVector(probs, 4, " ");
Console.WriteLine("=================================");
costPerShare = CostForOneShare(outstanding, liq);
Console.WriteLine("Current costs for one share are: ");
ShowVector(costPerShare, 4, " \$");
Console.WriteLine("Update: expert [01] sells 10 shares " +
"of team [0]");
costTrans = CostOfTrans(outstanding, 0, -10, liq);
Console.WriteLine("Cost of transaction to expert was: \$" +
costTrans.ToString("F2"));
outstanding = new int[] { 70, 20 };
Console.WriteLine("New number of shares owned are:");
ShowVector(outstanding);
probs = Probabilities(outstanding, liq);
Console.WriteLine("New inferred probs of winning:");
ShowVector(probs, 4, " ");
Console.WriteLine("=================================");
Console.WriteLine("Update: Market Closed");
Console.WriteLine("\nEnd prediction market demo \n");
} // Main()
static double[]Probabilities(int[] outstanding,
double liq)
{
double[] result = new double[2];
double denom = 0.0;
for (int i = 0; i < 2; ++i)
denom += Math.Exp(outstanding[i] / liq);
for (int i = 0; i < 2; ++i)
result[i] = Math.Exp(outstanding[i] / liq) / denom;
return result;
}
static double Cost(int[] outstanding, double liq)
{
double sum = 0.0;
for (int i = 0; i < 2; ++i)
sum += Math.Exp(outstanding[i] / liq);
return liq * Math.Log(sum);
}
static double CostOfTrans(int[] outstanding, int idx,
int nShares, double liq)
{
int[] after = new int[2];
Array.Copy(outstanding, after, 2);
after[idx] += nShares;
return Cost(after, liq) - Cost(outstanding, liq);
}
static double[] CostForOneShare(int[] outstanding,
double liq)
{
double[] result = new double[2];
result[0] = CostOfTrans(outstanding, 0, 1, liq);
result[1] = CostOfTrans(outstanding, 1, 1, liq);
return result;
}
static void ShowVector(double[] vector, int dec, string pre)
{
for (int i = 0; i < vector.Length; ++i)
Console.Write(pre + vector[i].ToString("F" + dec) + " ");
Console.WriteLine("\n");
}
static void ShowVector(int[] vector)
{
for (int i = 0; i < vector.Length; ++i)
Console.Write(vector[i] + " ");
Console.WriteLine("\n");
}
} // Program class
} // ns
``````

``````double liq = 100.0;
int[] outstanding = new int[] { 0, 0 };
ShowVector(outstanding);
``````

``````double[] probs = Probabilities(outstanding, liq);
Console.WriteLine("Initial probabilities of winning:");
ShowVector(probs, 4, " ");
``````

``````double[] costPerShare = CostForOneShare(outstanding, liq);
Console.WriteLine("Current costs for one share are: ");
ShowVector(costPerShare, 4, " \$");
``````

``````Console.WriteLine("Update: expert [01] buys 20 shares of team [0]");
double costTrans = CostOfTrans(outstanding, 0, 20, liq);
Console.WriteLine("Cost of transaction to expert was: \$" +
costTrans.ToString("F2"));
``````

``````outstanding = new int[] { 20, 0 };
Console.WriteLine("New number of shares owned on teams [0] " +
"and [1] are: ");
ShowVector(outstanding);
``````

``````probs = Probabilities(outstanding, liq);
Console.WriteLine("New inferred probabilities of
winning are: ");
ShowVector(probs, 4, " ");
``````

• 顯示每個小組的一個共用的目前成本
• 執行買入還是賣出交易
• 顯示交易的成本
• 更新共用未處理的總數
• 更新每個小組獲獎的機率

## 總結

Dr。James McCaffreyRedmond，華盛頓中的 Microsoft Research 的運作方式他曾在包括 Internet Explorer 和 Bing 的數個 Microsoft 產品。Dr.McCaffrey 可以到達jammc@microsoft.com