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

梦想的初衷 提交于 2019-12-24 00:58:06

问题


******Last news****

I've posted the same question in Math.StackExchange and got a really interesting answer on how to do this recursively; I have no experience on using matrixes in php neither with recursive calls. Could you please give me a hint on how to code in PHP what he said?

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


This could appear a little bit complex at first sight but afterall there's nothing special.

We are in a poker touranment, there are 'n' players, each one has a stack of chips. There are only 'p' places to be paid at the end of the tournament.

We want to apply a math formula to convert the actual players' stacks into money. So we call 'equity' the probability of each player to finish the tournament in each paid place.

I'll hypothesize there are 4 players (P1,P2,P3,P4) and 3 paid places:

Equity on 1st place is:

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

Equity on 2nd place:

To calculate the equity of each player on 2nd place we have to hypothesize that each one of the remanent players has won the first prize, subtract his(the winner's) stack from the total chips, divide the player 2 chips by the remanent chips, and multiply this number by the probability(equity on 1st prize of the player who won the 1st prize). I know, brains' explosions have been reported here.

//-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));

if there would have been more players the cycle should have continued.

Equity on 3rd place

If your brain hasn't exploded yet, it will after you'll read this :p To calculate P1 equity on 3rd place we have to: 1) Subtract the winner AND the second winner stacks from the total chips. 2) Subtract P1 stack from the number we got. (Remainingstack) 3) Calculate the equity of the second winner on second place after a winner has been hypothesized. 4) Multiply the number we got (3)(P1_stack/ Remainingstack)Equity of the winner on 1st place.

I've written a working code so far but it works only if there are 3 paid placed. I'd like to modify it to get a more elegant and versatile way to get equity even if the paid places are more than 3.

This is my code: http://codepad.org/Q62l2wfv

I'm not an expert coder but maybe by doing it with a recursive call it should be faster and equity on 4th, 5th, 6th place can be calculated with no difficulties.


回答1:


I've answered that question here:

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

For completeness I will reproduce that answer:

Here you go.

I place this code into the public domain.

# 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;
}

Example:

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


来源:https://stackoverflow.com/questions/8575534/updated-applying-a-math-formula-in-a-more-elegant-way-maybe-a-recursive-call-w

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!