commit 1d0ed53d47e15f007fd55223c7d12a1af4762779 Author: NunoSempere Date: Fri Apr 2 17:37:40 2021 +0200 Updated webpage & created a git repository diff --git a/CSS/main.css b/CSS/main.css new file mode 100644 index 0000000..17f8ad3 --- /dev/null +++ b/CSS/main.css @@ -0,0 +1,63 @@ + diff --git a/ProportionalApprovalVoting.html b/ProportionalApprovalVoting.html new file mode 100644 index 0000000..e317376 --- /dev/null +++ b/ProportionalApprovalVoting.html @@ -0,0 +1,219 @@ + + + + + + + + + + + +

Proportional Approval Voting MVP

+

What is this? How does this work?

+

This is the simplest version of a program which computes the result of an election, under the Proportional Approval Voting method.

+

It takes a csv (comma separated value) file, with the same format as this one, which might be produced by a Google Forms like this one.

+

It computes the result using client-side JavaScript, which means that all operations are run in your browser, as opposed to in a server which is not under your control. In effect, all this webpage does is provide you with a bunch of functions. In fact, you could just load this page, disconnect from the internet, upload your files, and you could still use the webpage to get the results you need.

+
+
+ Upload your CSV File to compute the result + + +
+ +
+
+

Proudly created by Nuño Sempere.

+ + + diff --git a/examples.html b/examples.html new file mode 100644 index 0000000..fc74fd5 --- /dev/null +++ b/examples.html @@ -0,0 +1,40 @@ + + + Shapley Value Calculator + + + + + + + + +

List of examples

+ +

Go back


+ +

+Example 1: Alice and Bob and both necessary to produce something which has value 1500. Alice is player 1, Bob is player 2. +


+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. +


+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 +


+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. +


+Example 5: Suppose that AMF will spend $1m on a 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. +


+Example 6: Suppose again that AMF commits $1m to a 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. +


+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. +


+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. +


+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. What if, instead, the NGO would have done something equally valuable? +


+Example 10 1: 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. +


+ +

Go back


+ + diff --git a/index.php b/index.php new file mode 100755 index 0000000..d3877b6 --- /dev/null +++ b/index.php @@ -0,0 +1,327 @@ +'; + +echo ' + Shapley Value Calculator + + + + + '; + // script is the reference to plausible.io web tracking + // link rel is the reference to the CSS style sheet.' +echo ''; + +echo ''; + +echo '

Shapley Value Calculator

'; + + +## Pointer to this file + +$thisFile = "index.php"; + +## Decide the Number of Players + +$numPlayers = $_POST["numplayers"] ?? 3; + // It sees what number of players the user has selected. + // If it's the first time the user comes to the webpage, the default will be 3 + +## Prints the current example: +$example = $_GET["example"] ?? -1; + +echo '
'; +echo "

"; +switch ($example) { + case 1: + echo "Example 1: Alice and Bob and both necessary to produce something which has value 1500. Alice is player 1, Bob is player 2."; + $numPlayers = 2; + $getCoalitionNum = array(0,0,0,1500); + break; + case 2: + 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."; + $getCoalitionNum = array(0,1000,1000,2000); + $numPlayers =2; + break; + case 3: + 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"; + $getCoalitionNum = array(0,100,100,100); + $numPlayers =2; + break; + case 4: + 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."; + $getCoalitionNum = array(0,100,0,100); + $numPlayers =2; + break; + case 5: + echo "Example 5: Suppose that AMF will spend $1M on a 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."; + $getCoalitionNum = array(0,1000000,200000,1400000); + $numPlayers =2; + break; + case 6: + echo "Example 6: Suppose that AMF commits $1M to a 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."; + $getCoalitionNum = array(0,1000000,500000,1250000); + $numPlayers =2; + break; + case 7: + 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."; + $numPlayers =7; + $getCoalitionNum = array(0); + for($i = 1; $i<128; $i++){ + array_push($getCoalitionNum, -100); + } + break; + case 8: + 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."; + $numPlayers =7; + $getCoalitionNum = array(0); + for($i = 1; $i<128; $i++){ + if($i<=64){ + array_push($getCoalitionNum, 0); + }else{ + array_push($getCoalitionNum, 100); + } + } + break; + case 9: + 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?"; + $numPlayers =2; + $getCoalitionNum = array(0,3000000,0,3500000); + break; + case 10: + 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."; + $numPlayers =7; + $getCoalitionNum = array(0); + for($i = 1; $i<128; $i++){ + if($i<126){ + array_push($getCoalitionNum, 0); + }else if($i==126){ + array_push($getCoalitionNum, 3000000); + }else{ + array_push($getCoalitionNum, 3500000); + } + } + break; + default: + $example = -1; + break; + +} +echo "

"; +echo "
"; +echo "
"; +echo "
+ "; + echo " "; + echo ""; + echo "
"; + +## Power set of all players + +for($n = 1; $n <= $numPlayers; $n++){ + $setOfPlayers[$n] = $n; +} + +$powerSet = powerSet($setOfPlayers); + +$i = 0; +foreach($powerSet as $set){ + + $powerSetAsStrings[$i] = setToString($set); + $i++; +} + + +## Forms for the value of the coalition. If there is an example, use that. + +if($example==-1){ + $getCoalitionNum = $_POST['CoalitionNum']?? 0; +} + +if($getCoalitionNum == 0){ + //That is, if we haven't yet posted anything to ourselves regarding the size of the coalition + // Or if we're not in an example. + echo '
'; + echo '
'; + + + echo ''; + $i = 0; + foreach($powerSetAsStrings as $setAsString){ + + echo '

'; + echo ''; + echo ''; + echo '

'; + $i++; + } + echo '
'; + + echo '
'; + echo ''; + echo '
'; + +}else{ + + echo '
'; + + echo ''; + + echo '
'; + $i = 0; + foreach($powerSetAsStrings as $setAsString){ + + echo '

'; + echo ''; + echo ''; + echo '

'; + $i++; + } + echo '
'; + + echo '
'; + echo ''; + echo '
'; + + +## Compute the Shapley values +// Now, we have posted our data to ourselves. Note that we're still inside the else{} part of the loop +// All that remains is to do the actual calculations. + +/* + +Reminder + +numPlayers: number of players +setOfPlayers: Numbers from 1 to n +powerSet: The power set of the above. All the different possible combinations. +getCoalitionNum: The value of coalitions 1 through 2^# +*/ + + +$i = 0; + +for($n = 0; $n<$numPlayers; $n++){ + $Impact[$n] = 0; +} + +foreach($powerSet as $set){ + // in_array() function: will be useful. https://www.php.net/manual/es/function.in-array.php + + $size = sizeof($set); + if($size !=0){ + foreach($set as $player){ + + $marginalImpact = $getCoalitionNum[$i] - $getCoalitionNum[$i - pow(2,($player-1) ) ]; + $Impact[$player-1] += $marginalImpact / choose($size-1,$numPlayers-1); + } + } + + $i++; + +} + +echo '

'; +for($n=1; $n <=$numPlayers; $n++){ + echo "The Shapley value of player ".$n." is: ".$Impact[$n-1]/$numPlayers."
"; +} +echo '

'; + +} // 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. + +echo '
'; + +echo 'List of examples
'; +echo 'Random example
'; + +## More HTML flavor +//echo '

Also of interest:

+// Shapley value resources
' +echo '

'; +echo '

'; +echo 'Other things I have done
'; +echo '

'; +echo ''; +echo ''; + + +## Functions + +function powerSet($array){ + + $results = array(array()); + foreach($array as $element){ + foreach($results as $combination){ + array_push($results, array_merge(array($element), $combination)); + } + } + return $results; + // https://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm +} + + + +function setToString($set){ + + $size = sizeof($set); + + if($size == 0){ + + return "{}"; + + }else{ + $return = "{"; + for($i=0; $i<$size-1; $i++){ + $return .= $set[$i].", "; + } + $return .= $set[$i]."}"; + return $return; + } +} + +function factorial($n){ + if($n == 0){ + return 1; + }else{ + + $f = 1; + for($i=$n; $i>=1;$i--){ + $f *= $i; + } + return $f; + } +} + +function choose($m, $n){ + // Choose n objects among m choices + return factorial($n) / (factorial($m)*factorial($n-$m)); + +} + +?> diff --git a/index_old.php b/index_old.php new file mode 100755 index 0000000..4b2c183 --- /dev/null +++ b/index_old.php @@ -0,0 +1,238 @@ +'; + +echo ' + Shapley Value Calculator + + + + '; + // This is the reference to our CSS style sheet.' +echo ''; + +echo ''; + +echo '

Shapley Value Calculator

'; + + +## Pointer to this file + +$thisFile = "index.php"; + +## Decide the Number of Players + +$numPlayers = $_POST["numplayers"] ?? 3; + // It sees what number of players the user has selected. + // If it's the first time the user comes to the webpage, the default will be 3 + + +echo "
+ "; + echo " "; + echo ""; + echo "
"; + +## Power set of all players + +for($n = 1; $n <= $numPlayers; $n++){ + $setOfPlayers[$n] = $n; +} + +$powerSet = powerSet($setOfPlayers); + +$i = 0; +foreach($powerSet as $set){ + + $powerSetAsStrings[$i] = setToString($set); + $i++; +} + + +## Forms for the value of the coalition + +$getCoalitionNum = $_POST['CoalitionNum']?? 0; + +if($getCoalitionNum == 0){ + //That is, if we haven't yet posted anything to ourselves regarding the size of the coalition + echo '
'; + echo '
'; + + + echo ''; + $i = 0; + foreach($powerSetAsStrings as $setAsString){ + + echo '

'; + echo ''; + echo ''; + echo '

'; + $i++; + } + echo '
'; + + echo '
'; + echo ''; + echo '
'; + +}else{ + + echo '
'; + + echo ''; + + echo '
'; + $i = 0; + foreach($powerSetAsStrings as $setAsString){ + + echo '

'; + echo ''; + echo ''; + echo '

'; + $i++; + } + echo '
'; + + echo '
'; + echo ''; + echo '
'; + + +## Compute the Shapley values +// Now, we have posted our data to ourselves. Note that we're still inside the else{} part of the loop +// All that remains is to do the actual calculations. + +$i = 0; + +/* + +Reminder + +numPlayers: number of players +setOfPlayers: Numbers from 1 to n +powerSet: The power set of the above. All the different possible combinations. +getCoalitionNum: The value of coalitions 1 through 2^# +*/ + + +$i = 0; + +for($n = 0; $n<$numPlayers; $n++){ + $Impact[$n] = 0; +} + +foreach($powerSet as $set){ + // in_array() function: will be useful. https://www.php.net/manual/es/function.in-array.php + + $size = sizeof($set); + if($size !=0){ + foreach($set as $player){ + + $marginalImpact = $getCoalitionNum[$i] - $getCoalitionNum[$i - pow(2,($player-1) ) ]; + $Impact[$player-1] += $marginalImpact / choose($size-1,$numPlayers-1); + } + } + + $i++; + +} + +echo '

'; +for($n=1; $n <=$numPlayers; $n++){ + echo "The Shapley value of player ".$n." is: ".$Impact[$n-1]/$numPlayers."
"; +} +echo '

'; + +} // this is the end of the else{} part of the "have we posted any data to ourselves yet" question. Only executes if we indeed have. + + +## More HTML flavor +//echo '

Also of interest:

+// Shapley value resources
' +echo '

'; +echo '

'; +echo 'Other things I have done
'; +echo '

'; +echo ''; +echo ''; + + +## Functions + +function powerSet($array){ + + $results = array(array()); + foreach($array as $element){ + foreach($results as $combination){ + array_push($results, array_merge(array($element), $combination)); + } + } + return $results; + // https://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm +} + + + +function setToString($set){ + + $size = sizeof($set); + + if($size == 0){ + + return "{}"; + + }else{ + $return = "{"; + for($i=0; $i<$size-1; $i++){ + $return .= $set[$i].", "; + } + $return .= $set[$i]."}"; + return $return; + } +} + +function factorial($n){ + if($n == 0){ + return 1; + }else{ + + $f = 1; + for($i=$n; $i>=1;$i--){ + $f *= $i; + } + return $f; + } +} + +function choose($m, $n){ + // Choose n objects among m choices + return factorial($n) / (factorial($m)*factorial($n-$m)); + +} + +?> diff --git a/publicGoodsGame.php b/publicGoodsGame.php new file mode 100755 index 0000000..e87266e --- /dev/null +++ b/publicGoodsGame.php @@ -0,0 +1,207 @@ + +'; + +echo ' + Shapley Value Calculator + + + + '; + // This is the reference to our CSS style sheet.' +echo ''; + +echo ''; + +echo '

Shapley Value Calculator

'; + + +## Pointer to this file + +$thisFile = "publicGoodsGame.php"; + + +## Power set of all players + +$numPlayers = 6; + ## Initially, the number of players is 6. +for($n = 1; $n <= $numPlayers; $n++){ + $setOfPlayers[$n] = $n; +} + +$powerSet = powerSet($setOfPlayers); + +$i = 0; +foreach($powerSet as $set){ + + $powerSetAsStrings[$i] = setToString($set); + $i++; +} + +## Check if the thing has been posted + +$getContributionPlayer = $_POST['ContributionPlayer']?? -1; + +$getMultiplier = $_POST['Multiplier'] ?? -1; + +if($getContributionPlayer == -1){ + + ## Get the contributions of each player to the common pot + echo '
'; + echo '
'; + + echo ''; + $i = 0; + + for($i = 0; $i<$numPlayers; $i++){ + echo '

'; + echo ''; + echo ''; + echo '

'; + } + echo '


'; + echo '

'; + echo ''; + echo ''; + echo '

'; + + echo '
'; + + echo '
'; + echo ''; + echo '
'; + +}else{ + + + echo '
'; + echo '
'; + + echo ''; + $i = 0; + + for($i = 0; $i<$numPlayers; $i++){ + echo '

'; + echo ''; + echo ''; + echo '

'; + } + + echo '


'; + echo '

'; + echo ''; + echo ''; + echo '

'; + + echo '
'; + + echo '
'; + echo ''; + echo '
'; + + ## Now, the Shapley value in a public good game is simply going to be contribution*multiplier + ## The classical reward is simply aggregate*multiplier/number of players. + ## Let D = Shapley value - Classical reward + ## Then it would be interesting to check what happens when the payout is + ## P = Classical Reward + Alpha*D, + ## Where alpha ranges from 0 to 1. + ## I think that just comparing alpha = 0, alpha = 1/3, alpha = 2/3, (alpha = 1) would be interesting. + + $sumContributions = array_sum($getContributionPlayer); + + echo '

'; + echo 'Sum of the contributions =  '.$sumContributions; + echo '

'; + echo '

'; + echo 'Sum*Multiplier =  '.($sumContributions*$getMultiplier); + echo '

'; + echo '

'; + $payout = ($sumContributions*$getMultiplier/$numPlayers); + echo 'Payout = Sum*Multiplier/ Number of Players =  '.$payout; + echo '

'; + + ## What is the difference between the payout and the Shapley value? + + echo '
'; + echo '

'; + echo "

For each player, what is the difference between the payout and the Shapley value?

"; + for($i = 0; $i<$numPlayers; $i++){ + echo '

'; + echo 'Difference for Player #'.($i+1).'  = '.($getMultiplier*$getContributionPlayer[$i]-$payout); + echo '

'; + } + echo '

'; + echo '
'; +} +## Footer +echo '

Go back


'; + +echo ''; +echo ''; + +## Functions + +function powerSet($array){ + + $results = array(array()); + foreach($array as $element){ + foreach($results as $combination){ + array_push($results, array_merge(array($element), $combination)); + } + } + return $results; + // https://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm +} + + + +function setToString($set){ + + $size = sizeof($set); + + if($size == 0){ + + return "{}"; + + }else{ + $return = "{"; + for($i=0; $i<$size-1; $i++){ + $return .= $set[$i].", "; + } + $return .= $set[$i]."}"; + return $return; + } +} + +function factorial($n){ + if($n == 0){ + return 1; + }else{ + + $f = 1; + for($i=$n; $i>=1;$i--){ + $f *= $i; + } + return $f; + } +} + +function choose($m, $n){ + // Choose n objects among m choices + return factorial($n) / (factorial($m)*factorial($n-$m)); + +}