weighted_statistics
High-performance weighted statistics functions for sparse data
Repository
schmidni/pg_weighted_statistics
https://github.com/schmidni/pg_weighted_statistics
Source
pg_weighted_statistics-1.0.0.tar.gz
pg_weighted_statistics-1.0.0.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_weighted_statistics | 1.0.0 | FUNC | PostgreSQL | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4680 | weighted_statistics | No | Yes | No | Yes | Yes | Yes | - |
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.0.0 | 1817161514 | pg_weighted_statistics | - |
| RPM | PIGSTY | 1.0.0 | 1817161514 | pg_weighted_statistics_$v | - |
| DEB | PIGSTY | 1.0.0 | 1817161514 | postgresql-$v-weighted-statistics | - |
Build
You can build the RPM / DEB packages for pg_weighted_statistics using pig build:
pig build pkg pg_weighted_statistics # build RPM / DEB packages
Install
You can install pg_weighted_statistics directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
pig repo add pgsql -u # Add repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install pg_weighted_statistics; # Install for current active PG version
pig ext install -y pg_weighted_statistics -v 18 # PG 18
pig ext install -y pg_weighted_statistics -v 17 # PG 17
pig ext install -y pg_weighted_statistics -v 16 # PG 16
pig ext install -y pg_weighted_statistics -v 15 # PG 15
pig ext install -y pg_weighted_statistics -v 14 # PG 14
dnf install -y pg_weighted_statistics_18 # PG 18
dnf install -y pg_weighted_statistics_17 # PG 17
dnf install -y pg_weighted_statistics_16 # PG 16
dnf install -y pg_weighted_statistics_15 # PG 15
dnf install -y pg_weighted_statistics_14 # PG 14
apt install -y postgresql-18-weighted-statistics # PG 18
apt install -y postgresql-17-weighted-statistics # PG 17
apt install -y postgresql-16-weighted-statistics # PG 16
apt install -y postgresql-15-weighted-statistics # PG 15
apt install -y postgresql-14-weighted-statistics # PG 14
Create Extension:
CREATE EXTENSION weighted_statistics;
Usage
weighted_statistics: weighted statistical functions for PostgreSQL
High-performance C extension providing weighted statistical functions with automatic sparse data handling (when sum(weights) < 1.0).
CREATE EXTENSION weighted_statistics;
Functions
| Function | Description |
|---|---|
weighted_mean(values[], weights[]) | Weighted mean |
weighted_variance(values[], weights[], ddof) | Weighted variance (ddof: 0=population, 1=sample) |
weighted_std(values[], weights[], ddof) | Weighted standard deviation |
weighted_quantile(values[], weights[], quantiles[]) | Empirical CDF quantiles |
wquantile(values[], weights[], quantiles[]) | Type 7 (Hyndman-Fan) quantiles |
whdquantile(values[], weights[], quantiles[]) | Harrell-Davis quantiles |
weighted_median(values[], weights[]) | 50th percentile shortcut (empirical CDF) |
Examples
-- Weighted mean
SELECT weighted_mean(ARRAY[1.0, 2.0, 3.0], ARRAY[0.2, 0.3, 0.5]);
-- 2.3
-- Weighted quantiles
SELECT weighted_quantile(ARRAY[10.0, 20.0, 30.0], ARRAY[0.3, 0.4, 0.3], ARRAY[0.25, 0.5, 0.75]);
-- {15.0, 20.0, 25.0}
-- Sparse data (auto-adds implicit zeros when sum(weights) < 1.0)
SELECT weighted_mean(ARRAY[10, 20], ARRAY[0.2, 0.3]);
-- 8.0 (equivalent to weighted_mean(ARRAY[10, 20, 0, 0], ARRAY[0.2, 0.3, 0.25, 0.25]))
-- Multiple statistics
SELECT weighted_mean(vals, weights),
weighted_std(vals, weights, 1),
weighted_quantile(vals, weights, ARRAY[0.05, 0.95])
FROM (SELECT array_agg(val) AS vals, array_agg(weight) AS weights FROM data) t;
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.