{"id":18001611,"url":"https://github.com/rigtorp/statkit","last_synced_at":"2025-03-26T08:30:51.888Z","repository":{"id":18139902,"uuid":"21228198","full_name":"rigtorp/statkit","owner":"rigtorp","description":"Statistics toolkit for JavaScript","archived":false,"fork":false,"pushed_at":"2014-12-03T05:44:44.000Z","size":176,"stargazers_count":51,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T00:31:29.337Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rigtorp.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}},"created_at":"2014-06-26T04:39:37.000Z","updated_at":"2024-02-23T19:56:56.000Z","dependencies_parsed_at":"2022-09-07T15:12:27.592Z","dependency_job_id":null,"html_url":"https://github.com/rigtorp/statkit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2Fstatkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2Fstatkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2Fstatkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rigtorp%2Fstatkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rigtorp","download_url":"https://codeload.github.com/rigtorp/statkit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245618576,"owners_count":20645026,"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":[],"created_at":"2024-10-29T23:18:08.808Z","updated_at":"2025-03-26T08:30:51.523Z","avatar_url":"https://github.com/rigtorp.png","language":"JavaScript","readme":"statkit\n=======\n\nA statistics toolkit for javascript.\n\nUsage\n=====\n\nInstall using [npm](https://npmjs.org):\n```\nnpm install statkit\n```\n\nFit a linear regression model using [MCMC](http://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo):\n\n```javascript\nvar sk = require(\"statkit.js\");\n\n// log-likelihood for the model y ~ N(m*x + b, 1/t)\nfunction lnlike(theta, x, y) {\n  var m = theta[0], b = theta[1], t = theta[2];\n  var s = 0.0;\n  for (var i = 0; i \u003c x.length; i++) {\n    var r = y[i] - (m * x[i] + b);\n    s += r*r*t - Math.log(t);\n  }\n  return -0.5*s;\n}\n\n// uniform log-prior for m, b, t\nfunction lnprior(theta) {\n  var m = theta[0], b = theta[1], t = theta[2];\n  if (0.0 \u003c m \u0026\u0026 m \u003c 1.0 \u0026\u0026 0.0 \u003c b \u0026\u0026 b \u003c 10.0 \u0026\u0026 0.0 \u003c t \u0026\u0026 t \u003c 100.0) {\n    return 0.0;\n  }\n  return -Infinity;\n}\n\n// posterior log-probability function\nfunction lnpost(theta, x, y) {\n  var lp = lnprior(theta);\n  if (!isFinite(lp)) {\n    return -Infinity;\n  }\n  return lp + lnlike(theta, x, y);\n}\n\nvar x = [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5];\nvar y = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68];\n\nvar res = sk.metropolis(function(theta) { return lnpost(theta, x, y); },\n  [0.5, 3.0, 1.0], 1000000, 0.1, 50000, 100);\n\nconsole.log('acceptance rate:', res.accepted)\nconsole.log('posteriors (16/50/84 percentiles):')\nconsole.log('m', sk.quantile(res.chain[0], 0.16),\n  sk.median(res.chain[0]), sk.quantile(res.chain[0], 0.84))\nconsole.log('b', sk.quantile(res.chain[1], 0.16),\n  sk.median(res.chain[1]), sk.quantile(res.chain[1], 0.84))\nconsole.log('t', sk.quantile(res.chain[2], 0.16),\n  sk.median(res.chain[2]), sk.quantile(res.chain[2], 0.84))\n```\n\nCalculate a confidence interval for a correlation using the bootstrap method:\n\n```javascript\nvar sk = require(\"statkit\");\n\nvar lsat = [576, 635, 558, 578, 666, 580, 555,\n            661, 651, 605, 653, 575, 545, 572, 594];\nvar gpa = [3.39, 3.30, 2.81, 3.03, 3.44, 3.07, 3.00,\n           3.43, 3.36, 3.13, 3.12, 2.74, 2.76, 2.88, 2.96];\n\nvar corr = sk.corr(gpa, lsat);\nvar ci = sk.bootci(100000, sk.corr, gpa, lsat);\n\nconsole.log(\"corr = \", corr, \"ci = \", ci);\n```\n\nPerform a linear regression on the first data set in\n[Anscombe's quartet](http://en.wikipedia.org/wiki/Anscombe%27s_quartet):\n\n```javascript\nvar sk = require(\"statkit\");\n\nvar x = [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5];\nvar y = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68];\n\nvar A = new Array(x.length*2);\nfor (var i = 0; i \u003c x.length; ++i) {\n  A[2*i] = 1;\n  A[2*i + 1] = x[i];\n}\nvar b = sk.lstsq(x.length, 2, A, y);\n\nconsole.log(\"intercept = \", b[0], \"slope = \", b[1]);\n```\n\nFunctions\n=========\n\n* `min(a)` - [Minimum](http://en.wikipedia.org/wiki/Minimum)\n* `max(a)` - [Maximum](http://en.wikipedia.org/wiki/Maximum)\n* `range(a)` - [Range](http://en.wikipedia.org/wiki/Range_(statistics))\n* `quantile(a)` - [Quantile](http://en.wikipedia.org/wiki/Quantile)\n* `median(a)` - [Median](http://en.wikipedia.org/wiki/Median)\n* `iqr(a)` - [Interquartile range](http://en.wikipedia.org/wiki/Interquartile_range)\n* `mean(a)` - [Mean](http://en.wikipedia.org/wiki/Mean)\n* `gmean(a)` - [Geometric mean](http://en.wikipedia.org/wiki/Mean)\n* `hmean(a)` - [Harmonic mean](http://en.wikipedia.org/wiki/Mean)\n* `var(a)` - [Variance](http://en.wikipedia.org/wiki/Variance)\n* `std(a)` - [Standard deviation](http://en.wikipedia.org/wiki/Standard_deviation)\n* `skew(a)` - [Skewness](http://en.wikipedia.org/wiki/Skewness)\n* `kurt(a)` - [Kurtosis](http://en.wikipedia.org/wiki/Kurtosis)\n* `corr(x, y)` - [Correlation](http://en.wikipedia.org/wiki/Correlation) between x and y\n* `entropy(p)` - [Entropy](http://en.wikipedia.org/wiki/Entropy_(information_theory))\n* `kldiv(p, q)` - [Kullback–Leibler divergence](http://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence)\n* `shuffle(a)` - [Shuffle](http://en.wikipedia.org/wiki/Random_permutation) using the [Fisher–Yates shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)\n* `sample(a)` - [Sample](http://en.wikipedia.org/wiki/Sampling_(statistics)) with replacement\n* `boot(nboot, bootfun, data...)` - [Bootstrap](http://en.wikipedia.org/wiki/Bootstrapping_(statistics)) the bootfun statistic\n* `bootci(nboot, bootfun, data...)` - Calculate bootstrap confidence intervals using the normal model\n* `randn()` - Draw random sample from the [standard normal distribution](http://en.wikipedia.org/wiki/Normal_distribution) using the [Marsaglia polar method](http://en.wikipedia.org/wiki/Marsaglia_polar_method)\n* `normcdf(x)` - Normal cumulative distribution function\n* `norminv(p)` - Normal inverse cumulative distribution function\n* `lufactor(A, n)` - Compute pivoted [LU decomposition](http://en.wikipedia.org/wiki/LU_decomposition)\n* `lusolve(LU, p, b)` - Solve `Ax=b` given the LU factorization of `A`\n* `qrfactor(m, n, A)` - Compute [QR factorization](http://en.wikipedia.org/wiki/QR_decomposition) of A\n* `qrsolve(m, n, QR, tau, b)` - Solve the least squares problem `min ||Ax = b||` using QR factorization `QR` of `A`\n* `lstsq(m, n, A, b)` - Solve the [least squares problem](http://en.wikipedia.org/wiki/Least_squares) `min ||Ax = b||`\n* `metropolis(lnpost, p, iterations, scale, burn, thin)` - Sample from `lnpost` starting at `p` using the [Metropolis-Hastings algorithm](http://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm)\n\nCredits\n=======\n(c) 2014 Erik Rigtorp \u003cerik@rigtorp.se\u003e. MIT License\n","funding_links":[],"categories":["Javascript","JavaScript"],"sub_categories":["Tools","[Tools](#tools-1)","Speech Recognition"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frigtorp%2Fstatkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frigtorp%2Fstatkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frigtorp%2Fstatkit/lists"}