@danielsimonjr/mathts-core 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +18 -1
- package/dist/index.js +12 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2538,6 +2538,23 @@ declare function pairwiseDot(a: ArrayLike<number>, b: ArrayLike<number>, start?:
|
|
|
2538
2538
|
* The two ranges are assumed the same length; callers guard that.
|
|
2539
2539
|
*/
|
|
2540
2540
|
declare function scaledDistance(a: ArrayLike<number>, b: ArrayLike<number>): number;
|
|
2541
|
+
/**
|
|
2542
|
+
* Sum of squared deviations from the mean, `Σ(xᵢ − x̄)²` — the **corrected two-pass** form, the
|
|
2543
|
+
* numerator of a numerically stable variance.
|
|
2544
|
+
*
|
|
2545
|
+
* Variance is where large means bite: the deviations `xᵢ − x̄` can be O(1) while the values sit on
|
|
2546
|
+
* a huge pedestal, so any error in `x̄` rides straight into every deviation. Two things fix it:
|
|
2547
|
+
* 1. compute the mean with {@link pairwiseSum}, not a naive running total; and
|
|
2548
|
+
* 2. subtract the residual mean-bias term `(Σd)²/n` — `Σd` is zero in exact arithmetic but not in
|
|
2549
|
+
* floating point, and that leftover is exactly the systematic error a plain `Σd²` carries.
|
|
2550
|
+
*
|
|
2551
|
+
* Measured on 1e9-pedestal data: the plain naive two-pass (what shipped) lands ~1e-7 relative error;
|
|
2552
|
+
* this lands ~1e-16 — better than `np.var`, which uses the uncorrected two-pass (~1e-13).
|
|
2553
|
+
*
|
|
2554
|
+
* Divide the result by `n` (uncorrected), `n − 1` (unbiased/sample), or `n + 1` (biased) for the
|
|
2555
|
+
* corresponding variance. Returns 0 for fewer than two elements.
|
|
2556
|
+
*/
|
|
2557
|
+
declare function sumSquaredDeviations(xs: ArrayLike<number>): number;
|
|
2541
2558
|
/**
|
|
2542
2559
|
* Cumulative sum with **Neumaier compensation** — each prefix total is written to `out[i]` carrying
|
|
2543
2560
|
* the low-order bits a naive running sum throws away.
|
|
@@ -2562,4 +2579,4 @@ declare function neumaierCumsum(xs: ArrayLike<number>, out: {
|
|
|
2562
2579
|
|
|
2563
2580
|
declare const VERSION = "0.1.0";
|
|
2564
2581
|
|
|
2565
|
-
export { ALL_UNITS, BASE_UNITS, BEST_PREFIXES, BIGNUMBER_E, BIGNUMBER_LN10, BIGNUMBER_LN2, BIGNUMBER_NEG_ONE, BIGNUMBER_ONE, BIGNUMBER_PI, BIGNUMBER_TEN, BIGNUMBER_ZERO, type BackendType, BigNumber, type BigNumberConfig, COMPLEX_NEG_ONE, COMPLEX_ONE, COMPLEX_ZERO, Complex, type ConversionDef, DEFAULT_CONFIG, DERIVED_UNITS, DIMENSIONLESS, DUAL_UNARY_RULES, DimensionMismatchError, type Dimensions, Dual, type DualUnaryRule, type DualUnaryRuleName, E, FRACTION_HALF, FRACTION_NEG_ONE, FRACTION_ONE, FRACTION_QUARTER, FRACTION_THIRD, FRACTION_ZERO, type FactoryDependencies, type FactoryFunction, type FactoryImport, Fraction, FunctionRegistry, I, type IBigNumber, type IComplex, type IFraction, type IMatrix, LN10, LN2, LOG10E, LOG2E, MATHTS_CONVERSIONS, MATHTS_TYPES, type MathTSConfig, type MathTSValue, type MatrixBackend, type MatrixDimensions, type NumericScalar, type NumericType, PHI, PI, Range, type RangeForEachCallback, type RangeFormatOptions, type RangeJSON, type RangeMapCallback, type RoundingMode, SI_PREFIXES, SQRT1_2, SQRT2, type Scalar, TAU, type TypeDef, TypeRegistry, UNIT_ALIASES, type UnitDef, UnitParseError, VERSION, abs, addScalar, createFactory, createMathTSTyped, createRangeClass, createTypedFunction, dim, divideScalar, equal, fix, getPrefix, getUnitDef, initTypedWasm, isArray, isBigInt, isBigNumber, isBoolean, isComplex, isDenseMatrix, isDual, isFraction, isFunction, isMatrix, isNull, isNumber, isNumeric, isObject, isSparseMatrix, isString, isTypedWasmAvailable, isUndefined, isUnit, math, mathTyped, multiplyScalar, neumaierCumsum, neumaierSum, norm2, number, pairwiseDot, pairwiseSum, pow, registerNativeTypes, registry, round, scaledDistance, subtractScalar };
|
|
2582
|
+
export { ALL_UNITS, BASE_UNITS, BEST_PREFIXES, BIGNUMBER_E, BIGNUMBER_LN10, BIGNUMBER_LN2, BIGNUMBER_NEG_ONE, BIGNUMBER_ONE, BIGNUMBER_PI, BIGNUMBER_TEN, BIGNUMBER_ZERO, type BackendType, BigNumber, type BigNumberConfig, COMPLEX_NEG_ONE, COMPLEX_ONE, COMPLEX_ZERO, Complex, type ConversionDef, DEFAULT_CONFIG, DERIVED_UNITS, DIMENSIONLESS, DUAL_UNARY_RULES, DimensionMismatchError, type Dimensions, Dual, type DualUnaryRule, type DualUnaryRuleName, E, FRACTION_HALF, FRACTION_NEG_ONE, FRACTION_ONE, FRACTION_QUARTER, FRACTION_THIRD, FRACTION_ZERO, type FactoryDependencies, type FactoryFunction, type FactoryImport, Fraction, FunctionRegistry, I, type IBigNumber, type IComplex, type IFraction, type IMatrix, LN10, LN2, LOG10E, LOG2E, MATHTS_CONVERSIONS, MATHTS_TYPES, type MathTSConfig, type MathTSValue, type MatrixBackend, type MatrixDimensions, type NumericScalar, type NumericType, PHI, PI, Range, type RangeForEachCallback, type RangeFormatOptions, type RangeJSON, type RangeMapCallback, type RoundingMode, SI_PREFIXES, SQRT1_2, SQRT2, type Scalar, TAU, type TypeDef, TypeRegistry, UNIT_ALIASES, type UnitDef, UnitParseError, VERSION, abs, addScalar, createFactory, createMathTSTyped, createRangeClass, createTypedFunction, dim, divideScalar, equal, fix, getPrefix, getUnitDef, initTypedWasm, isArray, isBigInt, isBigNumber, isBoolean, isComplex, isDenseMatrix, isDual, isFraction, isFunction, isMatrix, isNull, isNumber, isNumeric, isObject, isSparseMatrix, isString, isTypedWasmAvailable, isUndefined, isUnit, math, mathTyped, multiplyScalar, neumaierCumsum, neumaierSum, norm2, number, pairwiseDot, pairwiseSum, pow, registerNativeTypes, registry, round, scaledDistance, subtractScalar, sumSquaredDeviations };
|
package/dist/index.js
CHANGED
|
@@ -1297,6 +1297,17 @@ function scaledDistance(a, b) {
|
|
|
1297
1297
|
}
|
|
1298
1298
|
return scale === 0 ? 0 : scale * Math.sqrt(ssq);
|
|
1299
1299
|
}
|
|
1300
|
+
function sumSquaredDeviations(xs) {
|
|
1301
|
+
const n = xs.length;
|
|
1302
|
+
if (n < 2) return 0;
|
|
1303
|
+
const mean = pairwiseSum(xs) / n;
|
|
1304
|
+
const d = new Float64Array(n);
|
|
1305
|
+
for (let i = 0; i < n; i++) d[i] = xs[i] - mean;
|
|
1306
|
+
const sumD = pairwiseSum(d);
|
|
1307
|
+
const sumDD = pairwiseDot(d, d);
|
|
1308
|
+
const corrected = sumDD - sumD * sumD / n;
|
|
1309
|
+
return corrected < 0 ? 0 : corrected;
|
|
1310
|
+
}
|
|
1300
1311
|
function neumaierCumsum(xs, out) {
|
|
1301
1312
|
let sum = 0;
|
|
1302
1313
|
let c = 0;
|
|
@@ -1411,5 +1422,6 @@ export {
|
|
|
1411
1422
|
round,
|
|
1412
1423
|
scaledDistance,
|
|
1413
1424
|
subtractScalar,
|
|
1425
|
+
sumSquaredDeviations,
|
|
1414
1426
|
typed
|
|
1415
1427
|
};
|