{"id":19245095,"url":"https://github.com/rxtoolkit/stats","last_synced_at":"2025-02-23T15:15:44.522Z","repository":{"id":214912874,"uuid":"737663052","full_name":"rxtoolkit/stats","owner":"rxtoolkit","description":"📊 RxJS operators for reactive statistics","archived":false,"fork":false,"pushed_at":"2024-01-01T18:14:33.000Z","size":84,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-05T09:16:13.062Z","etag":null,"topics":["data-science","fp","functional-programming","observables","package","reactive-programming","rxjs","statistics"],"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/rxtoolkit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-01T01:20:44.000Z","updated_at":"2024-01-02T15:41:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"5ca7b300-0cc8-4a51-9043-954f6e358893","html_url":"https://github.com/rxtoolkit/stats","commit_stats":null,"previous_names":["rxtoolkit/stats"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rxtoolkit%2Fstats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rxtoolkit%2Fstats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rxtoolkit%2Fstats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rxtoolkit%2Fstats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rxtoolkit","download_url":"https://codeload.github.com/rxtoolkit/stats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240331361,"owners_count":19784646,"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":["data-science","fp","functional-programming","observables","package","reactive-programming","rxjs","statistics"],"created_at":"2024-11-09T17:26:37.365Z","updated_at":"2025-02-23T15:15:44.490Z","avatar_url":"https://github.com/rxtoolkit.png","language":"JavaScript","readme":"# @rxtk/stats\n\u003e 📊 RxJS operators for reactive statistics\n\n```bash\nnpm i @rxtk/stats\n```\n\n```bash\nyarn add @rxtk/stats\n```\n\n## API\n\n### `accuracy([initialState={truePositives: 0, falsePositives: 0, trueNegatives: 0, falseNegatives: 0}])`\n```js\nimport { from } from 'rxjs';\nimport { accuracy } from '@rxtk/stats';\n\nconst items = [\n  [0, 1], // [trueLabel, prediction]\n  [0, 1],\n  [0, 1],\n  [0, 0],\n  [0, 0],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n  [1, 0],\n];\n\nconst accuracy$ = from(items).pipe(\n  accuracy()\n);\n\naccuracy$.subscribe(console.log);\n// Output\n// 0\n// 0\n// 0\n// 0.25\n// 0.40\n// 0.50\n// 0.5714285714\n// 0.625\n// 0.6666666667\n// 0.6\n```\n\n### `change()`\n```js\nimport { from } from 'rxjs';\nimport { change } from '@rxtk/stats';\n\nconst num$ = from([2, 5, 9, 16, 26]);\nconst change$ = num$.pipe(\n  change()\n);\nchange$.subscribe(console.log);\n// Output:\n// 3\n// 4\n// 7\n// 10\n```\n\n### `countValues([initialState={valueCounts: {}, keyCount: 0}])`\n```js\nimport { from } from 'rxjs';\nimport { countValues } from '@rxtk/stats';\n\nconst item$ = from([1, 2, 2, 2, 'foo', 'foo']);\nconst counts$ = item$.pipe(\n  countValues()\n);\ncounts$.subscribe(console.log);\n// Output:\n// [{value: 1, count: 1}]\n// [{value: 1, count: 1}, {value: 2, count: 1}]\n// [{value: 1, count: 1}, {value: 2, count: 2}]\n// [{value: 1, count: 1}, {value: 2, count: 3}]\n// [{value: 1, count: 1}, {value: 2, count: 1}, {value: 'foo', count: 1}]\n// [{value: 1, count: 1}, {value: 2, count: 1}, {value: 'foo', count: 2}]\n```\n\n### `dirtyR([initialState={meanState: Object, stdevState: Object}])`\n\u003e Warning: This method is called \"dirty\" because, by default, it will estimate R values using incremental estimates of the sample mean and standard deviation. This provides faster, streamable results but does not guarantee that the R value will be completely correct for all data sets.\n\nEstimates R (the correlation coefficient) of an Observable. It will compute the current sample mean and sample standard deviation of the stream and then use those to estimate R. This allows it to provide estimates quickly and in real-time.\n\nHowever, R values estimated early in the stream will typically be less correct than those estimated later (because the sample mean and variance will be estimated more correctly as more data points are ingested). For most large and randomly sampled datasets, the R value will eventually converge to its true values as more items are ingested over time.\n\n```js\nimport { from } from 'rxjs';\nimport { dirtyR } from '@rxtk/stats';\n\nconst instance$ = from([\n  // [x, y], // where x is a variable/feature and y is the value to predict\n  [600, 75],\n  [470, 60],\n  [170, 15],\n  [430, 40],\n  [300, 30],\n]);\nconst r$ = from(instance$).pipe(\n  dirtyR()\n);\n\nr$.subscribe(console.log);\n// 0.5\n// 0.8684,\n// 0.5723,\n// 0.5130\n\n```\n\n### dirtyZScore([initialState={meanState: Object, stdevState: Object}])\n\u003e Warning: This method is called \"dirty\" because, by default, it will estimate z-score values using incremental estimates of the sample mean and standard deviation. If you want the true (pure) z-score, then you must pass it the true mean and true standard deviation in its initialState.\n\nEstimates z-score (the correlation coefficient) of an Observable.\n\nBy default, it will compute the current sample mean and sample standard deviation of the stream and then use those to estimate the z-score. This allows it to provide estimates quickly and in real-time. Using this approach the entire dataset can be analyzed in just one pass. However, z-score values estimated early in the stream will typically be less correct than those estimated later (because the sample mean and variance will be estimated more correctly as more data points are ingested). For most large and randomly sampled datasets, the z-score value will eventually converge to its true values as more items are ingested over time.\n\nCalculating the true (pure) z-score, requires first calculating the mean and standard deviation of the dataset and passing these values in as part of the initial state. (See example below)\n```js\nimport { from } from 'rxjs';\nimport { dirtyZScore } from '@rxtk/stats';\n\nconst zombiePirateHeight$ = [\n  600,\n  470,\n  170,\n  430,\n  300,\n];\nconst zScore$ = from(zombiePirateHeight$).pipe(\n  dirtyZScore()\n);\n\nzScore$.subscribe(console.log);\n// -0.7071\n// -1.1034\n// 0.0693\n// -0.5707\n```\n\n**Calculate True z-score**:\n\n```js\nimport { from, zip } from 'rxjs';\nimport { mergeMap, takeLast } from 'rxjs/operators';\nimport { dirtyZScore, mean, stdev } from '@rxtk/stats';\n\nconst zombiePirateHeight$ = from([\n  600,\n  470,\n  170,\n  430,\n  300,\n]);\n\nconst mean$ = zombiePirateHeight$.pipe(\n  mean(),\n  takeLast(1)\n);\nconst stdev$ = zombiePirateHeight$.pipe(\n  stdev(),\n  takeLast(1)\n);\nconst trueZScore$ = zip(mean$, stdev$).pipe(\n  mergeMap(([trueMean, trueStdev]) =\u003e (\n    zombiePirateHeight$.pipe(\n      dirtyZScore({trueMean, trueStdev})\n    )\n  ))\n);\n\ntrueZScore$.subscribe(console.log);\n```\n\n### `f1([initialState={truePositives: 0, falsePositives: 0, falseNegatives: 0}])`\nGiven an Observable of ground-truth labels and predictions, the f1 operator returns the f1 score.\n\n```js\nimport { from } from 'rxjs';\nimport { skip } from 'rxjs/operators';\nimport { f1 } from '@rxtk/stats';\n\nconst items = [\n  [0, 1], // [trueLabel, predictedLabel]\n  [1, 0],\n  [1, 0],\n  [0, 0],\n  [0, 0],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n];\n\nconst f1$ = from(items).pipe(\n  f1(),\n  skip(4)\n);\n\nf1$.subscribe(console.log);\n// Output\n// 0.4\n// 0.571429\n// 0.666667\n// 0.727273\n// 0.769231\n```\n\n### `mean([initialState={average: 0, sum: 0, index: 0}])`\nCalculate the mean on a stream of numbers.\n```js\nimport { from } from 'rxjs';\nimport { mean } from '@rxtk/stats';\n\nconst mean$ = from([1, 2, 3, 4]).pipe(\n  mean()\n);\n\nmean$.subscribe(console.log);\n// 1\n// 1.5\n// 2\n// 2.5\n```\n\n### `precision([initialState={truePositives: 0, falsePositives: 0}])`\nGiven an Observable of ground-truth labels and predictions, the precision operator returns the precision.\n```js\nimport { from } from 'rxjs';\nimport { precision } from '@rxtk/stats';\n\nconst items = [\n  [0, 1], // [trueLabel, prediction]\n  [0, 1],\n  [0, 1],\n  [0, 0],\n  [0, 0],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n  [1, 0],\n];\n\nconst precision$ = from(items).pipe(\n  precision()\n);\n\nprecision$.subscribe(console.log);\n// Output\n// 0\n// 0\n// 0\n// 0\n// 0\n// 0.25\n// 0.40\n// 0.50\n// 0.5714285714\n// 0.5714285714\n```\n\n### `recall([initialState={truePositives: 0, falseNegatives: 0}])`\nGiven an Observable of ground-truth labels and predictions, the recall operator returns the recall.\n\n```js\nimport { from } from 'rxjs';\nimport { recall } from '@rxtk/stats';\n\nconst items = [\n  [0, 1], // [trueLabel, prediction]\n  [0, 1],\n  [0, 1],\n  [0, 0],\n  [0, 0],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n  [1, 1],\n  [1, 0],\n];\n\nconst recall$ = from(items).pipe(\n  recall()\n);\n\nrecall$.subscribe(console.log);\n// Output\n// 0\n// 0\n// 0\n// 0\n// 0\n// 0.25\n// 0.40\n// 0.50\n// 0.5714285714\n// 0.5714285714\n```\n\n### `roundTo(numDecimalPlaces: Number)`\nRounds numbers in an Observable to the number of desired decimal places.\n\n```js\nimport { from } from 'rxjs';\nimport { roundTo } from '@rxtk/stats';\n\nconst num$ = from([1.234567, 4.5678]);\nconst roundedNum$ = num$.pipe(\n  roundTo(3)\n);\nroundedNum$.subscribe(console.log);\n// Output:\n// 1.235\n// 4.568\n```\n\n### `stdev([initialState={index: 0, mean: 0, m2: null}], [sample=true])`\nComputes the sample standard deviation of an Observable using [Welford's Online Algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm).\n\nBy default, it will compute the sample (rather than population) standard deviation.\n\n```js\nimport { from } from 'rxjs';\nimport { takeLast } from 'rxjs';\nimport { stdev, roundTo } from '@rxtk/stats';\n\nconst stdev$ = from([600, 470, 170, 430, 300]).pipe(\n  stdev(),\n  takeLast(1),\n  roundTo(6)\n);\n\nstdev$.subscribe(console.log);\n// Output:\n// 164.71187\n```\n\n### `sum([initialState={total: 0}])`\nCalculates the sum of all items in an Observable.\n\n```js\nimport { from } from 'rxjs';\nimport { sum } from '@rxtk/stats';\n\nconst num$ = from([1, 2, 3, 4, 5]);\nconst sum$ = num$.pipe(\n  sum()\n);\nnum$.subscribe(console.log);\n// Output:\n// 1\n// 3\n// 6\n// 10\n// 15\n```\n\n### `variance([initialState={index: 0, mean: 0, m2: null}], [sample=true])`\nComputes the variance of an Observable using [Welford's Online Algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm).\n\nBy default, it will compute the sample variance.\n\n**Sample variance**:\n```js\nimport { from } from 'rxjs';\nimport { takeLast } from 'rxjs';\nimport { variance } from '@rxtk/stats';\n\nconst variance$ = from([600, 470, 170, 430, 300]).pipe(\n  variance(),\n  takeLast(1)\n);\n\nvariance$.subscribe(console.log);\n// Output:\n// 27130\n```\n\n**Population variance**:\n```js\nimport { from } from 'rxjs';\nimport { takeLast } from 'rxjs';\nimport { variance } from '@rxtk/stats';\n\nconst variance$ = from([600, 470, 170, 430, 300]).pipe(\n  variance(false),\n  takeLast(1)\n);\n\nvariance$.subscribe(console.log);\n// Output:\n// 21704\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frxtoolkit%2Fstats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frxtoolkit%2Fstats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frxtoolkit%2Fstats/lists"}