<?php
$SIMULATIONS = filter_input(INPUT_GET, 'simulations') ?: 10000;
$TOURS_PAR_SIMULATION = filter_input(INPUT_GET, 'tours') ?: 100;
$RANDOMIZATION_POURCENTS = filter_input(INPUT_GET, 'pc') ?: 5.0;
$GROUPS_COUNT = filter_input(INPUT_GET, 'gcount') ?: 25.;
$RANDOM_TYPE = filter_input(INPUT_GET, 'type') === 'ADD' ? 'ADD' : 'MULT';
$MEDIAN_CORRECTION = (filter_input(INPUT_GET, 'correct') && $RANDOM_TYPE === 'MULT');
const X_SIZE = 30;
const PC_Y_HEIGHT = 300;

$RANDOMIZATION_POURCENTS_PLUS = filter_input(INPUT_GET, 'pcp') ?: ($MEDIAN_CORRECTION ? (100*$RANDOMIZATION_POURCENTS)/(100 - $RANDOMIZATION_POURCENTS) : $RANDOMIZATION_POURCENTS);
$RANDOMIZATION_POURCENTS_MINUS = filter_input(INPUT_GET, 'pcm') ?: ($RANDOMIZATION_POURCENTS);
$results = array();
for ($i = $SIMULATIONS; $i > 0; $i--) {
	$value = 100.0;
	for ($t = $TOURS_PAR_SIMULATION; $t > 0; $t--) {
		if ($RANDOM_TYPE === 'ADD') {
			$value += mt_rand(-1000*$RANDOMIZATION_POURCENTS_MINUS, 1000*$RANDOMIZATION_POURCENTS_PLUS)/1000.;
		} else {
			$value *= (100.+mt_rand(-1000*$RANDOMIZATION_POURCENTS_MINUS, 1000*$RANDOMIZATION_POURCENTS_PLUS)/1000.)/100.;
		}
	}
	$results[] = $value;
}

sort($results);

$groupIncrement = floor( max($results)/$GROUPS_COUNT );
$rmax = max($results);
$rmin = min($results);
$imax = ceil($rmax/$groupIncrement);
$groups = array_fill(0, $imax+1, 0);

$NARROWED_PBA = 0;
$SMALLEST_PBA1 = 0;
foreach ($results as $r) {
	$i = max(floor($r/$groupIncrement), 0);
	$groups[$i]++;
	if ($r > 95 && $r < 105) {
		$NARROWED_PBA++;
	}
	if ($r < 10) {
		$SMALLEST_PBA1++;
	}
}
$MOYENNE = array_sum($results)/count($results);
$MEDIANE = $results[ceil(count($results)/2)];
$NARROWED_PBA = $NARROWED_PBA/$SIMULATIONS*100.;
$SMALLEST_PBA1 = $SMALLEST_PBA1/$SIMULATIONS*100.;

$xmin = min(array_keys($groups));
$xmax = max(array_keys($groups));
// var_dump($groups);
header('content-type: image/svg+xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
?>
<svg xmlns="http://www.w3.org/2000/svg"
		viewBox="<?php echo ($xmin*X_SIZE); ?> <?php echo -(PC_Y_HEIGHT + 20); ?> <?php echo ($xmax - $xmin)*X_SIZE; ?> <?php echo (PC_Y_HEIGHT + 20 + 20); ?>">
	<style>
		rect {
			fill: #faa;
			stroke: red;
		}
		.axes {
			fill: none;
			stroke: black;
		}
		text {
			stroke: none;
			fill: black;
			text-anchor: middle;
			font-size: 8px;
		}
		.axes text {
			text-anchor: start;
		}
		.axes text tspan {
			font-weight: bold;
		}
		circle {
			fill: hsla(0, 80%, 80%, 0.02);
			stroke: none;
		}
	</style>
	<!--<g>
		?php foreach ($results as $r) {
			echo '<circle cx="' . ($xmin + ($r - $rmin)/($rmax - $rmin)*($xmax - $xmin))*X_SIZE . '" cy="' . -(PC_Y_HEIGHT/2.) . '" r="' . 2 . '"/>';
		} ?>
	</g>-->
	<g>
		<?php foreach ($groups as $i => $count) {
			$pc = round(100*$count / $SIMULATIONS);
			$tx = (($i+0.5)*X_SIZE);
			$ty = $pc/100.*PC_Y_HEIGHT;
			?>
		<rect x="<?php echo ($i*X_SIZE); ?>" y="<?php echo -($ty); ?>" width="<?php echo X_SIZE; ?>" height="<?php echo ($ty); ?>"/>
		<text x="<?php echo $tx; ?>" y="<?php echo -($ty + 4); ?>">
			<tspan x="<?php echo $tx; ?>" dy="0"><?php echo $pc . '%'; ?></tspan>
		</text>
		<text x="<?php echo $tx; ?>" y="8">
			<tspan x="<?php echo $tx; ?>" dy="0"><?php echo ($i*$groupIncrement); ?></tspan>
			<tspan x="<?php echo $tx; ?>" dy="3"><?php echo '..'; ?></tspan>
			<tspan x="<?php echo $tx; ?>" dy="8"><?php echo (($i+1)*$groupIncrement); ?></tspan>
		</text>
		<?php } ?>
	</g>
	
	<g class="axes">
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT - 10; ?>"><?php echo 'Random type: <tspan>' . ($RANDOM_TYPE === 'ADD' ? 'u(N+1)=u(N)+/-3' : 'u(N+1)=u(N)*(1+/-3%)') . '</tspan> ($_GET[\'type\'])'; ?></text>
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT + 00; ?>"><?php echo 'Simulations: <tspan>' . $SIMULATIONS . '</tspan> ($_GET[\'simulations\'])'; ?></text>
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT + 10; ?>"><?php echo 'Tours/simulation: <tspan>' . $TOURS_PAR_SIMULATION . '</tspan> ($_GET[\'tours\'])'; ?></text>
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT + 20; ?>"><?php echo '<tspan>-' . $RANDOMIZATION_POURCENTS_MINUS . '%..+' . $RANDOMIZATION_POURCENTS_PLUS . '%</tspan> par tour' . ' ($_GET[\'pc/pcp/pcm\'])'; ?></text>
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT + 30; ?>"><?php echo 'Correction de la médiane: <tspan>' . ($MEDIAN_CORRECTION ? 'OUI' : 'non') . '</tspan> ' . ' ($_GET[\'correct\'])'; ?></text>
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT + 40; ?>"><?php echo 'Moyenne: <tspan>' . round($MOYENNE,1) . '</tspan>'; ?></text>
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT + 50; ?>"><?php echo 'Mediane: <tspan>' . round($MEDIANE,1) . '</tspan>'; ?></text>
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT + 60; ?>"><?php echo 'Probabilité 95..105: <tspan>' . round($NARROWED_PBA,1) . '%</tspan>'; ?></text>
		<text x="<?php echo ($xmax - $xmin)*X_SIZE*0.25; ?>" y="<?php echo -PC_Y_HEIGHT + 70; ?>"><?php echo 'Probabilité 0..10: <tspan>' . round($SMALLEST_PBA1,1) . '%</tspan>'; ?></text>
		<path d="M0,0 L<?php echo $SIMULATIONS; ?>,0"/>
		<path d="M0,0 L0,500"/>
	</g>
</svg>
