pgquant
pgquant · Quantitative finance returns, risk, volatility, covariance and factor construction
Overview
PostgreSQL · 14, 15, 16, 17
Usage
Sources:
pgquant computes simple/log returns, cumulative and annualized returns, and historical, Gaussian and Student-t VaR/ES. Version 0.7.1 also provides volatility, covariance and factor construction. WML momentum and unconstrained mean-variance optimization are implemented; long-only optimization and Black-Litterman models are implemented.
Core Workflow
Operational Boundaries
Upstream documents PostgreSQL 14–17. The control file permits non-superuser installation but does not mark the extension trusted. No preload is specified. Query-based functions require ordered symbol, date and double-precision price columns; the first observation per symbol has no return. Supply trusted SQL text. Confidence levels, distribution assumptions and input validation affect results; Student-t ES requires degrees of freedom greater than one.
Volatility and Covariance
pgquant_rolling_vol uses an ordered returns query; pgquant_ewma_vol returns an array and pgquant_ewma_lambda_mle estimates its decay parameter. pgquant_garch11_normal and pgquant_garch11_t fit GARCH parameters and require at least ten observations. pgquant_sample_cov and pgquant_shrinkage_cov_lw return asset-pair covariance from complete shared dates, taking a query with symbol, date and double-precision return columns. Input ordering, missing data and distribution assumptions affect the estimates.
Factor Construction
pgquant_portfolio_sort(query, num_buckets) takes exactly symbol TEXT, date DATE and characteristic_value DOUBLE PRECISION columns and returns symbol, date and bucket; bucket 1 contains the lowest values. pgquant_construct_smb(query) and pgquant_construct_hml(query) consume symbol, date, return DOUBLE PRECISION, size_bucket INT, btm_bucket INT and weight DOUBLE PRECISION. Size buckets are 1/2; book-to-market buckets are 1/2/3. Returns are date with smb_return or hml_return. Prepare a correctly typed, aligned panel and supply trusted SQL text.
WML Momentum Factor
pgquant_construct_wml(query) consumes symbol TEXT, date DATE, return DOUBLE PRECISION, size_bucket INT, mom_bucket INT and weight DOUBLE PRECISION columns. Momentum buckets are 1 for losers, 2 for neutral and 3 for winners; output is date and wml_return. Build a correctly typed size/momentum panel and supply trusted SQL. Version 0.3.0 includes WML, and unconstrained portfolio optimization is available.
Unconstrained Portfolio Optimization
pgquant_mv_optimize_unconstrained takes expected returns, a flattened row-major covariance matrix, a risk-free rate and positive risk aversion. It returns risky-asset weights; they may be negative and need not sum to one. The residual allocation is to the risk-free asset. This is not a long-only optimizer.
Portfolio Optimization in 0.5.1
Long-only optimization uses constrained quadratic programming. Black-Litterman functions compute posterior returns/covariance, and 0.5.1 adds Idzorek view-confidence calibration. Risk parity and portfolio risk decomposition in 0.7.1 are described below. Supply dimensionally consistent arrays and valid covariance/confidence inputs. These numerical models do not establish the suitability of a portfolio.
Risk Parity and Attribution in 0.7.1
pgquant_risk_parity_optimize accepts a flattened row-major covariance matrix and returns normalized equal-risk-contribution weights. pgquant_risk_decomposition accepts portfolio weights and the covariance matrix and reports portfolio volatility plus marginal, component and percentage risk contributions. Supply dimensionally consistent, valid covariance inputs; mathematical outputs are not investment recommendations.