Skip to content

Commit

Permalink
fix(formula): fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and wpxp123456 committed Oct 31, 2024
1 parent 0bee8bf commit eab14e2
Show file tree
Hide file tree
Showing 27 changed files with 359 additions and 107 deletions.
4 changes: 2 additions & 2 deletions packages/engine-formula/src/basics/statistical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,9 @@ export function poissonPDF(x: number, mean: number): number {
}

export function studentTCDF(x: number, degFreedom: number): number {
const sqrt = Math.sqrt(x ** 2 + degFreedom);
const result = 0.5 * incompleteBetaFunction(degFreedom / (x ** 2 + degFreedom), degFreedom / 2, 0.5);

return incompleteBetaFunction((x + sqrt) / (2 * sqrt), degFreedom / 2, degFreedom / 2);
return x < 0 ? result : 1 - result;
}

export function studentTPDF(x: number, degFreedom: number): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Test tdist function', () => {
const degFreedom = NumberValueObject.create(2);
const tails = NumberValueObject.create(1);
const result = testFunction.calculate(x, degFreedom, tails);
expect(getObjectValue(result)).toBe(0.21132486540518702);
expect(getObjectValue(result)).toBe(0.2113248654052009);
});

it('DegFreedom value test', () => {
Expand All @@ -53,7 +53,7 @@ describe('Test tdist function', () => {
const degFreedom = NumberValueObject.create(2);
const tails = NumberValueObject.create(2);
const result = testFunction.calculate(x, degFreedom, tails);
expect(getObjectValue(result)).toBe(0.42264973081037405);
expect(getObjectValue(result)).toBe(0.4226497308104018);

const tails2 = NumberValueObject.create(0);
const result2 = testFunction.calculate(x, degFreedom, tails2);
Expand All @@ -73,15 +73,15 @@ describe('Test tdist function', () => {
const degFreedom = NumberValueObject.create(2);
const tails = NumberValueObject.create(1);
const result = testFunction.calculate(x, degFreedom, tails);
expect(getObjectValue(result)).toBe(0.21132486540518702);
expect(getObjectValue(result)).toBe(0.2113248654052009);
});

it('Value is null', () => {
const x = NullValueObject.create();
const degFreedom = NumberValueObject.create(2);
const tails = NumberValueObject.create(1);
const result = testFunction.calculate(x, degFreedom, tails);
expect(getObjectValue(result)).toBe(0.4999999999999998);
expect(getObjectValue(result)).toBe(0.5);
});

it('Value is error', () => {
Expand Down Expand Up @@ -118,9 +118,17 @@ describe('Test tdist function', () => {
const tails = NumberValueObject.create(1);
const result = testFunction.calculate(x, degFreedom, tails);
expect(getObjectValue(result)).toStrictEqual([
[0.21132486540518702, ErrorType.VALUE, 0.1718728078319529, 0.21132486540518702, 0.4999999999999998, 0.4999999999999998],
[0.4999999999999998, 0.00004999250124970711, 0.07207975447577919, ErrorType.VALUE, ErrorType.NUM, ErrorType.NAME],
[0.2113248654052009, ErrorType.VALUE, 0.1718728077748428, 0.2113248654052009, 0.5, 0.5],
[0.5, 0.00004999250124977886, 0.07207975447515451, ErrorType.VALUE, ErrorType.NUM, ErrorType.NAME],
]);
});

it('More test', () => {
const x = NumberValueObject.create(0.1);
const degFreedom = NumberValueObject.create(900000000);
const tails = NumberValueObject.create(1);
const result = testFunction.calculate(x, degFreedom, tails);
expect(getObjectValue(result)).toBe(0.4601721457389526);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../basics/error-type';
import { studentTCDF } from '../../../basics/statistical';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';

export class Tdist extends BaseFunction {
override minParams = 3;
Expand Down Expand Up @@ -96,7 +96,7 @@ export class Tdist extends BaseFunction {
return ErrorValueObject.create(ErrorType.NUM);
}

let result = 1 - studentTCDF(xValue, degFreedomValue);
let result = studentTCDF(-xValue, degFreedomValue);

if (tailsValue === 2) {
result *= 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Test percentrankExc function', () => {
column: 0,
});
const result2 = testFunction.calculate(array2, x);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.NUM);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.NA);

const array3 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
Expand Down Expand Up @@ -191,5 +191,23 @@ describe('Test percentrankExc function', () => {
[0.46, 0.46572, 0.4657272727272],
]);
});

it('More test', () => {
const array = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[null, 'test'],
]),
rowCount: 1,
columnCount: 2,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const x = StringValueObject.create('test');
const significance = NullValueObject.create();
const result = testFunction.calculate(array, x, significance);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class PercentrankExc extends BaseFunction {

const n = array.length;

if (xValue < array[0] || xValue > array[n - 1]) {
if (n === 0 || xValue < array[0] || xValue > array[n - 1]) {
return ErrorValueObject.create(ErrorType.NA);
}

Expand Down Expand Up @@ -160,10 +160,6 @@ export class PercentrankExc extends BaseFunction {
}
}

if (values.length === 0) {
return ErrorValueObject.create(ErrorType.NUM);
}

return values.sort((a, b) => a - b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Test percentrankInc function', () => {
column: 0,
});
const result2 = testFunction.calculate(array2, x);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.NUM);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.NA);

const array3 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
Expand Down Expand Up @@ -191,5 +191,23 @@ describe('Test percentrankInc function', () => {
[0.45, 0.45811, 0.4581111111111],
]);
});

it('More test', () => {
const array = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[null, 'test'],
]),
rowCount: 1,
columnCount: 2,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const x = StringValueObject.create('test');
const significance = NullValueObject.create();
const result = testFunction.calculate(array, x, significance);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class PercentrankInc extends BaseFunction {

const n = array.length;

if (xValue < array[0] || xValue > array[n - 1]) {
if (n === 0 || xValue < array[0] || xValue > array[n - 1]) {
return ErrorValueObject.create(ErrorType.NA);
}

Expand Down Expand Up @@ -160,10 +160,6 @@ export class PercentrankInc extends BaseFunction {
}
}

if (values.length === 0) {
return ErrorValueObject.create(ErrorType.NUM);
}

return values.sort((a, b) => a - b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../basics/error-type';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';

export class Permut extends BaseFunction {
override minParams = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,20 @@ describe('Test permutationa function', () => {
const numberChosen = NumberValueObject.create(2);
const result = testFunction.calculate(number, numberChosen);
expect(getObjectValue(result)).toStrictEqual([
[ErrorType.NUM, ErrorType.VALUE, ErrorType.NUM, ErrorType.NUM, ErrorType.NUM, ErrorType.NUM],
[ErrorType.NUM, 10000, 4, ErrorType.VALUE, ErrorType.NUM, ErrorType.NAME],
[1, ErrorType.VALUE, 1, 1, 0, 0],
[0, 10000, 4, ErrorType.VALUE, ErrorType.NUM, ErrorType.NAME],
]);
});

it('More test', () => {
const number = NumberValueObject.create(0);
const numberChosen = NumberValueObject.create(0);
const result = testFunction.calculate(number, numberChosen);
expect(getObjectValue(result)).toBe(1);

const numberChosen2 = NumberValueObject.create(1);
const result2 = testFunction.calculate(number, numberChosen2);
expect(getObjectValue(result2)).toBe(0);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../basics/error-type';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';

export class Permutationa extends BaseFunction {
override minParams = 2;
Expand Down Expand Up @@ -63,10 +63,19 @@ export class Permutationa extends BaseFunction {
const numberValue = Math.floor(+_numberObject.getValue());
const numberChosenValue = Math.floor(+_numberChosenObject.getValue());

if (numberValue < 0 || numberValue >= 2147483647 || numberChosenValue < 0 || numberValue < numberChosenValue) {
if (numberValue < 0 || numberValue >= 2147483647 || numberChosenValue < 0) {
return ErrorValueObject.create(ErrorType.NUM);
}

// for excel
if (numberValue === 0) {
if (numberChosenValue === 0) {
return NumberValueObject.create(1);
} else {
return NumberValueObject.create(0);
}
}

const result = numberValue ** numberChosenValue;

if (!Number.isFinite(result)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';

export class Phi extends BaseFunction {
override minParams = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,27 @@ describe('Test poissonDist function', () => {
[0.006737946999085467, 1, 0.12465201948308113, ErrorType.VALUE, ErrorType.NUM, ErrorType.NAME],
]);
});

it('More test', () => {
const x = NumberValueObject.create(17.16017);
const mean = NumberValueObject.create(0.1);
const cumulative = BooleanValueObject.create(false);
const result = testFunction.calculate(x, mean, cumulative);
expect(getObjectValue(result)).toBe(2.543911722940471e-32);

const mean2 = NumberValueObject.create(2);
const result2 = testFunction.calculate(x, mean2, cumulative);
expect(getObjectValue(result2)).toBe(4.987150189520231e-11);

const x2 = NumberValueObject.create(0.25);
const mean3 = NumberValueObject.create(0);
const result3 = testFunction.calculate(x2, mean3, cumulative);
expect(getObjectValue(result3)).toBe(1);

const x3 = NumberValueObject.create(139.7316);
const mean4 = NumberValueObject.create(161);
const result4 = testFunction.calculate(x3, mean4, cumulative);
expect(getObjectValue(result4)).toBe(0.00698880992304486);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../basics/error-type';
import { poissonCDF, poissonPDF } from '../../../basics/statistical';
import { expandArrayValueObject } from '../../../engine/utils/array-object';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';

export class PoissonDist extends BaseFunction {
override minParams = 3;
Expand Down Expand Up @@ -88,7 +88,7 @@ export class PoissonDist extends BaseFunction {

const [_xObject, _meanObject, _cumulativeObject] = variants as BaseValueObject[];

const xValue = +_xObject.getValue();
const xValue = Math.floor(+_xObject.getValue());
const meanValue = +_meanObject.getValue();
const cumulativeValue = +_cumulativeObject.getValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../basics/error-type';
import { getTwoArrayNumberValues } from '../../../basics/statistical';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';

export class Rsq extends BaseFunction {
override minParams = 2;
Expand Down Expand Up @@ -57,8 +57,6 @@ export class Rsq extends BaseFunction {
if (_array1.isNull() || _array2.isNull()) {
return ErrorValueObject.create(ErrorType.VALUE);
}

return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
}

if (array1RowCount * array1ColumnCount !== array2RowCount * array2ColumnCount) {
Expand Down Expand Up @@ -91,6 +89,10 @@ export class Rsq extends BaseFunction {
}

private _getResult(array1: number[], array2: number[]): BaseValueObject {
if (array1.length === 0) {
return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
}

const n = array1.length;

let sumX = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Test small function', () => {
});
const k = NumberValueObject.create(2.5);
const result = testFunction.calculate(array, k);
expect(getObjectValue(result)).toStrictEqual(3);
expect(getObjectValue(result)).toStrictEqual(2);
});

it('Value is number string', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Small extends BaseFunction {
return ErrorValueObject.create(ErrorType.NUM);
}

kValue = Math.ceil(kValue);
kValue = Math.floor(kValue);

return NumberValueObject.create(array[kValue - 1]);
}
Expand Down
Loading

0 comments on commit eab14e2

Please sign in to comment.