Skip to content

Commit

Permalink
feat(formula): add t.test/trimmean/weibull.dist/z.test function
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and wpxp123456 committed Oct 15, 2024
1 parent ef32f92 commit f5875e5
Show file tree
Hide file tree
Showing 37 changed files with 2,388 additions and 740 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ import { QuartileInc } from '../statistical/quartile-inc';
import { StdevP } from '../statistical/stdev-p';
import { StdevS } from '../statistical/stdev-s';
import { TInv2t } from '../statistical/t-inv-2t';
import { TTest } from '../statistical/t-test';
import { VarP } from '../statistical/var-p';
import { VarS } from '../statistical/var-s';
import { WeibullDist } from '../statistical/weibull-dist';
import { ZTest } from '../statistical/z-test';
import { Betadist } from './betadist';
import { FUNCTION_NAMES_COMPATIBILITY } from './function-names';
import { Hypgeomdist } from './hypgeomdist';
Expand Down Expand Up @@ -85,6 +88,9 @@ export const functionCompatibility = [
[StdevP, FUNCTION_NAMES_COMPATIBILITY.STDEVP],
[Tdist, FUNCTION_NAMES_COMPATIBILITY.TDIST],
[TInv2t, FUNCTION_NAMES_COMPATIBILITY.TINV],
[TTest, FUNCTION_NAMES_COMPATIBILITY.TTEST],
[VarS, FUNCTION_NAMES_COMPATIBILITY.VAR],
[VarP, FUNCTION_NAMES_COMPATIBILITY.VARP],
[WeibullDist, FUNCTION_NAMES_COMPATIBILITY.WEIBULL],
[ZTest, FUNCTION_NAMES_COMPATIBILITY.ZTEST],
];
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ import { TDist2t } from './t-dist-2t';
import { TDistRt } from './t-dist-rt';
import { TInv } from './t-inv';
import { TInv2t } from './t-inv-2t';
import { TTest } from './t-test';
import { Trimmean } from './trimmean';
import { VarP } from './var-p';
import { VarS } from './var-s';
import { Vara } from './vara';
import { Varpa } from './varpa';
import { WeibullDist } from './weibull-dist';
import { ZTest } from './z-test';

export const functionStatistical = [
[Avedev, FUNCTION_NAMES_STATISTICAL.AVEDEV],
Expand Down Expand Up @@ -211,8 +215,12 @@ export const functionStatistical = [
[TDistRt, FUNCTION_NAMES_STATISTICAL.T_DIST_RT],
[TInv, FUNCTION_NAMES_STATISTICAL.T_INV],
[TInv2t, FUNCTION_NAMES_STATISTICAL.T_INV_2T],
[TTest, FUNCTION_NAMES_STATISTICAL.T_TEST],
[Trimmean, FUNCTION_NAMES_STATISTICAL.TRIMMEAN],
[VarP, FUNCTION_NAMES_STATISTICAL.VAR_P],
[VarS, FUNCTION_NAMES_STATISTICAL.VAR_S],
[Vara, FUNCTION_NAMES_STATISTICAL.VARA],
[Varpa, FUNCTION_NAMES_STATISTICAL.VARPA],
[WeibullDist, FUNCTION_NAMES_STATISTICAL.WEIBULL_DIST],
[ZTest, FUNCTION_NAMES_STATISTICAL.Z_TEST],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';

import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../../engine/value-object/primitive-object';
import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_STATISTICAL } from '../../function-names';
import { TTest } from '../index';

describe('Test tTest function', () => {
const testFunction = new TTest(FUNCTION_NAMES_STATISTICAL.T_TEST);

describe('TTest', () => {
it('Value is normal', () => {
const array1 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[3, 4, 5, 8, 9, 1, 2, 4, 5],
]),
rowCount: 1,
columnCount: 9,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const array2 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[6, 19, 3, 2, 14, 4, 5, 17, 1],
]),
rowCount: 1,
columnCount: 9,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const tails = NumberValueObject.create(2);
const type = NumberValueObject.create(1);
const result = testFunction.calculate(array1, array2, tails, type);
expect(getObjectValue(result)).toBe(0.19601578492529903);
});

it('Array1 and array2 value test', () => {
let array1 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[0, 1, 2, 3, null],
]),
rowCount: 1,
columnCount: 5,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
let array2 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[0.2, 0.3, 0.1, 0.4],
]),
rowCount: 1,
columnCount: 4,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const tails = NumberValueObject.create(2);
const type = NumberValueObject.create(1);
let result = testFunction.calculate(array1, array2, tails, type);
expect(getObjectValue(result)).toBe(ErrorType.NA);

array1 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[null],
]),
rowCount: 1,
columnCount: 1,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
array2 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[null],
]),
rowCount: 1,
columnCount: 1,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
result = testFunction.calculate(array1, array2, tails, type);
expect(getObjectValue(result)).toBe(ErrorType.VALUE);

array1 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[null, true, false, 'test'],
]),
rowCount: 1,
columnCount: 4,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
array2 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[0.2, 0.3, 0.1, 0.4],
]),
rowCount: 1,
columnCount: 4,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
result = testFunction.calculate(array1, array2, tails, type);
expect(getObjectValue(result)).toBe(ErrorType.DIV_BY_ZERO);

array1 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[ErrorType.NAME, 1, 2, 3],
]),
rowCount: 1,
columnCount: 4,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
result = testFunction.calculate(array1, array2, tails, type);
expect(getObjectValue(result)).toBe(ErrorType.NAME);

const array3 = ErrorValueObject.create(ErrorType.NAME);
const array4 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[0.2, 0.3, 0.1, 0.4],
]),
rowCount: 1,
columnCount: 4,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const result2 = testFunction.calculate(array3, array4, tails, type);
expect(getObjectValue(result2)).toBe(ErrorType.NAME);

const array5 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[0, 1, 2, 3],
]),
rowCount: 1,
columnCount: 4,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const array6 = ErrorValueObject.create(ErrorType.NAME);
const result3 = testFunction.calculate(array5, array6, tails, type);
expect(getObjectValue(result3)).toBe(ErrorType.NAME);
});

it('Tails value test', () => {
const array1 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[3, 4, 5, 8, 9, 1, 2, 4, 5],
]),
rowCount: 1,
columnCount: 9,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const array2 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[6, 19, 3, 2, 14, 4, 5, 17, 1],
]),
rowCount: 1,
columnCount: 9,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const tails = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[-1, 0, 1, 2, 3, true, false, null, 'test', ErrorType.NAME],
]),
rowCount: 1,
columnCount: 10,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const type = NumberValueObject.create(1);
const result = testFunction.calculate(array1, array2, tails, type);
expect(getObjectValue(result)).toStrictEqual([
[ErrorType.NUM, ErrorType.NUM, 0.09800789246264952, 0.19601578492529903, ErrorType.NUM, 0.09800789246264952, ErrorType.NUM, ErrorType.NUM, ErrorType.VALUE, ErrorType.NAME],
]);
});

it('Type Value is normal', () => {
const array1 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[3, 4, 5, 8, 9, 1, 2, 4, 5],
]),
rowCount: 1,
columnCount: 9,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const array2 = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[6, 19, 3, 2, 14, 4, 5, 17, 1],
]),
rowCount: 1,
columnCount: 9,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const tails = NumberValueObject.create(2);
const type = ArrayValueObject.create({
calculateValueList: transformToValueObject([
[-1, 0, 1, 2, 3, true, false, null, 'test', ErrorType.NAME],
]),
rowCount: 1,
columnCount: 10,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const result = testFunction.calculate(array1, array2, tails, type);
expect(getObjectValue(result)).toStrictEqual([
[ErrorType.NUM, ErrorType.NUM, 0.19601578492529903, 0.1919958867535514, 0.20229392337016794, 0.19601578492529903, ErrorType.NUM, ErrorType.NUM, ErrorType.VALUE, ErrorType.NAME],
]);
});
});
});
Loading

0 comments on commit f5875e5

Please sign in to comment.