### Install statistics.js via npm or yarn Source: https://thisancog.github.io/statistics.js/index Demonstrates how to install the statistics.js library using package managers like npm or yarn. After installation, the module can be required in your project. ```bash $ npm install statistics.js $ yarn add statistics.js ``` ```javascript var Statistics = require('/path/to/statistics.js'); ``` -------------------------------- ### Initialize statistics.js with Data and Column Definitions Source: https://thisancog.github.io/statistics.js/index This example illustrates how to initialize the statistics.js library with sample data and column definitions. It shows the structure for data as an array of objects and column types (e.g., 'ordinal', 'interval'). ```javascript var data = [ { ID: 1, age: 33 }, { ID: 2, age: 42 }, { ID: 3, age: 27 }, … ]; var columns = { ID: 'ordinal', age: 'interval' … }; var settings = { … }; var stats = new Statistics(data, columns, settings); var meanAge = stats.arithmeticMean("age"); var stdDevAge = stats.standardDeviation("age"); // Returns: // meanAge: 34 // stdDevAge: 7.54983… ``` -------------------------------- ### Get Measurement Scale of a Column Source: https://thisancog.github.io/statistics.js/inc/core The `getScale` method retrieves the scale of measurement (e.g., 'interval', 'nominal') for a specified column within the dataset. ```javascript var scale = stats.getScale("age"); ``` -------------------------------- ### Initialize Statistics with Custom Settings Source: https://thisancog.github.io/statistics.js/index Demonstrates how to initialize the Statistics library with a custom settings object. This allows for fine-tuning of various parameters like column exclusion and warning suppression. ```javascript var settings = { excludeColumns: ["ID", "patientNumber"], suppressWarnings: true, zTableIterations: 15 }; var stats = new Statistics(data, columns, settings); ``` -------------------------------- ### Initialize and Use Ziggurat Generator (JavaScript) Source: https://thisancog.github.io/statistics.js/inc/error Demonstrates how to create a new Ziggurat generator instance with specified mean and standard deviation, and then generate a random number using the `next()` method. The generator produces pseudo-random numbers from a normal distribution. ```javascript var ziggurat = new stats.ziggurat(5, 1); var random = ziggurat.next(); console.log(random); ``` -------------------------------- ### Get Unique Values from Data Source: https://thisancog.github.io/statistics.js/inc/core Retrieves an ordered array of unique values from a dataset. The data can be provided as a column name (string) or an array of values. Returns an array of unique, sorted values. ```javascript var uniques = stats.getUniqueValues([13, 4, 9, 7, 4, 5, 6, 13, 7, 5, 2, 1, 1, 3]); // Returns: [1, 2, 3, 4, 5, 6, 7, 9, 13] ``` -------------------------------- ### Ziggurat Generator Source: https://thisancog.github.io/statistics.js/inc/error Initializes a Ziggurat generator and generates a pseudo-random number from a normal distribution. ```APIDOC ## Ziggurat Generator ### Description Generates pseudo-random numbers from a normal distribution with `mean` and `standardDeviation`. This implementation is a variation of Filip Zembowicz' translation of the original C code described in the original paper by Georg Marsaglia and Wai Wan Tsang. ### Method `new stats.ziggurat(mean, standardDeviation)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript var ziggurat = new stats.ziggurat(5, 1); var random = ziggurat.next(); ``` ### Response #### Success Response (200) - **random** (number) - A pseudo-random number generated from the normal distribution. #### Response Example ```json { "random": 4.72130 } ``` ``` -------------------------------- ### Calculate Binomial Distribution (JavaScript) Source: https://thisancog.github.io/statistics.js/inc/distributions Computes the binomial distribution for given n and probability, returning an array of probabilities for k successes out of n trials. It also shows how to calculate a single probability mass using binomialProbabilityMass. ```javascript var distribution = stats.binomialDistribution(5, 0.5); // probabilities of k = 0, 1, … 5 out of 5 coin tosses are heads var single = stats.binomialProbabilityMass(12, 22, 0.4); // the probability of exactly 12 out of 22 succesful events, // each with a probability to occur of 0.4 ``` -------------------------------- ### Configure Columns with Scales and Value Maps in statistics.js Source: https://thisancog.github.io/statistics.js/index Illustrates how to define column properties, including scale of measurement and value maps for ordinal variables. Value maps help in labeling data internally stored as integers. ```javascript var columns = { age: 'metric', agreement: { scale: 'ordinal', valueMap: ['none', 'somewhat', 'undecided', 'much', 'total'] }, iq: 'metric' }; var stats = new Statistics(data, columns); ``` -------------------------------- ### contingencyTable() Source: https://thisancog.github.io/statistics.js/inc/core Creates a contingency table for two specified columns, showing counts for each pairing, totals, and optionally 'a, b, c, d' values for 2x2 tables. ```APIDOC ## contingencyTable() ### Description Creates a contingency table of the stored data for two nominal or ordinal variables given by `firstColumn` and `secondColumn` and returns a nested object with the individual counts for each pairing, total counts, and in case of only two unique values for each variable the individual fields represented as `a, b, c, d`. ### Method (Not specified, likely a method of a Statistics object) ### Parameters #### Path Parameters - **firstColumn** (string) - Required - The name of the first column for the contingency table. - **secondColumn** (string) - Required - The name of the second column for the contingency table. ### Request Example ```javascript var testData = [ { gender: 'male', agreement: 'no' }, { gender: 'male', agreement: 'no' }, { gender: 'male', agreement: 'no' }, { gender: 'male', agreement: 'no' }, { gender: 'male', agreement: 'yes' }, { gender: 'male', agreement: 'yes' }, { gender: 'male', agreement: 'yes' }, { gender: 'female', agreement: 'no' }, { gender: 'female', agreement: 'yes' }, { gender: 'female', agreement: 'yes' }, { gender: 'female', agreement: 'yes' }, { gender: 'female', agreement: 'yes' } ]; var testVars = { gender: 'nominal', agreement: 'nominal' }; var stats = new Statistics(testData, testVars); var contingencyTable = stats.contingencyTable('gender', 'agreement'); ``` ### Response #### Success Response (object) - **detailled** (object) - A nested object containing counts for each combination of values and their totals. - **a, b, c, d** (number) - Specific values for 2x2 tables. #### Response Example ```json { detailled: { female: { no: 1, yes: 2, total: 3 }, male: { no: 4, yes: 3, total: 7 }, total: { female: 3, male: 7, no: 5, yes: 5, total: 10 } }, a: 4, b: 3, c: 1, d: 2 } ``` ``` -------------------------------- ### Random Number Generation and Shuffle Source: https://thisancog.github.io/statistics.js/inc/error Implementations of pseudo-random number generators for shuffling and sampling, including Box-Muller, Fisher-Yates shuffle, and Xorshift. ```APIDOC ## boxMuller() ### Description Generates pseudo-random numbers from a normal distribution with a specified mean and standard deviation. Optionally, it can be seeded with two random number generators. ### Method GET (or equivalent for a library function) ### Endpoint N/A (This is a library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **mean** (integer or float) - Optional, default: 0. The mean of the normal distribution. - **standardDeviation** (integer or float) - Optional, default: 1. The standard deviation of the normal distribution. ### Options - **randomSourceA** (function) - Optional, default: `Math.random`. A function that returns a random number from a uniform distribution in the interval [0, 1). - **randomSourceB** (function) - Optional, default: `Math.random`. A function that returns a random number from a uniform distribution in the interval [0, 1). ### Request Example ```javascript var xorshift = new stats.xorshift([100934093, 482920221, 592807725, 993051833]), seeds = { randomSourceA: xorshift.next, randomSourceB: xorshift.next }, boxMuller = stats.boxMuller(12, 4, seeds); ``` ### Response #### Success Response (200) - **boxMuller** (float) - A pseudo-random number from the specified normal distribution. #### Response Example ```json { "boxMuller": "10.62038…" } ``` ## fisherYatesShuffle() ### Description Shuffles the values in a dataset (column name or array) according to Durstenfeld’s version of the Fisher-Yates shuffle algorithm. It returns a random permutation of the values. ### Method GET (or equivalent for a library function) ### Endpoint N/A (This is a library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **data** (string or array) - Required. The dataset to shuffle (column name or array of values). - **randomSource** (function) - Optional, default: `Math.random`. A function that returns a float in the interval [0, 1) for use in the shuffle. ### Request Example ```javascript var fisherYatesShuffle = stats.fisherYatesShuffle([1, 3, 7, 4, 12, 4, 4, 7, 3, 6, 7, 1, 2]); ``` ### Response #### Success Response (200) - **fisherYatesShuffle** (array) - A randomly permuted array of the input data. #### Response Example ```json { "fisherYatesShuffle": "[6, 2, 1, 3, 4, 7, 4, 3, 7, 4, 12, 7, 1]" } ``` ## xorshift() ### Description Generates pseudo-random numbers from a uniform distribution using the Xorshift algorithm. It can be seeded for deterministic results and can output raw or normalized values. ### Method GET (or equivalent for a library function) ### Endpoint N/A (This is a library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **seed** (array) - Required. An array of exactly four numeric seeds. None should be zero. - **startIndex** (integer) - Optional, default: 0. The index to start generating numbers from, allowing the generator to reach better performance. ### Usage ```javascript var xorshift = new stats.xorshift([100934093, 482920221, 592807725, 993051833]); var random = xorshift.next(false); var randomNormalised = xorshift.next(true); ``` ### Response #### Success Response (200) - **random** (integer) - A raw pseudo-random number. - **randomNormalised** (float) - A pseudo-random number normalized to the interval [0, 1). #### Response Example ```json { "random": 471695451, "randomNormalised": "0.27623…" } ``` ``` -------------------------------- ### Displaying Data with Statistics.js Source: https://thisancog.github.io/statistics.js/inc/core The showData method is a utility for logging data to the console. It can display a specific column, the entire dataset in a table format, or any other provided input using `console.log()`. ```javascript var show = stats.showData(); ``` -------------------------------- ### Algebra - product() Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the product of all the values in a dataset. ```APIDOC ## POST /product ### Description Computes the product of all numeric values in a given dataset. Non-numeric elements are ignored. ### Method POST ### Endpoint /product ### Parameters #### Request Body - **data** (string or array) - Required - The dataset (column name or array of values) for which to compute the product. ### Request Example ```json { "data": [1, 3, 7, 4, 12, true, 4, "a", 3, 6, 7, 1, 2] } ``` ### Response #### Success Response (200) - **result** (number) - The computed product of the numeric values in the dataset. #### Response Example ```json { "result": 1016064 } ``` ``` -------------------------------- ### Scatter Plot Functionality Source: https://thisancog.github.io/statistics.js/inc/core This section describes the options available for creating a scatter plot using the `scatterPlot` function. It details the parameters for customizing the plot's appearance and behavior. ```APIDOC ## Scatter Plot Options ### Description Configuration options for creating a scatter plot. ### Parameters #### Options - **canvas** (node) - Optional - The canvas node to draw on. Defaults to creating a new canvas. - **xAxis** (integer) - Optional - The field to use for the x-axis. - **yAxis** (integer) - Optional - The field to use for the y-axis. - **height** (integer) - Optional - The height of the plot. Defaults to automatically sized. - **width** (integer) - Optional - The width of the plot. Defaults to automatically sized. - **dotRadius** (integer or float) - Optional - The radius of the data points. Defaults to 4. - **showGrid** (boolean) - Optional - Whether to display the grid. Defaults to false. - **minNumberXMarks** (integer) - Optional - Minimum number of marks on the x-axis. Defaults to 8. - **minNumberYMarks** (integer) - Optional - Minimum number of marks on the y-axis. Defaults to 8. - **background** (string) - Optional - Background color of the plot. Defaults to '#FFFFFF'. - **color** (string) - Optional - Color of the data points. Defaults to '#000000'. - **gridColor** (string) - Optional - Color of the grid lines. Defaults to '#CCCCCC'. - **axisColor** (string) - Optional - Color of the axes. Defaults to '#000000'. ### Request Example ```javascript var binomials = [ { k: 0, value: 0.01153 }, { k: 1, value: 0.05765 }, { k: 2, value: 0.13691 }, { k: 3, value: 0.20536 }, // ... ]; var options = { axis: 'k', yAxis: 'value', width: 600, height: 600, dotColor: '#EE2211' }; var plottedCanvas = stats.scatterPlot(binomials, options); document.body.appendChild(plottedCanvas); ``` ``` -------------------------------- ### showData() Source: https://thisancog.github.io/statistics.js/inc/core A utility function to display data in the console. It can log a specific column, the entire dataset in a table, or any other provided input. ```APIDOC ## showData() ### Description This method is a simple wrapper for JavaScript's built-in `console.log()` and `console.table()` methods. If `input` is the name of a stored column, then this will be logged. The complete stored data will be logged in a table if no `input` is supplied. For any other `input`, this will simply be logged to the console. ### Method `showData(input)` ### Parameters #### Query Parameters - **input** (any) - Optional - The data to log. If not provided, the entire dataset is logged in a table. ### Request Example ```javascript var show = stats.showData(); // Logs the entire dataset as a table var specificColumn = stats.showData('age'); // Logs the 'age' column ``` ``` -------------------------------- ### Algebra - regularisedBeta() Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the regularised beta function I_x(a, b) of two values a and b with x over the interval [0, x]. ```APIDOC ## POST /regularisedBeta ### Description Computes the regularised beta function I_x(a, b) for given values x, a, and b. This is the ratio of the lower incomplete beta function to the complete beta function. ### Method POST ### Endpoint /regularisedBeta ### Parameters #### Request Body - **x** (integer or float) - Required - The upper limit of the interval. - **a** (integer or float) - Required - The first parameter of the regularised beta function. - **b** (integer or float) - Required - The second parameter of the regularised beta function. ### Request Example ```json { "x": 0.75, "a": 4.2, "b": 3 } ``` ### Response #### Success Response (200) - **result** (number) - The computed regularised beta function value. #### Response Example ```json { "result": 0.27207 } ``` ``` -------------------------------- ### Algebra - binomialCoefficient() Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the binomial coefficient C(n, k) for two integers n and k. ```APIDOC ## POST /binomialCoefficient ### Description Computes the binomial coefficient C(n, k) for two integers n and k. This represents the number of ways to choose k items from a set of n items. ### Method POST ### Endpoint /binomialCoefficient ### Parameters #### Request Body - **n** (integer) - Required - The total number of items. - **k** (integer) - Required - The number of items to choose. ### Request Example ```json { "n": 7, "k": 3 } ``` ### Response #### Success Response (200) - **result** (integer) - The computed binomial coefficient. #### Response Example ```json { "result": 35 } ``` ``` -------------------------------- ### Scatter Plotting with Statistics.js Source: https://thisancog.github.io/statistics.js/inc/core Demonstrates how to create a scatter plot using the stats.scatterPlot function. It takes an array of data points and an options object to configure the plot's appearance and axes. The function returns a canvas node that can be appended to the DOM. ```javascript var binomials = [ { k: 0, value: 0.01153 }, { k: 1, value: 0.05765 }, { k: 2, value: 0.13691 }, { k: 3, value: 0.20536 }, … ]; var options = { xAxis: 'k', yAxis: 'value', width: 600, height: 600, dotColor: '#EE2211' }; var plottedCanvas = stats.scatterPlot(binomials, options); document.body.appendChild(plottedCanvas); ``` -------------------------------- ### Algebra - incompleteBeta() Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the lower incomplete beta function I_x(a, b) of two values a and b with x over the interval [0, x]. ```APIDOC ## POST /incompleteBeta ### Description Computes the lower incomplete beta function I_x(a, b) for given values x, a, and b. This method uses the continued fraction representation for convergence. ### Method POST ### Endpoint /incompleteBeta ### Parameters #### Request Body - **x** (integer or float) - Required - The upper limit of the interval. - **a** (integer or float) - Required - The first parameter of the incomplete beta function. - **b** (integer or float) - Required - The second parameter of the incomplete beta function. ### Request Example ```json { "x": 0.75, "a": 4.2, "b": 3 } ``` ### Response #### Success Response (200) - **result** (number) - The computed lower incomplete beta function value. #### Response Example ```json { "result": 0.01205 } ``` ``` -------------------------------- ### getScale() Source: https://thisancog.github.io/statistics.js/inc/core Retrieves the scale of measurement for a specified column. ```APIDOC ## getScale() ### Description Retrieves the scale of measurement for a column `column`. See also the explanation of the different scales. ### Method (Not specified, likely a method of a Statistics object) ### Parameters #### Path Parameters - **column** (string) - Required - The name of the column to get the scale for. ### Request Example ```javascript var scale = stats.getScale("age"); ``` ### Response #### Success Response (string) - **return value** (string) - The scale of measurement for the column (e.g., 'interval', 'nominal', 'ordinal'). #### Response Example ```json "interval" ``` ``` -------------------------------- ### Algebra - regularisedGamma() Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the regularised gamma function P(s, x) of two values s and x. ```APIDOC ## POST /regularisedGamma ### Description Computes the regularised gamma function P(s, x) for given values s and x. ### Method POST ### Endpoint /regularisedGamma ### Parameters #### Request Body - **s** (integer or float) - Required - The first parameter of the regularised gamma function. - **x** (integer or float) - Required - The second parameter of the regularised gamma function. ### Request Example ```json { "s": 0.75, "x": 4.2 } ``` ### Response #### Success Response (200) - **result** (number) - The computed regularised gamma function value. #### Response Example ```json { "result": 0.33768 } ``` ``` -------------------------------- ### scatterPlot Source: https://thisancog.github.io/statistics.js/inc/core Generates a scatter plot visualization from provided data. Supports data as an array of objects or an array of numeric values. Allows customization of plot appearance and axes. ```APIDOC ## POST /visualizations/scatterPlot ### Description This method provides an interface to create simple scatter plots of given `data`. It is intended to get a quick visual overview of data and thus is far from the precision and sophisticiation of dedicated plotting libraries such as D3.js or plotly.js. Be aware, that for some edge cases it may behave erroneously (in that case please file a bug report). ### Method POST ### Endpoint /visualizations/scatterPlot ### Parameters #### Request Body - **data** (array) - Optional (default: stored data) - Contains the data to be plotted either as an array of objects or an array of values. In the first case, any item without the properties `xAxis` or `yAxis` will be ignored as in both cases non-numerical values will be ignored as well. If an array of numeric values is supplied, the method will count the number of occurences for each unique value and plot them. If you want to plot each value to its index, rather supply an object. - **canvas** (HTMLCanvasElement) - Optional - Reference to a HTML5 canvas node in which the plot should be drawn, otherwise a new one will be created but not inserted into the DOM. - **xAxis** (string) - Optional - If `data` stores objects, then this will signify the name of the property to plot on the x axis. - **yAxis** (string) - Optional - If `data` stores objects, then this will signify the name of the property to plot on the y axis. - **height** (integer) - Optional - The height of the canvas. If omitted, it will be automatically sized. Minimum is `400` px. - **width** (integer) - Optional - The width of the canvas. If omitted, it will be automatically sized. Minimum is `400` px. - **dotRadius** (integer) - Optional - The data points’ radius. - **showGrid** (boolean) - Optional - Determines if a grid should extend from the axes markers. - **minNumberXMarks** (integer) - Optional - The minimum amount of marks on the x axis. - **minNumberYMarks** (integer) - Optional - The minimum amount of marks on the y axis. - **background** (string) - Optional - The background color in the form of any valid CSS color string (including "transparent"). - **dotColor** (string) - Optional - The color for the data points in the form of any valid CSS color string (including "transparent"). - **gridColor** (string) - Optional - The grid color in the form of any valid CSS color string (including "transparent"). - **axisColor** (string) - Optional - The axis color in the form of any valid CSS color string (including "transparent"). ### Request Example ```json { "data": [ {"x": 1, "y": 5}, {"x": 2, "y": 7}, {"x": 3, "y": 6} ], "xAxis": "x", "yAxis": "y", "height": 500, "width": 600, "dotRadius": 5, "showGrid": true, "dotColor": "blue" } ``` ### Response #### Success Response (200) - **plotUrl** (string) - A URL or data URI representing the generated scatter plot image. #### Response Example ```json { "plotUrl": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." } ``` ``` -------------------------------- ### addData() Source: https://thisancog.github.io/statistics.js/inc/core Adds multiple rows of new data to the existing dataset. The new data must have the same columns as the existing data. Returns true on success. ```APIDOC ## addData() ### Description Adds several rows of new data to the existing data to work with. All the elements of `data` or, if `data` is a JSON encoded string the array it evaluates to, need to have the same columns as the rest of the already existing data. Returns `true` on success. ### Method (Not specified, likely a method of a Statistics object) ### Parameters #### Request Body - **dataset** (array or JSON encoded string) - Required - The data to add. ### Request Example ```javascript stats.addData([{age: 22, gender: "male", "heartFrequency": 89}, {age: 37, gender: "female", "heartFrequency": 76}]); ``` ### Response #### Success Response (true) - **return value** (boolean) - `true` if the data was added successfully. #### Response Example ```json true ``` ``` -------------------------------- ### reset Source: https://thisancog.github.io/statistics.js/inc/core Clears all loaded data from the current session while preserving scale measurements, value maps, settings, and calculations. Returns true upon successful reset. ```APIDOC ## POST /datasets/reset ### Description Deletes all the data previously loaded, but keeps information about the scales of measure for each column, value maps, settings and all stored calculations (e.g. larger factorials, the z-table). Returns true on success. ### Method POST ### Endpoint /datasets/reset ### Parameters None ### Request Example ``` POST /datasets/reset ``` ### Response #### Success Response (200) - **status** (boolean) - True if the reset operation was successful. #### Response Example ```json { "status": true } ``` ``` -------------------------------- ### Define Data Structure for statistics.js Source: https://thisancog.github.io/statistics.js/index Shows the expected format for input data when initializing statistics.js. Data should be an array of objects, where each object represents a record with key-value pairs for variables. ```javascript var data = [ { age: 32, agreement: 'none', iq: 104 }, { age: 45, agreement: 'somewhat', iq: 110 }, … ]; ``` -------------------------------- ### Algebra - gamma() Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the gamma function for a given n. Different methods can be applied to approximate the actual value. ```APIDOC ## POST /gamma ### Description Computes the gamma function for a given number n. Allows for approximation using Stirling-Nemes or Spouge's approximation. ### Method POST ### Endpoint /gamma ### Parameters #### Request Body - **n** (integer or float) - Required - The number for which to compute the gamma function. - **moreExact** (boolean) - Optional - If true, uses Spouge's approximation; otherwise, uses Stirling-Nemes approximation. Defaults to false. ### Request Example ```json { "n": 4.32, "moreExact": true } ``` ### Response #### Success Response (200) - **result** (number) - The computed gamma function value. #### Response Example ```json { "result": 9.09595 } ``` ``` -------------------------------- ### Algebra - beta() Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the beta function of two values a and b with x. If both variables have an integer value, factorials instead of the gamma function will be computed. ```APIDOC ## POST /beta ### Description Computes the beta function of two values a and b with x. If both variables have an integer value, factorials instead of the gamma function will be computed. ### Method POST ### Endpoint /beta ### Parameters #### Request Body - **a** (integer or float) - Required - The first value for the beta function. - **b** (integer or float) - Required - The second value for the beta function. - **x** (number) - Required - The value for which to compute the beta function. ### Request Example ```json { "a": 4.2, "b": 3, "x": 0.75 } ``` ### Response #### Success Response (200) - **result** (number) - The computed beta function value. #### Response Example ```json { "result": 0.01477 } ``` ``` -------------------------------- ### Generate Pseudo-Random Numbers (Xorshift) Source: https://thisancog.github.io/statistics.js/inc/error Generates pseudo-random numbers from a uniform distribution using the Xorshift algorithm. It requires an array 'seed' of four numeric seeds and an optional 'startIndex' to skip initial numbers. Numbers can be generated as raw values or normalized to the interval [0, 1] via the 'next()' method. ```javascript var xorshift = new stats.xorshift([100934093, 482920221, 592807725, 993051833]); var random = xorshift.next(false); var randomNormalised = xorshift.next(true); ``` -------------------------------- ### Binomial Distribution Source: https://thisancog.github.io/statistics.js/inc/distributions Computes the binomial distribution for a given n and probability. It returns an array of probabilities for k successes out of n trials. ```APIDOC ## GET /distributions/binomial ### Description Computes the binomial distribution for a given `n` and `probability` where the element at the index `k` corresponds to the probability that out of `n` independent trials exactly `k` are successful, when each of them has an individual `probability`. This method returns an array of floating point numbers with their indices. ### Method GET ### Endpoint /distributions/binomial ### Parameters #### Query Parameters - **n** (integer) - Optional, default: 10 - The number of trials. - **probability** (integer or float) - Optional, default: 0.5 - The probability of success in each trial. ### Request Example ``` GET /distributions/binomial?n=5&probability=0.5 ``` ### Response #### Success Response (200) - **distribution** (array) - An array of floating point numbers representing the probabilities for k = 0, 1, ..., n. #### Response Example ```json { "distribution": [ 0.03125, 0.15625, 0.3125, 0.3125, 0.15625, 0.03125 ] } ``` ``` -------------------------------- ### reduceToPairs Source: https://thisancog.github.io/statistics.js/inc/core Processes two columns to return paired values, along with counts of total datasets, missing values, and separate and combined value arrays. ```APIDOC ## GET /datasets/reduceToPairs ### Description Returns the values of those datasets that have valid data for both `firstColumn` and `secondColumn` along with their total count and the number of datasets with missing values. ### Method GET ### Endpoint /datasets/reduceToPairs ### Parameters #### Query Parameters - **firstColumn** (string) - Required - The name of the first column to process. - **secondColumn** (string) - Required - The name of the second column to process. ### Request Example ``` GET /datasets/reduceToPairs?firstColumn=age&secondColumn=gender ``` ### Response #### Success Response (200) - **length** (integer) - The total number of datasets. - **missings** (integer) - The number of datasets with missing values in either column. - **valuesFirst** (array) - An array of values from the `firstColumn`. - **valuesSecond** (array) - An array of values from the `secondColumn`. - **valuesCombined** (array) - An array of objects, where each object contains paired values from `firstColumn` and `secondColumn`. #### Response Example ```json { "length": 18, "missings": 2, "valuesFirst": [ 27, 24, ... ], "valuesSecond": [ 'male', 'male', ... ], "valuesCombined": [ { "age": 22, "gender": "male" }, { "age": 24, "gender": "male" }, ... ] } ``` ``` -------------------------------- ### Compute Quantile in JavaScript Source: https://thisancog.github.io/statistics.js/inc/measures Computes the quantiles of a dataset. The data can be a column name (string) or an array of values. The percentage must be between 0 and 1. Requires ordinal or higher scale of measure. Returns undefined and throws an error for incompatible scales. ```javascript var quantile = stats.quantile([1, 3, 7, 4, 12, 4, 4, 7, 3, 6, 7, 1, 2], 0.33); // Returns: 3 ``` -------------------------------- ### Calculate Poisson Distribution (JavaScript) Source: https://thisancog.github.io/statistics.js/inc/distributions Computes the Poisson distribution for a given lambda, returning an array of probabilities for observing k=0, 1, 2,... events. It also offers a function to calculate the probability mass for a specific k. ```javascript var distribution = stats.poissonDistribution(0.3); // probabilities that an event which happens at a rate of 0.3 per time interval // will be observed k = 0, 1, … times in that interval var single = stats.poissonProbabilityMass(2, 4); ``` -------------------------------- ### Generate Random Numbers from Normal Distribution (Box-Muller) Source: https://thisancog.github.io/statistics.js/inc/error Generates pseudo-random numbers from a normal distribution with a specified mean and standard deviation. Optionally, it can be seeded with two random number generators. If not provided, Math.random() is used as a fallback. Parameters include 'mean', 'standardDeviation', and an 'options' object for 'randomSourceA' and 'randomSourceB'. ```javascript var xorshift = new stats.xorshift([100934093, 482920221, 592807725, 993051833]), seeds = { randomSourceA: xorshift.next, randomSourceB: xorshift.next }, boxMuller = stats.boxMuller(12, 4, seeds); ``` -------------------------------- ### Compute Binomial Coefficient Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the binomial coefficient 'n choose k' for two integers 'n' and 'k'. This represents the number of ways to choose 'k' items from a set of 'n' items without regard to the order of selection. It takes two required integer parameters and returns an integer. ```javascript var nChooseK = stats.binomialCoefficient(7, 3); ``` -------------------------------- ### Calculate Binomial Cumulative Distribution (JavaScript) Source: https://thisancog.github.io/statistics.js/inc/distributions Computes the binomial cumulative distribution for given n and probability, returning an array of probabilities for k or fewer successes out of n trials. It also demonstrates calculating a single cumulative probability using binomialCumulativeValue. ```javascript var distribution = stats.binomialCumulativeDistribution(5, 0.5); // probabilities of k ≤ 0, 1, … 5 out of 5 coin tosses are heads var single = stats.binomialCumulativeValue(12, 22, 0.4); // the probability of no more than 12 out of 22 succesful events, // each with a probability to occur of 0.4 ``` -------------------------------- ### sort() Source: https://thisancog.github.io/statistics.js/inc/core Sorts an array of values. This method is type-agnostic for integers, floating-point numbers, and number strings. It supports both ascending and descending order. ```APIDOC ## sort() ### Description A simple sorting method that is type agnostic for integers, floating point numbers and number strings. Setting `order` to `"desc"` will result in descending order. ### Method `sort(values, order)` ### Parameters #### Path Parameters - **values** (array) - Required - The array of values to sort. - **order** (string) - Optional - The sort order. Accepts 'asc' (default) or 'desc'. ### Request Example ```javascript var sorted = stats.sort([1, 3, 7, 4, 12, "4", 4, 7, "9", 6, 7, 1, 2]); var sortedDesc = stats.sort([1, 3, 7, 4, 12], "desc"); ``` ### Response #### Success Response - **return value** (array) - The sorted array of values. ``` -------------------------------- ### Reduce Dataset to Value Pairs Source: https://thisancog.github.io/statistics.js/inc/core Extracts pairs of values from two specified columns, along with counts of total datasets and those with missing values for either column. Requires column names as input. ```javascript var reduced = stats.reduceToPairs('age', 'gender'); /* Returns: { length: 18, missings: 2, valuesFirst: [ 27, 24, … ], valuesSecond: [ 'male', 'male', … ], valuesCombined: [ { age: 22, gender: 'male' }, { age: 24, gender: 'male' }, … ] } */ ``` -------------------------------- ### Retrieve Median using Statistics.js Source: https://thisancog.github.io/statistics.js/inc/measures Retrieves the median of a dataset. Accepts an array of numbers or a column name. Compatible with ordinal, interval, and metric scales of measure. ```javascript var median = stats.median([1, 3, 7, 4, 12, 4, 4, 7, 3, 6, 7, 1, 2]); ``` -------------------------------- ### Shuffle Data using Fisher-Yates Algorithm Source: https://thisancog.github.io/statistics.js/inc/error Shuffles the values in a dataset using Durstenfeld’s version of the Fisher-Yates algorithm. The data can be a column name (string) or an array of values. It returns a random permutation of the values based on a provided random number generator. Parameters are 'data' and an optional 'randomSource' function. ```javascript var fisherYatesShuffle = stats.fisherYatesShuffle([1, 3, 7, 4, 12, 4, 4, 7, 3, 6, 7, 1, 2]); ``` -------------------------------- ### setScale() Source: https://thisancog.github.io/statistics.js/inc/core Sets the scale of measurement for a specified column. This function allows you to define how data in a column should be interpreted, referencing predefined scale explanations. ```APIDOC ## setScale() ### Description Set the scale of measurement for a column `column` to `scale`. See also the explanation of the different scales. ### Method `setScale(column, scale)` ### Parameters #### Path Parameters - **column** (string) - Required - The name of the column to set the scale for. - **scale** (string) - Required - The scale to apply to the column (e.g., 'interval', 'nominal'). ### Request Example ```javascript var scale = stats.setScale("age", "interval"); ``` ### Response #### Success Response (true) - **return value** (boolean) - Returns `true` upon successful scale setting. ``` -------------------------------- ### Setting Measurement Scale with Statistics.js Source: https://thisancog.github.io/statistics.js/inc/core The setScale function allows you to define the measurement scale for a specific column. This is useful for ensuring data is interpreted correctly based on its type (e.g., 'interval'). It requires the column name and the desired scale as string arguments. ```javascript var scale = stats.setScale("age", "interval"); ``` -------------------------------- ### getColumn() Source: https://thisancog.github.io/statistics.js/inc/core Retrieves all values from a specified column in the dataset as an array. ```APIDOC ## getColumn() ### Description Returns the values of the column `dataset` in an array. ### Method (Not specified, likely a method of a Statistics object) ### Parameters #### Path Parameters - **column** (string) - Required - The name of the column to retrieve. ### Request Example ```javascript var ageColumn = stats.getColumn('age'); ``` ### Response #### Success Response (array) - **return value** (array) - An array containing all values from the specified column. #### Response Example ```json [ 22, 34, 27, 19, 27, 24, 37, 42, 45, 37 ] ``` ``` -------------------------------- ### Compute Incomplete Beta Function Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the lower incomplete beta function for values 'x', 'a', and 'b'. It uses a continued fraction representation for fast convergence. The number of iterations can be adjusted. It takes three required numeric parameters and returns a float. ```javascript var incompleteBeta = stats.incompleteBeta(0.75, 4.2, 3); ``` -------------------------------- ### Algebra - incompleteGamma() Source: https://thisancog.github.io/statistics.js/inc/algebra Computes the lower incomplete gamma function P(s, x) of two values s and x with x over the interval [0, x]. ```APIDOC ## POST /incompleteGamma ### Description Computes the lower incomplete gamma function P(s, x) for given values s and x. This method uses the continued fraction representation for convergence. ### Method POST ### Endpoint /incompleteGamma ### Parameters #### Request Body - **s** (integer or float) - Required - The first parameter of the incomplete gamma function. - **x** (integer or float) - Required - The upper limit of the interval. ### Request Example ```json { "s": 0.75, "x": 4.2 } ``` ### Response #### Success Response (200) - **result** (number) - The computed lower incomplete gamma function value. #### Response Example ```json { "result": 1.21543 } ``` ``` -------------------------------- ### getUniqueValues() Source: https://thisancog.github.io/statistics.js/inc/core Retrieves the unique values from a specified column. ```APIDOC ## getUniqueValues() ### Description Retrieves the unique values from a specified column. ### Method (Not specified, likely a method of a Statistics object) ### Parameters #### Path Parameters - **column** (string) - Required - The name of the column to get unique values from. ### Request Example ```javascript // Assuming stats object is initialized with data // var uniqueAges = stats.getUniqueValues('age'); ``` ### Response #### Success Response (array) - **return value** (array) - An array containing the unique values from the specified column. #### Response Example ```json [ 22, 34, 27, 19, 24, 37, 42, 45 ] ``` ``` -------------------------------- ### Perform Simple Linear Regression with statistics.js Source: https://thisancog.github.io/statistics.js/inc/correlation Calculates measures for simple linear regression between two specified columns. It returns an object containing the correlation coefficient, coefficient of determination, corrected coefficient of determination, and regression coefficients for both directional mappings. ```javascript var testData = [ { price: 2.00, sold: 0 }, { price: 1.90, sold: 2 }, { price: 1.80, sold: 1 }, { price: 1.70, sold: 2 }, { price: 1.60, sold: 3 }, { price: 1.50, sold: 6 }, { price: 1.40, sold: 4 }, { price: 1.30, sold: 9 }, { price: 1.20, sold: 8 }, { price: 1.10, sold: 10 }, { price: 1.00, sold: 14 } ]; var testVars = { price: 'metric', sold: 'metric' }; var stats = new Statistics(testData, testVars); var regression = stats.linearRegression('price', 'sold')); console.log(regression); ``` -------------------------------- ### Create Scatter Plot Source: https://thisancog.github.io/statistics.js/inc/core Generates a simple scatter plot on an HTML5 canvas. It can plot arrays of values or objects with specified x and y axes. Various options control appearance like size, colors, and grid display. ```javascript // Example usage with an array of values: stats.scatterPlot([1, 2, 2, 3, 3, 3, 4]); // Example usage with an array of objects: stats.scatterPlot([ { x: 1, y: 5 }, { x: 2, y: 7 }, { x: 3, y: 6 } ], { xAxis: 'x', yAxis: 'y', height: 500, width: 600, dotColor: 'blue', showGrid: true }); ```