-
Notifications
You must be signed in to change notification settings - Fork 37
/
kdeDistributionBuilder.ts
77 lines (68 loc) · 2.11 KB
/
kdeDistributionBuilder.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
import { FieldAccessor } from '../../interfaces'
import { DatasetManager } from '../dataset'
import { DistributionBuilder } from './interfaces'
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const { field: vegaField } = require('vega-util')
/**
* Represents a kernel density estimate for a set of numerical values. This
* method uses a Gaussian kernel to estimate a smoothed, continuous probability
* distribution.
*/
export interface KdeDistributionBuilder extends DistributionBuilder {
/**
* The name of the data set to analyze.
* @param name
*/
from(name: string): KdeDistributionBuilder
/**
* The data field containing the values to model.
* @param field
*/
field(field: FieldAccessor): KdeDistributionBuilder
/**
* An optional parameter that determines the width of the Gaussian kernel.
* If set to 0 (the default), the bandwidth value will be automatically
* estimated from the input data.
* @param value
*/
bandwidth(value: number): KdeDistributionBuilder
}
export class KdeDistributionBuilderImpl implements KdeDistributionBuilder {
private fromValue: string | undefined
private fieldValue: FieldAccessor | undefined
private bandwidthValue: number | undefined
public from(value: string): KdeDistributionBuilder {
this.fromValue = value
return this
}
public field(field: FieldAccessor): KdeDistributionBuilder {
this.fieldValue = field
return this
}
public bandwidth(value: number): KdeDistributionBuilder {
this.bandwidthValue = value
return this
}
public build(ds: DatasetManager) {
// TODO: wire in source
if (!this.fromValue) {
throw new Error('kde distribution must define a source table')
}
if (!this.fieldValue) {
throw new Error('kde distribution must define a field value')
}
return {
function: 'kde',
from: ds.getTable(this.fromValue),
field: vegaField(this.fieldValue),
bandwidth: this.bandwidthValue,
}
}
}
export function kde(): KdeDistributionBuilder {
return new KdeDistributionBuilderImpl()
}