Diversity prediction theorem

From Rosetta Code
Revision as of 13:19, 3 December 2016 by rosettacode>Aloisdg (Add C++)

Template:Draft

The wisdom of the crowd is the collective opinion of a group of individuals rather than that of a single expert.

Wisdom-of-the-crowds research routinely attributes the superiority of crowd averages over individual judgments to the elimination of individual noise, an explanation that assumes independence of the individual judgments from each other. Thus the crowd tends to make its best decisions if it is made up of diverse opinions and ideologies.

Scott E. Page introduced the diversity prediction theorem: "The squared error of the collective prediction equals the average squared error minus the predictive diversity". Therefore, when the diversity in a group is large, the error of the crowd is small.

- Average Individual Error: Average of the individual squared errors

- Collective Error: Squared error of the collective prediction

- Prediction Diversity: Average squared distance from the individual predictions to the collective prediction

So, The Diversity Prediction Theorem: Given a crowd of predictive models

Collective Error = Average Individual Error - Prediction Diversity

wikipedia paper



C++

<lang C++>

  1. include <iostream>
  2. include <vector>
  3. include <numeric>

float sum(const std::vector<float> &array) {

   return std::accumulate(array.begin(), array.end(), 0.0);

}

float square(float x) {

   return x * x;

}

float mean(const std::vector<float> &array) {

   return sum(array) / array.size();

}

float averageSquareDiff(float a, const std::vector<float> &predictions) {

   std::vector<float> results;
   for (float x : predictions)
       results.push_back(square(x - a));
   return mean(results);

}

void diversityTheorem(float truth, const std::vector<float> &predictions) {

   float average = mean(predictions);
   std::cout
       << "average-error: " << averageSquareDiff(truth, predictions) << "\n"
       << "crowd-error: " << square(truth - average) << "\n"
       << "diversity: " << averageSquareDiff(average, predictions) << std::endl;

}

int main() {

   diversityTheorem(49, {48,47,51});
   diversityTheorem(49, {48,47,51,42});
   return 0;

} </lang>

Output:
average-error: 3
crowd-error: 0.11111
diversity: 2.88889
average-error: 14.5
crowd-error: 4
diversity: 10.5

TypeScript

<lang TypeScript> function sum(array: Array<number>): number {

   return array.reduce((a, b) => a + b)

}

function square(x : number) :number {

   return x * x

}

function mean(array: Array<number>): number {

   return sum(array) / array.length

}

function averageSquareDiff(a: number, predictions: Array<number>): number {

   return mean(predictions.map(x => square(x - a)))

}

function diversityTheorem(truth: number, predictions: Array<number>): Object {

   const average: number = mean(predictions)
   return {
       "average-error": averageSquareDiff(truth, predictions),
       "crowd-error": square(truth - average),
       "diversity": averageSquareDiff(average, predictions)
   }

}

console.log(diversityTheorem(49, [48,47,51])) console.log(diversityTheorem(49, [48,47,51,42])) </lang>

Output:
{ 'average-error': 3,
  'crowd-error': 0.11111111111111269,
  diversity: 2.888888888888889 }
{ 'average-error': 14.5, 'crowd-error': 4, diversity: 10.5 }