@cloudglides/nox 2.0.0 → 3.0.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.
@@ -9,7 +9,7 @@
9
9
  "preview": "vite preview"
10
10
  },
11
11
  "dependencies": {
12
- "@cloudglides/nox": "^1.1.3",
12
+ "@cloudglides/nox": "workspace:*",
13
13
  "react": "^18.2.0",
14
14
  "react-dom": "^18.2.0"
15
15
  },
@@ -8,7 +8,7 @@ import {
8
8
  tTest, welchTTest, oneWayAnova,
9
9
  LinearRegression, MultipleRegression,
10
10
  diff, lag, sma, ema, acf,
11
- matrixAdd, matrixMul, determinant, inverse,
11
+ matrixAdd, matrixMul, determinant, matrixInverse,
12
12
  trapezoidal, simpsons, numericalDerivative,
13
13
  LinearInterpolator, CubicSplineInterpolator,
14
14
  bisection, newtonRaphson, brent,
@@ -170,7 +170,7 @@ export default function App() {
170
170
  const sum = matrixAdd(A, B)
171
171
  const prod = matrixMul(A, B)
172
172
  const det = determinant(A)
173
- const inv = inverse(A)
173
+ const inv = matrixInverse(A)
174
174
 
175
175
  setResults({
176
176
  title: 'Matrix Operations',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudglides/nox",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "Unpredictable random number generator with multiple algorithms and distributions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,3 @@
1
+ packages:
2
+ - '.'
3
+ - 'example'
@@ -1,18 +1,43 @@
1
1
  export { rng, RNG } from './rng.browser.js';
2
2
  export { deterministic } from './presets.browser.js';
3
3
 
4
- export { normal, exponential, uniform, poisson } from './utils/distributions.js';
5
- export { beta, gamma, chi2 } from './utils/distributions-extra.js';
4
+ export { normal, exponential, uniform, poisson, normals, exponentials } from './utils/distributions.js';
5
+ export { beta, gamma, chi2, binomial, geometric } from './utils/distributions-extra.js';
6
6
  export { weibull, lognormal, rayleigh, cauchy } from './utils/distributions-special.js';
7
- export { shuffle, pick, sample } from './utils/sequence.js';
7
+ export { dirichlet, dirichlets, mixture, mixtures, studentT, studentTs, betaBinomial } from './utils/distributions-advanced.js';
8
+ export { shuffle, pick, sample, sampleWithReplacement, permute, range, cycle } from './utils/sequence.js';
8
9
  export { saveState, restoreState, cloneGenerator } from './utils/state.js';
9
10
  export { weightedPick, weightedSample, reservoirSample } from './utils/sampling.js';
10
- export { meanTest, varianceTest, kolmogorovSmirnovTest, chiSquareTest, entropy, autocorrelation, runTest } from './utils/statistics.js';
11
+ export { meanTest, varianceTest, kolmogorovSmirnovTest, chiSquareTest, entropy, autocorrelation, runTest, skewness, kurtosis, median, quantile } from './utils/statistics.js';
11
12
  export { combined, clearCryptoCache } from './utils/entropy.browser.js';
12
13
  export { brownianMotion, ornsteinUhlenbeck, geometricBrownian } from './utils/stochastic.js';
13
14
  export { categorical, multinomial, categorical2D } from './utils/categorical.js';
14
15
  export { perlin2D, valueNoise } from './utils/noise.js';
15
16
  export { combinations, permutations, kPermutations, randomCombination, randomPermutation } from './utils/combinatorics.js';
16
- export { rotateBits, extractBits, hammingWeight, bitRange } from './utils/bits.js';
17
+ export { rotateBits, extractBits, hammingWeight, bitRange, popcountNum, clz, ctz, reverseBits, setBit, clearBit, toggleBit } from './utils/bits.js';
17
18
  export { SeedSequence, seedMultiple } from './utils/seeding.js';
18
19
  export { seedFromTime, seedFromEntropy } from './utils/seed.js';
20
+ export { repeat, until, times } from './utils/helpers.js';
21
+ export { bootstrap, jackknife, crossValidation, permutationTest } from './utils/resampling.js';
22
+ export { stratifiedSample, stratifiedSampleProportional, stratify } from './utils/stratified.js';
23
+ export { cdf, ppf, sf } from './utils/probability.js';
24
+ export { WeightedDistribution, CategoricalDistribution, NormalDistribution } from './utils/precomputed.js';
25
+ export { bootstrapCI, meanCI, proportionCI } from './utils/confidence.js';
26
+ export { cohensD, hedgesG, correlation, cramersV, etaSquared, glasssDelta } from './utils/effects.js';
27
+ export { sum, mean, min, max, unique, chunk, flatten, zip, transpose, partition } from './utils/arrays.js';
28
+ export { tTest, welchTTest, mannWhitneyU, oneWayAnova } from './utils/hypothesis.js';
29
+ export { zscore, standardize, minMaxScale, logTransform, sqrtTransform, rank, robustScale } from './utils/transforms.js';
30
+ export { LinearRegression, MultipleRegression } from './utils/regression.js';
31
+ export { diff, lag, shift, sma, ema, acf, pacf } from './utils/timeseries.js';
32
+ export { matrixAdd, matrixSub, matrixMul, elementwiseMul, scalarMul, determinant, trace, norm, inverse as matrixInverse } from './utils/matrix.js';
33
+ export { trapezoidal, simpsons, adaptiveSimpson, gaussQuadrature, numericalDerivative, numericalSecondDerivative } from './utils/integration.js';
34
+ export { LinearInterpolator, CubicSplineInterpolator, lagrangeInterpolation, polynomialFit } from './utils/interpolation.js';
35
+ export { bisection, newtonRaphson, secant, falsePosition, fixedPoint } from './utils/rootfinding.js';
36
+ export { eulerMethod, rk4, systemEuler, systemRK4, rk45Adaptive } from './utils/odesolvers.js';
37
+ export { svd, qr, cholesky, lu } from './utils/decomposition.js';
38
+ export { gradientDescent, momentumDescent, adam, simplex, brent } from './utils/optimization.js';
39
+ export { tTestPValue, zTestPValue, fTestPValue, chi2PValue, uTestPValue, pearsonPValue } from './utils/pvalues.js';
40
+ export { residualDiagnostics, leverageValues, cookDistance, durwinWatson, vif } from './utils/diagnostics.js';
41
+ export { LogisticRegression } from './utils/logistic.js';
42
+ export { PolynomialRegression } from './utils/polynomial.js';
43
+ export { KMeans } from './utils/clustering.js';
package/src/core.js CHANGED
@@ -36,3 +36,8 @@ export { bisection, newtonRaphson, secant, falsePosition, fixedPoint } from './u
36
36
  export { eulerMethod, rk4, systemEuler, systemRK4, rk45Adaptive } from './utils/odesolvers.js';
37
37
  export { svd, qr, cholesky, lu } from './utils/decomposition.js';
38
38
  export { gradientDescent, momentumDescent, adam, simplex, brent } from './utils/optimization.js';
39
+ export { tTestPValue, zTestPValue, fTestPValue, chi2PValue, uTestPValue, pearsonPValue } from './utils/pvalues.js';
40
+ export { residualDiagnostics, leverageValues, cookDistance, durwinWatson, vif } from './utils/diagnostics.js';
41
+ export { LogisticRegression } from './utils/logistic.js';
42
+ export { PolynomialRegression } from './utils/polynomial.js';
43
+ export { KMeans } from './utils/clustering.js';
@@ -6,9 +6,15 @@ export {
6
6
  exponential,
7
7
  uniform,
8
8
  poisson,
9
+ normals,
10
+ exponentials,
9
11
  shuffle,
10
12
  pick,
11
13
  sample,
14
+ sampleWithReplacement,
15
+ permute,
16
+ range,
17
+ cycle,
12
18
  saveState,
13
19
  restoreState,
14
20
  cloneGenerator,
@@ -17,7 +23,168 @@ export {
17
23
  reservoirSample,
18
24
  meanTest,
19
25
  varianceTest,
20
- kolmogorovSmirnovTest
26
+ kolmogorovSmirnovTest,
27
+ skewness,
28
+ kurtosis,
29
+ median,
30
+ quantile,
31
+ beta,
32
+ gamma,
33
+ chi2,
34
+ binomial,
35
+ geometric,
36
+ weibull,
37
+ lognormal,
38
+ rayleigh,
39
+ cauchy,
40
+ dirichlet,
41
+ dirichlets,
42
+ mixture,
43
+ mixtures,
44
+ studentT,
45
+ studentTs,
46
+ betaBinomial,
47
+ brownianMotion,
48
+ ornsteinUhlenbeck,
49
+ geometricBrownian,
50
+ categorical,
51
+ multinomial,
52
+ categorical2D,
53
+ perlin2D,
54
+ valueNoise,
55
+ combinations,
56
+ permutations,
57
+ kPermutations,
58
+ randomCombination,
59
+ randomPermutation,
60
+ rotateBits,
61
+ extractBits,
62
+ hammingWeight,
63
+ bitRange,
64
+ popcountNum,
65
+ clz,
66
+ ctz,
67
+ reverseBits,
68
+ setBit,
69
+ clearBit,
70
+ toggleBit,
71
+ SeedSequence,
72
+ seedMultiple,
73
+ seedFromTime,
74
+ seedFromEntropy,
75
+ repeat,
76
+ until,
77
+ times,
78
+ chiSquareTest,
79
+ entropy,
80
+ autocorrelation,
81
+ runTest,
82
+ clearCryptoCache,
83
+ combined,
84
+ bootstrap,
85
+ jackknife,
86
+ crossValidation,
87
+ permutationTest,
88
+ stratifiedSample,
89
+ stratifiedSampleProportional,
90
+ stratify,
91
+ cdf,
92
+ ppf,
93
+ sf,
94
+ WeightedDistribution,
95
+ CategoricalDistribution,
96
+ NormalDistribution,
97
+ bootstrapCI,
98
+ meanCI,
99
+ proportionCI,
100
+ cohensD,
101
+ hedgesG,
102
+ correlation,
103
+ cramersV,
104
+ etaSquared,
105
+ glasssDelta,
106
+ sum,
107
+ mean,
108
+ min,
109
+ max,
110
+ unique,
111
+ chunk,
112
+ flatten,
113
+ zip,
114
+ transpose,
115
+ partition,
116
+ tTest,
117
+ welchTTest,
118
+ mannWhitneyU,
119
+ oneWayAnova,
120
+ zscore,
121
+ standardize,
122
+ minMaxScale,
123
+ logTransform,
124
+ sqrtTransform,
125
+ rank,
126
+ robustScale,
127
+ LinearRegression,
128
+ MultipleRegression,
129
+ diff,
130
+ lag,
131
+ shift,
132
+ sma,
133
+ ema,
134
+ acf,
135
+ pacf,
136
+ matrixAdd,
137
+ matrixSub,
138
+ matrixMul,
139
+ elementwiseMul,
140
+ scalarMul,
141
+ determinant,
142
+ trace,
143
+ norm,
144
+ matrixInverse,
145
+ trapezoidal,
146
+ simpsons,
147
+ adaptiveSimpson,
148
+ gaussQuadrature,
149
+ numericalDerivative,
150
+ numericalSecondDerivative,
151
+ LinearInterpolator,
152
+ CubicSplineInterpolator,
153
+ lagrangeInterpolation,
154
+ polynomialFit,
155
+ bisection,
156
+ newtonRaphson,
157
+ secant,
158
+ falsePosition,
159
+ fixedPoint,
160
+ eulerMethod,
161
+ rk4,
162
+ systemEuler,
163
+ systemRK4,
164
+ rk45Adaptive,
165
+ svd,
166
+ qr,
167
+ cholesky,
168
+ lu,
169
+ gradientDescent,
170
+ momentumDescent,
171
+ adam,
172
+ simplex,
173
+ brent,
174
+ tTestPValue,
175
+ zTestPValue,
176
+ fTestPValue,
177
+ chi2PValue,
178
+ uTestPValue,
179
+ pearsonPValue,
180
+ residualDiagnostics,
181
+ leverageValues,
182
+ cookDistance,
183
+ durwinWatson,
184
+ vif,
185
+ LogisticRegression,
186
+ PolynomialRegression,
187
+ KMeans
21
188
  } from './core.browser.js';
22
189
 
23
190
  export {
package/src/index.js CHANGED
@@ -170,7 +170,21 @@ export {
170
170
  momentumDescent,
171
171
  adam,
172
172
  simplex,
173
- brent
173
+ brent,
174
+ tTestPValue,
175
+ zTestPValue,
176
+ fTestPValue,
177
+ chi2PValue,
178
+ uTestPValue,
179
+ pearsonPValue,
180
+ residualDiagnostics,
181
+ leverageValues,
182
+ cookDistance,
183
+ durwinWatson,
184
+ vif,
185
+ LogisticRegression,
186
+ PolynomialRegression,
187
+ KMeans
174
188
  } from './core.js';
175
189
 
176
190
  export {
@@ -182,3 +196,4 @@ export {
182
196
  Tent,
183
197
  Mixer
184
198
  } from './generators/index.js';
199
+
@@ -0,0 +1,143 @@
1
+ export class KMeans {
2
+ constructor(k, maxIter = 100, tol = 1e-6) {
3
+ if (typeof k !== 'number' || k < 1) {
4
+ throw new RangeError('k must be positive integer');
5
+ }
6
+ if (typeof maxIter !== 'number' || maxIter < 1) {
7
+ throw new RangeError('maxIter must be positive integer');
8
+ }
9
+
10
+ this.k = k;
11
+ this.maxIter = maxIter;
12
+ this.tol = tol;
13
+ this.centroids = null;
14
+ this.labels = null;
15
+ this.inertia = null;
16
+ }
17
+
18
+ fit(X) {
19
+ if (!Array.isArray(X) || X.length === 0) {
20
+ throw new TypeError('X must be non-empty array');
21
+ }
22
+ if (!Array.isArray(X[0]) || X[0].length === 0) {
23
+ throw new TypeError('X must be array of numeric arrays');
24
+ }
25
+
26
+ const n = X.length;
27
+ const d = X[0].length;
28
+
29
+ if (this.k > n) {
30
+ throw new RangeError('k cannot exceed number of samples');
31
+ }
32
+
33
+ this.centroids = this._initializeCentroids(X);
34
+ let prevInertia = Infinity;
35
+
36
+ for (let iter = 0; iter < this.maxIter; iter++) {
37
+ this.labels = this._assignClusters(X);
38
+ const newCentroids = this._updateCentroids(X);
39
+
40
+ this.inertia = this._calculateInertia(X);
41
+
42
+ if (Math.abs(prevInertia - this.inertia) < this.tol) {
43
+ break;
44
+ }
45
+
46
+ prevInertia = this.inertia;
47
+ this.centroids = newCentroids;
48
+ }
49
+
50
+ return this;
51
+ }
52
+
53
+ predict(X) {
54
+ if (this.centroids === null) {
55
+ throw new Error('Model not fitted. Call fit() first.');
56
+ }
57
+ if (!Array.isArray(X) || X.length === 0) {
58
+ throw new TypeError('X must be non-empty array');
59
+ }
60
+
61
+ return this._assignClusters(X);
62
+ }
63
+
64
+ _initializeCentroids(X) {
65
+ const indices = new Set();
66
+ while (indices.size < this.k) {
67
+ indices.add(Math.floor(Math.random() * X.length));
68
+ }
69
+
70
+ return Array.from(indices).map(i => [...X[i]]);
71
+ }
72
+
73
+ _assignClusters(X) {
74
+ const labels = new Array(X.length);
75
+
76
+ for (let i = 0; i < X.length; i++) {
77
+ let minDist = Infinity;
78
+ let bestCluster = 0;
79
+
80
+ for (let j = 0; j < this.k; j++) {
81
+ const dist = this._euclideanDistance(X[i], this.centroids[j]);
82
+ if (dist < minDist) {
83
+ minDist = dist;
84
+ bestCluster = j;
85
+ }
86
+ }
87
+
88
+ labels[i] = bestCluster;
89
+ }
90
+
91
+ return labels;
92
+ }
93
+
94
+ _updateCentroids(X) {
95
+ const newCentroids = [];
96
+ const d = X[0].length;
97
+
98
+ for (let j = 0; j < this.k; j++) {
99
+ const clusterPoints = [];
100
+
101
+ for (let i = 0; i < X.length; i++) {
102
+ if (this.labels[i] === j) {
103
+ clusterPoints.push(X[i]);
104
+ }
105
+ }
106
+
107
+ if (clusterPoints.length === 0) {
108
+ newCentroids.push([...this.centroids[j]]);
109
+ } else {
110
+ const centroid = new Array(d).fill(0);
111
+ for (let dim = 0; dim < d; dim++) {
112
+ let sum = 0;
113
+ for (let point of clusterPoints) {
114
+ sum += point[dim];
115
+ }
116
+ centroid[dim] = sum / clusterPoints.length;
117
+ }
118
+ newCentroids.push(centroid);
119
+ }
120
+ }
121
+
122
+ return newCentroids;
123
+ }
124
+
125
+ _euclideanDistance(a, b) {
126
+ let sum = 0;
127
+ for (let i = 0; i < a.length; i++) {
128
+ sum += (a[i] - b[i]) ** 2;
129
+ }
130
+ return Math.sqrt(sum);
131
+ }
132
+
133
+ _calculateInertia(X) {
134
+ let inertia = 0;
135
+
136
+ for (let i = 0; i < X.length; i++) {
137
+ const dist = this._euclideanDistance(X[i], this.centroids[this.labels[i]]);
138
+ inertia += dist * dist;
139
+ }
140
+
141
+ return inertia;
142
+ }
143
+ }