Launch Week Day 3: Introducing Organization Notifications in Socket.Learn More
Socket
Book a DemoSign in
Socket

ml-regression-base

Package Overview
Dependencies
Maintainers
7
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-regression-base - npm Package Compare versions

Comparing version
3.0.0
to
4.0.0
+24
lib-esm/BaseRegression.d.ts
import { type NumberArray } from 'cheminfo-types';
export interface RegressionScore {
r: number;
r2: number;
chi2: number;
rmsd: number;
}
export declare class BaseRegression {
constructor();
predict(x: number): number;
predict(x: NumberArray): number[];
_predict(x: number): number;
train(): void;
toString(precision?: number): string;
toLaTeX(precision?: number): string;
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x: NumberArray, y: NumberArray): RegressionScore;
}
//# sourceMappingURL=BaseRegression.d.ts.map
{"version":3,"file":"BaseRegression.d.ts","sourceRoot":"","sources":["../src/BaseRegression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAKlD,MAAM,WAAW,eAAe;IAC9B,CAAC,EAAE,MAAM,CAAC;IACV,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AACD,qBAAa,cAAc;;IAOzB,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAC1B,OAAO,CAAC,CAAC,EAAE,WAAW,GAAG,MAAM,EAAE;IAgBjC,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAI3B,KAAK;IAKL,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM;IAK3B,OAAO,CAAC,SAAS,CAAC,EAAE,MAAM;IAI1B;;;;;OAKG;IACH,KAAK,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,GAAG,eAAe;CAuCvD"}
import { isAnyArray } from 'is-any-array';
import { checkArrayLength } from './checkArrayLength';
export class BaseRegression {
constructor() {
if (new.target === BaseRegression) {
throw new Error('BaseRegression must be subclassed');
}
}
predict(x) {
if (typeof x === 'number') {
return this._predict(x);
}
else if (isAnyArray(x)) {
const y = [];
for (const xVal of x) {
y.push(this._predict(xVal));
}
return y;
}
else {
throw new TypeError('x must be a number or array');
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_predict(x) {
throw new Error('_predict must be implemented');
}
train() {
// Do nothing for this package
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toString(precision) {
return '';
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toLaTeX(precision) {
return '';
}
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x, y) {
checkArrayLength(x, y);
const n = x.length;
const y2 = new Array(n);
for (let i = 0; i < n; i++) {
y2[i] = this._predict(x[i]);
}
let xSum = 0;
let ySum = 0;
let chi2 = 0;
let rmsd = 0;
let xSquared = 0;
let ySquared = 0;
let xY = 0;
for (let i = 0; i < n; i++) {
xSum += y2[i];
ySum += y[i];
xSquared += y2[i] * y2[i];
ySquared += y[i] * y[i];
xY += y2[i] * y[i];
if (y[i] !== 0) {
chi2 += ((y[i] - y2[i]) * (y[i] - y2[i])) / y[i];
}
rmsd += (y[i] - y2[i]) * (y[i] - y2[i]);
}
const r = (n * xY - xSum * ySum) /
Math.sqrt((n * xSquared - xSum * xSum) * (n * ySquared - ySum * ySum));
return {
r,
r2: r * r,
chi2,
rmsd: Math.sqrt(rmsd / n),
};
}
}
//# sourceMappingURL=BaseRegression.js.map
{"version":3,"file":"BaseRegression.js","sourceRoot":"","sources":["../src/BaseRegression.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAQtD,MAAM,OAAO,cAAc;IACzB;QACE,IAAI,GAAG,CAAC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GAAG,IAAI,GAAG,IAAI,CAAC;YACtB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC;QAEzE,OAAO;YACL,CAAC;YACD,EAAE,EAAE,CAAC,GAAG,CAAC;YACT,IAAI;YACJ,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;SAC1B,CAAC;IACJ,CAAC;CACF"}
import { type NumberArray } from 'cheminfo-types';
export interface RegressionScore {
r: number;
r2: number;
chi2: number;
rmsd: number;
}
export declare class BaseRegression {
constructor();
predict(x: number): number;
predict(x: NumberArray): number[];
_predict(x: number): number;
train(): void;
toString(precision?: number): string;
toLaTeX(precision?: number): string;
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x: NumberArray, y: NumberArray): RegressionScore;
}
//# sourceMappingURL=BaseRegression.d.ts.map
{"version":3,"file":"BaseRegression.d.ts","sourceRoot":"","sources":["../src/BaseRegression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAKlD,MAAM,WAAW,eAAe;IAC9B,CAAC,EAAE,MAAM,CAAC;IACV,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AACD,qBAAa,cAAc;;IAOzB,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAC1B,OAAO,CAAC,CAAC,EAAE,WAAW,GAAG,MAAM,EAAE;IAgBjC,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAI3B,KAAK;IAKL,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM;IAK3B,OAAO,CAAC,SAAS,CAAC,EAAE,MAAM;IAI1B;;;;;OAKG;IACH,KAAK,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,GAAG,eAAe;CAuCvD"}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseRegression = void 0;
const is_any_array_1 = require("is-any-array");
const checkArrayLength_1 = require("./checkArrayLength");
class BaseRegression {
constructor() {
if (new.target === BaseRegression) {
throw new Error('BaseRegression must be subclassed');
}
}
predict(x) {
if (typeof x === 'number') {
return this._predict(x);
}
else if ((0, is_any_array_1.isAnyArray)(x)) {
const y = [];
for (const xVal of x) {
y.push(this._predict(xVal));
}
return y;
}
else {
throw new TypeError('x must be a number or array');
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_predict(x) {
throw new Error('_predict must be implemented');
}
train() {
// Do nothing for this package
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toString(precision) {
return '';
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toLaTeX(precision) {
return '';
}
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x, y) {
(0, checkArrayLength_1.checkArrayLength)(x, y);
const n = x.length;
const y2 = new Array(n);
for (let i = 0; i < n; i++) {
y2[i] = this._predict(x[i]);
}
let xSum = 0;
let ySum = 0;
let chi2 = 0;
let rmsd = 0;
let xSquared = 0;
let ySquared = 0;
let xY = 0;
for (let i = 0; i < n; i++) {
xSum += y2[i];
ySum += y[i];
xSquared += y2[i] * y2[i];
ySquared += y[i] * y[i];
xY += y2[i] * y[i];
if (y[i] !== 0) {
chi2 += ((y[i] - y2[i]) * (y[i] - y2[i])) / y[i];
}
rmsd += (y[i] - y2[i]) * (y[i] - y2[i]);
}
const r = (n * xY - xSum * ySum) /
Math.sqrt((n * xSquared - xSum * xSum) * (n * ySquared - ySum * ySum));
return {
r,
r2: r * r,
chi2,
rmsd: Math.sqrt(rmsd / n),
};
}
}
exports.BaseRegression = BaseRegression;
//# sourceMappingURL=B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AAG,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1C,CAAC;QAED,MAAM,CAAC,GACL,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;YACtB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC;QAEzE,OAAO;YACL,CAAC;YACD,EAAE,EAAE,CAAC,GAAG,CAAC;YACT,IAAI;YACJ,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;SAC1B,CAAC;IACJ,CAAC;CACF;AAvFD,wCAuFC"}
import { expect, it, describe } from 'vitest';
import { BaseRegression } from '..';
class NoPredict extends BaseRegression {}
class Basic extends BaseRegression {
factor: number;
constructor(factor: number) {
super();
this.factor = factor;
}
_predict(x: number) {
return x * this.factor;
}
}
describe('base regression', () => {
it('should not be directly constructable', () => {
expect(() => {
// eslint-disable-next-line no-new
new BaseRegression();
}).toThrow(/BaseRegression must be subclassed/);
});
it('should throw if _predict is not implemented', () => {
const reg = new NoPredict();
expect(() => {
reg.predict(0);
}).toThrow(/_predict must be implemented/);
});
it('should do a basic prediction', () => {
const basic = new Basic(2);
expect(basic.predict(1)).toBe(2);
expect(basic.predict(2)).toBe(4);
expect(basic.predict([2, 3])).toStrictEqual([4, 6]);
});
it('should throw on invalid value', () => {
const basic = new Basic(2);
expect(() => {
// @ts-expect-error testing invalid input
basic.predict();
}).toThrow(/must be a number or array/);
});
it('should implement dummy predictor functions', () => {
const basic = new Basic(2);
basic.train(); // should not throw
expect(basic.toString()).toBe('');
expect(basic.toLaTeX()).toBe('');
});
it('should implement a scoring function', () => {
const basic = new Basic(2);
expect(basic.score([1, 2], [2, 4])).toStrictEqual({
r: 1,
r2: 1,
chi2: 0,
rmsd: 0,
});
expect(basic.score([1, 2], [2, 4.1]).rmsd).toBe(0.0707106781186545);
expect(basic.score([1, 2], [0.5, 2])).toStrictEqual({
r: 1,
r2: 1,
chi2: 6.5,
rmsd: 1.7677669529663689,
});
expect(basic.score([1, 2], [0, 1])).toStrictEqual({
r: 1,
r2: 1,
chi2: 9,
rmsd: 2.5495097567963922,
});
});
it('should throw error if inputs are not arrays or has different length', () => {
const basic = new Basic(2);
expect(() => {
// @ts-expect-error testing invalid input
basic.score(1, 2);
}).toThrow('x and y must be arrays');
expect(() => {
basic.score([1, 2], [2]);
}).toThrow('x and y arrays must have the same length');
});
});
import { type NumberArray } from 'cheminfo-types';
import { isAnyArray } from 'is-any-array';
import { checkArrayLength } from './checkArrayLength';
export interface RegressionScore {
r: number;
r2: number;
chi2: number;
rmsd: number;
}
export class BaseRegression {
constructor() {
if (new.target === BaseRegression) {
throw new Error('BaseRegression must be subclassed');
}
}
predict(x: number): number;
predict(x: NumberArray): number[];
predict(x: number | NumberArray) {
if (typeof x === 'number') {
return this._predict(x);
} else if (isAnyArray(x)) {
const y = [];
for (const xVal of x) {
y.push(this._predict(xVal));
}
return y;
} else {
throw new TypeError('x must be a number or array');
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_predict(x: number): number {
throw new Error('_predict must be implemented');
}
train() {
// Do nothing for this package
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toString(precision?: number) {
return '';
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toLaTeX(precision?: number) {
return '';
}
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x: NumberArray, y: NumberArray): RegressionScore {
checkArrayLength(x, y);
const n = x.length;
const y2: number[] = new Array(n);
for (let i = 0; i < n; i++) {
y2[i] = this._predict(x[i]);
}
let xSum = 0;
let ySum = 0;
let chi2 = 0;
let rmsd = 0;
let xSquared = 0;
let ySquared = 0;
let xY = 0;
for (let i = 0; i < n; i++) {
xSum += y2[i];
ySum += y[i];
xSquared += y2[i] * y2[i];
ySquared += y[i] * y[i];
xY += y2[i] * y[i];
if (y[i] !== 0) {
chi2 += ((y[i] - y2[i]) * (y[i] - y2[i])) / y[i];
}
rmsd += (y[i] - y2[i]) * (y[i] - y2[i]);
}
const r =
(n * xY - xSum * ySum) /
Math.sqrt((n * xSquared - xSum * xSum) * (n * ySquared - ySum * ySum));
return {
r,
r2: r * r,
chi2,
rmsd: Math.sqrt(rmsd / n),
};
}
}
+1
-1

@@ -8,3 +8,3 @@ import { NumberArray } from 'cheminfo-types';

*/
export default function checkArrayLength(x: NumberArray, y: NumberArray): void;
export declare function checkArrayLength(x: NumberArray, y: NumberArray): void;
//# sourceMappingURL=checkArrayLength.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"checkArrayLength.d.ts","sourceRoot":"","sources":["../src/checkArrayLength.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,QAOtE"}
{"version":3,"file":"checkArrayLength.d.ts","sourceRoot":"","sources":["../src/checkArrayLength.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,QAO9D"}

@@ -8,3 +8,3 @@ import { isAnyArray } from 'is-any-array';

*/
export default function checkArrayLength(x, y) {
export function checkArrayLength(x, y) {
if (!isAnyArray(x) || !isAnyArray(y)) {

@@ -11,0 +11,0 @@ throw new TypeError('x and y must be arrays');

@@ -1,1 +0,1 @@

{"version":3,"file":"checkArrayLength.js","sourceRoot":"","sources":["../src/checkArrayLength.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,CAAc,EAAE,CAAc;IACrE,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE;QACpC,MAAM,IAAI,SAAS,CAAC,wBAAwB,CAAC,CAAC;KAC/C;IACD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE;QACzB,MAAM,IAAI,UAAU,CAAC,0CAA0C,CAAC,CAAC;KAClE;AACH,CAAC"}
{"version":3,"file":"checkArrayLength.js","sourceRoot":"","sources":["../src/checkArrayLength.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,CAAc,EAAE,CAAc;IAC7D,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,SAAS,CAAC,wBAAwB,CAAC,CAAC;IAChD,CAAC;IACD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;QAC1B,MAAM,IAAI,UAAU,CAAC,0CAA0C,CAAC,CAAC;IACnE,CAAC;AACH,CAAC"}

@@ -1,27 +0,4 @@

import { type NumberArray } from 'cheminfo-types';
import checkArrayLength from './checkArrayLength';
export { default as maybeToPrecision } from './maybeToPrecision';
export interface RegressionScore {
r: number;
r2: number;
chi2: number;
rmsd: number;
}
export default class BaseRegression {
constructor();
predict(x: number): number;
predict(x: NumberArray): number[];
_predict(x: number): number;
train(): void;
toString(precision?: number): string;
toLaTeX(precision?: number): string;
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x: NumberArray, y: NumberArray): RegressionScore;
}
export { checkArrayLength, type NumberArray };
export * from './BaseRegression';
export * from './checkArrayLength';
export * from './maybeToPrecision';
//# sourceMappingURL=index.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAGlD,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEjE,MAAM,WAAW,eAAe;IAC9B,CAAC,EAAE,MAAM,CAAC;IACV,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AACD,MAAM,CAAC,OAAO,OAAO,cAAc;;IAOjC,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAC1B,OAAO,CAAC,CAAC,EAAE,WAAW,GAAG,MAAM,EAAE;IAgBjC,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAI3B,KAAK;IAKL,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM;IAK3B,OAAO,CAAC,SAAS,CAAC,EAAE,MAAM;IAI1B;;;;;OAKG;IACH,KAAK,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,GAAG,eAAe;CAuCvD;AAED,OAAO,EAAE,gBAAgB,EAAE,KAAK,WAAW,EAAE,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC"}

@@ -1,82 +0,4 @@

import { isAnyArray } from 'is-any-array';
import checkArrayLength from './checkArrayLength';
export { default as maybeToPrecision } from './maybeToPrecision';
export default class BaseRegression {
constructor() {
if (new.target === BaseRegression) {
throw new Error('BaseRegression must be subclassed');
}
}
predict(x) {
if (typeof x === 'number') {
return this._predict(x);
}
else if (isAnyArray(x)) {
const y = [];
for (const xVal of x) {
y.push(this._predict(xVal));
}
return y;
}
else {
throw new TypeError('x must be a number or array');
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_predict(x) {
throw new Error('_predict must be implemented');
}
train() {
// Do nothing for this package
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toString(precision) {
return '';
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toLaTeX(precision) {
return '';
}
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x, y) {
checkArrayLength(x, y);
const n = x.length;
const y2 = new Array(n);
for (let i = 0; i < n; i++) {
y2[i] = this._predict(x[i]);
}
let xSum = 0;
let ySum = 0;
let chi2 = 0;
let rmsd = 0;
let xSquared = 0;
let ySquared = 0;
let xY = 0;
for (let i = 0; i < n; i++) {
xSum += y2[i];
ySum += y[i];
xSquared += y2[i] * y2[i];
ySquared += y[i] * y[i];
xY += y2[i] * y[i];
if (y[i] !== 0) {
chi2 += ((y[i] - y2[i]) * (y[i] - y2[i])) / y[i];
}
rmsd += (y[i] - y2[i]) * (y[i] - y2[i]);
}
const r = (n * xY - xSum * ySum) /
Math.sqrt((n * xSquared - xSum * xSum) * (n * ySquared - ySum * ySum));
return {
r,
r2: r * r,
chi2,
rmsd: Math.sqrt(rmsd / n),
};
}
}
export { checkArrayLength };
export * from './BaseRegression';
export * from './checkArrayLength';
export * from './maybeToPrecision';
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAQjE,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CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC;QAEzE,OAAO;YACL,CAAC;YACD,EAAE,EAAE,CAAC,GAAG,CAAC;YACT,IAAI;YACJ,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;SAC1B,CAAC;IACJ,CAAC;CACF;AAED,OAAO,EAAE,gBAAgB,EAAoB,CAAC"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC"}

@@ -7,3 +7,3 @@ /**

*/
export default function maybeToPrecision(number: number, figures?: number): string;
export declare function maybeToPrecision(number: number, figures?: number): string;
//# sourceMappingURL=maybeToPrecision.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"maybeToPrecision.d.ts","sourceRoot":"","sources":["../src/maybeToPrecision.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,UAaxE"}
{"version":3,"file":"maybeToPrecision.d.ts","sourceRoot":"","sources":["../src/maybeToPrecision.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,UAahE"}

@@ -7,3 +7,3 @@ /**

*/
export default function maybeToPrecision(number, figures) {
export function maybeToPrecision(number, figures) {
if (number < 0) {

@@ -10,0 +10,0 @@ number = 0 - number;

@@ -1,1 +0,1 @@

{"version":3,"file":"maybeToPrecision.js","sourceRoot":"","sources":["../src/maybeToPrecision.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,MAAc,EAAE,OAAgB;IACvE,IAAI,MAAM,GAAG,CAAC,EAAE;QACd,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC;QACpB,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;YAC/B,OAAO,KAAK,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;SAC3C;aAAM;YACL,OAAO,KAAK,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;SACjC;KACF;SAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QACtC,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;KACpC;SAAM;QACL,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;KAC1B;AACH,CAAC"}
{"version":3,"file":"maybeToPrecision.js","sourceRoot":"","sources":["../src/maybeToPrecision.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAc,EAAE,OAAgB;IAC/D,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QACf,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC;QACpB,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAChC,OAAO,KAAK,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5C,CAAC;aAAM,CAAC;YACN,OAAO,KAAK,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;QAClC,CAAC;IACH,CAAC;SAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QACvC,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;AACH,CAAC"}

@@ -8,3 +8,3 @@ import { NumberArray } from 'cheminfo-types';

*/
export default function checkArrayLength(x: NumberArray, y: NumberArray): void;
export declare function checkArrayLength(x: NumberArray, y: NumberArray): void;
//# sourceMappingURL=checkArrayLength.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"checkArrayLength.d.ts","sourceRoot":"","sources":["../src/checkArrayLength.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,QAOtE"}
{"version":3,"file":"checkArrayLength.d.ts","sourceRoot":"","sources":["../src/checkArrayLength.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,QAO9D"}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkArrayLength = void 0;
const is_any_array_1 = require("is-any-array");

@@ -18,3 +19,3 @@ /**

}
exports.default = checkArrayLength;
exports.checkArrayLength = checkArrayLength;
//# sourceMappingURL=checkArrayLength.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"checkArrayLength.js","sourceRoot":"","sources":["../src/checkArrayLength.ts"],"names":[],"mappings":";;AACA,+CAA0C;AAC1C;;;;;GAKG;AACH,SAAwB,gBAAgB,CAAC,CAAc,EAAE,CAAc;IACrE,IAAI,CAAC,IAAA,yBAAU,EAAC,CAAC,CAAC,IAAI,CAAC,IAAA,yBAAU,EAAC,CAAC,CAAC,EAAE;QACpC,MAAM,IAAI,SAAS,CAAC,wBAAwB,CAAC,CAAC;KAC/C;IACD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE;QACzB,MAAM,IAAI,UAAU,CAAC,0CAA0C,CAAC,CAAC;KAClE;AACH,CAAC;AAPD,mCAOC"}
{"version":3,"file":"checkArrayLength.js","sourceRoot":"","sources":["../src/checkArrayLength.ts"],"names":[],"mappings":";;;AACA,+CAA0C;AAC1C;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,CAAc,EAAE,CAAc;IAC7D,IAAI,CAAC,IAAA,yBAAU,EAAC,CAAC,CAAC,IAAI,CAAC,IAAA,yBAAU,EAAC,CAAC,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,SAAS,CAAC,wBAAwB,CAAC,CAAC;IAChD,CAAC;IACD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;QAC1B,MAAM,IAAI,UAAU,CAAC,0CAA0C,CAAC,CAAC;IACnE,CAAC;AACH,CAAC;AAPD,4CAOC"}

@@ -1,27 +0,4 @@

import { type NumberArray } from 'cheminfo-types';
import checkArrayLength from './checkArrayLength';
export { default as maybeToPrecision } from './maybeToPrecision';
export interface RegressionScore {
r: number;
r2: number;
chi2: number;
rmsd: number;
}
export default class BaseRegression {
constructor();
predict(x: number): number;
predict(x: NumberArray): number[];
_predict(x: number): number;
train(): void;
toString(precision?: number): string;
toLaTeX(precision?: number): string;
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x: NumberArray, y: NumberArray): RegressionScore;
}
export { checkArrayLength, type NumberArray };
export * from './BaseRegression';
export * from './checkArrayLength';
export * from './maybeToPrecision';
//# sourceMappingURL=index.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAGlD,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEjE,MAAM,WAAW,eAAe;IAC9B,CAAC,EAAE,MAAM,CAAC;IACV,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AACD,MAAM,CAAC,OAAO,OAAO,cAAc;;IAOjC,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAC1B,OAAO,CAAC,CAAC,EAAE,WAAW,GAAG,MAAM,EAAE;IAgBjC,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAI3B,KAAK;IAKL,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM;IAK3B,OAAO,CAAC,SAAS,CAAC,EAAE,MAAM;IAI1B;;;;;OAKG;IACH,KAAK,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,GAAG,eAAe;CAuCvD;AAED,OAAO,EAAE,gBAAgB,EAAE,KAAK,WAAW,EAAE,CAAC"}
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC"}
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkArrayLength = exports.maybeToPrecision = void 0;
const is_any_array_1 = require("is-any-array");
const checkArrayLength_1 = __importDefault(require("./checkArrayLength"));
exports.checkArrayLength = checkArrayLength_1.default;
var maybeToPrecision_1 = require("./maybeToPrecision");
Object.defineProperty(exports, "maybeToPrecision", { enumerable: true, get: function () { return __importDefault(maybeToPrecision_1).default; } });
class BaseRegression {
constructor() {
if (new.target === BaseRegression) {
throw new Error('BaseRegression must be subclassed');
}
}
predict(x) {
if (typeof x === 'number') {
return this._predict(x);
}
else if ((0, is_any_array_1.isAnyArray)(x)) {
const y = [];
for (const xVal of x) {
y.push(this._predict(xVal));
}
return y;
}
else {
throw new TypeError('x must be a number or array');
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_predict(x) {
throw new Error('_predict must be implemented');
}
train() {
// Do nothing for this package
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toString(precision) {
return '';
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toLaTeX(precision) {
return '';
}
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x, y) {
(0, checkArrayLength_1.default)(x, y);
const n = x.length;
const y2 = new Array(n);
for (let i = 0; i < n; i++) {
y2[i] = this._predict(x[i]);
}
let xSum = 0;
let ySum = 0;
let chi2 = 0;
let rmsd = 0;
let xSquared = 0;
let ySquared = 0;
let xY = 0;
for (let i = 0; i < n; i++) {
xSum += y2[i];
ySum += y[i];
xSquared += y2[i] * y2[i];
ySquared += y[i] * y[i];
xY += y2[i] * y[i];
if (y[i] !== 0) {
chi2 += ((y[i] - y2[i]) * (y[i] - y2[i])) / y[i];
}
rmsd += (y[i] - y2[i]) * (y[i] - y2[i]);
}
const r = (n * xY - xSum * ySum) /
Math.sqrt((n * xSquared - xSum * xSum) * (n * ySquared - ySum * ySum));
return {
r,
r2: r * r,
chi2,
rmsd: Math.sqrt(rmsd / n),
};
}
}
exports.default = BaseRegression;
__exportStar(require("./BaseRegression"), exports);
__exportStar(require("./checkArrayLength"), exports);
__exportStar(require("./maybeToPrecision"), exports);
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":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,CAAC,CAAC,CAAC,CAAC,CAAC;SACzC;QAED,MAAM,CAAC,GACL,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;YACtB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC;QAEzE,OAAO;YACL,CAAC;YACD,EAAE,EAAE,CAAC,GAAG,CAAC;YACT,IAAI;YACJ,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;SAC1B,CAAC;IACJ,CAAC;CACF;AAvFD,iCAuFC"}
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,qDAAmC;AACnC,qDAAmC"}

@@ -7,3 +7,3 @@ /**

*/
export default function maybeToPrecision(number: number, figures?: number): string;
export declare function maybeToPrecision(number: number, figures?: number): string;
//# sourceMappingURL=maybeToPrecision.d.ts.map

@@ -1,1 +0,1 @@

{"version":3,"file":"maybeToPrecision.d.ts","sourceRoot":"","sources":["../src/maybeToPrecision.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,UAaxE"}
{"version":3,"file":"maybeToPrecision.d.ts","sourceRoot":"","sources":["../src/maybeToPrecision.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,UAahE"}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.maybeToPrecision = void 0;
/**

@@ -26,3 +27,3 @@ * Cast `number` to string. Optionally `digits` specifies significant figures.

}
exports.default = maybeToPrecision;
exports.maybeToPrecision = maybeToPrecision;
//# sourceMappingURL=maybeToPrecision.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"maybeToPrecision.js","sourceRoot":"","sources":["../src/maybeToPrecision.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,SAAwB,gBAAgB,CAAC,MAAc,EAAE,OAAgB;IACvE,IAAI,MAAM,GAAG,CAAC,EAAE;QACd,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC;QACpB,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;YAC/B,OAAO,KAAK,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;SAC3C;aAAM;YACL,OAAO,KAAK,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;SACjC;KACF;SAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QACtC,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;KACpC;SAAM;QACL,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;KAC1B;AACH,CAAC;AAbD,mCAaC"}
{"version":3,"file":"maybeToPrecision.js","sourceRoot":"","sources":["../src/maybeToPrecision.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,MAAc,EAAE,OAAgB;IAC/D,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QACf,MAAM,GAAG,CAAC,GAAG,MAAM,CAAC;QACpB,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAChC,OAAO,KAAK,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5C,CAAC;aAAM,CAAC;YACN,OAAO,KAAK,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;QAClC,CAAC;IACH,CAAC;SAAM,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QACvC,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;AACH,CAAC;AAbD,4CAaC"}
{
"name": "ml-regression-base",
"version": "3.0.0",
"version": "4.0.0",
"description": "Base class for regression modules",

@@ -40,14 +40,14 @@ "main": "./lib/index.js",

"dependencies": {
"cheminfo-types": "^1.7.2",
"cheminfo-types": "^1.7.3",
"is-any-array": "^2.0.1"
},
"devDependencies": {
"@vitest/coverage-v8": "^0.34.1",
"@vitest/coverage-v8": "^1.6.0",
"eslint": "^8.46.0",
"eslint-config-cheminfo-typescript": "^12.0.4",
"prettier": "^3.0.1",
"rimraf": "^5.0.1",
"typescript": "^5.1.6",
"vitest": "^0.34.1"
"eslint-config-cheminfo-typescript": "^12.4.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.7",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
}
}
import { expect, it, describe } from 'vitest';
import checkArrayLength from '../checkArrayLength';
import { checkArrayLength } from '..';

@@ -5,0 +5,0 @@ describe('checkArrayLength', () => {

import { expect, it, describe } from 'vitest';
import maybeToPrecision from '../maybeToPrecision';
import { maybeToPrecision } from '..';

@@ -5,0 +5,0 @@ describe('maybeToPrecision', () => {

@@ -9,3 +9,3 @@ import { NumberArray } from 'cheminfo-types';

*/
export default function checkArrayLength(x: NumberArray, y: NumberArray) {
export function checkArrayLength(x: NumberArray, y: NumberArray) {
if (!isAnyArray(x) || !isAnyArray(y)) {

@@ -12,0 +12,0 @@ throw new TypeError('x and y must be arrays');

@@ -1,103 +0,3 @@

import { type NumberArray } from 'cheminfo-types';
import { isAnyArray } from 'is-any-array';
import checkArrayLength from './checkArrayLength';
export { default as maybeToPrecision } from './maybeToPrecision';
export interface RegressionScore {
r: number;
r2: number;
chi2: number;
rmsd: number;
}
export default class BaseRegression {
constructor() {
if (new.target === BaseRegression) {
throw new Error('BaseRegression must be subclassed');
}
}
predict(x: number): number;
predict(x: NumberArray): number[];
predict(x: number | NumberArray) {
if (typeof x === 'number') {
return this._predict(x);
} else if (isAnyArray(x)) {
const y = [];
for (const xVal of x) {
y.push(this._predict(xVal));
}
return y;
} else {
throw new TypeError('x must be a number or array');
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_predict(x: number): number {
throw new Error('_predict must be implemented');
}
train() {
// Do nothing for this package
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toString(precision?: number) {
return '';
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toLaTeX(precision?: number) {
return '';
}
/**
* Return the correlation coefficient of determination (r) and chi-square.
* @param x - explanatory variable
* @param y - response variable
* @return - Object with further statistics.
*/
score(x: NumberArray, y: NumberArray): RegressionScore {
checkArrayLength(x, y);
const n = x.length;
const y2 = new Array(n);
for (let i = 0; i < n; i++) {
y2[i] = this._predict(x[i]);
}
let xSum = 0;
let ySum = 0;
let chi2 = 0;
let rmsd = 0;
let xSquared = 0;
let ySquared = 0;
let xY = 0;
for (let i = 0; i < n; i++) {
xSum += y2[i];
ySum += y[i];
xSquared += y2[i] * y2[i];
ySquared += y[i] * y[i];
xY += y2[i] * y[i];
if (y[i] !== 0) {
chi2 += ((y[i] - y2[i]) * (y[i] - y2[i])) / y[i];
}
rmsd += (y[i] - y2[i]) * (y[i] - y2[i]);
}
const r =
(n * xY - xSum * ySum) /
Math.sqrt((n * xSquared - xSum * xSum) * (n * ySquared - ySum * ySum));
return {
r,
r2: r * r,
chi2,
rmsd: Math.sqrt(rmsd / n),
};
}
}
export { checkArrayLength, type NumberArray };
export * from './BaseRegression';
export * from './checkArrayLength';
export * from './maybeToPrecision';

@@ -7,3 +7,3 @@ /**

*/
export default function maybeToPrecision(number: number, figures?: number) {
export function maybeToPrecision(number: number, figures?: number) {
if (number < 0) {

@@ -10,0 +10,0 @@ number = 0 - number;

import { expect, it, describe } from 'vitest';
import BaseRegression from '..';
class NoPredict extends BaseRegression {}
class Basic extends BaseRegression {
factor: number;
constructor(factor: number) {
super();
this.factor = factor;
}
_predict(x: number) {
return x * this.factor;
}
}
describe('base regression', () => {
it('should not be directly constructable', () => {
expect(() => {
new BaseRegression();
}).toThrow(/BaseRegression must be subclassed/);
});
it('should throw if _predict is not implemented', () => {
const reg = new NoPredict();
expect(() => {
reg.predict(0);
}).toThrow(/_predict must be implemented/);
});
it('should do a basic prediction', () => {
const basic = new Basic(2);
expect(basic.predict(1)).toBe(2);
expect(basic.predict(2)).toBe(4);
expect(basic.predict([2, 3])).toStrictEqual([4, 6]);
});
it('should throw on invalid value', () => {
const basic = new Basic(2);
expect(() => {
// @ts-expect-error testing invalid input
basic.predict();
}).toThrow(/must be a number or array/);
});
it('should implement dummy predictor functions', () => {
const basic = new Basic(2);
basic.train(); // should not throw
expect(basic.toString()).toBe('');
expect(basic.toLaTeX()).toBe('');
});
it('should implement a scoring function', () => {
const basic = new Basic(2);
expect(basic.score([1, 2], [2, 4])).toStrictEqual({
r: 1,
r2: 1,
chi2: 0,
rmsd: 0,
});
expect(basic.score([1, 2], [2, 4.1]).rmsd).toBe(0.0707106781186545);
expect(basic.score([1, 2], [0.5, 2])).toStrictEqual({
r: 1,
r2: 1,
chi2: 6.5,
rmsd: 1.7677669529663689,
});
expect(basic.score([1, 2], [0, 1])).toStrictEqual({
r: 1,
r2: 1,
chi2: 9,
rmsd: 2.5495097567963922,
});
});
it('should throw error if inputs are not arrays or has different length', () => {
const basic = new Basic(2);
expect(() => {
// @ts-expect-error testing invalid input
basic.score(1, 2);
}).toThrow('x and y must be arrays');
expect(() => {
basic.score([1, 2], [2]);
}).toThrow('x and y arrays must have the same length');
});
});