-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_computer.py
50 lines (33 loc) · 1.51 KB
/
error_computer.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from itertools import permutations, combinations
import numpy as np
class ErrorComputer:
def compute_error(self, changepoints, estimates):
output = []
changepoints = np.array(changepoints)
estimates = np.array(estimates)
for i in range(1, len(changepoints) + 1):
if len(estimates) >= i:
output.append(self.get_smallest_error(changepoints, estimates, i) / i)
else:
output.append(-1)
return output
def get_smallest_error(self, changepoints, estimates, subset_size):
estimate_combinations = list(combinations(estimates, subset_size))
smallest_error = np.inf
for combination in estimate_combinations:
error = self.combination_error(changepoints, combination)
if smallest_error > error:
smallest_error = error
return smallest_error
def combination_error(self, changepoints, estimates):
permutations_array = permutations(range(len(changepoints)))
smallest_error = np.inf
for permutation in permutations_array:
error = self.permutation_error(changepoints, estimates, permutation)
if smallest_error > error:
smallest_error = error
return smallest_error
def permutation_error(self, changepoints, estimates, permutation):
permutation = np.array(permutation)
error = np.sum(np.abs(changepoints[permutation][:len(estimates)] - estimates))
return error