-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathonline_background.py
More file actions
109 lines (86 loc) · 2.84 KB
/
online_background.py
File metadata and controls
109 lines (86 loc) · 2.84 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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.lines import Line2D
np.random.seed(42)
n_samples = 5000
MU = np.array([0.5, 1.5])
COV = np.array([[1., 0.7], [0.7, 2.]])
def get_samples(n):
return np.random.multivariate_normal(mean=MU, cov=COV, size=n)
class BackgroundCheck(object):
def __init__(self, model):
self.model = model
def fit(self, x):
self.model.fit(x)
def prob_foreground(self, x):
l = self.model.likelihood(x)
l_max = self.model.max
return np.true_divide(l, l_max)
def prob_background(self, x):
return 1 - self.prob_foreground(x)
def predict_proba(self, x):
return self.prob_background(x)
class GaussianEstimation(object):
def __init__(self):
self.mu = None
self.cov = None
self.N = 0
def fit(self, x):
N = x.shape[1]
mu = np.mean(x, axis=0)
cov = np.cov(x, rowvar=False)
if self.N is 0:
self.N = N
self.mu = mu
self.k = len(mu)
self.cov = cov
else:
self.mu = np.true_divide((self.mu * self.N) + (mu * N), self.N + N)
self.cov = np.true_divide((self.cov * self.N) + (cov * N), self.N + N)
self.N += N
def likelihood(self, x):
return np.exp(self.log_likelihood(x))
def log_likelihood(self, x):
x_mu = x - self.mu
# a = np.array([[1, 2]])
# b = np.array([[1, 2],[3,4]])
# np.inner(np.inner(a, b.T), a)
inverse = np.linalg.inv(self.cov)
exp = np.array([np.inner(np.inner(a, inverse.T), a) for a in x_mu])
return - 0.5 * (
np.log(np.linalg.det(self.cov))
+ exp \
+ self.k * np.log(2*np.pi)
)
@property
def max(self):
return self.likelihood(self.mu.reshape(1,-1))
model = BackgroundCheck(GaussianEstimation())
for i in range(n_samples/2):
x = get_samples(2)
model.fit(x)
x = get_samples(n_samples)
p_foreground = 1 - model.predict_proba(x)
fig = plt.figure('scatter')
fig.clf()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x[:,0], x[:,1], p_foreground)
ax.set_xlabel('$x_0$')
ax.set_ylabel('$x_1$')
ax.set_zlabel('p_foreground')
fig.savefig('p_foreground_x.svg')
X = np.linspace(min(x[:,0]), max(x[:,0]), 30)
Y = np.linspace(min(x[:,1]), max(x[:,1]), 30)
X, Y = np.meshgrid(X, Y)
grid = np.concatenate((X.reshape(-1,1), Y.reshape(-1,1)), axis=1)
p_foreground = 1 - model.predict_proba(grid).reshape(X.shape[0], X.shape[1])
fig = plt.figure('surface')
fig.clf()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, p_foreground, cmap=cm.coolwarm)
ax.set_xlabel('$x_0$')
ax.set_ylabel('$x_1$')
ax.set_zlabel('p_foreground')
fig.savefig('p_foreground_grid.svg')