更新:以更优雅的方式应用数学公式(也许递归调用可以做到这一点)


Updated: Applying a Math Formula in a more elegant way (maybe a recursive call would do the trick)

******最新消息****

我已经在数学上发布了同样的问题。StackExchange,得到了一个很有趣的答案关于如何递归地做到这一点;我没有在php中使用矩阵的经验,也没有递归调用。你能给我一个提示如何在PHP代码他说什么?

https://math.stackexchange.com/questions/92942/applying-a-math-formula-in-a-more-elegant-way-maybe-a-recursive-call-would-do-t


乍一看可能有点复杂,但毕竟没有什么特别的。

我们在一个扑克锦标赛中,有n个玩家,每个人有一堆筹码。在比赛结束时,只有"p"个名额需要付费。

我们想要应用一个数学公式来将实际玩家的堆栈转换为金钱。所以我们称"公平"为每个玩家在每个付费地点完成比赛的概率。

我假设有4个玩家(P1,P2,P3,P4)和3个付费位置:

第一名的权益是:

$eq1_1 = ($P1_stack / $total_chips);
$eq2_1 = ($P2_stack / $total_chips);
$eq3_1 = ($P3_stack / $total_chips);
$eq4_1 = ($P4_stack / $total_chips);

第二名:

要计算每个玩家在第二名的权益,我们必须假设每个剩余的玩家都赢得了第一名,从总筹码中减去他的(获胜者的)堆栈,将玩家2筹码除以剩余筹码,并将这个数字乘以概率(获得第一名的玩家的第一名的权益)。我知道,这里曾报道过脑爆炸。

//-if P2 wins the tornament
$eq1_2 = $eq2_1*($P1_stack/($total_chips-$P2_stack));
//-if P3 wins the tornament
$eq1_2 = $eq1_2 + $eq3_1*($P1_stack/($total_chips-$P3_stack));
//-if P4 wins the tornament
$eq1_2 = $eq1_2 + $eq4_1*($P1_stack/($total_chips-$P4_stack));

如果有更多的玩家,这个循环应该会继续。

股权排名第三

如果你的大脑还没有爆炸,在你读了这篇文章之后,它会爆炸的要计算第三位的P1权益,我们必须:1)从总筹码中减去第一名和第二名。2)从我们得到的数字中减去P1堆栈。(Remainingstack)3)在假设获胜者之后,计算第二名获胜者在第二名上的权益。4)乘以我们得到的数字(3)(P1_stack/Remainingstack)第一名获胜者的权益

我已经写了一个工作代码到目前为止,但它只工作,如果有3支付放置。我想修改它,以获得更优雅和通用的方式获得权益,即使付费位置超过3个。

这是我的代码:http://codepad.org/Q62l2wfv

我不是一个编码专家,但也许通过递归调用来做它应该更快,并且可以毫无困难地计算出第4,第5,第6位。

我已经在这里回答了这个问题:

https://stackoverflow.com/a/8663431/815724

为了完整起见,我将复制这个答案:

给你。

我将这段代码放入公共领域。

# Function to make an array of 'width' zeros
function makerow($width){
 $row=array();
 for($x=0;$x<$width;$x++){
   $row[$x]=0;
 }
 return $row;
}
# Function to make a width*height matrix
function makematrix($width,$height){
 $matrix=array();
 for($y=0;$y<$height;$y++){
  $matrix[$y]=array();
  for($x=0;$x<$width;$x++){
   $matrix[$y][$x]=0;
  }
 }
 return $matrix;
}
# Adds one matrix to another
function matrixadd(&$matrixdest,&$matrixsrc){
 for($i=0;$i<count($matrixdest);$i++){
  for($j=0;$j<count($matrixdest[$i]);$j++){
   $matrixdest[$i][$j]+=$matrixsrc[$i][$j];
  }
 }
}
# Multiplies a matrix by a scalar
function matrixmultiply(&$matrix,$scalar){
 for($i=0;$i<count($matrix);$i++){
  for($j=0;$j<count($matrix[$i]);$j++){
   $matrix[$i][$j]*=$scalar;
  }
 }
}
# Calculates the equity of each place. Rows indicate players;
# columns indicate places (0 is 1st place, 1 is second, and so on)
function equitymatrix(&$stacks){
 # If there's only one stack it's easy, one player, thus one place
 if(count($stacks)==1){
  return array(array(1));
 }  
 # Calculate the total of all stacks
 $totalStacks=0;
 for($i=0;$i<count($stacks);$i++){
  $totalStacks+=$stacks[$i];
 }
 # Calculate the probabilities of each player getting first place
 $probabilities=array();
 for($i=0;$i<count($stacks);$i++){
  $probabilities[$i]=$stacks[$i]*1.0/$totalStacks;
 }
 $subequities=array();
 for($i=0;$i<count($stacks);$i++){
  $substacks=array();
  # Assume that player i would be in first place
  # Create a new array with i's stack removed
  for($j=0;$j<count($stacks);$j++){
   if($j!=$i){
    array_push($substacks,$stacks[$j]);
   }
  }
  # Find the subequity of the remaining players
  $subequities[$i]=equitymatrix($substacks);
  for($j=0;$j<count($subequities[$i]);$j++){
   array_splice($subequities[$i][$j],0,0,0);
  }
  # Add player i back
  $newrow=makerow(count($stacks));
  $newrow[0]=1;
  array_splice($subequities[$i],$i,0,array($newrow));
 }
 $equities=makematrix(count($stacks),count($stacks));
 for($i=0;$i<count($stacks);$i++){
  # Multiply the probabilities
  matrixmultiply($subequities[$i],$probabilities[$i]);
  # Add the subequity
  matrixadd($equities,$subequities[$i]);
 }
 return $equities;
}

的例子:

$mystacks=array(10,40,30,20);
print_r(equitymatrix($mystacks));