-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathksmetric.py
More file actions
68 lines (49 loc) · 1.96 KB
/
ksmetric.py
File metadata and controls
68 lines (49 loc) · 1.96 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
"""
A few functions for calculating the discrete Kolmogorov-Smirnov distance
between observed_data and data from a probability model. typically
observed_data would be ones and zeros while model_data would be a probability
field.
"""
import numpy as np
import matplotlib.pyplot as plt
def ks_metric(observed_data,model_data,sorder=None):
strout = "samples must have the same shape"
assert observed_data.shape == model_data.shape, strout
od = observed_data.flatten()
md = model_data.flatten()
if sorder is None:
sorder = md.argsort()
residual = od-md
ksm = np.max(np.abs(np.cumsum(residual[sorder])))
return ksm
def ks_metric_L1(observed_data,model_data,sorder=None):
strout = "samples must have the same shape"
assert observed_data.shape == model_data.shape, strout
od = observed_data.flatten()
md = model_data.flatten()
if sorder is None:
sorder = md.argsort()
residual = od-md
ksm = np.mean(np.abs(np.cumsum(residual[sorder])))
return ksm
def realize(model):
return np.ones(model.shape) * (np.random.rand(*model.shape) < model)
def ksm_plot(X,Xhat,sorder=None):
if sorder is None:
sorder = Xhat.flatten().argsort()
print "L1: {} KS: {} L1KS: {} Kuiper: {}".format(np.sum(np.abs(X-Xhat)),
ks_metric(X,Xhat,sorder),
ks_metric_L1(X,Xhat,sorder),
kuiper(X,Xhat,sorder))
plt.plot(np.cumsum((Xhat-X).flatten()[sorder]))
def kuiper(observed_data,model_data,sorder=None):
strout = "samples must have the same shape"
assert observed_data.shape == model_data.shape, strout
od = observed_data.flatten()
md = model_data.flatten()
if sorder is None:
sorder = md.argsort()
residual = od-md
cs = np.cumsum(residual[sorder])
k = np.max(cs) - np.min(cs)
return k