aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/dynamic/CohensDTool.ts
blob: 51cadeb6d51e7720b1c79c9d27ae9c038af9e1e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Observation } from '../../types/types';
import { ParametersType, ToolInfo } from '../../types/tool_types';
import { BaseTool } from '../BaseTool';

const cohensDToolParams = [
                    {
                        name: 'meandifference',
                        type: 'number',
                        description: 'The difference between the means of two groups',
                        required: true
                    },
                    {
                        name: 'standarddeviation',
                        type: 'number',
                        description: 'The pooled standard deviation of the two groups',
                        required: true
                    },
                    {
                        name: 'samplesize1',
                        type: 'number',
                        description: 'The sample size of the first group',
                        required: true
                    },
                    {
                        name: 'samplesize2',
                        type: 'number',
                        description: 'The sample size of the second group',
                        required: true
                    }
                ] as const;

                type CohensDToolParamsType = typeof cohensDToolParams;

                const cohensDToolInfo: ToolInfo<CohensDToolParamsType> = {
                    name: 'cohensdtool',
                    description: 'Calculates Cohen\'s d for effect size and determines statistical significance levels.',
                    citationRules: 'No citation needed.',
                    parameterRules: cohensDToolParams
                };

                export class CohensDTool extends BaseTool<CohensDToolParamsType> {
                    constructor() {
                        super(cohensDToolInfo);
                    }

                    async execute(args: ParametersType<CohensDToolParamsType>): Promise<Observation[]> {
                        const { meandifference, standarddeviation, samplesize1, samplesize2 } = args;
                        const pooledSD = Math.sqrt(((samplesize1 - 1) * Math.pow(standarddeviation, 2) + (samplesize2 - 1) * Math.pow(standarddeviation, 2)) / (samplesize1 + samplesize2 - 2));
                        const cohensD = meandifference / pooledSD;
                        return [{ type: 'text', text: `Cohen's d: ${cohensD.toFixed(3)}` }];
                    }
                }