-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.js
More file actions
159 lines (143 loc) · 5.09 KB
/
models.js
File metadata and controls
159 lines (143 loc) · 5.09 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
export function isNumericArray(xs, l) {
let expectedLength = l ?? xs.length;
return Array.isArray(xs) && xs.length === expectedLength && xs.length > 0 && xs.every(Number.isFinite);
}
export class Measurements {
constructor(iters, times, tukey) {
this.iters = iters;
this.times = times;
this.tukey = tukey;
}
get averages() {
return this.times.map((t, i) => t / this.iters[i])
}
static parse(pojo) {
let {iters, times, tukey} = pojo;
if (!isNumericArray(iters)) {
throw new TypeError('expected `measurements.iters` to be a numeric array, was:' + JSON.stringify(tukey));
}
if (!isNumericArray(times, iters.length)) {
throw new TypeError('expected `measurements.times` to be a numeric array');
}
if (!isNumericArray(tukey, 4)) {
throw new TypeError('expected `measurements.tukey` to be a numeric array, was:' + JSON.stringify(tukey));
}
return new this(iters, times, tukey)
}
}
class Estimates {
constructor(confidenceLevel, lowerBound, upperBound, standardError, pointEstimate) {
this.confidenceLevel = confidenceLevel;
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.standardError = standardError;
this.pointEstimate = pointEstimate;
}
static parse(pojo) {
let {
confidenceLevel,
lowerBound,
upperBound,
standardError,
pointEstimate
} = pojo;
for (let p of [confidenceLevel, lowerBound, upperBound, standardError, pointEstimate]) {
if (!Number.isFinite(p)) {
throw new TypeError(`Expected '${p}' to be a number, was '${typeof p}'`)
}
}
return new this(confidenceLevel, lowerBound, upperBound, standardError, pointEstimate);
}
}
class Statistic {
constructor(estimates, bootstrap) {
this.estimates = estimates;
this.bootstrap = bootstrap;
}
static parse(name, pojo) {
let knownStatistics = ['mean', 'median', 'medianAbsDev', 'slope', 'stdDev'];
if (!knownStatistics.includes(name)) {
throw new TypeError(`Unknown statistic: ${name}`);
}
let {estimates, bootstrap} = pojo;
if (!isNumericArray(pojo.bootstrap)) {
throw new TypeError(`Expected bootstrap to be a numeric array`)
}
return new this(Estimates.parse(estimates), bootstrap);
}
}
class Statistics {
constructor(mean, median, medianAbsDev, slope, stdDev) {
this.mean = mean;
this.median = median;
this.medianAbsDev = medianAbsDev;
this.slope = slope;
this.stdDev = stdDev;
}
static parse(pojo) {
let {mean, median, medianAbsDev, slope, stdDev} = pojo;
return new this(
Statistic.parse('mean', mean),
Statistic.parse('median', median),
Statistic.parse('medianAbsDev', medianAbsDev),
Statistic.parse('slope', slope),
Statistic.parse('stdDev', stdDev)
);
}
static build(estimates, distributions) {
let {
mean,
median,
medianAbsDev,
slope,
stdDev
} = Object.fromEntries(Object.keys(estimates).map(statistic =>
[
statistic, new Statistic(new Estimates(
estimates[statistic].confidenceInterval.confidenceLevel,
estimates[statistic].confidenceInterval.lowerBound,
estimates[statistic].confidenceInterval.upperBound,
estimates[statistic].standardError,
estimates[statistic].pointEstimate,
),
distributions[statistic].numbers
)]));
return new this(mean, median, medianAbsDev, slope, stdDev);
}
}
export class ReportData {
constructor(groupId, functionId, measurements, statistics) {
this.groupId = groupId;
this.functionId = functionId;
this.measurements = measurements;
this.statistics = statistics;
}
get title() {
return `${this.groupId}/${this.functionId}`;
}
static build(id, iters, times, tukey, estimates, distributions) {
let groupId = id.groupId;
let functionId = id.functionId;
return new this(
groupId,
functionId,
new Measurements(iters, times, tukey),
Statistics.build(estimates, distributions)
);
}
static parse(pojo) {
let {groupId, functionId, measurements, statistics} = pojo;
if (typeof groupId !== 'string') {
throw new Error(`expected \`groupId\` (${groupId}) to be 'string', was '${typeof groupId}'`);
}
if (typeof functionId !== 'string') {
throw new Error(`expected \`functionId\` (${functionId}) to be 'string', was '${typeof functionId}'`);
}
return new this(
groupId,
functionId,
Measurements.parse(measurements),
Statistics.parse(statistics)
);
}
}