weighted_statistics
weighted_statistics
pg_weighted_statistics : High-performance weighted statistics functions for sparse data
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4680 | weighted_statistics | pg_weighted_statistics | 1.0.0 | FUNC | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-r | No | Yes | No | Yes | yes | no |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.0.0 | 18 17 16 15 14 | pg_weighted_statistics | - |
| RPM | PIGSTY | 1.0.0 | 18 17 16 15 14 | pg_weighted_statistics_$v | - |
| DEB | PIGSTY | 1.0.0 | 18 17 16 15 14 | postgresql-$v-weighted-statistics | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el8.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el9.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el9.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el10.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
el10.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
d12.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
d12.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
d13.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
d13.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
u22.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
u22.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
u24.x86_64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
u24.aarch64 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 | PIGSTY 1.0.0 |
Source
pig build pkg pg_weighted_statistics;# build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pg_weighted_statistics;# install via package name, for the active PG version pig install weighted_statistics;# install by extension name, for the current active PG version pig install weighted_statistics -v 18; # install for PG 18 pig install weighted_statistics -v 17; # install for PG 17 pig install weighted_statistics -v 16; # install for PG 16 pig install weighted_statistics -v 15; # install for PG 15 pig install weighted_statistics -v 14; # install for PG 14Create this extension with:
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;Last updated on