-
Notifications
You must be signed in to change notification settings - Fork 0
/
multivariate.py
69 lines (53 loc) · 2.12 KB
/
multivariate.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import scipy.stats._multivariate as mv
import scipy as sp
import math
_LOG_2PI = mv._LOG_2PI
def multi_gaussian(x, P):
# x is a N dimensional vector
return math.exp(-0.5*x.dot(np.linalg.solve(P, x.T)))/math.sqrt(np.linalg.det(P)*(2*math.pi)**x.shape[0])
def log_pdf(x, mean, prec_U, log_det_cov, rank, out=None, x_copy=None):
x = np.subtract(x, mean, out=x_copy) # dev
np.dot(x, prec_U, out=x)
np.square(x, out=x)
y = np.sum(x, axis=-1, out=out) # maha
np.add(y, (rank * mv._LOG_2PI + log_det_cov), out=y)
np.multiply(y, -0.5, out=y)
return y
def pdf(x, mean, cov, out=None, x_copy=None):
psd = mv._PSD(cov, allow_singular=False)
y = log_pdf(x, mean, psd.U, psd.log_pdet, psd.rank, out, x_copy)
return np.exp(y, out=y)
class Multivariate:
def __init__(self, mean, cov):
self.mean = mean
self.cov = cov
self.psd = mv._PSD(cov, allow_singular=False)
def pdf(self, x, out=None, x_copy=None):
y = log_pdf(x, self.mean, self.psd.U, self.psd.log_pdet, self.psd.rank, out, x_copy)
return np.exp(y, out=y)
def pdf_bivariate(x, y, mx, my, var_x, var_y, out=None, x_copy=None, y_copy=None):
x = np.subtract(x, mx, out=x_copy)
np.square(x, out=x)
np.multiply(x, -1 / (2 * var_x), out=x)
y = np.subtract(y, my, out=y_copy)
np.square(y, out=y)
np.multiply(y, -1 / (2 * var_y), out=y)
#pos = np.add(x, y, out=out)
pos = np.add(x, y, out=out)
np.exp(pos, out=pos)
np.multiply(pos, 1 / (2 * np.pi * math.sqrt(var_x * var_y)), out=pos)
return pos
def pdf_bivariate2(x, y, mx, my, var_x, var_y, out=None, x_copy=None, y_copy=None):
x = np.subtract(x, mx, out=x_copy)
np.square(x, out=x)
np.multiply(x, -1 / (2 * var_x), out=x)
np.exp(x, out=x)
y = np.subtract(y, my, out=y_copy)
np.square(y, out=y)
np.multiply(y, -1 / (2 * var_y), out=y)
np.exp(y, out=y)
np.multiply(x, 1 / (2 * np.pi * math.sqrt(var_x * var_y)), out=x)
pos = np.multiply(x, y, out=out)
#print("x: {} y: {}, Expected out: {}, reality: {}".format(x.shape, y.shape, out.shape, pos.shape))
return pos