File size: 868 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// configuration for a set of data points
// strings are descriptors for the set
// numbers are used for synthetic data generation)
type BoxPlotConfig = {
    group: string
    subgroup?: string
    mu: number
    sd: number
    n: number
}

// create a random number from a distribution that has a mean and some spread
// (uses Math.atanh instead of normal distribution - only meant for test cases)
const randomValue = (mu = 0, sigma = 1) => {
    const z = (Math.random() - 0.5) * 2
    return Math.atanh(z) * sigma + mu
}

export const generateBoxPlotData = (config: BoxPlotConfig[]) => {
    return config
        .map(x => {
            const values = Array(x.n)
                .fill(0)
                .map(() => randomValue(x.mu, x.sd))
            return values.map(v => {
                return { ...x, value: v }
            })
        })
        .flat()
}