PHP code example of tomkyle / binning
1. Go to this page and download the library: Download tomkyle/binning library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tomkyle / binning example snippets
use tomkyle\Binning\BinSelection;
$data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
// Default method: Freedman-Diaconis Rule (1981)
$h = BinSelection::suggestBinWidth($data);
$h = BinSelection::suggestBinWidth($data, BinSelection::DEFAULT);
// Explicitly set method
$h = BinSelection::suggestBinWidth($data, BinSelection::FREEDMAN_DIACONIS);
$h = BinSelection::suggestBinWidth($data, BinSelection::SCOTT);
use tomkyle\Binning\BinSelection;
$data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
// Defaults to Freedman-Diaconis Rule
$k = BinSelection::suggestBins($data);
$k = BinSelection::suggestBins($data, BinSelection::DEFAULT);
// Square Root Rule (Pearson, 1892)
$k = BinSelection::suggestBins($data, BinSelection::SQUARE_ROOT);
$k = BinSelection::suggestBins($data, BinSelection::PEARSON);
// Sturges' Rule (1926)
$k = BinSelection::suggestBins($data, BinSelection::STURGES);
// Doane's Rule (1976) in 2 variants for samples (default) or populations
$k = BinSelection::suggestBins($data, BinSelection::DOANE);
$k = BinSelection::suggestBins($data, BinSelection::DOANE, population: true);
// Scott's Rule (1979)
$k = BinSelection::suggestBins($data, BinSelection::SCOTT);
// Freedman-Diaconis Rule (1981)
$k = BinSelection::suggestBins($data, BinSelection::FREEDMAN_DIACONIS);
// Terrell-Scott’s Rule (1985)
$k = BinSelection::suggestBins($data, BinSelection::TERRELL_SCOTT);
// Rice University Rule
$k = BinSelection::suggestBins($data, BinSelection::RICE);
$k = BinSelection::squareRoot($data);
$k = BinSelection::sturges($data);
// Using sample-based calculation (default)
$k = BinSelection::doane($data);
// Using population-based calculation
$k = BinSelection::doane($data, population: true);
list($h, $k, $R, stddev) = BinSelection::scott($data);
list($h, $k, $R, $IQR) = BinSelection::freedmanDiaconis($data);
$k = BinSelection::terrellScott($data);
$k = BinSelection::rice($data);
use tomkyle\Binning\BinSelection;
// Generate sample data (e.g., from measurements)
$measurements = [
12.3, 14.1, 13.8, 15.2, 12.9, 14.7, 13.1, 15.8, 12.5, 14.3,
13.6, 15.1, 12.8, 14.9, 13.4, 15.5, 12.7, 14.2, 13.9, 15.0
];
echo "Data points: " . count($measurements) . "\n\n";
// Compare different methods
$methods = [
'Sturges’s Rule' => BinSelection::STURGES,
'Rice University Rule' => BinSelection::RICE,
'Terrell-Scott’s Rule' => BinSelection::TERRELL_SCOTT,
'Square Root Rule' => BinSelection::SQUARE_ROOT,
'Doane’s Rule' => BinSelection::DOANE,
'Scott’s Rule' => BinSelection::SCOTT,
'Freedman-Diaconis Rule' => BinSelection::FREEDMAN_DIACONIS,
];
foreach ($methods as $name => $method) {
$bins = BinSelection::suggestBins($measurements, $method);
echo sprintf("%-18s: %2d bins\n", $name, $bins);
}
try {
// This will throw an exception
$bins = BinSelection::sturges([]);
} catch (InvalidArgumentException $e) {
echo "Error: " . $e->getMessage();
// Output: "Dataset cannot be empty to apply the Sturges' Rule."
}
try {
// This will throw an exception
$bins = BinSelection::suggestBins($data, 'invalid-method');
} catch (InvalidArgumentException $e) {
echo "Error: " . $e->getMessage();
// Output: "Unknown binning method: invalid-method"
}