定制 richjenks/stats 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

richjenks/stats

Composer 安装命令:

composer require richjenks/stats

包简介

Statistics library for non-statistical people

README 文档

README

Statistics library for non-statistical people

Introduction

If you're into statistics then PHP will not be your language of choice (try R instead) but if for any reason you, a non-statistician, need to do some stats then this library aims to provide a simple set of methods for common statistical functions.

By design, with the exception of statistical tests, functions generally accept a single series of data at a time. This is to keep the library simple to use

Many of the methods in this library are available from the Statistics Extension, however this is not included in PHP by default. If possible, I'd recommend using this extension rather than my stats library.

Installation

  1. Install with Composer: composer require richjenks/stats
  2. Include autoloader: require 'vendor/autoload.php';
  3. All static methods are available from the RichJenks\Stats\Stats class

Quickstart

<?php
require 'vendor/autoload.php';
use RichJenks\Stats\Stats;
echo Stats::mean([1, 2, 3]);
// 2

Stats will generally return either a float or an array, whichever is most appropriate for the function

Usage

Mean/Average

Calculates the mean/average of given data:

Stats::mean([1, 2, 3]);
// 2

Stats::mean([15, 1000, 68.5, 9]);
// 273.125

The average function aliases mean, e.g. Stats::average([1, 2, 3]); also returns 2

Median

Calculates the median (middle value) of given data:

Stats::median([1, 2, 3, 4]);
// 2.5

Stats::median([3.141, 1.618, 1.234]);
// 1.618

Mode

Calculates the mode(s) — most common value(s) — of given data:

Stats::mode([1, 2, 2, 3]);
// [2]

`Stats::mode([1, 2, 2, 3, 3]);
// [2, 3]

This function always return an array because it is able to handle multi-modal data and an empty array would mean there is no mode

Frequencies

Constructs a sorted array of frequencies for each value in a series:

Stats::frequencies([1, 2, 3]);
// [
//   1 => 1,
//   2 => 1,
//   3 => 1,
// ]

Stats::frequencies([10, 20, 20]);
// [
//   20 => 2,
//   10 => 1,
// ]

Range

Determines the range (highest minus lowest) of given data:

Stats::range([1, 9]);
// 8

Stats::range([-41, 1.61803]);
// 42.61803

Variance & Standard Deviation

These functions calculate:

  • Variance: square of average variation from the mean
  • Standard Deviation: average variation from the mean (square root of Variance)
$data = [1, 2, 3, 4, 5];

Stats::variance($data);
// 2.5

Stats::sd($data);
// 1.5811388301

Individual Deviations

The deviations function is also available if you require the deviations for each individual value, for example:

Stats::deviations([1, 2, 3, 4, 5]);
// [
//   1 => 4,
//   2 => 1,
//   3 => 0,
//   4 => 1,
//   5 => 4,
// ]

Stats::deviations([42, 75, 101, 22.5, 18]);
// [
//   42   => 94.09,
//   75   => 542.89,
//   101  => 2430.49,
//   22.5 => 852.64,
//   18   => 1135.69,
// ]

Sample or Population

Sample is the default mode for Variance and Standard Deviation but if you're unsure of the effect this decision has on your data then you probably don't need it and can skip this section.

Definitions

Population Every subject applicable, e.g. people who wear glasses or non-extinct species of frog

Sample The subset of subjects for which data is available, e.g. 100 glass-wearing subjects or a dozen species of frog

You can optionally pass the constants Stats::Sample or Stats::POPULATION as second parameters to determine whether your data is for a sample or a whole population:

$data = [1, 2, 3, 4, 5];

Stats::variance($data, Stats::POPULATION);
// 2

Stats::sd($data, Stats::POPULATION);
// 1.4142135624

Standard Error of the Mean

Estimates how well the sample mean approximates the population mean:

Stats::sem([1, 2, 3, 4, 5]);
// 0.70710678118655

Quartiles, Interquartile Range & Outliers

These functions calculate the data required to construct a Box Plot which, when you understand what each data point means, is a concise way of displaying and comparing data sets.

Quartiles

Calculates Quartiles 0—4, where:

  • 0 is the lowest data point
  • 1 is Q¹
  • 2 is Q² (the median)
  • 3 is Q³
  • 4 is the highest data point
Stats::quartiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
// [
//   0 => 1,
//   1 => 3.5,
//   2 => 6.5,
//   3 => 9.5,
//   4 => 12,
// ]

Stats::quartiles([839, 560, 607, 828, 875, 805, 646, 450, 930, 443])
// [
//   0 => 443,
//   1 => 560,
//   2 => 725.5,
//   3 => 839,
//   4 => 930,
// ]

Interquartile Range

Calculates the range between Q¹ and Q³ (the middle 50% of data):

Stats::iqr([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
// 6

Stats::iqr([839, 560, 607, 828, 875, 805, 646, 450, 930, 443])
// 279

Outliers

Determines which values in a series are outliers (too far from the other values so sometimes omitted from the data set, possibly due to experimental error):

Stats::outliers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// []

Stats::outliers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 999])
// [999]

Inliers

Determines which values in a series are not outliers, i.e. removes outliers:

Stats::inliers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 999])
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Whiskers

Determines the lower and upper limit for identifying outliers:

Stats::whiskers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 999])
// ['lower' => -6, 'upper' => 18]

Percentiles

All percentile functions accept an optional additional parameter for rounding that works as follows:

  • If omitted, percentages are rounded to the nearest whole
  • If a positive integer, percentages are rounded to that many decimal places
  • If a negative integer (e.g. -1), percentages are not rounded

All Percentiles

Determines the percentile of each value:

// Closest Rank
Stats::percentiles([15, 20, 35, 40, 50]);
// [
//   15 => 0,
//   20 => 14,
//   35 => 57,
//   40 => 71,
//   50 => 100,
// ]

Single Percentile

Determines the value closest to the given percentile:

Stats::percentile([15, 20, 35, 40, 50], 75);
// [
//   'value'      => 40,
//   'percentile' => 71,
// ]

Intra-Percentile

Determines the values that fall in the given percentile, i.e. the lowest x% of all values:

Stats::intrapercentile([15, 20, 35, 40, 50], 60);
// [
//   15 => 0,
//   20 => 14,
//   35 => 57,
// ]

CLI

CLI usage is supported via the included scli (Stats Command Line Interface) file and simply expects the name of the required method followed by its arguments:

./scli mean 1 2 3
# 2

./scli inliers 1 2 3 4 5 999
# 1,2,3,4,5

In cases where the result is a set (i.e. an array) it is presented as comma-separated

Unit Tests

phpunit --bootstrap Stats.php tests/StatsTest

richjenks/stats 适用场景与选型建议

richjenks/stats 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 153.63k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2020 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「stats」 「statistics」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 richjenks/stats 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 richjenks/stats 我们能提供哪些服务?
定制开发 / 二次开发

基于 richjenks/stats 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 153.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 11
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-27