Skip to content

Commit

Permalink
statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
mzusin committed Mar 5, 2024
1 parent c8debeb commit 16387ef
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dist/mz-math.esm.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/mz-math.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mz-math.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/mz-math.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mz-math.node.cjs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/mz-math.node.cjs.map

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/main/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setDecimalPlaces } from './format';
// -------------------- CENTRAL TENDENCY ----------------------------

/**
* Central tendency: Calculate the Average (mean)
* Central tendency: Calculate the Average (mean = μ)
* Sum of all numbers divided by the array length.
*/
export const getArithmeticMean = (data: number[], decimalPlaces = Infinity) : number|undefined => {
Expand Down Expand Up @@ -79,7 +79,7 @@ TODO:
* Dispersion: the average square distance from the mean.
* Sum of (x - mean)^2 / N
*/
export const getVariance = (data: number[], decimalPlaces = Infinity) : number|undefined => {
export const getVariance1 = (data: number[], decimalPlaces = Infinity) : number|undefined => {
if(!data || data.length <= 0) return undefined;

const mean = getArithmeticMean(data);
Expand All @@ -91,10 +91,10 @@ export const getVariance = (data: number[], decimalPlaces = Infinity) : number|u
};

/**
* Another formula
* Another formula of dispersion - the average square distance from the mean.
* (Sum of x^2) / N - (mean ^ 2)
*/
export const getVariance1 = (data: number[], decimalPlaces = Infinity) : number|undefined => {
export const getVariance = (data: number[], decimalPlaces = Infinity) : number|undefined => {
if(!data || data.length <= 0) return undefined;

const mean = getArithmeticMean(data);
Expand All @@ -105,6 +105,9 @@ export const getVariance1 = (data: number[], decimalPlaces = Infinity) : number|
return setDecimalPlaces((sum / data.length) - (mean ** 2), decimalPlaces);
};

/**
* σ
*/
export const getStandardDeviation = (data: number[], decimalPlaces = Infinity) => {
const variance = getVariance(data) ?? 0;
return setDecimalPlaces(Math.sqrt(variance), decimalPlaces);
Expand Down

0 comments on commit 16387ef

Please sign in to comment.