shapleyvalue.com

Attribute value according to a parsimonious game theory solution
Log | Files | Refs

index.php (10302B)


      1 <?php
      2 
      3 /*
      4 
      5 *	File: /shapleyvalue/calculator
      6 *	By: Nuño Sempere
      7 * 	Date: 23 Sept 2019
      8 * 	Last modified: 2 Apr 2021
      9 *	This file calculates the Shapley value of every member of a given coalition.
     10 
     11 */
     12 
     13 
     14 ## HTML Flavor 
     15 
     16 echo '<html>';
     17 
     18 echo '<head>
     19 	<title>Shapley Value Calculator</title>
     20 	<meta name="copyright" content="Nuño Sempere López-Hidalgo">
     21 	<meta name="keywords" content="Shapley Value, Shapley value, calculate Shapley value, calculate Shapley value online free, Shapley value program">
     22 	<meta name="description" content="Shapley value calculator">
     23 	<script async defer data-domain="shapleyvalue.com" src="https://plausible.io/js/plausible.js"></script>
     24 	<link rel="stylesheet" href="CSS/main.css" type="text/css">'; 
     25 	// script is the reference to plausible.io web tracking
     26 	// link rel is the reference to the CSS style sheet.'
     27 echo '</head>';
     28 
     29 echo '<body>';
     30 
     31 echo '<h1> Shapley Value Calculator </h1>';
     32 
     33 
     34 ## Pointer to this file
     35 
     36 $thisFile = "index.php";
     37 
     38 ## Decide the Number of Players
     39  
     40 $numPlayers = $_POST["numplayers"] ?? 3; 
     41 	// It sees what number of players the user has selected.
     42 	// If it's the first time the user comes to the webpage, the default will be 3
     43 
     44 ## Prints the current example:
     45 $example = $_GET["example"] ?? -1;
     46 
     47 echo '<div class="centeredExample">';
     48 echo "<p>";
     49 switch ($example) {
     50     case 1:
     51         echo "Example 1: Alice and Bob and both necessary to produce something which has value 1500. Alice is player 1, Bob is player 2.";
     52         $numPlayers = 2;
     53 	$getCoalitionNum = array(0,0,0,1500);
     54 	break;
     55     case 2:
     56         echo "Example 2: Alice and Bob are each individually responsible for two different projects, each of which has value 1000. Alice is player 1, Bob is player 2.";
     57 	$getCoalitionNum = array(0,1000,1000,2000);
     58         $numPlayers =2;
     59         break;
     60     case 3:
     61 	echo "Example 3: Newton and Leibniz invented calculus at the same time. It has a value of 100, in arbitrary units. Assumption: Nobody else could have invented calculus. Newton is player 1, Leibniz is player 2";
     62 	$getCoalitionNum = array(0,100,100,100);
     63         $numPlayers =2;
     64         break;
     65     case 4:
     66 	echo "Example 4: Netwon invented Calculus. Leibniz, mad with envy, pretended that he also invented calculus at the same time. Newton is player 1. Lebniz is player 2.";
     67 	$getCoalitionNum = array(0,100,0,100);
     68         $numPlayers =2;
     69         break;
     70     case 5:
     71 	echo "Suppose that the Against Malaria Foundation (AMF) will spend $1M on a malaria net distribution. As a result of AMF's commitment, the Gates Foundation contributes $400,000. If AMF had not acted, Gates would have spent the $400,000 on something else, half as valuable. AMF is player 1, Gates is player 2.";
     72 	$getCoalitionNum = array(0,1000000,200000,1400000);
     73         $numPlayers =2;
     74         break;
     75     case 6:
     76 	echo "Example 6: Suppose that the Against Malaria Foundation (AMF) commits $1M to a malaria net distribution. But if AMF had put nothing in, DFID would instead have committed $500,000 to the net distribution. Now, DFID commits that money to something half as valuable. AMF is player 1, DFID is player 2.";
     77 	$getCoalitionNum = array(0,1000000,500000,1250000);
     78         $numPlayers =2;
     79         break;
     80     case 7:
     81 	echo "Example 7: 7 people boil a goat in their mother's milk, independently and at the same time. According to the Kabbalah, this has terrible implications: -1000 value is lost. Suppose that all the damage is done once the first deed is done.";
     82         $numPlayers =7;
     83 	$getCoalitionNum = array(0);
     84 	for($i = 1; $i<128; $i++){
     85 		array_push($getCoalitionNum, -100);
     86 	}
     87         break;
     88     case 8:
     89 	echo "Example 8: Suppose that there was a position in an EA org, for which there were 6 qualified applicants which are otherwise 'idle'. In arbitrary units, the person in that position in that organization can produce an impact of 100 utility. The organization is player 1, applicants are players 2-7.";
     90         $numPlayers =7;
     91 	$getCoalitionNum = array(0);
     92 	for($i = 1; $i<128; $i++){
     93 		if($i<=64){
     94 			array_push($getCoalitionNum, 0);
     95 		}else{
     96 			array_push($getCoalitionNum, 100);
     97 		}
     98 	}
     99         break;
    100     case 9:
    101 	echo "Example 9: A small Indian state with 10 million inhabitants spends $60 million to vaccinate 30% of their population. An NGO which would otherwise be doing something really ineffective, comes in, and by sending reminders, increases the vaccination rate to 35%. They do this very cheaply, for $100,000. The government is player 1, the indian state is player 2. Exercise: What if, instead, the NGO would have done something equally valuable?";
    102         $numPlayers =2;
    103 	$getCoalitionNum = array(0,3000000,0,3500000);
    104         break;
    105     case 10:
    106 	echo "Example 10: Same as Example 9, but now there are 6 government subagencies, each of which we consider as a distinct agent. The NGO is player 1, government agencies are players 2-7.";
    107         $numPlayers =7;
    108 	$getCoalitionNum = array(0);
    109 	for($i = 1; $i<128; $i++){
    110 		if($i<126){
    111 			array_push($getCoalitionNum, 0);
    112 		}else if($i==126){
    113 			array_push($getCoalitionNum, 3000000);
    114 		}else{
    115 			array_push($getCoalitionNum, 3500000);
    116 		}
    117 	}
    118 	break;
    119     case 11:
    120         echo "Example 11: An organization organizes a costless online vote to pass a measure of value 1, and it passes if it gets more than 3 votes. 7 people vote in favour.";
    121         $getCoalitionNum = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
    122         $numPlayers = 7;
    123         break;
    124     default:
    125         $example = -1;
    126 	break;
    127 
    128 }
    129 echo "</p>";
    130 echo "</div>";
    131 echo "</br>";
    132 echo "<form action=".$thisFile." method='post'>
    133 	<select name='numplayers'>";
    134 	
    135 	for($n = 1; $n<8; $n++){
    136 		
    137 		if($n == $numPlayers){
    138 			$hasBeenSelected = "selected = 'selected'";
    139 		}else{
    140 			$hasBeenSelected = "";
    141 		}
    142 		echo "<option value =".$n." ".$hasBeenSelected.">Number of players: ".$n."</option>";
    143 	}
    144 
    145 	echo "</select>";
    146 	echo "&nbsp;";
    147 	echo "<input type='submit' value='Change' class = 'Buttons'>";
    148 	echo "</form>";
    149 
    150 ## Power set of all players
    151 
    152 for($n = 1; $n <= $numPlayers; $n++){
    153 	$setOfPlayers[$n] = $n;
    154 }
    155 
    156 $powerSet = powerSet($setOfPlayers);
    157 
    158 $i = 0;
    159 foreach($powerSet as $set){
    160 	
    161 	$powerSetAsStrings[$i] = setToString($set);
    162 	$i++;
    163 }
    164 
    165 
    166 ## Forms for the value of the coalition. If there is an example, use that.
    167 
    168 if($example==-1){
    169 	$getCoalitionNum = $_POST['CoalitionNum']?? 0;
    170 }
    171 
    172 if($getCoalitionNum == 0){
    173 	//That is, if we haven't yet posted anything to ourselves regarding the size of the coalition
    174 	// Or if we're not in an example.
    175 	echo '<form action='.$thisFile.' method = "post">';
    176 	echo '<div class="form">';
    177 	
    178 
    179 	echo '<input type="hidden" name="numplayers" value='.$numPlayers.' />';
    180 	$i = 0;
    181 	foreach($powerSetAsStrings as $setAsString){
    182 
    183 	echo '<p>';
    184 		echo '<label>Value of coalition '.$setAsString.': </label>';	
    185 		echo '<input type="number" name="CoalitionNum['.$i.']" value = "0" class = "Box">';
    186 	echo '</p>';
    187 	$i++;
    188 	}
    189 		echo '</br>';
    190 
    191 	echo '</div>';
    192 		echo '<input type="submit" value="Compute" class="Buttons" >';
    193 	echo '</form>';
    194 
    195 }else{
    196 	
    197 	echo '<form action='.$thisFile.' method = "post">';
    198 
    199 	echo '<input type="hidden" name="numplayers" value='.$numPlayers.' />';
    200 	
    201 	echo '<div class="form">';
    202 	$i = 0;
    203 	foreach($powerSetAsStrings as $setAsString){
    204 
    205 	echo '<p>';
    206 		echo '<label>Value of coalition '.$setAsString.': </label>';	
    207 		echo '<input type="number" name="CoalitionNum['.$i.']" value ='.$getCoalitionNum[$i].' class = "Box">';
    208 	echo '</p>';
    209 	$i++;
    210 	}
    211 		echo '</br>';
    212 
    213 	echo '</div>';
    214 		echo '<input type="submit" value="Compute" class="Buttons" >';
    215 	echo '</form>';
    216 
    217 
    218 ## Compute the Shapley values
    219 //  Now, we have posted our data to ourselves. Note that we're still inside the else{} part of the loop
    220 // All that remains is to do the actual calculations.
    221 
    222 /*
    223 
    224 Reminder
    225 
    226 numPlayers: number of players
    227 setOfPlayers: Numbers from 1 to n
    228 powerSet: The power set of the above. All the different possible combinations.
    229 getCoalitionNum: The value of coalitions 1 through 2^#
    230 */
    231 
    232 
    233 $i = 0;
    234 
    235 for($n = 0; $n<$numPlayers; $n++){
    236 	$Impact[$n] = 0;
    237 }
    238 
    239 foreach($powerSet as $set){
    240 	// in_array() function: will be useful. https://www.php.net/manual/es/function.in-array.php	
    241 
    242 	$size = sizeof($set);
    243 	if($size !=0){
    244 		foreach($set as $player){
    245 			
    246 			$marginalImpact =  $getCoalitionNum[$i] - $getCoalitionNum[$i - pow(2,($player-1) ) ];
    247 			$Impact[$player-1] += $marginalImpact / choose($size-1,$numPlayers-1);
    248 		}
    249 	}
    250 
    251 	$i++;
    252 
    253 }
    254 
    255 echo 	'<p>';
    256 for($n=1; $n <=$numPlayers; $n++){
    257 	echo "The Shapley value of player ".$n." is: ".$Impact[$n-1]/$numPlayers."</br>";
    258 }
    259 echo 	'</p>';
    260 
    261 } // this is the end of the else{} part of the "have we posted any data to ourselves yet, or are we in an example" question. Only executes if we indeed have.
    262 
    263 echo '</br>';
    264 
    265 echo '<a href="/examples.html">List of examples</a></br>';
    266 echo '<a href="?example='.rand(1,11).'">Random example</a></br>';
    267 
    268 ## More HTML flavor
    269 //echo '<h3>Also of interest:</h3>
    270 //	<a href="url">Shapley value resources</a> </br>'
    271 echo 	'<p>';
    272 echo 	'<br /></br>';
    273 echo	'See also: <a href="https://www.effectivealtruism.org/" target="_blank">"Effective Altruism"</a> for this concept applied to altruism</br>';
    274 echo 	'</p>';
    275 echo '</body>';
    276 echo '</html>';
    277 
    278 
    279 ## Functions
    280 
    281 function powerSet($array){
    282 	
    283 	$results = array(array());
    284 	foreach($array as $element){
    285 		foreach($results as $combination){
    286 			array_push($results, array_merge(array($element), $combination));
    287 		}
    288 	}
    289 	return $results;
    290 	// https://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm
    291 }
    292 
    293 
    294 
    295 function setToString($set){
    296 	
    297 	$size = sizeof($set);
    298 	
    299 	if($size == 0){
    300 	
    301 		return "{}";
    302 	
    303 	}else{
    304 		$return = "{";
    305 		for($i=0; $i<$size-1; $i++){
    306 			$return .= $set[$i].", ";
    307 		}
    308 		$return .= $set[$i]."}";
    309 		return $return;
    310 	}
    311 }
    312 
    313 function factorial($n){
    314 	if($n == 0){
    315 		return 1;
    316 	}else{
    317 
    318 		$f = 1;
    319 		for($i=$n; $i>=1;$i--){
    320 			$f *= $i;
    321 		}
    322 		return $f;
    323 	}	
    324 }
    325 
    326 function choose($m, $n){
    327 	// Choose n objects among m choices
    328 	return factorial($n) / (factorial($m)*factorial($n-$m));
    329 
    330 }
    331 
    332 ?>