@nirs4all/methods 1.0.6
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/README.md +135 -0
- package/dist/config.d.ts +16 -0
- package/dist/config.js +58 -0
- package/dist/context.d.ts +18 -0
- package/dist/context.js +44 -0
- package/dist/ffi.d.ts +45 -0
- package/dist/ffi.js +115 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +34 -0
- package/dist/methodResult.d.ts +28 -0
- package/dist/methodResult.js +143 -0
- package/dist/model.d.ts +197 -0
- package/dist/model.js +577 -0
- package/dist/n4m.js +16 -0
- package/dist/n4m.wasm +0 -0
- package/dist/preprocessing.d.ts +16 -0
- package/dist/preprocessing.js +121 -0
- package/dist/types.d.ts +73 -0
- package/dist/types.js +76 -0
- package/package.json +38 -0
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { Matrix } from "./types.js";
|
|
2
|
+
export interface PlsModel {
|
|
3
|
+
/** Regression coefficients, row-major (n_features × n_targets). */
|
|
4
|
+
coefficients: Float64Array;
|
|
5
|
+
/** Per-feature mean used for centring. */
|
|
6
|
+
xMean: Float64Array;
|
|
7
|
+
/** Per-target mean used for centring. */
|
|
8
|
+
yMean: Float64Array;
|
|
9
|
+
/** Number of features `p` and targets `q`. */
|
|
10
|
+
n_features: number;
|
|
11
|
+
n_targets: number;
|
|
12
|
+
}
|
|
13
|
+
/** Fit a SIMPLS PLS-regression model on (X, Y).
|
|
14
|
+
*
|
|
15
|
+
* @param X row-major (n × p) input matrix.
|
|
16
|
+
* @param Y row-major (n × q) target matrix.
|
|
17
|
+
* @param n_components number of latent components.
|
|
18
|
+
*/
|
|
19
|
+
export declare function fitPls(X: Matrix, Y: Matrix, n_components: number): PlsModel;
|
|
20
|
+
/** Predict from a fitted PlsModel for new X (row-major n_new × p). */
|
|
21
|
+
export declare function predictPls(model: PlsModel, X_new: Matrix): Matrix;
|
|
22
|
+
/** A fitted coefficient-based model produced by {@link fitModel}. */
|
|
23
|
+
export interface FittedModel {
|
|
24
|
+
/** Regression coefficients, row-major (n_features × n_targets). */
|
|
25
|
+
coefficients: Float64Array;
|
|
26
|
+
/** Per-feature mean used for centring. */
|
|
27
|
+
xMean: Float64Array;
|
|
28
|
+
/** Per-target mean used for centring. */
|
|
29
|
+
yMean: Float64Array;
|
|
30
|
+
/** Per-target intercept (null when the model centres without one). */
|
|
31
|
+
intercept: Float64Array | null;
|
|
32
|
+
n_features: number;
|
|
33
|
+
n_targets: number;
|
|
34
|
+
}
|
|
35
|
+
/** Fit any coefficient-based libn4m model by its catalog `type` token.
|
|
36
|
+
*
|
|
37
|
+
* Tier A (PLS / PLSRegression / PCR / PLSCanonical / PLSSVD / PLSDA) routes
|
|
38
|
+
* through the algorithm-enum model API; Tier B (Ridge, RidgePLS, CPPLS, ...)
|
|
39
|
+
* through the matching standalone fit. The `params` vector is the documented
|
|
40
|
+
* positional contract per model (see the studio-lite catalog). Unknown or
|
|
41
|
+
* non-coefficient tokens throw (the C side returns N4M_ERR_NOT_IMPLEMENTED).
|
|
42
|
+
*
|
|
43
|
+
* @param model catalog `type` token, e.g. `'Ridge'`.
|
|
44
|
+
* @param X row-major (n × p) input matrix.
|
|
45
|
+
* @param Y row-major (n × q) target matrix.
|
|
46
|
+
* @param n_components number of latent components (used by the PLS family).
|
|
47
|
+
* @param params positional hyper-parameter vector for the model.
|
|
48
|
+
*/
|
|
49
|
+
export declare function fitModel(model: string, X: Matrix, Y: Matrix, n_components: number, params?: number[]): FittedModel;
|
|
50
|
+
/** Predict from a fitted {@link FittedModel} for new X (row-major n_new × p). */
|
|
51
|
+
export declare function predictModel(model: FittedModel, X_new: Matrix): Matrix;
|
|
52
|
+
/** A fitted AOM-PLS model — a {@link FittedModel} (so {@link predictModel}
|
|
53
|
+
* works unchanged) plus the screen result. Its `intercept` is a genuine
|
|
54
|
+
* input-space intercept and its `xMean` / `yMean` are zero, so prediction is
|
|
55
|
+
* the affine form y = intercept + X.B on RAW X. */
|
|
56
|
+
export interface AomModel extends FittedModel {
|
|
57
|
+
/** Bank index of the operator the internal CV selected. */
|
|
58
|
+
selectedOperator: number;
|
|
59
|
+
/** Best internal-CV score of the selected operator. */
|
|
60
|
+
score: number;
|
|
61
|
+
}
|
|
62
|
+
/** Fit AOM-PLS (operator-adaptive PLS) on (X, Y).
|
|
63
|
+
*
|
|
64
|
+
* Screens a bank of strict-linear preprocessing operators by internal k-fold CV
|
|
65
|
+
* and fits SIMPLS on the winner, returning INPUT-SPACE coefficients so the model
|
|
66
|
+
* predicts on RAW X — it is therefore used WITHOUT preceding preprocessing steps
|
|
67
|
+
* (the screen does the preprocessing internally). Numerics are 100% libn4m
|
|
68
|
+
* (`n4m_model_selection_aom_pls_select`); this only builds the bank + validation plan.
|
|
69
|
+
*
|
|
70
|
+
* @param X row-major (n × p) input matrix.
|
|
71
|
+
* @param Y row-major (n × q) target matrix.
|
|
72
|
+
* @param maxComponents max latent components for the internal SIMPLS fits.
|
|
73
|
+
* @param nFolds internal-CV fold count for the operator screen.
|
|
74
|
+
* @param seed reserved (the contiguous-fold partition is deterministic).
|
|
75
|
+
* @param operatorKinds optional `n4m_operator_kind_t` bank override; when
|
|
76
|
+
* omitted a default strict bank (identity / detrend / SG smooth / SG
|
|
77
|
+
* derivative / finite-difference) is screened.
|
|
78
|
+
*/
|
|
79
|
+
export declare function fitAom(X: Matrix, Y: Matrix, maxComponents: number, nFolds?: number, seed?: number, operatorKinds?: number[]): AomModel;
|
|
80
|
+
/** A fitted POP-PLS model — a {@link FittedModel} (so {@link predictModel}
|
|
81
|
+
* works unchanged) plus the per-component screen result. Its `intercept` is a
|
|
82
|
+
* genuine input-space intercept and its `xMean` / `yMean` are zero, so
|
|
83
|
+
* prediction is the affine form y = intercept + X.B on RAW X. */
|
|
84
|
+
export interface PopModel extends FittedModel {
|
|
85
|
+
/** Bank index of the operator picked at each selected latent component
|
|
86
|
+
* (length = `selectedComponents`). */
|
|
87
|
+
selectedOperators: number[];
|
|
88
|
+
/** Number of latent components the per-component screen selected. */
|
|
89
|
+
selectedComponents: number;
|
|
90
|
+
/** Best internal-CV prefix score of the selected model. */
|
|
91
|
+
score: number;
|
|
92
|
+
}
|
|
93
|
+
/** Fit POP-PLS (per-component operator-adaptive PLS) on (X, Y).
|
|
94
|
+
*
|
|
95
|
+
* Like AOM-PLS but picks one strict-linear operator PER latent component
|
|
96
|
+
* (`n4m_model_selection_pop_pls_select`) rather than one for the whole model, then
|
|
97
|
+
* returns INPUT-SPACE coefficients so it predicts on RAW X via the same affine
|
|
98
|
+
* intercept path — so it is used WITHOUT preceding preprocessing steps (the
|
|
99
|
+
* screen does the preprocessing internally). Numerics are 100% libn4m; this
|
|
100
|
+
* only builds the bank + validation plan.
|
|
101
|
+
*
|
|
102
|
+
* @param X row-major (n × p) input matrix.
|
|
103
|
+
* @param Y row-major (n × q) target matrix.
|
|
104
|
+
* @param maxComponents max latent components for the internal SIMPLS fits.
|
|
105
|
+
* @param nFolds internal-CV fold count for the operator screen.
|
|
106
|
+
* @param seed reserved (the contiguous-fold partition is deterministic).
|
|
107
|
+
* @param operatorKinds optional `n4m_operator_kind_t` bank override; when
|
|
108
|
+
* omitted a default strict bank (identity / detrend / SG smooth / SG
|
|
109
|
+
* derivative / finite-difference) is screened.
|
|
110
|
+
*/
|
|
111
|
+
export declare function fitPop(X: Matrix, Y: Matrix, maxComponents: number, nFolds?: number, seed?: number, operatorKinds?: number[]): PopModel;
|
|
112
|
+
/** Options for the AOM Ridge simplex blender. */
|
|
113
|
+
export interface AomRidgeOptions {
|
|
114
|
+
/** operator/chain bank profile: 0 = compact, 1 = wide (default 0). */
|
|
115
|
+
profile?: number;
|
|
116
|
+
/** internal CV folds for OOF Ridge scoring (default 5). */
|
|
117
|
+
cv?: number;
|
|
118
|
+
/** Ridge λ candidate grid; omit for a default log grid. */
|
|
119
|
+
ridgeLambdas?: number[];
|
|
120
|
+
/** non-negative shrinkage of the simplex blend toward uniform (default 0.01). */
|
|
121
|
+
regularizer?: number;
|
|
122
|
+
}
|
|
123
|
+
/** Fit the AOM Ridge simplex blender (n4m_ensemble_aom_ridge_blender_fit): builds
|
|
124
|
+
* a strict-linear chain bank internally, OOF-blends (chain, λ) Ridge candidates
|
|
125
|
+
* over `cv` contiguous folds, and returns the weighted final INPUT-SPACE
|
|
126
|
+
* coefficients + intercept — so it predicts on RAW X via the affine form
|
|
127
|
+
* y = intercept + X.B (used WITHOUT preceding preprocessing). */
|
|
128
|
+
export declare function fitAomRidge(X: Matrix, Y: Matrix, opts?: AomRidgeOptions): FittedModel;
|
|
129
|
+
/** Options for the AOM operator-PLS score stack (Ridge head). */
|
|
130
|
+
export interface AomStackOptions {
|
|
131
|
+
/** operator bank profile: 0 = compact, 1 = wide (default 0). */
|
|
132
|
+
profile?: number;
|
|
133
|
+
/** internal CV folds for the (n_components, alpha) screen (default 5). */
|
|
134
|
+
cv?: number;
|
|
135
|
+
/** component grid endpoint — screens [1..maxComponents] (default 15). */
|
|
136
|
+
maxComponents?: number;
|
|
137
|
+
/** Ridge-head α grid; omit for a default log grid. */
|
|
138
|
+
alphas?: number[];
|
|
139
|
+
/** non-negative penalty on OOF-RMSE std in the selection criterion (default 0). */
|
|
140
|
+
stdPenalty?: number;
|
|
141
|
+
/** non-negative penalty on (mean_oof_rmse - mean_train_rmse) (default 0). */
|
|
142
|
+
gapPenalty?: number;
|
|
143
|
+
}
|
|
144
|
+
/** Fit the AOM operator-PLS score stack with Ridge head
|
|
145
|
+
* (n4m_ensemble_aom_operator_pls_stack_fit). SINGLE-TARGET only (Y must be
|
|
146
|
+
* n × 1). Returns the stack folded into INPUT-SPACE coefficients + intercept,
|
|
147
|
+
* so it predicts on RAW X via the affine form (used WITHOUT preprocessing). */
|
|
148
|
+
export declare function fitAomStack(X: Matrix, Y: Matrix, opts?: AomStackOptions): FittedModel;
|
|
149
|
+
/** A train/test splitter kind for {@link computeSplit}. */
|
|
150
|
+
export type SplitKind = "KennardStone" | "SPXY" | "KMeans" | "KBinsStratified" | "DataTwinning" | "SystematicCircular";
|
|
151
|
+
/** Options for {@link computeSplit}. `testSize` is a fraction in (0, 1). */
|
|
152
|
+
export interface SplitOptions {
|
|
153
|
+
/** test fraction in (0, 1); default 0.25. */
|
|
154
|
+
testSize?: number;
|
|
155
|
+
/** seed for the stochastic splitters (KMeans, KBinsStratified). */
|
|
156
|
+
seed?: number;
|
|
157
|
+
/** KMeans: max iterations (default 100). */
|
|
158
|
+
maxIter?: number;
|
|
159
|
+
/** KBinsStratified: number of Y bins (default 5). */
|
|
160
|
+
nBins?: number;
|
|
161
|
+
/** KBinsStratified: 0 = uniform-width bins, 1 = quantile bins. */
|
|
162
|
+
strategy?: number;
|
|
163
|
+
}
|
|
164
|
+
/** Ordered row indices returned by libn4m splitters. */
|
|
165
|
+
export interface SplitIndices {
|
|
166
|
+
trainIndices: Int32Array;
|
|
167
|
+
testIndices: Int32Array;
|
|
168
|
+
}
|
|
169
|
+
/** Compute a single train/test split over the rows of X (and Y) via libn4m's
|
|
170
|
+
* splitters, returning a `Uint8Array` mask of length n where 1 = test, 0 = train.
|
|
171
|
+
*
|
|
172
|
+
* Numerics are 100% libn4m (`n4m_wasm_split` → n4m_split_*). KennardStone and
|
|
173
|
+
* SPXY are deterministic; KMeans and KBinsStratified use `opts.seed`. SPXY and
|
|
174
|
+
* KBinsStratified need Y; KennardStone and KMeans use X only.
|
|
175
|
+
*
|
|
176
|
+
* @param kind splitter strategy.
|
|
177
|
+
* @param X row-major (n × p) input matrix.
|
|
178
|
+
* @param Y row-major (n × q) target matrix (required for SPXY / KBinsStratified).
|
|
179
|
+
* @param opts split options (testSize / seed / maxIter / nBins / strategy).
|
|
180
|
+
*/
|
|
181
|
+
export declare function computeSplit(kind: SplitKind, X: Matrix, Y: Matrix | null, opts?: SplitOptions): Uint8Array;
|
|
182
|
+
/** Compute a train/test split and return the ordered train/test indices from libn4m.
|
|
183
|
+
*
|
|
184
|
+
* Unlike {@link computeSplit}, this preserves splitter-specific ordering, which matters
|
|
185
|
+
* for strict parity with the native Python binding.
|
|
186
|
+
*/
|
|
187
|
+
export declare function computeSplitIndices(kind: SplitKind, X: Matrix, Y: Matrix | null, opts?: SplitOptions): SplitIndices;
|
|
188
|
+
export declare class Model {
|
|
189
|
+
private _data;
|
|
190
|
+
private constructor();
|
|
191
|
+
static fit(_ctx: unknown, _cfg: unknown, X: Matrix, Y: Matrix, n_components?: number): Model;
|
|
192
|
+
predict(_ctx: unknown, X_new: Matrix): Matrix;
|
|
193
|
+
get coefficients(): Float64Array;
|
|
194
|
+
get xMean(): Float64Array;
|
|
195
|
+
get yMean(): Float64Array;
|
|
196
|
+
destroy(): void;
|
|
197
|
+
}
|