Skip to content

Instantly share code, notes, and snippets.

@lucasgautheron
Created January 8, 2018 19:33
Show Gist options
  • Save lucasgautheron/7ebcb42b546811eb6814cba8002b96a9 to your computer and use it in GitHub Desktop.
Save lucasgautheron/7ebcb42b546811eb6814cba8002b96a9 to your computer and use it in GitHub Desktop.
<?php
// effectuer un tirage au sort (simulation d'un individu)
function trial()
{
// probabilité de croyance de chaque théorie d'après l'enquête
static $probs = [0.55, 0.54, 0.32, 0.31, 0.28, 0.24, 0.20, 0.18, 0.17, 0.16, 0.09];
$count = 0;
// pour chaque théorie, on tire au sort conformément à sa prob. si l'individu
// y adhere ou non
foreach($probs as $p)
{
if((float)rand()/(float)getrandmax() < $p)
$count++;
}
return $count;
}
// on simule un grand nombre d'individus pour tendre vers la vérité
// (erreur stat totalement négligeable)
$trials = 10000000;
// init
$pop = [];
for($i = 0; $i <= 11; ++$i)
{
$pop[$i] = 0;
}
// essais
for($i = 0; $i < $trials; ++$i)
{
$pop[trial()]++;
}
// normalisation
$sum = array_sum($pop);
for($i = 0; $i <= 11; ++$i)
{
$pop[$i] /= $sum/100;
}
// affichage
for($i = 0; $i <= 4; ++$i)
{
echo "$i {$pop[$i]}\n";
}
$fs = $pop[5]+$pop[6];
echo "5,6 $fs\n";
$s = 0;
for($i = 7; $i <= 11; ++$i)
{
$s += $pop[$i];
}
echo "7+ $s";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment