-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkde.py
26 lines (22 loc) · 892 Bytes
/
kde.py
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
# © 2022 Joseph Craig <[email protected]>
# This code is not released under a standard OSS license. Please read README.md.
import numpy as np
import scipy.stats
def generate_density_estimation(performance: np.ndarray, minimum: int, maximum: int, steps: int = 5000):
xs = []
ys = []
n = len(performance)
h = _estimate_bandwidth(performance)
coefficient = 1. / (h * np.sqrt(2 * np.pi))
step_size = (maximum - minimum) / float(steps)
for i in range(steps + 1):
x = minimum + i * step_size
xs.append(x)
y = np.sum(np.exp(-0.5 * ((x - performance) / h) ** 2) * coefficient) / n
ys.append(y)
return xs, ys
def _estimate_bandwidth(performance: np.ndarray):
s = np.std(performance)
iqr = scipy.stats.iqr(performance)
coefficient = len(performance) ** (-1. / 5.)
return 0.9 * min(s, iqr / 1.34) * coefficient