PHP Classes

Weighted PHP Statistics: Calculate statistics on weighted data sets

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 126 All time: 9,419 This week: 560Up
Version License PHP version Categories
weightedstats 1.0.0Freely Distributable5Algorithms, PHP 5, Statistics
Description 

Author

This class can calculate statistics on weighted data sets.

It can take as parameters one data set array with numeric values and another array with the respective weights for the values in the first parameter array.

The class can calculate several types of common statistics on the data set values considering the weight values, like: the mean, average, standard deviation, percentile and skew.

Innovation Award
PHP Programming Innovation award nominee
May 2019
Number 5


Prize: One subscription to the PDF edition of the PHP Architect magazine
There are many types of statistical functions that can be applied to data sets like the mean, average, standard deviation, etc..

This class implements a variant version of those statistical functions by applying variable weights to each number value in the data set to which it is computing the values of those statistical functions.

Manuel Lemos
Picture of Greg Neyman
  Performance   Level  
Name: Greg Neyman <contact>
Classes: 2 packages by
Country: United Kingdom
Age: 46
All time rank: 4204187 in United Kingdom
Week rank: 305 Up14 in United Kingdom Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php
include 'weightedstats.php';

//First argument is the variable of interest, in array form
$inputs = array(222,190,49,106,148,464,221,134,109,90,224,63,262,182,87,166,75,291,205,155,432,226,259,257,297,94,93,120,138,224,77,276,323,78,98,45,48,63,118,410,116,1091,449,1344,201,23,346,271,214,184,25,92,185,41,225,284,303,248,35,409,125,1279,153,184,226,59,160,88,304,408,33,337,615,20,97,208,334,560,306,142,133,19,274,56,167,104,206,366,56,4476,392,222,272,119,255,362,23,239,292,357);

//Second argument is the sample weight, where index 0 is the weight for $inputs[0], etc
$weights = array(795,948,1665,8134,2140,9884,14275,7242,7601,4160,14228,12147,2082,7071,20988,7889,6480,795,10062,2740,5415,8391,14312,14228,7791,2184,5522,14275,12649,3866,4346,4774,6083,15805,10875,5112,1444,20988,8224,14037,14312,7071,2328,10321,963,948,9547,8883,2870,1352,7403,7451,3181,6652,6439,3268,18789,3064,800,3913,7062,2991,10321,9343,9874,4550,4782,2991,5415,9547,2328,3622,3181,1444,6652,2740,4071,2450,4335,1351,2930,948,3622,7062,35714,1931,11584,7442,7986,9206,2450,18132,4346,1369,1448,5272,4931,9327,4445,21477);

//The two arrays must match lengths, otherwise a fatal error is thrown
$ws = new weightedstats($inputs, $weights);

print
"<html><body><h1>Weighted Statistics Class Example</h1><dl>";
print
"<dt>Arithmetic Mean</dt>";
print
"<dd>The following is the <b>incorrect</b> mean for a weighted data set: ";
print
array_sum($inputs)/count($inputs);
print
"</dd><dt>Weighted Mean</dt>";
print
"<dd>This is the correct way to perform the mean when the data provided has statistical weights: ";
print
$ws->average();
print
"</dd><dt>Standard Deviation</dt><dd>";
print
$ws->stdev();
print
"</dd><dt>Quartile 1</dt><dd>";
print
$ws->percentile(25);
print
"</dd><dt>Median</dt><dd>";
print
$ws->percentile(50);
print
"</dd><dt>Quartile 3</dt><dd>";
print
$ws->percentile(75);
print
"</dd><dt>Skew</dt><dd>";
print
$ws->skew();
print
"</dd></dl></body></html>";
?>


Details

Many datasets have individual points which have differing weights than other points in the set. These datasets will include that information in a Weight column. Attempting to calculate statistics in a traditional way on such data will give incorrect results. This class has packaged the modified methods for common stats so they can be reliably performed when needed.

  Files folder image Files  
File Role Description
Accessible without login Plain text file readme Doc. readme
Accessible without login Plain text file weightedexample.php Example Example
Plain text file weightedstats.php Class Source File

 Version Control Unique User Downloads Download Rankings  
 0%
Total:126
This week:0
All time:9,419
This week:560Up