Skip to content

Instantly share code, notes, and snippets.

@divinity76
Last active September 19, 2023 08:07
Show Gist options
  • Save divinity76/f393ef1745e6935333305ba3f598be9c to your computer and use it in GitHub Desktop.
Save divinity76/f393ef1745e6935333305ba3f598be9c to your computer and use it in GitHub Desktop.
sick simulation
{
"percent_sick_if_tested_positive": 4.458833791853989,
"percent_healthy_if_tested_positive": 48.80539325842697,
"percent_sick_if_tested_negative": 0.0011333259793069787,
"percent_healthy_if_tested_negative": 49.99971666368849,
"percent_tested_positive_and_sick": 49.719416386083054,
"percent_tested_positive_and_healthy": 1.9703818540227784,
"percent_tested_negative_and_sick": 1.1037527593818985,
"percent_tested_negative_and_healthy": 49.49240211146362,
"total_population": 5408000,
"total_tested": 5408000,
"total_sick": 5376,
"total_healthy": 5402624,
"total_positive": 113908,
"total_negative": 5294092,
"positive_and_sick": 5316,
"positive_and_healthy": 108592,
"negative_and_healthy": 5294032,
"negative_and_sick": 60
}
<?php
declare(strict_types=1);
/*
0.1% i Norge har en sykdom.
Positiv test (+): testen sier at du har sykdommen
2% får positiv test selv om de ikke er syke
99% får positiv test når de er syke.
Du tester deg og får positiv test - hva er sannsynligheten for at du er syk?
*/
use function var_dump as d;
function returnTrueXPercentOfTheTime(float $percent): bool
{
return random_int(0, 100000) <= ($percent * 1000);
}
function calculatePercent(float $a, float $b): float
{
return 100 * ($a / ($a + $b));
}
$total_population = 5_408_000; // population of Norway
$total_tested = $total_population;
$total_positive = 0;
$total_negative = 0;
$positive_and_sick = 0;
$positive_and_healthy = 0;
$negative_and_healthy = 0;
$negative_and_sick = 0;
$total_sick = 0;
$total_healthy = 0;
for ($personId = 0; $personId < $total_population; ++$personId) {
$is_sick = returnTrueXPercentOfTheTime(0.1);
$tested_positive = returnTrueXPercentOfTheTime($is_sick ? 99 : 2);
if ($is_sick) {
++$total_sick;
if ($tested_positive) {
++$total_positive;
++$positive_and_sick;
} else {
++$total_negative;
++$negative_and_sick;
}
} else { // !$is_sick
++$total_healthy;
if ($tested_positive) {
++$total_positive;
++$positive_and_healthy;
} else {
++$total_negative;
++$negative_and_healthy;
}
}
}
$percent_tested_positive_and_sick = calculatePercent($positive_and_sick, $total_sick);
$percent_tested_positive_and_healthy = calculatePercent($positive_and_healthy, $total_healthy);
$percent_tested_negative_and_sick = calculatePercent($negative_and_sick, $total_sick);
$percent_tested_negative_and_healthy = calculatePercent($negative_and_healthy, $total_healthy);
$percent_sick_if_tested_positive = calculatePercent($positive_and_sick, $total_positive);
$percent_healthy_if_tested_positive = calculatePercent($positive_and_healthy, $total_positive);
$percent_sick_if_tested_negative = calculatePercent($negative_and_sick, $total_negative);
$percent_healthy_if_tested_negative = calculatePercent($negative_and_healthy, $total_negative);
$data = [
'percent_sick_if_tested_positive' => $percent_sick_if_tested_positive,
'percent_healthy_if_tested_positive' => $percent_healthy_if_tested_positive,
'percent_sick_if_tested_negative' => $percent_sick_if_tested_negative,
'percent_healthy_if_tested_negative' => $percent_healthy_if_tested_negative,
'percent_tested_positive_and_sick' => $percent_tested_positive_and_sick,
'percent_tested_positive_and_healthy' => $percent_tested_positive_and_healthy,
'percent_tested_negative_and_sick' => $percent_tested_negative_and_sick,
'percent_tested_negative_and_healthy' => $percent_tested_negative_and_healthy,
'total_population' => $total_population,
'total_tested' => $total_tested,
'total_sick' => $total_sick,
'total_healthy' => $total_healthy,
'total_positive' => $total_positive,
'total_negative' => $total_negative,
'positive_and_sick' => $positive_and_sick,
'positive_and_healthy' => $positive_and_healthy,
'negative_and_healthy' => $negative_and_healthy,
'negative_and_sick' => $negative_and_sick,
];
$pretty = json_encode($data, JSON_PRETTY_PRINT);
echo $pretty;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment