{"id":15047999,"url":"https://github.com/kthohr/stats","last_synced_at":"2025-04-05T00:10:23.123Z","repository":{"id":46970393,"uuid":"95018772","full_name":"kthohr/stats","owner":"kthohr","description":"A C++ header-only library of statistical distribution functions.","archived":false,"fork":false,"pushed_at":"2023-05-14T13:04:46.000Z","size":1699,"stargazers_count":541,"open_issues_count":2,"forks_count":73,"subscribers_count":30,"default_branch":"master","last_synced_at":"2025-03-28T23:09:11.946Z","etag":null,"topics":["armadillo","armadillo-library","blaze","cdf","constexpr","cpp","cpp11","density-functions","distributions","eigen","eigen3","numerical-methods","openmp","probability","quantile","quantile-functions","statistics","stats"],"latest_commit_sha":null,"homepage":"https://statslib.readthedocs.io/en/latest/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kthohr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-06-21T15:28:39.000Z","updated_at":"2025-03-22T16:12:38.000Z","dependencies_parsed_at":"2022-08-12T13:11:23.191Z","dependency_job_id":"8d2232e7-d9b2-4369-bd6b-fcf5aa97c214","html_url":"https://github.com/kthohr/stats","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kthohr%2Fstats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kthohr%2Fstats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kthohr%2Fstats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kthohr%2Fstats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kthohr","download_url":"https://codeload.github.com/kthohr/stats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266565,"owners_count":20910836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["armadillo","armadillo-library","blaze","cdf","constexpr","cpp","cpp11","density-functions","distributions","eigen","eigen3","numerical-methods","openmp","probability","quantile","quantile-functions","statistics","stats"],"created_at":"2024-09-24T21:06:44.390Z","updated_at":"2025-04-05T00:10:23.104Z","avatar_url":"https://github.com/kthohr.png","language":"C++","readme":"# StatsLib \u0026nbsp; [![Mentioned in Awesome Cpp](https://awesome.re/mentioned-badge.svg)](https://github.com/fffaraz/awesome-cpp#math) [![Build Status](https://github.com/kthohr/stats/actions/workflows/main.yml/badge.svg)](https://github.com/kthohr/stats/actions/workflows/main.yml) [![Coverage Status](https://codecov.io/github/kthohr/stats/coverage.svg?branch=master)](https://codecov.io/github/kthohr/stats?branch=master) [![License](https://img.shields.io/badge/Licence-Apache%202.0-blue.svg)](./LICENSE) [![Documentation Status](https://readthedocs.org/projects/statslib/badge/?version=latest)](https://statslib.readthedocs.io/en/latest/?badge=latest)\n\n\nStatsLib is a templated C++ library of statistical distribution functions, featuring unique compile-time computing capabilities and seamless integration with several popular linear algebra libraries.\n\nFeatures:\n* A header-only library of probability density functions, cumulative distribution functions, quantile functions, and random sampling methods.\n* Functions are written in C++11 `constexpr` format, enabling the library to operate as both a compile-time and run-time computation engine.\n* Designed with a simple **R**-like syntax.\n* Optional vector-matrix functionality with wrappers to support:\n    * STL Vectors (`std::vector`)\n    * [Armadillo](http://arma.sourceforge.net/)\n    * [Blaze](https://bitbucket.org/blaze-lib/blaze)\n    * [Eigen](http://eigen.tuxfamily.org/index.php)\n* Matrix-based operations are parallelizable with OpenMP.\n* Released under a permissive, non-GPL license.\n\n### Contents:\n* [Distributions](#distributions)\n* [Installation](#installation-and-depdencies)\n* [Documentation](#documentation)\n* [Jupyter Notebook](#jupyter-notebook)\n* [Options](#Options)\n* [Syntax and Examples](#syntax-and-examples)\n* [Compile-time Computation Capabilities](#compile-time-computing-capabilities)\n* [Author and License](#author)\n\n## Distributions\n\nFunctions to compute the cdf, pdf, quantile, as well as random sampling methods, are available for the following distributions:\n\n* Bernoulli\n* Beta\n* Binomial\n* Cauchy\n* Chi-squared\n* Exponential\n* F\n* Gamma\n* Inverse-Gamma\n* Inverse-Gaussian\n* Laplace\n* Logistic\n* Log-Normal\n* Normal (Gaussian)\n* Poisson\n* Rademacher\n* Student's t\n* Uniform\n* Weibull\n\nIn addition, pdf and random sampling functions are available for several multivariate distributions:\n\n* inverse-Wishart\n* Multivariate Normal\n* Wishart\n\n## Installation and Dependencies\n\nStatsLib is a header-only library. Simply add the header files to your project using\n```cpp\n#include \"stats.hpp\"\n```\n\nThe only dependency is the latest version of [GCEM](https://github.com/kthohr/gcem) and a C++11 compatible compiler.\n\n## Documentation\n\nFull documentation is available online:\n\n[![Documentation Status](https://readthedocs.org/projects/statslib/badge/?version=latest)](https://statslib.readthedocs.io/en/latest/?badge=latest)\n\nA PDF version of the documentation is available [here](https://buildmedia.readthedocs.org/media/pdf/statslib/latest/statslib.pdf).\n\n## Jupyter Notebook\n\nYou can test the library online using an interactive Jupyter notebook: \n\n[![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/kthohr/stats/master?filepath=notebooks%2Fstats.ipynb)\n\n## Options\n\nThe following options should be declared **before** including the StatsLib header files. \n\n* For inline-only functionality (i.e., no `constexpr` specifiers):\n```cpp\n#define STATS_GO_INLINE\n```\n\n* OpenMP functionality is enabled by default if the `_OPENMP` macro is detected (e.g., by invoking `-fopenmp` with GCC or Clang). To explicitly enable OpenMP features use:\n```cpp\n#define STATS_USE_OPENMP\n```\n\n* To disable OpenMP functionality:\n```cpp\n#define STATS_DONT_USE_OPENMP\n```\n\n* To use StatsLib with Armadillo, Blaze or Eigen:\n```cpp\n#define STATS_ENABLE_ARMA_WRAPPERS\n#define STATS_ENABLE_BLAZE_WRAPPERS\n#define STATS_ENABLE_EIGEN_WRAPPERS\n```\n\n* To enable wrappers for `std::vector`:\n```cpp\n#define STATS_ENABLE_STDVEC_WRAPPERS\n```\n\n## Syntax and Examples\n\nFunctions are called using an **R**-like syntax. Some general rules:\n\n* density functions: `stats::d*`. For example, the Normal (Gaussian) density is called using\n``` cpp\nstats::dnorm(\u003cvalue\u003e,\u003cmean parameter\u003e,\u003cstandard deviation\u003e);\n```\n* cumulative distribution functions: `stats::p*`. For example, the Gamma CDF is called using\n``` cpp\nstats::pgamma(\u003cvalue\u003e,\u003cshape parameter\u003e,\u003cscale parameter\u003e);\n```\n* quantile functions: `stats::q*`. For example, the Beta quantile is called using\n``` cpp\nstats::qbeta(\u003cvalue\u003e,\u003ca parameter\u003e,\u003cb parameter\u003e);\n```\n* random sampling: `stats::r*`. For example, to generate a single draw from the Logistic distribution:\n``` cpp\nstats::rlogis(\u003clocation parameter\u003e,\u003cscale parameter\u003e,\u003cseed value or random number engine\u003e);\n```\n\n\u003cbr\u003e\n\nAll of these functions have matrix-based equivalents using Armadillo, Blaze, and Eigen dense matrices.\n\n* The pdf, cdf, and quantile functions can take matrix-valued arguments. For example,\n\n```cpp\n// Using Armadillo:\narma::mat norm_pdf_vals = stats::dnorm(arma::ones(10,20),1.0,2.0);\n```\n\n* The randomization functions (`r*`) can output random matrices of arbitrary size. For example, For example, the following code will generate a 100-by-50 matrix of iid draws from a Gamma(3,2) distribution:\u003c/li\u003e\n\n```cpp\n// Armadillo:\narma::mat gamma_rvs = stats::rgamma\u003carma::mat\u003e(100,50,3.0,2.0);\n\n// Blaze:\nblaze::DynamicMatrix\u003cdouble\u003e gamma_rvs = stats::rgamma\u003cblaze::DynamicMatrix\u003cdouble\u003e\u003e(100,50,3.0,2.0);\n\n// Eigen:\nEigen::MatrixXd gamma_rvs = stats::rgamma\u003cEigen::MatrixXd\u003e(100,50,3.0,2.0);\n```\n\n* All matrix-based operations are parallelizable with OpenMP. For GCC and Clang compilers, simply include the `-fopenmp` option during compilation.\n\n### Seeding Values\n\nRandom number seeding is available in two forms: seed values and random number engines.\n\n* Seed values are passed as unsigned integers. For example, to generate a draw from a normal distribution N(1,2) with seed value 1776:\n``` cpp\nstats::rnorm(1,2,1776);\n```\n* Random engines in StatsLib use the 64-bit Mersenne-Twister generator (`std::mt19937_64`) and are passed by reference. Example:\n``` cpp\nstd::mt19937_64 engine(1776);\nstats::rnorm(1,2,engine);\n```\n\n### Examples\n\nMore examples with code:\n```cpp\n// evaluate the normal PDF at x = 1, mu = 0, sigma = 1\ndouble dval_1 = stats::dnorm(1.0,0.0,1.0);\n \n// evaluate the normal PDF at x = 1, mu = 0, sigma = 1, and return the log value\ndouble dval_2 = stats::dnorm(1.0,0.0,1.0,true);\n \n// evaluate the normal CDF at x = 1, mu = 0, sigma = 1\ndouble pval = stats::pnorm(1.0,0.0,1.0);\n \n// evaluate the Laplacian quantile at p = 0.1, mu = 0, sigma = 1\ndouble qval = stats::qlaplace(0.1,0.0,1.0);\n\n// draw from a t-distribution dof = 30\ndouble rval = stats::rt(30);\n\n// matrix output\narma::mat beta_rvs = stats::rbeta\u003carma::mat\u003e(100,100,3.0,2.0);\n\n// matrix input\narma::mat beta_cdf_vals = stats::pbeta(beta_rvs,3.0,2.0);\n```\n\n## Compile-time Computing Capabilities\n\nStatsLib is designed to operate equally well as a compile-time computation engine. Compile-time computation allows the compiler to replace function calls (e.g., `dnorm(0,0,1)`) with static values in the source code. That is, functions are evaluated during the compilation process, rather than at run-time. This capability is made possible due to the templated `constexpr` design of the library and can be verified by inspecting the assembly code generated by the compiler. \n\nThe compile-time features are enabled using the `constexpr` specifier. The example below computes the pdf, cdf, and quantile function of the Laplace distribution.\n```cpp\n#include \"stats.hpp\"\n\nint main()\n{\n    \n    constexpr double dens_1  = stats::dlaplace(1.0,1.0,2.0); // answer = 0.25\n    constexpr double prob_1  = stats::plaplace(1.0,1.0,2.0); // answer = 0.5\n    constexpr double quant_1 = stats::qlaplace(0.1,1.0,2.0); // answer = -2.218875...\n\n    return 0;\n}\n```\nAssembly code generated by Clang without any optimization:\n```assembly\nLCPI0_0:\n\t.quad\t-4611193153885729483    ## double -2.2188758248682015\nLCPI0_1:\n\t.quad\t4602678819172646912     ## double 0.5\nLCPI0_2:\n\t.quad\t4598175219545276417     ## double 0.25000000000000006\n\t.section\t__TEXT,__text,regular,pure_instructions\n\t.globl\t_main\n\t.p2align\t4, 0x90\n_main:                                  ## @main\n\tpush\trbp\n\tmov\trbp, rsp\n\txor\teax, eax\n\tmovsd\txmm0, qword ptr [rip + LCPI0_0] ## xmm0 = mem[0],zero\n\tmovsd\txmm1, qword ptr [rip + LCPI0_1] ## xmm1 = mem[0],zero\n\tmovsd\txmm2, qword ptr [rip + LCPI0_2] ## xmm2 = mem[0],zero\n\tmov\tdword ptr [rbp - 4], 0\n\tmovsd\tqword ptr [rbp - 16], xmm2\n\tmovsd\tqword ptr [rbp - 24], xmm1\n\tmovsd\tqword ptr [rbp - 32], xmm0\n\tpop\trbp\n\tret\n```\n\n## Author\n\nKeith O'Hara\n\n## License\n\nApache Version 2\n","funding_links":[],"categories":["Math","进程间通信","Statistics"],"sub_categories":["数学"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkthohr%2Fstats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkthohr%2Fstats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkthohr%2Fstats/lists"}